Zephyr API Documentation 3.7.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
sys_clock.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2015 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
16#ifndef ZEPHYR_INCLUDE_SYS_CLOCK_H_
17#define ZEPHYR_INCLUDE_SYS_CLOCK_H_
18
19#include <zephyr/sys/util.h>
20#include <zephyr/sys/dlist.h>
21
22#include <zephyr/toolchain.h>
23#include <zephyr/types.h>
24
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
45#ifdef CONFIG_TIMEOUT_64BIT
46typedef int64_t k_ticks_t;
47#else
49#endif
50
51#define K_TICKS_FOREVER ((k_ticks_t) -1)
52
65typedef struct {
68
80#define K_TIMEOUT_EQ(a, b) ((a).ticks == (b).ticks)
81
83#define NSEC_PER_USEC 1000U
84
86#define NSEC_PER_MSEC 1000000U
87
89#define USEC_PER_MSEC 1000U
90
92#define MSEC_PER_SEC 1000U
93
95#define SEC_PER_MIN 60U
96
98#define MIN_PER_HOUR 60U
99
101#define HOUR_PER_DAY 24U
102
104#define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC))
105
107#define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC))
108
112#define Z_TIMEOUT_NO_WAIT ((k_timeout_t) {0})
113#if defined(__cplusplus) && ((__cplusplus - 0) < 202002L)
114#define Z_TIMEOUT_TICKS(t) ((k_timeout_t) { (t) })
115#else
116#define Z_TIMEOUT_TICKS(t) ((k_timeout_t) { .ticks = (t) })
117#endif
118#define Z_FOREVER Z_TIMEOUT_TICKS(K_TICKS_FOREVER)
119
120#ifdef CONFIG_TIMEOUT_64BIT
121# define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ms_to_ticks_ceil64(MAX(t, 0)))
122# define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS((k_ticks_t)k_us_to_ticks_ceil64(MAX(t, 0)))
123# define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ns_to_ticks_ceil64(MAX(t, 0)))
124# define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS((k_ticks_t)k_cyc_to_ticks_ceil64(MAX(t, 0)))
125# define Z_TIMEOUT_MS_TICKS(t) ((k_ticks_t)k_ms_to_ticks_ceil64(MAX(t, 0)))
126#else
127# define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ms_to_ticks_ceil32(MAX(t, 0)))
128# define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS((k_ticks_t)k_us_to_ticks_ceil32(MAX(t, 0)))
129# define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ns_to_ticks_ceil32(MAX(t, 0)))
130# define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS((k_ticks_t)k_cyc_to_ticks_ceil32(MAX(t, 0)))
131# define Z_TIMEOUT_MS_TICKS(t) ((k_ticks_t)k_ms_to_ticks_ceil32(MAX(t, 0)))
132#endif
133
134/* Converts between absolute timeout expiration values (packed into
135 * the negative space below K_TICKS_FOREVER) and (non-negative) delta
136 * timeout values. If the result of Z_TICK_ABS(t) is >= 0, then the
137 * value was an absolute timeout with the returned expiration time.
138 * Note that this macro is bidirectional: Z_TICK_ABS(Z_TICK_ABS(t)) ==
139 * t for all inputs, and that the representation of K_TICKS_FOREVER is
140 * the same value in both spaces! Clever, huh?
141 */
142#define Z_TICK_ABS(t) (K_TICKS_FOREVER - 1 - (t))
143
144/* added tick needed to account for tick in progress */
145#define _TICK_ALIGN 1
146
149#if defined(CONFIG_SYS_CLOCK_EXISTS) && \
150 (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0)
151#error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!"
152#endif
153
154
155/* kernel clocks */
156
157/*
158 * We default to using 64-bit intermediates in timescale conversions,
159 * but if the HW timer cycles/sec, ticks/sec and ms/sec are all known
160 * to be nicely related, then we can cheat with 32 bits instead.
161 */
167#ifdef CONFIG_SYS_CLOCK_EXISTS
168
169#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
170 (MSEC_PER_SEC % CONFIG_SYS_CLOCK_TICKS_PER_SEC) || \
171 (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC % CONFIG_SYS_CLOCK_TICKS_PER_SEC)
172#define _NEED_PRECISE_TICK_MS_CONVERSION
173#endif
174
175#endif
176
181#define SYS_CLOCK_HW_CYCLES_TO_NS_AVG(X, NCYCLES) \
182 (uint32_t)(k_cyc_to_ns_floor64(X) / NCYCLES)
183
192
201
202#ifndef CONFIG_SYS_CLOCK_EXISTS
203#define sys_clock_tick_get() (0)
204#define sys_clock_tick_get_32() (0)
205#endif
206
207#ifdef CONFIG_SYS_CLOCK_EXISTS
208
219typedef struct { uint64_t tick; } k_timepoint_t;
220
238
253
265{
266 if (a.tick == b.tick) {
267 return 0;
268 }
269 return a.tick < b.tick ? -1 : 1;
270}
271
272#else
273
274/*
275 * When timers are configured out, timepoints can't relate to anything.
276 * The best we can do is to preserve whether or not they are derived from
277 * K_NO_WAIT. Anything else will translate back to K_FOREVER.
278 */
279typedef struct { bool wait; } k_timepoint_t;
280
281static inline k_timepoint_t sys_timepoint_calc(k_timeout_t timeout)
282{
283 k_timepoint_t timepoint;
284
285 timepoint.wait = !K_TIMEOUT_EQ(timeout, Z_TIMEOUT_NO_WAIT);
286 return timepoint;
287}
288
289static inline k_timeout_t sys_timepoint_timeout(k_timepoint_t timepoint)
290{
291 return timepoint.wait ? Z_FOREVER : Z_TIMEOUT_NO_WAIT;
292}
293
294static inline int sys_timepoint_cmp(k_timepoint_t a, k_timepoint_t b)
295{
296 if (a.wait == b.wait) {
297 return 0;
298 }
299 return b.wait ? -1 : 1;
300}
301
302#endif
303
312static inline bool sys_timepoint_expired(k_timepoint_t timepoint)
313{
314 return K_TIMEOUT_EQ(sys_timepoint_timeout(timepoint), Z_TIMEOUT_NO_WAIT);
315}
316
319#ifdef __cplusplus
320}
321#endif
322
323#endif /* ZEPHYR_INCLUDE_SYS_CLOCK_H_ */
uint32_t sys_clock_tick_get_32(void)
Return the lower part of the current system tick count.
k_timepoint_t sys_timepoint_calc(k_timeout_t timeout)
Calculate a timepoint value.
int64_t sys_clock_tick_get(void)
Return the current system tick count.
k_timeout_t sys_timepoint_timeout(k_timepoint_t timepoint)
Remaining time to given timepoint.
static bool sys_timepoint_expired(k_timepoint_t timepoint)
Indicates if timepoint is expired.
Definition sys_clock.h:312
uint32_t k_ticks_t
Tick precision used in timeout APIs.
Definition sys_clock.h:48
#define K_TIMEOUT_EQ(a, b)
Compare timeouts for equality.
Definition sys_clock.h:80
static int sys_timepoint_cmp(k_timepoint_t a, k_timepoint_t b)
Compare two timepoint values.
Definition sys_clock.h:264
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__INT64_TYPE__ int64_t
Definition stdint.h:75
Kernel timeout type.
Definition sys_clock.h:65
k_ticks_t ticks
Definition sys_clock.h:66
Kernel timepoint type.
Definition sys_clock.h:219
uint64_t tick
Definition sys_clock.h:219
Misc utilities.
Macros to abstract toolchain specific capabilities.