CAN-Bus in the Raspberry Pi

There are many devices in the industry, especially the ones used in land or air vehicles, that uses the CAN-BUS (Control Area Network) for the communication of sensors and actuators with a electronic control unit (ECU). On the other hand, development kits such as Raspberry Pi, allow the students to implement complex project with small budget. Therefore, I present here a tutorial for activating CAN features on a Raspberry Pi.

1.- PREREQUISITES:

:~$ sudo apt-get install ncurses-dev make git

2.- Get compiler
:~$ cd /opt
:~$ sudo mkdir raspberrypi
:~$ cd raspberrypi
:~$ sudo chmod og+w .
:~$ git clone git://github.com/raspberrypi/tools.git
:~$ echo export CC_RASPI=/opt/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- >> ~/.bashrc
:~$ source ~/.bashrc

3.- Get Kernel

 :~$ git clone --depth 1 https://github.com/raspberrypi/linux/
 :~$ cd linux
 :~$ git fetch --depth=1  git://github.com/raspberrypi/linux.git rpi-3.6.y:refs/remotes/origin/rpi-3.6.y
 :~$ git checkout rpi-3.6.y

 

remote copy the compile time configuration out of the initial kernel to the source for the new kernel
:~$ rsync -rtv This email address is being protected from spambots. You need JavaScript enabled to view it.:/proc/config.gz ./
:~$ zcat ./config.gz > .config
:~$ make ARCH=arm CROSS_COMPILE=$CC_RASPI menuconfig

[*] Networking support --->
....<M> CAN bus subsystem support --->
........<M> Raw CAN Protocol (raw access with CAN-ID filtering)
........<M> Broadcast Manager CAN Protocol (with content filtering)
............CAN Device Drivers --->
................<M> Virtual Local CAN Interface (vcan)
................<M> Platform CAN drivers with Netlink support
................[*] CAN bit-timing calculation
................<M> Microchip MCP251x SPI CAN controllers
................[*] CAN devices debugging messages

....Device Drivers --->
........[*] SPI support --->
............<M> BCM2708 SPI controller driver (SPI0)
............<M> User mode SPI driver support
.......-*- GPIO Support --->
............[*] /sys/class/gpio/... (sysfs interface)

patch the file bcm2708.c with my patch
:~$ cd arch/arm/mach-bcm2708/
:~$ patch -p0 <bcm2708.c.diff
:~$ cd /opt/raspberrypi/linux
compile the kernel
:~$ make ARCH=arm CROSS_COMPILE=$CC_RASPI -j3

use tools to generate an image from the build kernel, and copy that to a new "build" directory
:~$ cd /opt/raspberrypi/tools/mkimage
:~$ ./imagetool-uncompressed.py ../../linux/arch/arm/boot/zImage
:~$ mkdir -p /opt/raspberrypi/build/boot
:~$ mv kernel.img /opt/raspberrypi/build/boot

compile kernel modules
:~$ cd ../../linux/
:~$ make ARCH=arm CROSS_COMPILE=$CC_RASPI modules_install INSTALL_MOD_PATH=/opt/raspberrypi/build/ -j3
:~$ cp .config ../build/boot/

get las firmware, and copy it to the build directory
:~$ cd ..
:~$ git clone --depth 1 git://github.com/raspberrypi/firmware.git
:~$ cd firmware
:~$ git fetch  --depth 1 git://github.com/raspberrypi/firmware.git next:refs/remotes/origin/next
:~$ git checkout next

:~$ cp boot/bootcode.bin /opt/raspberrypi/build/boot
:~$ cp boot/fixup.dat /opt/raspberrypi/build/boot
:~$ cp boot/start.elf /opt/raspberrypi/build/boot
 
:~$ mkdir -p /opt/raspberrypi/build/opt
:~$ cp -r hardfp/opt/vc /opt/raspberrypi/build/opt

creat root password to be able to copy the files
:~$ ssh -t This email address is being protected from spambots. You need JavaScript enabled to view it. 'sudo passwd root'
you will get following interaction:
enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Connection to 192.168.2.102 closed.

copy the build directories to the raspberry (the slash at the end of the path is very important, otherwise it creates the folder inside the folder)
:~$ cd /opt/raspberrypi/build
:~$ rsync -rtv ./boot/ This email address is being protected from spambots. You need JavaScript enabled to view it.:/boot/
:~$ rsync -rtv ./lib/ This email address is being protected from spambots. You need JavaScript enabled to view it.:/lib/
:~$ rsync -rtv ./opt/ This email address is being protected from spambots. You need JavaScript enabled to view it.:/opt/

4. - Restart Raspberry pi
:~$ ssh -t This email address is being protected from spambots. You need JavaScript enabled to view it. 'sudo reboot'

5.- compile and install libsocket
:~$ cd /opt/raspberrypi/
:~$ mkdir CAN
:~$ cd CAN
:~$ wget http://www.pengutronix.de/software/libsocketcan/download/libsocketcan-0.0.9.tar.bz2
:~$ tar xvjf libsocketcan-0.0.9.tar.bz2
:~$ rm libsocketcan-0.0.9.tar.bz2
:~$ cd libsocketcan-0.0.9
:~$ mkdir raspi_local
:~$ ./configure --host=arm-linux CC=$(CC_RASPI)gcc --prefix=/opt/raspberrypi/CAN/libsocketcan-0.0.9/raspi_local
:~$ make; make install
:~$ cd raspi_local
:~$ rsync -rtv ./include/ This email address is being protected from spambots. You need JavaScript enabled to view it.:/usr/local/include/
:~$ rsync -rtv ./share/ This email address is being protected from spambots. You need JavaScript enabled to view it.:/usr/local/share/
:~$ rsync -rtv ./lib/ This email address is being protected from spambots. You need JavaScript enabled to view it.:/usr/local/lib/

6.- compile and install can utils ( this hast to be done inside raspberry)
:~$ ssh This email address is being protected from spambots. You need JavaScript enabled to view it.
# git clone git://gitorious.org/linux-can/can-utils.git can-util
# cd can-util
# make
# make install
# cd ..

7.- Load modules at start up
:~$ cd /opt/raspberrypi/build/
:~$ mkdir -p ./etc/init.d
:~$ cd ./etc/init.d
:~$ wget http://(address)/CAN                ************************* (in one step?)
:~$ rsync -rtv ./CAN This email address is being protected from spambots. You need JavaScript enabled to view it.:/etc/init.d/
:~$ ssh -r This email address is being protected from spambots. You need JavaScript enabled to view it. 'sudo chmod 755 /etc/init.d/CAN'
:~$ ssh -r This email address is being protected from spambots. You need JavaScript enabled to view it. 'sudo update-rc.d CAN defaults'
:~$ ssh -r This email address is being protected from spambots. You need JavaScript enabled to view it. 'sudo insserv CAN'
:~$ ssh -r This email address is being protected from spambots. You need JavaScript enabled to view it. 'sudo /etc/init.d/CAN start'
:~$ ssh -r This email address is being protected from spambots. You need JavaScript enabled to view it. 'sudo reboot'

8.- test you can bus
- connect your raspberry to a can bus with 250MBS
:~$ ssh This email address is being protected from spambots. You need JavaScript enabled to view it.
# ifconfig
check that the can0 is up
# candump any,0:0,#FFFFFFFF

9.- useful commands
dmesg
candump any,0:0,#FFFFFFFF
cansend 123#deadbeef
ip -s -d link show can0
cat /proc/interrupts
cat /proc/net/can/stats
tail -n 5 /var/log/messages
cat /var/log/messages | grep can