Skip to content

Commit

Permalink
fix: use hclk/8 tick time driver, compatible with v103 SYSTICK
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed May 2, 2024
1 parent db60ee8 commit a260e52
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/embassy/time_driver_systick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ impl SystickDriver {
let rb = &crate::pac::SYSTICK;
let hclk = crate::rcc::clocks().hclk.0 as u64;

let cnt_per_second = hclk; // not HCLK/8
let cnt_per_second = hclk / 8; // HCLK/8
let cnt_per_tick = cnt_per_second / embassy_time_driver::TICK_HZ;

crate::println!(
"using systick: hclk={} cnt_per_second={} cnt_per_tick={}",
hclk,
cnt_per_second,
cnt_per_tick
);

self.period.store(cnt_per_tick as u32, Ordering::Relaxed);

// UNDOCUMENTED: Avoid initial interrupt
Expand All @@ -70,7 +77,7 @@ impl SystickDriver {
// w.set_init(true);
w.set_mode(vals::Mode::UPCOUNT);
w.set_stre(false);
w.set_stclk(vals::Stclk::HCLK);
w.set_stclk(vals::Stclk::HCLK_DIV8);
w.set_ste(true);
});
})
Expand All @@ -81,18 +88,19 @@ impl SystickDriver {
let rb = &crate::pac::SYSTICK;
rb.sr().write(|w| w.set_cntif(false)); // clear IF

let period = self.period.load(Ordering::Relaxed) as u64;

let next_timestamp = critical_section::with(|cs| {
let next = self.alarms.borrow(cs)[0].timestamp.get();
if next > self.now() {
if next > self.now() + 1 {
return next;
}
self.trigger_alarm(cs);
return u64::MAX;
});

let period = self.period.load(Ordering::Relaxed) as u64;
let new_cmp = u64::min(self.raw_cnt().wrapping_add(period), next_timestamp.wrapping_mul(period));
rb.cmp().write_value(new_cmp);
let new_cmp = u64::min(next_timestamp * period, self.raw_cnt().wrapping_add(period));
rb.cmp().write_value(new_cmp + 1);
}

fn trigger_alarm(&self, cs: CriticalSection) {
Expand Down

0 comments on commit a260e52

Please sign in to comment.