os_time.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
20 #ifndef OS_OS_TIME_H
21 #define OS_OS_TIME_H
22 
23 #include "os/os_error.h"
24 #include "ztimer.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
33 #define OS_TICKS_PER_SEC (MS_PER_SEC)
34 
40 static inline os_time_t os_time_get(void)
41 {
42  return ztimer_now(ZTIMER_MSEC);
43 }
44 
53 static inline os_error_t os_time_ms_to_ticks(uint32_t ms, os_time_t *out_ticks)
54 {
55  *out_ticks = ms;
56  return OS_OK;
57 }
58 
67 static inline os_error_t os_time_ticks_to_ms(os_time_t ticks, uint32_t *out_ms)
68 {
69  *out_ms = ticks;
70  return OS_OK;
71 }
72 
80 static inline os_time_t os_time_ms_to_ticks32(uint32_t ms)
81 {
82  return ms;
83 }
84 
93 {
94  return ticks;
95 }
96 
102 static inline void os_time_delay(os_time_t ticks)
103 {
104  if (irq_is_in()) {
105  ztimer_spin(ZTIMER_MSEC, ticks);
106  }
107  else {
108  ztimer_sleep(ZTIMER_MSEC, ticks);
109  }
110 }
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* OS_OS_TIME_H */
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:742
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition: ztimer.h:683
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:80
static os_time_t os_time_get(void)
Returns the low 32 bits of cputime.
Definition: os_time.h:40
static void os_time_delay(os_time_t ticks)
Wait until the number of ticks has elapsed, BLOICKING.
Definition: os_time.h:102
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:92
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:67
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:53
uint32_t os_time_t
time type
Definition: os_types.h:48
ztimer API