如何在Yocto构建的树莓派CM3镜像中启用SPI适配MCP2515
Hey there! Let's get your SPI and CAN setup working properly on your Raspberry Pi CM3 with Yocto. Here's a step-by-step breakdown to fix this:
config.txt Since you mentioned SPI is set to off in config.txt, enabling it is the first critical step. You can do this either during the Yocto build (for permanent changes) or directly on your flashed image:
Option A: Modify during Yocto build (recommended)
- In your Yocto project, create or edit a
bbappendfile to modify the Raspberry Pi boot config. For example, make a filerecipes-bsp/bootfiles/rpi-config.txt.bbappendin your custom layer, then add:CONFIG += "dtparam=spi=on" # Uncomment below if you're using the auxiliary SPI bus (SPI1/SPI2) # CONFIG += "dtparam=spi1=on" - Rebuild your image with
bitbake <your-image-name>to apply the changes to the boot partition.
Option B: Modify on the flashed image
- Mount the FAT32 boot partition of your CM3 (either by connecting the SD card to your PC or mounting it directly on the Pi).
- Open
config.txtin a text editor and add these lines at the end:dtparam=spi=on # Uncomment if using auxiliary SPI # dtparam=spi1=on - Save the file, unmount the partition, and reboot your CM3.
After rebooting, confirm the SPI driver initializes correctly:
- Run
dmesg | grep spi— you should see output like this (varies by SPI bus):
Or for auxiliary SPI:spi_bcm2835 3f204000.spi: SPI Controller at 0x3f204000 (irq 80)spi_bcm2835aux 3f215080.spi: SPI Controller at 0x3f215080 (irq 132) - Check for SPI device nodes with
ls /dev/spidev*— you should see entries like/dev/spidev0.0or/dev/spidev1.0depending on which bus you enabled.
While you've loaded all necessary modules, the order matters: SPI bus drivers need to load before the MCP2515 driver. Try reloading them in the correct sequence:
# Unload existing modules rmmod mcp251x rmmod spi_bcm2835 rmmod spi_bcm2835aux # Reload in proper order modprobe spi_bcm2835 # Use this instead if you're using auxiliary SPI: modprobe spi_bcm2835aux modprobe mcp251x
Now run dmesg | grep can again — you should see the MCP2515 binding to the SPI bus successfully.
Once SPI is working, set up your CAN interface for testing:
# Bring up the CAN interface with your desired bitrate (e.g., 500kbps) ip link set can0 up type can bitrate 500000 # Verify the interface is active ip addr show can0
You can test communication with tools like candump or cansend — make sure these are included in your Yocto image by adding can-utils to your image recipe if needed.
Double-check that your kernel has the required SPI and CAN settings enabled. In your local.conf, add or verify these lines:
CONFIG_SPI=y CONFIG_SPI_BCM2835=m CONFIG_SPI_BCM2835AUX=m CONFIG_CAN=m CONFIG_CAN_MCP251X=m CONFIG_CAN_DEV=m
If using a custom kernel recipe, ensure these configs are enabled in your defconfig or kernel fragments.
内容的提问来源于stack exchange,提问作者Asma Maaroufi

