ChibiOS/RT  6.0.3
chsys.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
3 
4  This file is part of ChibiOS.
5 
6  ChibiOS is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  ChibiOS is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /**
21  * @file chsys.h
22  * @brief System related macros and structures.
23  *
24  * @addtogroup system
25  * @{
26  */
27 
28 #ifndef CHSYS_H
29 #define CHSYS_H
30 
31 /*lint -sem(chSysHalt, r_no)*/
32 
33 /*===========================================================================*/
34 /* Module constants. */
35 /*===========================================================================*/
36 
37 /**
38  * @name Masks of executable integrity checks.
39  * @{
40  */
41 #define CH_INTEGRITY_RLIST 1U
42 #define CH_INTEGRITY_VTLIST 2U
43 #define CH_INTEGRITY_REGISTRY 4U
44 #define CH_INTEGRITY_PORT 8U
45 /** @} */
46 
47 /*===========================================================================*/
48 /* Module pre-compile time settings. */
49 /*===========================================================================*/
50 
51 /*===========================================================================*/
52 /* Derived constants and error checks. */
53 /*===========================================================================*/
54 
55 /*===========================================================================*/
56 /* Module data structures and types. */
57 /*===========================================================================*/
58 
59 /*===========================================================================*/
60 /* Module macros. */
61 /*===========================================================================*/
62 
63 /**
64  * @name ISRs abstraction macros
65  */
66 /**
67  * @brief Priority level validation macro.
68  * @details This macro determines if the passed value is a valid priority
69  * level for the underlying architecture.
70  *
71  * @param[in] prio the priority level
72  * @return Priority range result.
73  * @retval false if the priority is invalid or if the architecture
74  * does not support priorities.
75  * @retval true if the priority is valid.
76  */
77 #if defined(PORT_IRQ_IS_VALID_PRIORITY) || defined(__DOXYGEN__)
78 #define CH_IRQ_IS_VALID_PRIORITY(prio) \
79  PORT_IRQ_IS_VALID_PRIORITY(prio)
80 #else
81 #define CH_IRQ_IS_VALID_PRIORITY(prio) false
82 #endif
83 
84 /**
85  * @brief Priority level validation macro.
86  * @details This macro determines if the passed value is a valid priority
87  * level that cannot preempt the kernel critical zone.
88  *
89  * @param[in] prio the priority level
90  * @return Priority range result.
91  * @retval false if the priority is invalid or if the architecture
92  * does not support priorities.
93  * @retval true if the priority is valid.
94  */
95 #if defined(PORT_IRQ_IS_VALID_KERNEL_PRIORITY) || defined(__DOXYGEN__)
96 #define CH_IRQ_IS_VALID_KERNEL_PRIORITY(prio) \
97  PORT_IRQ_IS_VALID_KERNEL_PRIORITY(prio)
98 #else
99 #define CH_IRQ_IS_VALID_KERNEL_PRIORITY(prio) false
100 #endif
101 
102 /**
103  * @brief IRQ handler enter code.
104  * @note Usually IRQ handlers functions are also declared naked.
105  * @note On some architectures this macro can be empty.
106  *
107  * @special
108  */
109 #define CH_IRQ_PROLOGUE() \
110  PORT_IRQ_PROLOGUE(); \
111  CH_CFG_IRQ_PROLOGUE_HOOK(); \
112  _stats_increase_irq(); \
113  _trace_isr_enter(__func__); \
114  _dbg_check_enter_isr()
115 
116 /**
117  * @brief IRQ handler exit code.
118  * @note Usually IRQ handlers function are also declared naked.
119  * @note This macro usually performs the final reschedule by using
120  * @p chSchIsPreemptionRequired() and @p chSchDoReschedule().
121  *
122  * @special
123  */
124 #define CH_IRQ_EPILOGUE() \
125  _dbg_check_leave_isr(); \
126  _trace_isr_leave(__func__); \
127  CH_CFG_IRQ_EPILOGUE_HOOK(); \
128  PORT_IRQ_EPILOGUE()
129 
130 /**
131  * @brief Standard normal IRQ handler declaration.
132  * @note @p id can be a function name or a vector number depending on the
133  * port implementation.
134  *
135  * @special
136  */
137 #define CH_IRQ_HANDLER(id) PORT_IRQ_HANDLER(id)
138 /** @} */
139 
140 /**
141  * @name Fast ISRs abstraction macros
142  */
143 /**
144  * @brief Standard fast IRQ handler declaration.
145  * @note @p id can be a function name or a vector number depending on the
146  * port implementation.
147  * @note Not all architectures support fast interrupts.
148  *
149  * @special
150  */
151 #define CH_FAST_IRQ_HANDLER(id) PORT_FAST_IRQ_HANDLER(id)
152 /** @} */
153 
154 /**
155  * @name Time conversion utilities for the realtime counter
156  * @{
157  */
158 /**
159  * @brief Seconds to realtime counter.
160  * @details Converts from seconds to realtime counter cycles.
161  * @note The macro assumes that @p freq >= @p 1.
162  *
163  * @param[in] freq clock frequency, in Hz, of the realtime counter
164  * @param[in] sec number of seconds
165  * @return The number of cycles.
166  *
167  * @api
168  */
169 #define S2RTC(freq, sec) ((freq) * (sec))
170 
171 /**
172  * @brief Milliseconds to realtime counter.
173  * @details Converts from milliseconds to realtime counter cycles.
174  * @note The result is rounded upward to the next millisecond boundary.
175  * @note The macro assumes that @p freq >= @p 1000.
176  *
177  * @param[in] freq clock frequency, in Hz, of the realtime counter
178  * @param[in] msec number of milliseconds
179  * @return The number of cycles.
180  *
181  * @api
182  */
183 #define MS2RTC(freq, msec) (rtcnt_t)((((freq) + 999UL) / 1000UL) * (msec))
184 
185 /**
186  * @brief Microseconds to realtime counter.
187  * @details Converts from microseconds to realtime counter cycles.
188  * @note The result is rounded upward to the next microsecond boundary.
189  * @note The macro assumes that @p freq >= @p 1000000.
190  *
191  * @param[in] freq clock frequency, in Hz, of the realtime counter
192  * @param[in] usec number of microseconds
193  * @return The number of cycles.
194  *
195  * @api
196  */
197 #define US2RTC(freq, usec) (rtcnt_t)((((freq) + 999999UL) / 1000000UL) * (usec))
198 
199 /**
200  * @brief Realtime counter cycles to seconds.
201  * @details Converts from realtime counter cycles number to seconds.
202  * @note The result is rounded up to the next second boundary.
203  * @note The macro assumes that @p freq >= @p 1.
204  *
205  * @param[in] freq clock frequency, in Hz, of the realtime counter
206  * @param[in] n number of cycles
207  * @return The number of seconds.
208  *
209  * @api
210  */
211 #define RTC2S(freq, n) ((((n) - 1UL) / (freq)) + 1UL)
212 
213 /**
214  * @brief Realtime counter cycles to milliseconds.
215  * @details Converts from realtime counter cycles number to milliseconds.
216  * @note The result is rounded up to the next millisecond boundary.
217  * @note The macro assumes that @p freq >= @p 1000.
218  *
219  * @param[in] freq clock frequency, in Hz, of the realtime counter
220  * @param[in] n number of cycles
221  * @return The number of milliseconds.
222  *
223  * @api
224  */
225 #define RTC2MS(freq, n) ((((n) - 1UL) / ((freq) / 1000UL)) + 1UL)
226 
227 /**
228  * @brief Realtime counter cycles to microseconds.
229  * @details Converts from realtime counter cycles number to microseconds.
230  * @note The result is rounded up to the next microsecond boundary.
231  * @note The macro assumes that @p freq >= @p 1000000.
232  *
233  * @param[in] freq clock frequency, in Hz, of the realtime counter
234  * @param[in] n number of cycles
235  * @return The number of microseconds.
236  *
237  * @api
238  */
239 #define RTC2US(freq, n) ((((n) - 1UL) / ((freq) / 1000000UL)) + 1UL)
240 /** @} */
241 
242 /**
243  * @brief Returns the current value of the system real time counter.
244  * @note This function is only available if the port layer supports the
245  * option @p PORT_SUPPORTS_RT.
246  *
247  * @return The value of the system realtime counter of
248  * type rtcnt_t.
249  *
250  * @xclass
251  */
252 #if (PORT_SUPPORTS_RT == TRUE) || defined(__DOXYGEN__)
253 #define chSysGetRealtimeCounterX() (rtcnt_t)port_rt_get_counter_value()
254 #endif
255 
256 /**
257  * @brief Performs a context switch.
258  * @note Not a user function, it is meant to be invoked by the scheduler
259  * itself or from within the port layer.
260  *
261  * @param[in] ntp the thread to be switched in
262  * @param[in] otp the thread to be switched out
263  *
264  * @special
265  */
266 #define chSysSwitch(ntp, otp) { \
267  \
268  _trace_switch(ntp, otp); \
269  _stats_ctxswc(ntp, otp); \
270  CH_CFG_CONTEXT_SWITCH_HOOK(ntp, otp); \
271  port_switch(ntp, otp); \
272 }
273 
274 /*===========================================================================*/
275 /* External declarations. */
276 /*===========================================================================*/
277 
278 #if !defined(__DOXYGEN__)
279 extern stkalign_t ch_idle_thread_wa[];
280 #endif
281 
282 #ifdef __cplusplus
283 extern "C" {
284 #endif
285  void chSysInit(void);
286  bool chSysIntegrityCheckI(unsigned testmask);
287  void chSysTimerHandlerI(void);
288  syssts_t chSysGetStatusAndLockX(void);
289  void chSysRestoreStatusX(syssts_t sts);
290 #if PORT_SUPPORTS_RT == TRUE
291  bool chSysIsCounterWithinX(rtcnt_t cnt, rtcnt_t start, rtcnt_t end);
292  void chSysPolledDelayX(rtcnt_t cycles);
293 #endif
294 #ifdef __cplusplus
295 }
296 #endif
297 
298 /*===========================================================================*/
299 /* Module inline functions. */
300 /*===========================================================================*/
301 
302 /**
303  * @brief Raises the system interrupt priority mask to the maximum level.
304  * @details All the maskable interrupt sources are disabled regardless their
305  * hardware priority.
306  * @note Do not invoke this API from within a kernel lock.
307  *
308  * @special
309  */
310 static inline void chSysDisable(void) {
311 
312  port_disable();
314 }
315 
316 /**
317  * @brief Raises the system interrupt priority mask to system level.
318  * @details The interrupt sources that should not be able to preempt the kernel
319  * are disabled, interrupt sources with higher priority are still
320  * enabled.
321  * @note Do not invoke this API from within a kernel lock.
322  * @note This API is no replacement for @p chSysLock(), the @p chSysLock()
323  * could do more than just disable the interrupts.
324  *
325  * @special
326  */
327 static inline void chSysSuspend(void) {
328 
329  port_suspend();
331 }
332 
333 /**
334  * @brief Lowers the system interrupt priority mask to user level.
335  * @details All the interrupt sources are enabled.
336  * @note Do not invoke this API from within a kernel lock.
337  * @note This API is no replacement for @p chSysUnlock(), the
338  * @p chSysUnlock() could do more than just enable the interrupts.
339  *
340  * @special
341  */
342 static inline void chSysEnable(void) {
343 
345  port_enable();
346 }
347 
348 /**
349  * @brief Enters the kernel lock state.
350  *
351  * @special
352  */
353 static inline void chSysLock(void) {
354 
355  port_lock();
357  _dbg_check_lock();
358 }
359 
360 /**
361  * @brief Leaves the kernel lock state.
362  *
363  * @special
364  */
365 static inline void chSysUnlock(void) {
366 
369 
370  /* The following condition can be triggered by the use of i-class functions
371  in a critical section not followed by a chSchResceduleS(), this means
372  that the current thread has a lower priority than the next thread in
373  the ready list.*/
374  chDbgAssert((ch.rlist.queue.next == (thread_t *)&ch.rlist.queue) ||
375  (ch.rlist.current->prio >= ch.rlist.queue.next->prio),
376  "priority order violation");
377 
378  port_unlock();
379 }
380 
381 /**
382  * @brief Enters the kernel lock state from within an interrupt handler.
383  * @note This API may do nothing on some architectures, it is required
384  * because on ports that support preemptable interrupt handlers
385  * it is required to raise the interrupt mask to the same level of
386  * the system mutual exclusion zone.<br>
387  * It is good practice to invoke this API before invoking any I-class
388  * syscall from an interrupt handler.
389  * @note This API must be invoked exclusively from interrupt handlers.
390  *
391  * @special
392  */
393 static inline void chSysLockFromISR(void) {
394 
395  port_lock_from_isr();
398 }
399 
400 /**
401  * @brief Leaves the kernel lock state from within an interrupt handler.
402  *
403  * @note This API may do nothing on some architectures, it is required
404  * because on ports that support preemptable interrupt handlers
405  * it is required to raise the interrupt mask to the same level of
406  * the system mutual exclusion zone.<br>
407  * It is good practice to invoke this API after invoking any I-class
408  * syscall from an interrupt handler.
409  * @note This API must be invoked exclusively from interrupt handlers.
410  *
411  * @special
412  */
413 static inline void chSysUnlockFromISR(void) {
414 
417  port_unlock_from_isr();
418 }
419 
420 /**
421  * @brief Unconditionally enters the kernel lock state.
422  * @note Can be called without previous knowledge of the current lock state.
423  * The final state is "s-locked".
424  *
425  * @special
426  */
427 static inline void chSysUnconditionalLock(void) {
428 
429  if (port_irq_enabled(port_get_irq_status())) {
430  chSysLock();
431  }
432 }
433 
434 /**
435  * @brief Unconditionally leaves the kernel lock state.
436  * @note Can be called without previous knowledge of the current lock state.
437  * The final state is "normal".
438  *
439  * @special
440  */
441 static inline void chSysUnconditionalUnlock(void) {
442 
443  if (!port_irq_enabled(port_get_irq_status())) {
444  chSysUnlock();
445  }
446 }
447 
448 #if (CH_CFG_NO_IDLE_THREAD == FALSE) || defined(__DOXYGEN__)
449 /**
450  * @brief Returns a pointer to the idle thread.
451  * @pre In order to use this function the option @p CH_CFG_NO_IDLE_THREAD
452  * must be disabled.
453  * @note The reference counter of the idle thread is not incremented but
454  * it is not strictly required being the idle thread a static
455  * object.
456  *
457  * @return Pointer to the idle thread.
458  *
459  * @xclass
460  */
461 static inline thread_t *chSysGetIdleThreadX(void) {
462 
463  return ch.rlist.queue.prev;
464 }
465 #endif /* CH_CFG_NO_IDLE_THREAD == FALSE */
466 
467 #endif /* CHSYS_H */
468 
469 /** @} */
static void chSysLock(void)
Enters the kernel lock state.
Definition: chsys.h:353
void chSysPolledDelayX(rtcnt_t cycles)
Polled delay.
Definition: chsys.c:443
void _dbg_check_disable(void)
Guard code for chSysDisable().
Definition: chdebug.c:112
void _dbg_check_lock_from_isr(void)
Guard code for chSysLockFromIsr().
Definition: chdebug.c:174
void _stats_start_measure_crit_thd(void)
Starts the measurement of a thread critical zone.
Definition: chstats.c:95
void _dbg_check_enable(void)
Guard code for chSysEnable().
Definition: chdebug.c:136
static void chSysUnlock(void)
Leaves the kernel lock state.
Definition: chsys.h:365
void _dbg_check_unlock(void)
Guard code for chSysUnlock().
Definition: chdebug.c:161
void _stats_stop_measure_crit_thd(void)
Stops the measurement of a thread critical zone.
Definition: chstats.c:103
static void chSysUnconditionalUnlock(void)
Unconditionally leaves the kernel lock state.
Definition: chsys.h:441
void chSysTimerHandlerI(void)
Handles time ticks for round robin preemption and timer increments.
Definition: chsys.c:341
bool chSysIntegrityCheckI(unsigned testmask)
System integrity check.
Definition: chsys.c:240
void _dbg_check_suspend(void)
Guard code for chSysSuspend().
Definition: chdebug.c:124
ready_list_t rlist
Ready list header.
Definition: chschd.h:415
void chSysInit(void)
ChibiOS/RT initialization.
Definition: chsys.c:100
ch_system_t ch
System data structures.
Definition: chschd.c:42
void _dbg_check_unlock_from_isr(void)
Guard code for chSysUnlockFromIsr().
Definition: chdebug.c:187
static void chSysUnlockFromISR(void)
Leaves the kernel lock state from within an interrupt handler.
Definition: chsys.h:413
syssts_t chSysGetStatusAndLockX(void)
Returns the execution status and enters a critical zone.
Definition: chsys.c:372
static void chSysLockFromISR(void)
Enters the kernel lock state from within an interrupt handler.
Definition: chsys.h:393
void chSysRestoreStatusX(syssts_t sts)
Restores the specified execution status and leaves a critical zone.
Definition: chsys.c:395
static thread_t * chSysGetIdleThreadX(void)
Returns a pointer to the idle thread.
Definition: chsys.h:461
bool chSysIsCounterWithinX(rtcnt_t cnt, rtcnt_t start, rtcnt_t end)
Realtime window test.
Definition: chsys.c:427
void _stats_start_measure_crit_isr(void)
Starts the measurement of an ISR critical zone.
Definition: chstats.c:111
static void chSysEnable(void)
Lowers the system interrupt priority mask to user level.
Definition: chsys.h:342
void _dbg_check_lock(void)
Guard code for chSysLock().
Definition: chdebug.c:148
#define chDbgAssert(c, r)
Condition assertion.
Definition: chdebug.h:127
static void chSysDisable(void)
Raises the system interrupt priority mask to the maximum level.
Definition: chsys.h:310
static void chSysUnconditionalLock(void)
Unconditionally enters the kernel lock state.
Definition: chsys.h:427
void _stats_stop_measure_crit_isr(void)
Stops the measurement of an ISR critical zone.
Definition: chstats.c:119
static void chSysSuspend(void)
Raises the system interrupt priority mask to system level.
Definition: chsys.h:327
Structure representing a thread.
Definition: chschd.h:153