Midnight Sun Firmware
Loading...
Searching...
No Matches
mcp2515_defs.h
1#pragma once
2
3/************************************************************************************************
4 * @file mcp2515_defs.h
5 *
6 * @brief Header file for MCP2515 register definitions
7 *
8 * @date 2025-01-31
9 * @author Midnight Sun Team #24 - MSXVI
10 ************************************************************************************************/
11
12/* Standard library Headers */
13
14/* Inter-component Headers */
15
16/* Intra-component Headers */
17
24// SPI commands: Table 12-1
25#define MCP2515_CMD_RESET 0xC0
26#define MCP2515_CMD_READ 0x03
27#define MCP2515_CMD_READ_RX 0x90
28#define MCP2515_CMD_WRITE 0x02
29#define MCP2515_CMD_LOAD_TX 0x40
30#define MCP2515_CMD_RTS 0x80
31#define MCP2515_CMD_READ_STATUS 0xA0
32#define MCP2515_CMD_RX_STATUS 0xB0
33#define MCP2515_CMD_BIT_MODIFY 0x05
34
35// READ_RX Arguments: Figure 12-3
36#define MCP2515_READ_RXB0SIDH 0x00 // ID
37#define MCP2515_READ_RXB0D0 0x02 // Data
38#define MCP2515_READ_RXB1SIDH 0x04
39#define MCP2515_READ_RXB1D0 0x06
40
41// LOAD_TX Arguments: Figure 12-5
42#define MCP2515_LOAD_TXB0SIDH 0x00
43#define MCP2515_LOAD_TXB0D0 0x01
44#define MCP2515_LOAD_TXB1SIDH 0x02
45#define MCP2515_LOAD_TXB1D0 0x03
46#define MCP2515_LOAD_TXB2SIDH 0x04
47#define MCP2515_LOAD_TXB2D0 0x05
48
49// RTS Arguments: Table 12-1 (RTS)
50#define MCP2515_RTS_TXB0 0x01
51#define MCP2515_RTS_TXB1 0x02
52#define MCP2515_RTS_TXB2 0x04
53
54// READ_STATUS Response: Figure 12-8
55#define MCP2515_STATUS_RX0IF 0x01
56#define MCP2515_STATUS_RX1IF 0x02
57#define MCP2515_STATUS_TX0REQ 0x04
58#define MCP2515_STATUS_TX0IF 0x08
59#define MCP2515_STATUS_TX1REQ 0x10
60#define MCP2515_STATUS_TX1IF 0x20
61#define MCP2515_STATUS_TX2REQ 0x40
62#define MCP2515_STATUS_TX2IF 0x80
63
64// Control Registers: Table 11-2
65#define MCP2515_CTRL_REG_BFPCTRL 0x0C // RX pins disabled by default
66#define MCP2515_CTRL_REG_TXRTSCTRL 0x0D // TX pins input by default
67#define MCP2515_CTRL_REG_CANSTAT 0x0E
68#define MCP2515_CTRL_REG_CANCTRL 0x0F
69#define MCP2515_CTRL_REG_TEC 0x1C
70#define MCP2515_CTRL_REG_REC 0x1D
71#define MCP2515_CTRL_REG_CNF3 0x28
72#define MCP2515_CTRL_REG_CNF2 0x29
73#define MCP2515_CTRL_REG_CNF1 0x2A
74#define MCP2515_CTRL_REG_CANINTE 0x2B
75#define MCP2515_CTRL_REG_CANINTF 0x2C
76#define MCP2515_CTRL_REG_EFLG 0x2D
77#define MCP2515_CTRL_REG_TXB0CTRL 0x30
78#define MCP2515_CTRL_REG_TXB1CTRL 0x40
79#define MCP2515_CTRL_REG_TXB2CTRL 0x50
80#define MCP2515_CTRL_REG_RXB0CTRL 0x60
81#define MCP2515_CTRL_REG_RXB1CTRL 0x70
82
83// TX Ctrl bits: Register 3-1
84#define MCP2515_TX_CTRL_BIT_ABTF (1 << 6)
85#define MCP2515_TX_CTRL_BIT_MLOA (1 << 5)
86#define MCP2515_TX_CTRL_BIT_TXERR (1 << 4)
87
88// Filter/Mask Registers: Table 11-1
89#define MCP2515_REG_RXF0SIDH 0x00
90#define MCP2515_REG_RXF1SIDH 0x04
91#define MCP2515_REG_RXF2SIDH 0x08
92#define MCP2515_REG_RXF3SIDH 0x10
93#define MCP2515_REG_RXF4SIDH 0x14
94#define MCP2515_REG_RXF5SIDH 0x18
95#define MCP2515_REG_RXM0SIDH 0x20
96#define MCP2515_REG_RXM1SIDH 0x24
97
98// CANCTRL: Register 10-1
99#define MCP2515_CANCTRL_OPMODE_MASK 0xE0
100#define MCP2515_CANCTRL_OPMODE_NORMAL 0x00
101#define MCP2515_CANCTRL_OPMODE_SLEEP 0x20
102#define MCP2515_CANCTRL_OPMODE_LOOPBACK 0x40
103#define MCP2515_CANCTRL_OPMODE_LISTEN 0x60
104#define MCP2515_CANCTRL_OPMODE_CONFIG 0x80
105
106#define MCP2515_CANCTRL_CLKOUT_MASK 0x07
107#define MCP2515_CANCTRL_CLKOUT_CLKPRE_1 0x04 // CLKEN is automatically enabled
108#define MCP2515_CANCTRL_CLKOUT_CLKPRE_2 0x05
109#define MCP2515_CANCTRL_CLKOUT_CLKPRE_4 0x06
110#define MCP2515_CANCTRL_CLKOUT_CLKPRE_8 0x07
111
112// CNF3: Register 5-3
113#define MCP2515_CNF3_PHSEG2_MASK 0x07
114
115// CNF2: Register 5-2
116#define MCP2515_CNF2_BTLMODE_MASK 0x80
117#define MCP2515_CNF2_BTLMODE_CNF3 0x80
118
119#define MCP2515_CNF2_SAMPLE_MASK 0x40
120#define MCP2515_CNF2_SAMPLE_3X 0x40
121
122#define MCP2515_CNF2_PHSEG1_MASK 0x38
123#define MCP2515_CNF2_PRSEG_MASK 0x07
124
125// CNF1: Register 5-1
126#define MCP2515_CNF1_BRP_MASK 0x3F
127
128// CANINTE/INTF: Register 7-1/2
129#define MCP2515_CANINT_MSG_ERROR 0x80
130#define MCP2515_CANINT_WAKEUP 0x40
131#define MCP2515_CANINT_EFLAG 0x20
132#define MCP2515_CANINT_TX2IE 0x10
133#define MCP2515_CANINT_TX1IE 0x08
134#define MCP2515_CANINT_TX0IE 0x04
135#define MCP2515_CANINT_RX1IE 0x02
136#define MCP2515_CANINT_RX0IE 0x01
137
138// EFLG: Register 6-3
139#define MCP2515_EFLG_RX1_OVERFLOW 0x80
140#define MCP2515_EFLG_RX0_OVERFLOW 0x40
141#define MCP2515_EFLG_TX_BUS_OFF 0x20
142#define MCP2515_EFLG_TX_EP_FLAG 0x10
143#define MCP2515_EFLG_RX_EP_FLAG 0x08
144#define MCP2515_EFLG_TX_WARNING 0x04
145#define MCP2515_EFLG_RX_WARNING 0x02
146#define MCP2515_EFLG_ERROR_WARNING 0x01
147
148// TXBnDLC: Register 3-7
149#define MCP2515_TXBNDLC_RTR_SHIFT 6
150#define MCP2515_TXBNDLC_RTR_FRAME 0x40
151#define MCP2515_TXBNDLC_DLC_MASK 0x0F
152
153// RXB0CTRL: Register 4-1
154#define MCP2515_RXB0CTRL_BUKT 0x4
155
156#define MCP2515_MAX_WRITE_BUFFER_LEN 10
157#define MCP2515_STANDARD_ID_LEN 11
158#define MCP2515_EXTENDED_ID_LEN 18
159
160#define MCP2515_CAN_BRP_125KBPS 3
161#define MCP2515_CAN_BRP_250KBPS 1
162#define MCP2515_CAN_BRP_500KBPS 0
163
164#define MCP2515_NUM_MASK_REGISTERS_STANDARD 2
165#define MCP2515_NUM_MASK_REGISTERS_EXTENDED 4
166
167// BFPCTRL: Register 4-3
168#define MCP2515_BFPCTRL_B1BFS 0x20
169#define MCP2515_BFPCTRL_B2BFS 0x10
170#define MCP2515_BFPCTRL_B1BFE 0x08
171#define MCP2515_BFPCTRL_B2BFE 0x04
172#define MCP2515_BFPCTRL_B1BFM 0x02
173#define MCP2515_BFPCTRL_B2BFM 0x01