1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/* Copyright (c) [2023] [Syswonder Community]
* [Rukos] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
//! Time-related operations.
pub use core::time::Duration;
/// A measurement of the system clock.
///
/// Currently, it reuses the [`core::time::Duration`] type. But it does not
/// represent a duration, but a clock time.
pub type TimeValue = Duration;
#[cfg(feature = "irq")]
pub use crate::platform::irq::TIMER_IRQ_NUM;
#[cfg(feature = "irq")]
pub use crate::platform::time::set_oneshot_timer;
pub use crate::platform::time::{current_ticks, nanos_to_ticks, ticks_to_nanos};
#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))]
#[cfg(feature = "rtc")]
pub use crate::platform::time::{rtc_read_time, rtc_write_time};
/// Number of milliseconds in a second.
pub const MILLIS_PER_SEC: u64 = 1_000;
/// Number of microseconds in a second.
pub const MICROS_PER_SEC: u64 = 1_000_000;
/// Number of nanoseconds in a second.
pub const NANOS_PER_SEC: u64 = 1_000_000_000;
/// Number of nanoseconds in a millisecond.
pub const NANOS_PER_MILLIS: u64 = 1_000_000;
/// Number of nanoseconds in a microsecond.
pub const NANOS_PER_MICROS: u64 = 1_000;
/// Returns the current clock time in nanoseconds.
pub fn current_time_nanos() -> u64 {
ticks_to_nanos(current_ticks())
}
/// 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")]
{
let nanos = current_time_nanos();
let rtc_time = rtc_read_time();
return Duration::new(rtc_time, (nanos % (NANOS_PER_SEC)) as u32);
}
TimeValue::from_nanos(current_time_nanos())
}
/// set time value
pub fn set_current_time(_new_tv: TimeValue) {
#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))]
#[cfg(feature = "rtc")]
rtc_write_time(_new_tv.as_secs() as u32);
}
/// Busy waiting for the given duration.
pub fn busy_wait(dur: Duration) {
busy_wait_until(current_time() + dur);
}
/// Busy waiting until reaching the given deadline.
pub fn busy_wait_until(deadline: TimeValue) {
while current_time() < deadline {
core::hint::spin_loop();
}
}