16#ifndef ZEPHYR_INCLUDE_SYS_CLOCK_H_
17#define ZEPHYR_INCLUDE_SYS_CLOCK_H_
45#ifdef CONFIG_TIMEOUT_64BIT
51#define K_TICKS_FOREVER ((k_ticks_t) -1)
80#define K_TIMEOUT_EQ(a, b) ((a).ticks == (b).ticks)
83#define NSEC_PER_USEC 1000U
86#define NSEC_PER_MSEC 1000000U
89#define USEC_PER_MSEC 1000U
92#define MSEC_PER_SEC 1000U
95#define SEC_PER_MIN 60U
98#define SEC_PER_HOUR 3600U
101#define SEC_PER_DAY 86400U
104#define MIN_PER_HOUR 60U
107#define HOUR_PER_DAY 24U
110#define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC))
113#define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC))
118#define Z_TIMEOUT_NO_WAIT ((k_timeout_t) {0})
119#if defined(__cplusplus) && ((__cplusplus - 0) < 202002L)
120#define Z_TIMEOUT_TICKS(t) ((k_timeout_t) { (t) })
122#define Z_TIMEOUT_TICKS(t) ((k_timeout_t) { .ticks = (t) })
124#define Z_FOREVER Z_TIMEOUT_TICKS(K_TICKS_FOREVER)
126#ifdef CONFIG_TIMEOUT_64BIT
127# define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ms_to_ticks_ceil64(MAX(t, 0)))
128# define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS((k_ticks_t)k_us_to_ticks_ceil64(MAX(t, 0)))
129# define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ns_to_ticks_ceil64(MAX(t, 0)))
130# define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS((k_ticks_t)k_cyc_to_ticks_ceil64(MAX(t, 0)))
131# define Z_TIMEOUT_MS_TICKS(t) ((k_ticks_t)k_ms_to_ticks_ceil64(MAX(t, 0)))
133# define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ms_to_ticks_ceil32(MAX(t, 0)))
134# define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS((k_ticks_t)k_us_to_ticks_ceil32(MAX(t, 0)))
135# define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ns_to_ticks_ceil32(MAX(t, 0)))
136# define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS((k_ticks_t)k_cyc_to_ticks_ceil32(MAX(t, 0)))
137# define Z_TIMEOUT_MS_TICKS(t) ((k_ticks_t)k_ms_to_ticks_ceil32(MAX(t, 0)))
148#define Z_TICK_ABS(t) (K_TICKS_FOREVER - 1 - (t))
155#if defined(CONFIG_SYS_CLOCK_EXISTS) && \
156 (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0)
157#error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!"
173#ifdef CONFIG_SYS_CLOCK_EXISTS
175#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
176 (MSEC_PER_SEC % CONFIG_SYS_CLOCK_TICKS_PER_SEC) || \
177 (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC % CONFIG_SYS_CLOCK_TICKS_PER_SEC)
178#define _NEED_PRECISE_TICK_MS_CONVERSION
187#define SYS_CLOCK_HW_CYCLES_TO_NS_AVG(X, NCYCLES) \
188 (uint32_t)(k_cyc_to_ns_floor64(X) / NCYCLES)
208#ifndef CONFIG_SYS_CLOCK_EXISTS
209#define sys_clock_tick_get() (0)
210#define sys_clock_tick_get_32() (0)
213#ifdef CONFIG_SYS_CLOCK_EXISTS
291 timepoint.wait = !
K_TIMEOUT_EQ(timeout, Z_TIMEOUT_NO_WAIT);
297 return timepoint.wait ? Z_FOREVER : Z_TIMEOUT_NO_WAIT;
302 if (a.wait == b.wait) {
305 return b.wait ? -1 : 1;
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:318
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:270
__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:225
uint64_t tick
Definition sys_clock.h:225