Skip to main content

Module canfd

Module canfd 

Source
Available on crate feature unstable only.
Expand description

§Controller Area Network Flexible Data-Rate (CAN FD)

§Overview

The CAN FD controller is a CTU CAN FD core. It is a different peripheral from the classic two-wire automotive interface (TWAI) controller. It supports classic CAN 2.0 frames and CAN FD frames. A CAN FD frame carries up to 64 bytes of payload and can switch to a faster bit rate for its data phase.

The peripheral needs an external transceiver to drive a real bus. ESP-IDF documentation

§Configuration

Config selects the bus participation Mode, the function ClockSource, and the bit Timing of the arbitration and data phases. The controller stays off the bus until CanFd::start is called, so the pins can be assigned first.

Received frames pass through three mask filters and one range filter; see MaskFilterConfig and RangeFilterConfig. Each filter matches either standard or extended identifiers, and accepts classic CAN frames, CAN FD frames or both, as set by FrameFormats.

§Usage

CanFd::into_async converts the driver into an async driver. Received frames stay in the hardware RX buffer until they are read, so CanFd::receive_async is cancel-safe and the driver needs no software frame queue.

Identifiers are the embedded_can types. ClassicFrame implements embedded_can::Frame and BusErrorKind implements embedded_can::Error, for code written against those traits.

§Examples

§Sending a CAN FD frame

use esp_hal::canfd::{CanFd, Config, Frame, StandardId};

let mut canfd = CanFd::new(peripherals.TWAI0, Config::default())?
    .with_rx(peripherals.GPIO9)
    .with_tx(peripherals.GPIO8);
canfd.start()?;

// A 64-byte frame, sent with the data phase at the FD bit rate.
let id = StandardId::new(0x123).unwrap();
let frame = Frame::new_fd(id, &[0xAA; 64])?.with_bit_rate_switch(true);
canfd.transmit(&frame)?;

§Receiving a range of identifiers

Filter A accepts every frame out of reset. Configure it to accept only CAN FD frames with the standard identifiers 0x120–0x12F.

use esp_hal::canfd::{
    CanFd,
    Config,
    FrameFormats,
    MaskFilter,
    MaskFilterConfig,
    StandardId,
};

let mut canfd = CanFd::new(peripherals.TWAI0, Config::default())?
    .with_rx(peripherals.GPIO9)
    .with_tx(peripherals.GPIO8);

let id = StandardId::new(0x120).unwrap();
let mask = StandardId::new(0x7F0).unwrap();
canfd.set_mask_filter(
    MaskFilter::A,
    &MaskFilterConfig::standard(id, mask).with_formats(FrameFormats::Fd),
);
canfd.start()?;

§Implementation State

  • Time-triggered transmission is not exposed.
  • TX buffer backup mode and RAM parity protection are not exposed.

“TRM” in this module means the ESP32-C5 TRM v1.1, chapter 38. Technical Reference Manual

Structs§

AnyCanFd
Any CAN FD peripheral.
CanFd
A CAN FD controller.
CanFdRx
Receiving half of a split controller.
CanFdTx
Transmitting half of a split controller.
ClassicFrame
A frame that is known to be classic CAN, for code written against embedded-can.
Config
CAN FD driver configuration.
ErrorCapture
Details of the last bus error the core captured.
ExtendedId
Extended 29-bit CAN Identifier (0..=1FFF_FFFF).
Frame
A CAN FD frame.
Identity
Identity of a CAN FD controller, read out of the hardware.
MaskFilterConfig
Configuration of one mask filter; see CanFd::set_mask_filter.
RangeFilterConfig
Configuration of the range filter; see CanFd::set_range_filter.
StandardId
Standard 11-bit CAN Identifier (0..=0x7FF).
Timing
Bit timing parameters for one phase (nominal or data).
TimingLimits
Hardware limits for one set of bit timing parameters.

Enums§

BusErrorKind
Reason the core last flagged a bus error (TRM 38.3.13.1).
CanFdInterrupt
An interrupt source of the controller (TRM 38.4).
ClockSource
Clock source for the CAN FD peripheral. The list of clock signals that the TWAI0_FUNCTION_CLOCK multiplexer can output.
ConfigError
Errors that can occur when the configuration is applied.
Error
Errors that can occur while the driver operates.
ErrorPosition
Where in a frame the core last flagged an error (TRM 38.3.13.1).
ErrorState
Fault confinement state (TRM 38.3.10).
FrameError
Errors that can occur when a frame is created.
FrameFormats
Frame formats a filter accepts.
Id
A CAN Identifier (standard or extended).
MaskFilter
One of the three mask filter instances.
Mode
How the controller participates on the bus.
TxBufferState
State of a single TX buffer (TRM figure 38.3-8).

Constants§

FD_TIMING_LIMITS
Limits for the data phase bit timing.
MAX_RETRANSMIT_LIMIT
Largest retransmission limit the RTRTH field can hold.
MAX_TX_PRIORITY
Largest TX buffer priority the TXTnP fields can hold.
NOMINAL_TIMING_LIMITS
Limits for the nominal (arbitration phase) bit timing.

Traits§

Instance
A peripheral singleton compatible with the CAN FD driver.