How-To CAN-Bus
Description
The Pi-Tron CM4 has one CAN FD interface, which allows the device to communicate with CAN 2.0B and CAN FD nodes. The CAN FD functionality is realized through an MCP251xFD controller connected to SPI 0 and chip select 1.
Requirements
- At least one or more CAN nodes on the CAN-Bus network or alternatively a CAN-Analyzer.
- 24 volts power supply with 2 pin power plug for the Pi-Tron CM4.
-
Development computer with network access to the Pi-Tron CM4 or alternatively connect an HDMI monitor, mouse and keyboard.
-
To login into the Pi-Tron CM4 via network, it is requires that the SSH server is running.
- The login details are by default: username: pi and password: raspberry
- Note: The latest version of the Raspberry Pi OS requires the user creating the initial SD card or writing the system to eMMC memory to choose a username and password or the information has to be entered during first boot of the system. The above mentioned defaults may not apply to your situation.
- An active Internet connection to install additional software on the Pi-Tron CM4.
Connector X12 Pins
The CAN-Bus interface is located on connector X12 and the pin layout of the connector is as follows.
Bus Termination
The bus termination for the CAN-Bus can be turned on by setting the switch number 5 on the S1 switch to ON.
How-To
1. Enable SPI 0
The raspi-config
program allows the activation of SPI 0 on the Pi-Tron CM4 via a graphical interface.
sudo raspi-config
Choose the menu entry Interface Options
and then SPI
. When asked to activate the SPI interface choose "yes". Then exit the program and when asked to reboot the Pi, choose "yes" again, otherwise do a manual reboot by entering:
sudo reboot
2. Edit the config.txt
To enable the functions of the MCP251xFD chip, edit the config.txt and add the needed device tree overlay information. For Raspberry Pi OS Buster and Bullseye use:
sudo nano /boot/config.txt
Starting with Raspberry Pi OS Bookworm, the config.txt file has moved:
sudo nano /boot/firmware/config.txt
Enter the following lines at the end of the file:
### Activate CAN interface
dtoverlay=mcp251xfd,spi0-1
dtparam=oscillator=20000000
dtparam=interrupt=4
dtparam=speed=1000000
3. Install CAN tools
To be able to send and receive CAN messages, for the purpose of this How-To we use the cansend and candump programs from the can-utils
software package.
sudo apt update
sudo apt install can-utils
4. Reboot
After all the changes another reboot is needed to activate the CAN-Bus interface with its device tree file.
sudo reboot
5. Configure the CAN-Bus interface
Depending on the CAN protocol, the configuration for CAN 2.0B and CAN FD are different. Disable the can0 device:
sudo ip link set can0 down
Configure can0 for CAN 2.0B with a 125 Kbits/s bitrate:
sudo ifconfig can0 txqueuelen 1000
sudo ip link set can0 type can bitrate 125000
Configure can0 for CAN FD with a 125 Kbits/s data and arbitration bitrate:
sudo ifconfig can0 txqueuelen 65536
sudo ip link set can0 type can bitrate 125000 dbitrate 125000 restart-ms 1000 berr-reporting on fd on
Now the can0 device can be enabled again:
sudo ip link set can0 up
6. Send and Receive CAN messages
For the purpose of this How-To, the programs cansend and candump are used to send and receive CAN messages. As with the configuration, sending a CAN 2.0B or a CAN FD message differs in its syntax, receiving is the same for both protocols.
Send CAN 2.0B message example:
cansend can0 123#DEADBEEF
Send CAN FD message example:
cansend can0 213##311
Receive CAN messages example:
candump can0
This concludes the How-To for the CAN-Bus interface on the Pi-Tron CM4. The CAN-Bus device can0 on the Pi-Tron CM4 should now be configured either for CAN 2.0B or CAN FD and is ready to send and receive messages.
Note: After a reboot the CAN-Bus configuration will be gone and has to be entered again. Therefore placing all instructions in a shell script can be beneficial. Furthermore, the shell script can be called from the rc.local file in the /etc/ folder after each start, this way the can0 device is ready for use automatically.
Restrictions
- The BL Pi-Tron CM4 offers access to the standard 40 pin Raspberry Pi header, known from the Raspberry Pi B models. This includes the GPIOs used for SPI 0. Connecting other devices to SPI 0 might work, as chip select 0 is unused, however they might interfere with the function of the MCP251xFD chip and render the CAN-Bus interface inoperable.
- Using the SPI 1 interface for custom expansions is recommended.
Related documentation
- Covering all aspects of the Raspberry Pi, the official documentation: https://www.raspberrypi.com/documentation
- Raspberry Pi configuration program raspi-config: https://www.raspberrypi.com/documentation/computers/configuration.html
- Raspberry Pi overlays readme, this also includes information about the mcp251xfd overlay: https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README
- Linux CAN Utils Github page: https://github.com/linux-can/can-utils
- Raspberry Pi OS now requires users to create their own login credentials on first boot: https://www.raspberrypi.com/news/raspberry-pi-bullseye-update-april-2022/