Skip to content

fix: #4 systick timer reading 64b timer #88

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions src/embassy/time_driver_systick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,24 @@ impl SystickDriver {
#[inline]
fn raw_cnt(&self) -> u64 {
let rb = crate::pac::SYSTICK;
rb.cnt().read()
// Typical implementation of reading 64-bit value from two 32-bit
// self-incrementing registers: H->L->H loop.
// See-also: https://github.com/ch32-rs/ch32-hal/issues/4
loop {
let cnt_high = rb.cnth().read();
let cnt_low = rb.cntl().read();
if rb.cnth().read() == cnt_high {
return (cnt_high as u64) << 32 | cnt_low as u64;
}
}
}
}

impl Driver for SystickDriver {
fn now(&self) -> u64 {
let rb = crate::pac::SYSTICK;
let period = self.period.load(Ordering::Relaxed) as u64;
rb.cnt().read() / period
self.raw_cnt() / period
}

unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> {
Expand Down Expand Up @@ -162,7 +171,6 @@ impl Driver for SystickDriver {

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

// See-also: https://github.com/ch32-rs/ch32-hal/issues/4
let t = self.raw_cnt();
let timestamp = timestamp * period;
if timestamp <= t {
Expand Down