1 /*
2  * Copyright (c) 2014-2015 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Variables needed for system clock
10  *
11  *
12  * Declare variables used by both system timer device driver and kernel
13  * components that use timer functionality.
14  */
15 
16 #ifndef ZEPHYR_INCLUDE_SYS_CLOCK_H_
17 #define ZEPHYR_INCLUDE_SYS_CLOCK_H_
18 
19 #include <zephyr/sys/util.h>
20 #include <zephyr/sys/dlist.h>
21 
22 #include <zephyr/toolchain.h>
23 #include <zephyr/types.h>
24 
25 #include <zephyr/sys/time_units.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /**
32  * @addtogroup clock_apis
33  * @{
34  */
35 
36 /**
37  * @brief Tick precision used in timeout APIs
38  *
39  * This type defines the word size of the timeout values used in
40  * k_timeout_t objects, and thus defines an upper bound on maximum
41  * timeout length (or equivalently minimum tick duration).  Note that
42  * this does not affect the size of the system uptime counter, which
43  * is always a 64 bit count of ticks.
44  */
45 #ifdef CONFIG_TIMEOUT_64BIT
46 typedef int64_t k_ticks_t;
47 #else
48 typedef uint32_t k_ticks_t;
49 #endif
50 
51 #define K_TICKS_FOREVER ((k_ticks_t) -1)
52 
53 /**
54  * @brief Kernel timeout type
55  *
56  * Timeout arguments presented to kernel APIs are stored in this
57  * opaque type, which is capable of representing times in various
58  * formats and units.  It should be constructed from application data
59  * using one of the macros defined for this purpose (e.g. `K_MSEC()`,
60  * `K_TIMEOUT_ABS_TICKS()`, etc...), or be one of the two constants
61  * K_NO_WAIT or K_FOREVER.  Applications should not inspect the
62  * internal data once constructed.  Timeout values may be compared for
63  * equality with the `K_TIMEOUT_EQ()` macro.
64  */
65 typedef struct {
66 	k_ticks_t ticks;
67 } k_timeout_t;
68 
69 /**
70  * @brief Compare timeouts for equality
71  *
72  * The k_timeout_t object is an opaque struct that should not be
73  * inspected by application code.  This macro exists so that users can
74  * test timeout objects for equality with known constants
75  * (e.g. K_NO_WAIT and K_FOREVER) when implementing their own APIs in
76  * terms of Zephyr timeout constants.
77  *
78  * @return True if the timeout objects are identical
79  */
80 #define K_TIMEOUT_EQ(a, b) ((a).ticks == (b).ticks)
81 
82 /** number of nanoseconds per micorsecond */
83 #define NSEC_PER_USEC 1000U
84 
85 /** number of nanoseconds per millisecond */
86 #define NSEC_PER_MSEC 1000000U
87 
88 /** number of microseconds per millisecond */
89 #define USEC_PER_MSEC 1000U
90 
91 /** number of milliseconds per second */
92 #define MSEC_PER_SEC 1000U
93 
94 /** number of seconds per minute */
95 #define SEC_PER_MIN 60U
96 
97 /** number of minutes per hour */
98 #define MIN_PER_HOUR 60U
99 
100 /** number of hours per day */
101 #define HOUR_PER_DAY 24U
102 
103 /** number of microseconds per second */
104 #define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC))
105 
106 /** number of nanoseconds per second */
107 #define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC))
108 
109 /** @} */
110 
111 /** @cond INTERNAL_HIDDEN */
112 #define Z_TIMEOUT_NO_WAIT ((k_timeout_t) {0})
113 #if defined(__cplusplus) && ((__cplusplus - 0) < 202002L)
114 #define Z_TIMEOUT_TICKS(t) ((k_timeout_t) { (t) })
115 #else
116 #define Z_TIMEOUT_TICKS(t) ((k_timeout_t) { .ticks = (t) })
117 #endif
118 #define Z_FOREVER Z_TIMEOUT_TICKS(K_TICKS_FOREVER)
119 
120 #ifdef CONFIG_TIMEOUT_64BIT
121 # define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ms_to_ticks_ceil64(MAX(t, 0)))
122 # define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS((k_ticks_t)k_us_to_ticks_ceil64(MAX(t, 0)))
123 # define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ns_to_ticks_ceil64(MAX(t, 0)))
124 # define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS((k_ticks_t)k_cyc_to_ticks_ceil64(MAX(t, 0)))
125 # define Z_TIMEOUT_MS_TICKS(t) ((k_ticks_t)k_ms_to_ticks_ceil64(MAX(t, 0)))
126 #else
127 # define Z_TIMEOUT_MS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ms_to_ticks_ceil32(MAX(t, 0)))
128 # define Z_TIMEOUT_US(t) Z_TIMEOUT_TICKS((k_ticks_t)k_us_to_ticks_ceil32(MAX(t, 0)))
129 # define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS((k_ticks_t)k_ns_to_ticks_ceil32(MAX(t, 0)))
130 # define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS((k_ticks_t)k_cyc_to_ticks_ceil32(MAX(t, 0)))
131 # define Z_TIMEOUT_MS_TICKS(t) ((k_ticks_t)k_ms_to_ticks_ceil32(MAX(t, 0)))
132 #endif
133 
134 /* Converts between absolute timeout expiration values (packed into
135  * the negative space below K_TICKS_FOREVER) and (non-negative) delta
136  * timeout values.  If the result of Z_TICK_ABS(t) is >= 0, then the
137  * value was an absolute timeout with the returned expiration time.
138  * Note that this macro is bidirectional: Z_TICK_ABS(Z_TICK_ABS(t)) ==
139  * t for all inputs, and that the representation of K_TICKS_FOREVER is
140  * the same value in both spaces!  Clever, huh?
141  */
142 #define Z_TICK_ABS(t) (K_TICKS_FOREVER - 1 - (t))
143 
144 /* added tick needed to account for tick in progress */
145 #define _TICK_ALIGN 1
146 
147 /** @endcond */
148 
149 #if defined(CONFIG_SYS_CLOCK_EXISTS) && \
150 	(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0)
151 #error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!"
152 #endif
153 
154 
155 /* kernel clocks */
156 
157 /*
158  * We default to using 64-bit intermediates in timescale conversions,
159  * but if the HW timer cycles/sec, ticks/sec and ms/sec are all known
160  * to be nicely related, then we can cheat with 32 bits instead.
161  */
162 /**
163  * @addtogroup clock_apis
164  * @{
165  */
166 
167 #ifdef CONFIG_SYS_CLOCK_EXISTS
168 
169 #if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
170 	(MSEC_PER_SEC % CONFIG_SYS_CLOCK_TICKS_PER_SEC) || \
171 	(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC % CONFIG_SYS_CLOCK_TICKS_PER_SEC)
172 #define _NEED_PRECISE_TICK_MS_CONVERSION
173 #endif
174 
175 #endif
176 
177 /**
178  * SYS_CLOCK_HW_CYCLES_TO_NS_AVG converts CPU clock cycles to nanoseconds
179  * and calculates the average cycle time
180  */
181 #define SYS_CLOCK_HW_CYCLES_TO_NS_AVG(X, NCYCLES) \
182 	(uint32_t)(k_cyc_to_ns_floor64(X) / NCYCLES)
183 
184 /**
185  *
186  * @brief Return the lower part of the current system tick count
187  *
188  * @return the current system tick count
189  *
190  */
191 uint32_t sys_clock_tick_get_32(void);
192 
193 /**
194  *
195  * @brief Return the current system tick count
196  *
197  * @return the current system tick count
198  *
199  */
200 int64_t sys_clock_tick_get(void);
201 
202 #ifndef CONFIG_SYS_CLOCK_EXISTS
203 #define sys_clock_tick_get() (0)
204 #define sys_clock_tick_get_32() (0)
205 #endif
206 
207 #ifdef CONFIG_SYS_CLOCK_EXISTS
208 
209 /**
210  * @brief Kernel timepoint type
211  *
212  * Absolute timepoints are stored in this opaque type.
213  * It is best not to inspect its content directly.
214  *
215  * @see sys_timepoint_calc()
216  * @see sys_timepoint_timeout()
217  * @see sys_timepoint_expired()
218  */
219 typedef struct { uint64_t tick; } k_timepoint_t;
220 
221 /**
222  * @brief Calculate a timepoint value
223  *
224  * Returns a timepoint corresponding to the expiration (relative to an
225  * unlocked "now"!) of a timeout object.  When used correctly, this should
226  * be called once, synchronously with the user passing a new timeout value.
227  * It should not be used iteratively to adjust a timeout (see
228  * `sys_timepoint_timeout()` for that purpose).
229  *
230  * @param timeout Timeout value relative to current time (may also be
231  *                `K_FOREVER` or `K_NO_WAIT`).
232  * @retval Timepoint value corresponding to given timeout
233  *
234  * @see sys_timepoint_timeout()
235  * @see sys_timepoint_expired()
236  */
237 k_timepoint_t sys_timepoint_calc(k_timeout_t timeout);
238 
239 /**
240  * @brief Remaining time to given timepoint
241  *
242  * Returns the timeout interval between current time and provided timepoint.
243  * If the timepoint is now in the past or if it was created with `K_NO_WAIT`
244  * then `K_NO_WAIT` is returned. If it was created with `K_FOREVER` then
245  * `K_FOREVER` is returned.
246  *
247  * @param timepoint Timepoint for which a timeout value is wanted.
248  * @retval Corresponding timeout value.
249  *
250  * @see sys_timepoint_calc()
251  */
252 k_timeout_t sys_timepoint_timeout(k_timepoint_t timepoint);
253 
254 /**
255  * @brief Provided for backward compatibility.
256  *
257  * This is deprecated. Consider `sys_timepoint_calc()` instead.
258  *
259  * @see sys_timepoint_calc()
260  */
261 __deprecated
sys_clock_timeout_end_calc(k_timeout_t timeout)262 static inline uint64_t sys_clock_timeout_end_calc(k_timeout_t timeout)
263 {
264 	k_timepoint_t tp = sys_timepoint_calc(timeout);
265 
266 	return tp.tick;
267 }
268 
269 /**
270  * @brief Compare two timepoint values.
271  *
272  * This function is used to compare two timepoint values.
273  *
274  * @param a Timepoint to compare
275  * @param b Timepoint to compare against.
276  * @return zero if both timepoints are the same. Negative value if timepoint @a a is before
277  * timepoint @a b, positive otherwise.
278  */
sys_timepoint_cmp(k_timepoint_t a,k_timepoint_t b)279 static inline int sys_timepoint_cmp(k_timepoint_t a, k_timepoint_t b)
280 {
281 	if (a.tick == b.tick) {
282 		return 0;
283 	}
284 	return a.tick < b.tick ? -1 : 1;
285 }
286 
287 #else
288 
289 /*
290  * When timers are configured out, timepoints can't relate to anything.
291  * The best we can do is to preserve whether or not they are derived from
292  * K_NO_WAIT. Anything else will translate back to K_FOREVER.
293  */
294 typedef struct { bool wait; } k_timepoint_t;
295 
sys_timepoint_calc(k_timeout_t timeout)296 static inline k_timepoint_t sys_timepoint_calc(k_timeout_t timeout)
297 {
298 	k_timepoint_t timepoint;
299 
300 	timepoint.wait = !K_TIMEOUT_EQ(timeout, Z_TIMEOUT_NO_WAIT);
301 	return timepoint;
302 }
303 
sys_timepoint_timeout(k_timepoint_t timepoint)304 static inline k_timeout_t sys_timepoint_timeout(k_timepoint_t timepoint)
305 {
306 	return timepoint.wait ? Z_FOREVER : Z_TIMEOUT_NO_WAIT;
307 }
308 
sys_timepoint_cmp(k_timepoint_t a,k_timepoint_t b)309 static inline int sys_timepoint_cmp(k_timepoint_t a, k_timepoint_t b)
310 {
311 	if (a.wait == b.wait) {
312 		return 0;
313 	}
314 	return b.wait ? -1 : 1;
315 }
316 
317 #endif
318 
319 /**
320  * @brief Indicates if timepoint is expired
321  *
322  * @param timepoint Timepoint to evaluate
323  * @retval true if the timepoint is in the past, false otherwise
324  *
325  * @see sys_timepoint_calc()
326  */
sys_timepoint_expired(k_timepoint_t timepoint)327 static inline bool sys_timepoint_expired(k_timepoint_t timepoint)
328 {
329 	return K_TIMEOUT_EQ(sys_timepoint_timeout(timepoint), Z_TIMEOUT_NO_WAIT);
330 }
331 
332 /** @} */
333 
334 #ifdef __cplusplus
335 }
336 #endif
337 
338 #endif /* ZEPHYR_INCLUDE_SYS_CLOCK_H_ */
339