The CAN bus (Controller Area Network) was originally designed by Bosch for the automotive market to connect ECUs (Engine/Electronic Control Units) together.
Today, this robust communications bus is commonly found, not only in vehicles, but also on the factory floor in automation (e.g. CANOpen) and other applications such as PV solar inverter/battery Energy Storage Systems (ESS).
The BeagleBone Black PCB, however, doesn’t include any CAN bus transceivers and these must be added externally.
CAN Transceiver Wiring
The BeagleBone Black has two DCAN ports:
- DCAN0 is multiplexed on the I2C2_SCL and I2C2_SDA pins and is generally reserved for the Cape Identification EEPROM.
- DCAN1 is multiplexed with UART1 and is considered the better choice if only one CAN bus is needed.
I/O pins on the Beagle Bone Black are 3.3V logic levels and not tolerant to 5V. Hence 5V transceivers should not be used without the appropriate level shifters/voltage dividers.
The module’s CTX line should be connected to DCANx_TX. Likewise, CRX should be connected to DCANx_RX.
The installation of CAN drivers on the BB is a breeze. The BeagleBone Debian distribution (I’m using Debian 9.9 2019-08-03) comes complete with CAN drivers already installed.
debian@beaglebone:~$ dmesg | grep -i can [ 1.045136] CAN device driver interface [ 1.046571] c_can_platform 481cc000.can: c_can_platform device registered (regs=fa1cc000, irq=42) [ 1.047539] c_can_platform 481d0000.can: c_can_platform device registered (regs=fa1d0000, irq=43) [ 1.275146] can: controller area network core (rev 20170425 abi 9) [ 856.286265] c_can_platform 481d0000.can can1: setting BTR=1c02 BRPE=0000
However the multiplexed pins are not assigned to the CAN controller by default.
To temporary assign I/O header P9 pins 24 & 26 to DCAN1, execute:
config-pin p9.24 can config-pin p9.26 can
or to assign I/O header P9 pins 19 and 20 to DCAN0, execute:
config-pin p9.19 can config-pin p9.20 can
Now manually bring up the CAN interface using:
sudo /sbin/ip link set can1 up type can bitrate 500000
The bitrate above is specified as 500kHz for example, but this should be changed to be compatible with devices on your CAN bus.
If you want a more permanent installation, the I/O assignment can be performed at boot via device tree overlays. Edit the /boot/uEnv.txt file and modify the following lines under ###Additional custom capes :
###Additional custom capes uboot_overlay_addr4=/lib/firmware/BB-CAN0-00A0.dtbo uboot_overlay_addr5=/lib/firmware/BB-CAN1-00A0.dtbo
This example enables both CAN0 and CAN1.
To automatically bring up the interface on boot, edit your /etc/network/interfaces file and add the following:
auto can0 iface can0 inet manual pre-up /sbin/ip link set can0 type can bitrate 500000 triple-sampling on restart-ms 100 up /sbin/ifconfig can0 up down /sbin/ifconfig can0 down
Cape Design Files
If you want to make your own CANBus Cape as pictured above, open source design files are available below:
- CircuitMaker Design Files
- Beaglebone CANbus Cape version 1.A.1 Gerbers PCB (Zip)
- Beaglebone CANbus Cape version 1.A.1 Schematics (PDF)
- Beaglebone CANbus Cape version 1.A.1 Bill of Materials (XLSX)
Testing & Code Development
CAN-Utils is a collection of extremely useful debugging tools using the SocketCAN interface and can be used to test and debug your interface.
Check out our tutorial for example C SocketCAN code.