diff --git a/drivers/timer/legacy_api.h b/drivers/timer/legacy_api.h index de1990c76bc..84da6350a0f 100644 --- a/drivers/timer/legacy_api.h +++ b/drivers/timer/legacy_api.h @@ -1,10 +1,11 @@ #ifndef ZEPHYR_LEGACY_SET_TIME_H__ #define ZEPHYR_LEGACY_SET_TIME_H__ -/* Stub implementation of z_clock_set_timeout() in terms of the - * original APIs. Used by older timer drivers. Should be replaced. +/* Stub implementation of z_clock_set_timeout() and z_clock_elapsed() + * in terms of the original APIs. Used by older timer drivers. + * Should be replaced. * - * Yes, this "header" includes a function definition and must be + * Yes, this "header" includes function definitions and must be * included only once in a single compilation. */ @@ -20,7 +21,9 @@ extern u32_t _get_remaining_program_time(void); extern u32_t _get_elapsed_program_time(void); #endif -extern void z_clock_set_timeout(s32_t ticks, bool idle) +extern u64_t z_clock_uptime(void); + +void z_clock_set_timeout(s32_t ticks, bool idle) { #ifdef CONFIG_TICKLESS_KERNEL if (idle) { @@ -31,4 +34,24 @@ extern void z_clock_set_timeout(s32_t ticks, bool idle) #endif } +/* The old driver "now" API would return a full uptime value. The new + * one only requires the driver to track ticks since the last announce + * call. Implement the new call in terms of the old one on legacy + * drivers by keeping (yet another) uptime value locally. + */ +static u32_t driver_uptime; + +u32_t z_clock_elapsed(void) +{ + return (u32_t)(z_clock_uptime() - driver_uptime); +} + +static void wrapped_announce(s32_t ticks) +{ + driver_uptime += ticks; + z_clock_announce(ticks); +} + +#define z_clock_announce(t) wrapped_announce(t) + #endif /* ZEPHYR_LEGACY_SET_TIME_H__ */ diff --git a/drivers/timer/pulpino_timer.c b/drivers/timer/pulpino_timer.c index effa0da2e12..513479279ba 100644 --- a/drivers/timer/pulpino_timer.c +++ b/drivers/timer/pulpino_timer.c @@ -72,3 +72,8 @@ u32_t _timer_cycle_get_32(void) { return accumulated_cycle_count + timer->val; } + +u32_t z_clock_elapsed(void) +{ + return 0; +} diff --git a/include/drivers/system_timer.h b/include/drivers/system_timer.h index 04da471413d..48a13f51617 100644 --- a/include/drivers/system_timer.h +++ b/include/drivers/system_timer.h @@ -102,12 +102,14 @@ extern void z_clock_idle_exit(void); extern void z_clock_announce(s32_t ticks); /** - * @brief System uptime in ticks + * @brief Ticks elapsed since last z_clock_announce() call * - * Queries the clock driver for the current time elapsed since system - * bootup in ticks. + * Queries the clock driver for the current time elapsed since the + * last call to z_clock_announce() was made. The kernel will call + * this with appropriate locking, the driver needs only provide an + * instantaneous answer. */ -extern u64_t z_clock_uptime(void); +extern u32_t z_clock_elapsed(void); #ifdef __cplusplus } diff --git a/kernel/sys_clock.c b/kernel/sys_clock.c index 9e5900b246c..5c7e5f3b76d 100644 --- a/kernel/sys_clock.c +++ b/kernel/sys_clock.c @@ -31,6 +31,8 @@ int z_clock_hw_cycles_per_sec; #endif #endif +extern u64_t z_clock_uptime(void); + /* Note that this value is 64 bits, and thus non-atomic on almost all * Zephyr archtictures. And of course it's routinely updated inside * timer interrupts. Access to it must be locked.