Skip to content

Commit b649c3d

Browse files
committed
Add slightly modified src/rtc.rs from stm32g0xx-hal
1 parent 5e114a6 commit b649c3d

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
@@ -88,6 +88,7 @@ pub mod pwm;
8888
pub mod pwr;
8989
// pub mod qei;
9090
pub mod rcc;
91+
pub mod rtc;
9192
// pub mod rng;
9293
pub mod serial;
9394
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
@@ -231,7 +231,7 @@ impl Rcc {
231231
}
232232
}
233233

234-
pub fn unlock_rtc(&mut self) {
234+
pub fn unlock_rtc_old(&mut self) {
235235
self.rb.apb1enr1.modify(|_, w| w.pwren().set_bit());
236236
let pwr = unsafe { &(*PWR::ptr()) };
237237
pwr.cr1.modify(|_, w| w.dbp().set_bit());
@@ -457,6 +457,46 @@ impl Rcc {
457457
pub fn clear_reset_reason(&mut self) {
458458
self.rb.csr.modify(|_, w| w.rmvf().set_bit());
459459
}
460+
461+
pub(crate) fn unlock_rtc(&self) {
462+
self.rb.apb1enr1.modify(|_, w| w.pwren().set_bit());
463+
let pwr = unsafe { &(*PWR::ptr()) };
464+
pwr.cr1.modify(|_, w| w.dbp().set_bit());
465+
while pwr.cr1.read().dbp().bit_is_clear() {}
466+
}
467+
468+
pub(crate) fn enable_rtc(&self, src: RTCSrc) {
469+
self.unlock_rtc();
470+
self.rb
471+
.apb1enr1
472+
.modify(|_, w| w.rtcapben().set_bit().pwren().set_bit());
473+
self.rb.apb1smenr1.modify(|_, w| w.rtcapbsmen().set_bit());
474+
self.rb.bdcr.modify(|_, w| w.bdrst().set_bit());
475+
476+
let rtc_sel = match src {
477+
RTCSrc::LSE | RTCSrc::LSE_BYPASS => 0b01,
478+
RTCSrc::LSI => 0b10,
479+
RTCSrc::HSE | RTCSrc::HSE_BYPASS => 0b11,
480+
};
481+
482+
self.rb.bdcr.modify(|_, w| {
483+
w.rtcsel()
484+
.bits(rtc_sel)
485+
.rtcen()
486+
.set_bit()
487+
.bdrst()
488+
.clear_bit()
489+
});
490+
491+
self.unlock_rtc();
492+
match src {
493+
RTCSrc::LSE => self.enable_lse(false),
494+
RTCSrc::LSE_BYPASS => self.enable_lse(true),
495+
RTCSrc::LSI => self.enable_lsi(),
496+
RTCSrc::HSE => self.enable_hse(false),
497+
RTCSrc::HSE_BYPASS => self.enable_hse(true),
498+
};
499+
}
460500
}
461501

462502
pub struct ResetReason {

0 commit comments

Comments
 (0)