CANtact on Raspberry Pi

From Linklayer
Revision as of 02:43, 26 December 2016 by Markstein (talk | contribs) (first shot)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The Raspberry Pi is a cheap mini computer that can be powered over USB and costs less then $40. It is therefore a good alternative to more expensive laptops or PCs.

This tutorial focuses on the third generation Pi but should work (ideally) without changes on previous models.

Install Linux

The most popular distributions on the Raspberry are Raspbian and Ubuntu Mate. We will focus on the latter one.

  1. Download the compressed image for 16.04.1 LTS for Raspberry Pi 3 aarch32 (ARMv7) here
  2. Extract the ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img.xz image (e.g. with a double click if you use a Mac)
  3. Write the image to an SD card
    • If you use Linux or Mac you can just use the terminal
    • Identify your SD card by inserting it and run:
    diskutil list
    • You should see something like disk4. Make sure it is the right one and unmount it
    diskutil unmountDisk /dev/disk4
    • Write the image to the SD card with dd (this will take several minutes... be patient)
    sudo dd bs=1m if=ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img of=/dev/disk4
    • If you use Windows you can pick one of the following tools
  4. After you wrote the image to the SD card you can insert it in your Raspberry and boot the system. If the Raspberry is not booting and you don't see the ACT LED blinking it means it can't read from your SD card. For further troubleshooting please see 1 or 2

Update Software and Firmware

After installing the operating system, it is highly recommended to update/upgrade both, software and firmware. The firmware of the Raspberry sits on the first partition of the SD card and the software sits on the second partition. Both need to be updated independently.

  1. Update Firmware
    • Ubuntu is coming with a simple tool to do all the work for you
    sudo rpi-update
    • After the update is finished you need to reboot the system
    sudo reboot
  2. Update Software
    • To update the software simply type
    sudo apt-get update –y && sudo apt-get upgrade –y
    (this may take a while)

Install Kernel Headers and the Missing Kernel Module

To be able to use the CANtact device or other serial devices on the Raspberry the kernel module slcan needs to get compiled and installed. This process varies slightly from the standard Ubuntu for desktops and server.

  1. Installing prerequisites
    • Needed by 'make menuconfig'
    sudo apt-get install libncurses5-dev
    sudo wget -O /usr/bin/rpi-source
    sudo chmod +x /usr/bin/rpi-source
    /usr/bin/rpi-source -q --tag-update
    • run rpi-source
    sudo rpi-source –skip-gcc
  2. Compile the missing kernel module
    • The source code for the slcan module is part of the installed kernel headers. Change to its directory
    cd /lib/modules/$(uname -r)/build/drivers/net/can
    • Backup the old Makefile
    sudo mv Makefile Makefile.original
    • Create a new Makefile for slcan and make it
    echo “obj-m := slcan.o” | sudo tee Makefile >/dev/null
    make -C /lib/modules/$(uname -r)/build M=$(pwd) modules
  3. Test the new kernel module
    • After you compiled the kernel module you should see if you can successfully insert it in your kernel
    sudo insmod slcan.ko
    • When you now check dmesg you should see the loaded module
    dmesg output after slcan kernel module gets loaded
    • If the module loads successfully, unload it and continue with the installation
    sudo rmmod slcan
  4. Install the slcan kernel module
    • To install the slcan module type:
    sudo make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install
    sudo depmod
    • Now you should be able to load the module every time you need it
    sudo modprobe slcan


To use SocketCAN and tools like candump and cangen you need to install the can-utils package

  1. sudo apt-get install can-utils

Using the CANtact

Readings from a Bosch ECU with candump
  1. To use the CANtact device on the raspberry you need to load at least two kernel modules
    sudo modprobe can
    sudo modprobe slcan
  2. Now you will need to link the serial interface with the virtual CAN device (assuming you hook up to an active CAN bus with 500 kBit/s)
    sudo slcand -o -c -s6 /dev/ttyACM0 can0
    sudo ifconfig can0 up
  3. Now you can use tools like candump or cansend to communicate on the bus