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§
- AnyCan
Fd - Any CAN FD peripheral.
- CanFd
- A CAN FD controller.
- CanFdRx
- Receiving half of a split controller.
- CanFdTx
- Transmitting half of a split controller.
- Classic
Frame - A frame that is known to be classic CAN, for code written against
embedded-can. - Config
- CAN FD driver configuration.
- Error
Capture - Details of the last bus error the core captured.
- Extended
Id - 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.
- Mask
Filter Config - Configuration of one mask filter; see
CanFd::set_mask_filter. - Range
Filter Config - Configuration of the range filter; see
CanFd::set_range_filter. - Standard
Id - Standard 11-bit CAN Identifier (
0..=0x7FF). - Timing
- Bit timing parameters for one phase (nominal or data).
- Timing
Limits - Hardware limits for one set of bit timing parameters.
Enums§
- BusError
Kind - Reason the core last flagged a bus error (TRM 38.3.13.1).
- CanFd
Interrupt - An interrupt source of the controller (TRM 38.4).
- Clock
Source - Clock source for the CAN FD peripheral.
The list of clock signals that the
TWAI0_FUNCTION_CLOCKmultiplexer can output. - Config
Error - Errors that can occur when the configuration is applied.
- Error
- Errors that can occur while the driver operates.
- Error
Position - Where in a frame the core last flagged an error (TRM 38.3.13.1).
- Error
State - Fault confinement state (TRM 38.3.10).
- Frame
Error - Errors that can occur when a frame is created.
- Frame
Formats - Frame formats a filter accepts.
- Id
- A CAN Identifier (standard or extended).
- Mask
Filter - One of the three mask filter instances.
- Mode
- How the controller participates on the bus.
- TxBuffer
State - 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
RTRTHfield can hold. - MAX_
TX_ PRIORITY - Largest TX buffer priority the
TXTnPfields can hold. - NOMINAL_
TIMING_ LIMITS - Limits for the nominal (arbitration phase) bit timing.
Traits§
- Instance
- A peripheral singleton compatible with the CAN FD driver.