CAN & CAN FD Explained For Open Source

CAN & CAN FD

CAN bus or Controller Area Network was developed as a robust means for embedded systems (microcontrollers) to talk to one another. CAN bus is largely synonymous with automotive systems as being one of the primary communication backbones used to broadcast information from electronic control units (ECU’s), sensors, and instrumentation systems in a vehicle.. CAN has also seen wide adoption in the automation industry and the transportation industry with multiple open and private communication protocols having been developed using CAN bus as the hardware layer (Examples: CANOpen, OBD2, GMLAN, ARINC-825, etc).

CAN Flexible Data Rate (CANFD) is an evolution of very popular CAN 2.0b standard, first introduced in 2011-2012 with widespread adoption in production vehicles starting in 2019. CAN FD allows for a relatively similar and even backward compatible design of the communication network, still implementing a 2 wire multi-drop network and 120 ohm terminations however two major improvements were introduced:

  • Faster baud rates, increasing top speed from 1Mbps to 5Mbps
  • Larger data payload sizes from 8 bytes to 64 bytes

The “Flexible Data Rate” in CAN FD refers to the ability of a transmitting node to dynamically change the data rate, enabling the two improvements above. This implementation also allows for the a bus to consist of nodes that use classical CAN (2.0b) and CANFD.

 CANCAN FD
Baud RateUp to 1MbpsUp to 5Mbps
Message SizeUp to 8 bytesUp to 64 bytes
ID size11 or 29bit11 or 29bit
Multi-dropYESYES
Number of Wires3
CANH,CANL,GND
3
CANH,CANL,GND

Mixing CAN & CANFD Messages

CANFD was actually designed to be somewhat backwards compatible with CAN, meaning CAN and CANFD packets are actually designed to co-exist on the same bus together. This is enabled by on-they fly bit-rate switching (BRS flag) in the format of the CANFD frame indicating the baud rate of the latter part of the packet is going to be boosted to accommodate a larger payload message. More specifically, CANFD nodes are able to normally transmit plain old lower-speed smaller CAN packets or larger high-speed CANFD packets however, the receiving nodes need to be compatible with CANFD messages. The primary drivers for having dual-baud rates and not transmitting at high-speed all of the time is bus congestion limited by bandwidth and number of nodes on the system. There are also cases where classical CAN nodes are able to co-exist on a CANFD bus however they may need to be switched off or reset after high-speed CANFD messages are sent between nodes.

Coding CAN & CAN FD in the Arduino IDE

Given the simple changes in the new standard, and the abstraction of low-level coding in open-source software, writing code for the CANFD in the Arduino IDE is pretty simple. There are really only two differences

  • Setting the mode of operation (CAN or CAN FD) and appropriate baud rate for the CAN controller*
  • Loading/unloading of message payload

*Note that the CAN controller is either native to into the microcontroller or an external peripheral such as an SPI chip

Below are a few simple example code snippets:

Setup for two CAN ports, 500K baud:

cCAN_CANFD CanPort0(0, _500K, _500K, MCAN_MODE_CAN);
cCAN_CANFD CanPort1(1, _500K, _500K, MCAN_MODE_CAN);

Setup two for CANFD ports for both 1M and 5M baud:

cCAN_CANFD CanPort0(0, _1M, _5M, MCAN_MODE_EXT_LEN_DUAL_RATE);
cCAN_CANFD CanPort1(1, _1M, _5M, MCAN_MODE_EXT_LEN_DUAL_RATE);

Setup for 64 byte CANFD transmission on ID 0x100. See full examples in GitHub repository linked below.

    //set TX frame to 0x100
    tx1.id  = 0x100;
    tx1.len = 64;
    tx1.data[0] = 0xFE;
    tx1.data[63] = 0x88;

Getting Started with CAN FD

To get going with CAN or CAN FD you will need an open-source microcontroller platform such as the CANFDuino, the Arduino IDE for writing code, and a few examples to get started.

  1. Order hardware from the CANFDuino crowd supply page
  2. Download the Arduino IDE
  3. See the Togglebit GitHub Repository with example code for CANFDuino