Skip to content

Commit a6067a3

Browse files
pabigotioannisg
authored andcommitted
kernel: reimplement k_uptime_get_32()
The current implementation does not return the low 32 bits of k_uptime_get() as suggested by it's documentation; it returns the number of milliseconds represented by the low 32-bits of the underlying system clock. The truncation before translation results in discontinuities at every point where the system clock increments bit 33. Reimplement it using the full-precision value, and update the documentation to note that this variant has little value for long-running applications. Closes #18739. Signed-off-by: Peter Bigot <[email protected]>
1 parent 64841fb commit a6067a3

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

include/kernel.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -1729,13 +1729,14 @@ __deprecated static inline void k_disable_sys_clock_always_on(void)
17291729
/**
17301730
* @brief Get system uptime (32-bit version).
17311731
*
1732-
* This routine returns the lower 32-bits of the elapsed time since the system
1733-
* booted, in milliseconds.
1732+
* This routine returns the lower 32 bits of the system uptime in
1733+
* milliseconds.
17341734
*
1735-
* This routine can be more efficient than k_uptime_get(), as it reduces the
1736-
* need for interrupt locking and 64-bit math. However, the 32-bit result
1737-
* cannot hold a system uptime time larger than approximately 50 days, so the
1738-
* caller must handle possible rollovers.
1735+
* Because correct conversion requires full precision of the system
1736+
* clock there is no benefit to using this over k_uptime_get() unless
1737+
* you know the application will never run long enough for the system
1738+
* clock to approach 2^32 ticks. Calls to this function may involve
1739+
* interrupt blocking and 64-bit math.
17391740
*
17401741
* @note
17411742
* @rst
@@ -1744,9 +1745,12 @@ __deprecated static inline void k_disable_sys_clock_always_on(void)
17441745
* :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
17451746
* @endrst
17461747
*
1747-
* @return Current uptime in milliseconds.
1748+
* @return The low 32 bits of the current uptime, in milliseconds.
17481749
*/
1749-
__syscall u32_t k_uptime_get_32(void);
1750+
static inline u32_t k_uptime_get_32(void)
1751+
{
1752+
return (u32_t)k_uptime_get();
1753+
}
17501754

17511755
/**
17521756
* @brief Get elapsed time.

kernel/timeout.c

-12
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,6 @@ u32_t z_tick_get_32(void)
229229
#endif
230230
}
231231

232-
u32_t z_impl_k_uptime_get_32(void)
233-
{
234-
return __ticks_to_ms(z_tick_get_32());
235-
}
236-
237-
#ifdef CONFIG_USERSPACE
238-
Z_SYSCALL_HANDLER(k_uptime_get_32)
239-
{
240-
return z_impl_k_uptime_get_32();
241-
}
242-
#endif
243-
244232
s64_t z_impl_k_uptime_get(void)
245233
{
246234
return __ticks_to_ms(z_tick_get());

0 commit comments

Comments
 (0)