Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
MCP2515Configurator.cpp
Go to the documentation of this file.
1
24
26#include <chrono>
27#include <thread>
28
37
46 std::this_thread::sleep_for(std::chrono::milliseconds(10));
47 uint8_t status = readRegister(CANSTAT);
48 return (status & 0xE0) == 0x80; // Verify configuration mode
49}
50
56 writeRegister(CNF1, 0x00); // Set BRP (Baud Rate Prescaler)
57 writeRegister(CNF2, 0x90); // Set Propagation and Phase Segment 1
58 writeRegister(CNF3, 0x02); // Set Phase Segment 2
59}
60
66 writeRegister(TXB0CTRL, 0x00); // Clear TX buffer control register
67}
68
75 0x60); // Enable rollover and set RX mode to receive all
76}
77
83 writeRegister(0x00, 0xFF); // Set filter 0
84 writeRegister(0x01, 0xFF); // Set mask 0
85}
86
92 writeRegister(CANINTE, 0x01); // Enable receive interrupt
93}
94
101 writeRegister(CANCTRL, mode);
102}
103
110bool MCP2515Configurator::verifyMode(uint8_t expectedMode) {
111 uint8_t mode = readRegister(CANSTAT) & 0xE0;
112 return mode == expectedMode;
113}
114
121void MCP2515Configurator::writeRegister(uint8_t address, uint8_t value) {
122 spiController.writeByte(address, value);
123}
124
131uint8_t MCP2515Configurator::readRegister(uint8_t address) {
132 return spiController.readByte(address);
133}
134
141void MCP2515Configurator::sendCommand(uint8_t command) {
142 uint8_t tx[] = {command};
143 spiController.spiTransfer(tx, nullptr, sizeof(tx));
144}
145
152std::vector<uint8_t> MCP2515Configurator::readCANMessage(uint16_t &frameID) {
153 std::vector<uint8_t> CAN_RX_Buf;
154
155 if (readRegister(CANINTF) & 0x01) { // Check if data is available
156 uint8_t sidh = readRegister(RXB0SIDH);
157 uint8_t sidl = readRegister(RXB0SIDL);
158 frameID = (sidh << 3) | (sidl >> 5);
159
160 uint8_t len = readRegister(0x65); // Length of the data
161 for (uint8_t i = 0; i < len; ++i) {
162 CAN_RX_Buf.push_back(readRegister(0x66 + i));
163 }
164
165 writeRegister(CANINTF, 0x00); // Clear interrupt flag
166 }
167
168 return CAN_RX_Buf;
169}
170
178void MCP2515Configurator::sendCANMessage(uint16_t frameID, uint8_t *CAN_TX_Buf,
179 uint8_t length1) {
180 uint8_t tempdata = readRegister(CAN_RD_STATUS);
181 writeRegister(TXB0SIDH, (frameID >> 3) & 0xFF);
182 writeRegister(TXB0SIDL, (frameID & 0x07) << 5);
183
186 writeRegister(TXB0DLC, length1);
187 for (uint8_t j = 0; j < length1; ++j) {
188 writeRegister(TXB0D0 + j, CAN_TX_Buf[j]);
189 }
190
191 if (tempdata & 0x04) { // TXREQ
192 std::this_thread::sleep_for(
193 std::chrono::milliseconds(10)); // sleep for 0.01 seconds
194 writeRegister(TXB0CTRL, 0); // clean flag
195 while (true) {
196 if ((readRegister(CAN_RD_STATUS) & 0x04) != 1) {
197 break;
198 }
199 }
200 }
201 uint8_t rtsCommand = CAN_RTS_TXB0;
202 spiController.spiTransfer(&rtsCommand, nullptr, 1);
203}
Definition of the MCP2515Configurator class.
Interface for the SPI controller.
MCP2515Configurator(ISPIController &spiController)
Construct a new MCP2515Configurator::MCP2515Configurator object.
static constexpr uint8_t RXB0SIDH
static constexpr uint8_t CNF2
static constexpr uint8_t CNF1
bool verifyMode(uint8_t expectedMode)
Verify the mode of the MCP2515.
static constexpr uint8_t TXB0CTRL
static constexpr uint8_t TXB0EID8
static constexpr uint8_t CNF3
static constexpr uint8_t CANCTRL
void configureRXBuffer()
Configure the RX buffer for the MCP2515.
static constexpr uint8_t TXB0D0
static constexpr uint8_t CANINTF
void setMode(uint8_t mode)
Set the mode for the MCP2515.
static constexpr uint8_t TXB0SIDL
static constexpr uint8_t RESET_CMD
bool resetChip()
clean up the resources used by the MCP2515Configurator.
void configureBaudRate()
Configure the baud rate for the MCP2515.
ISPIController & spiController
Reference to the SPI controller.
static constexpr uint8_t CANINTE
uint8_t readRegister(uint8_t address)
Read a value from a register.
void writeRegister(uint8_t address, uint8_t value)
Write a value to a register.
void configureInterrupts()
Configure the interrupts for the MCP2515.
static constexpr uint8_t CAN_RD_STATUS
void sendCommand(uint8_t command)
Send a command to the MCP2515.
std::vector< uint8_t > readCANMessage(uint16_t &frameID)
Read a CAN message from the MCP2515.
static constexpr uint8_t TXB0EID0
static constexpr uint8_t RXB0SIDL
static constexpr uint8_t TXB0SIDH
void configureFiltersAndMasks()
Configure the filters and masks for the MCP2515.
static constexpr uint8_t CAN_RTS_TXB0
static constexpr uint8_t TXB0DLC
void configureTXBuffer()
Configure the TX buffer for the MCP2515.
static constexpr uint8_t CANSTAT
void sendCANMessage(uint16_t frameID, uint8_t *CAN_TX_Buf, uint8_t length1)
Send a CAN message to the MCP2515.
static constexpr uint8_t RXB0CTRL