User Tools

Site Tools


canberry

CAN-Berry

When planning to connect a CAN-Bus (WikiPedia: Controller Area Network (CAN)) by using a RaspberryPi, CanBerry I can really recommend the already set-up and soldered CanBerry or even better the CanBerryDual module from industrial berry. The module also includes a RTC, which is quite useful to replace the fake hwclock from the RaspberryPi.

I had ordered already my second CanBerry and with the current raspian it's pretty easy to set it up in only a few minutes. It's working on one of the first Pis the same way as working on the current new RaspberryPi V3, which was used to write these lines.

Preparation

Ensure, to take the newest raspian image and ensure, it's freshly updated with the common kernel device tree activated (default) and so on.

sudo apt-get update; sudo apt-get dist-upgrade

In a next step the basic setup has to be done by either using sudo raspi-config or setting it in manually in the config-files. Ensure SPI is activated and loaded, as the MCP2515 CAN controller is used with the SPI interface.

The /boot/config.txt needs to be extended with two (or one) new dtoverlay entries (mcp2515-canX-overlay), depending on the CanBerry beeing used, to prepare the MCP2515 and trigger it on the right interrupt. The MCP2515i mplements CAN V2.0B at max. 1 Mb/s. In this example here the CanBerryDual is used and setup. Also ensure to find the row, activating the spi module, if not yet existing, add it:

pi@CAN-BerryPi:~ $ cat /boot/config.txt
dtparam=spi=on

#CANBerry needed setup for the two MCP2515
dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=22
dtoverlay=mcp2515-can1-overlay,oscillator=16000000,interrupt=25

Within that moment, the basic setup is done, reboot the RaspberryPi with the CanBerryDual beeing attached.

Verify setup and init CAN-bus

By checking the dmesg messages since reboot, there should be a line now, prooving the Pi loaded the MCP2515/CAN controller and is ready to use it on a CAN-bus.

pi@CAN-BerryPi:~ $ dmesg | grep -i can
[    7.153151] CAN device driver interface

When this test was successful so far, write a short init-shell-script for testing to initiate the CAN-controller. As on the CanBerryDual there are two controllers, can0 and can1 should be setup. They can be setup with the same or different speeds, depending on the bus they are going to be connected to.

Following an example setup for 125 kBit and 1 MBit. An Audi A4 (8E) Infotainment or Comfort CAN-bus uses e.g. 100kBit as bus speed.

pi@CAN-BerryPi:~ $ cat can-init.sh 
#!/bin/sh
#CAN Init
ip link set can0 up type can bitrate 125000  #125 kBit
ip link set can1 up type can bitrate 1000000 #1 MBit

Reminde, the script should be set to 755 to be executable (chmod 755 can-init.sh) before proceeding.

If this script is executed as super user, the CAN interfaces will be initiated as linux network interfaces:

pi@CAN-BerryPi:~ $ sudo ./can-init.sh

Validating it by just checking the current network interfaces:

pi@CAN-BerryPi:~ $ ifconfig 
can0      Link encap:UNSPEC  Hardware Adresse 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          UP RUNNING NOARP  MTU:16  Metrik:1
          RX packets:15915 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          Kollisionen:0 Sendewarteschlangenlänge:10 
          RX bytes:127076 (124.0 KiB)  TX bytes:0 (0.0 B)

can1      Link encap:UNSPEC  Hardware Adresse 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          UP RUNNING NOARP  MTU:16  Metrik:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          Kollisionen:0 Sendewarteschlangenlänge:10 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          
eth0      Link encap:Ethernet  Hardware Adresse b8:27:eb:24:59:42  
          inet Adresse:192.168.0.139  Bcast:192.168.0.255  Maske:255.255.255.0
          inet6-Adresse: fe80::9443:b2a8:c472:aa0d/64 Gültigkeitsbereich:Verbindung
          inet6-Adresse: fd00::9b6f:4153:eb8f:b15c/64 Gültigkeitsbereich:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metrik:1
          RX packets:15557 errors:0 dropped:7 overruns:0 frame:0
          TX packets:11908 errors:0 dropped:0 overruns:0 carrier:0
          Kollisionen:0 Sendewarteschlangenlänge:1000 
          RX bytes:1266770 (1.2 MiB)  TX bytes:1961848 (1.8 MiB)

