Skip to main content

CanFd

Struct CanFd 

Source
pub struct CanFd<'d, Dm: DriverMode = Blocking> { /* private fields */ }
Expand description

A CAN FD controller.

Implementations§

Source§

impl<'d> CanFd<'d, Blocking>

Source

pub fn set_interrupt_handler(&mut self, handler: InterruptHandler)

Available on crate feature unstable only.

Registers an interrupt handler for this controller.

Replaces any previously registered handler. The handler runs for the sources enabled with CanFd::listen. It must clear them with CanFd::clear_interrupts, or it runs again as soon as it returns.

§Stability

This API is marked as unstable and is only available when the unstable crate feature is enabled. This comes with no stability guarantees, and could be changed or removed at any time.

Source

pub fn listen(&mut self, interrupts: impl Into<EnumSet<CanFdInterrupt>>)

Enables the given interrupt sources.

Without a handler from CanFd::set_interrupt_handler this only makes CanFd::interrupts record what happened.

Source

pub fn unlisten(&mut self, interrupts: impl Into<EnumSet<CanFdInterrupt>>)

Disables the given interrupt sources.

Source

pub fn interrupts(&self) -> EnumSet<CanFdInterrupt>

Returns the interrupt sources that are currently asserted.

Source

pub fn clear_interrupts( &mut self, interrupts: impl Into<EnumSet<CanFdInterrupt>>, )

Clears the given interrupt sources.

Source

