Skip to content

Commit a1466cf

Browse files
aescolarkartben
authored andcommitted
sys: timeutil: Move and rename K_TICKS macros
These macros depend on time.h (which is not included in clock.h) and are not really kernel related, but translation from the kernel tick representation into time_spec values. Let's place them in timeutils with their related macros/functions. Signed-off-by: Alberto Escolar Piedras <[email protected]>
1 parent 551a7ab commit a1466cf

File tree

3 files changed

+126
-120
lines changed

3 files changed

+126
-120
lines changed

include/zephyr/sys/clock.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -166,49 +166,12 @@ typedef struct {
166166
/* added tick needed to account for tick in progress */
167167
#define _TICK_ALIGN 1
168168

169-
/* Maximum and minimum value TIME_T can hold */
170-
#define SYS_TIME_T_MAX ((((time_t)1 << (8 * sizeof(time_t) - 2)) - 1) * 2 + 1)
171-
#define SYS_TIME_T_MIN (-SYS_TIME_T_MAX - 1)
172-
173-
/* Converts ticks to seconds, discarding any fractional seconds */
174-
#define K_TICKS_TO_SECS(ticks) \
175-
(((uint64_t)(ticks) >= (uint64_t)K_TICKS_FOREVER) ? SYS_TIME_T_MAX \
176-
: k_ticks_to_sec_floor64(ticks))
177-
178-
/* Converts ticks to nanoseconds, modulo NSEC_PER_SEC */
179-
#define K_TICKS_TO_NSECS(ticks) \
180-
(((uint64_t)(ticks) >= (uint64_t)K_TICKS_FOREVER) \
181-
? (NSEC_PER_SEC - 1) \
182-
: k_ticks_to_ns_floor32((uint64_t)(ticks) % CONFIG_SYS_CLOCK_TICKS_PER_SEC))
183-
184-
/* Define a timespec */
185-
#define K_TIMESPEC(sec, nsec) \
186-
((struct timespec){ \
187-
.tv_sec = (time_t)CLAMP((int64_t)(sec), SYS_TIME_T_MIN, SYS_TIME_T_MAX), \
188-
.tv_nsec = (long)(nsec), \
189-
})
190-
191-
/* Initialize a struct timespec object from a tick count */
192-
#define K_TICKS_TO_TIMESPEC(ticks) K_TIMESPEC(K_TICKS_TO_SECS(ticks), K_TICKS_TO_NSECS(ticks))
193-
194169
/* The minimum duration in ticks strictly greater than that of K_NO_WAIT */
195170
#define K_TICK_MIN ((k_ticks_t)1)
196171

197172
/* The maximum duration in ticks strictly and semantically "less than" K_FOREVER */
198173
#define K_TICK_MAX ((k_ticks_t)(IS_ENABLED(CONFIG_TIMEOUT_64BIT) ? INT64_MAX : UINT32_MAX - 1))
199174

200-
/* The semantic equivalent of K_NO_WAIT but expressed as a timespec object*/
201-
#define K_TIMESPEC_NO_WAIT K_TICKS_TO_TIMESPEC(0)
202-
203-
/* The semantic equivalent of K_TICK_MIN but expressed as a timespec object */
204-
#define K_TIMESPEC_MIN K_TICKS_TO_TIMESPEC(K_TICK_MIN)
205-
206-
/* The semantic equivalent of K_TICK_MAX but expressed as a timespec object */
207-
#define K_TIMESPEC_MAX K_TICKS_TO_TIMESPEC(K_TICK_MAX)
208-
209-
/* The semantic equivalent of K_FOREVER but expressed as a timespec object*/
210-
#define K_TIMESPEC_FOREVER K_TIMESPEC(SYS_TIME_T_MAX, NSEC_PER_SEC - 1)
211-
212175
/** @endcond */
213176

214177
#ifndef CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME

include/zephyr/sys/timeutil.h

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,44 @@
4040
extern "C" {
4141
#endif
4242

43+
/* Maximum and minimum value TIME_T can hold */
44+
#define SYS_TIME_T_MAX ((((time_t)1 << (8 * sizeof(time_t) - 2)) - 1) * 2 + 1)
45+
#define SYS_TIME_T_MIN (-SYS_TIME_T_MAX - 1)
46+
47+
/* Converts ticks to seconds, discarding any fractional seconds */
48+
#define SYS_TICKS_TO_SECS(ticks) \
49+
(((uint64_t)(ticks) >= (uint64_t)K_TICKS_FOREVER) ? SYS_TIME_T_MAX \
50+
: k_ticks_to_sec_floor64(ticks))
51+
52+
/* Converts ticks to nanoseconds, modulo NSEC_PER_SEC */
53+
#define SYS_TICKS_TO_NSECS(ticks) \
54+
(((uint64_t)(ticks) >= (uint64_t)K_TICKS_FOREVER) \
55+
? (NSEC_PER_SEC - 1) \
56+
: k_ticks_to_ns_floor32((uint64_t)(ticks) % CONFIG_SYS_CLOCK_TICKS_PER_SEC))
57+
58+
/* Define a timespec */
59+
#define SYS_TIMESPEC(sec, nsec) \
60+
((struct timespec){ \
61+
.tv_sec = (time_t)CLAMP((int64_t)(sec), SYS_TIME_T_MIN, SYS_TIME_T_MAX), \
62+
.tv_nsec = (long)(nsec), \
63+
})
64+
65+
/* Initialize a struct timespec object from a tick count */
66+
#define SYS_TICKS_TO_TIMESPEC(ticks) SYS_TIMESPEC(SYS_TICKS_TO_SECS(ticks), \
67+
SYS_TICKS_TO_NSECS(ticks))
68+
69+
/* The semantic equivalent of K_NO_WAIT but expressed as a timespec object*/
70+
#define SYS_TIMESPEC_NO_WAIT SYS_TICKS_TO_TIMESPEC(0)
71+
72+
/* The semantic equivalent of K_TICK_MIN but expressed as a timespec object */
73+
#define SYS_TIMESPEC_MIN SYS_TICKS_TO_TIMESPEC(K_TICK_MIN)
74+
75+
/* The semantic equivalent of K_TICK_MAX but expressed as a timespec object */
76+
#define SYS_TIMESPEC_MAX SYS_TICKS_TO_TIMESPEC(K_TICK_MAX)
77+
78+
/* The semantic equivalent of K_FOREVER but expressed as a timespec object*/
79+
#define SYS_TIMESPEC_FOREVER SYS_TIMESPEC(SYS_TIME_T_MAX, NSEC_PER_SEC - 1)
80+
4381
/**
4482
* @defgroup timeutil_apis Time Utility APIs
4583
* @ingroup utilities
@@ -634,13 +672,13 @@ static inline void timespec_from_timeout(k_timeout_t timeout, struct timespec *t
634672
/* equivalent of K_FOREVER without including kernel.h */
635673
if (K_TIMEOUT_EQ(timeout, (k_timeout_t){K_TICKS_FOREVER})) {
636674
/* duration == K_TICKS_FOREVER ticks */
637-
*ts = K_TIMESPEC_FOREVER;
675+
*ts = SYS_TIMESPEC_FOREVER;
638676
/* equivalent of K_NO_WAIT without including kernel.h */
639677
} else if (K_TIMEOUT_EQ(timeout, (k_timeout_t){0})) {
640678
/* duration <= 0 ticks */
641-
*ts = K_TIMESPEC_NO_WAIT;
679+
*ts = SYS_TIMESPEC_NO_WAIT;
642680
} else {
643-
*ts = K_TICKS_TO_TIMESPEC(timeout.ticks);
681+
*ts = SYS_TICKS_TO_TIMESPEC(timeout.ticks);
644682
}
645683

646684
__ASSERT_NO_MSG(timespec_is_valid(ts));
@@ -681,7 +719,7 @@ static inline k_timeout_t timespec_to_timeout(const struct timespec *req, struct
681719

682720
__ASSERT_NO_MSG((req != NULL) && timespec_is_valid(req));
683721

684-
if (timespec_compare(req, &K_TIMESPEC_NO_WAIT) <= 0) {
722+
if (timespec_compare(req, &SYS_TIMESPEC_NO_WAIT) <= 0) {
685723
if (rem != NULL) {
686724
*rem = *req;
687725
}
@@ -690,16 +728,16 @@ static inline k_timeout_t timespec_to_timeout(const struct timespec *req, struct
690728
return timeout;
691729
}
692730

693-
if (timespec_compare(req, &K_TIMESPEC_FOREVER) == 0) {
731+
if (timespec_compare(req, &SYS_TIMESPEC_FOREVER) == 0) {
694732
if (rem != NULL) {
695-
*rem = K_TIMESPEC_NO_WAIT;
733+
*rem = SYS_TIMESPEC_NO_WAIT;
696734
}
697735
/* equivalent of K_FOREVER without including kernel.h */
698736
timeout.ticks = K_TICKS_FOREVER;
699737
return timeout;
700738
}
701739

702-
if (timespec_compare(req, &K_TIMESPEC_MAX) >= 0) {
740+
if (timespec_compare(req, &SYS_TIMESPEC_MAX) >= 0) {
703741
/* round down to align to max ticks */
704742
timeout.ticks = K_TICK_MAX;
705743
} else {

0 commit comments

Comments
 (0)