Skip to content

libcanard: Cyphal/CAN and DroneCAN

lbuild module: modm:ext:libcanard

Compact implementation of the Cyphal/CAN protocol stack in C for high-integrity real-time embedded systems. Supports Cyphal v1.1 and v1.0 on Classic CAN and CAN FD, and the legacy UAVCAN v0/DroneCAN on Classic CAN.

See https://github.com/OpenCyphal/libcanard.

modm::canard::Node

modm::canard::Node<Can> connects libcanard to a modm CAN driver: it owns a canard_t, a private o1heap arena for the memory of libcanard, the receive loop and the acceptance filters. Use the libcanard C API on node.canard with the data types of the modm:ext:dronecan module or your own serialization code.

#include <modm/ext/canard/canard.hpp>

modm::canard::Node<Fdcan1> node;
node.initialize(42, /* unique seed */ 0x1234);

while (true)
{
    node.update();  // receives, transmits and updates the filters
    // libcanard API on node.canard
}

Subscription callbacks receive the payload and must free it with modm::canard::release(subscription, payload). They must not call libcanard.

Cyphal

A Cyphal node publishes and subscribes with the canard_*() functions of libcanard. For CAN FD, set modm:architecture:can:message.buffer to 64 and initialize the CAN driver with a data bitrate.

DroneCAN

libcanard sends and receives DroneCAN (UAVCAN v0) transfers with the canard_v0_*() functions, always as Classic CAN frames. The modm:ext:dronecan module generates the data types and provides typed helpers for them.

Memory

libcanard allocates memory for every queued frame, received transfer and remote node, so the bus traffic decides how much it needs. By default each node has a private o1heap arena of 4096 bytes (Node<Can, ArenaSize>), so that a busy bus cannot exhaust the application heap. The allocations take constant time, and an arena of twice the peak usage never fails due to fragmentation. Use node.heapDiagnostics() to find the peak usage and size the arena.

With Node<Can, 0> libcanard uses malloc() and free() of the application heap instead, which needs the modm:platform:heap module.

Hosted

On hosted targets, the CAN drivers are objects, which CanInstance adapts to the static interface of the node:

modm::platform::SocketCan can;
modm::canard::Node<modm::canard::CanInstance<can>> node;

can.open("vcan0");
node.initialize(42, seed);

CAN Driver Configuration

libcanard keeps its transmit queue sorted by priority and drops frames whose deadline has passed. Frames that are already in the queues of the CAN driver cannot be dropped anymore, so keep the driver queues short: for the STM32 FDCAN disable the software buffer (buffer.tx = 0) and use the priority mode of the hardware queue (tx_hw_queue_mode = Priority). The bxCAN always has a software buffer, which is sorted by priority as well.

libcanard configures the extended acceptance filters of the STM32 FDCAN and bxCAN drivers, which needs one CAN controller per node: for several nodes on one controller initialize them with {.filters = false} and configure a filter that accepts all extended frames. On the bxCAN, the extended filters use all filter banks of the instance, so they replace the filters set with CanFilter.

libcanard treats a received frame with its own node-ID as a node-ID collision, so a node with a CAN controller in loopback mode must drop the echoes of its own frames with {.loopback = true}. Several nodes on one controller read the driver once and pass each frame to all nodes with ingest().

This module is only available for hosted, nrf, rp, sam, stm32.

Dependencies

modm:ext:libcanard modm_ext_libcanard modm: ext: libcanard modm_architecture_assert modm: architecture: assert modm_ext_libcanard->modm_architecture_assert modm_architecture_can modm: architecture: can modm_ext_libcanard->modm_architecture_can modm_architecture_clock modm: architecture: clock modm_ext_libcanard->modm_architecture_clock modm_ext_o1heap modm: ext: o1heap modm_ext_libcanard->modm_ext_o1heap

Limited availability: Check with 'lbuild discover' if this module is available for your target!