ch.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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 nil/include/ch.h
  17. * @brief Nil RTOS main header file.
  18. * @details This header includes all the required kernel headers so it is the
  19. * only header you usually need to include in your application.
  20. *
  21. * @addtogroup NIL_KERNEL
  22. * @{
  23. */
  24. #ifndef CH_H
  25. #define CH_H
  26. #include "chtypes.h"
  27. #include "chconf.h"
  28. #include "chlicense.h"
  29. /*===========================================================================*/
  30. /* Module constants. */
  31. /*===========================================================================*/
  32. /**
  33. * @brief ChibiOS/NIL identification macro.
  34. */
  35. #define _CHIBIOS_NIL_
  36. /**
  37. * @brief Stable release flag.
  38. */
  39. #define CH_KERNEL_STABLE 1
  40. /**
  41. * @name ChibiOS/NIL version identification
  42. * @{
  43. */
  44. /**
  45. * @brief Kernel version string.
  46. */
  47. #define CH_KERNEL_VERSION "3.2.0"
  48. /**
  49. * @brief Kernel version major number.
  50. */
  51. #define CH_KERNEL_MAJOR 3
  52. /**
  53. * @brief Kernel version minor number.
  54. */
  55. #define CH_KERNEL_MINOR 2
  56. /**
  57. * @brief Kernel version patch number.
  58. */
  59. #define CH_KERNEL_PATCH 0
  60. /** @} */
  61. /**
  62. * @name Constants for configuration options
  63. */
  64. /**
  65. * @brief Generic 'false' preprocessor boolean constant.
  66. * @note It is meant to be used in configuration files as switch.
  67. */
  68. #if !defined(FALSE) || defined(__DOXYGEN__)
  69. #define FALSE 0
  70. #endif
  71. /**
  72. * @brief Generic 'true' preprocessor boolean constant.
  73. * @note It is meant to be used in configuration files as switch.
  74. */
  75. #if !defined(TRUE) || defined(__DOXYGEN__)
  76. #define TRUE 1
  77. #endif
  78. /** @} */
  79. /**
  80. * @name Wakeup messages
  81. * @{
  82. */
  83. #define MSG_OK (msg_t)0 /**< @brief OK wakeup message. */
  84. #define MSG_TIMEOUT (msg_t)-1 /**< @brief Wake-up caused by
  85. a timeout condition. */
  86. #define MSG_RESET (msg_t)-2 /**< @brief Wake-up caused by
  87. a reset condition. */
  88. /** @} */
  89. /**
  90. * @name Special time constants
  91. * @{
  92. */
  93. /**
  94. * @brief Zero time specification for some functions with a timeout
  95. * specification.
  96. * @note Not all functions accept @p TIME_IMMEDIATE as timeout parameter,
  97. * see the specific function documentation.
  98. */
  99. #define TIME_IMMEDIATE ((sysinterval_t)-1)
  100. /**
  101. * @brief Infinite time specification for all functions with a timeout
  102. * specification.
  103. */
  104. #define TIME_INFINITE ((sysinterval_t)0)
  105. /**
  106. * @brief Maximum interval constant usable as timeout.
  107. */
  108. #define TIME_MAX_INTERVAL ((sysinterval_t)-2)
  109. /**
  110. * @brief Maximum system of system time before it wraps.
  111. */
  112. #define TIME_MAX_SYSTIME ((systime_t)-1)
  113. /** @} */
  114. /**
  115. * @name Thread state related macros
  116. * @{
  117. */
  118. #define NIL_STATE_READY (tstate_t)0 /**< @brief Thread ready or
  119. executing. */
  120. #define NIL_STATE_SLEEPING (tstate_t)1 /**< @brief Thread sleeping. */
  121. #define NIL_STATE_SUSP (tstate_t)2 /**< @brief Thread suspended. */
  122. #define NIL_STATE_WTQUEUE (tstate_t)3 /**< @brief On queue or semaph. */
  123. #define NIL_STATE_WTOREVT (tstate_t)4 /**< @brief Waiting for events. */
  124. #define NIL_THD_IS_READY(tp) ((tp)->state == NIL_STATE_READY)
  125. #define NIL_THD_IS_SLEEPING(tp) ((tp)->state == NIL_STATE_SLEEPING)
  126. #define NIL_THD_IS_SUSP(tp) ((tp)->state == NIL_STATE_SUSP)
  127. #define NIL_THD_IS_WTQUEUE(tp) ((tp)->state == NIL_STATE_WTQUEUE)
  128. #define NIL_THD_IS_WTOREVT(tp) ((tp)->state == NIL_STATE_WTOREVT)
  129. /** @} */
  130. /**
  131. * @name Events related macros
  132. * @{
  133. */
  134. /**
  135. * @brief All events allowed mask.
  136. */
  137. #define ALL_EVENTS ((eventmask_t)-1)
  138. /**
  139. * @brief Returns an event mask from an event identifier.
  140. */
  141. #define EVENT_MASK(eid) ((eventmask_t)(1 << (eid)))
  142. /** @} */
  143. /*===========================================================================*/
  144. /* Module pre-compile time settings. */
  145. /*===========================================================================*/
  146. /*-*
  147. * @brief Number of user threads in the application.
  148. * @note This number is not inclusive of the idle thread which is
  149. * implicitly handled.
  150. */
  151. #if !defined(CH_CFG_NUM_THREADS) || defined(__DOXYGEN__)
  152. #define CH_CFG_NUM_THREADS 2
  153. #endif
  154. /*-*
  155. * @brief System time counter resolution.
  156. * @note Allowed values are 16 or 32 bits.
  157. */
  158. #if !defined(CH_CFG_ST_RESOLUTION) || defined(__DOXYGEN__)
  159. #define CH_CFG_ST_RESOLUTION 32
  160. #endif
  161. /*-*
  162. * @brief System tick frequency.
  163. * @note This value together with the @p CH_CFG_ST_RESOLUTION
  164. * option defines the maximum amount of time allowed for
  165. * timeouts.
  166. */
  167. #if !defined(CH_CFG_ST_FREQUENCY) || defined(__DOXYGEN__)
  168. #define CH_CFG_ST_FREQUENCY 100
  169. #endif
  170. /*-*
  171. * @brief Time delta constant for the tick-less mode.
  172. * @note If this value is zero then the system uses the classic
  173. * periodic tick. This value represents the minimum number
  174. * of ticks that is safe to specify in a timeout directive.
  175. * The value one is not valid, timeouts are rounded up to
  176. * this value.
  177. */
  178. #if !defined(CH_CFG_ST_TIMEDELTA) || defined(__DOXYGEN__)
  179. #define CH_CFG_ST_TIMEDELTA 0
  180. #endif
  181. /*-*
  182. * @brief Semaphores APIs.
  183. * @details If enabled then the Semaphores APIs are included in the kernel.
  184. *
  185. * @note The default is @p TRUE.
  186. */
  187. #if !defined(CH_CFG_USE_SEMAPHORES) || defined(__DOXYGEN__)
  188. #define CH_CFG_USE_SEMAPHORES TRUE
  189. #endif
  190. /*-*
  191. * @brief Mutexes APIs.
  192. * @details If enabled then the mutexes APIs are included in the kernel.
  193. *
  194. * @note Feature not currently implemented.
  195. * @note The default is @p FALSE.
  196. */
  197. #if !defined(CH_CFG_USE_MUTEXES) || defined(__DOXYGEN__)
  198. #define CH_CFG_USE_MUTEXES FALSE
  199. #endif
  200. /*-*
  201. * @brief Events Flags APIs.
  202. * @details If enabled then the event flags APIs are included in the kernel.
  203. *
  204. * @note The default is @p TRUE.
  205. */
  206. #if !defined(CH_CFG_USE_EVENTS) || defined(__DOXYGEN__)
  207. #define CH_CFG_USE_EVENTS TRUE
  208. #endif
  209. /*-*
  210. * @brief Mailboxes APIs.
  211. * @details If enabled then the asynchronous messages (mailboxes) APIs are
  212. * included in the kernel.
  213. *
  214. * @note The default is @p TRUE.
  215. * @note Requires @p CH_CFG_USE_SEMAPHORES.
  216. */
  217. #if !defined(CH_CFG_USE_MAILBOXES) || defined(__DOXYGEN__)
  218. #define CH_CFG_USE_MAILBOXES TRUE
  219. #endif
  220. /*-*
  221. * @brief Core Memory Manager APIs.
  222. * @details If enabled then the core memory manager APIs are included
  223. * in the kernel.
  224. *
  225. * @note The default is @p TRUE.
  226. */
  227. #if !defined(CH_CFG_USE_MEMCORE) || defined(__DOXYGEN__)
  228. #define CH_CFG_USE_MEMCORE TRUE
  229. #endif
  230. /*-*
  231. * @brief Heap Allocator APIs.
  232. * @details If enabled then the memory heap allocator APIs are included
  233. * in the kernel.
  234. *
  235. * @note The default is @p TRUE.
  236. */
  237. #if !defined(CH_CFG_USE_HEAP) || defined(__DOXYGEN__)
  238. #define CH_CFG_USE_HEAP TRUE
  239. #endif
  240. /*-*
  241. * @brief Memory Pools Allocator APIs.
  242. * @details If enabled then the memory pools allocator APIs are included
  243. * in the kernel.
  244. *
  245. * @note The default is @p TRUE.
  246. */
  247. #if !defined(CH_CFG_USE_MEMPOOLS) || defined(__DOXYGEN__)
  248. #define CH_CFG_USE_MEMPOOLS TRUE
  249. #endif
  250. /**
  251. * @brief Objects Factory APIs.
  252. * @details If enabled then the objects factory APIs are included in the
  253. * kernel.
  254. *
  255. * @note The default is @p FALSE.
  256. */
  257. #if !defined(CH_CFG_USE_FACTORY) || defined(__DOXYGEN__)
  258. #define CH_CFG_USE_FACTORY TRUE
  259. #endif
  260. /**
  261. * @brief Maximum length for object names.
  262. * @details If the specified length is zero then the name is stored by
  263. * pointer but this could have unintended side effects.
  264. */
  265. #if !defined(CH_CFG_FACTORY_MAX_NAMES_LENGTH) || defined(__DOXYGEN__)
  266. #define CH_CFG_FACTORY_MAX_NAMES_LENGTH 8
  267. #endif
  268. /**
  269. * @brief Enables the registry of generic objects.
  270. */
  271. #if !defined(CH_CFG_FACTORY_OBJECTS_REGISTRY) || defined(__DOXYGEN__)
  272. #define CH_CFG_FACTORY_OBJECTS_REGISTRY TRUE
  273. #endif
  274. /**
  275. * @brief Enables factory for generic buffers.
  276. */
  277. #if !defined(CH_CFG_FACTORY_GENERIC_BUFFERS) || defined(__DOXYGEN__)
  278. #define CH_CFG_FACTORY_GENERIC_BUFFERS TRUE
  279. #endif
  280. /**
  281. * @brief Enables factory for semaphores.
  282. */
  283. #if !defined(CH_CFG_FACTORY_SEMAPHORES) || defined(__DOXYGEN__)
  284. #define CH_CFG_FACTORY_SEMAPHORES TRUE
  285. #endif
  286. /**
  287. * @brief Enables factory for mailboxes.
  288. */
  289. #if !defined(CH_CFG_FACTORY_MAILBOXES) || defined(__DOXYGEN__)
  290. #define CH_CFG_FACTORY_MAILBOXES TRUE
  291. #endif
  292. /**
  293. * @brief Enables factory for objects FIFOs.
  294. */
  295. #if !defined(CH_CFG_FACTORY_OBJ_FIFOS) || defined(__DOXYGEN__)
  296. #define CH_CFG_FACTORY_OBJ_FIFOS TRUE
  297. #endif
  298. /*-*
  299. * @brief Debug option, kernel statistics.
  300. *
  301. * @note Feature not currently implemented.
  302. * @note The default is @p FALSE.
  303. */
  304. #if !defined(CH_DBG_STATISTICS) || defined(__DOXYGEN__)
  305. #define CH_DBG_STATISTICS FALSE
  306. #endif
  307. /*-*
  308. * @brief Debug option, system state check.
  309. * @note This is a planned feature, not yet implemented.
  310. *
  311. * @note The default is @p FALSE.
  312. */
  313. #if !defined(CH_DBG_SYSTEM_STATE_CHECK) || defined(__DOXYGEN__)
  314. #define CH_DBG_SYSTEM_STATE_CHECK FALSE
  315. #endif
  316. /*-*
  317. * @brief Debug option, parameters checks.
  318. *
  319. * @note The default is @p FALSE.
  320. */
  321. #if !defined(CH_DBG_ENABLE_CHECKS) || defined(__DOXYGEN__)
  322. #define CH_DBG_ENABLE_CHECKS FALSE
  323. #endif
  324. /*-*
  325. * @brief System assertions.
  326. *
  327. * @note The default is @p FALSE.
  328. */
  329. #if !defined(CH_DBG_ENABLE_ASSERTS) || defined(__DOXYGEN__)
  330. #define CH_DBG_ENABLE_ASSERTS FALSE
  331. #endif
  332. /*-*
  333. * @brief Stack check.
  334. *
  335. * @note The default is @p FALSE.
  336. */
  337. #if !defined(CH_DBG_ENABLE_STACK_CHECK) || defined(__DOXYGEN__)
  338. #define CH_DBG_ENABLE_STACK_CHECK FALSE
  339. #endif
  340. /*-*
  341. * @brief System initialization hook.
  342. */
  343. #if !defined(CH_CFG_SYSTEM_INIT_HOOK) || defined(__DOXYGEN__)
  344. #define CH_CFG_SYSTEM_INIT_HOOK() {}
  345. #endif
  346. /*-*
  347. * @brief Threads descriptor structure extension.
  348. * @details User fields added to the end of the @p thread_t structure.
  349. */
  350. #if !defined(CH_CFG_THREAD_EXT_FIELDS) || defined(__DOXYGEN__)
  351. #define CH_CFG_THREAD_EXT_FIELDS
  352. #endif
  353. /*-*
  354. * @brief Threads initialization hook.
  355. */
  356. #if !defined(CH_CFG_THREAD_EXT_INIT_HOOK) || defined(__DOXYGEN__)
  357. #define CH_CFG_THREAD_EXT_INIT_HOOK(tp) {}
  358. #endif
  359. /*-*
  360. * @brief Idle thread enter hook.
  361. * @note This hook is invoked within a critical zone, no OS functions
  362. * should be invoked from here.
  363. * @note This macro can be used to activate a power saving mode.
  364. */
  365. #if !defined(CH_CFG_IDLE_ENTER_HOOK) || defined(__DOXYGEN__)
  366. #define CH_CFG_IDLE_ENTER_HOOK() {}
  367. #endif
  368. /*-*
  369. * @brief Idle thread leave hook.
  370. * @note This hook is invoked within a critical zone, no OS functions
  371. * should be invoked from here.
  372. * @note This macro can be used to deactivate a power saving mode.
  373. */
  374. #if !defined(CH_CFG_IDLE_LEAVE_HOOK) || defined(__DOXYGEN__)
  375. #define CH_CFG_IDLE_LEAVE_HOOK() {}
  376. #endif
  377. /*-*
  378. * @brief System halt hook.
  379. */
  380. #if !defined(CH_CFG_SYSTEM_HALT_HOOK) || defined(__DOXYGEN__)
  381. #define CH_CFG_SYSTEM_HALT_HOOK(reason) {}
  382. #endif
  383. /*===========================================================================*/
  384. /* Derived constants and error checks. */
  385. /*===========================================================================*/
  386. /* License checks.*/
  387. #if !defined(CH_CUSTOMER_LIC_NIL) || !defined(CH_LICENSE_FEATURES)
  388. #error "malformed chlicense.h"
  389. #endif
  390. #if CH_CUSTOMER_LIC_NIL == FALSE
  391. #error "ChibiOS/NIL not licensed"
  392. #endif
  393. #if (CH_LICENSE_FEATURES != CH_FEATURES_FULL) && \
  394. (CH_LICENSE_FEATURES != CH_FEATURES_INTERMEDIATE) && \
  395. (CH_LICENSE_FEATURES != CH_FEATURES_BASIC)
  396. #error "invalid CH_LICENSE_FEATURES setting"
  397. #endif
  398. /* Restrictions in basic and intermediate modes.*/
  399. #if (CH_LICENSE_FEATURES == CH_FEATURES_INTERMEDIATE) || \
  400. (CH_LICENSE_FEATURES == CH_FEATURES_BASIC)
  401. /* System tick limited to 1000hz.*/
  402. #if CH_CFG_ST_FREQUENCY > 1000
  403. #undef CH_CFG_ST_FREQUENCY
  404. #define CH_CFG_ST_FREQUENCY 1000
  405. #endif
  406. #endif /* (CH_LICENSE_FEATURES == CH_FEATURES_INTERMEDIATE) ||
  407. (CH_LICENSE_FEATURES == CH_FEATURES_BASIC) */
  408. /* Restrictions in basic mode.*/
  409. #if CH_LICENSE_FEATURES == CH_FEATURES_BASIC
  410. /* Tick-Less mode restricted.*/
  411. #undef CH_CFG_ST_TIMEDELTA
  412. #define CH_CFG_ST_TIMEDELTA 0
  413. #endif /* CH_LICENSE_FEATURES == CH_FEATURES_BASIC */
  414. #if !defined(_CHIBIOS_NIL_CONF_)
  415. #error "missing or wrong configuration file"
  416. #endif
  417. #if !defined(_CHIBIOS_NIL_CONF_VER_3_2_)
  418. #error "obsolete or unknown configuration file"
  419. #endif
  420. #if CH_CFG_NUM_THREADS < 1
  421. #error "at least one thread must be defined"
  422. #endif
  423. #if CH_CFG_NUM_THREADS > 16
  424. #error "ChibiOS/NIL is not recommended for thread-intensive applications," \
  425. "consider ChibiOS/RT instead"
  426. #endif
  427. #if (CH_CFG_ST_RESOLUTION != 16) && (CH_CFG_ST_RESOLUTION != 32)
  428. #error "invalid CH_CFG_ST_RESOLUTION specified, must be 16 or 32"
  429. #endif
  430. #if CH_CFG_ST_FREQUENCY <= 0
  431. #error "invalid CH_CFG_ST_FREQUENCY specified, must be greater than zero"
  432. #endif
  433. #if (CH_CFG_ST_TIMEDELTA < 0) || (CH_CFG_ST_TIMEDELTA == 1)
  434. #error "invalid CH_CFG_ST_TIMEDELTA specified, must " \
  435. "be zero or greater than one"
  436. #endif
  437. #if CH_CFG_USE_MUTEXES == TRUE
  438. #error "mutexes not yet supported"
  439. #endif
  440. #if CH_DBG_STATISTICS == TRUE
  441. #error "statistics not yet supported"
  442. #endif
  443. #if (CH_DBG_SYSTEM_STATE_CHECK == TRUE) || \
  444. (CH_DBG_ENABLE_CHECKS == TRUE) || \
  445. (CH_DBG_ENABLE_ASSERTS == TRUE) || \
  446. (CH_DBG_ENABLE_STACK_CHECK == TRUE)
  447. #define NIL_DBG_ENABLED TRUE
  448. #else
  449. #define NIL_DBG_ENABLED FALSE
  450. #endif
  451. /** Boundaries of the idle thread boundaries, only required if stack checking
  452. is enabled.*/
  453. #if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || defined(__DOXYGEN__)
  454. #define THD_IDLE_BASE (&__main_thread_stack_base__)
  455. #define THD_IDLE_END (&__main_thread_stack_end__)
  456. #else
  457. #define THD_IDLE_BASE NULL
  458. #define THD_IDLE_END NULL
  459. #endif
  460. /*===========================================================================*/
  461. /* Module data structures and types. */
  462. /*===========================================================================*/
  463. #if (CH_CFG_ST_RESOLUTION == 32) || defined(__DOXYGEN__)
  464. /**
  465. * @brief Type of system time.
  466. * @note It is selectable in configuration between 16 or 32 bits.
  467. */
  468. typedef uint32_t systime_t;
  469. /**
  470. * @brief Type of time interval.
  471. * @note It is selectable in configuration between 16 or 32 bits.
  472. */
  473. typedef uint32_t sysinterval_t;
  474. /**
  475. * @brief Type of time conversion variable.
  476. * @note This type must have double width than other time types, it is
  477. * only used internally for conversions.
  478. */
  479. typedef uint64_t time_conv_t;
  480. #else
  481. typedef uint16_t systime_t;
  482. typedef uint16_t sysinterval_t;
  483. typedef uint32_t time_conv_t;
  484. #endif
  485. /**
  486. * @brief Type of a structure representing a thread.
  487. * @note It is required as an early definition.
  488. */
  489. typedef struct nil_thread thread_t;
  490. #include "chcore.h"
  491. /**
  492. * @brief Structure representing a queue of threads.
  493. */
  494. struct nil_threads_queue {
  495. volatile cnt_t cnt; /**< @brief Threads Queue counter. */
  496. };
  497. /**
  498. * @brief Type of a queue of threads.
  499. */
  500. typedef struct nil_threads_queue threads_queue_t;
  501. #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
  502. /**
  503. * @brief Type of a structure representing a semaphore.
  504. * @note Semaphores are implemented on thread queues, the object is the
  505. * same, the behavior is slightly different.
  506. */
  507. typedef threads_queue_t semaphore_t;
  508. #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
  509. /**
  510. * @brief Thread function.
  511. */
  512. typedef void (*tfunc_t)(void *p);
  513. /**
  514. * @brief Type of a structure representing a thread static configuration.
  515. */
  516. typedef struct nil_thread_cfg thread_config_t;
  517. /**
  518. * @brief Structure representing a thread static configuration.
  519. */
  520. struct nil_thread_cfg {
  521. stkalign_t *wbase; /**< @brief Thread working area base. */
  522. stkalign_t *wend; /**< @brief Thread working area end. */
  523. const char *namep; /**< @brief Thread name, for debugging. */
  524. tfunc_t funcp; /**< @brief Thread function. */
  525. void *arg; /**< @brief Thread function argument. */
  526. };
  527. /**
  528. * @brief Type of a thread reference.
  529. */
  530. typedef thread_t * thread_reference_t;
  531. /**
  532. * @brief Structure representing a thread.
  533. */
  534. struct nil_thread {
  535. struct port_context ctx; /**< @brief Processor context. */
  536. tstate_t state; /**< @brief Thread state. */
  537. /* Note, the following union contains a pointer while the thread is in a
  538. sleeping state (!NIL_THD_IS_READY()) else contains the wake-up message.*/
  539. union {
  540. msg_t msg; /**< @brief Wake-up message. */
  541. void *p; /**< @brief Generic pointer. */
  542. thread_reference_t *trp; /**< @brief Pointer to thread reference.*/
  543. threads_queue_t *tqp; /**< @brief Pointer to thread queue. */
  544. #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
  545. semaphore_t *semp; /**< @brief Pointer to semaphore. */
  546. #endif
  547. #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
  548. eventmask_t ewmask; /**< @brief Enabled events mask. */
  549. #endif
  550. } u1;
  551. volatile sysinterval_t timeout; /**< @brief Timeout counter, zero
  552. if disabled. */
  553. #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
  554. eventmask_t epmask; /**< @brief Pending events mask. */
  555. #endif
  556. #if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || defined(__DOXYGEN__)
  557. stkalign_t *wabase; /**< @brief Thread stack boundary. */
  558. #endif
  559. /* Optional extra fields.*/
  560. CH_CFG_THREAD_EXT_FIELDS
  561. };
  562. /**
  563. * @brief Type of a structure representing the system.
  564. */
  565. typedef struct nil_system nil_system_t;
  566. /**
  567. * @brief System data structure.
  568. * @note This structure contain all the data areas used by the OS except
  569. * stacks.
  570. */
  571. struct nil_system {
  572. /**
  573. * @brief Pointer to the running thread.
  574. */
  575. thread_t *current;
  576. /**
  577. * @brief Pointer to the next thread to be executed.
  578. * @note This pointer must point at the same thread pointed by @p current
  579. * or to an higher priority thread if a switch is required.
  580. */
  581. thread_t *next;
  582. #if (CH_CFG_ST_TIMEDELTA == 0) || defined(__DOXYGEN__)
  583. /**
  584. * @brief System time.
  585. */
  586. volatile systime_t systime;
  587. #endif
  588. #if (CH_CFG_ST_TIMEDELTA > 0) || defined(__DOXYGEN__)
  589. /**
  590. * @brief System time of the last tick event.
  591. */
  592. systime_t lasttime;
  593. /**
  594. * @brief Time of the next scheduled tick event.
  595. */
  596. systime_t nexttime;
  597. #endif
  598. #if (CH_DBG_SYSTEM_STATE_CHECK == TRUE) || defined(__DOXYGEN__)
  599. /**
  600. * @brief ISR nesting level.
  601. */
  602. cnt_t isr_cnt;
  603. /**
  604. * @brief Lock nesting level.
  605. */
  606. cnt_t lock_cnt;
  607. #endif
  608. #if (NIL_DBG_ENABLED == TRUE) || defined(__DOXYGEN__)
  609. /**
  610. * @brief Panic message.
  611. * @note This field is only present if some debug options have been
  612. * activated.
  613. * @note Accesses to this pointer must never be optimized out so the
  614. * field itself is declared volatile.
  615. */
  616. const char * volatile dbg_panic_msg;
  617. #endif
  618. /**
  619. * @brief Thread structures for all the defined threads.
  620. */
  621. thread_t threads[CH_CFG_NUM_THREADS + 1];
  622. };
  623. /*===========================================================================*/
  624. /* Module macros. */
  625. /*===========================================================================*/
  626. #if CH_DBG_SYSTEM_STATE_CHECK == TRUE
  627. #define _dbg_enter_lock() (nil.lock_cnt = (cnt_t)1)
  628. #define _dbg_leave_lock() (nil.lock_cnt = (cnt_t)0)
  629. #endif
  630. /**
  631. * @brief Utility to make the parameter a quoted string.
  632. */
  633. #define __CH_STRINGIFY(a) #a
  634. /**
  635. * @name Threads tables definition macros
  636. * @{
  637. */
  638. /**
  639. * @brief Start of user threads table.
  640. */
  641. #define THD_TABLE_BEGIN \
  642. const thread_config_t nil_thd_configs[CH_CFG_NUM_THREADS + 1] = {
  643. /**
  644. * @brief Entry of user threads table
  645. */
  646. #define THD_TABLE_ENTRY(wap, name, funcp, arg) \
  647. {wap, ((stkalign_t *)(wap)) + (sizeof (wap) / sizeof(stkalign_t)), \
  648. name, funcp, arg},
  649. /**
  650. * @brief End of user threads table.
  651. */
  652. #define THD_TABLE_END \
  653. {THD_IDLE_BASE, THD_IDLE_END, "idle", NULL, NULL} \
  654. };
  655. /** @} */
  656. /**
  657. * @name Memory alignment support macros
  658. */
  659. /**
  660. * @brief Alignment mask constant.
  661. *
  662. * @param[in] a alignment, must be a power of two
  663. */
  664. #define MEM_ALIGN_MASK(a) ((size_t)(a) - 1U)
  665. /**
  666. * @brief Aligns to the previous aligned memory address.
  667. *
  668. * @param[in] p variable to be aligned
  669. * @param[in] a alignment, must be a power of two
  670. */
  671. #define MEM_ALIGN_PREV(p, a) ((size_t)(p) & ~MEM_ALIGN_MASK(a))
  672. /**
  673. * @brief Aligns to the new aligned memory address.
  674. *
  675. * @param[in] p variable to be aligned
  676. * @param[in] a alignment, must be a power of two
  677. */
  678. #define MEM_ALIGN_NEXT(p, a) MEM_ALIGN_PREV((size_t)(p) + \
  679. MEM_ALIGN_MASK(a), (a))
  680. /**
  681. * @brief Returns whatever a pointer or memory size is aligned.
  682. *
  683. * @param[in] p variable to be aligned
  684. * @param[in] a alignment, must be a power of two
  685. */
  686. #define MEM_IS_ALIGNED(p, a) (((size_t)(p) & MEM_ALIGN_MASK(a)) == 0U)
  687. /**
  688. * @brief Returns whatever a constant is a valid alignment.
  689. * @details Valid alignments are powers of two.
  690. *
  691. * @param[in] a alignment to be checked, must be a constant
  692. */
  693. #define MEM_IS_VALID_ALIGNMENT(a) \
  694. (((size_t)(a) != 0U) && (((size_t)(a) & ((size_t)(a) - 1U)) == 0U))
  695. /** @} */
  696. /**
  697. * @name Working Areas
  698. */
  699. /**
  700. * @brief Calculates the total Working Area size.
  701. *
  702. * @param[in] n the stack size to be assigned to the thread
  703. * @return The total used memory in bytes.
  704. *
  705. * @api
  706. */
  707. #define THD_WORKING_AREA_SIZE(n) MEM_ALIGN_NEXT(PORT_WA_SIZE(n), \
  708. PORT_STACK_ALIGN)
  709. /**
  710. * @brief Static working area allocation.
  711. * @details This macro is used to allocate a static thread working area
  712. * aligned as both position and size.
  713. *
  714. * @param[in] s the name to be assigned to the stack array
  715. * @param[in] n the stack size to be assigned to the thread
  716. *
  717. * @api
  718. */
  719. #define THD_WORKING_AREA(s, n) PORT_WORKING_AREA(s, n)
  720. /** @} */
  721. /**
  722. * @name Threads abstraction macros
  723. */
  724. /**
  725. * @brief Thread declaration macro.
  726. * @note Thread declarations should be performed using this macro because
  727. * the port layer could define optimizations for thread functions.
  728. */
  729. #define THD_FUNCTION(tname, arg) PORT_THD_FUNCTION(tname, arg)
  730. /** @} */
  731. /**
  732. * @name ISRs abstraction macros
  733. */
  734. /**
  735. * @brief Priority level validation macro.
  736. * @details This macro determines if the passed value is a valid priority
  737. * level for the underlying architecture.
  738. *
  739. * @param[in] prio the priority level
  740. * @return Priority range result.
  741. * @retval false if the priority is invalid or if the architecture
  742. * does not support priorities.
  743. * @retval true if the priority is valid.
  744. */
  745. #if defined(PORT_IRQ_IS_VALID_PRIORITY) || defined(__DOXYGEN__)
  746. #define CH_IRQ_IS_VALID_PRIORITY(prio) \
  747. PORT_IRQ_IS_VALID_PRIORITY(prio)
  748. #else
  749. #define CH_IRQ_IS_VALID_PRIORITY(prio) false
  750. #endif
  751. /**
  752. * @brief Priority level validation macro.
  753. * @details This macro determines if the passed value is a valid priority
  754. * level that cannot preempt the kernel critical zone.
  755. *
  756. * @param[in] prio the priority level
  757. * @return Priority range result.
  758. * @retval false if the priority is invalid or if the architecture
  759. * does not support priorities.
  760. * @retval true if the priority is valid.
  761. */
  762. #if defined(PORT_IRQ_IS_VALID_KERNEL_PRIORITY) || defined(__DOXYGEN__)
  763. #define CH_IRQ_IS_VALID_KERNEL_PRIORITY(prio) \
  764. PORT_IRQ_IS_VALID_KERNEL_PRIORITY(prio)
  765. #else
  766. #define CH_IRQ_IS_VALID_KERNEL_PRIORITY(prio) false
  767. #endif
  768. /**
  769. * @brief IRQ handler enter code.
  770. * @note Usually IRQ handlers functions are also declared naked.
  771. * @note On some architectures this macro can be empty.
  772. *
  773. * @special
  774. */
  775. #define CH_IRQ_PROLOGUE() \
  776. PORT_IRQ_PROLOGUE(); \
  777. _dbg_check_enter_isr()
  778. /**
  779. * @brief IRQ handler exit code.
  780. * @note Usually IRQ handlers function are also declared naked.
  781. *
  782. * @special
  783. */
  784. #define CH_IRQ_EPILOGUE() \
  785. _dbg_check_leave_isr(); \
  786. PORT_IRQ_EPILOGUE()
  787. /**
  788. * @brief Standard normal IRQ handler declaration.
  789. * @note @p id can be a function name or a vector number depending on the
  790. * port implementation.
  791. *
  792. * @special
  793. */
  794. #define CH_IRQ_HANDLER(id) PORT_IRQ_HANDLER(id)
  795. /** @} */
  796. /**
  797. * @name Fast ISRs abstraction macros
  798. */
  799. /**
  800. * @brief Standard fast IRQ handler declaration.
  801. * @note @p id can be a function name or a vector number depending on the
  802. * port implementation.
  803. * @note Not all architectures support fast interrupts.
  804. *
  805. * @special
  806. */
  807. #define CH_FAST_IRQ_HANDLER(id) PORT_FAST_IRQ_HANDLER(id)
  808. /** @} */
  809. /**
  810. * @name Time conversion utilities
  811. * @{
  812. */
  813. /**
  814. * @brief Seconds to time interval.
  815. * @details Converts from seconds to system ticks number.
  816. * @note The result is rounded upward to the next tick boundary.
  817. * @note Use of this macro for large values is not secure because
  818. * integer overflows, make sure your value can be correctly
  819. * converted.
  820. *
  821. * @param[in] secs number of seconds
  822. * @return The number of ticks.
  823. *
  824. * @api
  825. */
  826. #define TIME_S2I(secs) \
  827. ((sysinterval_t)((time_conv_t)(secs) * (time_conv_t)CH_CFG_ST_FREQUENCY))
  828. /**
  829. * @brief Milliseconds to time interval.
  830. * @details Converts from milliseconds to system ticks number.
  831. * @note The result is rounded upward to the next tick boundary.
  832. * @note Use of this macro for large values is not secure because
  833. * integer overflows, make sure your value can be correctly
  834. * converted.
  835. *
  836. * @param[in] msecs number of milliseconds
  837. * @return The number of ticks.
  838. *
  839. * @api
  840. */
  841. #define TIME_MS2I(msecs) \
  842. ((sysinterval_t)((((time_conv_t)(msecs) * \
  843. (time_conv_t)CH_CFG_ST_FREQUENCY) + \
  844. (time_conv_t)999) / (time_conv_t)1000))
  845. /**
  846. * @brief Microseconds to time interval.
  847. * @details Converts from microseconds to system ticks number.
  848. * @note The result is rounded upward to the next tick boundary.
  849. * @note Use of this macro for large values is not secure because
  850. * integer overflows, make sure your value can be correctly
  851. * converted.
  852. *
  853. * @param[in] usecs number of microseconds
  854. * @return The number of ticks.
  855. *
  856. * @api
  857. */
  858. #define TIME_US2I(usecs) \
  859. ((sysinterval_t)((((time_conv_t)(usecs) * \
  860. (time_conv_t)CH_CFG_ST_FREQUENCY) + \
  861. (time_conv_t)999999) / (time_conv_t)1000000))
  862. /**
  863. * @brief Time interval to seconds.
  864. * @details Converts from system ticks number to seconds.
  865. * @note The result is rounded up to the next second boundary.
  866. * @note Use of this macro for large values is not secure because
  867. * integer overflows, make sure your value can be correctly
  868. * converted.
  869. *
  870. * @param[in] interval interval in ticks
  871. * @return The number of seconds.
  872. *
  873. * @api
  874. */
  875. #define TIME_I2S(interval) \
  876. (time_secs_t)(((time_conv_t)(interval) + \
  877. (time_conv_t)CH_CFG_ST_FREQUENCY - \
  878. (time_conv_t)1) / (time_conv_t)CH_CFG_ST_FREQUENCY)
  879. /**
  880. * @brief Time interval to milliseconds.
  881. * @details Converts from system ticks number to milliseconds.
  882. * @note The result is rounded up to the next millisecond boundary.
  883. * @note Use of this macro for large values is not secure because
  884. * integer overflows, make sure your value can be correctly
  885. * converted.
  886. *
  887. * @param[in] interval interval in ticks
  888. * @return The number of milliseconds.
  889. *
  890. * @api
  891. */
  892. #define TIME_I2MS(interval) \
  893. (time_msecs_t)((((time_conv_t)(interval) * (time_conv_t)1000) + \
  894. (time_conv_t)CH_CFG_ST_FREQUENCY - (time_conv_t)1) / \
  895. (time_conv_t)CH_CFG_ST_FREQUENCY)
  896. /**
  897. * @brief Time interval to microseconds.
  898. * @details Converts from system ticks number to microseconds.
  899. * @note The result is rounded up to the next microsecond boundary.
  900. * @note Use of this macro for large values is not secure because
  901. * integer overflows, make sure your value can be correctly
  902. * converted.
  903. *
  904. * @param[in] interval interval in ticks
  905. * @return The number of microseconds.
  906. *
  907. * @api
  908. */
  909. #define TIME_I2US(interval) \
  910. (time_msecs_t)((((time_conv_t)(interval) * (time_conv_t)1000000) + \
  911. (time_conv_t)CH_CFG_ST_FREQUENCY - (time_conv_t)1) / \
  912. (time_conv_t)CH_CFG_ST_FREQUENCY)
  913. /** @} */
  914. /**
  915. * @name Threads queues
  916. */
  917. /**
  918. * @brief Data part of a static threads queue object initializer.
  919. * @details This macro should be used when statically initializing a threads
  920. * queue that is part of a bigger structure.
  921. *
  922. * @param[in] name the name of the threads queue variable
  923. */
  924. #define _THREADS_QUEUE_DATA(name) {(cnt_t)0}
  925. /**
  926. * @brief Static threads queue object initializer.
  927. * @details Statically initialized threads queues require no explicit
  928. * initialization using @p queue_init().
  929. *
  930. * @param[in] name the name of the threads queue variable
  931. */
  932. #define _THREADS_QUEUE_DECL(name) \
  933. threads_queue_t name = _THREADS_QUEUE_DATA(name)
  934. /** @} */
  935. /**
  936. * @name Semaphores macros
  937. * @{
  938. */
  939. /**
  940. * @brief Data part of a static semaphore initializer.
  941. * @details This macro should be used when statically initializing a semaphore
  942. * that is part of a bigger structure.
  943. *
  944. * @param[in] name the name of the semaphore variable
  945. * @param[in] n the counter initial value, this value must be
  946. * non-negative
  947. */
  948. #define _SEMAPHORE_DATA(name, n) {n}
  949. /**
  950. * @brief Static semaphore initializer.
  951. * @details Statically initialized semaphores require no explicit
  952. * initialization using @p chSemInit().
  953. *
  954. * @param[in] name the name of the semaphore variable
  955. * @param[in] n the counter initial value, this value must be
  956. * non-negative
  957. */
  958. #define SEMAPHORE_DECL(name, n) semaphore_t name = _SEMAPHORE_DATA(name, n)
  959. /** @} */
  960. /**
  961. * @name Macro Functions
  962. * @{
  963. */
  964. /**
  965. * @brief Returns the current value of the system real time counter.
  966. * @note This function is only available if the port layer supports the
  967. * option @p PORT_SUPPORTS_RT.
  968. *
  969. * @return The value of the system realtime counter of
  970. * type rtcnt_t.
  971. *
  972. * @xclass
  973. */
  974. #if (PORT_SUPPORTS_RT == TRUE) || defined(__DOXYGEN__)
  975. #define chSysGetRealtimeCounterX() (rtcnt_t)port_rt_get_counter_value()
  976. #endif
  977. /**
  978. * @brief Raises the system interrupt priority mask to the maximum level.
  979. * @details All the maskable interrupt sources are disabled regardless their
  980. * hardware priority.
  981. * @note Do not invoke this API from within a kernel lock.
  982. *
  983. * @special
  984. */
  985. #define chSysDisable() { \
  986. port_disable(); \
  987. _dbg_check_disable(); \
  988. }
  989. /**
  990. * @brief Raises the system interrupt priority mask to system level.
  991. * @details The interrupt sources that should not be able to preempt the kernel
  992. * are disabled, interrupt sources with higher priority are still
  993. * enabled.
  994. * @note Do not invoke this API from within a kernel lock.
  995. * @note This API is no replacement for @p chSysLock(), the @p chSysLock()
  996. * could do more than just disable the interrupts.
  997. *
  998. * @special
  999. */
  1000. #define chSysSuspend() { \
  1001. port_suspend(); \
  1002. _dbg_check_suspend(); \
  1003. }
  1004. /**
  1005. * @brief Lowers the system interrupt priority mask to user level.
  1006. * @details All the interrupt sources are enabled.
  1007. * @note Do not invoke this API from within a kernel lock.
  1008. * @note This API is no replacement for @p chSysUnlock(), the
  1009. * @p chSysUnlock() could do more than just enable the interrupts.
  1010. *
  1011. * @special
  1012. */
  1013. #define chSysEnable() { \
  1014. _dbg_check_enable(); \
  1015. port_enable(); \
  1016. }
  1017. /**
  1018. * @brief Enters the kernel lock state.
  1019. *
  1020. * @special
  1021. */
  1022. #define chSysLock() { \
  1023. port_lock(); \
  1024. _dbg_check_lock(); \
  1025. }
  1026. /**
  1027. * @brief Leaves the kernel lock state.
  1028. *
  1029. * @special
  1030. */
  1031. #define chSysUnlock() { \
  1032. _dbg_check_unlock(); \
  1033. port_unlock(); \
  1034. }
  1035. /**
  1036. * @brief Enters the kernel lock state from within an interrupt handler.
  1037. * @note This API may do nothing on some architectures, it is required
  1038. * because on ports that support preemptable interrupt handlers
  1039. * it is required to raise the interrupt mask to the same level of
  1040. * the system mutual exclusion zone.<br>
  1041. * It is good practice to invoke this API before invoking any I-class
  1042. * syscall from an interrupt handler.
  1043. * @note This API must be invoked exclusively from interrupt handlers.
  1044. *
  1045. * @special
  1046. */
  1047. #define chSysLockFromISR() { \
  1048. port_lock_from_isr(); \
  1049. _dbg_check_lock_from_isr(); \
  1050. }
  1051. /**
  1052. * @brief Leaves the kernel lock state from within an interrupt handler.
  1053. *
  1054. * @note This API may do nothing on some architectures, it is required
  1055. * because on ports that support preemptable interrupt handlers
  1056. * it is required to raise the interrupt mask to the same level of
  1057. * the system mutual exclusion zone.<br>
  1058. * It is good practice to invoke this API after invoking any I-class
  1059. * syscall from an interrupt handler.
  1060. * @note This API must be invoked exclusively from interrupt handlers.
  1061. *
  1062. * @special
  1063. */
  1064. #define chSysUnlockFromISR() { \
  1065. _dbg_check_unlock_from_isr(); \
  1066. port_unlock_from_isr(); \
  1067. }
  1068. /**
  1069. * @brief Evaluates if a reschedule is required.
  1070. *
  1071. * @retval true if there is a thread that must go in running state
  1072. * immediately.
  1073. * @retval false if preemption is not required.
  1074. *
  1075. * @iclass
  1076. */
  1077. #define chSchIsRescRequiredI() ((bool)(nil.current != nil.next))
  1078. /**
  1079. * @brief Returns a pointer to the current @p thread_t.
  1080. *
  1081. * @xclass
  1082. */
  1083. #define chThdGetSelfX() nil.current
  1084. /**
  1085. * @brief Delays the invoking thread for the specified number of seconds.
  1086. * @note The specified time is rounded up to a value allowed by the real
  1087. * system clock.
  1088. * @note The maximum specified value is implementation dependent.
  1089. *
  1090. * @param[in] secs time in seconds, must be different from zero
  1091. *
  1092. * @api
  1093. */
  1094. #define chThdSleepSeconds(secs) chThdSleep(TIME_S2I(secs))
  1095. /**
  1096. * @brief Delays the invoking thread for the specified number of
  1097. * milliseconds.
  1098. * @note The specified time is rounded up to a value allowed by the real
  1099. * system clock.
  1100. * @note The maximum specified value is implementation dependent.
  1101. *
  1102. * @param[in] msecs time in milliseconds, must be different from zero
  1103. *
  1104. * @api
  1105. */
  1106. #define chThdSleepMilliseconds(msecs) chThdSleep(TIME_MS2I(msecs))
  1107. /**
  1108. * @brief Delays the invoking thread for the specified number of
  1109. * microseconds.
  1110. * @note The specified time is rounded up to a value allowed by the real
  1111. * system clock.
  1112. * @note The maximum specified value is implementation dependent.
  1113. *
  1114. * @param[in] usecs time in microseconds, must be different from zero
  1115. *
  1116. * @api
  1117. */
  1118. #define chThdSleepMicroseconds(usecs) chThdSleep(TIME_US2I(usecs))
  1119. /**
  1120. * @brief Suspends the invoking thread for the specified time.
  1121. *
  1122. * @param[in] timeout the delay in system ticks
  1123. *
  1124. * @sclass
  1125. */
  1126. #define chThdSleepS(timeout) \
  1127. (void) chSchGoSleepTimeoutS(NIL_STATE_SLEEPING, timeout)
  1128. /**
  1129. * @brief Suspends the invoking thread until the system time arrives to the
  1130. * specified value.
  1131. *
  1132. * @param[in] abstime absolute system time
  1133. *
  1134. * @sclass
  1135. */
  1136. #define chThdSleepUntilS(abstime) \
  1137. (void) chSchGoSleepTimeoutS(NIL_STATE_SLEEPING, \
  1138. chTimeDiffX(chVTGetSystemTimeX(), (abstime)))
  1139. /**
  1140. * @brief Initializes a threads queue object.
  1141. *
  1142. * @param[out] tqp pointer to the threads queue object
  1143. *
  1144. * @init
  1145. */
  1146. #define chThdQueueObjectInit(tqp) ((tqp)->cnt = (cnt_t)0)
  1147. /**
  1148. * @brief Evaluates to @p true if the specified queue is empty.
  1149. *
  1150. * @param[out] tqp pointer to the threads queue object
  1151. * @return The queue status.
  1152. * @retval false if the queue is not empty.
  1153. * @retval true if the queue is empty.
  1154. *
  1155. * @iclass
  1156. */
  1157. #define chThdQueueIsEmptyI(tqp) ((bool)(tqp->cnt >= (cnt_t)0))
  1158. #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
  1159. /**
  1160. * @brief Initializes a semaphore with the specified counter value.
  1161. *
  1162. * @param[out] sp pointer to a @p semaphore_t structure
  1163. * @param[in] n initial value of the semaphore counter. Must be
  1164. * non-negative.
  1165. *
  1166. * @init
  1167. */
  1168. #define chSemObjectInit(sp, n) ((sp)->cnt = (n))
  1169. /**
  1170. * @brief Performs a wait operation on a semaphore.
  1171. *
  1172. * @param[in] sp pointer to a @p semaphore_t structure
  1173. * @return A message specifying how the invoking thread has been
  1174. * released from the semaphore.
  1175. * @retval CH_MSG_OK if the thread has not stopped on the semaphore or the
  1176. * semaphore has been signaled.
  1177. * @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset().
  1178. *
  1179. * @api
  1180. */
  1181. #define chSemWait(sp) chSemWaitTimeout(sp, TIME_INFINITE)
  1182. /**
  1183. * @brief Performs a wait operation on a semaphore.
  1184. *
  1185. * @param[in] sp pointer to a @p semaphore_t structure
  1186. * @return A message specifying how the invoking thread has been
  1187. * released from the semaphore.
  1188. * @retval CH_MSG_OK if the thread has not stopped on the semaphore or the
  1189. * semaphore has been signaled.
  1190. * @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset().
  1191. *
  1192. * @sclass
  1193. */
  1194. #define chSemWaitS(sp) chSemWaitTimeoutS(sp, TIME_INFINITE)
  1195. /**
  1196. * @brief Decreases the semaphore counter.
  1197. * @details This macro can be used when the counter is known to be positive.
  1198. *
  1199. * @param[in] sp pointer to a @p semaphore_t structure
  1200. *
  1201. * @iclass
  1202. */
  1203. #define chSemFastWaitI(sp) ((sp)->cnt--)
  1204. /**
  1205. * @brief Increases the semaphore counter.
  1206. * @details This macro can be used when the counter is known to be not
  1207. * negative.
  1208. *
  1209. * @param[in] sp pointer to a @p semaphore_t structure
  1210. *
  1211. * @iclass
  1212. */
  1213. #define chSemFastSignalI(sp) ((sp)->cnt++)
  1214. /**
  1215. * @brief Returns the semaphore counter current value.
  1216. *
  1217. * @iclass
  1218. */
  1219. #define chSemGetCounterI(sp) ((sp)->cnt)
  1220. #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
  1221. /**
  1222. * @brief Current system time.
  1223. * @details Returns the number of system ticks since the @p chSysInit()
  1224. * invocation.
  1225. * @note The counter can reach its maximum and then restart from zero.
  1226. * @note This function can be called from any context but its atomicity
  1227. * is not guaranteed on architectures whose word size is less than
  1228. * @p systime_t size.
  1229. *
  1230. * @return The system time in ticks.
  1231. *
  1232. * @xclass
  1233. */
  1234. #if (CH_CFG_ST_TIMEDELTA == 0) || defined(__DOXYGEN__)
  1235. #define chVTGetSystemTimeX() (nil.systime)
  1236. #else
  1237. #define chVTGetSystemTimeX() port_timer_get_time()
  1238. #endif
  1239. /**
  1240. * @brief Returns the elapsed time since the specified start time.
  1241. *
  1242. * @param[in] start start time
  1243. * @return The elapsed time.
  1244. *
  1245. * @xclass
  1246. */
  1247. #define chVTTimeElapsedSinceX(start) \
  1248. chTimeDiffX((start), chVTGetSystemTimeX())
  1249. /**
  1250. * @brief Adds an interval to a system time returning a system time.
  1251. *
  1252. * @param[in] systime base system time
  1253. * @param[in] interval interval to be added
  1254. * @return The new system time.
  1255. *
  1256. * @xclass
  1257. */
  1258. #define chTimeAddX(systime, interval) \
  1259. ((systime_t)(systime) + (systime_t)(interval))
  1260. /**
  1261. * @brief Subtracts two system times returning an interval.
  1262. *
  1263. * @param[in] start first system time
  1264. * @param[in] end second system time
  1265. * @return The interval representing the time difference.
  1266. *
  1267. * @xclass
  1268. */
  1269. #define chTimeDiffX(start, end) \
  1270. ((sysinterval_t)((systime_t)((systime_t)(end) - (systime_t)(start))))
  1271. /**
  1272. * @brief Checks if the specified time is within the specified time range.
  1273. * @note When start==end then the function returns always true because the
  1274. * whole time range is specified.
  1275. *
  1276. * @param[in] time the time to be verified
  1277. * @param[in] start the start of the time window (inclusive)
  1278. * @param[in] end the end of the time window (non inclusive)
  1279. * @retval true current time within the specified time window.
  1280. * @retval false current time not within the specified time window.
  1281. *
  1282. * @xclass
  1283. */
  1284. #define chTimeIsInRangeX(time, start, end) \
  1285. ((bool)((systime_t)((systime_t)(time) - (systime_t)(start)) < \
  1286. (systime_t)((systime_t)(end) - (systime_t)(start))))
  1287. /**
  1288. * @brief Function parameters check.
  1289. * @details If the condition check fails then the kernel panics and halts.
  1290. * @note The condition is tested only if the @p CH_DBG_ENABLE_CHECKS switch
  1291. * is specified in @p chconf.h else the macro does nothing.
  1292. *
  1293. * @param[in] c the condition to be verified to be true
  1294. *
  1295. * @api
  1296. */
  1297. #if !defined(chDbgCheck)
  1298. #define chDbgCheck(c) do { \
  1299. /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
  1300. if (CH_DBG_ENABLE_CHECKS != FALSE) { \
  1301. if (!(c)) { \
  1302. /*lint -restore*/ \
  1303. chSysHalt(__func__); \
  1304. } \
  1305. } \
  1306. } while (false)
  1307. #endif /* !defined(chDbgCheck) */
  1308. /**
  1309. * @brief Condition assertion.
  1310. * @details If the condition check fails then the kernel panics with a
  1311. * message and halts.
  1312. * @note The condition is tested only if the @p CH_DBG_ENABLE_ASSERTS
  1313. * switch is specified in @p chconf.h else the macro does nothing.
  1314. * @note The remark string is not currently used except for putting a
  1315. * comment in the code about the assertion.
  1316. *
  1317. * @param[in] c the condition to be verified to be true
  1318. * @param[in] r a remark string
  1319. *
  1320. * @api
  1321. */
  1322. #if !defined(chDbgAssert)
  1323. #define chDbgAssert(c, r) do { \
  1324. /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
  1325. if (CH_DBG_ENABLE_ASSERTS != FALSE) { \
  1326. if (!(c)) { \
  1327. /*lint -restore*/ \
  1328. chSysHalt(__func__); \
  1329. } \
  1330. } \
  1331. } while (false)
  1332. #endif /* !defined(chDbgAssert) */
  1333. /** @} */
  1334. /* Empty macros if the state checker is not enabled.*/
  1335. #if CH_DBG_SYSTEM_STATE_CHECK == FALSE
  1336. #define _dbg_enter_lock()
  1337. #define _dbg_leave_lock()
  1338. #define _dbg_check_disable()
  1339. #define _dbg_check_suspend()
  1340. #define _dbg_check_enable()
  1341. #define _dbg_check_lock()
  1342. #define _dbg_check_unlock()
  1343. #define _dbg_check_lock_from_isr()
  1344. #define _dbg_check_unlock_from_isr()
  1345. #define _dbg_check_enter_isr()
  1346. #define _dbg_check_leave_isr()
  1347. #define chDbgCheckClassI()
  1348. #define chDbgCheckClassS()
  1349. #endif
  1350. /*===========================================================================*/
  1351. /* External declarations. */
  1352. /*===========================================================================*/
  1353. #if !defined(__DOXYGEN__)
  1354. #if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || defined(__DOXYGEN__)
  1355. extern stkalign_t __main_thread_stack_base__, __main_thread_stack_end__;
  1356. #endif
  1357. extern nil_system_t nil;
  1358. extern const thread_config_t nil_thd_configs[CH_CFG_NUM_THREADS + 1];
  1359. #endif
  1360. #ifdef __cplusplus
  1361. extern "C" {
  1362. #endif
  1363. void chSysInit(void);
  1364. void chSysHalt(const char *reason);
  1365. void chSysTimerHandlerI(void);
  1366. void chSysUnconditionalLock(void);
  1367. void chSysUnconditionalUnlock(void);
  1368. syssts_t chSysGetStatusAndLockX(void);
  1369. bool chSysIsCounterWithinX(rtcnt_t cnt, rtcnt_t start, rtcnt_t end);
  1370. void chSysPolledDelayX(rtcnt_t cycles);
  1371. void chSysRestoreStatusX(syssts_t sts);
  1372. thread_t *chSchReadyI(thread_t *tp, msg_t msg);
  1373. bool chSchIsPreemptionRequired(void);
  1374. void chSchDoReschedule(void);
  1375. void chSchRescheduleS(void);
  1376. msg_t chSchGoSleepTimeoutS(tstate_t newstate, sysinterval_t timeout);
  1377. msg_t chThdSuspendTimeoutS(thread_reference_t *trp, sysinterval_t timeout);
  1378. void chThdResumeI(thread_reference_t *trp, msg_t msg);
  1379. void chThdResume(thread_reference_t *trp, msg_t msg);
  1380. void chThdSleep(sysinterval_t timeout);
  1381. void chThdSleepUntil(systime_t abstime);
  1382. msg_t chThdEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout);
  1383. void chThdDoDequeueNextI(threads_queue_t *tqp, msg_t msg);
  1384. void chThdDequeueNextI(threads_queue_t *tqp, msg_t msg);
  1385. void chThdDequeueAllI(threads_queue_t *tqp, msg_t msg);
  1386. #if CH_CFG_USE_SEMAPHORES == TRUE
  1387. msg_t chSemWaitTimeout(semaphore_t *sp, sysinterval_t timeout);
  1388. msg_t chSemWaitTimeoutS(semaphore_t *sp, sysinterval_t timeout);
  1389. void chSemSignal(semaphore_t *sp);
  1390. void chSemSignalI(semaphore_t *sp);
  1391. void chSemReset(semaphore_t *sp, cnt_t n);
  1392. void chSemResetI(semaphore_t *sp, cnt_t n);
  1393. #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
  1394. #if CH_CFG_USE_EVENTS == TRUE
  1395. void chEvtSignal(thread_t *tp, eventmask_t mask);
  1396. void chEvtSignalI(thread_t *tp, eventmask_t mask);
  1397. eventmask_t chEvtWaitAnyTimeout(eventmask_t mask, sysinterval_t timeout);
  1398. #endif
  1399. #if CH_DBG_SYSTEM_STATE_CHECK == TRUE
  1400. void _dbg_check_disable(void);
  1401. void _dbg_check_suspend(void);
  1402. void _dbg_check_enable(void);
  1403. void _dbg_check_lock(void);
  1404. void _dbg_check_unlock(void);
  1405. void _dbg_check_lock_from_isr(void);
  1406. void _dbg_check_unlock_from_isr(void);
  1407. void _dbg_check_enter_isr(void);
  1408. void _dbg_check_leave_isr(void);
  1409. void chDbgCheckClassI(void);
  1410. void chDbgCheckClassS(void);
  1411. #endif
  1412. #ifdef __cplusplus
  1413. }
  1414. #endif
  1415. /* OSLIB.*/
  1416. #include "chlib.h"
  1417. #endif /* CH_H */
  1418. /** @} */