diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-09-25 22:21:29 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-25 22:21:29 +0000 |
| commit | e8587e2fba04490f8a17e21489331b6cef583663 (patch) | |
| tree | 8a797018cd769884a165d7d80fea8582d682fa3f /embassy-stm32 | |
| parent | c79a84a98a92bb3f1280b4a7657936fcd827a0e0 (diff) | |
| parent | 5d8817d1095589e1916a92adc9db3feb1a3b91b5 (diff) | |
Merge pull request #1950 from embassy-rs/stm32-test-cleanup
stm32/usart: return error instead of panicking on bad baudrate.
Diffstat (limited to 'embassy-stm32')
| -rw-r--r-- | embassy-stm32/src/usart/buffered.rs | 26 | ||||
| -rw-r--r-- | embassy-stm32/src/usart/mod.rs | 87 | ||||
| -rw-r--r-- | embassy-stm32/src/usart/ringbuffered.rs | 6 |
3 files changed, 70 insertions, 49 deletions
diff --git a/embassy-stm32/src/usart/buffered.rs b/embassy-stm32/src/usart/buffered.rs index 323d83818..e2d6e42af 100644 --- a/embassy-stm32/src/usart/buffered.rs +++ b/embassy-stm32/src/usart/buffered.rs | |||
| @@ -118,7 +118,7 @@ impl<'d, T: BasicInstance> SetConfig for BufferedUart<'d, T> { | |||
| 118 | type Config = Config; | 118 | type Config = Config; |
| 119 | 119 | ||
| 120 | fn set_config(&mut self, config: &Self::Config) { | 120 | fn set_config(&mut self, config: &Self::Config) { |
| 121 | self.set_config(config) | 121 | unwrap!(self.set_config(config)) |
| 122 | } | 122 | } |
| 123 | } | 123 | } |
| 124 | 124 | ||
| @@ -126,7 +126,7 @@ impl<'d, T: BasicInstance> SetConfig for BufferedUartRx<'d, T> { | |||
| 126 | type Config = Config; | 126 | type Config = Config; |
| 127 | 127 | ||
| 128 | fn set_config(&mut self, config: &Self::Config) { | 128 | fn set_config(&mut self, config: &Self::Config) { |
| 129 | self.set_config(config) | 129 | unwrap!(self.set_config(config)) |
| 130 | } | 130 | } |
| 131 | } | 131 | } |
| 132 | 132 | ||
| @@ -134,7 +134,7 @@ impl<'d, T: BasicInstance> SetConfig for BufferedUartTx<'d, T> { | |||
| 134 | type Config = Config; | 134 | type Config = Config; |
| 135 | 135 | ||
| 136 | fn set_config(&mut self, config: &Self::Config) { | 136 | fn set_config(&mut self, config: &Self::Config) { |
| 137 | self.set_config(config) | 137 | unwrap!(self.set_config(config)) |
| 138 | } | 138 | } |
| 139 | } | 139 | } |
| 140 | 140 | ||
| @@ -147,7 +147,7 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 147 | tx_buffer: &'d mut [u8], | 147 | tx_buffer: &'d mut [u8], |
| 148 | rx_buffer: &'d mut [u8], | 148 | rx_buffer: &'d mut [u8], |
| 149 | config: Config, | 149 | config: Config, |
| 150 | ) -> BufferedUart<'d, T> { | 150 | ) -> Result<Self, ConfigError> { |
| 151 | // UartRx and UartTx have one refcount ea. | 151 | // UartRx and UartTx have one refcount ea. |
| 152 | T::enable(); | 152 | T::enable(); |
| 153 | T::enable(); | 153 | T::enable(); |
| @@ -166,7 +166,7 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 166 | tx_buffer: &'d mut [u8], | 166 | tx_buffer: &'d mut [u8], |
| 167 | rx_buffer: &'d mut [u8], | 167 | rx_buffer: &'d mut [u8], |
| 168 | config: Config, | 168 | config: Config, |
| 169 | ) -> BufferedUart<'d, T> { | 169 | ) -> Result<Self, ConfigError> { |
| 170 | into_ref!(cts, rts); | 170 | into_ref!(cts, rts); |
| 171 | 171 | ||
| 172 | // UartRx and UartTx have one refcount ea. | 172 | // UartRx and UartTx have one refcount ea. |
| @@ -194,7 +194,7 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 194 | tx_buffer: &'d mut [u8], | 194 | tx_buffer: &'d mut [u8], |
| 195 | rx_buffer: &'d mut [u8], | 195 | rx_buffer: &'d mut [u8], |
| 196 | config: Config, | 196 | config: Config, |
| 197 | ) -> BufferedUart<'d, T> { | 197 | ) -> Result<Self, ConfigError> { |
| 198 | into_ref!(de); | 198 | into_ref!(de); |
| 199 | 199 | ||
| 200 | // UartRx and UartTx have one refcount ea. | 200 | // UartRx and UartTx have one refcount ea. |
| @@ -217,7 +217,7 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 217 | tx_buffer: &'d mut [u8], | 217 | tx_buffer: &'d mut [u8], |
| 218 | rx_buffer: &'d mut [u8], | 218 | rx_buffer: &'d mut [u8], |
| 219 | config: Config, | 219 | config: Config, |
| 220 | ) -> BufferedUart<'d, T> { | 220 | ) -> Result<Self, ConfigError> { |
| 221 | into_ref!(_peri, rx, tx); | 221 | into_ref!(_peri, rx, tx); |
| 222 | 222 | ||
| 223 | let state = T::buffered_state(); | 223 | let state = T::buffered_state(); |
| @@ -230,7 +230,7 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 230 | rx.set_as_af(rx.af_num(), AFType::Input); | 230 | rx.set_as_af(rx.af_num(), AFType::Input); |
| 231 | tx.set_as_af(tx.af_num(), AFType::OutputPushPull); | 231 | tx.set_as_af(tx.af_num(), AFType::OutputPushPull); |
| 232 | 232 | ||
| 233 | configure(r, &config, T::frequency(), T::KIND, true, true); | 233 | configure(r, &config, T::frequency(), T::KIND, true, true)?; |
| 234 | 234 | ||
| 235 | r.cr1().modify(|w| { | 235 | r.cr1().modify(|w| { |
| 236 | #[cfg(lpuart_v2)] | 236 | #[cfg(lpuart_v2)] |
| @@ -243,17 +243,17 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 243 | T::Interrupt::unpend(); | 243 | T::Interrupt::unpend(); |
| 244 | unsafe { T::Interrupt::enable() }; | 244 | unsafe { T::Interrupt::enable() }; |
| 245 | 245 | ||
| 246 | Self { | 246 | Ok(Self { |
| 247 | rx: BufferedUartRx { phantom: PhantomData }, | 247 | rx: BufferedUartRx { phantom: PhantomData }, |
| 248 | tx: BufferedUartTx { phantom: PhantomData }, | 248 | tx: BufferedUartTx { phantom: PhantomData }, |
| 249 | } | 249 | }) |
| 250 | } | 250 | } |
| 251 | 251 | ||
| 252 | pub fn split(self) -> (BufferedUartTx<'d, T>, BufferedUartRx<'d, T>) { | 252 | pub fn split(self) -> (BufferedUartTx<'d, T>, BufferedUartRx<'d, T>) { |
| 253 | (self.tx, self.rx) | 253 | (self.tx, self.rx) |
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | pub fn set_config(&mut self, config: &Config) { | 256 | pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> { |
| 257 | reconfigure::<T>(config) | 257 | reconfigure::<T>(config) |
| 258 | } | 258 | } |
| 259 | } | 259 | } |
| @@ -333,7 +333,7 @@ impl<'d, T: BasicInstance> BufferedUartRx<'d, T> { | |||
| 333 | } | 333 | } |
| 334 | } | 334 | } |
| 335 | 335 | ||
| 336 | pub fn set_config(&mut self, config: &Config) { | 336 | pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> { |
| 337 | reconfigure::<T>(config) | 337 | reconfigure::<T>(config) |
| 338 | } | 338 | } |
| 339 | } | 339 | } |
| @@ -407,7 +407,7 @@ impl<'d, T: BasicInstance> BufferedUartTx<'d, T> { | |||
| 407 | } | 407 | } |
| 408 | } | 408 | } |
| 409 | 409 | ||
| 410 | pub fn set_config(&mut self, config: &Config) { | 410 | pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> { |
| 411 | reconfigure::<T>(config) | 411 | reconfigure::<T>(config) |
| 412 | } | 412 | } |
| 413 | } | 413 | } |
diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index ff02d0a63..45580fe30 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs | |||
| @@ -13,11 +13,10 @@ use futures::future::{select, Either}; | |||
| 13 | use crate::dma::{NoDma, Transfer}; | 13 | use crate::dma::{NoDma, Transfer}; |
| 14 | use crate::gpio::sealed::AFType; | 14 | use crate::gpio::sealed::AFType; |
| 15 | use crate::interrupt::typelevel::Interrupt; | 15 | use crate::interrupt::typelevel::Interrupt; |
| 16 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 17 | #[allow(unused_imports)] | 16 | #[allow(unused_imports)] |
| 17 | #[cfg(not(any(usart_v1, usart_v2)))] | ||
| 18 | use crate::pac::usart::regs::Isr as Sr; | 18 | use crate::pac::usart::regs::Isr as Sr; |
| 19 | #[cfg(any(usart_v1, usart_v2))] | 19 | #[cfg(any(usart_v1, usart_v2))] |
| 20 | #[allow(unused_imports)] | ||
| 21 | use crate::pac::usart::regs::Sr; | 20 | use crate::pac::usart::regs::Sr; |
| 22 | #[cfg(not(any(usart_v1, usart_v2)))] | 21 | #[cfg(not(any(usart_v1, usart_v2)))] |
| 23 | use crate::pac::usart::Lpuart as Regs; | 22 | use crate::pac::usart::Lpuart as Regs; |
| @@ -76,12 +75,14 @@ impl<T: BasicInstance> interrupt::typelevel::Handler<T::Interrupt> for Interrupt | |||
| 76 | } | 75 | } |
| 77 | 76 | ||
| 78 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] | 77 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 78 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 79 | pub enum DataBits { | 79 | pub enum DataBits { |
| 80 | DataBits8, | 80 | DataBits8, |
| 81 | DataBits9, | 81 | DataBits9, |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] | 84 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 85 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 85 | pub enum Parity { | 86 | pub enum Parity { |
| 86 | ParityNone, | 87 | ParityNone, |
| 87 | ParityEven, | 88 | ParityEven, |
| @@ -89,6 +90,7 @@ pub enum Parity { | |||
| 89 | } | 90 | } |
| 90 | 91 | ||
| 91 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] | 92 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 93 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 92 | pub enum StopBits { | 94 | pub enum StopBits { |
| 93 | #[doc = "1 stop bit"] | 95 | #[doc = "1 stop bit"] |
| 94 | STOP1, | 96 | STOP1, |
| @@ -102,6 +104,14 @@ pub enum StopBits { | |||
| 102 | 104 | ||
| 103 | #[non_exhaustive] | 105 | #[non_exhaustive] |
| 104 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] | 106 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 107 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 108 | pub enum ConfigError { | ||
| 109 | BaudrateTooLow, | ||
| 110 | BaudrateTooHigh, | ||
| 111 | } | ||
| 112 | |||
| 113 | #[non_exhaustive] | ||
| 114 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
| 105 | pub struct Config { | 115 | pub struct Config { |
| 106 | pub baudrate: u32, | 116 | pub baudrate: u32, |
| 107 | pub data_bits: DataBits, | 117 | pub data_bits: DataBits, |
| @@ -173,8 +183,8 @@ impl<'d, T: BasicInstance, TxDma, RxDma> SetConfig for Uart<'d, T, TxDma, RxDma> | |||
| 173 | type Config = Config; | 183 | type Config = Config; |
| 174 | 184 | ||
| 175 | fn set_config(&mut self, config: &Self::Config) { | 185 | fn set_config(&mut self, config: &Self::Config) { |
| 176 | self.tx.set_config(config); | 186 | unwrap!(self.tx.set_config(config)); |
| 177 | self.rx.set_config(config); | 187 | unwrap!(self.rx.set_config(config)); |
| 178 | } | 188 | } |
| 179 | } | 189 | } |
| 180 | 190 | ||
| @@ -187,7 +197,7 @@ impl<'d, T: BasicInstance, TxDma> SetConfig for UartTx<'d, T, TxDma> { | |||
| 187 | type Config = Config; | 197 | type Config = Config; |
| 188 | 198 | ||
| 189 | fn set_config(&mut self, config: &Self::Config) { | 199 | fn set_config(&mut self, config: &Self::Config) { |
| 190 | self.set_config(config); | 200 | unwrap!(self.set_config(config)); |
| 191 | } | 201 | } |
| 192 | } | 202 | } |
| 193 | 203 | ||
| @@ -203,7 +213,7 @@ impl<'d, T: BasicInstance, RxDma> SetConfig for UartRx<'d, T, RxDma> { | |||
| 203 | type Config = Config; | 213 | type Config = Config; |
| 204 | 214 | ||
| 205 | fn set_config(&mut self, config: &Self::Config) { | 215 | fn set_config(&mut self, config: &Self::Config) { |
| 206 | self.set_config(config); | 216 | unwrap!(self.set_config(config)); |
| 207 | } | 217 | } |
| 208 | } | 218 | } |
| 209 | 219 | ||
| @@ -214,7 +224,7 @@ impl<'d, T: BasicInstance, TxDma> UartTx<'d, T, TxDma> { | |||
| 214 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 224 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, |
| 215 | tx_dma: impl Peripheral<P = TxDma> + 'd, | 225 | tx_dma: impl Peripheral<P = TxDma> + 'd, |
| 216 | config: Config, | 226 | config: Config, |
| 217 | ) -> Self { | 227 | ) -> Result<Self, ConfigError> { |
| 218 | T::enable(); | 228 | T::enable(); |
| 219 | T::reset(); | 229 | T::reset(); |
| 220 | 230 | ||
| @@ -227,7 +237,7 @@ impl<'d, T: BasicInstance, TxDma> UartTx<'d, T, TxDma> { | |||
| 227 | cts: impl Peripheral<P = impl CtsPin<T>> + 'd, | 237 | cts: impl Peripheral<P = impl CtsPin<T>> + 'd, |
| 228 | tx_dma: impl Peripheral<P = TxDma> + 'd, | 238 | tx_dma: impl Peripheral<P = TxDma> + 'd, |
| 229 | config: Config, | 239 | config: Config, |
| 230 | ) -> Self { | 240 | ) -> Result<Self, ConfigError> { |
| 231 | into_ref!(cts); | 241 | into_ref!(cts); |
| 232 | 242 | ||
| 233 | T::enable(); | 243 | T::enable(); |
| @@ -245,25 +255,25 @@ impl<'d, T: BasicInstance, TxDma> UartTx<'d, T, TxDma> { | |||
| 245 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 255 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, |
| 246 | tx_dma: impl Peripheral<P = TxDma> + 'd, | 256 | tx_dma: impl Peripheral<P = TxDma> + 'd, |
| 247 | config: Config, | 257 | config: Config, |
| 248 | ) -> Self { | 258 | ) -> Result<Self, ConfigError> { |
| 249 | into_ref!(_peri, tx, tx_dma); | 259 | into_ref!(_peri, tx, tx_dma); |
| 250 | 260 | ||
| 251 | let r = T::regs(); | 261 | let r = T::regs(); |
| 252 | 262 | ||
| 253 | tx.set_as_af(tx.af_num(), AFType::OutputPushPull); | 263 | tx.set_as_af(tx.af_num(), AFType::OutputPushPull); |
| 254 | 264 | ||
| 255 | configure(r, &config, T::frequency(), T::KIND, false, true); | 265 | configure(r, &config, T::frequency(), T::KIND, false, true)?; |
| 256 | 266 | ||
| 257 | // create state once! | 267 | // create state once! |
| 258 | let _s = T::state(); | 268 | let _s = T::state(); |
| 259 | 269 | ||
| 260 | Self { | 270 | Ok(Self { |
| 261 | tx_dma, | 271 | tx_dma, |
| 262 | phantom: PhantomData, | 272 | phantom: PhantomData, |
| 263 | } | 273 | }) |
| 264 | } | 274 | } |
| 265 | 275 | ||
| 266 | pub fn set_config(&mut self, config: &Config) { | 276 | pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> { |
| 267 | reconfigure::<T>(config) | 277 | reconfigure::<T>(config) |
| 268 | } | 278 | } |
| 269 | 279 | ||
| @@ -307,7 +317,7 @@ impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> { | |||
| 307 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 317 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 308 | rx_dma: impl Peripheral<P = RxDma> + 'd, | 318 | rx_dma: impl Peripheral<P = RxDma> + 'd, |
| 309 | config: Config, | 319 | config: Config, |
| 310 | ) -> Self { | 320 | ) -> Result<Self, ConfigError> { |
| 311 | T::enable(); | 321 | T::enable(); |
| 312 | T::reset(); | 322 | T::reset(); |
| 313 | 323 | ||
| @@ -321,7 +331,7 @@ impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> { | |||
| 321 | rts: impl Peripheral<P = impl RtsPin<T>> + 'd, | 331 | rts: impl Peripheral<P = impl RtsPin<T>> + 'd, |
| 322 | rx_dma: impl Peripheral<P = RxDma> + 'd, | 332 | rx_dma: impl Peripheral<P = RxDma> + 'd, |
| 323 | config: Config, | 333 | config: Config, |
| 324 | ) -> Self { | 334 | ) -> Result<Self, ConfigError> { |
| 325 | into_ref!(rts); | 335 | into_ref!(rts); |
| 326 | 336 | ||
| 327 | T::enable(); | 337 | T::enable(); |
| @@ -340,14 +350,14 @@ impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> { | |||
| 340 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 350 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 341 | rx_dma: impl Peripheral<P = RxDma> + 'd, | 351 | rx_dma: impl Peripheral<P = RxDma> + 'd, |
| 342 | config: Config, | 352 | config: Config, |
| 343 | ) -> Self { | 353 | ) -> Result<Self, ConfigError> { |
| 344 | into_ref!(peri, rx, rx_dma); | 354 | into_ref!(peri, rx, rx_dma); |
| 345 | 355 | ||
| 346 | let r = T::regs(); | 356 | let r = T::regs(); |
| 347 | 357 | ||
| 348 | rx.set_as_af(rx.af_num(), AFType::Input); | 358 | rx.set_as_af(rx.af_num(), AFType::Input); |
| 349 | 359 | ||
| 350 | configure(r, &config, T::frequency(), T::KIND, true, false); | 360 | configure(r, &config, T::frequency(), T::KIND, true, false)?; |
| 351 | 361 | ||
| 352 | T::Interrupt::unpend(); | 362 | T::Interrupt::unpend(); |
| 353 | unsafe { T::Interrupt::enable() }; | 363 | unsafe { T::Interrupt::enable() }; |
| @@ -355,16 +365,16 @@ impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> { | |||
| 355 | // create state once! | 365 | // create state once! |
| 356 | let _s = T::state(); | 366 | let _s = T::state(); |
| 357 | 367 | ||
| 358 | Self { | 368 | Ok(Self { |
| 359 | _peri: peri, | 369 | _peri: peri, |
| 360 | rx_dma, | 370 | rx_dma, |
| 361 | detect_previous_overrun: config.detect_previous_overrun, | 371 | detect_previous_overrun: config.detect_previous_overrun, |
| 362 | #[cfg(any(usart_v1, usart_v2))] | 372 | #[cfg(any(usart_v1, usart_v2))] |
| 363 | buffered_sr: stm32_metapac::usart::regs::Sr(0), | 373 | buffered_sr: stm32_metapac::usart::regs::Sr(0), |
| 364 | } | 374 | }) |
| 365 | } | 375 | } |
| 366 | 376 | ||
| 367 | pub fn set_config(&mut self, config: &Config) { | 377 | pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> { |
| 368 | reconfigure::<T>(config) | 378 | reconfigure::<T>(config) |
| 369 | } | 379 | } |
| 370 | 380 | ||
| @@ -680,7 +690,7 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 680 | tx_dma: impl Peripheral<P = TxDma> + 'd, | 690 | tx_dma: impl Peripheral<P = TxDma> + 'd, |
| 681 | rx_dma: impl Peripheral<P = RxDma> + 'd, | 691 | rx_dma: impl Peripheral<P = RxDma> + 'd, |
| 682 | config: Config, | 692 | config: Config, |
| 683 | ) -> Self { | 693 | ) -> Result<Self, ConfigError> { |
| 684 | // UartRx and UartTx have one refcount ea. | 694 | // UartRx and UartTx have one refcount ea. |
| 685 | T::enable(); | 695 | T::enable(); |
| 686 | T::enable(); | 696 | T::enable(); |
| @@ -699,7 +709,7 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 699 | tx_dma: impl Peripheral<P = TxDma> + 'd, | 709 | tx_dma: impl Peripheral<P = TxDma> + 'd, |
| 700 | rx_dma: impl Peripheral<P = RxDma> + 'd, | 710 | rx_dma: impl Peripheral<P = RxDma> + 'd, |
| 701 | config: Config, | 711 | config: Config, |
| 702 | ) -> Self { | 712 | ) -> Result<Self, ConfigError> { |
| 703 | into_ref!(cts, rts); | 713 | into_ref!(cts, rts); |
| 704 | 714 | ||
| 705 | // UartRx and UartTx have one refcount ea. | 715 | // UartRx and UartTx have one refcount ea. |
| @@ -726,7 +736,7 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 726 | tx_dma: impl Peripheral<P = TxDma> + 'd, | 736 | tx_dma: impl Peripheral<P = TxDma> + 'd, |
| 727 | rx_dma: impl Peripheral<P = RxDma> + 'd, | 737 | rx_dma: impl Peripheral<P = RxDma> + 'd, |
| 728 | config: Config, | 738 | config: Config, |
| 729 | ) -> Self { | 739 | ) -> Result<Self, ConfigError> { |
| 730 | into_ref!(de); | 740 | into_ref!(de); |
| 731 | 741 | ||
| 732 | // UartRx and UartTx have one refcount ea. | 742 | // UartRx and UartTx have one refcount ea. |
| @@ -748,7 +758,7 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 748 | tx_dma: impl Peripheral<P = TxDma> + 'd, | 758 | tx_dma: impl Peripheral<P = TxDma> + 'd, |
| 749 | rx_dma: impl Peripheral<P = RxDma> + 'd, | 759 | rx_dma: impl Peripheral<P = RxDma> + 'd, |
| 750 | config: Config, | 760 | config: Config, |
| 751 | ) -> Self { | 761 | ) -> Result<Self, ConfigError> { |
| 752 | into_ref!(peri, rx, tx, tx_dma, rx_dma); | 762 | into_ref!(peri, rx, tx, tx_dma, rx_dma); |
| 753 | 763 | ||
| 754 | let r = T::regs(); | 764 | let r = T::regs(); |
| @@ -770,7 +780,7 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 770 | } | 780 | } |
| 771 | } | 781 | } |
| 772 | 782 | ||
| 773 | configure(r, &config, T::frequency(), T::KIND, true, true); | 783 | configure(r, &config, T::frequency(), T::KIND, true, true)?; |
| 774 | 784 | ||
| 775 | T::Interrupt::unpend(); | 785 | T::Interrupt::unpend(); |
| 776 | unsafe { T::Interrupt::enable() }; | 786 | unsafe { T::Interrupt::enable() }; |
| @@ -778,7 +788,7 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 778 | // create state once! | 788 | // create state once! |
| 779 | let _s = T::state(); | 789 | let _s = T::state(); |
| 780 | 790 | ||
| 781 | Self { | 791 | Ok(Self { |
| 782 | tx: UartTx { | 792 | tx: UartTx { |
| 783 | tx_dma, | 793 | tx_dma, |
| 784 | phantom: PhantomData, | 794 | phantom: PhantomData, |
| @@ -790,10 +800,10 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 790 | #[cfg(any(usart_v1, usart_v2))] | 800 | #[cfg(any(usart_v1, usart_v2))] |
| 791 | buffered_sr: stm32_metapac::usart::regs::Sr(0), | 801 | buffered_sr: stm32_metapac::usart::regs::Sr(0), |
| 792 | }, | 802 | }, |
| 793 | } | 803 | }) |
| 794 | } | 804 | } |
| 795 | 805 | ||
| 796 | pub fn set_config(&mut self, config: &Config) { | 806 | pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> { |
| 797 | reconfigure::<T>(config) | 807 | reconfigure::<T>(config) |
| 798 | } | 808 | } |
| 799 | 809 | ||
| @@ -842,18 +852,27 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { | |||
| 842 | } | 852 | } |
| 843 | } | 853 | } |
| 844 | 854 | ||
| 845 | fn reconfigure<T: BasicInstance>(config: &Config) { | 855 | fn reconfigure<T: BasicInstance>(config: &Config) -> Result<(), ConfigError> { |
| 846 | T::Interrupt::disable(); | 856 | T::Interrupt::disable(); |
| 847 | let r = T::regs(); | 857 | let r = T::regs(); |
| 848 | 858 | ||
| 849 | let cr = r.cr1().read(); | 859 | let cr = r.cr1().read(); |
| 850 | configure(r, config, T::frequency(), T::KIND, cr.re(), cr.te()); | 860 | configure(r, config, T::frequency(), T::KIND, cr.re(), cr.te())?; |
| 851 | 861 | ||
| 852 | T::Interrupt::unpend(); | 862 | T::Interrupt::unpend(); |
| 853 | unsafe { T::Interrupt::enable() }; | 863 | unsafe { T::Interrupt::enable() }; |
| 864 | |||
| 865 | Ok(()) | ||
| 854 | } | 866 | } |
| 855 | 867 | ||
| 856 | fn configure(r: Regs, config: &Config, pclk_freq: Hertz, kind: Kind, enable_rx: bool, enable_tx: bool) { | 868 | fn configure( |
| 869 | r: Regs, | ||
| 870 | config: &Config, | ||
| 871 | pclk_freq: Hertz, | ||
| 872 | kind: Kind, | ||
| 873 | enable_rx: bool, | ||
| 874 | enable_tx: bool, | ||
| 875 | ) -> Result<(), ConfigError> { | ||
| 857 | if !enable_rx && !enable_tx { | 876 | if !enable_rx && !enable_tx { |
| 858 | panic!("USART: At least one of RX or TX should be enabled"); | 877 | panic!("USART: At least one of RX or TX should be enabled"); |
| 859 | } | 878 | } |
| @@ -921,7 +940,7 @@ fn configure(r: Regs, config: &Config, pclk_freq: Hertz, kind: Kind, enable_rx: | |||
| 921 | found_brr = Some(brr); | 940 | found_brr = Some(brr); |
| 922 | break; | 941 | break; |
| 923 | } | 942 | } |
| 924 | panic!("USART: baudrate too high"); | 943 | return Err(ConfigError::BaudrateTooHigh); |
| 925 | } | 944 | } |
| 926 | 945 | ||
| 927 | if brr < brr_max { | 946 | if brr < brr_max { |
| @@ -933,7 +952,7 @@ fn configure(r: Regs, config: &Config, pclk_freq: Hertz, kind: Kind, enable_rx: | |||
| 933 | } | 952 | } |
| 934 | } | 953 | } |
| 935 | 954 | ||
| 936 | let brr = found_brr.expect("USART: baudrate too low"); | 955 | let brr = found_brr.ok_or(ConfigError::BaudrateTooLow)?; |
| 937 | 956 | ||
| 938 | #[cfg(not(usart_v1))] | 957 | #[cfg(not(usart_v1))] |
| 939 | let oversampling = if over8 { "8 bit" } else { "16 bit" }; | 958 | let oversampling = if over8 { "8 bit" } else { "16 bit" }; |
| @@ -985,6 +1004,8 @@ fn configure(r: Regs, config: &Config, pclk_freq: Hertz, kind: Kind, enable_rx: | |||
| 985 | r.cr3().modify(|w| { | 1004 | r.cr3().modify(|w| { |
| 986 | w.set_onebit(config.assume_noise_free); | 1005 | w.set_onebit(config.assume_noise_free); |
| 987 | }); | 1006 | }); |
| 1007 | |||
| 1008 | Ok(()) | ||
| 988 | } | 1009 | } |
| 989 | 1010 | ||
| 990 | mod eh02 { | 1011 | mod eh02 { |
diff --git a/embassy-stm32/src/usart/ringbuffered.rs b/embassy-stm32/src/usart/ringbuffered.rs index eed5ef5d6..347aae7c9 100644 --- a/embassy-stm32/src/usart/ringbuffered.rs +++ b/embassy-stm32/src/usart/ringbuffered.rs | |||
| @@ -7,7 +7,7 @@ use embassy_embedded_hal::SetConfig; | |||
| 7 | use embassy_hal_internal::PeripheralRef; | 7 | use embassy_hal_internal::PeripheralRef; |
| 8 | use futures::future::{select, Either}; | 8 | use futures::future::{select, Either}; |
| 9 | 9 | ||
| 10 | use super::{clear_interrupt_flags, rdr, reconfigure, sr, BasicInstance, Config, Error, UartRx}; | 10 | use super::{clear_interrupt_flags, rdr, reconfigure, sr, BasicInstance, Config, ConfigError, Error, UartRx}; |
| 11 | use crate::dma::ReadableRingBuffer; | 11 | use crate::dma::ReadableRingBuffer; |
| 12 | use crate::usart::{Regs, Sr}; | 12 | use crate::usart::{Regs, Sr}; |
| 13 | 13 | ||
| @@ -20,7 +20,7 @@ impl<'d, T: BasicInstance, RxDma: super::RxDma<T>> SetConfig for RingBufferedUar | |||
| 20 | type Config = Config; | 20 | type Config = Config; |
| 21 | 21 | ||
| 22 | fn set_config(&mut self, config: &Self::Config) { | 22 | fn set_config(&mut self, config: &Self::Config) { |
| 23 | self.set_config(config); | 23 | unwrap!(self.set_config(config)); |
| 24 | } | 24 | } |
| 25 | } | 25 | } |
| 26 | 26 | ||
| @@ -63,7 +63,7 @@ impl<'d, T: BasicInstance, RxDma: super::RxDma<T>> RingBufferedUartRx<'d, T, RxD | |||
| 63 | Err(err) | 63 | Err(err) |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | pub fn set_config(&mut self, config: &Config) { | 66 | pub fn set_config(&mut self, config: &Config) -> Result<(), ConfigError> { |
| 67 | self.teardown_uart(); | 67 | self.teardown_uart(); |
| 68 | reconfigure::<T>(config) | 68 | reconfigure::<T>(config) |
| 69 | } | 69 | } |
