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 
9 #pragma once
10 
22 #include "os/os_error.h"
23 #include "ztimer.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
32 #define OS_TICKS_PER_SEC (MS_PER_SEC)
33 
39 static inline os_time_t os_time_get(void)
40 {
41  return ztimer_now(ZTIMER_MSEC);
42 }
43 
52 static inline os_error_t os_time_ms_to_ticks(uint32_t ms, os_time_t *out_ticks)
53 {
54  *out_ticks = ms;
55  return OS_OK;
56 }
57 
66 static inline os_error_t os_time_ticks_to_ms(os_time_t ticks, uint32_t *out_ms)
67 {
68  *out_ms = ticks;
69  return OS_OK;
70 }
71 
79 static inline os_time_t os_time_ms_to_ticks32(uint32_t ms)
80 {
81  return ms;
82 }
83 
92 {
93  return ticks;
94 }
95 
101 static inline void os_time_delay(os_time_t ticks)
102 {
103  if (irq_is_in()) {
104  ztimer_spin(ZTIMER_MSEC, ticks);
105  }
106  else {
107  ztimer_sleep(ZTIMER_MSEC, ticks);
108  }
109 }
110 
111 #ifdef __cplusplus
112 }
113 #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: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:79
static os_time_t os_time_get(void)
Returns the low 32 bits of cputime.
Definition: os_time.h:39
static void os_time_delay(os_time_t ticks)
Wait until the number of ticks has elapsed, BLOICKING.
Definition: os_time.h:101
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:91
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:66
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:52
uint32_t os_time_t
time type
Definition: os_types.h:47
ztimer API