Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
clock.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2015 Wind River Systems, Inc.
3 * Copyright (c) 2025 Tenstorrent AI ULC
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
17
18#ifndef ZEPHYR_INCLUDE_SYS_CLOCK_H_
19#define ZEPHYR_INCLUDE_SYS_CLOCK_H_
20
21#include <zephyr/sys/dlist.h>
23#include <zephyr/sys/util.h>
24#include <zephyr/toolchain.h>
25#include <zephyr/types.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
35
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 SEC_PER_HOUR 3600U
99
101#define SEC_PER_DAY 86400U
102
104#define MIN_PER_HOUR 60U
105
107#define HOUR_PER_DAY 24U
108
110#define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC))
111
113#define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC))
114
116
118#define Z_TIMEOUT_NO_WAIT_INIT {0}
119#define Z_TIMEOUT_NO_WAIT ((k_timeout_t)Z_TIMEOUT_NO_WAIT_INIT)
120#if defined(__cplusplus) && ((__cplusplus - 0) < 202002L)
121#define Z_TIMEOUT_TICKS_INIT(t) {(t)}
122#else
123#define Z_TIMEOUT_TICKS_INIT(t) {.ticks = (t)}
124#endif
125#define Z_TIMEOUT_TICKS(t) ((k_timeout_t)Z_TIMEOUT_TICKS_INIT(t))
126#define Z_FOREVER Z_TIMEOUT_TICKS(K_TICKS_FOREVER)
127
128#ifdef CONFIG_TIMEOUT_64BIT
129#define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ms_to_ticks_ceil64(MAX(t, 0)))
130#define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS((k_ticks_t)k_us_to_ticks_ceil64(MAX(t, 0)))
131#define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ns_to_ticks_ceil64(MAX(t, 0)))
132#define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS((k_ticks_t)k_cyc_to_ticks_ceil64(MAX(t, 0)))
133#define Z_TIMEOUT_MS_TICKS(t) ((k_ticks_t)k_ms_to_ticks_ceil64(MAX(t, 0)))
134#else
135#define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ms_to_ticks_ceil32(MAX(t, 0)))
136#define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS((k_ticks_t)k_us_to_ticks_ceil32(MAX(t, 0)))
137#define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ns_to_ticks_ceil32(MAX(t, 0)))
138#define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS((k_ticks_t)k_cyc_to_ticks_ceil32(MAX(t, 0)))
139#define Z_TIMEOUT_MS_TICKS(t) ((k_ticks_t)k_ms_to_ticks_ceil32(MAX(t, 0)))
140#endif
141
142/* Converts between absolute timeout expiration values (packed into
143 * the negative space below K_TICKS_FOREVER) and (non-negative) delta
144 * timeout values. If the result of Z_TICK_ABS(t) is >= 0, then the
145 * value was an absolute timeout with the returned expiration time.
146 * Note that this macro is bidirectional: Z_TICK_ABS(Z_TICK_ABS(t)) ==
147 * t for all inputs, and that the representation of K_TICKS_FOREVER is
148 * the same value in both spaces! Clever, huh?
149 */
150#define Z_TICK_ABS(t) (K_TICKS_FOREVER - 1 - (t))
151
152/* Test for relative timeout */
153#if CONFIG_TIMEOUT_64BIT
154/* Positive values are relative/delta timeouts and negative values are absolute
155 * timeouts, except -1 which is reserved for K_TIMEOUT_FOREVER. 0 is K_NO_WAIT,
156 * which is historically considered a relative timeout.
157 * K_TIMEOUT_FOREVER is not considered a relative timeout and neither is it
158 * considerd an absolute timeouts (so !Z_IS_TIMEOUT_RELATIVE() does not
159 * necessarily mean it is an absolute timeout if ticks == -1);
160 */
161#define Z_IS_TIMEOUT_RELATIVE(timeout) (((timeout).ticks) >= 0)
162#else
163#define Z_IS_TIMEOUT_RELATIVE(timeout) true
164#endif
165
166/* added tick needed to account for tick in progress */
167#define _TICK_ALIGN 1
168
169/* The minimum duration in ticks strictly greater than that of K_NO_WAIT */
170#define K_TICK_MIN ((k_ticks_t)1)
171
172/* The maximum duration in ticks strictly and semantically "less than" K_FOREVER */
173#define K_TICK_MAX ((k_ticks_t)(IS_ENABLED(CONFIG_TIMEOUT_64BIT) ? INT64_MAX : UINT32_MAX - 1))
174
189static inline k_ticks_t z_timeout_sum(k_timeout_t t1, k_timeout_t t2)
190{
191 k_ticks_t ticks1 = t1.ticks;
192 k_ticks_t ticks2 = t2.ticks;
193
194#ifdef CONFIG_TIMEOUT_64BIT
195 if ((ticks1 == K_TICKS_FOREVER) || (ticks2 == K_TICKS_FOREVER)) {
196 return K_TICKS_FOREVER;
197 }
198
199 if (ticks1 < 0) {
200 if (ticks2 < 0) {
201 return K_TICKS_FOREVER; /* Both absolute timeouts */
202 }
203
204 return ((ticks1 - INT64_MIN) < ticks2) ?
205 K_TICKS_FOREVER : (ticks1 - ticks2);
206 } else if (ticks2 < 0) {
207 return ((ticks2 - INT64_MIN) < ticks1) ?
208 K_TICKS_FOREVER : (ticks2 - ticks1);
209 } else {
210 return ((INT64_MAX - ticks1) < ticks2) ?
211 K_TICKS_FOREVER : ticks1 + ticks2;
212 }
213#else
214 return ((UINT32_MAX - ticks1) < ticks2) ? K_TICKS_FOREVER : ticks1 + ticks2;
215#endif
216}
217
219
220#ifndef CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME
221#if defined(CONFIG_SYS_CLOCK_EXISTS)
222#if CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0
223#error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!"
224#endif /* CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0 */
225#endif /* CONFIG_SYS_CLOCK_EXISTS */
226#endif /* CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME */
227
228/* kernel clocks */
229
230/*
231 * We default to using 64-bit intermediates in timescale conversions,
232 * but if the HW timer cycles/sec, ticks/sec and ms/sec are all known
233 * to be nicely related, then we can cheat with 32 bits instead.
234 */
239
240#ifdef CONFIG_SYS_CLOCK_EXISTS
241
242#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
243 (MSEC_PER_SEC % CONFIG_SYS_CLOCK_TICKS_PER_SEC) || \
244 (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC % CONFIG_SYS_CLOCK_TICKS_PER_SEC)
245#define _NEED_PRECISE_TICK_MS_CONVERSION
246#endif
247
248#endif
249
254#define SYS_CLOCK_HW_CYCLES_TO_NS_AVG(X, NCYCLES) (uint32_t)(k_cyc_to_ns_floor64(X) / NCYCLES)
255
264
273
274#ifndef CONFIG_SYS_CLOCK_EXISTS
275#define sys_clock_tick_get() (0)
276#define sys_clock_tick_get_32() (0)
277#endif
278
279#ifdef CONFIG_SYS_CLOCK_EXISTS
280
291typedef struct {
294
312
327
339{
340 if (a.tick == b.tick) {
341 return 0;
342 }
343 return a.tick < b.tick ? -1 : 1;
344}
345
346#else
347
348/*
349 * When timers are configured out, timepoints can't relate to anything.
350 * The best we can do is to preserve whether or not they are derived from
351 * K_NO_WAIT. Anything else will translate back to K_FOREVER.
352 */
353typedef struct {
354 bool wait;
356
357static inline k_timepoint_t sys_timepoint_calc(k_timeout_t timeout)
358{
359 k_timepoint_t timepoint;
360
361 timepoint.wait = !K_TIMEOUT_EQ(timeout, Z_TIMEOUT_NO_WAIT);
362 return timepoint;
363}
364
365static inline k_timeout_t sys_timepoint_timeout(k_timepoint_t timepoint)
366{
367 return timepoint.wait ? Z_FOREVER : Z_TIMEOUT_NO_WAIT;
368}
369
370static inline int sys_timepoint_cmp(k_timepoint_t a, k_timepoint_t b)
371{
372 if (a.wait == b.wait) {
373 return 0;
374 }
375 return b.wait ? -1 : 1;
376}
377
378#endif
379
388static inline bool sys_timepoint_expired(k_timepoint_t timepoint)
389{
390 return K_TIMEOUT_EQ(sys_timepoint_timeout(timepoint), Z_TIMEOUT_NO_WAIT);
391}
392
394
399
409#define SYS_CLOCK_REALTIME 1
410
417#define SYS_CLOCK_MONOTONIC 4
418
425#define SYS_TIMER_ABSTIME 4
426
428/* forward declaration as workaround for time.h */
429struct timespec;
430
431/* Convert a POSIX clock (cast to int) to a sys_clock identifier */
432int sys_clock_from_clockid(int clock_id);
434
451__syscall void sys_clock_getrtoffset(struct timespec *tp);
452
461int sys_clock_gettime(int clock_id, struct timespec *tp);
462
472__syscall int sys_clock_settime(int clock_id, const struct timespec *tp);
473
499__syscall int sys_clock_nanosleep(int clock_id, int flags, const struct timespec *rqtp,
500 struct timespec *rmtp);
501
505
506#ifndef CONFIG_BOARD_UNIT_TESTING
507#include <zephyr/syscalls/clock.h>
508#endif
509
510#ifdef __cplusplus
511}
512#endif
513
514#endif /* ZEPHYR_INCLUDE_SYS_CLOCK_H_ */
int sys_clock_nanosleep(int clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp)
Sleep for the specified amount of time with respect to the specified clock.
int sys_clock_settime(int clock_id, const struct timespec *tp)
Set the current time for the specified clock.
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.
#define K_TICKS_FOREVER
Definition clock.h:51
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 clock.h:388
int sys_clock_gettime(int clock_id, struct timespec *tp)
Get the current time from the specified clock.
uint32_t k_ticks_t
Tick precision used in timeout APIs.
Definition clock.h:48
#define K_TIMEOUT_EQ(a, b)
Compare timeouts for equality.
Definition clock.h:80
void sys_clock_getrtoffset(struct timespec *tp)
Get the offset SYS_CLOCK_REALTIME with respect to SYS_CLOCK_MONOTONIC.
static int sys_timepoint_cmp(k_timepoint_t a, k_timepoint_t b)
Compare two timepoint values.
Definition clock.h:338
flags
Definition parser.h:97
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
#define INT64_MIN
Definition stdint.h:25
#define UINT32_MAX
Definition stdint.h:29
__INT64_TYPE__ int64_t
Definition stdint.h:75
#define INT64_MAX
Definition stdint.h:19
Kernel timeout type.
Definition clock.h:65
k_ticks_t ticks
Definition clock.h:66
Kernel timepoint type.
Definition clock.h:291
uint64_t tick
Definition clock.h:292
Definition posix_signal.h:53
Misc utilities.
Macros to abstract toolchain specific capabilities.