Update : Latest Raspbian supports CAN Controller MCP251x, you don’t have to add modules like described here.
Please read the new post on how to use CAN Bus with Raspberry Pi.
After several hours trying to find a complete solution to add CAN bus support to my Raspberry Pi, I’m sharing with you what worked for me, by combining different sources and information that I get on the web.
This method uses MCP251x Microchip’s stand-alone controller, connected to SPI bus of the Raspberry Pi, but it should work also for any other CAN controller.
If you meet any problem, please let me know it by comment below or email.
This method is tested with latest Raspbian (3.18.7+), I recommend you to have the same version before proceeding.
I’ll skip Raspbian first installation, if you are not familiar with it, you can use NOOBS setup (version 1.4.0) and follow instructions.
For next steps, you will need :
- Raspberry Pi (I’m using Model B) + SD Card Raspbian.
- Screen/Keyboard/Mouse/Internet Access
After installing Raspbian, start by updating/upgrading, and rebooting your Raspberry Pi.
sudo apt-get update sudo apt-get upgrade sudo apt-get autoremove sudo reboot
Untar in root level (to avoid any permission error), or in /tmp if you want to remove it after rebooting :
cd / sudo tar -xjf ~/rpi-can-3.18.7+.tar.bz2
Register new modules, and reboot :
sudo depmod -a sudo reboot
There is two ways to configure and load your CAN modules :
1 – Doing it directly by command-line :
Start by configuring SPI (copy it as it is, with no spaces) :
sudo modprobe spi-config devices=bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=25:pd=20:pds32-0=16000000:pdu32-4=0x2002:force_release
Explanation : 10MHz SPI clock GPIO 25 for MCP2515 /INT and 16Mhz clock (crystal) – 0x2002 -> IRQF_ONESHOT
Then, load MCP module :
sudo modprobe mcp251x
2 – Doing it in autoload file :
Open your modules config :
sudo nano /etc/modules
Add the following lines :
Press CTRL+O to save changes, then press ENTER to confirm file name. Press CTRL+X to quit nano.
Don’t forget to reboot to load your new modules :
Normally, I say, normally, you are now able to set up a CAN bus link with bitrate value, for example :
ip link set can0 up type can bitrate 250000
If it is not working, here main mistakes I did, and eventually you are doing :
1- SPI module is not installed/enabled
Firstly, check if your SPI is well installed in Raspbian (There is no reason that it isn’t, but check it anyway)
Check your SPI device and module availability with these commands :
ls /sys/bus/spi/devices/ ls /sys/module/spidev/
You need to have (spi0.0 spi0.1), and see also (parameters uevents)
The easy way to enable SPI module it by using Raspi-config interface :
Go to Advanced Options and enable SPI kernel module, and reboot.
Another method is by enabling SPI in Raspi-blacklist, open it :
sudo nano /etc/modprobe.d/raspi-blacklist.conf
Then withelist SPI and MCP with # comment :
CTRL+O, ENTER, CTRL+X.
2- SPI module reference is different from above commands
Shame on me, but some times I used spi-bcm2708 instead of spi-bcm2835…
Take a look to your SPI reference in modules list (search for spi_bcmXXXX) :
3- CAN bus is not connected
You need to connect your Raspberry Pi to physical CAN bus in order to set up can0 interface, otherwise you can’t see it.
Alternative solution is to use virtual CAN bus, it means that you will have internal CAN node in your Raspberry Pi, very helpful for desktop testing.
sudo modprobe vcan sudo ip link add dev vcan0 type vcan sudo ip link set up vcan0
You can now see vcan0 when you display your ifconfig :
Send data (for example send byte “10” to device with identifier 100) :
cansend vcan0 100#01
Helpful Links :
CAN + Raspberry Pi
Raspberry Pi SPI Documentation
Raspberry Pi BCM2835 Documentation
Linux CAN-Utils Documentation