Skip to content

Commit da1edcd

Browse files
committed
Serial: Implement UART peripherals based on device category
This requires setting an io-* Cargo feature in order to access the `serial` module.
1 parent d408ae7 commit da1edcd

File tree

5 files changed

+88
-21
lines changed

5 files changed

+88
-21
lines changed

Cargo.toml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ opt-level = "s"
264264

265265
[[example]]
266266
name = "adc_cont"
267-
required-features = ["stm32l0x2"]
267+
required-features = ["stm32l0x2", "io-STM32L071"]
268268

269269
[[example]]
270270
name = "adc_multi"
271-
required-features = ["stm32l0x2"]
271+
required-features = ["stm32l0x2", "io-STM32L071"]
272272

273273
[[example]]
274274
name = "adc_trig"
275-
required-features = ["stm32l0x2"]
275+
required-features = ["stm32l0x2", "io-STM32L071"]
276276

277277
[[example]]
278278
name = "aes_ecb"
@@ -312,15 +312,19 @@ required-features = ["rt","stm32l0x2"]
312312

313313
[[example]]
314314
name = "rtc"
315-
required-features = ["stm32l0x2"]
315+
required-features = ["stm32l0x2", "io-STM32L071"]
316+
317+
[[example]]
318+
name = "serial"
319+
required-features = ["stm32l0x2", "io-STM32L071"]
316320

317321
[[example]]
318322
name = "serial_dma"
319-
required-features = ["stm32l0x2"]
323+
required-features = ["stm32l0x2", "io-STM32L071"]
320324

321325
[[example]]
322326
name = "serial_dma_async"
323-
required-features = ["stm32l0x2"]
327+
required-features = ["stm32l0x2", "io-STM32L071"]
324328

325329
[[example]]
326330
name = "timer"

src/dma.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,29 @@ use crate::{
3535
I2C1,
3636
I2C2,
3737
I2C3,
38-
USART1,
39-
USART2,
4038
},
4139
rcc::Rcc,
42-
serial,
4340
};
4441

42+
#[cfg(any(feature = "io-STM32L051", feature = "io-STM32L071"))]
43+
use crate::pac::USART1;
44+
45+
#[cfg(any(
46+
feature = "io-STM32L021",
47+
feature = "io-STM32L031",
48+
feature = "io-STM32L051",
49+
feature = "io-STM32L071",
50+
))]
51+
use crate::pac::USART2;
52+
53+
#[cfg(any(
54+
feature = "io-STM32L021",
55+
feature = "io-STM32L031",
56+
feature = "io-STM32L051",
57+
feature = "io-STM32L071",
58+
))]
59+
use crate::serial;
60+
4561
#[cfg(feature = "stm32l082")]
4662
use crate::aes;
4763

@@ -521,13 +537,24 @@ impl_target!(
521537
// ADC
522538
adc::DmaToken, Channel1, 0;
523539
adc::DmaToken, Channel2, 0;
540+
);
524541

542+
#[cfg(any(feature = "io-STM32L051", feature = "io-STM32L071"))]
543+
impl_target!(
525544
// USART1
526545
serial::Tx<USART1>, Channel2, 3;
527546
serial::Tx<USART1>, Channel4, 3;
528547
serial::Rx<USART1>, Channel3, 3;
529548
serial::Rx<USART1>, Channel5, 3;
549+
);
530550

551+
#[cfg(any(
552+
feature = "io-STM32L021",
553+
feature = "io-STM32L031",
554+
feature = "io-STM32L051",
555+
feature = "io-STM32L071",
556+
))]
557+
impl_target!(
531558
// USART2
532559
serial::Tx<USART2>, Channel4, 4;
533560
serial::Tx<USART2>, Channel7, 4;

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ pub mod rcc;
3232
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
3333
pub mod rng;
3434
pub mod rtc;
35+
#[cfg(any(
36+
feature = "io-STM32L021",
37+
feature = "io-STM32L031",
38+
feature = "io-STM32L051",
39+
feature = "io-STM32L071",
40+
))]
3541
pub mod serial;
3642
pub mod spi;
3743
pub mod syscfg;

