Struct stm32wlxx_hal::uart::Uart2

source ·
pub struct Uart2<RX, TX> { /* private fields */ }
Expand description

UART2 driver.

Implementations§

source§

impl Uart2<NoRx, NoTx>

source

pub fn new( uart: USART2, baud: u32, clk: Clk, rcc: &mut RCC ) -> Uart2<NoRx, NoTx>

Create a new UART driver from a UART peripheral.

This will enable clocks and reset the UART peripheral.

Example
use stm32wlxx_hal::{
    pac,
    uart::{self, NoRx, NoTx, Uart2},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

// enable the HSI16 source clock
dp.RCC.cr.modify(|_, w| w.hsion().set_bit());
while dp.RCC.cr.read().hsirdy().is_not_ready() {}

let uart: Uart2<NoRx, NoTx> = Uart2::new(dp.USART2, 115_200, uart::Clk::Hsi16, &mut dp.RCC);
source§

impl<RX, TX> Uart2<RX, TX>

source

pub fn clock_hz(&self, rcc: &RCC) -> u32

Calculate the clock frequency.

Fractional frequencies will be rounded towards zero.

source§

impl Uart2<NoRx, NoTx>

source

pub unsafe fn pulse_reset(rcc: &mut RCC)

Reset the UART.

Safety
  1. The UART must not be in-use.
  2. You are responsible for setting up the UART after a reset.
Example

See steal

source§

impl Uart2<NoRx, NoTx>

source

pub fn enable_clock(rcc: &mut RCC)

Enable the UART clock.

This is done for you in new

Example
use stm32wlxx_hal::{pac, uart::LpUart};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
LpUart::enable_clock(&mut dp.RCC);
source

pub unsafe fn disable_clock(rcc: &mut RCC)

Disable the UART clock.

Safety
  1. You are responsible for ensuring the UART is in a state where the clock can be disabled without entering an error state.
  2. You cannot use the UART while the clock is disabled.
  3. You are responsible for re-enabling the clock before resuming use of the UART.
  4. You are responsible for setting up anything that may have lost state while the clock was disabled.
source§

impl Uart2<NoRx, NoTx>

source

pub unsafe fn steal() -> Uart2<NoRx, NoTx>

Steal the UART peripheral from whatever is currently using it.

This will not initialize the peripheral (unlike new).

Safety
  1. Ensure that the code stealing the UART has exclusive access to the peripheral. Singleton checks are bypassed with this method.
  2. You are responsible for setting up the UART correctly.
Example
use stm32wlxx_hal::{
    pac,
    uart::{LpUart, NoRx, NoTx},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

LpUart::enable_clock(&mut dp.RCC);
// safety:
// 1. Nothing else is using the LPUART in this code.
// 2. This code performs setup for the LPUART.
unsafe { LpUart::pulse_reset(&mut dp.RCC) };

// safety:
// 1. Nothing else is using the LPUART in this code.
// 2. The LPUART has been setup, clocks are enabled and the LPUART has been reset.
let mut lpuart: LpUart<NoRx, NoTx> = unsafe { LpUart::steal() };
source§

impl<RX, TX> Uart2<RX, TX>

source

pub fn free(self) -> USART2

Free the UART peripheral from the driver.

Example
use stm32wlxx_hal::{
    pac,
    uart::{self, LpUart, NoRx, NoTx},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let lpuart: pac::LPUART = dp.LPUART;

let lpuart: LpUart<NoRx, NoTx> =
    LpUart::new(lpuart, 115_200, uart::Clk::Hsi16, &mut dp.RCC);
// ... use LPUART
let lpuart: pac::LPUART = lpuart.free();
source§

impl<RX> Uart2<RX, NoTx>

source

pub fn enable_tx<TX: Uart2Tx>( self, tx: TX, cs: &CriticalSection ) -> Uart2<RX, TX>

Enable the UART transmitter.

Example
use stm32wlxx_hal::{
    cortex_m,
    gpio::{pins, PortB},
    pac,
    uart::{self, LpUart, NoRx},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

// enable the HSI16 source clock
dp.RCC.cr.modify(|_, w| w.hsion().set_bit());
while dp.RCC.cr.read().hsirdy().is_not_ready() {}

let gpiob: PortB = PortB::split(dp.GPIOB, &mut dp.RCC);
let uart: LpUart<NoRx, pins::B11> = cortex_m::interrupt::free(|cs| {
    LpUart::new(dp.LPUART, 115_200, uart::Clk::Hsi16, &mut dp.RCC)
        .enable_tx(gpiob.b11, cs)
});
source§

impl<RX> Uart2<RX, NoTx>

source

pub fn enable_tx_dma<TxPin: Uart2Tx, TxDma: DmaCh>( self, tx: TxPin, tx_dma: TxDma, cs: &CriticalSection ) -> Uart2<RX, (TxPin, TxDma)>

Enable the UART transmitter with a DMA channel.

Example
use stm32wlxx_hal::{
    cortex_m,
    dma::{AllDma, Dma2Ch7},
    gpio::{pins, PortB},
    pac,
    uart::{self, LpUart, NoRx},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

// enable the HSI16 source clock
dp.RCC.cr.modify(|_, w| w.hsion().set_bit());
while dp.RCC.cr.read().hsirdy().is_not_ready() {}

let dma: AllDma = AllDma::split(dp.DMAMUX, dp.DMA1, dp.DMA2, &mut dp.RCC);
let gpiob: PortB = PortB::split(dp.GPIOB, &mut dp.RCC);
let uart: LpUart<NoRx, (pins::B11, Dma2Ch7)> = cortex_m::interrupt::free(|cs| {
    LpUart::new(dp.LPUART, 115_200, uart::Clk::Hsi16, &mut dp.RCC)
        .enable_tx_dma(gpiob.b11, dma.d2.c7, cs)
});
source§

impl<RX, TX> Uart2<RX, TX>

source

pub fn disable_tx(self) -> (Uart2<RX, NoTx>, TX)

Disable the UART transmitter.

Example
use stm32wlxx_hal::{
    cortex_m,
    gpio::{pins, PortB},
    pac,
    uart::{self, LpUart, NoRx, NoTx},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

// enable the HSI16 source clock
dp.RCC.cr.modify(|_, w| w.hsion().set_bit());
while dp.RCC.cr.read().hsirdy().is_not_ready() {}

let gpiob: PortB = PortB::split(dp.GPIOB, &mut dp.RCC);
let uart: LpUart<NoRx, pins::B11> = cortex_m::interrupt::free(|cs| {
    LpUart::new(dp.LPUART, 115_200, uart::Clk::Hsi16, &mut dp.RCC)
        .enable_tx(gpiob.b11, cs)
});

let (uart, b11): (LpUart<NoRx, NoTx>, pins::B11) = uart.disable_tx();
source§

impl<TX> Uart2<NoRx, TX>

source

pub fn enable_rx<RX: Uart2Rx>( self, rx: RX, cs: &CriticalSection ) -> Uart2<RX, TX>

Enable the UART receiver.

Example
use stm32wlxx_hal::{
    cortex_m,
    gpio::{pins, PortB},
    pac,
    uart::{self, LpUart, NoTx},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

// enable the HSI16 source clock
dp.RCC.cr.modify(|_, w| w.hsion().set_bit());
while dp.RCC.cr.read().hsirdy().is_not_ready() {}

let gpiob: PortB = PortB::split(dp.GPIOB, &mut dp.RCC);
let uart: LpUart<pins::B10, NoTx> = cortex_m::interrupt::free(|cs| {
    LpUart::new(dp.LPUART, 115_200, uart::Clk::Hsi16, &mut dp.RCC)
        .enable_rx(gpiob.b10, cs)
});
source§

impl<TX> Uart2<NoRx, TX>

source

pub fn enable_rx_dma<RxPin: Uart2Rx, RxDma: DmaCh>( self, rx: RxPin, rx_dma: RxDma, cs: &CriticalSection ) -> Uart2<(RxPin, RxDma), TX>

Enable the UART receiver with a DMA channel.

Example
use stm32wlxx_hal::{
    dma::{AllDma, Dma2Ch2},
    gpio::{pins, PortB},
    pac,
    uart::{self, LpUart, NoTx},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

// enable the HSI16 source clock
dp.RCC.cr.modify(|_, w| w.hsion().set_bit());
while dp.RCC.cr.read().hsirdy().is_not_ready() {}

let dma: AllDma = AllDma::split(dp.DMAMUX, dp.DMA1, dp.DMA2, &mut dp.RCC);
let gpiob: PortB = PortB::split(dp.GPIOB, &mut dp.RCC);
let uart: LpUart<(pins::B10, Dma2Ch2), NoTx> = cortex_m::interrupt::free(|cs| {
    LpUart::new(dp.LPUART, 115_200, uart::Clk::Hsi16, &mut dp.RCC)
        .enable_rx_dma(gpiob.b10, dma.d2.c2, cs)
});
source§

impl<RX, TX> Uart2<RX, TX>

source

pub fn disable_rx(self) -> (Uart2<NoRx, TX>, RX)

Disable the UART receiver.

Example
use stm32wlxx_hal::{
    gpio::{pins, PortB},
    pac,
    uart::{self, LpUart, NoRx, NoTx},
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

// enable the HSI16 source clock
dp.RCC.cr.modify(|_, w| w.hsion().set_bit());
while dp.RCC.cr.read().hsirdy().is_not_ready() {}

let gpiob: PortB = PortB::split(dp.GPIOB, &mut dp.RCC);
let uart: LpUart<pins::B10, NoTx> = cortex_m::interrupt::free(|cs| {
    LpUart::new(dp.LPUART, 115_200, uart::Clk::Hsi16, &mut dp.RCC)
        .enable_rx(gpiob.b10, cs)
});

let (uart, b10): (LpUart<NoRx, NoTx>, pins::B10) = uart.disable_rx();
source§

impl<RxPin, RxDma, TX> Uart2<(RxPin, RxDma), TX>where RxPin: Uart2Rx, RxDma: DmaCh,

source

pub fn bread_all(&mut self, buffer: &mut [u8]) -> Result<(), Error>

This is not an embedded-hal trait, it is added simply for parity with what exists on the TX side.

Trait Implementations§

source§

impl<RX: Debug, TX: Debug> Debug for Uart2<RX, TX>

source§

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

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

impl<RX, TX> Write for Uart2<RX, TX>where Uart2<RX, TX>: 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<RX, TxPin, TxDma> Write<u8> for Uart2<RX, (TxPin, TxDma)>where TxPin: Uart2Tx, TxDma: DmaCh,

§

type Error = Error

The type of error that can occur when writing
source§

fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error>

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

fn bflush(&mut self) -> Result<(), Self::Error>

Block until the serial interface has sent all buffered words
source§

impl<RX, TX> Read<u8> for Uart2<RX, TX>where RX: Uart2Rx,

§

type Error = Error

Read error
source§

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

Reads a single word from the serial interface
source§

impl<RX, TX> Write<u8> for Uart2<RX, TX>where TX: Uart2Tx,

§

type Error = Error

Write error
source§

fn write(&mut self, word: u8) -> Result<(), Self::Error>

Writes a single word to the serial interface
source§

fn flush(&mut self) -> Result<(), Self::Error>

Ensures that none of the previously written words are still buffered

Auto Trait Implementations§

§

impl<RX, TX> RefUnwindSafe for Uart2<RX, TX>where RX: RefUnwindSafe, TX: RefUnwindSafe,

§

impl<RX, TX> Send for Uart2<RX, TX>where RX: Send, TX: Send,

§

impl<RX, TX> !Sync for Uart2<RX, TX>

§

impl<RX, TX> Unpin for Uart2<RX, TX>where RX: Unpin, TX: Unpin,

§

impl<RX, TX> UnwindSafe for Uart2<RX, TX>where RX: UnwindSafe, TX: 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.