Struct stm32f3xx_hal::serial::Serial
source · 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,
impl<Usart, Tx, Rx> Serial<Usart, (Tx, Rx)>where Usart: Instance,
sourcepub 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>,
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.
sourcepub unsafe fn peripheral(&mut self) -> &mut Usart
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§impl<Usart, Pins> Serial<Usart, Pins>where
Usart: Instance,
impl<Usart, Pins> Serial<Usart, Pins>where Usart: Instance,
sourcepub fn read_data_register(&self) -> Option<u8>
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.
sourcepub fn is_busy(&mut self) -> bool
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.
sourcepub fn interrupt(&self) -> <Usart as InterruptNumber>::Interrupt
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.
sourcepub fn enable_interrupt(&mut self, event: Event)
pub fn enable_interrupt(&mut self, event: Event)
Enable the interrupt for the specified Event
.
sourcepub fn disable_interrupt(&mut self, event: Event)
pub fn disable_interrupt(&mut self, event: Event)
Disable the interrupt for the specified Event
.
sourcepub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Switch>)
pub fn configure_interrupt(&mut self, event: Event, enable: impl Into<Switch>)
Enable or disable the interrupt for the specified Event
.
sourcepub fn configure_interrupts(&mut self, events: EnumSet<Event>)
pub fn configure_interrupts(&mut self, events: EnumSet<Event>)
Enable or disable interrupt for the specified Event
s.
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.
sourcepub fn is_interrupt_configured(&self, event: Event) -> bool
pub fn is_interrupt_configured(&self, event: Event) -> bool
Check if an interrupt is configured for the Event
sourcepub fn configured_interrupts(&mut self) -> EnumSet<Event>
pub fn configured_interrupts(&mut self) -> EnumSet<Event>
Check which interrupts are enabled for all Event
s
sourcepub fn is_event_triggered(&self, event: Event) -> bool
pub fn is_event_triggered(&self, event: Event) -> bool
Check if an interrupt event happend.
sourcepub fn triggered_events(&self) -> EnumSet<Event>
pub fn triggered_events(&self) -> EnumSet<Event>
sourcepub fn clear_event(&mut self, event: Event)
pub fn clear_event(&mut self, event: Event)
Clear the given interrupt event flag.
sourcepub fn clear_events(&mut self)
pub fn clear_events(&mut self)
Clear all interrupt events.
sourcepub fn detect_overrun(&mut self, enable: bool)
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.
sourcepub fn set_match_character(&mut self, char: u8)
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()
sourcepub fn match_character(&self) -> u8
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,
impl<Usart, Tx, Rx> Serial<Usart, (Tx, Rx)>where Usart: Instance + ReceiverTimeoutExt,
sourcepub fn set_receiver_timeout(&mut self, value: Option<u32>)
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
).
sourcepub fn receiver_timeout(&self) -> Option<u32>
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.
Trait Implementations§
source§impl<Usart, Pins> Target for Serial<Usart, Pins>where
Usart: Instance + Dma,
impl<Usart, Pins> Target for Serial<Usart, Pins>where Usart: Instance + Dma,
source§fn enable_dma(&mut self)
fn enable_dma(&mut self)
source§fn disable_dma(&mut self)
fn disable_dma(&mut self)
source§impl<Usart, Pins> Write for Serial<Usart, Pins>where
Serial<Usart, Pins>: Write<u8>,
impl<Usart, Pins> Write for Serial<Usart, Pins>where Serial<Usart, Pins>: Write<u8>,
source§impl<Usart, Tx, Rx> Read<u8> for Serial<Usart, (Tx, Rx)>where
Usart: Instance,
impl<Usart, Tx, Rx> Read<u8> for Serial<Usart, (Tx, Rx)>where Usart: Instance,
source§fn read(&mut self) -> Result<u8, Error>
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
.
…