Skip to content

RaspberryPi CAN Interfaces

kwindrem edited this page Feb 15, 2021 · 70 revisions

This document is part of the instruction on running Venus OS on a RaspberryPi.

Main document is here.

To make the canbus work, there are two things to do:

  1. First, get the linux driver up & running.
  2. Configure Venus OS such that it knows there is a canbus available.

The RPi Venus image comes with a number of drivers and utilities that enable several different types of CAN interface to be connected. The included package can-utils can be used to test any CAN device by running candump can0, which will display the raw frames read from an active CAN/NMEA2k bus.

To connect can0 to the Venus system the following should be added:

  1. Create a file /etc/venus/canbus_ports that contains the entry can0
  2. Copy the directory /opt/victronenergy/can-bus-bms/service to /service/can-bus-bms.can0
  3. Copy the directory /opt/victronenergy/vecan-dbus/service to /service/vecan-dbus.can0
  4. Copy the directory /opt/victronenergy/mqtt-n2k/service to /service/mqtt-n2k.can0
  5. In all three of those /service/*.can0 directories change DEV to can0 in the run and log/run files.

Here's a script that automates the tasks described above as well as installing the shell scripts, udev rules and overlays for each interface. The script supports multiple VeCan (aka CANbus) interfaces if needed. Currently VenusOS supports at most 2 can interfaces (can0 and can1).

https://github.com/kwindrem/Victron-VenusOs-VeCan-Port-Setup

The following CAN interface devices have been tested on RPi Venus:

This is a RPi HAT board that connects to the SPI bus via the GPIO header. The RPi Venus image includes kernel drivers for the MCP2515 chip used by the Canberry, as well as a device tree overlay to configure it.

To activate this device, the file /u-boot/config.txt should have the following lines at the end:

[all]
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=22

To bring it up for testing prior to configuring the services, type the command:

ip link set can0 up type can bitrate 250000

There are a number of SLCAN devices on the market, most of which are clones of the Lawicel CANUSB. This device requires the utility slcand which is part of the can-utils package included in the RPi Venus image. The device presents a normal USB serial interface and will appear as /dev/ttyUSBx. To activate, slcand is run with the following arguments (NB: These examples assume 250kb/s CAN bus. Change the -s value for other speeds):

slcand -o -c -f -s5 /dev/ttyUSBx can0

The can0 interface is then brought up with:

ip link set can0 up

This process can be automated using udev. Create a file /etc/udev/rules.d/slcan.rules:

# Lawicel CANUSB module
ACTION=="add", ENV{ID_MODEL}=="CANUSB", ENV{ID_BUS}=="usb", SYMLINK+="ttycan0", RUN+="/usr/bin/logger [udev] Lawicel CANUSB detected - running canusb_add.sh!", RUN+="/usr/local/bin/canusb_add.sh $kernel", ENV{VE_SERVICE}="ignore"
ACTION=="remove", ENV{ID_MODEL}=="CANUSB", ENV{ID_BUS}=="usb", RUN+="/usr/bin/logger [udev] Lawicel CANUSB removed - running slcan_remove.sh!", RUN+="/usr/local/bin/slcan_remove.sh"

and the two shell files:

In /usr/local/bin/canusb_add.sh:

#!/bin/sh
# Bind the USBCAN device
slcand -o -c -f -s5 /dev/$1 can0

and in /usr/local/bin/slcan_remove.sh:

#!/bin/sh
# Remove the USBCAN device
killall slcand

These devices are very similar to SLCAN devices and use the above shell commands & files, but require the following udev rules:

# CANtact/CANable USB module

ACTION=="add", ENV{ID_BUS}=="usb", ENV{ID_VENDOR_ID}=="ad50", ENV{ID_MODEL_ID}=="60c4", SYMLINK+="ttycan0", RUN+="/usr/bin/logger [udev] CANtact detected - running canusb_add.sh!", RUN+="/usr/local/bin/canusb_add.sh $kernel", ENV{VE_SERVICE}="ignore"
ACTION=="remove", ENV{ID_BUS}=="usb", ENV{ID_VENDOR_ID}=="ad50", ENV{ID_MODEL_ID}=="60c4", RUN+="/usr/bin/logger [udev] CANtact removed - running slcan_remove.sh!", RUN+="/usr/local/bin/slcan_remove.sh"

CANable has two different firmware: SLCAN and Candelight. Candelight has the advantage of supporting multiple CANbus links, say operating at different baud rates.

More about flashing firmware CANable here: https://www.canable.io/updater/

Avoid powered USB hubs with CANable! Raspberry Pi powers down the USB ports at boot. CANable resets when plugged into a Pi port or a passive hub. But a powered hub doesn't seem to pass this power down to CANable and CANable hangs.

This device runs with slcand, but requires different arguments:

slcand -o -s5 -t hw -S 3000000 /dev/ttyUSBx can0

VScom udev rules:

# VScom USB-CAN+ module
ACTION=="add", ENV{ID_BUS}=="usb", ENV{ID_MODEL}=="VScom_USB-CAN_Plus", SYMLINK+="ttycan0", RUN+="/usr/bin/logger [udev] VScom USB-CAN Plus detected - running vscan_add.sh!", RUN+="/usr/local/bin/vscan_add.sh $kernel", ENV{VE_SERVICE}="ignore"
ACTION=="remove", ENV{ID_BUS}=="usb", ENV{ID_MODEL}=="VScom_USB-CAN_Plus", RUN+="/usr/bin/logger [udev] VScom USB-CAN Plus removed - running slcan_remove.sh!", RUN+="/usr/local/bin/slcan_remove.sh"

The startup scripts (/usr/local/bin/vscan_add.sh):

#!/bin/sh
# Bind the VScom CAN device
slcand -o -s5 -t hw -S 3000000 /dev/$1 can0

The Peak driver is included in the RPi Venus image. To bring it up for testing prior to configuring the services, type the command:

ip link set can0 up type can bitrate 250000

This is a RPi HAT board that connects to the SPI bus via the GPIO header. The RPi Venus image includes kernel drivers for the MCP2515 chip used by the WaveShare, as well as a device tree overlay to configure it.

To activate this device, the file /u-boot/config.txt should have the following lines at the end:

[all]
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000

if the crystal on the board is the old 8MHz and not the 12MHz, the dtoverlay should be:

dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=25,spimaxfrequency=1000000

To bring it up for testing prior to configuring the services, type the command:

ip link set can0 up type can bitrate 250000

The PiCANs are a series of RPi HATs available in single and dual port versions, some with isolation that connects to the SPI bus via the GPIO header. It is available from a number of resellers. Reference this dual port example: http://skpang.co.uk/catalog/pican2-duo-canbus-board-for-raspberry-pi-23-p-1480.html or https://copperhilltech.com/raspberry-pi/

The RPi Venus image includes kernel drivers for the MCP2515 chip used by the PiCANs, as well as a device tree overlay to configure it.

To activate edit the file /u-boot/config.txt and add the following lines at the end:

[all]
dtparam=spi=on

# Bring up the PiCAN controllers.
dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25
dtoverlay=mcp2515-can1-overlay,oscillator=16000000,interrupt=24
dtoverlay=spi-bcm2835-overlay

(Omit "...can1..." line if single port HAT is begin used)

To bring it up for testing prior to configuring the services, type the command:

ip link set can0 up type can bitrate 250000`

Likewise can1 may also be brought up with:

ip link set can1 up type can bitrate 250000`

Note that, oddly, CAN Port 'B' will be configured as can0, while 'A' will become can1

To automatically bring up the subsystems at boot time, the following may be added to /etc/network/interfaces , but note that might be overwritten with a firmware refresh.

#CAN 0
auto can0
iface can0 inet manual
   pre-up /sbin/ip link set can0 type can bitrate 250000 triple-sampling on restart-ms 100
   up /sbin/ifconfig can0 up
   down /sbin/ifconfig can0 down

#CAN 1
auto can1
iface can1 inet manual
   pre-up /sbin/ip link set can0 type can bitrate 250000 triple-sampling on restart-ms 100
   up /sbin/ifconfig can1 up
   down /sbin/ifconfig can1 down
Clone this wiki locally