Setting up CAN on a Pi
The Subsystems Computer's interface to the firmware
Linux has support for the CAN protocol when using an external chip, but it needs to be enabled and configured before it can be used. We use the MCP2518FD`
chip on a custom-built Pi hat to achieve this, and this page describes how to set up the chip on a fresh Raspberry Pi.
The CAN0 overlay
First, we need to tell the Pi how to interface with the chip. The Raspberry Pi works with a "device tree" that tells it what hardware to use and how to find it. We will add our own file to this tree and modify the boot config to expose this device to the operating system.
First, add the following to a new file, titled mcp2518fd-can0.dts:
Compile this file to a .dtbo
and copy it to the boot directory:
Now add the following lines to the end of /boot/config.txt
:
The command line
Reboot the Pi. Now that the hardware has been configured, it's time to set up CAN as a network device in the operating system.
First, update your software packages:
Then, use the ip tool to register CAN:
The bitrate must match the one registered in config.txt
and the one used on the firmware. The add
command needs to be run just once, but the set
command needs to be run on every boot. This can also be done by the Subsystems program.
Testing
The can-utils
package offers tools to quickly test the CAN bus. Run sudo apt install can-utils
to install the tools, then use cansend
to send data and candump
to receive data. One quick test is to open two terminals and run candump can0
in one and cansend can0#deadbeef
in the other. Note that at least one other CAN-capable device must be connected and listening for messages in order for the bus to work.
More Resources
The
can-utils
tool
Last updated
Was this helpful?