Skip to content

How to set up access to physical CAN interface

Teri Lenard edited this page Mar 13, 2022 · 1 revision

How to set up access to physical CAN interface

Required tools

$ sudo apt install can-utils

To enable a CAN controller (e.g., MCP2515) the /boot/config.txt must be edited, the spi enabled, and a kernel overlay added for the controller. For convenience we are using a MCP2515 CAN controller.

Edit as root /boot/config.txt

sudo nano /boot/config.txt

Uncomment the line

dtparam=spi=on

Above the dtparam line add the overlay as follows: if the MCP is connected on SPI0.0 add the following lines

dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=12
dtoverlay=spi-1cs

or

dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=12
dtoverlay=spi-bcm2835

if the MCP is connected on SPI1.0 then copy the mcp2515-can2.dtbo file to /boot/overlays and add the following lines:

dtoverlay=spi1-1cs,cs0_pin=16,cs0_spidev=off
dtoverlay=mcp2515-can2,oscillator=8000000,interrupt=26

mcp2515-can2.dtbo can be found here

After, reboot. Depending on your CAN controller, and how it is linked to the raspberrypi, the parameters may differ. After boot, setup the new CAN interface (can0, can1, etc. )

sudo ip link set can0 up type can bitrate 500000 loopback off restart-ms 100

And finally, to make it persistent append the following lines in /etc/network/interfaces

auto can0
iface can0 inet manual
   pre-up /sbin/ip link set can0 type can bitrate 500000 loopback off restart-ms 100
   up /sbin/ifconfig can0 up
   down /sbin/ifconfig can0 down

How to set up Virtual CAN interface

Required tools

$ sudo apt install can-utils

Virtual CAN Interface

Creating and setting up the virtual can interface

$ sudo modprobe vcan
$ sudo ip link add dev vcan0 type vcan
$ sudo ip link set up vcan0

To make it persistent, add the following lines in /etc/network/interfaces

auto vcan0
   iface vcan0 inet manual
   pre-up /sbin/ip link add dev $IFACE type vcan
   up /sbin/ifconfig $IFACE up

And in /etc/modules add the line:

vcan

finally, restart networking service:

sudo /etc/init.d/networking restart

The interface should be visible with ifconfig.