pub fn new( twai: impl Instance + 'd, config: Config, ) -> Result<Self, ConfigError>

Creates a new CAN FD driver in Blocking mode.

The controller is configured but stays off the bus. Assign the pins with CanFd::with_rx and CanFd::with_tx, then call CanFd::start.

§Errors

ConfigError when the configuration does not fit the hardware.

Source

pub fn into_async(self) -> CanFd<'d, Async>

Converts the driver into an async driver.

Source§

impl<'d> CanFd<'d, Async>

Source

pub async fn receive_async(&mut self) -> Frame

Waits until a frame is received, then reads it.

§Cancellation Safety

This future is cancel-safe. A frame stays in the hardware RX buffer until it is read, so dropping the future loses nothing.

Source

pub async fn transmit_async(&mut self, frame: &Frame) -> Result<(), Error>

Queues a frame and waits until the hardware finishes with it.

§Errors

Error::ControllerStopped when the controller is not on the bus, and Error::NoFreeTxBuffer when every TX buffer is occupied; see CanFdTx::transmit.

Error::TransmitFailed when the controller gave up on the frame.

Error::TransmitAborted when the transmission was aborted.

§Cancellation Safety

This future is not cancel-safe. Dropping it leaves the frame queued, and the hardware may still transmit it.

Source

pub fn into_blocking(self) -> CanFd<'d, Blocking>

Converts the driver back into a blocking driver.

Source§

impl<'d, Dm: DriverMode> CanFd<'d, Dm>

Source

pub fn with_rx(self, rx: impl PeripheralInput<'d>) -> Self

Assigns the RX pin.

The pin is driven according to Config::with_no_transceiver. That setting is committed to the hardware here and cannot change afterwards.

Source

pub fn with_tx(self, tx: impl PeripheralOutput<'d>) -> Self

Assigns the TX pin.

With Config::with_no_transceiver set, the pin is driven open-drain with a pull-up. Assigning the same pin to CanFd::with_rx then forms a single-node bus that needs no external wiring. That setting is committed to the hardware here and cannot change afterwards.

Source

pub fn apply_config(&mut self, config: &Config) -> Result<(), ConfigError>

Applies a new configuration.

The controller leaves the bus while the configuration is applied. A running driver must be started again with CanFd::start afterwards. A change of clock source also stops a running timestamp counter, because its resolution is derived from that clock. Start it again with CanFd::start_timestamp_timer and use the resolution that it returns.

§Errors

ConfigError when the configuration does not fit the hardware.

ConfigError::TransceiverModeLocked when the configuration changes Config::with_no_transceiver after a pin has been assigned.

Source

pub fn config(&self) -> &Config

Returns the current configuration.

Source

pub fn start(&mut self) -> Result<(), Error>

Joins the bus and waits for the controller to become error-active.

Follows the initialization sequence in TRM 38.3.5.

§Errors

Error::BusIntegrationTimeout when the bus does not go idle-recessive long enough for the controller to join it. The controller stays off the bus in that case, unless it was on the bus before the call.

Source

pub fn stop(&mut self) -> Result<(), Error>

Leaves the bus, following the de-initialization sequence in TRM 38.3.6.

Every TX buffer returns to the empty state without its memory being cleared, and the RX buffer is flushed.

While the controller is on the bus, the driver holds a WakeLock, because Light-sleep mode gates the function clock. Leaving the bus releases the lock. A running timestamp counter does not advance during sleep.

§Errors

Error::AbortTimeout when a transmission does not settle. The controller stays on the bus in that case.

Source

pub fn is_started(&self) -> bool

Returns whether the controller is on the bus.

This is true between a successful CanFd::start and the next CanFd::stop or CanFd::apply_config.

Source

pub fn identity(&self) -> Identity

Returns the identity of the core.

Source

pub fn function_clock_frequency(&self) -> u32

Returns the frequency of the controller’s function clock, in Hz.

Source

pub fn split(&mut self) -> (CanFdRx<'_, Dm>, CanFdTx<'_, Dm>)

Splits the controller into a receiving and a transmitting half.

The two halves can be used at the same time, for example to wait for a frame while a transmission is in flight. They arm different interrupt sources, so neither disturbs the other.

The halves borrow the driver, which keeps the configuration and the teardown: everything that reconfigures the controller or takes it off the bus stays on CanFd and is unavailable while a half is alive. For two independent tasks, split a driver that lives in a static.

Source

pub fn tx_buffer_count(&self) -> u8

Returns the number of TX buffers the hardware provides.

Source

pub fn transmit(&mut self, frame: &Frame) -> Result<u8, Error>

Queues a frame in the first free TX buffer and arms it.

Returns the index of the buffer used. The hardware chooses the order in which armed buffers are sent, not the order in which they were queued; see CanFdTx::transmit.

§Errors

Error::ControllerStopped when the controller is not on the bus, and Error::NoFreeTxBuffer when every TX buffer is occupied; see CanFdTx::transmit.

Source

pub fn tx_buffer_state(&self, index: u8) -> TxBufferState

Returns the state of one TX buffer.

Returns TxBufferState::NotExist for an index the hardware does not have.

Source

pub fn abort_transmit(&mut self, index: u8)

Requests that a queued transmission be aborted.

The buffer moves to “aborted”, or to “abort in progress” if the frame is already on the wire. Does nothing for an index the hardware does not have.

Source

pub fn release_tx_buffer(&mut self, index: u8)

Returns a TX buffer to the empty state, releasing it for reuse.

Does nothing for an index the hardware does not have.

Source

pub fn set_tx_priority(&mut self, index: u8, priority: u8)

Sets a TX buffer’s arbitration priority.

Higher values win, and a value above MAX_TX_PRIORITY counts as that maximum; see CanFdTx::set_tx_priority.

Source

pub fn rx_frame_count(&self) -> u16

Returns the number of complete frames waiting in the RX buffer.

Source

pub fn receive(&mut self) -> Result<Frame, Error>

Reads one frame from the RX buffer.

§Errors

Error::RxBufferEmpty when no complete frame is waiting.

Source

pub fn flush_rx(&mut self)

Discards everything in the RX buffer.

Source

pub fn rx_buffer_size_words(&self) -> u16

Returns the size of the RX buffer in 32-bit words.

Source

pub fn rx_buffer_free_words(&self) -> u16

Returns the free space in the RX buffer in 32-bit words.

Source

pub fn rx_overrun(&self) -> bool

Returns whether the RX buffer has overrun and dropped a frame (TRM 38.3.9.4).

Source

pub fn clear_rx_overrun(&mut self)

Clears the RX buffer overrun flag.

Source

pub fn error_state(&self) -> ErrorState

Returns the current fault confinement state.

Returns ErrorState::BusOff while the controller is off the bus, both before the first CanFd::start and after CanFd::stop; that is how the hardware reports a disabled controller. CanFd::is_started tells the two apart.

Source

pub fn error_warning(&self) -> bool

Returns whether an error counter has reached the error warning limit.

A node at the limit is still ErrorState::Active (TRM 38.3.10).

Source

pub fn receive_error_count(&self) -> u16

Returns the receive error counter.

Source

pub fn transmit_error_count(&self) -> u16

Returns the transmit error counter.

Source

pub fn request_bus_off_recovery(&mut self)

Requests that a bus-off controller rejoin the bus; see CanFdTx::request_bus_off_recovery.

Source

pub fn set_error_warning_limit(&mut self, limit: u8) -> Result<(), Error>

Sets the error warning limit, which defaults to 96.

The hardware only accepts the write in test mode (TRM 38.3.10), which is entered for the write and left again.

§Errors

Error::ControllerRunning when the controller is still on the bus.

Source

pub fn reset_traffic_counters(&mut self)

Resets the received and transmitted frame counters.

Source

pub fn rx_traffic_count(&self) -> u32

Returns the number of frames received since the traffic counters were last reset.

Source

pub fn tx_traffic_count(&self) -> u32

Returns the number of frames transmitted since the traffic counters were last reset.

Source

pub fn set_mask_filter(&mut self, filter: MaskFilter, config: &MaskFilterConfig)

Configures one mask filter and enables it.

A frame reaches the RX buffer if it passes at least one enabled filter (TRM 38.3.9.8). Out of reset, filter A accepts every frame; see CanFd::accept_all.

Source

pub fn disable_mask_filter(&mut self, filter: MaskFilter)

Disables one mask filter.

Source

pub fn set_range_filter(&mut self, config: &RangeFilterConfig)

Configures the range filter and enables it.

Source

pub fn disable_range_filter(&mut self)

Disables the range filter.

Source

pub fn accept_all(&mut self)

Accepts every frame, by giving filter A a zero mask and disabling the rest.

This is the one filter setting that matches standard and extended identifiers at once.

Source

pub fn error_capture(&self) -> ErrorCapture

Returns details of the last bus error the core captured.

Only meaningful after the core has reported a bus error; see ErrorCapture.

Source

pub fn retransmit_count(&self) -> u8

Returns the number of retransmission attempts made for the frame currently being sent.

Source

pub fn nominal_error_count(&self) -> u16

Returns the number of errors detected at the nominal bit rate.

Source

pub fn fd_error_count(&self) -> u16

Returns the number of errors detected at the data bit rate, in the data phase of bit-rate-switched frames.

Source

pub fn transmitter_delay(&self) -> u8

Returns the transmitter delay the core measured, in function clock periods.

The core measures the delay during every FD frame it sends (TRM 38.3.7.2), so the value reads zero until the first one. The secondary sample point sits at this delay plus the configured offset, and the four-bit-time limit of TRM 38.3.7.3 applies to that sum.

Source

pub fn start_timestamp_timer( &mut self, resolution_hz: u32, ) -> Result<u32, ConfigError>

Starts the timestamp counter from zero, at the requested resolution.

Received frames are stamped at the sixth bit of their end-of-frame field, which is when a frame becomes valid. The achievable resolution is the function clock divided by an integer, so the returned value is the resolution actually programmed, in Hz.

§Errors

ConfigError::UnsupportedTimestampResolution when the resolution cannot be reached from the function clock.

ConfigError::TimestampTimerStalled when the counter does not start.

Source

pub fn stop_timestamp_timer(&mut self)

Stops the timestamp counter.

Source

pub fn timestamp(&self) -> u64

Returns the current value of the timestamp counter.

Source

pub fn timestamp_bit_width(&self) -> u8

Returns the width of the timestamp counter in bits.

Trait Implementations§

Source§

impl<Dm: DriverMode> Debug for CanFd<'_, Dm>

Available on crate feature unstable only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl InterruptConfigurable for CanFd<'_, Blocking>

Available on crate feature unstable only.

§Stability

This API is marked as unstable and is only available when the unstable crate feature is enabled. This comes with no stability guarantees, and could be changed or removed at any time.

Source§

fn set_interrupt_handler(&mut self, handler: InterruptHandler)

Registers an interrupt handler for the peripheral. Read more

Auto Trait Implementations§

§

impl<'d, Dm = Blocking> !RefUnwindSafe for CanFd<'d, Dm>

§

impl<'d, Dm = Blocking> !UnwindSafe for CanFd<'d, Dm>

§

impl<'d, Dm> Freeze for CanFd<'d, Dm>
where PhantomData<Dm>: Freeze,

§

impl<'d, Dm> Send for CanFd<'d, Dm>
where PhantomData<Dm>: Send,

§

impl<'d, Dm> Sync for CanFd<'d, Dm>
where PhantomData<Dm>: Sync,

§

impl<'d, Dm> Unpin for CanFd<'d, Dm>
where PhantomData<Dm>: Unpin,

§

impl<'d, Dm> UnsafeUnpin for CanFd<'d, Dm>
where PhantomData<Dm>: UnsafeUnpin,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = !

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.