Controller Area Network (CAN)
Overview
RTL8721F provides a CAN interface, a multi-master serial bus for real-time control networks using standard or extended frame identifiers. The Rust driver ameba_hal::can wraps the PAC register block directly — no raw bit-masking or C FFI is involved.
Features
Two peripheral instances,
CAN0_PTRandCAN1_PTR, both accessed throughCan::stealStandard 11-bit or Extended 29-bit frame identifiers via
IdType::Standard/IdType::Extended, and Data or Remote frame type viaFrameType::Data/FrameType::RemoteFour bus test modes via
WorkMode::Normal/Silence/ExternalLoopback/InternalLoopback, withInternalLoopbackletting a message be sent and received without any physical bus attachedConfigurable bit timing through
BitTiming, or computed automatically for a target bit rate viaCan::calc_bit_timing16 RAM message buffers, each independently configurable for TX or RX with its own ID filter and mask via
write_msg/set_rx_msg_buf, plus a dedicated Rx FIFO virtual buffer read throughread_rx_from_fifoAuto-reply support, letting a buffer transmit data automatically when it receives a matching remote frame via
tx_auto_reply/rx_auto_replyOptional per-buffer RX DMA via
msgbuf_ctrl_config, writing incoming frames directly into RAM throughrx_dma_dest_base_addrBus-off and error-passive monitoring through
bus_status,err_cnt_status, and separate TX/RX error countersRaw interrupt flag control via
IntFlagsand per-buffer TX/RX interrupt enables, polled or handled by a user-registered NVIC handler rather than a driver-managed callback
Note
The driver exposes CAN 2.0 message buffers with a 64-bit data field, and there is no embedded-hal CAN trait implementation, since embedded-hal does not yet define a stable CAN interface.
Operating Modes
CAN exposes a single Blocking Mode: all transfers go through direct, poll-driven register access.
Blocking Mode
The CPU writes a message buffer with write_msg and checks completion with msgbuf_tx_done or the raw interrupt flags, and reads received frames with read_msg or read_rx_from_fifo. There is no async or DMA-backed transfer API, and RX DMA only offloads copying a received frame into RAM, not the CPU-driven initiation and completion check.
use ameba_hal::{
can::{CAN0_PTR, Can, CanConfig, FrameType, IdType, TxMsg},
pinmux::{self, Pin, PinFunction, Pull},
println,
};
...
pinmux::set(Pin::PA25, PinFunction::CAN0_TX);
pinmux::set(Pin::PA26, PinFunction::CAN0_RX);
// Pull STB pin low so an attached transceiver is put into normal mode.
pinmux::set_pull(Pin::PA3, Pull::Down);
let can = unsafe { Can::steal(CAN0_PTR) };
can.init(&CanConfig::default());
can.enable();
can.bus_on();
...
let mut data = [0u32; 16];
data[0] = u32::from_le_bytes([0x01, 0x02, 0x03, 0x04]);
data[1] = u32::from_le_bytes([0x05, 0x06, 0x07, 0x08]);
let msg = TxMsg {
buf_idx: 0,
frame_type: FrameType::Data,
id_type: IdType::Standard,
std_id: 0x123,
ext_id: 0,
dlc: 8,
data,
};
can.write_msg(&msg);
Full example: examples/can/src/main.rs. The example only demonstrates TX. read_msg / read_rx_from_fifo are described above from the driver API, not from this example.
Application Examples
The Rust SDK provides one example project under examples/can/:
-
CAN0, initializes the bus and transmits a single Standard ID
0x123Data frame with 8 bytes of payload.Wiring: CAN0 — TX = PA25, RX = PA26. No external bus is required — the example sends one frame without waiting for an ACK, so it completes even with nothing attached.
[CAN] start [CAN] Sending frame id=0x123 [PASS] CAN TX: done
Note
Compile the example with cargo xtask build example --name can.
API Reference
Quick Usage
use ameba_hal::can::{CAN0_PTR, Can, CanConfig, FrameType, IdType, TxMsg};
let can = unsafe { Can::steal(CAN0_PTR) };
can.init(&CanConfig::default());
can.enable();
can.bus_on();
let mut data = [0u32; 16];
data[0] = u32::from_le_bytes([0x01, 0x02, 0x03, 0x04]);
let tx = TxMsg {
buf_idx: 0,
frame_type: FrameType::Data,
id_type: IdType::Standard,
std_id: 0x123,
ext_id: 0,
dlc: 4,
data,
};
can.write_msg(&tx);
Can also exposes set_rx_msg_buf / read_msg / read_rx_from_fifo for receiving frames, and calc_bit_timing for computing BitTiming from a target bit rate — see the Operating Modes section above.