Zephyr Project API 4.0.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_TIME_UNITS_H_
8#define ZEPHYR_INCLUDE_TIME_UNITS_H_
9
10#include <zephyr/toolchain.h>
11#include <zephyr/sys/util.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
33#define SYS_FOREVER_MS (-1)
34
39#define SYS_FOREVER_US (-1)
40
44#define SYS_TIMEOUT_MS_INIT(ms) \
45 Z_TIMEOUT_TICKS_INIT((ms) == SYS_FOREVER_MS ? \
46 K_TICKS_FOREVER : Z_TIMEOUT_MS_TICKS(ms))
47
50#define SYS_TIMEOUT_MS(ms) ((k_timeout_t) SYS_TIMEOUT_MS_INIT(ms))
51
52/* Exhaustively enumerated, highly optimized time unit conversion API */
53
54#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
55__syscall int sys_clock_hw_cycles_per_sec_runtime_get(void);
56
57static inline int z_impl_sys_clock_hw_cycles_per_sec_runtime_get(void)
58{
59 extern int z_clock_hw_cycles_per_sec;
60
61 return z_clock_hw_cycles_per_sec;
62}
63#endif /* CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME */
64
65#if defined(__cplusplus) && (__cplusplus >= 201402L)
66 #if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
67 #define TIME_CONSTEXPR
68 #else
69 #define TIME_CONSTEXPR constexpr
70 #endif
71#else
72 #define TIME_CONSTEXPR
73#endif
74
79#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
80#define sys_clock_hw_cycles_per_sec() sys_clock_hw_cycles_per_sec_runtime_get()
81#else
82#define sys_clock_hw_cycles_per_sec() CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC
83#endif
84
96#define z_tmcvt_use_fast_algo(from_hz, to_hz) \
97 ((DIV_ROUND_UP(CONFIG_SYS_CLOCK_MAX_TIMEOUT_DAYS * 24ULL * 3600ULL * from_hz, \
98 UINT32_MAX) * to_hz) <= UINT32_MAX)
99
100/* Time converter generator gadget. Selects from one of three
101 * conversion algorithms: ones that take advantage when the
102 * frequencies are an integer ratio (in either direction), or a full
103 * precision conversion. Clever use of extra arguments causes all the
104 * selection logic to be optimized out, and the generated code even
105 * reduces to 32 bit only if a ratio conversion is available and the
106 * result is 32 bits.
107 *
108 * This isn't intended to be used directly, instead being wrapped
109 * appropriately in a user-facing API. The boolean arguments are:
110 *
111 * const_hz - The hz arguments are known to be compile-time
112 * constants (because otherwise the modulus test would
113 * have to be done at runtime)
114 * result32 - The result will be truncated to 32 bits on use
115 * round_up - Return the ceiling of the resulting fraction
116 * round_off - Return the nearest value to the resulting fraction
117 * (pass both round_up/off as false to get "round_down")
118 *
119 * All of this must be implemented as expressions so that, when constant,
120 * the results may be used to initialize global variables.
121 */
122
123/* true if the conversion is the identity */
124#define z_tmcvt_is_identity(__from_hz, __to_hz) \
125 ((__to_hz) == (__from_hz))
126
127/* true if the conversion requires a simple integer multiply */
128#define z_tmcvt_is_int_mul(__from_hz, __to_hz) \
129 ((__to_hz) > (__from_hz) && (__to_hz) % (__from_hz) == 0U)
130
131/* true if the conversion requires a simple integer division */
132#define z_tmcvt_is_int_div(__from_hz, __to_hz) \
133 ((__from_hz) > (__to_hz) && (__from_hz) % (__to_hz) == 0U)
134
135/*
136 * Compute the offset needed to round the result correctly when
137 * the conversion requires a simple integer division
138 */
139#define z_tmcvt_off_div(__from_hz, __to_hz, __round_up, __round_off) \
140 ((__round_off) ? ((__from_hz) / (__to_hz)) / 2 : \
141 (__round_up) ? ((__from_hz) / (__to_hz)) - 1 : \
142 0)
143
144/*
145 * All users of this macro MUST ensure its output is never used when a/b
146 * is zero because it incorrectly but by design never returns zero.
147 *
148 * Some compiler versions emit a divide-by-zero warning for this code:
149 * "false ? 42/0 : 43". Dealing with (generated) dead code is hard:
150 * https://github.com/zephyrproject-rtos/zephyr/issues/63564
151 * https://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html
152 *
153 * To silence such divide-by-zero warnings, "cheat" and never return
154 * zero. Return 1 instead. Use octal "01u" as a breadcrumb to ease a
155 * little bit the huge pain of "reverse-engineering" pre-processor
156 * output.
157 *
158 * The "Elvis" operator "a/b ?: 1" is tempting because it avoids
159 * evaluating the same expression twice. However: 1. it's a non-standard
160 * GNU extension; 2. everything in this file is designed to be computed
161 * at compile time anyway.
162 */
163#define z_tmcvt_divisor(a, b) ((a)/(b) ? (a)/(b) : 01u)
164
165/*
166 * Compute the offset needed to round the result correctly when
167 * the conversion requires a full mul/div
168 */
169#define z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off) \
170 ((__round_off) ? (__from_hz) / 2 : \
171 (__round_up) ? (__from_hz) - 1 : \
172 0)
173
174/* Integer division 32-bit conversion */
175#define z_tmcvt_int_div_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
176 ((uint64_t) (__t) <= 0xffffffffU - \
177 z_tmcvt_off_div(__from_hz, __to_hz, __round_up, __round_off) ? \
178 ((uint32_t)((__t) + \
179 z_tmcvt_off_div(__from_hz, __to_hz, \
180 __round_up, __round_off)) / \
181 z_tmcvt_divisor(__from_hz, __to_hz)) \
182 : \
183 (uint32_t) (((uint64_t) (__t) + \
184 z_tmcvt_off_div(__from_hz, __to_hz, \
185 __round_up, __round_off)) / \
186 z_tmcvt_divisor(__from_hz, __to_hz)) \
187 )
188
189/* Integer multiplication 32-bit conversion */
190#define z_tmcvt_int_mul_32(__t, __from_hz, __to_hz) \
191 (uint32_t) (__t)*((__to_hz) / (__from_hz))
192
193/* General 32-bit conversion */
194#define z_tmcvt_gen_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
195 ((uint32_t) (((uint64_t) (__t)*(__to_hz) + \
196 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off)) / (__from_hz)))
197
198/* Integer division 64-bit conversion */
199#define z_tmcvt_int_div_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
200 (((uint64_t) (__t) + z_tmcvt_off_div(__from_hz, __to_hz, \
201 __round_up, __round_off)) / \
202 z_tmcvt_divisor(__from_hz, __to_hz))
203
204/* Integer multiplication 64-bit conversion */
205#define z_tmcvt_int_mul_64(__t, __from_hz, __to_hz) \
206 (uint64_t) (__t)*((__to_hz) / (__from_hz))
207
208/* Fast 64-bit conversion. This relies on the multiply not overflowing */
209#define z_tmcvt_gen_64_fast(__t, __from_hz, __to_hz, __round_up, __round_off) \
210 (((uint64_t) (__t)*(__to_hz) + \
211 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off)) / (__from_hz))
212
213/* Slow 64-bit conversion. This avoids overflowing the multiply */
214#define z_tmcvt_gen_64_slow(__t, __from_hz, __to_hz, __round_up, __round_off) \
215 (((uint64_t) (__t) / (__from_hz))*(__to_hz) + \
216 (((uint64_t) (__t) % (__from_hz))*(__to_hz) + \
217 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off)) / (__from_hz))
218
219/* General 64-bit conversion. Uses one of the two above macros */
220#define z_tmcvt_gen_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
221 (z_tmcvt_use_fast_algo(__from_hz, __to_hz) ? \
222 z_tmcvt_gen_64_fast(__t, __from_hz, __to_hz, __round_up, __round_off) : \
223 z_tmcvt_gen_64_slow(__t, __from_hz, __to_hz, __round_up, __round_off))
224
225/* Convert, generating a 32-bit result */
226#define z_tmcvt_32(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
227 ((__const_hz) ? \
228 ( \
229 z_tmcvt_is_identity(__from_hz, __to_hz) ? \
230 (uint32_t) (__t) \
231 : \
232 z_tmcvt_is_int_div(__from_hz, __to_hz) ? \
233 z_tmcvt_int_div_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
234 : \
235 z_tmcvt_is_int_mul(__from_hz, __to_hz) ? \
236 z_tmcvt_int_mul_32(__t, __from_hz, __to_hz) \
237 : \
238 z_tmcvt_gen_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
239 ) \
240 : \
241 z_tmcvt_gen_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
242 )
243
244/* Convert, generating a 64-bit result */
245#define z_tmcvt_64(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
246 ((__const_hz) ? \
247 ( \
248 z_tmcvt_is_identity(__from_hz, __to_hz) ? \
249 (uint64_t) (__t) \
250 : \
251 z_tmcvt_is_int_div(__from_hz, __to_hz) ? \
252 z_tmcvt_int_div_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
253 : \
254 z_tmcvt_is_int_mul(__from_hz, __to_hz) ? \
255 z_tmcvt_int_mul_64(__t, __from_hz, __to_hz) \
256 : \
257 z_tmcvt_gen_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
258 ) \
259 : \
260 z_tmcvt_gen_64_slow(__t, __from_hz, __to_hz, __round_up, __round_off) \
261 )
262
263#define z_tmcvt(__t, __from_hz, __to_hz, __const_hz, __result32, __round_up, __round_off) \
264 ((__result32) ? \
265 z_tmcvt_32(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) : \
266 z_tmcvt_64(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off))
267
268/* The following code is programmatically generated using this perl
269 * code, which enumerates all possible combinations of units, rounding
270 * modes and precision. Do not edit directly.
271 *
272 * Note that nano/microsecond conversions are only defined with 64 bit
273 * precision. These units conversions were not available in 32 bit
274 * variants historically, and doing 32 bit math with units that small
275 * has precision traps that we probably don't want to support in an
276 * official API.
277 *
278 * #!/usr/bin/perl -w
279 * use strict;
280 *
281 * my %human = ("sec" => "seconds",
282 * "ms" => "milliseconds",
283 * "us" => "microseconds",
284 * "ns" => "nanoseconds",
285 * "cyc" => "hardware cycles",
286 * "ticks" => "ticks");
287 * my %human_round = ("ceil" => "Rounds up",
288 * "near" => "Round nearest",
289 * "floor" => "Truncates");
290 *
291 * sub big { return $_[0] eq "us" || $_[0] eq "ns"; }
292 * sub prefix { return $_[0] eq "sec" || $_[0] eq "ms" || $_[0] eq "us" || $_[0] eq "ns"; }
293 *
294 * for my $from_unit ("sec", "ms", "us", "ns", "cyc", "ticks") {
295 * for my $to_unit ("sec", "ms", "us", "ns", "cyc", "ticks") {
296 * next if $from_unit eq $to_unit;
297 * next if prefix($from_unit) && prefix($to_unit);
298 * for my $round ("floor", "near", "ceil") {
299 * for(my $big=0; $big <= 1; $big++) {
300 * my $sz = $big ? 64 : 32;
301 * my $sym = "k_${from_unit}_to_${to_unit}_$round$sz";
302 * my $type = "uint${sz}_t";
303 * my $const_hz = ($from_unit eq "cyc" || $to_unit eq "cyc")
304 * ? "Z_CCYC" : "true";
305 * my $ret32 = $big ? "64" : "32";
306 * my $rup = $round eq "ceil" ? "true" : "false";
307 * my $roff = $round eq "near" ? "true" : "false";
308 *
309 * my $hfrom = $human{$from_unit};
310 * my $hto = $human{$to_unit};
311 * my $hround = $human_round{$round};
312 * print "/", "** \@brief Convert $hfrom to $hto. $ret32 bits. $hround.\n";
313 * print " *\n";
314 * print " * Converts time values in $hfrom to $hto.\n";
315 * print " * Computes result in $sz bit precision.\n";
316 * if ($round eq "ceil") {
317 * print " * Rounds up to the next highest output unit.\n";
318 * } elsif ($round eq "near") {
319 * print " * Rounds to the nearest output unit.\n";
320 * } else {
321 * print " * Truncates to the next lowest output unit.\n";
322 * }
323 * print " *\n";
324 * print " * \@warning Generated. Do not edit. See above.\n";
325 * print " *\n";
326 * print " * \@param t Source time in $hfrom. uint64_t\n";
327 * print " *\n";
328 * print " * \@return The converted time value in $hto. $type\n";
329 * print " *", "/\n";
330 * print "#define $sym(t) \\\n";
331 * print "\tz_tmcvt_$ret32(t, Z_HZ_$from_unit, Z_HZ_$to_unit,";
332 * print " $const_hz, $rup, $roff)\n";
333 * print "\n\n";
334 * }
335 * }
336 * }
337 * }
338 */
339
340/* Some more concise declarations to simplify the generator script and
341 * save bytes below
342 */
343#define Z_HZ_sec 1
344#define Z_HZ_ms 1000
345#define Z_HZ_us 1000000
346#define Z_HZ_ns 1000000000
347#define Z_HZ_cyc sys_clock_hw_cycles_per_sec()
348#define Z_HZ_ticks CONFIG_SYS_CLOCK_TICKS_PER_SEC
349#define Z_CCYC (!IS_ENABLED(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME))
350
363#define k_sec_to_cyc_floor32(t) \
364 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, false)
365
366
379#define k_sec_to_cyc_floor64(t) \
380 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, false)
381
382
395#define k_sec_to_cyc_near32(t) \
396 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, true)
397
398
411#define k_sec_to_cyc_near64(t) \
412 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, true)
413
414
427#define k_sec_to_cyc_ceil32(t) \
428 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, true, false)
429
430
443#define k_sec_to_cyc_ceil64(t) \
444 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, true, false)
445
446
459#define k_sec_to_ticks_floor32(t) \
460 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_ticks, true, false, false)
461
462
475#define k_sec_to_ticks_floor64(t) \
476 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_ticks, true, false, false)
477
478
491#define k_sec_to_ticks_near32(t) \
492 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_ticks, true, false, true)
493
494
507#define k_sec_to_ticks_near64(t) \
508 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_ticks, true, false, true)
509
510
523#define k_sec_to_ticks_ceil32(t) \
524 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_ticks, true, true, false)
525
526
539#define k_sec_to_ticks_ceil64(t) \
540 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_ticks, true, true, false)
541
542
555#define k_ms_to_cyc_floor32(t) \
556 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, false)
557
558
571#define k_ms_to_cyc_floor64(t) \
572 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, false)
573
574
587#define k_ms_to_cyc_near32(t) \
588 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, true)
589
590
603#define k_ms_to_cyc_near64(t) \
604 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, true)
605
606
619#define k_ms_to_cyc_ceil32(t) \
620 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, true, false)
621
622
635#define k_ms_to_cyc_ceil64(t) \
636 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, true, false)
637
638
651#define k_ms_to_ticks_floor32(t) \
652 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, false, false)
653
654
667#define k_ms_to_ticks_floor64(t) \
668 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, false, false)
669
670
683#define k_ms_to_ticks_near32(t) \
684 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, false, true)
685
686
699#define k_ms_to_ticks_near64(t) \
700 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, false, true)
701
702
715#define k_ms_to_ticks_ceil32(t) \
716 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, true, false)
717
718
731#define k_ms_to_ticks_ceil64(t) \
732 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, true, false)
733
734
747#define k_us_to_cyc_floor32(t) \
748 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, false)
749
750
763#define k_us_to_cyc_floor64(t) \
764 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, false)
765
766
779#define k_us_to_cyc_near32(t) \
780 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, true)
781
782
795#define k_us_to_cyc_near64(t) \
796 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, true)
797
798
811#define k_us_to_cyc_ceil32(t) \
812 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, true, false)
813
814
827#define k_us_to_cyc_ceil64(t) \
828 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, true, false)
829
830
843#define k_us_to_ticks_floor32(t) \
844 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, false, false)
845
846
859#define k_us_to_ticks_floor64(t) \
860 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, false, false)
861
862
875#define k_us_to_ticks_near32(t) \
876 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, false, true)
877
878
891#define k_us_to_ticks_near64(t) \
892 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, false, true)
893
894
907#define k_us_to_ticks_ceil32(t) \
908 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, true, false)
909
910
923#define k_us_to_ticks_ceil64(t) \
924 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, true, false)
925
926
939#define k_ns_to_cyc_floor32(t) \
940 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, false)
941
942
955#define k_ns_to_cyc_floor64(t) \
956 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, false)
957
958
971#define k_ns_to_cyc_near32(t) \
972 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, true)
973
974
987#define k_ns_to_cyc_near64(t) \
988 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, true)
989
990
1003#define k_ns_to_cyc_ceil32(t) \
1004 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, true, false)
1005
1006
1019#define k_ns_to_cyc_ceil64(t) \
1020 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, true, false)
1021
1022
1035#define k_ns_to_ticks_floor32(t) \
1036 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, false, false)
1037
1038
1051#define k_ns_to_ticks_floor64(t) \
1052 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, false, false)
1053
1054
1067#define k_ns_to_ticks_near32(t) \
1068 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, false, true)
1069
1070
1083#define k_ns_to_ticks_near64(t) \
1084 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, false, true)
1085
1086
1099#define k_ns_to_ticks_ceil32(t) \
1100 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, true, false)
1101
1102
1115#define k_ns_to_ticks_ceil64(t) \
1116 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, true, false)
1117
1118
1131#define k_cyc_to_sec_floor32(t) \
1132 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, false)
1133
1134
1147#define k_cyc_to_sec_floor64(t) \
1148 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, false)
1149
1150
1163#define k_cyc_to_sec_near32(t) \
1164 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, true)
1165
1166
1179#define k_cyc_to_sec_near64(t) \
1180 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, true)
1181
1182
1195#define k_cyc_to_sec_ceil32(t) \
1196 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, true, false)
1197
1198
1211#define k_cyc_to_sec_ceil64(t) \
1212 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, true, false)
1213
1214
1227#define k_cyc_to_ms_floor32(t) \
1228 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, false)
1229
1230
1243#define k_cyc_to_ms_floor64(t) \
1244 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, false)
1245
1246
1259#define k_cyc_to_ms_near32(t) \
1260 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, true)
1261
1262
1275#define k_cyc_to_ms_near64(t) \
1276 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, true)
1277
1278
1291#define k_cyc_to_ms_ceil32(t) \
1292 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, true, false)
1293
1294
1307#define k_cyc_to_ms_ceil64(t) \
1308 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, true, false)
1309
1310
1323#define k_cyc_to_us_floor32(t) \
1324 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, false)
1325
1326
1339#define k_cyc_to_us_floor64(t) \
1340 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, false)
1341
1342
1355#define k_cyc_to_us_near32(t) \
1356 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, true)
1357
1358
1371#define k_cyc_to_us_near64(t) \
1372 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, true)
1373
1374
1387#define k_cyc_to_us_ceil32(t) \
1388 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, true, false)
1389
1390
1403#define k_cyc_to_us_ceil64(t) \
1404 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, true, false)
1405
1406
1419#define k_cyc_to_ns_floor32(t) \
1420 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, false)
1421
1422
1435#define k_cyc_to_ns_floor64(t) \
1436 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, false)
1437
1438
1451#define k_cyc_to_ns_near32(t) \
1452 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, true)
1453
1454
1467#define k_cyc_to_ns_near64(t) \
1468 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, true)
1469
1470
1483#define k_cyc_to_ns_ceil32(t) \
1484 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, true, false)
1485
1486
1499#define k_cyc_to_ns_ceil64(t) \
1500 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, true, false)
1501
1502
1515#define k_cyc_to_ticks_floor32(t) \
1516 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, false)
1517
1518
1531#define k_cyc_to_ticks_floor64(t) \
1532 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, false)
1533
1534
1547#define k_cyc_to_ticks_near32(t) \
1548 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, true)
1549
1550
1563#define k_cyc_to_ticks_near64(t) \
1564 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, true)
1565
1566
1579#define k_cyc_to_ticks_ceil32(t) \
1580 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, true, false)
1581
1582
1595#define k_cyc_to_ticks_ceil64(t) \
1596 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, true, false)
1597
1598
1611#define k_ticks_to_sec_floor32(t) \
1612 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_sec, true, false, false)
1613
1614
1627#define k_ticks_to_sec_floor64(t) \
1628 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_sec, true, false, false)
1629
1630
1643#define k_ticks_to_sec_near32(t) \
1644 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_sec, true, false, true)
1645
1646
1659#define k_ticks_to_sec_near64(t) \
1660 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_sec, true, false, true)
1661
1662
1675#define k_ticks_to_sec_ceil32(t) \
1676 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_sec, true, true, false)
1677
1678
1691#define k_ticks_to_sec_ceil64(t) \
1692 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_sec, true, true, false)
1693
1694
1707#define k_ticks_to_ms_floor32(t) \
1708 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, false, false)
1709
1710
1723#define k_ticks_to_ms_floor64(t) \
1724 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, false, false)
1725
1726
1739#define k_ticks_to_ms_near32(t) \
1740 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, false, true)
1741
1742
1755#define k_ticks_to_ms_near64(t) \
1756 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, false, true)
1757
1758
1771#define k_ticks_to_ms_ceil32(t) \
1772 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, true, false)
1773
1774
1787#define k_ticks_to_ms_ceil64(t) \
1788 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, true, false)
1789
1790
1803#define k_ticks_to_us_floor32(t) \
1804 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, false, false)
1805
1806
1819#define k_ticks_to_us_floor64(t) \
1820 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, false, false)
1821
1822
1835#define k_ticks_to_us_near32(t) \
1836 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, false, true)
1837
1838
1851#define k_ticks_to_us_near64(t) \
1852 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, false, true)
1853
1854
1867#define k_ticks_to_us_ceil32(t) \
1868 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, true, false)
1869
1870
1883#define k_ticks_to_us_ceil64(t) \
1884 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, true, false)
1885
1886
1899#define k_ticks_to_ns_floor32(t) \
1900 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, false, false)
1901
1902
1915#define k_ticks_to_ns_floor64(t) \
1916 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, false, false)
1917
1918
1931#define k_ticks_to_ns_near32(t) \
1932 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, false, true)
1933
1934
1947#define k_ticks_to_ns_near64(t) \
1948 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, false, true)
1949
1950
1963#define k_ticks_to_ns_ceil32(t) \
1964 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, true, false)
1965
1966
1979#define k_ticks_to_ns_ceil64(t) \
1980 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, true, false)
1981
1982
1995#define k_ticks_to_cyc_floor32(t) \
1996 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, false)
1997
1998
2011#define k_ticks_to_cyc_floor64(t) \
2012 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, false)
2013
2014
2027#define k_ticks_to_cyc_near32(t) \
2028 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, true)
2029
2030
2043#define k_ticks_to_cyc_near64(t) \
2044 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, true)
2045
2046
2059#define k_ticks_to_cyc_ceil32(t) \
2060 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, true, false)
2061
2062
2075#define k_ticks_to_cyc_ceil64(t) \
2076 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, true, false)
2077
2078#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
2079#include <zephyr/syscalls/time_units.h>
2080#endif
2081
2082#undef TIME_CONSTEXPR
2083
2088#ifdef __cplusplus
2089} /* extern "C" */
2090#endif
2091
2092#endif /* ZEPHYR_INCLUDE_TIME_UNITS_H_ */
Misc utilities.
Macros to abstract toolchain specific capabilities.