Loading...
Searching...
No Matches
os_time.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 Inria
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
18
19#include "os/os_error.h"
20#include "ztimer.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
29#define OS_TICKS_PER_SEC (MS_PER_SEC)
30
36static inline os_time_t os_time_get(void)
37{
38 return ztimer_now(ZTIMER_MSEC);
39}
40
49static inline os_error_t os_time_ms_to_ticks(uint32_t ms, os_time_t *out_ticks)
50{
51 *out_ticks = ms;
52 return OS_OK;
53}
54
63static inline os_error_t os_time_ticks_to_ms(os_time_t ticks, uint32_t *out_ms)
64{
65 *out_ms = ticks;
66 return OS_OK;
67}
68
76static inline os_time_t os_time_ms_to_ticks32(uint32_t ms)
77{
78 return ms;
79}
80
89{
90 return ticks;
91}
92
98static inline void os_time_delay(os_time_t ticks)
99{
100 if (irq_is_in()) {
101 ztimer_spin(ZTIMER_MSEC, ticks);
102 }
103 else {
105 }
106}
107
108#ifdef __cplusplus
109}
110#endif
MAYBE_INLINE bool irq_is_in(void)
Check whether called from interrupt service routine.
static void ztimer_spin(ztimer_clock_t *clock, uint32_t duration)
Busy-wait specified duration.
Definition ztimer.h:739
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition ztimer.h:680
void ztimer_sleep(ztimer_clock_t *clock, uint32_t duration)
Put the calling thread to sleep for the specified number of ticks.
ztimer_clock_t *const ZTIMER_MSEC
Default ztimer millisecond clock.
static os_time_t os_time_ms_to_ticks32(uint32_t ms)
Converts the given number of milliseconds into cputime ticks.
Definition os_time.h:76
static os_time_t os_time_get(void)
Returns the low 32 bits of cputime.
Definition os_time.h:36
static void os_time_delay(os_time_t ticks)
Wait until the number of ticks has elapsed, BLOICKING.
Definition os_time.h:98
static os_time_t os_time_ticks_to_ms32(os_time_t ticks)
Convert the given number of ticks into milliseconds.
Definition os_time.h:88
static os_error_t os_time_ticks_to_ms(os_time_t ticks, uint32_t *out_ms)
Convert the given number of ticks into milliseconds.
Definition os_time.h:63
static os_error_t os_time_ms_to_ticks(uint32_t ms, os_time_t *out_ticks)
Converts the given number of milliseconds into cputime ticks.
Definition os_time.h:49
uint32_t os_time_t
time type
Definition os_types.h:44
ztimer API