oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
27#ifndef OVE_TIME_H
28#define OVE_TIME_H
29
30#include "ove/types.h"
31#include "ove_config.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#ifdef CONFIG_OVE_TIME
38
49int ove_time_get_us(uint64_t *out);
50
62int ove_time_get_ns(uint64_t *out);
63
74void ove_time_delay_ms(uint32_t ms);
75
86void ove_time_delay_us(uint32_t us);
87
88#else /* !CONFIG_OVE_TIME */
89
90static inline int ove_time_get_us(uint64_t *o)
91{
92 if (o) {
93 *o = 0;
94 }
96}
97static inline int ove_time_get_ns(uint64_t *o)
98{
99 if (o) {
100 *o = 0;
101 }
103}
104static inline void ove_time_delay_ms(uint32_t ms)
105{
106 (void)ms;
107}
108static inline void ove_time_delay_us(uint32_t us)
109{
110 (void)us;
111}
112
113#endif /* CONFIG_OVE_TIME */
114
115/* ── Duration helper macros (ns-resolution) ────────────────────────────
116 *
117 * Express timeouts for the upcoming uint64_t ns APIs ergonomically:
118 * ove_mutex_lock_ns(m, OVE_MS(100))
119 * ove_queue_send_ns(q, &item, OVE_SEC(5))
120 *
121 * All multiplications fold at compile time for constant inputs.
122 * Available in both CONFIG_OVE_TIME modes — the macros don't call into
123 * the time backend.
124 */
125#define OVE_NS(n) ((uint64_t)(n))
126#define OVE_US(n) ((uint64_t)(n) * 1000ULL)
127#define OVE_MS(n) ((uint64_t)(n) * 1000000ULL)
128#define OVE_SEC(n) ((uint64_t)(n) * 1000000000ULL)
129#define OVE_MIN(n) (OVE_SEC(n) * 60ULL)
130
140static inline uint64_t ove_time_now_steady_ns(void)
141{
142 uint64_t out = 0;
143 int rc = ove_time_get_ns(&out);
144 (void)rc;
145 return out;
146}
147
162static inline uint64_t ove_time_deadline_to_timeout_ns(uint64_t deadline_ns)
163{
164 if (deadline_ns == OVE_WAIT_FOREVER)
165 return OVE_WAIT_FOREVER;
166 uint64_t now = ove_time_now_steady_ns();
167 return (deadline_ns > now) ? (deadline_ns - now) : 0;
168}
169
170#ifdef __cplusplus
171}
172#endif
173
/* end of ove_time group */
175
176#endif /* OVE_TIME_H */
void ove_time_delay_ms(uint32_t ms)
Block the calling task for at least ms milliseconds.
void ove_time_delay_us(uint32_t us)
Block the calling task for at least us microseconds.
static uint64_t ove_time_now_steady_ns(void)
Get the current monotonic time in nanoseconds (value-return form).
Definition time.h:140
static uint64_t ove_time_deadline_to_timeout_ns(uint64_t deadline_ns)
Convert a steady-clock deadline to a duration suitable for the existing timeout_ns-taking APIs.
Definition time.h:162
int ove_time_get_ns(uint64_t *out)
Get the current monotonic time in nanoseconds.
int ove_time_get_us(uint64_t *out)
Get the current monotonic time in microseconds.
#define OVE_WAIT_FOREVER
Timeout value that means "block indefinitely".
Definition types.h:162
@ OVE_ERR_NOT_SUPPORTED
Definition types.h:98