Zephyr API Documentation 4.5.0-rc1
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/util.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
25
33#define SYS_FOREVER_MS (-1)
34
39#define SYS_FOREVER_US (-1)
40
43#define SYS_TIMEOUT_MS(ms) \
44 Z_TIMEOUT_TICKS((ms) == SYS_FOREVER_MS ? \
45 K_TICKS_FOREVER : Z_TIMEOUT_MS_TICKS(ms))
46
47/* Exhaustively enumerated, highly optimized time unit conversion API */
48
49#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
50 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
58__syscall unsigned int sys_clock_hw_cycles_per_sec_runtime_get(void);
59
60static inline unsigned int z_impl_sys_clock_hw_cycles_per_sec_runtime_get(void)
61{
62 extern unsigned int z_clock_hw_cycles_per_sec;
63
64 return z_clock_hw_cycles_per_sec;
65}
66#endif /* CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME */
67
68#if defined(__cplusplus) && (__cplusplus >= 201402L)
69 #if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
70 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
71 #define TIME_CONSTEXPR
72 #else
73 #define TIME_CONSTEXPR constexpr
74 #endif
75#else
76 #define TIME_CONSTEXPR
77#endif
78
83#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
84 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
85#define sys_clock_hw_cycles_per_sec() sys_clock_hw_cycles_per_sec_runtime_get()
86#else
87#define sys_clock_hw_cycles_per_sec() (uint32_t)CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC
88#endif
89
101#define z_tmcvt_use_fast_algo(from_hz, to_hz) \
102 ((DIV_ROUND_UP(CONFIG_SYS_CLOCK_MAX_TIMEOUT_DAYS * 24ULL * 3600ULL * from_hz, \
103 UINT32_MAX) * to_hz) <= UINT32_MAX)
104
105/* Time converter generator gadget. Selects from one of three
106 * conversion algorithms: ones that take advantage when the
107 * frequencies are an integer ratio (in either direction), or a full
108 * precision conversion. Clever use of extra arguments causes all the
109 * selection logic to be optimized out, and the generated code even
110 * reduces to 32 bit only if a ratio conversion is available and the
111 * result is 32 bits.
112 *
113 * This isn't intended to be used directly, instead being wrapped
114 * appropriately in a user-facing API. The boolean arguments are:
115 *
116 * const_hz - The hz arguments are known to be compile-time
117 * constants (because otherwise the modulus test would
118 * have to be done at runtime)
119 * result32 - The result will be truncated to 32 bits on use
120 * round_up - Return the ceiling of the resulting fraction
121 * round_off - Return the nearest value to the resulting fraction
122 * (pass both round_up/off as false to get "round_down")
123 *
124 * All of this must be implemented as expressions so that, when constant,
125 * the results may be used to initialize global variables.
126 */
127
128/* true if the conversion is the identity */
129#define z_tmcvt_is_identity(__from_hz, __to_hz) \
130 ((__to_hz) == (__from_hz))
131
132/* true if the conversion requires a simple integer multiply */
133#define z_tmcvt_is_int_mul(__from_hz, __to_hz) \
134 ((__to_hz) > (__from_hz) && (__to_hz) % (__from_hz) == 0U)
135
136/* true if the conversion requires a simple integer division */
137#define z_tmcvt_is_int_div(__from_hz, __to_hz) \
138 ((__from_hz) > (__to_hz) && (__from_hz) % (__to_hz) == 0U)
139
140/*
141 * Compute the offset needed to round the result correctly when
142 * the conversion requires a simple integer division
143 */
144#define z_tmcvt_off_div(__from_hz, __to_hz, __round_up, __round_off) \
145 ((__round_off) ? ((__from_hz) / (__to_hz)) / 2 : \
146 (__round_up) ? ((__from_hz) / (__to_hz)) - 1 : \
147 0)
148
149/*
150 * All users of this macro MUST ensure its output is never used when a/b
151 * is zero because it incorrectly but by design never returns zero.
152 *
153 * Some compiler versions emit a divide-by-zero warning for this code:
154 * "false ? 42/0 : 43". Dealing with (generated) dead code is hard:
155 * https://github.com/zephyrproject-rtos/zephyr/issues/63564
156 * https://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html
157 *
158 * To silence such divide-by-zero warnings, "cheat" and never return
159 * zero. Return 1 instead. Use octal "01u" as a breadcrumb to ease a
160 * little bit the huge pain of "reverse-engineering" pre-processor
161 * output.
162 *
163 * The "Elvis" operator "a/b ?: 1" is tempting because it avoids
164 * evaluating the same expression twice. However: 1. it's a non-standard
165 * GNU extension; 2. everything in this file is designed to be computed
166 * at compile time anyway.
167 */
168#define z_tmcvt_divisor(a, b) ((a)/(b) ? (a)/(b) : 1U)
169
170/*
171 * Compute the offset needed to round the result correctly when
172 * the conversion requires a full mul/div
173 */
174#define z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off) \
175 ((__round_off) ? (__from_hz) / 2 : \
176 (__round_up) ? (__from_hz) - 1 : \
177 0)
178
179/* Integer division 32-bit conversion */
180#define z_tmcvt_int_div_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
181 ((uint64_t) (__t) <= 0xffffffffU - \
182 z_tmcvt_off_div(__from_hz, __to_hz, __round_up, __round_off) ? \
183 ((uint32_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 (uint32_t) (((uint64_t) (__t) + \
189 z_tmcvt_off_div(__from_hz, __to_hz, \
190 __round_up, __round_off)) / \
191 z_tmcvt_divisor(__from_hz, __to_hz)) \
192 )
193
194/* Integer multiplication 32-bit conversion */
195#define z_tmcvt_int_mul_32(__t, __from_hz, __to_hz) \
196 ((uint32_t) ((__t)*((__to_hz) / (__from_hz))))
197
198/* General 32-bit conversion */
199#define z_tmcvt_gen_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
200 ((uint32_t) (((uint64_t) (__t)*(__to_hz) + \
201 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off)) / (__from_hz)))
202
203/* Integer division 64-bit conversion */
204#define z_tmcvt_int_div_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
205 (((uint64_t) (__t) + z_tmcvt_off_div(__from_hz, __to_hz, \
206 __round_up, __round_off)) / \
207 z_tmcvt_divisor(__from_hz, __to_hz))
208
209/* Integer multiplication 64-bit conversion */
210#define z_tmcvt_int_mul_64(__t, __from_hz, __to_hz) \
211 (uint64_t) (__t)*((__to_hz) / (__from_hz))
212
213/* Fast 64-bit conversion. This relies on the multiply not overflowing */
214#define z_tmcvt_gen_64_fast(__t, __from_hz, __to_hz, __round_up, __round_off) \
215 (((uint64_t) (__t)*(__to_hz) + \
216 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off)) / (__from_hz))
217
218/* Slow 64-bit conversion. This avoids overflowing the multiply */
219#define z_tmcvt_gen_64_slow(__t, __from_hz, __to_hz, __round_up, __round_off) \
220 ((((uint64_t) (__t) / (__from_hz))*(__to_hz)) + \
221 (((((uint64_t) (__t) % (__from_hz))*(__to_hz)) + \
222 z_tmcvt_off_gen(__from_hz, __to_hz, __round_up, __round_off)) / (__from_hz)))
223
224/* General 64-bit conversion. Uses one of the two above macros */
225#define z_tmcvt_gen_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
226 (z_tmcvt_use_fast_algo(__from_hz, __to_hz) ? \
227 z_tmcvt_gen_64_fast(__t, __from_hz, __to_hz, __round_up, __round_off) : \
228 z_tmcvt_gen_64_slow(__t, __from_hz, __to_hz, __round_up, __round_off))
229
230/* Convert, generating a 32-bit result */
231#define z_tmcvt_32(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
232 ((__const_hz) ? \
233 ( \
234 z_tmcvt_is_identity(__from_hz, __to_hz) ? \
235 (uint32_t) (__t) \
236 : \
237 z_tmcvt_is_int_div(__from_hz, __to_hz) ? \
238 z_tmcvt_int_div_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
239 : \
240 z_tmcvt_is_int_mul(__from_hz, __to_hz) ? \
241 z_tmcvt_int_mul_32(__t, __from_hz, __to_hz) \
242 : \
243 z_tmcvt_gen_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
244 ) \
245 : \
246 z_tmcvt_gen_32(__t, __from_hz, __to_hz, __round_up, __round_off) \
247 )
248
249/* Convert, generating a 64-bit result */
250#define z_tmcvt_64(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) \
251 ((__const_hz) ? \
252 ( \
253 z_tmcvt_is_identity(__from_hz, __to_hz) ? \
254 (uint64_t) (__t) \
255 : \
256 z_tmcvt_is_int_div(__from_hz, __to_hz) ? \
257 z_tmcvt_int_div_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
258 : \
259 z_tmcvt_is_int_mul(__from_hz, __to_hz) ? \
260 z_tmcvt_int_mul_64(__t, __from_hz, __to_hz) \
261 : \
262 z_tmcvt_gen_64(__t, __from_hz, __to_hz, __round_up, __round_off) \
263 ) \
264 : \
265 z_tmcvt_gen_64_slow(__t, __from_hz, __to_hz, __round_up, __round_off) \
266 )
267
268#define z_tmcvt(__t, __from_hz, __to_hz, __const_hz, __result32, __round_up, __round_off) \
269 ((__result32) ? \
270 z_tmcvt_32(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off) : \
271 z_tmcvt_64(__t, __from_hz, __to_hz, __const_hz, __round_up, __round_off))
272
273/* The following code is programmatically generated using this perl
274 * code, which enumerates all possible combinations of units, rounding
275 * modes and precision. Do not edit directly.
276 *
277 * Note that nano/microsecond conversions are only defined with 64 bit
278 * precision. These units conversions were not available in 32 bit
279 * variants historically, and doing 32 bit math with units that small
280 * has precision traps that we probably don't want to support in an
281 * official API.
282 *
283 * #!/usr/bin/perl -w
284 * use strict;
285 *
286 * my %human = ("sec" => "seconds",
287 * "ms" => "milliseconds",
288 * "us" => "microseconds",
289 * "ns" => "nanoseconds",
290 * "cyc" => "hardware cycles",
291 * "ticks" => "ticks");
292 * my %human_round = ("ceil" => "Rounds up",
293 * "near" => "Round nearest",
294 * "floor" => "Truncates");
295 *
296 * sub big { return $_[0] eq "us" || $_[0] eq "ns"; }
297 * sub prefix { return $_[0] eq "sec" || $_[0] eq "ms" || $_[0] eq "us" || $_[0] eq "ns"; }
298 *
299 * for my $from_unit ("sec", "ms", "us", "ns", "cyc", "ticks") {
300 * for my $to_unit ("sec", "ms", "us", "ns", "cyc", "ticks") {
301 * next if $from_unit eq $to_unit;
302 * next if prefix($from_unit) && prefix($to_unit);
303 * for my $round ("floor", "near", "ceil") {
304 * for(my $big=0; $big <= 1; $big++) {
305 * my $sz = $big ? 64 : 32;
306 * my $sym = "k_${from_unit}_to_${to_unit}_$round$sz";
307 * my $type = "uint${sz}_t";
308 * my $const_hz = ($from_unit eq "cyc" || $to_unit eq "cyc")
309 * ? "Z_CCYC" : "true";
310 * my $ret32 = $big ? "64" : "32";
311 * my $rup = $round eq "ceil" ? "true" : "false";
312 * my $roff = $round eq "near" ? "true" : "false";
313 *
314 * my $hfrom = $human{$from_unit};
315 * my $hto = $human{$to_unit};
316 * my $hround = $human_round{$round};
317 * print "/", "** \@brief Convert $hfrom to $hto. $ret32 bits. $hround.\n";
318 * print " *\n";
319 * print " * Converts time values in $hfrom to $hto.\n";
320 * print " * Computes result in $sz bit precision.\n";
321 * if ($round eq "ceil") {
322 * print " * Rounds up to the next highest output unit.\n";
323 * } elsif ($round eq "near") {
324 * print " * Rounds to the nearest output unit.\n";
325 * } else {
326 * print " * Truncates to the next lowest output unit.\n";
327 * }
328 * print " *\n";
329 * print " * \@warning Generated. Do not edit. See above.\n";
330 * print " *\n";
331 * print " * \@param t Source time in $hfrom. uint64_t\n";
332 * print " *\n";
333 * print " * \@return The converted time value in $hto. $type\n";
334 * print " *", "/\n";
335 * print "#define $sym(t) \\\n";
336 * print "\tz_tmcvt_$ret32(t, Z_HZ_$from_unit, Z_HZ_$to_unit,";
337 * print " $const_hz, $rup, $roff)\n";
338 * print "\n\n";
339 * }
340 * }
341 * }
342 * }
343 */
344
345/* Some more concise declarations to simplify the generator script and
346 * save bytes below
347 */
348#define Z_HZ_sec 1
349#define Z_HZ_ms 1000
350#define Z_HZ_us 1000000
351#define Z_HZ_ns 1000000000
352#define Z_HZ_cyc sys_clock_hw_cycles_per_sec()
353#define Z_HZ_ticks CONFIG_SYS_CLOCK_TICKS_PER_SEC
354#define Z_CCYC (!IS_ENABLED(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) && \
355 !IS_ENABLED(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE))
356
369#define k_sec_to_cyc_floor32(t) \
370 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, false)
371
372
385#define k_sec_to_cyc_floor64(t) \
386 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, false)
387
388
401#define k_sec_to_cyc_near32(t) \
402 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, true)
403
404
417#define k_sec_to_cyc_near64(t) \
418 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, false, true)
419
420
433#define k_sec_to_cyc_ceil32(t) \
434 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, true, false)
435
436
449#define k_sec_to_cyc_ceil64(t) \
450 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_cyc, Z_CCYC, true, false)
451
452
465#define k_sec_to_ticks_floor32(t) \
466 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_ticks, true, false, false)
467
468
481#define k_sec_to_ticks_floor64(t) \
482 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_ticks, true, false, false)
483
484
497#define k_sec_to_ticks_near32(t) \
498 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_ticks, true, false, true)
499
500
513#define k_sec_to_ticks_near64(t) \
514 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_ticks, true, false, true)
515
516
529#define k_sec_to_ticks_ceil32(t) \
530 z_tmcvt_32(t, Z_HZ_sec, Z_HZ_ticks, true, true, false)
531
532
545#define k_sec_to_ticks_ceil64(t) \
546 z_tmcvt_64(t, Z_HZ_sec, Z_HZ_ticks, true, true, false)
547
548
561#define k_ms_to_cyc_floor32(t) \
562 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, false)
563
564
577#define k_ms_to_cyc_floor64(t) \
578 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, false)
579
580
593#define k_ms_to_cyc_near32(t) \
594 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, true)
595
596
609#define k_ms_to_cyc_near64(t) \
610 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, false, true)
611
612
625#define k_ms_to_cyc_ceil32(t) \
626 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, true, false)
627
628
641#define k_ms_to_cyc_ceil64(t) \
642 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_cyc, Z_CCYC, true, false)
643
644
657#define k_ms_to_ticks_floor32(t) \
658 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, false, false)
659
660
673#define k_ms_to_ticks_floor64(t) \
674 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, false, false)
675
676
689#define k_ms_to_ticks_near32(t) \
690 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, false, true)
691
692
705#define k_ms_to_ticks_near64(t) \
706 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, false, true)
707
708
721#define k_ms_to_ticks_ceil32(t) \
722 z_tmcvt_32(t, Z_HZ_ms, Z_HZ_ticks, true, true, false)
723
724
737#define k_ms_to_ticks_ceil64(t) \
738 z_tmcvt_64(t, Z_HZ_ms, Z_HZ_ticks, true, true, false)
739
740
753#define k_us_to_cyc_floor32(t) \
754 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, false)
755
756
769#define k_us_to_cyc_floor64(t) \
770 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, false)
771
772
785#define k_us_to_cyc_near32(t) \
786 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, true)
787
788
801#define k_us_to_cyc_near64(t) \
802 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, false, true)
803
804
817#define k_us_to_cyc_ceil32(t) \
818 z_tmcvt_32(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, true, false)
819
820
833#define k_us_to_cyc_ceil64(t) \
834 z_tmcvt_64(t, Z_HZ_us, Z_HZ_cyc, Z_CCYC, true, false)
835
836
849#define k_us_to_ticks_floor32(t) \
850 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, false, false)
851
852
865#define k_us_to_ticks_floor64(t) \
866 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, false, false)
867
868
881#define k_us_to_ticks_near32(t) \
882 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, false, true)
883
884
897#define k_us_to_ticks_near64(t) \
898 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, false, true)
899
900
913#define k_us_to_ticks_ceil32(t) \
914 z_tmcvt_32(t, Z_HZ_us, Z_HZ_ticks, true, true, false)
915
916
929#define k_us_to_ticks_ceil64(t) \
930 z_tmcvt_64(t, Z_HZ_us, Z_HZ_ticks, true, true, false)
931
932
945#define k_ns_to_cyc_floor32(t) \
946 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, false)
947
948
961#define k_ns_to_cyc_floor64(t) \
962 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, false)
963
964
977#define k_ns_to_cyc_near32(t) \
978 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, true)
979
980
993#define k_ns_to_cyc_near64(t) \
994 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, false, true)
995
996
1009#define k_ns_to_cyc_ceil32(t) \
1010 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, true, false)
1011
1012
1025#define k_ns_to_cyc_ceil64(t) \
1026 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_cyc, Z_CCYC, true, false)
1027
1028
1040#define ns_to_cyc_at_hz_floor32(t, freq) \
1041 z_tmcvt_32(t, Z_HZ_ns, freq, false, false, false)
1042
1043
1055#define ns_to_cyc_at_hz_floor64(t, freq) \
1056 z_tmcvt_64(t, Z_HZ_ns, freq, false, false, false)
1057
1058
1070#define ns_to_cyc_at_hz_near32(t, freq) \
1071 z_tmcvt_32(t, Z_HZ_ns, freq, false, false, true)
1072
1073
1085#define ns_to_cyc_at_hz_near64(t, freq) \
1086 z_tmcvt_64(t, Z_HZ_ns, freq, false, false, true)
1087
1088
1100#define ns_to_cyc_at_hz_ceil32(t, freq) \
1101 z_tmcvt_32(t, Z_HZ_ns, freq, false, true, false)
1102
1103
1115#define ns_to_cyc_at_hz_ceil64(t, freq) \
1116 z_tmcvt_64(t, Z_HZ_ns, freq, false, true, false)
1117
1118
1131#define k_ns_to_ticks_floor32(t) \
1132 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, false, false)
1133
1134
1147#define k_ns_to_ticks_floor64(t) \
1148 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, false, false)
1149
1150
1163#define k_ns_to_ticks_near32(t) \
1164 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, false, true)
1165
1166
1179#define k_ns_to_ticks_near64(t) \
1180 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, false, true)
1181
1182
1195#define k_ns_to_ticks_ceil32(t) \
1196 z_tmcvt_32(t, Z_HZ_ns, Z_HZ_ticks, true, true, false)
1197
1198
1211#define k_ns_to_ticks_ceil64(t) \
1212 z_tmcvt_64(t, Z_HZ_ns, Z_HZ_ticks, true, true, false)
1213
1214
1227#define k_cyc_to_sec_floor32(t) \
1228 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, false)
1229
1230
1243#define k_cyc_to_sec_floor64(t) \
1244 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, false)
1245
1246
1259#define k_cyc_to_sec_near32(t) \
1260 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, true)
1261
1262
1275#define k_cyc_to_sec_near64(t) \
1276 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, false, true)
1277
1278
1291#define k_cyc_to_sec_ceil32(t) \
1292 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, true, false)
1293
1294
1307#define k_cyc_to_sec_ceil64(t) \
1308 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_sec, Z_CCYC, true, false)
1309
1310
1323#define k_cyc_to_ms_floor32(t) \
1324 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, false)
1325
1326
1339#define k_cyc_to_ms_floor64(t) \
1340 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, false)
1341
1342
1355#define k_cyc_to_ms_near32(t) \
1356 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, true)
1357
1358
1371#define k_cyc_to_ms_near64(t) \
1372 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, false, true)
1373
1374
1387#define k_cyc_to_ms_ceil32(t) \
1388 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, true, false)
1389
1390
1403#define k_cyc_to_ms_ceil64(t) \
1404 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ms, Z_CCYC, true, false)
1405
1406
1419#define k_cyc_to_us_floor32(t) \
1420 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, false)
1421
1422
1435#define k_cyc_to_us_floor64(t) \
1436 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, false)
1437
1438
1451#define k_cyc_to_us_near32(t) \
1452 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, true)
1453
1454
1467#define k_cyc_to_us_near64(t) \
1468 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, false, true)
1469
1470
1483#define k_cyc_to_us_ceil32(t) \
1484 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, true, false)
1485
1486
1499#define k_cyc_to_us_ceil64(t) \
1500 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_us, Z_CCYC, true, false)
1501
1502
1515#define k_cyc_to_ns_floor32(t) \
1516 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, false)
1517
1518
1531#define k_cyc_to_ns_floor64(t) \
1532 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, false)
1533
1534
1547#define k_cyc_to_ns_near32(t) \
1548 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, true)
1549
1550
1563#define k_cyc_to_ns_near64(t) \
1564 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, false, true)
1565
1566
1579#define k_cyc_to_ns_ceil32(t) \
1580 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, true, false)
1581
1582
1595#define k_cyc_to_ns_ceil64(t) \
1596 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ns, Z_CCYC, true, false)
1597
1598
1611#define k_cyc_to_ticks_floor32(t) \
1612 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, false)
1613
1614
1627#define k_cyc_to_ticks_floor64(t) \
1628 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, false)
1629
1630
1643#define k_cyc_to_ticks_near32(t) \
1644 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, true)
1645
1646
1659#define k_cyc_to_ticks_near64(t) \
1660 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, false, true)
1661
1662
1675#define k_cyc_to_ticks_ceil32(t) \
1676 z_tmcvt_32(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, true, false)
1677
1678
1691#define k_cyc_to_ticks_ceil64(t) \
1692 z_tmcvt_64(t, Z_HZ_cyc, Z_HZ_ticks, Z_CCYC, true, false)
1693
1694
1707#define k_ticks_to_sec_floor32(t) \
1708 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_sec, true, false, false)
1709
1710
1723#define k_ticks_to_sec_floor64(t) \
1724 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_sec, true, false, false)
1725
1726
1739#define k_ticks_to_sec_near32(t) \
1740 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_sec, true, false, true)
1741
1742
1755#define k_ticks_to_sec_near64(t) \
1756 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_sec, true, false, true)
1757
1758
1771#define k_ticks_to_sec_ceil32(t) \
1772 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_sec, true, true, false)
1773
1774
1787#define k_ticks_to_sec_ceil64(t) \
1788 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_sec, true, true, false)
1789
1790
1803#define k_ticks_to_ms_floor32(t) \
1804 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, false, false)
1805
1806
1819#define k_ticks_to_ms_floor64(t) \
1820 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, false, false)
1821
1822
1835#define k_ticks_to_ms_near32(t) \
1836 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, false, true)
1837
1838
1851#define k_ticks_to_ms_near64(t) \
1852 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, false, true)
1853
1854
1867#define k_ticks_to_ms_ceil32(t) \
1868 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ms, true, true, false)
1869
1870
1883#define k_ticks_to_ms_ceil64(t) \
1884 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ms, true, true, false)
1885
1886
1899#define k_ticks_to_us_floor32(t) \
1900 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, false, false)
1901
1902
1915#define k_ticks_to_us_floor64(t) \
1916 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, false, false)
1917
1918
1931#define k_ticks_to_us_near32(t) \
1932 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, false, true)
1933
1934
1947#define k_ticks_to_us_near64(t) \
1948 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, false, true)
1949
1950
1963#define k_ticks_to_us_ceil32(t) \
1964 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_us, true, true, false)
1965
1966
1979#define k_ticks_to_us_ceil64(t) \
1980 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_us, true, true, false)
1981
1982
1995#define k_ticks_to_ns_floor32(t) \
1996 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, false, false)
1997
1998
2011#define k_ticks_to_ns_floor64(t) \
2012 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, false, false)
2013
2014
2027#define k_ticks_to_ns_near32(t) \
2028 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, false, true)
2029
2030
2043#define k_ticks_to_ns_near64(t) \
2044 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, false, true)
2045
2046
2059#define k_ticks_to_ns_ceil32(t) \
2060 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_ns, true, true, false)
2061
2062
2075#define k_ticks_to_ns_ceil64(t) \
2076 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_ns, true, true, false)
2077
2078
2091#define k_ticks_to_cyc_floor32(t) \
2092 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, false)
2093
2094
2107#define k_ticks_to_cyc_floor64(t) \
2108 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, false)
2109
2110
2123#define k_ticks_to_cyc_near32(t) \
2124 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, true)
2125
2126
2139#define k_ticks_to_cyc_near64(t) \
2140 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, false, true)
2141
2142
2155#define k_ticks_to_cyc_ceil32(t) \
2156 z_tmcvt_32(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, true, false)
2157
2158
2171#define k_ticks_to_cyc_ceil64(t) \
2172 z_tmcvt_64(t, Z_HZ_ticks, Z_HZ_cyc, Z_CCYC, true, false)
2173
2174#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
2175 defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
2176#include <zephyr/syscalls/time_units.h>
2177#endif
2178
2179#undef TIME_CONSTEXPR
2180
2184
2185#ifdef __cplusplus
2186} /* extern "C" */
2187#endif
2188
2189#endif /* ZEPHYR_INCLUDE_SYS_TIME_UNITS_H_ */
Misc utilities.
Macros to abstract toolchain specific capabilities.