Skip to content

Commit 0d1228a

Browse files
Andy Rossnashif
authored andcommitted
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 <[email protected]>
1 parent 853b734 commit 0d1228a

File tree

3 files changed

+114
-115
lines changed

3 files changed

+114
-115
lines changed

include/kernel.h

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -355,21 +355,6 @@ struct __packed _k_thread_stack_element {
355355
};
356356
typedef struct _k_thread_stack_element k_thread_stack_t;
357357

358-
/* timeouts */
359-
360-
struct _timeout;
361-
typedef void (*_timeout_func_t)(struct _timeout *t);
362-
363-
struct _timeout {
364-
sys_dnode_t node;
365-
struct k_thread *thread;
366-
sys_dlist_t *wait_q;
367-
s32_t delta_ticks_from_prev;
368-
_timeout_func_t func;
369-
};
370-
371-
extern s32_t _timeout_remaining_get(struct _timeout *timeout);
372-
373358
/**
374359
* @typedef k_thread_entry_t
375360
* @brief Thread entry point function type.
@@ -1235,11 +1220,6 @@ __syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
12351220
*/
12361221
__syscall const char *k_thread_name_get(k_tid_t thread_id);
12371222

1238-
/**
1239-
* @}
1240-
*/
1241-
#include <sys_clock.h>
1242-
12431223
/**
12441224
* @addtogroup clock_apis
12451225
* @{
@@ -1321,81 +1301,6 @@ __syscall const char *k_thread_name_get(k_tid_t thread_id);
13211301
* @cond INTERNAL_HIDDEN
13221302
*/
13231303

