ove/
time.rs

1// Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4//
5// This file is part of oveRTOS.
6
7//! Time and delay utilities for oveRTOS.
8//!
9//! Provides a monotonic microsecond timestamp ([`get_us`]) and busy-wait or
10//! scheduler-yielding delay functions ([`delay_ms`], [`delay_us`]).
11
12use crate::bindings;
13use crate::error::{Error, Result};
14
15/// Get the current monotonic time in microseconds since an arbitrary epoch.
16///
17/// # Errors
18/// Returns an error if the underlying hardware timer is unavailable.
19pub fn get_us() -> Result<u64> {
20    let mut us: u64 = 0;
21    let rc = unsafe { bindings::ove_time_get_us(&mut us) };
22    Error::from_code(rc)?;
23    Ok(us)
24}
25
26/// Block the current thread for at least `ms` milliseconds.
27///
28/// Yields the CPU to other threads for the duration. Prefer [`crate::Thread::sleep_ms`]
29/// for thread-level sleeping.
30pub fn delay_ms(ms: u32) {
31    unsafe { bindings::ove_time_delay_ms(ms) }
32}
33
34/// Block the current thread for at least `us` microseconds.
35///
36/// On most platforms this is a busy-wait for short durations; use sparingly.
37pub fn delay_us(us: u32) {
38    unsafe { bindings::ove_time_delay_us(us) }
39}