Samstag, 15. August 2015

Step-by-step guide: Installing the Lawicel CANUSB adapter on Linux

This tutorial explains how to get the Lawicel CANUSB device running and automatically loaded when it’s plugged in. You’ll need access to an operating CAN network with at least one sending node to test. Other CAN interfaces may work as well, as long as they’re using „slcan“ (interfaces not using "slcan" are set up very different from what you'll read here). Tested on Ubuntu 14.04. If you want to run it on a Raspberry Pi, note that you need to use the Raspberry Pi 2, since the RPi1 doesn’t support Ubuntu. Using Raspbian would also be possible, but requires compiling the kernel including the slcan drivers (which means a lot of work).

Follow the following steps carefully:


1. Install the „can-utils“ package (which includes the required slcan daemon):
sudo apt-get install can-utils

2. Check if the kernel modules „can“, „can_raw“ and „slcan“ are already loaded:
lsmod
If not, load them manually:
sudo modprobe can
sudo modprobe can_raw
sudo modprobe slcan
If modprobe says „Module not found“ at this point, your linux distribution is missing the can modules. If modprobe says nothing, the modules exist. Now let’s make them load automatically:
sudo nano /etc/modules
Add the three modules „can“, „can_raw“, „slcan“ to the list (each goes in one line).

3. Connect the CANUSB to the CAN network, plug it into the USB port and check if it shows up:
lsusb
It should be listed as „Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC“. Also check that it shows up as device „ttyUSBX“ (in most cases „ttyUSB0“):
ls /dev
If it doesn’t show up, install the FTDI VCP drivers (see http://www.ftdichip.com).

4. Now we will bind the CANUSB device to the slcan interface:
The following table contains all possible CAN bus speed settings:
sX
CAN speed
s0
10 Kbit/s
s1
20 Kbit/s
s2
50 Kbit/s
s3
100 Kbit/s
s4
125 Kbit/s
s5
250 Kbit/s
s6
500 Kbit/s
s7
800 Kbit/s
s8
1000 Kbit/s
Bind the interface (replace the parameters –sX and ttyUSBX with the correct values):
sudo slcand -o -c -f -sX /dev/ttyUSBX slcan0
Now check that the interface has been added successfully (the following command should list „slcan0“ in state „DOWN“):
ip addr
If it’s not in the list, check the system log file for errors:
sudo tail -n 10 -f /var/log/syslog

5. Bring the slcan interface up:
sudo ifconfig slcan0 up
Before proceeding, check the interface state – it should’ve changed from „DOWN“ to „UNKNOWN“:
ip addr

6. Now check for CAN messages (make sure at least one CAN node is sending):
candump slcan0
You should see CAN messages now. If not, check the green LED (it should light up whenever a CAN message is received). Also make sure you chose the correct CAN bus speed setting and the CAN bus has at least one active sending node. Exit the program using CTRL+C.

7. Now we will automate the linking process (steps 3 to 5) so the system will perform it whenever we plug in the CANUSB interface. First create a new udev rule:
sudo nano /etc/udev/rules.d/90-slcan.rules
Add the following content:
# Lawicel CANUSB module
ACTION=="add", ENV{ID_MODEL}=="CANUSB", ENV{SUBSYSTEM}=="tty", RUN+="/usr/bin/logger [udev] Lawicel CANUSB detected - running slcan_add.sh!", RUN+="/usr/local/bin/slcan_add.sh $kernel"
ACTION=="remove", ENV{ID_MODEL}=="CANUSB", ENV{SUBSYSTEM}=="usb", RUN+="/usr/bin/logger [udev] Lawicel CANUSB removed - running slcan_remove.sh!", RUN+="/usr/local/bin/slcan_remove.sh"
Make sure that you removed the line breaks (each ACTION command needs to be in it’s own line).

8. Create the script that will be called by udev when CANUSB is plugged in:
sudo nano /usr/local/bin/slcan_add.sh
Add the following content:
#!/bin/sh
# Bind the USBCAN device
slcand -o -c -f -sX /dev/$1 slcan0
sleep 2
ifconfig slcan0 up 
Reminder: Fill in the -sX parameter to suit your CAN bus speed!

9. Create the script that will be called by udev when CANUSB is removed:
sudo nano /usr/local/bin/slcan_remove.sh
Add the following content:
#!/bin/sh
# Remove the USBCAN device
pkill slcand

10. Make both scripts executable:
sudo chmod +x /usr/local/bin/slcan_add.sh
sudo chmod +x /usr/local/bin/slcan_remove.sh
Now remove the CANUSB from the USB port.

11. Reboot to make udev apply the new rule:
sudo reboot
Plug the CANUSB back in.

12. Check slcan for functionality again:
candump slcan0
If it doesn’t work, check the system log for errors and use the udev monitor for hints:
sudo tail -n 10 -f /var/log/syslog
udevadm monitor --property

Done! From now on the Lawicel CANUSB interface can be plugged in and out at any time and it should set up automatically.
  
Trivia:
- The Lawicel CANUSB interface uses the FTDI Serial-to-USB chip, which comes with two drivers: The D2XX (direct device access) and the VCP (ascii based virtual com port), which we use.
- Newer linux distributions have the required SocketCAN kernel modules included. There are 3 types available: Native CAN („can“), virtual CAN („vcan“) and serial CAN („slcan“). Since we are using the VCP driver, we are using slcan.
- The green LED on the CANUSB indicates that CAN frames have been received or sent (they’re only working if the correct CAN bus speed has been set). The red LED shows if the module is in errornous state – to fix, clear the error bit by software (or replug and reboot).