Skip to content

Commit ed2718d

Browse files
committed
Add slightly modified src/rtc.rs from stm32g0xx-hal
1 parent b153cc3 commit ed2718d

File tree

4 files changed

+449
-1
lines changed

4 files changed

+449
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub mod prelude;
7676
pub mod pwm;
7777
// pub mod qei;
7878
pub mod rcc;
79+
pub mod rtc;
7980
// pub mod rng;
8081
pub mod serial;
8182
pub mod signature;

src/rcc/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ pub enum PLLSrc {
4646
HSE_BYPASS(Hertz),
4747
}
4848

49+
/// RTC clock input source
50+
#[derive(Clone, Copy)]
51+
pub enum RTCSrc {
52+
LSE,
53+
LSE_BYPASS,
54+
LSI,
55+
HSE,
56+
HSE_BYPASS,
57+
}
58+
4959
/// Divider for the PLL clock input (M)
5060
/// This must be set based on the input clock to keep the PLL input frequency within the limits
5161
/// specified in the datasheet.

src/rcc/mod.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl Rcc {
179179
}
180180
}
181181

182-
pub fn unlock_rtc(&mut self) {
182+
pub fn unlock_rtc_old(&mut self) {
183183
self.rb.apb1enr1.modify(|_, w| w.pwren().set_bit());
184184
let pwr = unsafe { &(*PWR::ptr()) };
185185
pwr.cr1.modify(|_, w| w.dbp().set_bit());
@@ -290,6 +290,46 @@ impl Rcc {
290290
self.rb.csr.modify(|_, w| w.lsion().set_bit());
291291
while self.rb.csr.read().lsirdy().bit_is_clear() {}
292292
}
293+
294+
pub(crate) fn unlock_rtc(&self) {
295+
self.rb.apb1enr1.modify(|_, w| w.pwren().set_bit());
296+
let pwr = unsafe { &(*PWR::ptr()) };
297+
pwr.cr1.modify(|_, w| w.dbp().set_bit());
298+
while pwr.cr1.read().dbp().bit_is_clear() {}
299+
}
300+
301+
pub(crate) fn enable_rtc(&self, src: RTCSrc) {
302+
self.unlock_rtc();
303+
self.rb
304+
.apb1enr1
305+
.modify(|_, w| w.rtcapben().set_bit().pwren().set_bit());
306+
self.rb.apb1smenr1.modify(|_, w| w.rtcapbsmen().set_bit());
307+
self.rb.bdcr.modify(|_, w| w.bdrst().set_bit());
308+
309+
let rtc_sel = match src {
310+
RTCSrc::LSE | RTCSrc::LSE_BYPASS => 0b01,
311+
RTCSrc::LSI => 0b10,
312+
RTCSrc::HSE | RTCSrc::HSE_BYPASS => 0b11,
313+
};
314+
315+
self.rb.bdcr.modify(|_, w| {
316+
w.rtcsel()
317+
.bits(rtc_sel)
318+
.rtcen()
319+
.set_bit()
320+
.bdrst()
321+
.clear_bit()
322+
});
323+
324+
self.unlock_rtc();
325+
match src {
326+
RTCSrc::LSE => self.enable_lse(false),
327+
RTCSrc::LSE_BYPASS => self.enable_lse(true),
328+
RTCSrc::LSI => self.enable_lsi(),
329+
RTCSrc::HSE => self.enable_hse(false),
330+
RTCSrc::HSE_BYPASS => self.enable_hse(true),
331+
};
332+
}
293333
}
294334

295335
/// Extension trait that constrains the `RCC` peripheral

0 commit comments

Comments
 (0)