7#ifndef ZEPHYR_INCLUDE_TIME_UNITS_H_
8#define ZEPHYR_INCLUDE_TIME_UNITS_H_
33#define SYS_FOREVER_MS (-1)
39#define SYS_FOREVER_US (-1)
43#define SYS_TIMEOUT_MS(ms) Z_TIMEOUT_TICKS((ms) == SYS_FOREVER_MS ? \
44 K_TICKS_FOREVER : Z_TIMEOUT_MS_TICKS(ms))
48#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
49__syscall
int sys_clock_hw_cycles_per_sec_runtime_get(
void);
51static inline int z_impl_sys_clock_hw_cycles_per_sec_runtime_get(
void)
53 extern int z_clock_hw_cycles_per_sec;
55 return z_clock_hw_cycles_per_sec;
59#if defined(__cplusplus) && __cplusplus >= 201402L
60 #if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
61 #define TIME_CONSTEXPR
63 #define TIME_CONSTEXPR constexpr
66 #define TIME_CONSTEXPR
73#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
74#define sys_clock_hw_cycles_per_sec() sys_clock_hw_cycles_per_sec_runtime_get()
76#define sys_clock_hw_cycles_per_sec() CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC
90#define z_tmcvt_use_fast_algo(from_hz, to_hz) \
91 ((DIV_ROUND_UP(CONFIG_SYS_CLOCK_MAX_TIMEOUT_DAYS * 24ULL * 3600ULL * from_hz, \
92 UINT32_MAX) * to_hz) <= UINT32_MAX)
118#define z_tmcvt_is_identity(__from_hz, __to_hz) \
119 ((__to_hz) == (__from_hz))
122#define z_tmcvt_is_int_mul(__from_hz, __to_hz) \
123 ((__to_hz) > (__from_hz) && (__to_hz) % (__from_hz) == 0U)
126#define z_tmcvt_is_int_div(__from_hz, __to_hz) \
127 ((__from_hz) > (__to_hz) && (__from_hz) % (__to_hz) == 0U)
133#define z_tmcvt_off_div(__from_hz, __to_hz, __round_up, __round_off) \
134 ((__round_off) ? ((__from_hz) / (__to_hz)) / 2 : \
135 (__round_up) ? ((__from_hz) / (__to_hz)) - 1 : \
143#define z_tmcvt_divisor(a, b) ((a) / (b) ?: 1)
145#define z_tmcvt_divisor(a, b) ((a) / (b))
152#define z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off) \
153 ((__round_off) ? (__from_hz) / 2 : \
154 (__round_up) ? (__from_hz) - 1 : \
158#define z_tmcvt_int_div_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
159 ((uint64_t) (__t) <= 0xffffffffU - \
160 z_tmcvt_off_div(__from_hz, __to_hz, __round_up, __round_off) ? \
161 ((uint32_t)((__t) + \
162 z_tmcvt_off_div(__from_hz, __to_hz, \
163 __round_up, __round_off)) / \
164 z_tmcvt_divisor(__from_hz, __to_hz)) \
166 (uint32_t) (((uint64_t) (__t) + \
167 z_tmcvt_off_div(__from_hz, __to_hz, \
168 __round_up, __round_off)) / \
169 z_tmcvt_divisor(__from_hz, __to_hz)) \
173#define z_tmcvt_int_mul_32(__t, __from_hz, __to_hz) \
174 (uint32_t) (__t)*((__to_hz) / (__from_hz))
177#define z_tmcvt_gen_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
178 ((uint32_t) (((uint64_t) (__t)*(__to_hz) + \
179 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off)) / (__from_hz)))
182#define z_tmcvt_int_div_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
183 (((uint64_t) (__t) + z_tmcvt_off_div(__from_hz, __to_hz, \
184 __round_up, __round_off)) / \
185 z_tmcvt_divisor(__from_hz, __to_hz))
188#define z_tmcvt_int_mul_64(__t, __from_hz, __to_hz) \
189 (uint64_t) (__t)*((__to_hz) / (__from_hz))
192#define z_tmcvt_gen_64_fast(__t, __from_hz, __to_hz, __round_up, __round_off) \
193 (((uint64_t) (__t)*(__to_hz) + \
194 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off)) / (__from_hz))
197#define z_tmcvt_gen_64_slow(__t, __from_hz, __to_hz, __round_up, __round_off) \
198 (((uint64_t) (__t) / (__from_hz))*(__to_hz) + \
199 (((uint64_t) (__t) % (__from_hz))*(__to_hz) + \
200 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off)) / (__from_hz))
203#define z_tmcvt_gen_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
204 (z_tmcvt_use_fast_algo(__from_hz, __to_hz) ? \
205 z_tmcvt_gen_64_fast(__t, __from_hz, __to_hz, __round_up, __round_off) : \
206 z_tmcvt_gen_64_slow(__t, __from_hz, __to_hz, __round_up, __round_off))
209#define z_tmcvt_32(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
212 z_tmcvt_is_identity(__from_hz, __to_hz) ? \
215 z_tmcvt_is_int_div(__from_hz, __to_hz) ? \
216 z_tmcvt_int_div_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
218 z_tmcvt_is_int_mul(__from_hz, __to_hz) ? \
219 z_tmcvt_int_mul_32(__t, __from_hz, __to_hz) \
221 z_tmcvt_gen_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
224 z_tmcvt_gen_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
228#define z_tmcvt_64(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
231 z_tmcvt_is_identity(__from_hz, __to_hz) ? \
234 z_tmcvt_is_int_div(__from_hz, __to_hz) ? \
235 z_tmcvt_int_div_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
237 z_tmcvt_is_int_mul(__from_hz, __to_hz) ? \
238 z_tmcvt_int_mul_64(__t, __from_hz, __to_hz) \
240 z_tmcvt_gen_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
243 z_tmcvt_gen_64_slow(__t, __from_hz, __to_hz, __round_up, __round_off) \
246#define z_tmcvt(__t, __from_hz, __to_hz, __const_hz, __result32, __round_up, __round_off) \
248 z_tmcvt_32(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) : \
249 z_tmcvt_64(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off))
326#define Z_HZ_us 1000000
327#define Z_HZ_ns 1000000000
328#define Z_HZ_cyc sys_clock_hw_cycles_per_sec()
329#define Z_HZ_ticks CONFIG_SYS_CLOCK_TICKS_PER_SEC
330#define Z_CCYC (!IS_ENABLED(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME))
343#define k_ms_to_cyc_floor32(t) \
344 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, false)
358#define k_ms_to_cyc_floor64(t) \
359 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, false)
373#define k_ms_to_cyc_near32(t) \
374 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, true)
388#define k_ms_to_cyc_near64(t) \
389 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, true)
403#define k_ms_to_cyc_ceil32(t) \
404 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, true, false)
418#define k_ms_to_cyc_ceil64(t) \
419 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, true, false)
433#define k_ms_to_ticks_floor32(t) \
434 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, false, false)
448#define k_ms_to_ticks_floor64(t) \
449 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, false, false)
463#define k_ms_to_ticks_near32(t) \
464 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, false, true)
478#define k_ms_to_ticks_near64(t) \
479 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, false, true)
493#define k_ms_to_ticks_ceil32(t) \
494 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, true, false)
508#define k_ms_to_ticks_ceil64(t) \
509 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, true, false)
523#define k_us_to_cyc_floor32(t) \
524 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, false)
538#define k_us_to_cyc_floor64(t) \
539 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, false)
553#define k_us_to_cyc_near32(t) \
554 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, true)
568#define k_us_to_cyc_near64(t) \
569 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, true)
583#define k_us_to_cyc_ceil32(t) \
584 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, true, false)
598#define k_us_to_cyc_ceil64(t) \
599 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, true, false)
613#define k_us_to_ticks_floor32(t) \
614 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, false, false)
628#define k_us_to_ticks_floor64(t) \
629 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, false, false)
643#define k_us_to_ticks_near32(t) \
644 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, false, true)
658#define k_us_to_ticks_near64(t) \
659 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, false, true)
673#define k_us_to_ticks_ceil32(t) \
674 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, true, false)
688#define k_us_to_ticks_ceil64(t) \
689 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, true, false)
703#define k_ns_to_cyc_floor32(t) \
704 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, false)
718#define k_ns_to_cyc_floor64(t) \
719 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, false)
733#define k_ns_to_cyc_near32(t) \
734 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, true)
748#define k_ns_to_cyc_near64(t) \
749 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, true)
763#define k_ns_to_cyc_ceil32(t) \
764 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, true, false)
778#define k_ns_to_cyc_ceil64(t) \
779 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, true, false)
793#define k_ns_to_ticks_floor32(t) \
794 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, false, false)
808#define k_ns_to_ticks_floor64(t) \
809 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, false, false)
823#define k_ns_to_ticks_near32(t) \
824 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, false, true)
838#define k_ns_to_ticks_near64(t) \
839 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, false, true)
853#define k_ns_to_ticks_ceil32(t) \
854 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, true, false)
868#define k_ns_to_ticks_ceil64(t) \
869 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, true, false)
883#define k_cyc_to_ms_floor32(t) \
884 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, false)
898#define k_cyc_to_ms_floor64(t) \
899 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, false)
913#define k_cyc_to_ms_near32(t) \
914 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, true)
928#define k_cyc_to_ms_near64(t) \
929 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, true)
943#define k_cyc_to_ms_ceil32(t) \
944 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, true, false)
958#define k_cyc_to_ms_ceil64(t) \
959 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, true, false)
973#define k_cyc_to_us_floor32(t) \
974 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, false)
988#define k_cyc_to_us_floor64(t) \
989 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, false)
1003#define k_cyc_to_us_near32(t) \
1004 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, true)
1018#define k_cyc_to_us_near64(t) \
1019 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, true)
1033#define k_cyc_to_us_ceil32(t) \
1034 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, true, false)
1048#define k_cyc_to_us_ceil64(t) \
1049 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, true, false)
1063#define k_cyc_to_ns_floor32(t) \
1064 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, false)
1078#define k_cyc_to_ns_floor64(t) \
1079 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, false)
1093#define k_cyc_to_ns_near32(t) \
1094 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, true)
1108#define k_cyc_to_ns_near64(t) \
1109 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, true)
1123#define k_cyc_to_ns_ceil32(t) \
1124 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, true, false)
1138#define k_cyc_to_ns_ceil64(t) \
1139 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, true, false)
1153#define k_cyc_to_ticks_floor32(t) \
1154 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, false)
1168#define k_cyc_to_ticks_floor64(t) \
1169 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, false)
1183#define k_cyc_to_ticks_near32(t) \
1184 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, true)
1198#define k_cyc_to_ticks_near64(t) \
1199 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, true)
1213#define k_cyc_to_ticks_ceil32(t) \
1214 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, true, false)
1228#define k_cyc_to_ticks_ceil64(t) \
1229 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, true, false)
1243#define k_ticks_to_ms_floor32(t) \
1244 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, false, false)
1258#define k_ticks_to_ms_floor64(t) \
1259 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, false, false)
1273#define k_ticks_to_ms_near32(t) \
1274 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, false, true)
1288#define k_ticks_to_ms_near64(t) \
1289 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, false, true)
1303#define k_ticks_to_ms_ceil32(t) \
1304 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, true, false)
1318#define k_ticks_to_ms_ceil64(t) \
1319 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, true, false)
1333#define k_ticks_to_us_floor32(t) \
1334 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, false, false)
1348#define k_ticks_to_us_floor64(t) \
1349 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, false, false)
1363#define k_ticks_to_us_near32(t) \
1364 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, false, true)
1378#define k_ticks_to_us_near64(t) \
1379 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, false, true)
1393#define k_ticks_to_us_ceil32(t) \
1394 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, true, false)
1408#define k_ticks_to_us_ceil64(t) \
1409 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, true, false)
1423#define k_ticks_to_ns_floor32(t) \
1424 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, false, false)
1438#define k_ticks_to_ns_floor64(t) \
1439 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, false, false)
1453#define k_ticks_to_ns_near32(t) \
1454 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, false, true)
1468#define k_ticks_to_ns_near64(t) \
1469 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, false, true)
1483#define k_ticks_to_ns_ceil32(t) \
1484 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, true, false)
1498#define k_ticks_to_ns_ceil64(t) \
1499 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, true, false)
1513#define k_ticks_to_cyc_floor32(t) \
1514 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, false)
1528#define k_ticks_to_cyc_floor64(t) \
1529 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, false)
1543#define k_ticks_to_cyc_near32(t) \
1544 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, true)
1558#define k_ticks_to_cyc_near64(t) \
1559 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, true)
1573#define k_ticks_to_cyc_ceil32(t) \
1574 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, true, false)
1588#define k_ticks_to_cyc_ceil64(t) \
1589 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, true, false)
1591#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
1592#include <syscalls/time_units.h>
1595#undef TIME_CONSTEXPR