pub struct Serial<Usart, Pins> { /* private fields */ }
Expand description

Serial abstraction

This is an abstraction of the UART peripheral intended to be used for standard duplex serial communication.

Implementations§

source§

impl<Usart, Tx, Rx> Serial<Usart, (Tx, Rx)>where Usart: Instance,

source

pub fn new<Config>( usart: Usart, pins: (Tx, Rx), config: Config, clocks: Clocks, apb: &mut <Usart as RccBus>::Bus ) -> Selfwhere Usart: Instance, Tx: TxPin<Usart>, Rx: RxPin<Usart>, Config: Into<Config>,

Configures a USART peripheral to provide serial communication

Panics

Panics if the configured baud rate is impossible for the hardware to setup.

source

pub unsafe fn peripheral(&mut self) -> &mut Usart

Get access to the underlying register block.

Safety

This function is not memory unsafe per se, but does not guarantee anything about assumptions of invariants made in this implementation.

Changing specific options can lead to un-expected behavior and nothing is guaranteed.

source

pub fn free(self) -> (Usart, (Tx, Rx))

Releases the USART peripheral and associated pins

source§

impl<Usart, Pins> Serial<Usart, Pins>where Usart: Instance,

source

pub fn read_data_register(&self) -> Option<u8>

Serial read out of the read register

No error handling and no additional side-effects, besides the implied side-effects when reading out the RDR register. Handling errors has to be done manually. This can be done, by checking the triggered events via Serial::triggered_events.

Returns None if the hardware is busy.

Embedded HAL

To have a more managed way to read from the serial use the embedded_hal::serial::Read trait implementation.

source

pub fn is_busy(&mut self) -> bool

Check if the USART peripheral is busy.

This can be useful to block on to synchronize between peripheral and CPU because of the asynchronous nature of the peripheral.

source

pub fn interrupt(&self) -> <Usart as InterruptNumber>::Interrupt

Obtain the associated interrupt number for the serial peripheral.

Used to unmask / enable the interrupt with cortex_m::peripheral::NVIC::unmask(). This is useful for all cortex_m::peripheral::INTERRUPT functions.

Note

This is the easier alternative to obatain the interrupt for:

use cortex_m::peripheral::INTERRUPT;
use stm32f3xx_hal::pac::USART1;
use stm32f3xx_hal::interrupt::InterruptNumber;

const INTERRUPT: Interrupt = <USART1 as InterruptNumber>::INTERRUPT;

though this function can not be used in a const context.

source

pub fn enable_interrupt(&mut self, event: Event)

Enable the interrupt for the specified Event.

source

pub fn disable_interrupt(&mut self, event: Event)

Disable the interrupt for the specified Event.

source

pub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Switch>)

Enable or disable the interrupt for the specified Event.

source

pub fn configure_interrupts(&mut self, events: EnumSet<Event>)

Enable or disable interrupt for the specified Events.

Like Serial::configure_interrupt, but instead using an enumset. The corresponding interrupt for every Event in the set will be enabled, every other interrupt will be disabled.

source

pub fn is_interrupt_configured(&self, event: Event) -> bool

Check if an interrupt is configured for the Event

source

pub fn configured_interrupts(&mut self) -> EnumSet<Event>

Check which interrupts are enabled for all Events

source

pub fn is_event_triggered(&self, event: Event) -> bool

Check if an interrupt event happend.

source

pub fn triggered_events(&self) -> EnumSet<Event>

Get an EnumSet of all fired interrupt events.

Examples

This allows disabling all fired event at once, via the enum set abstraction, like so

for event in serial.events() {
    serial.listen(event, false);
}
source

pub fn clear_event(&mut self, event: Event)

Clear the given interrupt event flag.

source

pub fn clear_events(&mut self)

Clear all interrupt events.

source

pub fn detect_overrun(&mut self, enable: bool)

Enable or disable overrun detection

When overrun detection is disabled and new data is received while the Event::ReceiveDataRegisterNotEmpty flag is still set, the Event::OverrunError flag is not set and the new received data overwrites the previous content of the RDR register.

source

pub fn set_match_character(&mut self, char: u8)

Configuring the UART to match each received character, with the configured one.

If the character is matched Event::CharacterMatch is generated, which can fire an interrupt, if enabled via Serial::configure_interrupt()

source

pub fn match_character(&self) -> u8

Read out the configured match character.

source§

impl<Usart, Tx, Rx> Serial<Usart, (Tx, Rx)>where Usart: Instance + ReceiverTimeoutExt,

source

pub fn set_receiver_timeout(&mut self, value: Option<u32>)

Set the receiver timeout value.

The RTOF flag (Event::ReceiverTimeout) is set if, after the last received character, no new start bit is detected for more than the receiver timeout value, where the value is being a counter, which is decreased by the configured baud rate.

A simple calculation might be time_per_counter_value = 1 / configured_baud_rate

