Skip to content

Commit

Permalink
Merge pull request #2105 from andresv/fix-stm32-uart-set-config
Browse files Browse the repository at this point in the history
Fix stm32 uart set_config
  • Loading branch information
Dirbaio authored Oct 24, 2023
2 parents 1a51a84 + bda99e5 commit b3879ec
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
48 changes: 33 additions & 15 deletions embassy-stm32/src/usart/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,28 @@ pub struct BufferedUartRx<'d, T: BasicInstance> {

impl<'d, T: BasicInstance> SetConfig for BufferedUart<'d, T> {
type Config = Config;
type ConfigError = ();
type ConfigError = ConfigError;

fn set_config(&mut self, config: &Self::Config) -> Result<(), ()> {
self.set_config(config).map_err(|_| ())
fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.set_config(config)
}
}

impl<'d, T: BasicInstance> SetConfig for BufferedUartRx<'d, T> {
type Config = Config;
type ConfigError = ();
type ConfigError = ConfigError;

fn set_config(&mut self, config: &Self::Config) -> Result<(), ()> {
self.set_config(config).map_err(|_| ())
fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.set_config(config)
}
}

impl<'d, T: BasicInstance> SetConfig for BufferedUartTx<'d, T> {
type Config = Config;
type ConfigError = ();
type ConfigError = ConfigError;

fn set_config(&mut self, config: &Self::Config) -> Result<(), ()> {
self.set_config(config).map_err(|_| ())
fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.set_config(config)
}
}

Expand Down Expand Up @@ -233,9 +233,6 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> {
configure(r, &config, T::frequency(), T::KIND, true, true)?;

r.cr1().modify(|w| {
#[cfg(lpuart_v2)]
w.set_fifoen(true);

w.set_rxneie(true);
w.set_idleie(true);
});
Expand All @@ -254,7 +251,14 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> {
}

pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> {
reconfigure::<T>(config)
reconfigure::<T>(config)?;

T::regs().cr1().modify(|w| {
w.set_rxneie(true);
w.set_idleie(true);
});

Ok(())
}
}

Expand Down Expand Up @@ -334,7 +338,14 @@ impl<'d, T: BasicInstance> BufferedUartRx<'d, T> {
}

pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> {
reconfigure::<T>(config)
reconfigure::<T>(config)?;

T::regs().cr1().modify(|w| {
w.set_rxneie(true);
w.set_idleie(true);
});

Ok(())
}
}

Expand Down Expand Up @@ -408,7 +419,14 @@ impl<'d, T: BasicInstance> BufferedUartTx<'d, T> {
}

pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> {
reconfigure::<T>(config)
reconfigure::<T>(config)?;

T::regs().cr1().modify(|w| {
w.set_rxneie(true);
w.set_idleie(true);
});

Ok(())
}
}

Expand Down
40 changes: 24 additions & 16 deletions embassy-stm32/src/usart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub enum StopBits {
pub enum ConfigError {
BaudrateTooLow,
BaudrateTooHigh,
RxOrTxNotEnabled,
}

#[non_exhaustive]
Expand Down Expand Up @@ -181,11 +182,11 @@ pub struct Uart<'d, T: BasicInstance, TxDma = NoDma, RxDma = NoDma> {

impl<'d, T: BasicInstance, TxDma, RxDma> SetConfig for Uart<'d, T, TxDma, RxDma> {
type Config = Config;
type ConfigError = ();
type ConfigError = ConfigError;

fn set_config(&mut self, config: &Self::Config) -> Result<(), ()> {
self.tx.set_config(config).map_err(|_| ())?;
self.rx.set_config(config).map_err(|_| ())
fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.tx.set_config(config)?;
self.rx.set_config(config)
}
}

Expand All @@ -196,10 +197,10 @@ pub struct UartTx<'d, T: BasicInstance, TxDma = NoDma> {

impl<'d, T: BasicInstance, TxDma> SetConfig for UartTx<'d, T, TxDma> {
type Config = Config;
type ConfigError = ();
type ConfigError = ConfigError;

fn set_config(&mut self, config: &Self::Config) -> Result<(), ()> {
self.set_config(config).map_err(|_| ())
fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.set_config(config)
}
}

Expand All @@ -213,10 +214,10 @@ pub struct UartRx<'d, T: BasicInstance, RxDma = NoDma> {

impl<'d, T: BasicInstance, RxDma> SetConfig for UartRx<'d, T, RxDma> {
type Config = Config;
type ConfigError = ();
type ConfigError = ConfigError;

fn set_config(&mut self, config: &Self::Config) -> Result<(), ()> {
self.set_config(config).map_err(|_| ())
fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.set_config(config)
}
}

Expand Down Expand Up @@ -866,7 +867,7 @@ fn configure(
enable_tx: bool,
) -> Result<(), ConfigError> {
if !enable_rx && !enable_tx {
panic!("USART: At least one of RX or TX should be enabled");
return Err(ConfigError::RxOrTxNotEnabled);
}

#[cfg(not(usart_v4))]
Expand Down Expand Up @@ -909,6 +910,11 @@ fn configure(
brr + rounding
}

// UART must be disabled during configuration.
r.cr1().modify(|w| {
w.set_ue(false);
});

#[cfg(not(usart_v1))]
let mut over8 = false;
let mut found_brr = None;
Expand Down Expand Up @@ -968,6 +974,12 @@ fn configure(
#[cfg(any(usart_v3, usart_v4))]
w.set_swap(config.swap_rx_tx);
});

#[cfg(not(usart_v1))]
r.cr3().modify(|w| {
w.set_onebit(config.assume_noise_free);
});

r.cr1().write(|w| {
// enable uart
w.set_ue(true);
Expand All @@ -976,6 +988,7 @@ fn configure(
// enable receiver
w.set_re(enable_rx);
// configure word size
// if using odd or even parity it must be configured to 9bits
w.set_m0(if config.parity != Parity::ParityNone {
vals::M0::BIT9
} else {
Expand All @@ -994,11 +1007,6 @@ fn configure(
w.set_fifoen(true);
});

#[cfg(not(usart_v1))]
r.cr3().modify(|w| {
w.set_onebit(config.assume_noise_free);
});

Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions embassy-stm32/src/usart/ringbuffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub struct RingBufferedUartRx<'d, T: BasicInstance, RxDma: super::RxDma<T>> {

impl<'d, T: BasicInstance, RxDma: super::RxDma<T>> SetConfig for RingBufferedUartRx<'d, T, RxDma> {
type Config = Config;
type ConfigError = ();
type ConfigError = ConfigError;

fn set_config(&mut self, config: &Self::Config) -> Result<(), ()> {
self.set_config(config).map_err(|_| ())
fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.set_config(config)
}
}

Expand Down

0 comments on commit b3879ec

Please sign in to comment.