Skip to content

Commit 35b9308

Browse files
karstenkoenignashif
authored andcommitted
drivers: can: mcp2515: Add driver for MCP2515 CAN controller
The MCP2515 is a CAN controller that can be connected via SPI to an host MCU. This driver adds support for the MCP2515 as a new driver in the CAN subsystem. As it is a SPI peripheral it uses a thread for its interrupt handling and the received message filtering is done inside this interrupt thread, as the MCP2515 filter capabilities are not sufficient for the Zephyr CAN interface. The driver was validated with an external CAN logger and the adjusted CAN sample application. Signed-off-by: Karsten Koenig <[email protected]>
1 parent b8774ae commit 35b9308

File tree

7 files changed

+933
-2
lines changed

7 files changed

+933
-2
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
/drivers/adc/ @anangl
107107
/drivers/bluetooth/ @sjanc @jhedberg @Vudentz
108108
/drivers/can/ @alexanderwachter
109+
/drivers/can/*mcp2515* @karstenkoenig
109110
/drivers/clock_control/*stm32f4* @rsalveti @idlethread
110111
/drivers/counter/ @nordic-krch
111112
/drivers/display/ @vanwinkeljan

drivers/can/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3-
zephyr_sources_ifdef(CONFIG_CAN_STM32 stm32_can.c)
4-
zephyr_sources_ifdef(CONFIG_USERSPACE can_handlers.c)
3+
zephyr_sources_ifdef(CONFIG_CAN_STM32 stm32_can.c)
4+
zephyr_sources_ifdef(CONFIG_CAN_MCP2515 mcp2515.c)
5+
zephyr_sources_ifdef(CONFIG_USERSPACE can_handlers.c)

drivers/can/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ config CAN_1
3636
Enable CAN controller 1
3737

3838
source "drivers/can/Kconfig.stm32"
39+
source "drivers/can/Kconfig.mcp2515"
3940

4041
endif # CAN

drivers/can/Kconfig.mcp2515

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Kconfig.mcp2515 - MCP2515 CAN configuration options
2+
3+
#
4+
# Copyright (c) 2018 Karsten Koenig
5+
#
6+
# SPDX-License-Identifier: Apache-2.0
7+
#
8+
9+
config CAN_MCP2515
10+
bool "MCP2515 CAN Driver"
11+
depends on SPI
12+
help
13+
Enable MCP2515 CAN Driver
14+
15+
if CAN_MCP2515
16+
17+
config CAN_MCP2515_NAME
18+
string "Driver name"
19+
default "MCP2515"
20+
21+
config CAN_MCP2515_OSC_FREQ
22+
int "Oscillator frequency"
23+
default 8000000
24+
help
25+
Specify the frequency of the oscillator connected to the MCP2515.
26+
27+
config CAN_PROP_SEG
28+
int "Prop_Seg"
29+
default 2
30+
range 1 8
31+
help
32+
Time quantums of propagation segment (ISO 11898-1)
33+
34+
config CAN_PHASE_SEG1
35+
int "Phase_Seg1"
36+
default 7
37+
range 1 8
38+
help
39+
Time quantums of phase buffer 1 segment (ISO 11898-1)
40+
41+
config CAN_PHASE_SEG2
42+
int "Phase_Seg2"
43+
default 6
44+
range 2 8
45+
help
46+
Time quantums of phase buffer 2 segment (ISO 11898-1)
47+
48+
config CAN_SJW
49+
int "SJW"
50+
default 1
51+
range 1 4
52+
help
53+
Resynchronization jump width (ISO 11898-1)
54+
55+
config CAN_MCP2515_SPI_PORT_NAME
56+
string "SPI device where MCP2515 is connected"
57+
default "SPI_1"
58+
help
59+
Specify the device name of the SPI device to which MCP2515 is
60+
connected.
61+
62+
config CAN_MCP2515_SPI_SLAVE
63+
int "SPI Slave Select where MCP2515 is connected"
64+
default 1
65+
help
66+
Specify the slave select pin of the SPI to which MCP2515 is
67+
connected.
68+
69+
config CAN_MCP2515_SPI_FREQ
70+
int "SPI frequency to use with MCP2515"
71+
default 1000000
72+
help
73+
SPI frequency to use with MCP2515
74+
75+
config CAN_MCP2515_INT_PORT_NAME
76+
string "INT GPIO controller port name"
77+
default "GPIO_0"
78+
help
79+
GPIO port where INT is connected.
80+
81+
config CAN_MCP2515_INT_PIN
82+
int "INT GPIO pin"
83+
default 19
84+
help
85+
GPIO pin where INT is connected.
86+
87+
config CAN_MCP2515_INT_THREAD_STACK_SIZE
88+
int "Stack size for interrupt handler"
89+
default 512
90+
help
91+
Size of the stack used for internal thread which is ran for
92+
interrupt handling and incoming packets.
93+
94+
config CAN_MCP2515_INT_THREAD_PRIO
95+
int "Priority for interrupt handler"
96+
default 2
97+
help
98+
Priority level of the internal thread which is ran for
99+
interrupt handling and incoming packets.
100+
101+
config CAN_MCP2515_GPIO_SPI_CS
102+
bool "Manage SPI CS through a GPIO pin"
103+
help
104+
This option is useful if one needs to manage SPI CS through a GPIO
105+
pin to by-pass the SPI controller's CS logic.
106+
107+
if CAN_MCP2515_GPIO_SPI_CS
108+
config CAN_MCP2515_SPI_CS_PORT_NAME
109+
string "GPIO driver's name to use to drive SPI CS through"
110+
help
111+
This option is mandatory to set which GPIO controller to use in
112+
order to actually emulate the SPI CS.
113+
114+
config CAN_MCP2515_SPI_CS_PIN
115+
int "GPIO PIN to use to drive SPI CS through"
116+
default 0
117+
help
118+
This option is mandatory to set which GPIO pin to use in order
119+
to actually emulate the SPI CS.
120+
121+
endif #CAN_MCP2515_GPIO_SPI_CS
122+
123+
config CAN_MCP2515_MAX_FILTER
124+
int "Maximum number of concurrent active filters"
125+
default 5
126+
range 1 32
127+
help
128+
Defines the array size of the callback/msgq pointers.
129+
Must be at least the size of concurrent reads.
130+
131+
132+
config CAN_MCP2515_INIT_PRIORITY
133+
int "Init priority"
134+
default 80
135+
help
136+
MCP2515 driver initialization priority, must be higher than SPI.
137+
138+
endif # CAN_MCP2515

0 commit comments

Comments
 (0)