Note
  • If the value is None, the receiver timeout feature is disabled.
  • This value must only be programmed once per received character.
  • Can be written on the fly. If the new value is lower than or equal to the counter, the RTOF flag is set.
  • Values higher than 24 bits are truncated to 24 bit max (16_777_216).
source

pub fn receiver_timeout(&self) -> Option<u32>

Read out the currently set timeout value

The relationship between the unit value and time is described in Serial::receiver_timeout.

  • If the value is None, the receiver timeout feature is disabled.
source§

impl<Usart, Pins> Serial<Usart, Pins>where Usart: Instance + Dma,

source

pub fn read_exact<B, C>(self, buffer: B, channel: C) -> Transfer<B, C, Self>where Self: OnChannel<C>, B: WriteBuffer<Word = u8> + 'static, C: Channel,

Fill the buffer with received data using DMA.

source

pub fn write_all<B, C>(self, buffer: B, channel: C) -> Transfer<B, C, Self>where Self: OnChannel<C>, B: ReadBuffer<Word = u8> + 'static, C: Channel,

Transmit all data in the buffer using DMA.

Trait Implementations§

source§

impl<Pins> Debug for Serial<UART4, Pins>

source§

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

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

impl<Pins> Debug for Serial<UART5, Pins>

source§

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

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

impl<Pins> Debug for Serial<USART1, Pins>

source§

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

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

impl<Pins> Debug for Serial<USART2, Pins>

source§

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

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

impl<Pins> Debug for Serial<USART3, Pins>

source§

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

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

impl<Usart, Pins> Target for Serial<Usart, Pins>where Usart: Instance + Dma,

source§

fn enable_dma(&mut self)

Enable DMA on the target
source§

fn disable_dma(&mut self)

Disable DMA on the target
source§

impl<Usart, Pins> Write for Serial<Usart, Pins>where Serial<Usart, Pins>: Write<u8>,

source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more
source§

impl<Usart, Tx, Rx> Read<u8> for Serial<Usart, (Tx, Rx)>where Usart: Instance,

source§

fn read(&mut self) -> Result<u8, Error>

Getting back an error means that the error is defined as “handled”:

This implementation has the side effect for error handling, that the Event flag of the returned Error is cleared.

This might be a problem, because if an interrupt is enabled for this particular flag, the interrupt handler might not have the chance to find out from which flag the interrupt originated.

So this function is only intended to be used for direct error handling and not leaving it up to the interrupt handler.

To read out the content of the read register without internal error handling, use embedded_hal::serial::Read. …

§

type Error = Error

Read error
source§

impl<Usart, Pins> Write<u8> for Serial<Usart, Pins>where Usart: Instance,

§

type Error = Infallible

Write error
source§

fn flush(&mut self) -> Result<(), Infallible>

Ensures that none of the previously written words are still buffered
source§

fn write(&mut self, byte: u8) -> Result<(), Infallible>

Writes a single word to the serial interface
source§

impl<USART, TX, RX> Default<u8> for Serial<USART, (TX, RX)>where USART: Instance,

source§

impl<Tx, Rx> OnChannel<C2> for Serial<USART3, (Tx, Rx)>

source§

impl<Tx, Rx> OnChannel<C3> for Serial<UART4, (Tx, Rx)>

source§

impl<Tx, Rx> OnChannel<C3> for Serial<USART3, (Tx, Rx)>

source§

impl<Tx, Rx> OnChannel<C4> for Serial<USART1, (Tx, Rx)>

source§

impl<Tx, Rx> OnChannel<C5> for Serial<UART4, (Tx, Rx)>

source§

impl<Tx, Rx> OnChannel<C5> for Serial<USART1, (Tx, Rx)>

source§

impl<Tx, Rx> OnChannel<C6> for Serial<USART2, (Tx, Rx)>

source§

impl<Tx, Rx> OnChannel<C7> for Serial<USART2, (Tx, Rx)>

Auto Trait Implementations§

§

impl<Usart, Pins> RefUnwindSafe for Serial<Usart, Pins>where Pins: RefUnwindSafe, Usart: RefUnwindSafe,

§

impl<Usart, Pins> Send for Serial<Usart, Pins>where Pins: Send, Usart: Send,

§

impl<Usart, Pins> Sync for Serial<Usart, Pins>where Pins: Sync, Usart: Sync,

§

impl<Usart, Pins> Unpin for Serial<Usart, Pins>where Pins: Unpin, Usart: Unpin,

§

impl<Usart, Pins> UnwindSafe for Serial<Usart, Pins>where Pins: UnwindSafe, Usart: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

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

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

Performs the conversion.
source§

impl<S, Word> Write<Word> for Swhere S: Default<Word>, Word: Clone,

§

type Error = <S as Write<Word>>::Error

The type of error that can occur when writing
source§

fn bwrite_all( &mut self, buffer: &[Word] ) -> Result<(), <S as Write<Word>>::Error>

Writes a slice, blocking until everything has been written Read more
source§

fn bflush(&mut self) -> Result<(), <S as Write<Word>>::Error>

Block until the serial interface has sent all buffered words