Skip to content

Commit 1ce91d5

Browse files
committed
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 4019d67 commit 1ce91d5

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
@@ -1676,22 +1676,26 @@ void k_disable_sys_clock_always_on(void);
16761676
/**
16771677
* @brief Get system uptime (32-bit version).
16781678
*
1679-
* This routine returns the lower 32-bits of the elapsed time since the system
1680-
* booted, in milliseconds.
1679+
* This routine returns the lower 32 bits of the system uptime in
1680+
* milliseconds.
16811681
*
1682-
* This routine can be more efficient than k_uptime_get(), as it reduces the
1683-
* need for interrupt locking and 64-bit math. However, the 32-bit result
1684-
* cannot hold a system uptime time larger than approximately 50 days, so the
1685-
* caller must handle possible rollovers.
1682+
* Because correct conversion requires full precision of the system
1683+
* clock there is no benefit to using this over k_uptime_get() unless
1684+
* you know the application will never run long enough for the system
1685+
* clock to approach 2^32 ticks. Calls to this function may involve
1686+
* interrupt blocking and 64-bit math.
16861687
*
16871688
* @note While this function returns time in milliseconds, it does not mean it
16881689
* has millisecond resolution. The actual resolution depends on
16891690
* :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the default
16901691
* setting of 100, system time is updated in increments of 10ms.
16911692
*
1692-
* @return Current uptime in milliseconds.
1693+
* @return The low 32 bits of the current uptime, in milliseconds.
16931694
*/
1694-
__syscall u32_t k_uptime_get_32(void);
1695+
static inline u32_t k_uptime_get_32(void)
1696+
{
1697+
return (u32_t)k_uptime_get();
1698+
}
16951699

16961700
/**
16971701
* @brief Get elapsed time.

kernel/timeout.c

-12
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,6 @@ u32_t z_tick_get_32(void)
234234
#endif
235235
}
236236

237-
u32_t z_impl_k_uptime_get_32(void)
238-
{
239-
return __ticks_to_ms(z_tick_get_32());
240-
}
241-
242-
#ifdef CONFIG_USERSPACE
243-
Z_SYSCALL_HANDLER(k_uptime_get_32)
244-
{
245-
return z_impl_k_uptime_get_32();
246-
}
247-
#endif
248-
249237
s64_t z_impl_k_uptime_get(void)
250238
{
251239
return __ticks_to_ms(z_tick_get());

0 commit comments

Comments
 (0)