1324-
/* kernel clocks */
1325-
1326-
#ifdef CONFIG_SYS_CLOCK_EXISTS
1327-
1328-
/*
1329-
* If timer frequency is known at compile time, a simple (32-bit)
1330-
* tick <-> ms conversion could be used for some combinations of
1331-
* hardware timer frequency and tick rate. Otherwise precise
1332-
* (64-bit) calculations are used.
1333-
*/
1334-
1335-
#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
1336-
#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0
1337-
#define _NEED_PRECISE_TICK_MS_CONVERSION
1338-
#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
1339-
#define _NON_OPTIMIZED_TICKS_PER_SEC
1340-
#endif
1341-
#endif
1342-
1343-
#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
1344-
defined(_NON_OPTIMIZED_TICKS_PER_SEC)
1345-
#define _NEED_PRECISE_TICK_MS_CONVERSION
1346-
#endif
1347-
#endif
1348-
1349-
static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
1350-
{
1351-
#ifdef CONFIG_SYS_CLOCK_EXISTS
1352-
1353-
#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1354-
/* use 64-bit math to keep precision */
1355-
return (s32_t)ceiling_fraction(
1356-
(s64_t)ms * sys_clock_hw_cycles_per_sec,
1357-
((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) /
1358-
sys_clock_ticks_per_sec);
1359-
#else
1360-
/* simple division keeps precision */
1361-
s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1362-
1363-
return (s32_t)ceiling_fraction(ms, ms_per_tick);
1364-
#endif
1365-
1366-
#else
1367-
__ASSERT(ms == 0, "ms not zero");
1368-
return 0;
1369-
#endif
1370-
}
1371-
1372-
static inline s64_t __ticks_to_ms(s64_t ticks)
1373-
{
1374-
#ifdef CONFIG_SYS_CLOCK_EXISTS
1375-
1376-
#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1377-
/* use 64-bit math to keep precision */
1378-
return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec;
1379-
#else
1380-
/* simple multiplication keeps precision */
1381-
u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1382-
1383-
return (u64_t)ticks * ms_per_tick;
1384-
#endif
1385-
1386-
#else
1387-
__ASSERT(ticks == 0, "ticks not zero");
1388-
return 0;
1389-
#endif
1390-
}
1391-
1392-
/* added tick needed to account for tick in progress */
1393-
#ifdef CONFIG_TICKLESS_KERNEL
1394-
#define _TICK_ALIGN 0
1395-
#else
1396-
#define _TICK_ALIGN 1
1397-
#endif
1398-
13991304
struct k_timer {
14001305
/*
14011306
* _timeout structure must be first here if we want to use

include/kernel_includes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
#include <misc/printk.h>
3434
#include <arch/cpu.h>
3535
#include <misc/rb.h>
36+
#include <sys_clock.h>
3637

3738
#endif /* ZEPHYR_INCLUDE_KERNEL_INCLUDES_H_ */

include/sys_clock.h

Lines changed: 113 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616
#ifndef ZEPHYR_INCLUDE_SYS_CLOCK_H_
1717
#define ZEPHYR_INCLUDE_SYS_CLOCK_H_
1818

19+
#include <misc/util.h>
20+
#include <misc/dlist.h>
21+
1922
#ifdef __cplusplus
2023
extern "C" {
2124
#endif
2225

2326
#include <toolchain.h>
2427
#include <zephyr/types.h>
2528

26-
#if defined(CONFIG_SYS_CLOCK_EXISTS) && \
27-
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0)
28-
#error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!"
29-
#endif
30-
3129
#ifdef CONFIG_TICKLESS_KERNEL
3230
#define sys_clock_ticks_per_sec \
3331
(1000000 / (CONFIG_TICKLESS_KERNEL_TIME_UNIT_IN_MICRO_SECS))
@@ -43,21 +41,10 @@ extern int sys_clock_hw_cycles_per_sec;
4341
#define sys_clock_hw_cycles_per_sec CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC
4442
#endif
4543

46-
/*
47-
* sys_clock_us_per_tick global variable represents a number
48-
* of microseconds in one OS timer tick
49-
*
50-
* Note: This variable is deprecated and will be removed soon!
51-
*/
52-
__deprecated extern int sys_clock_us_per_tick;
53-
54-
/*
55-
* sys_clock_hw_cycles_per_tick global variable represents a number
56-
* of platform clock ticks in one OS timer tick.
57-
* sys_clock_hw_cycles_per_tick often represents a value of divider
58-
* of the board clock frequency
59-
*/
60-
extern int sys_clock_hw_cycles_per_tick;
44+
#if defined(CONFIG_SYS_CLOCK_EXISTS) && \
45+
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0)
46+
#error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!"
47+
#endif
6148

6249
/* number of nsec per usec */
6350
#define NSEC_PER_USEC 1000
@@ -75,6 +62,97 @@ extern int sys_clock_hw_cycles_per_tick;
7562
#define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC))
7663

7764

65+
/* kernel clocks */
66+
67+
#ifdef CONFIG_SYS_CLOCK_EXISTS
68+
69+
/*
70+
* If timer frequency is known at compile time, a simple (32-bit)
71+
* tick <-> ms conversion could be used for some combinations of
72+
* hardware timer frequency and tick rate. Otherwise precise
73+
* (64-bit) calculations are used.
74+
*/
75+
76+
#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
77+
#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0
78+
#define _NEED_PRECISE_TICK_MS_CONVERSION
79+
#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
80+
#define _NON_OPTIMIZED_TICKS_PER_SEC
81+
#endif
82+
#endif
83+
84+
#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
85+
defined(_NON_OPTIMIZED_TICKS_PER_SEC)
86+
#define _NEED_PRECISE_TICK_MS_CONVERSION
87+
#endif
88+
#endif
89+
90+
static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
91+
{
92+
#ifdef CONFIG_SYS_CLOCK_EXISTS
93+
94+
#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
95+
/* use 64-bit math to keep precision */
96+
return (s32_t)ceiling_fraction(
97+
(s64_t)ms * sys_clock_hw_cycles_per_sec,
98+
((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) /
99+
sys_clock_ticks_per_sec);
100+
#else
101+
/* simple division keeps precision */
102+
s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
103+
104+
return (s32_t)ceiling_fraction(ms, ms_per_tick);
105+
#endif
106+
107+
#else
108+
__ASSERT(ms == 0, "ms not zero");
109+
return 0;
110+
#endif
111+
}
112+
113+
static inline s64_t __ticks_to_ms(s64_t ticks)
114+
{
115+
#ifdef CONFIG_SYS_CLOCK_EXISTS
116+
117+
#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
118+
/* use 64-bit math to keep precision */
119+
return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec;
120+
#else
121+
/* simple multiplication keeps precision */
122+
u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
123+
124+
return (u64_t)ticks * ms_per_tick;
125+
#endif
126+
127+
#else
128+
__ASSERT(ticks == 0, "ticks not zero");
129+
return 0;
130+
#endif
131+
}
132+
133+
/* added tick needed to account for tick in progress */
134+
#ifdef CONFIG_TICKLESS_KERNEL
135+
#define _TICK_ALIGN 0
136+
#else
137+
#define _TICK_ALIGN 1
138+
#endif
139+
140+
/*
141+
* sys_clock_us_per_tick global variable represents a number
142+
* of microseconds in one OS timer tick
143+
*
144+
* Note: This variable is deprecated and will be removed soon!
145+
*/
146+
__deprecated extern int sys_clock_us_per_tick;
147+
148+
/*
149+
* sys_clock_hw_cycles_per_tick global variable represents a number
150+
* of platform clock ticks in one OS timer tick.
151+
* sys_clock_hw_cycles_per_tick often represents a value of divider
152+
* of the board clock frequency
153+
*/
154+
extern int sys_clock_hw_cycles_per_tick;
155+
78156
/* SYS_CLOCK_HW_CYCLES_TO_NS64 converts CPU clock cycles to nanoseconds */
79157
#define SYS_CLOCK_HW_CYCLES_TO_NS64(X) \
80158
(((u64_t)(X) * NSEC_PER_SEC) / sys_clock_hw_cycles_per_sec)
@@ -110,6 +188,21 @@ extern int sys_clock_hw_cycles_per_tick;
110188

111189
extern volatile u64_t _sys_clock_tick_count;
112190

191+
/* timeouts */
192+
193+
struct _timeout;
194+
typedef void (*_timeout_func_t)(struct _timeout *t);
195+
196+
struct _timeout {
197+
sys_dnode_t node;
198+
struct k_thread *thread;
199+
sys_dlist_t *wait_q;
200+
s32_t delta_ticks_from_prev;
201+
_timeout_func_t func;
202+
};
203+
204+
extern s32_t _timeout_remaining_get(struct _timeout *timeout);
205+
113206
/*
114207
* Number of ticks for x seconds. NOTE: With MSEC() or USEC(),
115208
* since it does an integer division, x must be greater or equal to

0 commit comments

Comments
 (0)