Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rtc #71

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft

Rtc #71

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/rtc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![no_main]
#![no_std]

use hal::{
delay::SYSTDelayExt,
rcc::RccExt,
rtc::RtcExt,
stm32::Peripherals,
time::{Date, ExtU32, Month, MonthDay, Time, Year},
};
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;

use utils::logger::info;

#[macro_use]
mod utils;

#[entry]
fn main() -> ! {
utils::logger::init();

info!("start");

let dp = Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

info!("rcc");

let mut rcc = dp.RCC.constrain();

info!("Setup rtc");
let mut delay = cp.SYST.delay(&rcc.clocks);
let mut rtc = dp.RTC.constrain(&mut rcc);

info!("Setup date");
rtc.set_date(&Date::new(Year(2024), Month(8), MonthDay(5)));
rtc.set_time(&Time::new(0.hours(), 59.minutes(), 2.secs(), true));

info!("Enter Loop");

loop {
info!("Timestamp: {:?} {:?}", rtc.get_date(), rtc.get_time());
delay.delay_ms(500);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub mod pwm;
pub mod pwr;
// pub mod qei;
pub mod rcc;
pub mod rtc;
// pub mod rng;
pub mod serial;
pub mod signature;
Expand Down
10 changes: 10 additions & 0 deletions src/rcc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ pub enum PLLSrc {
HSE_BYPASS(Hertz),
}

/// RTC clock input source
#[derive(Clone, Copy)]
pub enum RtcSrc {
LSE,
LSE_BYPASS,
LSI,
HSE,
HSE_BYPASS,
}

/// Divider for the PLL clock input (M)
/// This must be set based on the input clock to keep the PLL input frequency within the limits
/// specified in the datasheet.
Expand Down
46 changes: 40 additions & 6 deletions src/rcc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,6 @@ impl Rcc {
}
}

pub fn unlock_rtc(&mut self) {
self.rb.apb1enr1.modify(|_, w| w.pwren().set_bit());
let pwr = unsafe { &(*PWR::ptr()) };
pwr.cr1.modify(|_, w| w.dbp().set_bit());
}

fn config_pll(&self, pll_cfg: PllConfig) -> PLLClocks {
// Disable PLL
self.rb.cr.modify(|_, w| w.pllon().clear_bit());
Expand Down Expand Up @@ -457,6 +451,46 @@ impl Rcc {
pub fn clear_reset_reason(&mut self) {
self.rb.csr.modify(|_, w| w.rmvf().set_bit());
}

pub(crate) fn unlock_rtc(&self) {
self.rb.apb1enr1.modify(|_, w| w.pwren().set_bit());
let pwr = unsafe { &(*PWR::ptr()) };
pwr.cr1.modify(|_, w| w.dbp().set_bit());
while pwr.cr1.read().dbp().bit_is_clear() {}
}

pub(crate) fn enable_rtc(&self, src: RtcSrc) {
self.unlock_rtc();
self.rb
.apb1enr1
.modify(|_, w| w.rtcapben().set_bit().pwren().set_bit());
self.rb.apb1smenr1.modify(|_, w| w.rtcapbsmen().set_bit());
self.rb.bdcr.modify(|_, w| w.bdrst().set_bit());

let rtc_sel = match src {
RtcSrc::LSE | RtcSrc::LSE_BYPASS => 0b01,
RtcSrc::LSI => 0b10,
RtcSrc::HSE | RtcSrc::HSE_BYPASS => 0b11,
};

self.rb.bdcr.modify(|_, w| {
w.rtcsel()
.bits(rtc_sel)
.rtcen()
.set_bit()
.bdrst()
.clear_bit()
});

self.unlock_rtc();
match src {
RtcSrc::LSE => self.enable_lse(false),
RtcSrc::LSE_BYPASS => self.enable_lse(true),
RtcSrc::LSI => self.enable_lsi(),
RtcSrc::HSE => self.enable_hse(false),
RtcSrc::HSE_BYPASS => self.enable_hse(true),
};
}
}

pub struct ResetReason {
Expand Down
Loading
Loading