stm32: fix rtc wakeup timing and add dbg
This commit is contained in:
parent
9f928010a8
commit
e981cd4968
7 changed files with 62 additions and 15 deletions
|
@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
|
||||||
src_base = "https://github.com/embassy-rs/embassy/blob/embassy-stm32-v$VERSION/embassy-stm32/src/"
|
src_base = "https://github.com/embassy-rs/embassy/blob/embassy-stm32-v$VERSION/embassy-stm32/src/"
|
||||||
src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-stm32/src/"
|
src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-stm32/src/"
|
||||||
|
|
||||||
features = ["nightly", "defmt", "unstable-pac", "unstable-traits", "exti", "time-driver-any", "time", "low-power"]
|
features = ["nightly", "defmt", "unstable-pac", "unstable-traits", "exti", "time-driver-any", "time", "low-power", "rtc-debug"]
|
||||||
flavors = [
|
flavors = [
|
||||||
{ regex_feature = "stm32f0.*", target = "thumbv6m-none-eabi" },
|
{ regex_feature = "stm32f0.*", target = "thumbv6m-none-eabi" },
|
||||||
{ regex_feature = "stm32f1.*", target = "thumbv7m-none-eabi" },
|
{ regex_feature = "stm32f1.*", target = "thumbv7m-none-eabi" },
|
||||||
|
@ -90,6 +90,7 @@ defmt = ["dep:defmt", "bxcan/unstable-defmt", "embassy-sync/defmt", "embassy-emb
|
||||||
|
|
||||||
exti = []
|
exti = []
|
||||||
low-power = [ "dep:embassy-executor", "embassy-executor/arch-cortex-m" ]
|
low-power = [ "dep:embassy-executor", "embassy-executor/arch-cortex-m" ]
|
||||||
|
rtc-debug = []
|
||||||
embassy-executor = []
|
embassy-executor = []
|
||||||
|
|
||||||
## Automatically generate `memory.x` file using [`stm32-metapac`](https://docs.rs/stm32-metapac/)
|
## Automatically generate `memory.x` file using [`stm32-metapac`](https://docs.rs/stm32-metapac/)
|
||||||
|
|
|
@ -89,6 +89,9 @@ impl Executor {
|
||||||
|
|
||||||
self.time_driver.resume_time();
|
self.time_driver.resume_time();
|
||||||
trace!("low power: resume time");
|
trace!("low power: resume time");
|
||||||
|
|
||||||
|
#[cfg(feature = "rtc-debug")]
|
||||||
|
cortex_m::asm::bkpt();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(self) fn stop_with_rtc(&mut self, rtc: &'static Rtc) {
|
pub(self) fn stop_with_rtc(&mut self, rtc: &'static Rtc) {
|
||||||
|
@ -118,6 +121,7 @@ impl Executor {
|
||||||
}
|
}
|
||||||
|
|
||||||
trace!("low power: enter stop...");
|
trace!("low power: enter stop...");
|
||||||
|
#[cfg(not(feature = "rtc-debug"))]
|
||||||
self.scb.set_sleepdeep();
|
self.scb.set_sleepdeep();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,6 +144,9 @@ impl Executor {
|
||||||
///
|
///
|
||||||
/// This function never returns.
|
/// This function never returns.
|
||||||
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
|
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
|
||||||
|
#[cfg(feature = "rtc-debug")]
|
||||||
|
trace!("low power: rtc debug enabled");
|
||||||
|
|
||||||
init(unsafe { EXECUTOR.as_mut().unwrap() }.inner.spawner());
|
init(unsafe { EXECUTOR.as_mut().unwrap() }.inner.spawner());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|
|
@ -17,7 +17,7 @@ use crate::{peripherals, Peripheral};
|
||||||
pub const HSI_FREQ: Hertz = Hertz(16_000_000);
|
pub const HSI_FREQ: Hertz = Hertz(16_000_000);
|
||||||
|
|
||||||
/// LSI speed
|
/// LSI speed
|
||||||
pub const LSI_FREQ: Hertz = Hertz(32_000);
|
pub const LSI_FREQ: Hertz = Hertz(32_768);
|
||||||
|
|
||||||
/// Clocks configuration
|
/// Clocks configuration
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
|
|
@ -47,6 +47,18 @@ struct RtcInstant {
|
||||||
subsecond: u16,
|
subsecond: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "low-power", feature = "defmt"))]
|
||||||
|
impl defmt::Format for RtcInstant {
|
||||||
|
fn format(&self, fmt: defmt::Formatter) {
|
||||||
|
defmt::write!(
|
||||||
|
fmt,
|
||||||
|
"{}:{}",
|
||||||
|
self.second,
|
||||||
|
RTC::regs().prer().read().prediv_s() - self.subsecond,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "low-power")]
|
#[cfg(feature = "low-power")]
|
||||||
impl core::ops::Sub for RtcInstant {
|
impl core::ops::Sub for RtcInstant {
|
||||||
type Output = embassy_time::Duration;
|
type Output = embassy_time::Duration;
|
||||||
|
@ -174,7 +186,7 @@ impl Rtc {
|
||||||
let second = bcd2_to_byte((tr.st(), tr.su()));
|
let second = bcd2_to_byte((tr.st(), tr.su()));
|
||||||
|
|
||||||
// Unlock the registers
|
// Unlock the registers
|
||||||
r.dr();
|
r.dr().read();
|
||||||
|
|
||||||
RtcInstant { second, subsecond }
|
RtcInstant { second, subsecond }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
use stm32_metapac::rtc::vals::{Init, Osel, Pol};
|
use stm32_metapac::rtc::vals::{Init, Osel, Pol};
|
||||||
|
|
||||||
#[cfg(feature = "low-power")]
|
|
||||||
use super::RtcInstant;
|
|
||||||
use super::{sealed, RtcConfig};
|
use super::{sealed, RtcConfig};
|
||||||
use crate::pac::rtc::Rtc;
|
use crate::pac::rtc::Rtc;
|
||||||
use crate::peripherals::RTC;
|
use crate::peripherals::RTC;
|
||||||
|
@ -77,6 +75,21 @@ impl super::Rtc {
|
||||||
/// start the wakeup alarm and wtih a duration that is as close to but less than
|
/// start the wakeup alarm and wtih a duration that is as close to but less than
|
||||||
/// the requested duration, and record the instant the wakeup alarm was started
|
/// the requested duration, and record the instant the wakeup alarm was started
|
||||||
pub(crate) fn start_wakeup_alarm(&self, requested_duration: embassy_time::Duration) {
|
pub(crate) fn start_wakeup_alarm(&self, requested_duration: embassy_time::Duration) {
|
||||||
|
#[cfg(feature = "rtc-debug")]
|
||||||
|
if critical_section::with(|cs| {
|
||||||
|
if let Some(instant) = self.stop_time.borrow(cs).take() {
|
||||||
|
self.stop_time.borrow(cs).replace(Some(instant));
|
||||||
|
|
||||||
|
Some(())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.is_some()
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
use embassy_time::{Duration, TICK_HZ};
|
use embassy_time::{Duration, TICK_HZ};
|
||||||
|
|
||||||
use crate::rcc::get_freqs;
|
use crate::rcc::get_freqs;
|
||||||
|
@ -86,17 +99,14 @@ impl super::Rtc {
|
||||||
let rtc_ticks = requested_duration.as_ticks() * rtc_hz / TICK_HZ;
|
let rtc_ticks = requested_duration.as_ticks() * rtc_hz / TICK_HZ;
|
||||||
let prescaler = WakeupPrescaler::compute_min((rtc_ticks / u16::MAX as u64) as u32);
|
let prescaler = WakeupPrescaler::compute_min((rtc_ticks / u16::MAX as u64) as u32);
|
||||||
|
|
||||||
// adjust the rtc ticks to the prescaler
|
// adjust the rtc ticks to the prescaler and subtract one rtc tick
|
||||||
let rtc_ticks = rtc_ticks / (<WakeupPrescaler as Into<u32>>::into(prescaler) as u64);
|
let rtc_ticks = rtc_ticks / (<WakeupPrescaler as Into<u32>>::into(prescaler) as u64);
|
||||||
let rtc_ticks = if rtc_ticks >= u16::MAX as u64 {
|
let rtc_ticks = if rtc_ticks >= u16::MAX as u64 {
|
||||||
u16::MAX - 1
|
u16::MAX - 1
|
||||||
} else {
|
} else {
|
||||||
rtc_ticks as u16
|
rtc_ticks as u16
|
||||||
};
|
}
|
||||||
|
.saturating_sub(1);
|
||||||
let duration = Duration::from_ticks(
|
|
||||||
rtc_ticks as u64 * TICK_HZ * (<WakeupPrescaler as Into<u32>>::into(prescaler) as u64) / rtc_hz,
|
|
||||||
);
|
|
||||||
|
|
||||||
self.write(false, |regs| {
|
self.write(false, |regs| {
|
||||||
regs.cr().modify(|w| w.set_wute(false));
|
regs.cr().modify(|w| w.set_wute(false));
|
||||||
|
@ -104,11 +114,21 @@ impl super::Rtc {
|
||||||
while !regs.isr().read().wutwf() {}
|
while !regs.isr().read().wutwf() {}
|
||||||
|
|
||||||
regs.cr().modify(|w| w.set_wucksel(prescaler.into()));
|
regs.cr().modify(|w| w.set_wucksel(prescaler.into()));
|
||||||
|
regs.wutr().write(|w| w.set_wut(rtc_ticks));
|
||||||
regs.cr().modify(|w| w.set_wute(true));
|
regs.cr().modify(|w| w.set_wute(true));
|
||||||
regs.cr().modify(|w| w.set_wutie(true));
|
regs.cr().modify(|w| w.set_wutie(true));
|
||||||
});
|
});
|
||||||
|
|
||||||
trace!("rtc: start wakeup alarm for {} ms", duration.as_millis());
|
trace!(
|
||||||
|
"rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}",
|
||||||
|
Duration::from_ticks(
|
||||||
|
rtc_ticks as u64 * TICK_HZ * (<WakeupPrescaler as Into<u32>>::into(prescaler) as u64) / rtc_hz,
|
||||||
|
)
|
||||||
|
.as_millis(),
|
||||||
|
<WakeupPrescaler as Into<u32>>::into(prescaler),
|
||||||
|
rtc_ticks,
|
||||||
|
self.instant(),
|
||||||
|
);
|
||||||
|
|
||||||
critical_section::with(|cs| assert!(self.stop_time.borrow(cs).replace(Some(self.instant())).is_none()))
|
critical_section::with(|cs| assert!(self.stop_time.borrow(cs).replace(Some(self.instant())).is_none()))
|
||||||
}
|
}
|
||||||
|
@ -119,7 +139,10 @@ impl super::Rtc {
|
||||||
pub(crate) fn stop_wakeup_alarm(&self) -> Option<embassy_time::Duration> {
|
pub(crate) fn stop_wakeup_alarm(&self) -> Option<embassy_time::Duration> {
|
||||||
use crate::interrupt::typelevel::Interrupt;
|
use crate::interrupt::typelevel::Interrupt;
|
||||||
|
|
||||||
trace!("rtc: stop wakeup alarm...");
|
trace!("rtc: stop wakeup alarm at {}", self.instant());
|
||||||
|
|
||||||
|
#[cfg(feature = "rtc-debug")]
|
||||||
|
return None;
|
||||||
|
|
||||||
self.write(false, |regs| {
|
self.write(false, |regs| {
|
||||||
regs.cr().modify(|w| w.set_wutie(false));
|
regs.cr().modify(|w| w.set_wutie(false));
|
||||||
|
|
|
@ -363,6 +363,7 @@ impl RtcDriver {
|
||||||
.start_wakeup_alarm(time_until_next_alarm);
|
.start_wakeup_alarm(time_until_next_alarm);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
#[cfg(not(feature = "rtc-debug"))]
|
||||||
T::regs_gp16().cr1().modify(|w| w.set_cen(false));
|
T::regs_gp16().cr1().modify(|w| w.set_cen(false));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -374,6 +375,7 @@ impl RtcDriver {
|
||||||
pub(crate) fn resume_time(&self) {
|
pub(crate) fn resume_time(&self) {
|
||||||
self.stop_wakeup_alarm();
|
self.stop_wakeup_alarm();
|
||||||
|
|
||||||
|
#[cfg(not(feature = "rtc-debug"))]
|
||||||
T::regs_gp16().cr1().modify(|w| w.set_cen(true));
|
T::regs_gp16().cr1().modify(|w| w.set_cen(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,8 +46,10 @@ async fn async_main(_spawner: Spawner) {
|
||||||
|
|
||||||
stop_with_rtc(rtc);
|
stop_with_rtc(rtc);
|
||||||
|
|
||||||
info!("Waiting 5 seconds");
|
info!("Waiting...");
|
||||||
Timer::after(Duration::from_secs(5)).await;
|
Timer::after(Duration::from_secs(2)).await;
|
||||||
|
info!("Waiting...");
|
||||||
|
Timer::after(Duration::from_secs(3)).await;
|
||||||
|
|
||||||
info!("Test OK");
|
info!("Test OK");
|
||||||
cortex_m::asm::bkpt();
|
cortex_m::asm::bkpt();
|
||||||
|
|
Loading…
Reference in a new issue