stm32: Implement set_config for Uart structs

This commit is contained in:
Scott Mabin
2023-09-16 22:32:14 +01:00
parent cd128c20fa
commit 88eb5cca71
4 changed files with 102 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ use core::marker::PhantomData;
use core::sync::atomic::{compiler_fence, Ordering};
use core::task::Poll;
use embassy_embedded_hal::SetConfig;
use embassy_hal_internal::drop::OnDrop;
use embassy_hal_internal::{into_ref, PeripheralRef};
use futures::future::{select, Either};
@@ -168,11 +169,28 @@ pub struct Uart<'d, T: BasicInstance, TxDma = NoDma, RxDma = NoDma> {
rx: UartRx<'d, T, RxDma>,
}
impl<'d, T: BasicInstance, TxDma, RxDma> SetConfig for Uart<'d, T, TxDma, RxDma> {
type Config = Config;
fn set_config(&mut self, config: &Self::Config) {
self.tx.set_config(config);
self.rx.set_config(config);
}
}
pub struct UartTx<'d, T: BasicInstance, TxDma = NoDma> {
phantom: PhantomData<&'d mut T>,
tx_dma: PeripheralRef<'d, TxDma>,
}
impl<'d, T: BasicInstance, TxDma> SetConfig for UartTx<'d, T, TxDma> {
type Config = Config;
fn set_config(&mut self, config: &Self::Config) {
self.set_config(config);
}
}
pub struct UartRx<'d, T: BasicInstance, RxDma = NoDma> {
_peri: PeripheralRef<'d, T>,
rx_dma: PeripheralRef<'d, RxDma>,
@@ -181,6 +199,14 @@ pub struct UartRx<'d, T: BasicInstance, RxDma = NoDma> {
buffered_sr: stm32_metapac::usart::regs::Sr,
}
impl<'d, T: BasicInstance, RxDma> SetConfig for UartRx<'d, T, RxDma> {
type Config = Config;
fn set_config(&mut self, config: &Self::Config) {
self.set_config(config);
}
}
impl<'d, T: BasicInstance, TxDma> UartTx<'d, T, TxDma> {
/// Useful if you only want Uart Tx. It saves 1 pin and consumes a little less power.
pub fn new(
@@ -237,6 +263,10 @@ impl<'d, T: BasicInstance, TxDma> UartTx<'d, T, TxDma> {
}
}
pub fn set_config(&mut self, config: &Config) {
reconfigure::<T>(config)
}
pub async fn write(&mut self, buffer: &[u8]) -> Result<(), Error>
where
TxDma: crate::usart::TxDma<T>,
@@ -334,6 +364,10 @@ impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> {
}
}
pub fn set_config(&mut self, config: &Config) {
reconfigure::<T>(config)
}
#[cfg(any(usart_v1, usart_v2))]
fn check_rx_flags(&mut self) -> Result<bool, Error> {
let r = T::regs();
@@ -759,6 +793,10 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
}
}
pub fn set_config(&mut self, config: &Config) {
reconfigure::<T>(config)
}
pub async fn write(&mut self, buffer: &[u8]) -> Result<(), Error>
where
TxDma: crate::usart::TxDma<T>,
@@ -804,6 +842,17 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
}
}
fn reconfigure<T: BasicInstance>(config: &Config) {
T::Interrupt::disable();
let r = T::regs();
let cr = r.cr1().read();
configure(r, config, T::frequency(), T::KIND, cr.re(), cr.te());
T::Interrupt::unpend();
unsafe { T::Interrupt::enable() };
}
fn configure(r: Regs, config: &Config, pclk_freq: Hertz, kind: Kind, enable_rx: bool, enable_tx: bool) {
if !enable_rx && !enable_tx {
panic!("USART: At least one of RX or TX should be enabled");