From 9c6d4914162b6f42e121e4b464ff1ba00fdb76ca Mon Sep 17 00:00:00 2001 From: lhw2002426 <1466397747@qq.com> Date: Tue, 27 Feb 2024 09:24:53 +0800 Subject: [PATCH] improve x86 rtc --- modules/ruxhal/src/time.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/modules/ruxhal/src/time.rs b/modules/ruxhal/src/time.rs index 47363cd15..749b41a0b 100644 --- a/modules/ruxhal/src/time.rs +++ b/modules/ruxhal/src/time.rs @@ -42,11 +42,30 @@ pub fn current_time_nanos() -> u64 { ticks_to_nanos(current_ticks()) } +#[cfg(all(feature = "rtc", target_arch = "x86_64"))] +pub static mut BASE_TIME: u64 = 0; +#[cfg(all(feature = "rtc", target_arch = "x86_64"))] +pub static mut LAST_TIME: u64 = 0; + /// 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 { + let nanos = current_time_nanos(); + if BASE_TIME == 0 { + BASE_TIME = rtc_read_time(); + } + if (nanos / NANOS_PER_SEC) - LAST_TIME > 1800 { + BASE_TIME = rtc_read_time() - (nanos / NANOS_PER_SEC); + LAST_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();