Skip to content

Commit

Permalink
improve x86 rtc
Browse files Browse the repository at this point in the history
  • Loading branch information
lhw2002426 committed Feb 26, 2024
1 parent 07a16d5 commit 34bbdf8
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions modules/ruxhal/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,22 @@ pub fn current_time_nanos() -> u64 {
/// Returns the current clock time in [`TimeValue`].
#[allow(unreachable_code)]
pub fn current_time() -> TimeValue {
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
#[cfg(feature = "rtc")]
#[cfg(all(feature = "rtc", target_arch = "x86_64"))]
{
unsafe {
static mut base_time: u64 = 0;
let nanos = current_time_nanos();
if base_time == 0 {
base_time = rtc_read_time();
}
if (nanos / NANOS_PER_SEC) % 1800 == 0 {
base_time = rtc_read_time() - (nanos / NANOS_PER_SEC);
}
let rtc_time = base_time + (nanos / NANOS_PER_SEC);
return Duration::new(rtc_time, (nanos % (NANOS_PER_SEC)) as u32);
}
}
#[cfg(all(feature = "rtc", target_arch = "aarch64"))]
{
let nanos = current_time_nanos();
let rtc_time = rtc_read_time();
Expand Down

0 comments on commit 34bbdf8

Please sign in to comment.