10#define MCP_8MHz_1000kBPS_CFG1 (0x00)
11#define MCP_8MHz_1000kBPS_CFG2 (0x80)
12#define MCP_8MHz_1000kBPS_CFG3 (0x80)
14#define MCP_8MHz_500kBPS_CFG1 (0x00)
15#define MCP_8MHz_500kBPS_CFG2 (0x90)
16#define MCP_8MHz_500kBPS_CFG3 (0x82)
18#define MCP_8MHz_250kBPS_CFG1 (0x00)
19#define MCP_8MHz_250kBPS_CFG2 (0xB1)
20#define MCP_8MHz_250kBPS_CFG3 (0x85)
22#define MCP_8MHz_200kBPS_CFG1 (0x00)
23#define MCP_8MHz_200kBPS_CFG2 (0xB4)
24#define MCP_8MHz_200kBPS_CFG3 (0x86)
26#define MCP_8MHz_125kBPS_CFG1 (0x01)
27#define MCP_8MHz_125kBPS_CFG2 (0xB1)
28#define MCP_8MHz_125kBPS_CFG3 (0x85)
30#define MCP_8MHz_100kBPS_CFG1 (0x01)
31#define MCP_8MHz_100kBPS_CFG2 (0xB4)
32#define MCP_8MHz_100kBPS_CFG3 (0x86)
34#define MCP_8MHz_80kBPS_CFG1 (0x01)
35#define MCP_8MHz_80kBPS_CFG2 (0xBF)
36#define MCP_8MHz_80kBPS_CFG3 (0x87)
38#define MCP_8MHz_50kBPS_CFG1 (0x03)
39#define MCP_8MHz_50kBPS_CFG2 (0xB4)
40#define MCP_8MHz_50kBPS_CFG3 (0x86)
42#define MCP_8MHz_40kBPS_CFG1 (0x03)
43#define MCP_8MHz_40kBPS_CFG2 (0xBF)
44#define MCP_8MHz_40kBPS_CFG3 (0x87)
46#define MCP_8MHz_33k3BPS_CFG1 (0x47)
47#define MCP_8MHz_33k3BPS_CFG2 (0xE2)
48#define MCP_8MHz_33k3BPS_CFG3 (0x85)
50#define MCP_8MHz_31k25BPS_CFG1 (0x07)
51#define MCP_8MHz_31k25BPS_CFG2 (0xA4)
52#define MCP_8MHz_31k25BPS_CFG3 (0x84)
54#define MCP_8MHz_20kBPS_CFG1 (0x07)
55#define MCP_8MHz_20kBPS_CFG2 (0xBF)
56#define MCP_8MHz_20kBPS_CFG3 (0x87)
58#define MCP_8MHz_10kBPS_CFG1 (0x0F)
59#define MCP_8MHz_10kBPS_CFG2 (0xBF)
60#define MCP_8MHz_10kBPS_CFG3 (0x87)
62#define MCP_8MHz_5kBPS_CFG1 (0x1F)
63#define MCP_8MHz_5kBPS_CFG2 (0xBF)
64#define MCP_8MHz_5kBPS_CFG3 (0x87)
69#define MCP_16MHz_1000kBPS_CFG1 (0x00)
70#define MCP_16MHz_1000kBPS_CFG2 (0xD0)
71#define MCP_16MHz_1000kBPS_CFG3 (0x82)
73#define MCP_16MHz_500kBPS_CFG1 (0x00)
74#define MCP_16MHz_500kBPS_CFG2 (0xF0)
75#define MCP_16MHz_500kBPS_CFG3 (0x86)
77#define MCP_16MHz_250kBPS_CFG1 (0x41)
78#define MCP_16MHz_250kBPS_CFG2 (0xF1)
79#define MCP_16MHz_250kBPS_CFG3 (0x85)
81#define MCP_16MHz_200kBPS_CFG1 (0x01)
82#define MCP_16MHz_200kBPS_CFG2 (0xFA)
83#define MCP_16MHz_200kBPS_CFG3 (0x87)
85#define MCP_16MHz_125kBPS_CFG1 (0x03)
86#define MCP_16MHz_125kBPS_CFG2 (0xF0)
87#define MCP_16MHz_125kBPS_CFG3 (0x86)
89#define MCP_16MHz_100kBPS_CFG1 (0x03)
90#define MCP_16MHz_100kBPS_CFG2 (0xFA)
91#define MCP_16MHz_100kBPS_CFG3 (0x87)
93#define MCP_16MHz_95kBPS_CFG1 (0x03)
94#define MCP_16MHz_95kBPS_CFG2 (0xAD)
95#define MCP_16MHz_95kBPS_CFG3 (0x07)
97#define MCP_16MHz_83k3BPS_CFG1 (0x03)
98#define MCP_16MHz_83k3BPS_CFG2 (0xBE)
99#define MCP_16MHz_83k3BPS_CFG3 (0x07)
101#define MCP_16MHz_80kBPS_CFG1 (0x03)
102#define MCP_16MHz_80kBPS_CFG2 (0xFF)
103#define MCP_16MHz_80kBPS_CFG3 (0x87)
105#define MCP_16MHz_50kBPS_CFG1 (0x07)
106#define MCP_16MHz_50kBPS_CFG2 (0xFA)
107#define MCP_16MHz_50kBPS_CFG3 (0x87)
109#define MCP_16MHz_40kBPS_CFG1 (0x07)
110#define MCP_16MHz_40kBPS_CFG2 (0xFF)
111#define MCP_16MHz_40kBPS_CFG3 (0x87)
113#define MCP_16MHz_33k3BPS_CFG1 (0x4E)
114#define MCP_16MHz_33k3BPS_CFG2 (0xF1)
115#define MCP_16MHz_33k3BPS_CFG3 (0x85)
117#define MCP_16MHz_20kBPS_CFG1 (0x0F)
118#define MCP_16MHz_20kBPS_CFG2 (0xFF)
119#define MCP_16MHz_20kBPS_CFG3 (0x87)
121#define MCP_16MHz_10kBPS_CFG1 (0x1F)
122#define MCP_16MHz_10kBPS_CFG2 (0xFF)
123#define MCP_16MHz_10kBPS_CFG3 (0x87)
125#define MCP_16MHz_5kBPS_CFG1 (0x3F)
126#define MCP_16MHz_5kBPS_CFG2 (0xFF)
127#define MCP_16MHz_5kBPS_CFG3 (0x87)
132#define MCP_20MHz_1000kBPS_CFG1 (0x00)
133#define MCP_20MHz_1000kBPS_CFG2 (0xD9)
134#define MCP_20MHz_1000kBPS_CFG3 (0x82)
136#define MCP_20MHz_500kBPS_CFG1 (0x00)
137#define MCP_20MHz_500kBPS_CFG2 (0xFA)
138#define MCP_20MHz_500kBPS_CFG3 (0x87)
140#define MCP_20MHz_250kBPS_CFG1 (0x41)
141#define MCP_20MHz_250kBPS_CFG2 (0xFB)
142#define MCP_20MHz_250kBPS_CFG3 (0x86)
144#define MCP_20MHz_200kBPS_CFG1 (0x01)
145#define MCP_20MHz_200kBPS_CFG2 (0xFF)
146#define MCP_20MHz_200kBPS_CFG3 (0x87)
148#define MCP_20MHz_125kBPS_CFG1 (0x03)
149#define MCP_20MHz_125kBPS_CFG2 (0xFA)
150#define MCP_20MHz_125kBPS_CFG3 (0x87)
152#define MCP_20MHz_100kBPS_CFG1 (0x04)
153#define MCP_20MHz_100kBPS_CFG2 (0xFA)
154#define MCP_20MHz_100kBPS_CFG3 (0x87)
156#define MCP_20MHz_83k3BPS_CFG1 (0x04)
157#define MCP_20MHz_83k3BPS_CFG2 (0xFE)
158#define MCP_20MHz_83k3BPS_CFG3 (0x87)
160#define MCP_20MHz_80kBPS_CFG1 (0x04)
161#define MCP_20MHz_80kBPS_CFG2 (0xFF)
162#define MCP_20MHz_80kBPS_CFG3 (0x87)
164#define MCP_20MHz_50kBPS_CFG1 (0x09)
165#define MCP_20MHz_50kBPS_CFG2 (0xFA)
166#define MCP_20MHz_50kBPS_CFG3 (0x87)
168#define MCP_20MHz_40kBPS_CFG1 (0x09)
169#define MCP_20MHz_40kBPS_CFG2 (0xFF)
170#define MCP_20MHz_40kBPS_CFG3 (0x87)
172#define MCP_20MHz_33k3BPS_CFG1 (0x0B)
173#define MCP_20MHz_33k3BPS_CFG2 (0xFF)
174#define MCP_20MHz_33k3BPS_CFG3 (0x87)
269 static const uint8_t CANCTRL_REQOP = 0xE0;
270 static const uint8_t CANCTRL_ABAT = 0x10;
271 static const uint8_t CANCTRL_OSM = 0x08;
272 static const uint8_t CANCTRL_CLKEN = 0x04;
273 static const uint8_t CANCTRL_CLKPRE = 0x03;
275 enum CANCTRL_REQOP_MODE : uint8_t {
276 CANCTRL_REQOP_NORMAL = 0x00,
277 CANCTRL_REQOP_SLEEP = 0x20,
278 CANCTRL_REQOP_LOOPBACK = 0x40,
279 CANCTRL_REQOP_LISTENONLY = 0x60,
280 CANCTRL_REQOP_CONFIG = 0x80,
281 CANCTRL_REQOP_POWERUP = 0xE0
284 static const uint8_t CANSTAT_OPMOD = 0xE0;
285 static const uint8_t CANSTAT_ICOD = 0x0E;
287 static const uint8_t CNF3_SOF = 0x80;
289 static const uint8_t TXB_EXIDE_MASK = 0x08;
290 static const uint8_t DLC_MASK = 0x0F;
291 static const uint8_t RTR_MASK = 0x40;
293 static const uint8_t RXBnCTRL_RXM_STD = 0x20;
294 static const uint8_t RXBnCTRL_RXM_EXT = 0x40;
295 static const uint8_t RXBnCTRL_RXM_STDEXT = 0x00;
296 static const uint8_t RXBnCTRL_RXM_MASK = 0x60;
297 static const uint8_t RXBnCTRL_RTR = 0x08;
298 static const uint8_t RXB0CTRL_BUKT = 0x04;
299 static const uint8_t RXB0CTRL_FILHIT_MASK = 0x03;
300 static const uint8_t RXB1CTRL_FILHIT_MASK = 0x07;
301 static const uint8_t RXB0CTRL_FILHIT = 0x00;
302 static const uint8_t RXB1CTRL_FILHIT = 0x01;
304 static const uint8_t MCP_SIDH = 0;
305 static const uint8_t MCP_SIDL = 1;
306 static const uint8_t MCP_EID8 = 2;
307 static const uint8_t MCP_EID0 = 3;
308 static const uint8_t MCP_DLC = 4;
309 static const uint8_t MCP_DATA = 5;
311 enum STAT : uint8_t {
316 static const uint8_t STAT_RXIF_MASK = STAT_RX0IF | STAT_RX1IF;
318 enum TXBnCTRL : uint8_t {
333 enum INSTRUCTION : uint8_t {
334 INSTRUCTION_WRITE = 0x02,
335 INSTRUCTION_READ = 0x03,
336 INSTRUCTION_BITMOD = 0x05,
337 INSTRUCTION_LOAD_TX0 = 0x40,
338 INSTRUCTION_LOAD_TX1 = 0x42,
339 INSTRUCTION_LOAD_TX2 = 0x44,
340 INSTRUCTION_RTS_TX0 = 0x81,
341 INSTRUCTION_RTS_TX1 = 0x82,
342 INSTRUCTION_RTS_TX2 = 0x84,
343 INSTRUCTION_RTS_ALL = 0x87,
344 INSTRUCTION_READ_RX0 = 0x90,
345 INSTRUCTION_READ_RX1 = 0x94,
346 INSTRUCTION_READ_STATUS = 0xA0,
347 INSTRUCTION_RX_STATUS = 0xB0,
348 INSTRUCTION_RESET = 0xC0
351 enum REGISTER : uint8_t {
431 static const uint32_t DEFAULT_SPI_CLOCK = 10000000;
433 static const int N_TXBUFFERS = 3;
434 static const int N_RXBUFFERS = 2;
436 static const struct TXBn_REGS {
442 static const struct RXBn_REGS {
458 ERROR setMode(
const CANCTRL_REQOP_MODE mode);
460 uint8_t readRegister(
const REGISTER reg);
461 void readRegisters(
const REGISTER reg, uint8_t values[],
const uint8_t n);
462 void setRegister(
const REGISTER reg,
const uint8_t value);
463 void setRegisters(
const REGISTER reg,
const uint8_t values[],
const uint8_t n);
464 void modifyRegister(
const REGISTER reg,
const uint8_t mask,
const uint8_t data);
466 void prepareId(uint8_t *buffer,
const bool ext,
const uint32_t
id);
469 MCP2515(
const uint8_t _CS,
const uint32_t _SPI_CLOCK = DEFAULT_SPI_CLOCK, SPIClass * _SPI =
nullptr);
CAN_CLKOUT
Definition mcp2515.h:201
@ CLKOUT_DIV1
Definition mcp2515.h:203
@ CLKOUT_DIV2
Definition mcp2515.h:204
@ CLKOUT_DIV8
Definition mcp2515.h:206
@ CLKOUT_DISABLE
Definition mcp2515.h:202
@ CLKOUT_DIV4
Definition mcp2515.h:205
CAN_SPEED
Definition mcp2515.h:182
@ CAN_500KBPS
Definition mcp2515.h:197
@ CAN_5KBPS
Definition mcp2515.h:183
@ CAN_50KBPS
Definition mcp2515.h:189
@ CAN_83K3BPS
Definition mcp2515.h:191
@ CAN_125KBPS
Definition mcp2515.h:194
@ CAN_200KBPS
Definition mcp2515.h:195
@ CAN_1000KBPS
Definition mcp2515.h:198
@ CAN_20KBPS
Definition mcp2515.h:185
@ CAN_95KBPS
Definition mcp2515.h:192
@ CAN_40KBPS
Definition mcp2515.h:188
@ CAN_33KBPS
Definition mcp2515.h:187
@ CAN_100KBPS
Definition mcp2515.h:193
@ CAN_31K25BPS
Definition mcp2515.h:186
@ CAN_80KBPS
Definition mcp2515.h:190
@ CAN_250KBPS
Definition mcp2515.h:196
@ CAN_10KBPS
Definition mcp2515.h:184
CAN_CLOCK
Definition mcp2515.h:176
@ MCP_20MHZ
Definition mcp2515.h:177
@ MCP_8MHZ
Definition mcp2515.h:179
@ MCP_16MHZ
Definition mcp2515.h:178
void clearTXInterrupts(void)
Definition mcp2515.cpp:736
ERROR setBitrate(const CAN_SPEED canSpeed)
Definition mcp2515.cpp:196
void clearRXnOVRFlags(void)
Definition mcp2515.cpp:716
ERROR sendMessage(const TXBn txbn, const struct can_frame *frame)
Definition mcp2515.cpp:587
uint8_t errorCountRX(void)
Definition mcp2515.cpp:766
uint8_t getErrorFlags(void)
Definition mcp2515.cpp:711
void clearERRIF()
Definition mcp2515.cpp:759
CANINTF
Definition mcp2515.h:246
@ CANINTF_TX2IF
Definition mcp2515.h:251
@ CANINTF_TX0IF
Definition mcp2515.h:249
@ CANINTF_ERRIF
Definition mcp2515.h:252
@ CANINTF_RX0IF
Definition mcp2515.h:247
@ CANINTF_TX1IF
Definition mcp2515.h:250
@ CANINTF_MERRF
Definition mcp2515.h:254
@ CANINTF_WAKIF
Definition mcp2515.h:253
@ CANINTF_RX1IF
Definition mcp2515.h:248
ERROR setFilterMask(const MASK num, const bool ext, const uint32_t ulData)
Definition mcp2515.cpp:537
RXBn
Definition mcp2515.h:235
@ RXB1
Definition mcp2515.h:237
@ RXB0
Definition mcp2515.h:236
ERROR setListenOnlyMode()
Definition mcp2515.cpp:155
TXBn
Definition mcp2515.h:240
@ TXB1
Definition mcp2515.h:242
@ TXB0
Definition mcp2515.h:241
@ TXB2
Definition mcp2515.h:243
uint8_t getStatus(void)
Definition mcp2515.cpp:140
ERROR readMessage(const RXBn rxbn, struct can_frame *frame)
Definition mcp2515.cpp:637
bool checkReceive(void)
Definition mcp2515.cpp:690
MASK
Definition mcp2515.h:221
@ MASK0
Definition mcp2515.h:222
@ MASK1
Definition mcp2515.h:223
void clearInterrupts(void)
Definition mcp2515.cpp:726
RXF
Definition mcp2515.h:226
@ RXF1
Definition mcp2515.h:228
@ RXF5
Definition mcp2515.h:232
@ RXF2
Definition mcp2515.h:229
@ RXF0
Definition mcp2515.h:227
@ RXF3
Definition mcp2515.h:230
@ RXF4
Definition mcp2515.h:231
bool checkError(void)
Definition mcp2515.cpp:700
EFLG
Definition mcp2515.h:257
@ EFLG_RXEP
Definition mcp2515.h:262
@ EFLG_TXWAR
Definition mcp2515.h:263
@ EFLG_EWARN
Definition mcp2515.h:265
@ EFLG_TXBO
Definition mcp2515.h:260
@ EFLG_RX1OVR
Definition mcp2515.h:258
@ EFLG_TXEP
Definition mcp2515.h:261
@ EFLG_RX0OVR
Definition mcp2515.h:259
@ EFLG_RXWAR
Definition mcp2515.h:264
void clearMERR()
Definition mcp2515.cpp:752
ERROR setFilter(const RXF num, const bool ext, const uint32_t ulData)
Definition mcp2515.cpp:560
ERROR setConfigMode()
Definition mcp2515.cpp:150
ERROR setNormalMode()
Definition mcp2515.cpp:170
ERROR setClkOut(const CAN_CLKOUT divisor)
Definition mcp2515.cpp:495
ERROR reset(void)
Definition mcp2515.cpp:40
void clearRXnOVR(void)
Definition mcp2515.cpp:741
ERROR setLoopbackMode()
Definition mcp2515.cpp:165
uint8_t getInterruptMask(void)
Definition mcp2515.cpp:731
ERROR
Definition mcp2515.h:212
@ ERROR_ALLTXBUSY
Definition mcp2515.h:215
@ ERROR_FAILINIT
Definition mcp2515.h:216
@ ERROR_FAIL
Definition mcp2515.h:214
@ ERROR_FAILTX
Definition mcp2515.h:217
@ ERROR_OK
Definition mcp2515.h:213
@ ERROR_NOMSG
Definition mcp2515.h:218
uint8_t errorCountTX(void)
Definition mcp2515.cpp:771
ERROR setSleepMode()
Definition mcp2515.cpp:160
uint8_t getInterrupts(void)
Definition mcp2515.cpp:721