From ba5ddc189ebbbb24924d8c76ad442d430afb9ba0 Mon Sep 17 00:00:00 2001 From: Benjamin Walsh Date: Wed, 21 Sep 2016 16:01:22 -0400 Subject: [PATCH] unified: implement k_uptime_{get,delta}() Simple conversion from ticks for now. Change-Id: Ib81fc738d45641a6a3a88d2adec1f3eb861f3f97 Signed-off-by: Benjamin Walsh --- include/kernel.h | 49 ++++++++++++++++++++++++++++++++++++++ kernel/unified/sys_clock.c | 26 ++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/kernel.h b/include/kernel.h index f77584ac367..8cdaa85a0f1 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -318,8 +318,57 @@ extern void k_timer_restart(struct k_timer *timer, int32_t duration, extern void k_timer_stop(struct k_timer *timer); extern int k_timer_test(struct k_timer *timer, void **data, int wait); extern int32_t k_timer_remaining_get(struct k_timer *timer); + + +/** + * @brief Get the time elapsed since the system booted (uptime) + * + * @return The current uptime of the system in ms + */ + extern int64_t k_uptime_get(void); + +/** + * @brief Get the lower 32-bit of time elapsed since the system booted (uptime) + * + * This function is potentially less onerous in both the time it takes to + * execute, the interrupt latency it introduces and the amount of 64-bit math + * it requires than k_uptime_get(), but it only provides an uptime value of + * 32-bits. The user must handle possible rollovers/spillovers. + * + * At a rate of increment of 1000 per second, it rolls over approximately every + * 50 days. + * + * @return The current uptime of the system in ms + */ + +extern uint32_t k_uptime_get_32(void); + +/** + * @brief Get the difference between a reference time and the current uptime + * + * @param reftime A pointer to a reference time. It is updated with the current + * uptime upon return. + * + * @return The delta between the reference time and the current uptime. + */ + extern int64_t k_uptime_delta(int64_t *reftime); + +/** + * @brief Get the difference between a reference time and the current uptime + * + * The 32-bit version of k_uptime_delta(). It has the same perks and issues as + * k_uptime_get_32(). + * + * @param reftime A pointer to a reference time. It is updated with the current + * uptime upon return. + * + * @return The delta between the reference time and the current uptime. + */ + +extern uint32_t k_uptime_delta_32(int64_t *reftime); + extern bool k_timer_pool_is_empty(void); extern uint32_t k_cycle_get_32(void); diff --git a/kernel/unified/sys_clock.c b/kernel/unified/sys_clock.c index 4af23602961..f70a169b644 100644 --- a/kernel/unified/sys_clock.c +++ b/kernel/unified/sys_clock.c @@ -56,6 +56,11 @@ uint32_t sys_tick_get_32(void) return (uint32_t)_sys_clock_tick_count; } +uint32_t k_uptime_get_32(void) +{ + return __ticks_to_ms(sys_tick_get_32()); +} + /** * * @brief Return the current system tick count @@ -79,6 +84,11 @@ int64_t sys_tick_get(void) return tmp_sys_clock_tick_count; } +int64_t k_uptime_get(void) +{ + return __ticks_to_ms(sys_tick_get()); +} + /** * * @brief Return number of ticks since a reference time @@ -145,6 +155,22 @@ uint32_t sys_tick_delta_32(int64_t *reftime) return (uint32_t)_nano_tick_delta(reftime); } +int64_t k_uptime_delta(int64_t *reftime) +{ + int64_t uptime, delta; + + uptime = k_uptime_get(); + delta = uptime - *reftime; + *reftime = uptime; + + return delta; +} + +uint32_t k_uptime_delta_32(int64_t *reftime) +{ + return (uint32_t)k_uptime_delta(reftime); +} + /* handle the expired timeouts in the nano timeout queue */ #if defined(CONFIG_NANO_TIMEOUTS) || defined(CONFIG_NANO_TIMERS)