src/prelude.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ pub use crate::{
1616
i2c::I2cExt as _,
1717
pwr::PowerMode as _,
1818
rcc::RccExt as _,
19-
serial::{
20-
Serial1Ext as _,
21-
Serial2Ext as _,
22-
},
2319
spi::SpiExt as _,
2420
time::U32Ext as _,
2521
timer::TimerExt as _,
@@ -28,3 +24,26 @@ pub use crate::{
2824
WindowWatchdogExt as _,
2925
},
3026
};
27+
28+
#[cfg(any(
29+
feature = "io-STM32L021",
30+
feature = "io-STM32L031",
31+
feature = "io-STM32L051",
32+
feature = "io-STM32L071",
33+
))]
34+
pub use crate::serial::{
35+
Serial1LpExt as _,
36+
Serial2Ext as _,
37+
};
38+
#[cfg(any(
39+
feature = "io-STM32L051",
40+
feature = "io-STM32L071",
41+
))]
42+
pub use crate::serial::Serial1LpExt as _;
43+
#[cfg(any(
44+
feature = "io-STM32L071",
45+
))]
46+
pub use crate::serial::{
47+
Serial4Ext as _,
48+
Serial5Ext as _,
49+
};

src/serial.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use core::fmt;
22
use core::marker::PhantomData;
33
use core::ptr;
44

5+
use nb::block;
6+
57
use crate::gpio::{gpioa::*, gpiob::*, gpioc::*, gpiod::*, gpioe::*};
68
use crate::gpio::{PinMode, AltMode};
79
use crate::hal;
810
use crate::hal::prelude::*;
911
pub use crate::pac::{LPUART1, USART1, USART2, USART4, USART5};
1012
use crate::rcc::Rcc;
11-
use nb::block;
1213

1314
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
1415
use core::{
@@ -23,7 +24,7 @@ use as_slice::{AsMutSlice, AsSlice};
2324
pub use crate::dma;
2425

2526
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
26-
use dma::Buffer;
27+
use crate::dma::Buffer;
2728

2829
/// Serial error
2930
#[derive(Debug)]
@@ -581,7 +582,7 @@ macro_rules! usart {
581582
}
582583
}
583584

584-
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
585+
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
585586
impl Tx<$USARTX> {
586587
pub fn write_all<Buffer, Channel>(self,
587588
dma: &mut dma::Handle,
@@ -638,17 +639,27 @@ macro_rules! usart {
638639
}
639640
}
640641

641-
#[cfg(feature = "stm32l0x1")]
642+
// LPUART1 and USART2 are available on category 1/2/3/5 MCUs
643+
#[cfg(any(
644+
feature = "io-STM32L021",
645+
feature = "io-STM32L031",
646+
feature = "io-STM32L051",
647+
feature = "io-STM32L071",
648+
))]
642649
usart! {
643-
LPUART1: (lpuart1, apb1enr, lpuart1en, apb1_clk, Serial1Ext),
650+
LPUART1: (lpuart1, apb1enr, lpuart1en, apb1_clk, Serial1LpExt),
644651
USART2: (usart2, apb1enr, usart2en, apb1_clk, Serial2Ext),
645652
}
646653

647-
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
654+
// USART1 is available on category 3/5 MCUs
655+
#[cfg(any(feature = "io-STM32L051", feature = "io-STM32L071"))]
648656
usart! {
649-
LPUART1: (lpuart1, apb1enr, lpuart1en, apb1_clk, Serial1LpExt),
650657
USART1: (usart1, apb2enr, usart1en, apb1_clk, Serial1Ext),
651-
USART2: (usart2, apb1enr, usart2en, apb1_clk, Serial2Ext),
658+
}
659+
660+
// USART4 and USART5 are available on category 5 MCUs
661+
#[cfg(feature = "io-STM32L071")]
662+
usart! {
652663
USART4: (usart4, apb1enr, usart4en, apb1_clk, Serial4Ext),
653664
USART5: (usart5, apb1enr, usart5en, apb1_clk, Serial5Ext),
654665
}

0 commit comments

Comments
 (0)