最近作项目有一个需求是要求挂接MCP2515模块到CM4的SPI1口(GPIO19,20,21)。但发现在/boot/overlays/README 中介绍如下
Name: mcp2515-can0
Info: Configures the MCP2515 CAN controller on spi0.0
Load: dtoverlay=mcp2515-can0,<param>=<val>
Params: oscillator Clock frequency for the CAN controller (Hz)
spimaxfrequency Maximum SPI frequence (Hz)
interrupt GPIO for interrupt signal
Name: mcp2515-can1
Info: Configures the MCP2515 CAN controller on spi0.1
Load: dtoverlay=mcp2515-can1,<param>=<val>
Params: oscillator Clock frequency for the CAN controller (Hz)
spimaxfrequency Maximum SPI frequence (Hz)
interrupt GPIO for interrupt signal
即只能加到SPI0口(GPIO9,10,11)
唯一能加载到SPI1口的只有 mcp251xfd。
网上查了一下也没有相关介绍。
难道只有自己修改OS?
到github上树莓派源代码时,查阅/boot/overlays/README突然发现有新的内容
Name: mcp2515
Info: Configures the MCP2515 CAN controller on spi0/1/2
For devices on spi1 or spi2, the interfaces should be enabled
with one of the spi1-1/2/3cs and/or spi2-1/2/3cs overlays.
Load: dtoverlay=mcp2515,<param>=<val>
Params: spi<n>-<m> Configure device at spi<n>, cs<m>
(boolean, required)
oscillator Clock frequency for the CAN controller (Hz)
speed Maximum SPI frequence (Hz)
interrupt GPIO for interrupt signal
原来有高人刚刚做好了设备树文件对于这个需求
firmware/mcp2515.dtbo at master · raspberrypi/firmware (github.com)
于是果断升级树莓派OS,升级完成后,版本号为5.10.63
在/boot/config.txt中添加如下代码
dtoverlay=spi1-3cs,cs0_spidev=disabled,cs1_spidev=disabled
dtoverlay=mcp2515,spi1-2,oscillator=8000000,interrupt=25,speed=10000000
我使用的是SPI1.2 所以把cs0和cs1 disable掉。
关机后,接上MCP2515,上电后,先安装 cantools,
sudo apt install can-utils pip3 install cantools
重启后,使用 ip a指令,就能看到can0的设备了
3: can0: <NOARP,ECHO> mtu 16 qdisc noop state DOWN group default qlen 10
link/can