Available on crate feature
unstable
only.Expand description
§Two-wire Automotive Interface (TWAI)
§Overview
The TWAI is a multi-master, multi-cast communication protocol with error detection and signaling and inbuilt message priorities and arbitration. The TWAI protocol is suited for automotive and industrial applications.
See ESP-IDF’s TWAI documentation for a summary on the protocol.
§Configuration
The driver offers functions for initializing the TWAI peripheral, setting up the timing parameters, configuring acceptance filters, handling interrupts, and transmitting/receiving messages on the TWAI bus.
This driver manages the ISO 11898-1 compatible TWAI controllers. It supports Standard Frame Format (11-bit) and Extended Frame Format (29-bit) frame identifiers.
§Examples
§Transmitting and Receiving Messages
// Use GPIO pins 2 and 3 to connect to the respective pins on the TWAI
// transceiver.
let twai_rx_pin = peripherals.GPIO3;
let twai_tx_pin = peripherals.GPIO2;
// The speed of the TWAI bus.
const TWAI_BAUDRATE: twai::BaudRate = BaudRate::B1000K;
// Begin configuring the TWAI peripheral. The peripheral is in a reset like
// state that prevents transmission but allows configuration.
let mut twai_config = twai::TwaiConfiguration::new(
peripherals.TWAI0,
twai_rx_pin,
twai_tx_pin,
TWAI_BAUDRATE,
TwaiMode::Normal
);
// Partially filter the incoming messages to reduce overhead of receiving
// undesired messages
twai_config.set_filter(const { SingleStandardFilter::new(b"xxxxxxxxxx0",
b"x", [b"xxxxxxxx", b"xxxxxxxx"]) });
// Start the peripheral. This locks the configuration settings of the
// peripheral and puts it into operation mode, allowing packets to be sent
// and received.
let mut twai = twai_config.start();
loop {
// Wait for a frame to be received.
let frame = block!(twai.receive()).unwrap();
// Transmit the frame back.
let _result = block!(twai.transmit(&frame)).unwrap();
}
§Self-testing (self reception of transmitted messages)
// Use GPIO pins 2 and 3 to connect to the respective pins on the TWAI
// transceiver.
let can_rx_pin = peripherals.GPIO3;
let can_tx_pin = peripherals.GPIO2;
// The speed of the TWAI bus.
const TWAI_BAUDRATE: twai::BaudRate = BaudRate::B1000K;
// Begin configuring the TWAI peripheral.
let mut can_config = twai::TwaiConfiguration::new(
peripherals.TWAI0,
can_rx_pin,
can_tx_pin,
TWAI_BAUDRATE,
TwaiMode::SelfTest
);
// Partially filter the incoming messages to reduce overhead of receiving
// undesired messages
can_config.set_filter(const { SingleStandardFilter::new(b"xxxxxxxxxx0",
b"x", [b"xxxxxxxx", b"xxxxxxxx"]) });
// Start the peripheral. This locks the configuration settings of the
// peripheral and puts it into operation mode, allowing packets to be sent
// and received.
let mut can = can_config.start();
let frame = EspTwaiFrame::new_self_reception(StandardId::ZERO,
&[1, 2, 3]).unwrap(); // Wait for a frame to be received.
let frame = block!(can.receive()).unwrap();
Modules§
- Two-wire Automotive Interface (TWAI) Filters
Structs§
- Any TWAI peripheral.
- A TWAI Frame.
- Extended 29-bit TWAI Identifier (
0..=1FFF_FFFF
). - Standard 11-bit TWAI Identifier (
0..=0x7FF
). - The underlying timings for the TWAI peripheral.
- An active TWAI peripheral in Normal Mode.
- An inactive TWAI peripheral in the “Reset”/configuration state.
- Interface to the TWAI receiver part.
- Interface to the TWAI transmitter part.
Enums§
- A selection of pre-determined baudrates for the TWAI driver. Currently these timings are sourced from the ESP IDF C driver which assumes an APB clock of 80MHz.
- TWAI error kind
- Represents errors that can occur in the TWAI driver. This enum defines the possible errors that can be encountered when interacting with the TWAI peripheral.
- A TWAI Identifier (standard or extended).
- Specifies in which mode the TWAI controller will operate.