Skip to content

Commit fc95b7e

Browse files
committed
Make sure LPUART1 works on stm32l431/3
Also implements LSE as clock source
1 parent 920778a commit fc95b7e

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

src/rcc.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub enum LpUart1ClockSource {
164164
Pclk = 0b00,
165165
Sysclk = 0b01,
166166
Hsi16 = 0b10,
167-
Lsi = 0b11
167+
Lse = 0b11,
168168
}
169169

170170
/// Peripherals independent clock configuration register
@@ -626,15 +626,21 @@ impl CFGR {
626626
// Check if HSI should be started
627627
let lpuart1_clk_from_hsi = match self.lpuart1_src {
628628
LpUart1ClockSource::Hsi16 => {
629-
rcc.ccipr.modify(|_, w| unsafe { w.lpuart1sel().bits(self.lpuart1_src as u8) });
629+
rcc.ccipr
630+
.modify(|_, w| unsafe { w.lpuart1sel().bits(self.lpuart1_src as u8) });
630631
true
631632
}
632-
LpUart1ClockSource::Lsi => {
633-
todo!()
633+
LpUart1ClockSource::Lse => {
634+
rcc.ccipr
635+
.modify(|_, w| unsafe { w.lpuart1sel().bits(self.lpuart1_src as u8) });
636+
false
634637
}
635638
_ => false,
636639
};
637-
if pll_source == PllSource::HSI16 || (self.msi.is_none() && self.hse.is_none()) || lpuart1_clk_from_hsi {
640+
if pll_source == PllSource::HSI16
641+
|| (self.msi.is_none() && self.hse.is_none())
642+
|| lpuart1_clk_from_hsi
643+
{
638644
rcc.cr.modify(|_, w| w.hsion().set_bit());
639645
while rcc.cr.read().hsirdy().bit_is_clear() {}
640646
}
@@ -845,7 +851,7 @@ impl CFGR {
845851
timclk1: timclk1.Hz(),
846852
timclk2: timclk2.Hz(),
847853
pll_source: pllconf.map(|_| pll_source),
848-
lpuart1_src: self.lpuart1_src
854+
lpuart1_src: self.lpuart1_src,
849855
}
850856
}
851857
}

src/serial.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ use crate::time::{Bps, U32Ext};
4141
// feature = "stm32l4s7",
4242
feature = "stm32l4r9",
4343
feature = "stm32l4s9",
44+
feature = "stm32l433",
45+
feature = "stm32l431",
4446
))]
4547
use crate::dma::dma2;
4648

@@ -221,10 +223,16 @@ pub struct Tx<USART> {
221223
}
222224

223225
macro_rules! potentially_uncompilable {
224-
(true, $expr:expr) => { $expr };
226+
(true, $expr:expr) => {
227+
$expr
228+
};
225229
(false, $expr:expr) => {};
226-
(true, $expr_true:expr, $expr_false:expr) => { $expr_true };
227-
(false, $expr_true:expr, $expr_false:expr) => { $expr_false };
230+
(true, $expr_true:expr, $expr_false:expr) => {
231+
$expr_true
232+
};
233+
(false, $expr_true:expr, $expr_false:expr) => {
234+
$expr_false
235+
};
228236
}
229237

230238
macro_rules! hal {
@@ -281,7 +289,7 @@ macro_rules! hal {
281289
match config.oversampling {
282290
Oversampling::Over8 => {
283291
let uartdiv = 2 * clocks.$pclkX().raw() / config.baudrate.0;
284-
assert!(uartdiv >= 16, "impossible baud rate");
292+
assert!(uartdiv >= 16, "impossible baud rate 1");
285293

286294
let lower = (uartdiv & 0xf) >> 1;
287295
let brr = (uartdiv & !0xf) | lower;
@@ -291,7 +299,7 @@ macro_rules! hal {
291299
}
292300
Oversampling::Over16 => {
293301
let brr = clocks.$pclkX().raw() / config.baudrate.0;
294-
assert!(brr >= 16, "impossible baud rate");
302+
assert!(brr >= 16, "impossible baud rate 2");
295303

296304
usart.brr.write(|w| unsafe { w.bits(brr) });
297305
}
@@ -303,9 +311,9 @@ macro_rules! hal {
303311
LpUart1ClockSource::Pclk => clocks.$pclkX().raw(),
304312
LpUart1ClockSource::Sysclk => clocks.sysclk().raw(),
305313
LpUart1ClockSource::Hsi16 => 16_000_000,
306-
LpUart1ClockSource::Lsi => 32_768,
314+
LpUart1ClockSource::Lse => 32_768,
307315
};
308-
assert!((fck >= 3 * config.baudrate.0) && (fck <= 4096 * config.baudrate.0), "impossible baud rate");
316+
assert!((fck >= 3 * config.baudrate.0) && (fck <= 4096 * config.baudrate.0), "impossible baud rate 3");
309317
let brr = 256u64 * (fck as u64) / config.baudrate.0 as u64;
310318
let brr = brr as u32;
311319
usart.brr.write(|w| unsafe { w.bits(brr) });
@@ -935,6 +943,11 @@ hal! {
935943
LPUART1: (lpuart1, pclk1, tx: (TxDmaL1, dma2::C6, DmaInput::LpUart1Tx), rx: (RxDmaL1, dma2::C7, DmaInput::LpUart1Rx), false,),
936944
}
937945

946+
#[cfg(any(feature = "stm32l431", feature = "stm32l433",))]
947+
hal! {
948+
LPUART1: (lpuart1, pclk1, tx: (TxDmaL1, dma2::C6, DmaInput::LpUart1Tx), rx: (RxDmaL1, dma2::C7, DmaInput::LpUart1Rx), false,),
949+
}
950+
938951
impl<USART, PINS> fmt::Write for Serial<USART, PINS>
939952
where
940953
Serial<USART, PINS>: crate::hal::serial::Write<u8>,
@@ -1173,6 +1186,8 @@ impl_pin_traits! {
11731186
// feature = "stm32l4s7",
11741187
// feature = "stm32l4r9",
11751188
// feature = "stm32l4s9",
1189+
feature = "stm32l433",
1190+
feature = "stm32l431",
11761191
))]
11771192
impl_pin_traits! {
11781193
LPUART1: {

0 commit comments

Comments
 (0)