lo        Link encap:Lokale Schleife  
          inet Adresse:127.0.0.1  Maske:255.0.0.0
          inet6-Adresse: ::1/128 Gültigkeitsbereich:Maschine
          UP LOOPBACK RUNNING  MTU:65536  Metrik:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          Kollisionen:0 Sendewarteschlangenlänge:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  Hardware Adresse b8:27:eb:71:0c:0c  
          inet Adresse:192.168.0.153  Bcast:192.168.0.255  Maske:255.255.255.0
          inet6-Adresse: fd00::aed2:966b:8456:e007/64 Gültigkeitsbereich:Global
          inet6-Adresse: fe80::ba27:ebff:fe71:c0c/64 Gültigkeitsbereich:Verbindung
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metrik:1
          RX packets:4495 errors:0 dropped:963 overruns:0 frame:0
          TX packets:73 errors:0 dropped:0 overruns:0 carrier:0
          Kollisionen:0 Sendewarteschlangenlänge:1000 
          RX bytes:986427 (963.3 KiB)  TX bytes:12784 (12.4 KiB)

Now the CAN is active and as it could be seen on the example here on can0 there is a CAN bus connected, which is already sending data, as there ar bytes received.

By the way…

If this setup shall be executed every time the pi boots, an init-script is not neccessary, as the initiation can also be done directly in the /etc/rc.local script. Here the following too lines are just added for a two times 125 kBit setup:

pi@CAN-BerryPi:~ $ cat /etc/rc.local 
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
[...]

#Initiate CAN-Bus for CANBerry module
ip link set can0 up type can bitrate 125000 #125 kBit
ip link set can1 up type can bitrate 125000 #125 kBit

exit 0



Install tools and test

All setup is done so far, so do some real tests. First install linux CAN utilities, for getting first data and do some testing:

sudo apt-get install can-utils

By using the candump command the data on the CAN interface can easily be dumped to the screen, to verify it's working and there is something going on. In this example output I have a CAN device with an Atmega AT90CAN128 creating some example output packages:

pi@CAN-BerryPi:~ $ candump can0
  can0  663   [8]  48 6F 6D 65 54 65 63 68
  can0  03E   [8]  48 6F 6D 65 54 65 63 68
  can0  1D1   [8]  48 6F 6D 65 54 65 63 68
  can0  0FD   [8]  4D 41 4A 26 41 4C 49 20
  can0  7DD   [8]  4D 41 4A 26 41 4C 49 20
  can0  021   [8]  4D 41 4A 26 41 4C 49 20
  can0  38A   [8]  4D 41 4A 26 41 4C 49 20
  can0  35F   [8]  48 6F 6D 65 54 65 63 68
  can0  094   [8]  48 6F 6D 65 54 65 63 68
  can0  5BD   [8]  48 6F 6D 65 54 65 63 68
  can0  0AB   [8]  4D 41 4A 26 41 4C 49 20
  can0  60D   [8]  4D 41 4A 26 41 4C 49 20
  can0  6CF   [8]  4D 41 4A 26 41 4C 49 20
  can0  735   [8]  4D 41 4A 26 41 4C 49 20
  can0  127   [8]  48 6F 6D 65 54 65 63 68
  can0  34C   [8]  48 6F 6D 65 54 65 63 68
  can0  22C   [8]  48 6F 6D 65 54 65 63 68

If something shall be sent on the CAN bus, the command cansend can be used, help explains how to use the command, and example could be:

pi@CAN-BerryPi:~ $ cansend can0 42A#43414e4265727279

In this example 42A is the sender aka CAN identifier and folling after the hash (#) are 8 Bytes of data/payload to be send.

Possible errors

Some possible errors, which could be done by executing or setting up the init-script:

Operation not permitted

pi@CAN-BerryPi:~ $ ./can-init.sh 
RTNETLINK answers: Operation not permitted
RTNETLINK answers: Operation not permitted

Solution: Here it was forgotton to run the init-script as super user! Add sudo in front of the script execution.

Numerical argument out of domain

pi@CAN-BerryPi:~ $ sudo ./can-init.sh 
RTNETLINK answers: Numerical argument out of domain
RTNETLINK answers: Numerical argument out of domain

Solution: The speed entered behind the init command was out of the valid range.

Common speeds are:

ip link set canX up type can bitrate <speedvalue>

CAN Speed
10 kBit/s
20 kBit/s
50 kBit/s
100 kBit/s
125 kBit/s
250 kBit/s
500 kBit/s
800 kBit/s
1 MBit/s

speedvalue
10000
20000
50000
100000
125000
250000
500000
800000
1000000

Ensure, to always have the same speed chosen, which is used overall on the connected CAN bus, for not messing up the bus with a wrong clock.

canberry.txt · Last modified: 21.03.2016 12:56 by andreas