aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-09-06 17:33:56 -0500
committerxoviat <[email protected]>2023-09-06 17:33:56 -0500
commitd097c99719f31aa68afdedde09149d6de1d0b600 (patch)
tree90e0da2a335b3d939fa7f3fd3830756b85fa3022
parenta05afc54262e180fbbbe5b9369246c5275a5c4a3 (diff)
stm32/rcc: add lsi and lse bd abstraction
-rw-r--r--embassy-stm32/src/rcc/bd.rs76
1 files changed, 74 insertions, 2 deletions
diff --git a/embassy-stm32/src/rcc/bd.rs b/embassy-stm32/src/rcc/bd.rs
index 4d8ed82aa..b4d21c35f 100644
--- a/embassy-stm32/src/rcc/bd.rs
+++ b/embassy-stm32/src/rcc/bd.rs
@@ -1,6 +1,34 @@
1#[allow(dead_code)]
2#[derive(Default)]
3pub enum LseDrive {
4 #[cfg(any(rtc_v2f7, rtc_v2l4))]
5 Low = 0,
6 MediumLow = 0x01,
7 #[default]
8 MediumHigh = 0x02,
9 #[cfg(any(rtc_v2f7, rtc_v2l4))]
10 High = 0x03,
11}
12
13#[cfg(any(rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l4))]
14impl From<LseDrive> for crate::pac::rcc::vals::Lsedrv {
15 fn from(value: LseDrive) -> Self {
16 use crate::pac::rcc::vals::Lsedrv;
17
18 match value {
19 #[cfg(any(rtc_v2f7, rtc_v2l4))]
20 LseDrive::Low => Lsedrv::LOW,
21 LseDrive::MediumLow => Lsedrv::MEDIUMLOW,
22 LseDrive::MediumHigh => Lsedrv::MEDIUMHIGH,
23 #[cfg(any(rtc_v2f7, rtc_v2l4))]
24 LseDrive::High => Lsedrv::HIGH,
25 }
26 }
27}
28
29#[allow(dead_code)]
1#[derive(Copy, Clone, Debug, PartialEq)] 30#[derive(Copy, Clone, Debug, PartialEq)]
2#[repr(u8)] 31#[repr(u8)]
3#[allow(dead_code)]
4pub enum RtcClockSource { 32pub enum RtcClockSource {
5 /// 00: No clock 33 /// 00: No clock
6 NoClock = 0b00, 34 NoClock = 0b00,
@@ -66,6 +94,38 @@ impl BackupDomain {
66 r 94 r
67 } 95 }
68 96
97 #[allow(dead_code, unused_variables)]
98 #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb))]
99 pub fn enable_lse(lse_drive: LseDrive) {
100 Self::modify(|w| {
101 #[cfg(any(rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l4))]
102 w.set_lsedrv(lse_drive.into());
103 w.set_lseon(true);
104 });
105
106 while !Self::read().lserdy() {}
107 }
108
109 #[allow(dead_code)]
110 #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb))]
111 pub fn enable_lsi() {
112 let csr = crate::pac::RCC.csr();
113
114 Self::modify(|_| {
115 #[cfg(not(rtc_v2wb))]
116 csr.modify(|w| w.set_lsion(true));
117
118 #[cfg(rtc_v2wb)]
119 csr.modify(|w| w.set_lsi1on(true));
120 });
121
122 #[cfg(not(rtc_v2wb))]
123 while !csr.read().lsirdy() {}
124
125 #[cfg(rtc_v2wb)]
126 while !csr.read().lsi1rdy() {}
127 }
128
69 #[cfg(any( 129 #[cfg(any(
70 rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3, 130 rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3,
71 rtc_v3u5 131 rtc_v3u5
@@ -74,7 +134,7 @@ impl BackupDomain {
74 pub fn set_rtc_clock_source(clock_source: RtcClockSource) { 134 pub fn set_rtc_clock_source(clock_source: RtcClockSource) {
75 let clock_source = clock_source as u8; 135 let clock_source = clock_source as u8;
76 #[cfg(any( 136 #[cfg(any(
77 all(not(any(rtc_v3, rtc_v3u5)), not(rtc_v2wb)), 137 not(any(rtc_v3, rtc_v3u5, rtc_v2wb)),
78 all(any(rtc_v3, rtc_v3u5), not(any(rcc_wl5, rcc_wle))) 138 all(any(rtc_v3, rtc_v3u5), not(any(rcc_wl5, rcc_wle)))
79 ))] 139 ))]
80 let clock_source = crate::pac::rcc::vals::Rtcsel::from_bits(clock_source); 140 let clock_source = crate::pac::rcc::vals::Rtcsel::from_bits(clock_source);
@@ -86,6 +146,18 @@ impl BackupDomain {
86 }); 146 });
87 } 147 }
88 148
149 #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb))]
150 #[allow(dead_code, unused_variables)]
151 pub fn configure_rtc(clock_source: RtcClockSource, lse_drive: Option<LseDrive>) {
152 match clock_source {
153 RtcClockSource::LSI => Self::enable_lsi(),
154 RtcClockSource::LSE => Self::enable_lse(lse_drive.unwrap_or_default()),
155 _ => {}
156 };
157
158 Self::set_rtc_clock_source(clock_source);
159 }
160
89 #[cfg(any( 161 #[cfg(any(
90 rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3, 162 rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3,
91 rtc_v3u5 163 rtc_v3u5