chsys.h 15 KB

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