Module esp_idf_svc::hal::can

source ·
Expand description

CAN bus peripheral control.

It is called Two-Wire Automotive Interface (TWAI) in ESP32 documentation.

§Example

Create a CAN peripheral and then transmit and receive a message.

use embedded_can::nb::Can;
use embedded_can::Frame;
use embedded_can::StandardId;
use esp_idf_hal::prelude::*;
use esp_idf_hal::can;

let peripherals = Peripherals::take().unwrap();
let pins = peripherals.pins;

// filter to accept only CAN ID 881
let filter = can::config::Filter::Standard {filter: 881, mask: 0x7FF };
// filter that accepts all CAN IDs
// let filter = can::config::Filter::standard_allow_all();

let timing = can::config::Timing::B500K;
let config = can::config::Config::new().filter(filter).timing(timing);
let mut can = can::CanDriver::new(peripherals.can, pins.gpio5, pins.gpio4, &config).unwrap();

let tx_frame = can::Frame::new(StandardId::new(0x042).unwrap(), &[0, 1, 2, 3, 4, 5, 6, 7]).unwrap();
nb::block!(can.transmit(&tx_frame)).unwrap();

if let Ok(rx_frame) = nb::block!(can.receive()) {
   info!("rx {:}:", rx_frame);
}

Modules§

Structs§

Enums§

Type Aliases§