You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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:

Step 1: Enable SPI via 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:

  • In your Yocto project, create or edit a bbappend file to modify the Raspberry Pi boot config. For example, make a file recipes-bsp/bootfiles/rpi-config.txt.bbappend in 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.txt in 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.
Step 2: Verify SPI is active

After rebooting, confirm the SPI driver initializes correctly:

  • Run dmesg | grep spi — you should see output like this (varies by SPI bus):
    spi_bcm2835 3f204000.spi: SPI Controller at 0x3f204000 (irq 80)
    
    Or for auxiliary SPI:
    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.0 or /dev/spidev1.0 depending on which bus you enabled.
Step 3: Fix module loading order

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.

Step 4: Configure and test the CAN interface

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.

Bonus: Validate Yocto Kernel Configurations

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:16:55