Skip to main content

leodos_libcfs/psp/
time.rs

1//! Safe wrappers for PSP high-resolution time functions.
2//!
3//! This provides access to a raw, monotonic hardware clock, which is useful for
4//! performance measurements. This time is distinct from the mission time provided
5//! by `CFE_TIME`.
6
7use crate::ffi;
8use crate::os::time::OsTime;
9use core::mem::MaybeUninit;
10
11/// A 64-bit timestamp from a high-resolution, monotonic hardware clock.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct Timebase {
14    /// The upper 32 bits of the 64-bit timebase value.
15    pub upper_32: u32,
16    /// The lower 32 bits of the 64-bit timebase value.
17    pub lower_32: u32,
18}
19
20impl Timebase {
21    /// Returns the full 64-bit value.
22    pub fn as_u64(&self) -> u64 {
23        ((self.upper_32 as u64) << 32) | (self.lower_32 as u64)
24    }
25}
26
27/// Reads the raw, monotonic platform clock without normalization.
28pub fn get_timebase() -> Timebase {
29    let mut upper = MaybeUninit::uninit();
30    let mut lower = MaybeUninit::uninit();
31    unsafe { ffi::CFE_PSP_Get_Timebase(upper.as_mut_ptr(), lower.as_mut_ptr()) };
32    Timebase {
33        upper_32: unsafe { upper.assume_init() },
34        lower_32: unsafe { lower.assume_init() },
35    }
36}
37
38/// Reads the monotonic platform clock and normalizes it to an `OsTime` value.
39pub fn get_time() -> OsTime {
40    let mut time = MaybeUninit::uninit();
41    unsafe { ffi::CFE_PSP_GetTime(time.as_mut_ptr()) };
42    OsTime(unsafe { time.assume_init() })
43}
44
45/// Returns the resolution of the timebase clock in ticks per
46/// second.
47///
48/// Guaranteed to be at least 1 MHz (1 µs per tick).
49pub fn get_timer_ticks_per_second() -> u32 {
50    unsafe { ffi::CFE_PSP_GetTimerTicksPerSecond() }
51}
52
53/// Returns the value at which the lower 32 bits of the timebase clock roll over.
54///
55/// A value of 0 indicates that it rolls over at the maximum `u32` value.
56pub fn get_timer_low32_rollover() -> u32 {
57    unsafe { ffi::CFE_PSP_GetTimerLow32Rollover() }
58}