Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
time_units.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_TIME_UNITS_H_
8#define ZEPHYR_INCLUDE_SYS_TIME_UNITS_H_
9
10#include <zephyr/toolchain.h>
11#include <zephyr/sys/div64.h>
12#include <zephyr/sys/util.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
26
34#define SYS_FOREVER_MS (-1)
35
40#define SYS_FOREVER_US (-1)
41
44#define SYS_TIMEOUT_MS(ms) \
45 Z_TIMEOUT_TICKS((ms) == SYS_FOREVER_MS ? \
46 K_TICKS_FOREVER : Z_TIMEOUT_MS_TICKS(ms))
47
48/* Exhaustively enumerated, highly optimized time unit conversion API */
49
50#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
51 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
59__syscall unsigned int sys_clock_hw_cycles_per_sec_runtime_get(void);
60
62
63/* z_sys_clock_cyc_recip holds the reciprocal of the system timer frequency,
64 * with the frequency in .div. z_sys_clock_hw_cycles_per_sec_set() updates
65 * it under a spinlock. Readers of more than .div take the same lock.
66 */
67extern struct sys_div64_recip z_sys_clock_cyc_recip;
68
69static inline unsigned int z_sys_clock_hw_cycles_per_sec_get(void)
70{
71 return z_sys_clock_cyc_recip.div;
72}
73
74/* The reciprocal of the system timer frequency, coherent with itself,
75 * copied to buf. For time conversions on 32-bit targets.
76 */
77__syscall const struct sys_div64_recip *z_sys_clock_cyc_recip_get(struct sys_div64_recip *buf);
78
79#if defined(Z_DIV64_PLAIN_RECIP)
80/* A plain reciprocal is the frequency alone, read without the lock. */
81static inline const struct sys_div64_recip *
82z_impl_z_sys_clock_cyc_recip_get(struct sys_div64_recip *buf)
83{
84 buf->div = z_sys_clock_hw_cycles_per_sec_get();
85 return buf;
86}
87#endif
88
89/* The system timer frequency, read only. z_sys_clock_hw_cycles_per_sec_set()
90 * changes it.
91 */
92#define z_clock_hw_cycles_per_sec z_sys_clock_hw_cycles_per_sec_get()
93
95
96static inline unsigned int z_impl_sys_clock_hw_cycles_per_sec_runtime_get(void)
97{
98 return z_clock_hw_cycles_per_sec;
99}
100#endif /* CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME */
101
102#if defined(__cplusplus) && (__cplusplus >= 201402L)
103 #if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
104 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
105 #define TIME_CONSTEXPR
106 #else
107 #define TIME_CONSTEXPR constexpr
108 #endif
109#else
110 #define TIME_CONSTEXPR
111#endif
112
117#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
118 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
119#define sys_clock_hw_cycles_per_sec() sys_clock_hw_cycles_per_sec_runtime_get()
120#else
121#define sys_clock_hw_cycles_per_sec() (uint32_t)CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC
122#endif
123
135#define z_tmcvt_use_fast_algo(from_hz, to_hz) \
136 ((DIV_ROUND_UP(CONFIG_SYS_CLOCK_MAX_TIMEOUT_DAYS * 24ULL * 3600ULL * from_hz, \
137 UINT32_MAX) * to_hz) <= UINT32_MAX)
138
139/* Time converter generator gadget. Selects from one of three
140 * conversion algorithms: ones that take advantage when the
141 * frequencies are an integer ratio (in either direction), or a full
142 * precision conversion. Clever use of extra arguments causes all the
143 * selection logic to be optimized out, and the generated code even
144 * reduces to 32 bit only if a ratio conversion is available and the
145 * result is 32 bits.
146 *
147 * This isn't intended to be used directly, instead being wrapped
148 * appropriately in a user-facing API. The other arguments are:
149 *
150 * const_hz - true if the hz arguments are known to be compile-time
151 * constants (because otherwise the modulus test would
152 * have to be done at runtime), Z_TMCVT_RT_CYC if one of
153 * them is the cycle rate set at run time, else false
154 * result32 - The result will be truncated to 32 bits on use
155 * round_up - Return the ceiling of the resulting fraction
156 * round_off - Return the nearest value to the resulting fraction
157 * (pass both round_up/off as false to get "round_down")
158 *
159 * All of this must be implemented as expressions so that, when constant,
160 * the results may be used to initialize global variables.
161 */
162
163/* true if the conversion is the identity */
164#define z_tmcvt_is_identity(__from_hz, __to_hz) \
165 ((__to_hz) == (__from_hz))
166
167/* true if the conversion requires a simple integer multiply */
168#define z_tmcvt_is_int_mul(__from_hz, __to_hz) \
169 ((__to_hz) > (__from_hz) && (__to_hz) % (__from_hz) == 0U)
170
171/* true if the conversion requires a simple integer division */
172#define z_tmcvt_is_int_div(__from_hz, __to_hz) \
173 ((__from_hz) > (__to_hz) && (__from_hz) % (__to_hz) == 0U)
174
175/*
176 * Compute the offset needed to round the result correctly when
177 * the conversion requires a simple integer division
178 */
179#define z_tmcvt_off_div(__from_hz, __to_hz, __round_up, __round_off) \
180 ((__round_off) ? ((__from_hz) / (__to_hz)) / 2 : \
181 (__round_up) ? ((__from_hz) / (__to_hz)) - 1 : \
182 0)
183
184/*
185 * All users of this macro MUST ensure its output is never used when a/b
186 * is zero because it incorrectly but by design never returns zero.
187 *
188 * Some compiler versions emit a divide-by-zero warning for this code:
189 * "false ? 42/0 : 43". Dealing with (generated) dead code is hard:
190 * https://github.com/zephyrproject-rtos/zephyr/issues/63564
191 * https://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html
192 *
193 * To silence such divide-by-zero warnings, "cheat" and never return
194 * zero. Return 1 instead. Use octal "01u" as a breadcrumb to ease a
195 * little bit the huge pain of "reverse-engineering" pre-processor
196 * output.
197 *
198 * The "Elvis" operator "a/b ?: 1" is tempting because it avoids
199 * evaluating the same expression twice. However: 1. it's a non-standard
200 * GNU extension; 2. everything in this file is designed to be computed
201 * at compile time anyway.
202 */
203#define z_tmcvt_divisor(a, b) ((a)/(b) ? (a)/(b) : 1U)
204
205/*
206 * 64-bit n / d and n % d. A conversion picks one form for all its
207 * divisions, so it writes its argument once per form, not once per
208 * division: plain C for a constant argument, as a static initializer
209 * needs, and the div64 helpers otherwise.
210 */
211#define z_tmcvt_c_div(__n, __d) ((uint64_t)(__n) / (__d))
212#define z_tmcvt_c_mod(__n, __d) ((uint64_t)(__n) % (__d))
213#define z_tmcvt_r_div(__n, __d) \
214 z_div64_u32_const((__n), (__d), IS_ENABLED(CONFIG_SPEED_OPTIMIZATIONS))
215#define z_tmcvt_r_mod(__n, __d) ((uint64_t)z_mod64_u32_const((__n), (__d)))
216
217/*
218 * Compute the offset needed to round the result correctly when
219 * the conversion requires a full mul/div
220 */
221#define z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off) \
222 ((__round_off) ? (__from_hz) / 2 : \
223 (__round_up) ? (__from_hz) - 1 : \
224 0)
225
226/* Integer division 32-bit conversion */
227#define z_tmcvt_int_div_32(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
228 ((uint64_t) (__t) <= 0xffffffffU - \
229 z_tmcvt_off_div(__from_hz, __to_hz, __round_up, __round_off) ? \
230 ((uint32_t)((__t) + \
231 z_tmcvt_off_div(__from_hz, __to_hz, \
232 __round_up, __round_off)) / \
233 (uint32_t)z_tmcvt_divisor(__from_hz, __to_hz)) \
234 : \
235 (uint32_t) __dv##_div((uint64_t) (__t) + \
236 z_tmcvt_off_div(__from_hz, __to_hz, \
237 __round_up, __round_off), \
238 z_tmcvt_divisor(__from_hz, __to_hz)) \
239 )
240
241/* Integer multiplication 32-bit conversion */
242#define z_tmcvt_int_mul_32(__t, __from_hz, __to_hz) \
243 ((uint32_t) ((__t)*((__to_hz) / (__from_hz))))
244
245/* General 32-bit conversion */
246#define z_tmcvt_gen_32(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
247 ((uint32_t) __dv##_div((uint64_t) (__t)*(__to_hz) + \
248 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off), __from_hz))
249
250/* Integer division 64-bit conversion */
251#define z_tmcvt_int_div_64(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
252 __dv##_div((uint64_t) (__t) + z_tmcvt_off_div(__from_hz, __to_hz, \
253 __round_up, __round_off), \
254 z_tmcvt_divisor(__from_hz, __to_hz))
255
256/* Integer multiplication 64-bit conversion */
257#define z_tmcvt_int_mul_64(__t, __from_hz, __to_hz) \
258 (uint64_t) (__t)*((__to_hz) / (__from_hz))
259
260/* Fast 64-bit conversion. This relies on the multiply not overflowing */
261#define z_tmcvt_gen_64_fast(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
262 __dv##_div((uint64_t) (__t)*(__to_hz) + \
263 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off), __from_hz)
264
265/* Slow 64-bit conversion. This avoids overflowing the multiply */
266#define z_tmcvt_gen_64_slow(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
267 ((__dv##_div(__t, __from_hz)*(__to_hz)) + \
268 __dv##_div((__dv##_mod(__t, __from_hz)*(__to_hz)) + \
269 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off), __from_hz))
270
271/* General 64-bit conversion. Uses one of the two above macros */
272#define z_tmcvt_gen_64(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
273 (z_tmcvt_use_fast_algo(__from_hz, __to_hz) ? \
274 z_tmcvt_gen_64_fast(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) : \
275 z_tmcvt_gen_64_slow(__dv, __t, __from_hz, __to_hz, __round_up, __round_off))
276
277/* const_hz for a conversion to or from the cycle rate set at run time */
278#define Z_TMCVT_RT_CYC 2
279
280/* The cast keeps g++ from warning that a bool is compared with 2 */
281#define z_tmcvt_const_hz_is(__const_hz, __val) ((int)(__const_hz) == (__val))
282
283#if !defined(CONFIG_64BIT) && \
284 (defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
285 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE))
286/* z_tmcvt_cyc() modes */
287#define Z_TMCVT_CYC_UP 1U
288#define Z_TMCVT_CYC_NEAR 2U
289#define Z_TMCVT_CYC_32 4U
290
291/* (t * to_hz + rounding) / the cycle rate, through its reciprocal. A 64-bit
292 * result takes two steps, as z_tmcvt_gen_64_slow(), with one copy of the
293 * reciprocal for both divisions.
294 */
295static ALWAYS_INLINE uint64_t z_tmcvt_cyc_inline(uint64_t t, uint32_t to_hz, uint32_t mode)
296{
297 struct sys_div64_recip buf;
298 const struct sys_div64_recip *r = z_sys_clock_cyc_recip_get(&buf);
299 uint32_t from_hz = r->div;
300 uint32_t off = ((mode & Z_TMCVT_CYC_NEAR) != 0U) ? (from_hz / 2U) :
301 (((mode & Z_TMCVT_CYC_UP) != 0U) ? (from_hz - 1U) : 0U);
302 uint64_t q;
303 uint32_t rem;
304
305 if ((mode & Z_TMCVT_CYC_32) != 0U) {
306 return (uint32_t)sys_div64_recip_div((t * to_hz) + off, r);
307 }
308
309 q = sys_div64_recip_div(t, r);
310 rem = (uint32_t)t - ((uint32_t)q * from_hz);
311
312 return (q * to_hz) + sys_div64_recip_div(((uint64_t)rem * to_hz) + off, r);
313}
314
315uint64_t z_tmcvt_cyc_out(uint64_t t, uint32_t to_hz, uint32_t mode);
316
317static ALWAYS_INLINE uint64_t z_tmcvt_cyc(uint64_t t, uint32_t to_hz, bool round_up,
318 bool round_off, bool result32)
319{
320 uint32_t mode = (round_off ? Z_TMCVT_CYC_NEAR : (round_up ? Z_TMCVT_CYC_UP : 0U)) |
321 (result32 ? Z_TMCVT_CYC_32 : 0U);
322
323 if (IS_ENABLED(CONFIG_SPEED_OPTIMIZATIONS)) {
324 return z_tmcvt_cyc_inline(t, to_hz, mode);
325 }
326
327 return z_tmcvt_cyc_out(t, to_hz, mode);
328}
329
330/* With Z_TMCVT_RT_CYC, a source rate that is not constant is the cycle rate */
331#define z_tmcvt_rt_32(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
332 ((z_tmcvt_const_hz_is(__const_hz, Z_TMCVT_RT_CYC) && \
333 !Z_DIV64_IF_CONST(__from_hz, true, false)) \
334 ? (uint32_t)z_tmcvt_cyc(__t, __to_hz, __round_up, __round_off, true) \
335 : z_tmcvt_gen_32(__dv, __t, __from_hz, __to_hz, __round_up, __round_off))
336#define z_tmcvt_rt_64(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
337 ((z_tmcvt_const_hz_is(__const_hz, Z_TMCVT_RT_CYC) && \
338 !Z_DIV64_IF_CONST(__from_hz, true, false)) \
339 ? z_tmcvt_cyc(__t, __to_hz, __round_up, __round_off, false) \
340 : z_tmcvt_gen_64_slow(__dv, __t, __from_hz, __to_hz, __round_up, __round_off))
341#else
342#define z_tmcvt_rt_32(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
343 z_tmcvt_gen_32(__dv, __t, __from_hz, __to_hz, __round_up, __round_off)
344#define z_tmcvt_rt_64(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
345 z_tmcvt_gen_64_slow(__dv, __t, __from_hz, __to_hz, __round_up, __round_off)
346#endif
347
348/* Convert, generating a 32-bit result */
349#define z_tmcvt_32_dv(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
350 (z_tmcvt_const_hz_is(__const_hz, 1) ? \
351 ( \
352 z_tmcvt_is_identity(__from_hz, __to_hz) ? \
353 (uint32_t) (__t) \
354 : \
355 z_tmcvt_is_int_div(__from_hz, __to_hz) ? \
356 z_tmcvt_int_div_32(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
357 : \
358 z_tmcvt_is_int_mul(__from_hz, __to_hz) ? \
359 z_tmcvt_int_mul_32(__t, __from_hz, __to_hz) \
360 : \
361 z_tmcvt_gen_32(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
362 ) \
363 : \
364 z_tmcvt_rt_32(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
365 )
366
367/* Convert, generating a 64-bit result */
368#define z_tmcvt_64_dv(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
369 (z_tmcvt_const_hz_is(__const_hz, 1) ? \
370 ( \
371 z_tmcvt_is_identity(__from_hz, __to_hz) ? \
372 (uint64_t) (__t) \
373 : \
374 z_tmcvt_is_int_div(__from_hz, __to_hz) ? \
375 z_tmcvt_int_div_64(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
376 : \
377 z_tmcvt_is_int_mul(__from_hz, __to_hz) ? \
378 z_tmcvt_int_mul_64(__t, __from_hz, __to_hz) \
379 : \
380 z_tmcvt_gen_64(__dv, __t, __from_hz, __to_hz, __round_up, __round_off) \
381 ) \
382 : \
383 z_tmcvt_rt_64(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
384 )
385
386#define z_tmcvt_dv(__dv, __t, __from_hz, __to_hz, __const_hz, __result32, __round_up, __round_off) \
387 ((__result32) ? \
388 z_tmcvt_32_dv(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) : \
389 z_tmcvt_64_dv(__dv, __t, __from_hz, __to_hz, __const_hz, __round_up, __round_off))
390
391#define z_tmcvt_32(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
392 Z_DIV64_IF_CONST(__t, \
393 z_tmcvt_32_dv(z_tmcvt_c, __t, __from_hz, __to_hz, __const_hz, \
394 __round_up, __round_off), \
395 z_tmcvt_32_dv(z_tmcvt_r, __t, __from_hz, __to_hz, __const_hz, \
396 __round_up, __round_off))
397
398#define z_tmcvt_64(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
399 Z_DIV64_IF_CONST(__t, \
400 z_tmcvt_64_dv(z_tmcvt_c, __t, __from_hz, __to_hz, __const_hz, \
401 __round_up, __round_off), \
402 z_tmcvt_64_dv(z_tmcvt_r, __t, __from_hz, __to_hz, __const_hz, \
403 __round_up, __round_off))
404
405#define z_tmcvt(__t, __from_hz, __to_hz, __const_hz, __result32, __round_up, __round_off) \
406 Z_DIV64_IF_CONST(__t, \
407 z_tmcvt_dv(z_tmcvt_c, __t, __from_hz, __to_hz, __const_hz, __result32, \
408 __round_up, __round_off), \
409 z_tmcvt_dv(z_tmcvt_r, __t, __from_hz, __to_hz, __const_hz, __result32, \
410 __round_up, __round_off))
411
412/* The following code is programmatically generated using this perl
413 * code, which enumerates all possible combinations of units, rounding
414 * modes and precision. Do not edit directly.
415 *
416 * Note that nano/microsecond conversions are only defined with 64 bit
417 * precision. These units conversions were not available in 32 bit
418 * variants historically, and doing 32 bit math with units that small
419 * has precision traps that we probably don't want to support in an
420 * official API.
421 *
422 * #!/usr/bin/perl -w
423 * use strict;
424 *
425 * my %human = ("sec" => "seconds",
426 * "ms" => "milliseconds",
427 * "us" => "microseconds",
428 * "ns" => "nanoseconds",
429 * "cyc" => "hardware cycles",
430 * "ticks" => "ticks");
431 * my %human_round = ("ceil" => "Rounds up",
432 * "near" => "Round nearest",
433 * "floor" => "Truncates");
434 *
435 * sub big { return $_[0] eq "us" || $_[0] eq "ns"; }
436 * sub prefix { return $_[0] eq "sec" || $_[0] eq "ms" || $_[0] eq "us" || $_[0] eq "ns"; }
437 *
438 * for my $from_unit ("sec", "ms", "us", "ns", "cyc", "ticks") {
439 * for my $to_unit ("sec", "ms", "us", "ns", "cyc", "ticks") {
440 * next if $from_unit eq $to_unit;
441 * next if prefix($from_unit) && prefix($to_unit);
442 * for my $round ("floor", "near", "ceil") {
443 * for(my $big=0; $big <= 1; $big++) {
444 * my $sz = $big ? 64 : 32;
445 * my $sym = "k_${from_unit}_to_${to_unit}_$round$sz";
446 * my $type = "uint${sz}_t";
447 * my $const_hz = ($from_unit eq "cyc" || $to_unit eq "cyc")
448 * ? "Z_CCYC" : "true";
449 * my $ret32 = $big ? "64" : "32";
450 * my $rup = $round eq "ceil" ? "true" : "false";
451 * my $roff = $round eq "near" ? "true" : "false";
452 *
453 * my $hfrom = $human{$from_unit};
454 * my $hto = $human{$to_unit};
455 * my $hround = $human_round{$round};
456 * print "/", "** \@brief Convert $hfrom to $hto. $ret32 bits. $hround.\n";
457 * print " *\n";
458 * print " * Converts time values in $hfrom to $hto.\n";
459 * print " * Computes result in $sz bit precision.\n";
460 * if ($round eq "ceil") {
461 * print " * Rounds up to the next highest output unit.\n";
462 * } elsif ($round eq "near") {
463 * print " * Rounds to the nearest output unit.\n";
464 * } else {
465 * print " * Truncates to the next lowest output unit.\n";
466 * }
467 * print " *\n";
468 * print " * \@warning Generated. Do not edit. See above.\n";
469 * print " *\n";
470 * print " * \@param t Source time in $hfrom. uint64_t\n";
471 * print " *\n";
472 * print " * \@return The converted time value in $hto. $type\n";
473 * print " *", "/\n";
474 * print "#define $sym(t) \\\n";
475 * print "\tz_tmcvt_$ret32(t, Z_HZ_$from_unit, Z_HZ_$to_unit,";
476 * print " $const_hz, $rup, $roff)\n";
477 * print "\n\n";
478 * }
479 * }
480 * }
481 * }
482 */
483
484/* Some more concise declarations to simplify the generator script and
485 * save bytes below
486 */
487#define Z_HZ_sec 1
488#define Z_HZ_ms 1000
489#define Z_HZ_us 1000000
490#define Z_HZ_ns 1000000000
491#define Z_HZ_cyc sys_clock_hw_cycles_per_sec()
492#define Z_HZ_ticks CONFIG_SYS_CLOCK_TICKS_PER_SEC
493#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
494 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
495#define Z_CCYC Z_TMCVT_RT_CYC
496#else
497#define Z_CCYC true
498#endif
499
512#define k_sec_to_cyc_floor32(t) \
513 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, false)
514
515
528#define k_sec_to_cyc_floor64(t) \
529 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, false)
530
531
544#define k_sec_to_cyc_near32(t) \
545 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, true)
546
547
560#define k_sec_to_cyc_near64(t) \
561 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, true)
562
563
576#define k_sec_to_cyc_ceil32(t) \
577 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, true, false)
578
579
592#define k_sec_to_cyc_ceil64(t) \
593 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, true, false)
594
595
608#define k_sec_to_ticks_floor32(t) \
609 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_ticks, true, false, false)
610
611
624#define k_sec_to_ticks_floor64(t) \
625 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_ticks, true, false, false)
626
627
640#define k_sec_to_ticks_near32(t) \
641 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_ticks, true, false, true)
642
643
656#define k_sec_to_ticks_near64(t) \
657 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_ticks, true, false, true)
658
659
672#define k_sec_to_ticks_ceil32(t) \
673 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_ticks, true, true, false)
674
675
688#define k_sec_to_ticks_ceil64(t) \
689 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_ticks, true, true, false)
690
691
704#define k_ms_to_cyc_floor32(t) \
705 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, false)
706
707
720#define k_ms_to_cyc_floor64(t) \
721 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, false)
722
723
736#define k_ms_to_cyc_near32(t) \
737 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, true)
738
739
752#define k_ms_to_cyc_near64(t) \
753 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, true)
754
755
768#define k_ms_to_cyc_ceil32(t) \
769 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, true, false)
770
771
784#define k_ms_to_cyc_ceil64(t) \
785 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, true, false)
786
787
800#define k_ms_to_ticks_floor32(t) \
801 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, false, false)
802
803
816#define k_ms_to_ticks_floor64(t) \
817 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, false, false)
818
819
832#define k_ms_to_ticks_near32(t) \
833 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, false, true)
834
835
848#define k_ms_to_ticks_near64(t) \
849 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, false, true)
850
851
864#define k_ms_to_ticks_ceil32(t) \
865 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, true, false)
866
867
880#define k_ms_to_ticks_ceil64(t) \
881 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, true, false)
882
883
896#define k_us_to_cyc_floor32(t) \
897 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, false)
898
899
912#define k_us_to_cyc_floor64(t) \
913 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, false)
914
915
928#define k_us_to_cyc_near32(t) \
929 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, true)
930
931
944#define k_us_to_cyc_near64(t) \
945 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, true)
946
947
960#define k_us_to_cyc_ceil32(t) \
961 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, true, false)
962
963
976#define k_us_to_cyc_ceil64(t) \
977 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, true, false)
978
979
992#define k_us_to_ticks_floor32(t) \
993 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, false, false)
994
995
1008#define k_us_to_ticks_floor64(t) \
1009 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, false, false)
1010
1011
1024#define k_us_to_ticks_near32(t) \
1025 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, false, true)
1026
1027
1040#define k_us_to_ticks_near64(t) \
1041 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, false, true)
1042
1043
1056#define k_us_to_ticks_ceil32(t) \
1057 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, true, false)
1058
1059
1072#define k_us_to_ticks_ceil64(t) \
1073 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, true, false)
1074
1075
1088#define k_ns_to_cyc_floor32(t) \
1089 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, false)
1090
1091
1104#define k_ns_to_cyc_floor64(t) \
1105 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, false)
1106
1107
1120#define k_ns_to_cyc_near32(t) \
1121 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, true)
1122
1123
1136#define k_ns_to_cyc_near64(t) \
1137 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, true)
1138
1139
1152#define k_ns_to_cyc_ceil32(t) \
1153 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, true, false)
1154
1155
1168#define k_ns_to_cyc_ceil64(t) \
1169 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, true, false)
1170
1171
1183#define ns_to_cyc_at_hz_floor32(t, freq) \
1184 z_tmcvt_32(t, Z_HZ_ns, freq, false, false, false)
1185
1186
1198#define ns_to_cyc_at_hz_floor64(t, freq) \
1199 z_tmcvt_64(t, Z_HZ_ns, freq, false, false, false)
1200
1201
1213#define ns_to_cyc_at_hz_near32(t, freq) \
1214 z_tmcvt_32(t, Z_HZ_ns, freq, false, false, true)
1215
1216
1228#define ns_to_cyc_at_hz_near64(t, freq) \
1229 z_tmcvt_64(t, Z_HZ_ns, freq, false, false, true)
1230
1231
1243#define ns_to_cyc_at_hz_ceil32(t, freq) \
1244 z_tmcvt_32(t, Z_HZ_ns, freq, false, true, false)
1245
1246
1258#define ns_to_cyc_at_hz_ceil64(t, freq) \
1259 z_tmcvt_64(t, Z_HZ_ns, freq, false, true, false)
1260
1261
1274#define k_ns_to_ticks_floor32(t) \
1275 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, false, false)
1276
1277
1290#define k_ns_to_ticks_floor64(t) \
1291 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, false, false)
1292
1293
1306#define k_ns_to_ticks_near32(t) \
1307 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, false, true)
1308
1309
1322#define k_ns_to_ticks_near64(t) \
1323 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, false, true)
1324
1325
1338#define k_ns_to_ticks_ceil32(t) \
1339 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, true, false)
1340
1341
1354#define k_ns_to_ticks_ceil64(t) \
1355 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, true, false)
1356
1357
1370#define k_cyc_to_sec_floor32(t) \
1371 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, false)
1372
1373
1386#define k_cyc_to_sec_floor64(t) \
1387 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, false)
1388
1389
1402#define k_cyc_to_sec_near32(t) \
1403 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, true)
1404
1405
1418#define k_cyc_to_sec_near64(t) \
1419 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, true)
1420
1421
1434#define k_cyc_to_sec_ceil32(t) \
1435 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, true, false)
1436
1437
1450#define k_cyc_to_sec_ceil64(t) \
1451 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, true, false)
1452
1453
1466#define k_cyc_to_ms_floor32(t) \
1467 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, false)
1468
1469
1482#define k_cyc_to_ms_floor64(t) \
1483 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, false)
1484
1485
1498#define k_cyc_to_ms_near32(t) \
1499 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, true)
1500
1501
1514#define k_cyc_to_ms_near64(t) \
1515 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, true)
1516
1517
1530#define k_cyc_to_ms_ceil32(t) \
1531 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, true, false)
1532
1533
1546#define k_cyc_to_ms_ceil64(t) \
1547 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, true, false)
1548
1549
1562#define k_cyc_to_us_floor32(t) \
1563 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, false)
1564
1565
1578#define k_cyc_to_us_floor64(t) \
1579 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, false)
1580
1581
1594#define k_cyc_to_us_near32(t) \
1595 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, true)
1596
1597
1610#define k_cyc_to_us_near64(t) \
1611 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, true)
1612
1613
1626#define k_cyc_to_us_ceil32(t) \
1627 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, true, false)
1628
1629
1642#define k_cyc_to_us_ceil64(t) \
1643 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, true, false)
1644
1645
1658#define k_cyc_to_ns_floor32(t) \
1659 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, false)
1660
1661
1674#define k_cyc_to_ns_floor64(t) \
1675 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, false)
1676
1677
1690#define k_cyc_to_ns_near32(t) \
1691 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, true)
1692
1693
1706#define k_cyc_to_ns_near64(t) \
1707 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, true)
1708
1709
1722#define k_cyc_to_ns_ceil32(t) \
1723 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, true, false)
1724
1725
1738#define k_cyc_to_ns_ceil64(t) \
1739 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, true, false)
1740
1741
1754#define k_cyc_to_ticks_floor32(t) \
1755 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, false)
1756
1757
1770#define k_cyc_to_ticks_floor64(t) \
1771 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, false)
1772
1773
1786#define k_cyc_to_ticks_near32(t) \
1787 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, true)
1788
1789
1802#define k_cyc_to_ticks_near64(t) \
1803 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, true)
1804
1805
1818#define k_cyc_to_ticks_ceil32(t) \
1819 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, true, false)
1820
1821
1834#define k_cyc_to_ticks_ceil64(t) \
1835 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, true, false)
1836
1837
1850#define k_ticks_to_sec_floor32(t) \
1851 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_sec, true, false, false)
1852
1853
1866#define k_ticks_to_sec_floor64(t) \
1867 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_sec, true, false, false)
1868
1869
1882#define k_ticks_to_sec_near32(t) \
1883 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_sec, true, false, true)
1884
1885
1898#define k_ticks_to_sec_near64(t) \
1899 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_sec, true, false, true)
1900
1901
1914#define k_ticks_to_sec_ceil32(t) \
1915 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_sec, true, true, false)
1916
1917
1930#define k_ticks_to_sec_ceil64(t) \
1931 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_sec, true, true, false)
1932
1933
1946#define k_ticks_to_ms_floor32(t) \
1947 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, false, false)
1948
1949
1962#define k_ticks_to_ms_floor64(t) \
1963 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, false, false)
1964
1965
1978#define k_ticks_to_ms_near32(t) \
1979 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, false, true)
1980
1981
1994#define k_ticks_to_ms_near64(t) \
1995 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, false, true)
1996
1997
2010#define k_ticks_to_ms_ceil32(t) \
2011 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, true, false)
2012
2013
2026#define k_ticks_to_ms_ceil64(t) \
2027 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, true, false)
2028
2029
2042#define k_ticks_to_us_floor32(t) \
2043 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, false, false)
2044
2045
2058#define k_ticks_to_us_floor64(t) \
2059 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, false, false)
2060
2061
2074#define k_ticks_to_us_near32(t) \
2075 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, false, true)
2076
2077
2090#define k_ticks_to_us_near64(t) \
2091 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, false, true)
2092
2093
2106#define k_ticks_to_us_ceil32(t) \
2107 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, true, false)
2108
2109
2122#define k_ticks_to_us_ceil64(t) \
2123 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, true, false)
2124
2125
2138#define k_ticks_to_ns_floor32(t) \
2139 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, false, false)
2140
2141
2154#define k_ticks_to_ns_floor64(t) \
2155 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, false, false)
2156
2157
2170#define k_ticks_to_ns_near32(t) \
2171 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, false, true)
2172
2173
2186#define k_ticks_to_ns_near64(t) \
2187 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, false, true)
2188
2189
2202#define k_ticks_to_ns_ceil32(t) \
2203 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, true, false)
2204
2205
2218#define k_ticks_to_ns_ceil64(t) \
2219 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, true, false)
2220
2221
2234#define k_ticks_to_cyc_floor32(t) \
2235 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, false)
2236
2237
2250#define k_ticks_to_cyc_floor64(t) \
2251 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, false)
2252
2253
2266#define k_ticks_to_cyc_near32(t) \
2267 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, true)
2268
2269
2282#define k_ticks_to_cyc_near64(t) \
2283 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, true)
2284
2285
2298#define k_ticks_to_cyc_ceil32(t) \
2299 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, true, false)
2300
2301
2314#define k_ticks_to_cyc_ceil64(t) \
2315 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, true, false)
2316
2317#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
2318 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
2319#include <zephyr/syscalls/time_units.h>
2320#endif
2321
2322#undef TIME_CONSTEXPR
2323
2327
2328#ifdef __cplusplus
2329} /* extern "C" */
2330#endif
2331
2332#endif /* ZEPHYR_INCLUDE_SYS_TIME_UNITS_H_ */
64-bit by 32-bit division without a 64-bit divide instruction.
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:166
static uint64_t sys_div64_recip_div(uint64_t n, const struct sys_div64_recip *r)
Divide a 64-bit value by a reciprocal's divisor.
Definition div64.h:399
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
Reciprocal of a 32-bit divisor known only at run time.
Definition div64.h:325
Misc utilities.
Macros to abstract toolchain specific capabilities.