CANtact on Raspberry Pi

From Linklayer
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 Pi 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. The data on the specified disk will be permanently destroyed!
    sudo dd bs=1m if=ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img of=/dev/rdisk4
    • If you use Windows you can pick one of the following tools
  4. After writing the image to the SD card, insert it in your Raspberry and boot the system. If the Pi is not booting and you don't see the ACT LED blinking, the SD card cannot be read. For further troubleshooting please see 1 or 2

Update Software and Firmware

After installing the operating system, it is highly recommended to update and upgrade the software and firmware. The firmware of the Raspberry Pi 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
    sudo 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

Canutils und pyvit

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

sudo apt-get install can-utils

for pyvit enter:

pip3 install pyvit

Using the CANtact

Readings from a Bosch ECU with candump

To use the CANtact device, two kernel modules must be loaded:

sudo modprobe can
sudo modprobe slcan

Now you will need to link the serial interface with the CAN interface (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

Now you can use SocketCAN tools like candump or cansend to communicate on the bus:

candump can0

For more information on using SocketCAN on Linux, see the article on SocketCAN.