Skip to main content

Crate can_core

Crate can_core 

Source
Expand description

CAN 2.0 and CAN FD frame codec — identifier parsing, DLC handling, bit-timing calculation, arbitration model.

can-core is the estate’s L0 leaf for Controller Area Network work: a total, allocation-free-at-parse frame codec over the SocketCAN wire format, plus the bit-timing and arbitration models every higher CAN layer shares.

§Zero-copy, total parsing

CanFrame::parse borrows the payload straight out of the input buffer — one buffer in, a typed view out, nothing copied. Every buffer access is bounds-checked: malformed, truncated, or hostile input yields a typed CanError, never a panic (enforced by deny-level lints on the lib target, a cargo-fuzz target, and miri).

§The frame model

A CanFrame is an identifier (CanId: 11-bit standard or 29-bit extended), a payload of 0–64 bytes, the Data Length Code, and the flag bits (is_remote, is_fd, brs, error_state_indicator). Classic CAN payloads cap at 8 bytes with DLC = length; CAN FD maps DLC 0–15 through the table 0,1,2,3,4,5,6,7,8,12,16,20,24,32,48,64 (dlc_to_len/len_to_dlc). Remote frames carry no payload, and CAN FD — which abolished remote frames — rejects them outright.

§Bit timing

timing::BitTiming::calculate solves the standard Bosch bit-timing algorithm from baudrate and clock: prescaler search plus segment split, sample point closest to the CiA default (87.5 % below 800 kbit/s, 75 % above), rendered as SJA1000-style BTR register bytes via timing::BitTiming::btr.

§Arbitration

Lower identifiers win — with the wire-level subtlety that a standard frame beats an extended frame sharing its first 11 id bits (dominant IDE vs recessive SRR). arbitration::dominates models the full comparison; CAN FD changes nothing at arbitration phase (BRS applies only after the winner is decided).

§Example

use can_core::{CanFrame, CanId, CanError};

// Build and round-trip a CAN FD frame:
let id = CanId::extended(0x18FF_0102)?;
let frame = CanFrame::fd(id, &[0xDE, 0xAD, 0xBE, 0xEF], true, false)?;
let bytes = frame.to_bytes();
assert_eq!(bytes.len(), 72);
assert_eq!(CanFrame::parse(&bytes)?, frame);

// Out-of-range ids are rejected, never truncated:
assert!(matches!(
    CanId::standard(0x800),
    Err(CanError::InvalidIdLength { max: 0x7FF, got: 0x800 })
));

§no_std

The crate is no_std + alloc — it builds for embedded targets (thumbv7em-none-eabihf is exercised in CI) and depends only on thiserror for the error taxonomy.

Modules§

arbitration
Arbitration — why lower identifiers win, and what CAN FD changes (nothing where it matters).
timing
CAN bit-timing — the standard Bosch calculation from baudrate and clock.

Structs§

CanFrame
A CAN 2.0 or CAN FD frame — a borrowed, zero-copy view over a payload.

Enums§

CanError
The single crate-level error for the frame codec and bit-timing calculator.
CanId
A CAN identifier: 11-bit (standard) or 29-bit (extended).

Constants§

CLASSIC_FRAME_SIZE
Size of struct can_frame — the SocketCAN classic wire format.
CLASSIC_MAX_LEN
Largest classic CAN 2.0 payload.
EXTENDED_ID_MAX
Largest 29-bit extended identifier.
FD_DLC_TO_LEN
DLC (0–15) → CAN FD payload length in bytes. The first nine entries also serve classic CAN, where the DLC equals the length directly.
FD_FRAME_SIZE
Size of struct canfd_frame — the SocketCAN CAN FD wire format.
FD_MAX_LEN
Largest CAN FD payload.
STANDARD_ID_MAX
Largest 11-bit standard identifier.

Functions§

dlc_to_len
Maps a DLC (0–15) to its CAN FD payload length.
len_to_dlc
Maps a CAN FD payload length to its DLC.