From 0d1228af36dc7b2ce7e3cc1655d99302d768049d Mon Sep 17 00:00:00 2001 From: Andy Ross Date: Wed, 19 Sep 2018 09:35:43 -0700 Subject: [PATCH] kernel.h: Header hygine, move clock/timer handling The kernel.h file had a bunch of internal APIs for timeout/clock handling mixed in. Move these to sys_clock.h, which it always included (in a weird location, so move THAT to kernel_includes.h with everything else). Signed-off-by: Andy Ross --- include/kernel.h | 95 --------------------------- include/kernel_includes.h | 1 + include/sys_clock.h | 135 ++++++++++++++++++++++++++++++++------ 3 files changed, 115 insertions(+), 116 deletions(-) diff --git a/include/kernel.h b/include/kernel.h index 5af00d69533..8294b259ec6 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -355,21 +355,6 @@ struct __packed _k_thread_stack_element { }; typedef struct _k_thread_stack_element k_thread_stack_t; -/* timeouts */ - -struct _timeout; -typedef void (*_timeout_func_t)(struct _timeout *t); - -struct _timeout { - sys_dnode_t node; - struct k_thread *thread; - sys_dlist_t *wait_q; - s32_t delta_ticks_from_prev; - _timeout_func_t func; -}; - -extern s32_t _timeout_remaining_get(struct _timeout *timeout); - /** * @typedef k_thread_entry_t * @brief Thread entry point function type. @@ -1235,11 +1220,6 @@ __syscall void k_thread_name_set(k_tid_t thread_id, const char *value); */ __syscall const char *k_thread_name_get(k_tid_t thread_id); -/** - * @} - */ -#include - /** * @addtogroup clock_apis * @{ @@ -1321,81 +1301,6 @@ __syscall const char *k_thread_name_get(k_tid_t thread_id); * @cond INTERNAL_HIDDEN */ -/* kernel clocks */ - -#ifdef CONFIG_SYS_CLOCK_EXISTS - -/* - * If timer frequency is known at compile time, a simple (32-bit) - * tick <-> ms conversion could be used for some combinations of - * hardware timer frequency and tick rate. Otherwise precise - * (64-bit) calculations are used. - */ - -#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) -#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0 - #define _NEED_PRECISE_TICK_MS_CONVERSION -#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0 - #define _NON_OPTIMIZED_TICKS_PER_SEC -#endif -#endif - -#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \ - defined(_NON_OPTIMIZED_TICKS_PER_SEC) - #define _NEED_PRECISE_TICK_MS_CONVERSION -#endif -#endif - -static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms) -{ -#ifdef CONFIG_SYS_CLOCK_EXISTS - -#ifdef _NEED_PRECISE_TICK_MS_CONVERSION - /* use 64-bit math to keep precision */ - return (s32_t)ceiling_fraction( - (s64_t)ms * sys_clock_hw_cycles_per_sec, - ((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) / - sys_clock_ticks_per_sec); -#else - /* simple division keeps precision */ - s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec; - - return (s32_t)ceiling_fraction(ms, ms_per_tick); -#endif - -#else - __ASSERT(ms == 0, "ms not zero"); - return 0; -#endif -} - -static inline s64_t __ticks_to_ms(s64_t ticks) -{ -#ifdef CONFIG_SYS_CLOCK_EXISTS - -#ifdef _NEED_PRECISE_TICK_MS_CONVERSION - /* use 64-bit math to keep precision */ - return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec; -#else - /* simple multiplication keeps precision */ - u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec; - - return (u64_t)ticks * ms_per_tick; -#endif - -#else - __ASSERT(ticks == 0, "ticks not zero"); - return 0; -#endif -} - -/* added tick needed to account for tick in progress */ -#ifdef CONFIG_TICKLESS_KERNEL -#define _TICK_ALIGN 0 -#else -#define _TICK_ALIGN 1 -#endif - struct k_timer { /* * _timeout structure must be first here if we want to use diff --git a/include/kernel_includes.h b/include/kernel_includes.h index c701f1d3790..283a9f19412 100644 --- a/include/kernel_includes.h +++ b/include/kernel_includes.h @@ -33,5 +33,6 @@ #include #include #include +#include #endif /* ZEPHYR_INCLUDE_KERNEL_INCLUDES_H_ */ diff --git a/include/sys_clock.h b/include/sys_clock.h index 723ff2ae650..b9b0a6b0a69 100644 --- a/include/sys_clock.h +++ b/include/sys_clock.h @@ -16,6 +16,9 @@ #ifndef ZEPHYR_INCLUDE_SYS_CLOCK_H_ #define ZEPHYR_INCLUDE_SYS_CLOCK_H_ +#include +#include + #ifdef __cplusplus extern "C" { #endif @@ -23,11 +26,6 @@ extern "C" { #include #include -#if defined(CONFIG_SYS_CLOCK_EXISTS) && \ - (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0) -#error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!" -#endif - #ifdef CONFIG_TICKLESS_KERNEL #define sys_clock_ticks_per_sec \ (1000000 / (CONFIG_TICKLESS_KERNEL_TIME_UNIT_IN_MICRO_SECS)) @@ -43,6 +41,102 @@ extern int sys_clock_hw_cycles_per_sec; #define sys_clock_hw_cycles_per_sec CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC #endif +#if defined(CONFIG_SYS_CLOCK_EXISTS) && \ + (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0) +#error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!" +#endif + +/* number of nsec per usec */ +#define NSEC_PER_USEC 1000 + +/* number of microseconds per millisecond */ +#define USEC_PER_MSEC 1000 + +/* number of milliseconds per second */ +#define MSEC_PER_SEC 1000 + +/* number of microseconds per second */ +#define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC)) + +/* number of nanoseconds per second */ +#define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC)) + + +/* kernel clocks */ + +#ifdef CONFIG_SYS_CLOCK_EXISTS + +/* + * If timer frequency is known at compile time, a simple (32-bit) + * tick <-> ms conversion could be used for some combinations of + * hardware timer frequency and tick rate. Otherwise precise + * (64-bit) calculations are used. + */ + +#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) +#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0 + #define _NEED_PRECISE_TICK_MS_CONVERSION +#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0 + #define _NON_OPTIMIZED_TICKS_PER_SEC +#endif +#endif + +#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \ + defined(_NON_OPTIMIZED_TICKS_PER_SEC) + #define _NEED_PRECISE_TICK_MS_CONVERSION +#endif +#endif + +static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms) +{ +#ifdef CONFIG_SYS_CLOCK_EXISTS + +#ifdef _NEED_PRECISE_TICK_MS_CONVERSION + /* use 64-bit math to keep precision */ + return (s32_t)ceiling_fraction( + (s64_t)ms * sys_clock_hw_cycles_per_sec, + ((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) / + sys_clock_ticks_per_sec); +#else + /* simple division keeps precision */ + s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec; + + return (s32_t)ceiling_fraction(ms, ms_per_tick); +#endif + +#else + __ASSERT(ms == 0, "ms not zero"); + return 0; +#endif +} + +static inline s64_t __ticks_to_ms(s64_t ticks) +{ +#ifdef CONFIG_SYS_CLOCK_EXISTS + +#ifdef _NEED_PRECISE_TICK_MS_CONVERSION + /* use 64-bit math to keep precision */ + return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec; +#else + /* simple multiplication keeps precision */ + u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec; + + return (u64_t)ticks * ms_per_tick; +#endif + +#else + __ASSERT(ticks == 0, "ticks not zero"); + return 0; +#endif +} + +/* added tick needed to account for tick in progress */ +#ifdef CONFIG_TICKLESS_KERNEL +#define _TICK_ALIGN 0 +#else +#define _TICK_ALIGN 1 +#endif + /* * sys_clock_us_per_tick global variable represents a number * of microseconds in one OS timer tick @@ -59,22 +153,6 @@ __deprecated extern int sys_clock_us_per_tick; */ extern int sys_clock_hw_cycles_per_tick; -/* number of nsec per usec */ -#define NSEC_PER_USEC 1000 - -/* number of microseconds per millisecond */ -#define USEC_PER_MSEC 1000 - -/* number of milliseconds per second */ -#define MSEC_PER_SEC 1000 - -/* number of microseconds per second */ -#define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC)) - -/* number of nanoseconds per second */ -#define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC)) - - /* SYS_CLOCK_HW_CYCLES_TO_NS64 converts CPU clock cycles to nanoseconds */ #define SYS_CLOCK_HW_CYCLES_TO_NS64(X) \ (((u64_t)(X) * NSEC_PER_SEC) / sys_clock_hw_cycles_per_sec) @@ -110,6 +188,21 @@ extern int sys_clock_hw_cycles_per_tick; extern volatile u64_t _sys_clock_tick_count; +/* timeouts */ + +struct _timeout; +typedef void (*_timeout_func_t)(struct _timeout *t); + +struct _timeout { + sys_dnode_t node; + struct k_thread *thread; + sys_dlist_t *wait_q; + s32_t delta_ticks_from_prev; + _timeout_func_t func; +}; + +extern s32_t _timeout_remaining_get(struct _timeout *timeout); + /* * Number of ticks for x seconds. NOTE: With MSEC() or USEC(), * since it does an integer division, x must be greater or equal to