cmsis_os.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. Concepts and parts of this file have been contributed by Andre R.
  17. */
  18. /**
  19. * @file cmsis_os.h
  20. * @brief CMSIS RTOS module macros and structures.
  21. *
  22. * @addtogroup CMSIS_OS
  23. * @{
  24. */
  25. #ifndef CMSIS_OS_H
  26. #define CMSIS_OS_H
  27. #include "ch.h"
  28. /*===========================================================================*/
  29. /* Module constants. */
  30. /*===========================================================================*/
  31. /**
  32. * @brief API version.
  33. */
  34. #define osCMSIS 0x10002
  35. /**
  36. * @brief Kernel version.
  37. */
  38. #define osKernelSystemId "KERNEL V1.00"
  39. /**
  40. * @brief ChibiOS/RT version encoded for CMSIS.
  41. */
  42. #define osCMSIS_KERNEL ((CH_KERNEL_MAJOR << 16) | \
  43. (CH_KERNEL_MINOR << 8) | \
  44. (CH_KERNEL_PATCH))
  45. /**
  46. * @name CMSIS Capabilities
  47. * @{
  48. */
  49. #define osFeature_MainThread 1
  50. #define osFeature_Pool 1
  51. #define osFeature_MailQ 0
  52. #define osFeature_MessageQ 1
  53. #define osFeature_Signals 24
  54. #define osFeature_Semaphore ((1U << 31) - 1U)
  55. #define osFeature_Wait 0
  56. #define osFeature_SysTick 1
  57. /**< @} */
  58. /**
  59. * @brief Wait forever specification for timeouts.
  60. */
  61. #define osWaitForever ((uint32_t)-1)
  62. /**
  63. * @brief System tick frequency.
  64. */
  65. #define osKernelSysTickFrequency CH_CFG_ST_FREQUENCY
  66. /*===========================================================================*/
  67. /* Module pre-compile time settings. */
  68. /*===========================================================================*/
  69. /**
  70. * @brief Number of pre-allocated static semaphores/mutexes.
  71. */
  72. #if !defined(CMSIS_CFG_DEFAULT_STACK)
  73. #define CMSIS_CFG_DEFAULT_STACK 256
  74. #endif
  75. /**
  76. * @brief Number of pre-allocated static semaphores/mutexes.
  77. */
  78. #if !defined(CMSIS_CFG_NUM_SEMAPHORES)
  79. #define CMSIS_CFG_NUM_SEMAPHORES 4
  80. #endif
  81. /**
  82. * @brief Number of pre-allocated static timers.
  83. */
  84. #if !defined(CMSIS_CFG_NUM_TIMERS)
  85. #define CMSIS_CFG_NUM_TIMERS 4
  86. #endif
  87. /*===========================================================================*/
  88. /* Derived constants and error checks. */
  89. /*===========================================================================*/
  90. #if !CH_CFG_USE_MEMPOOLS
  91. #error "CMSIS RTOS requires CH_CFG_USE_MEMPOOLS"
  92. #endif
  93. #if !CH_CFG_USE_EVENTS
  94. #error "CMSIS RTOS requires CH_CFG_USE_EVENTS"
  95. #endif
  96. #if !CH_CFG_USE_EVENTS_TIMEOUT
  97. #error "CMSIS RTOS requires CH_CFG_USE_EVENTS_TIMEOUT"
  98. #endif
  99. #if !CH_CFG_USE_SEMAPHORES
  100. #error "CMSIS RTOS requires CH_CFG_USE_SEMAPHORES"
  101. #endif
  102. #if !CH_CFG_USE_DYNAMIC
  103. #error "CMSIS RTOS requires CH_CFG_USE_DYNAMIC"
  104. #endif
  105. /*===========================================================================*/
  106. /* Module data structures and types. */
  107. /*===========================================================================*/
  108. /**
  109. * @brief Type of priority levels.
  110. */
  111. typedef enum {
  112. osPriorityIdle = -3,
  113. osPriorityLow = -2,
  114. osPriorityBelowNormal = -1,
  115. osPriorityNormal = 0,
  116. osPriorityAboveNormal = +1,
  117. osPriorityHigh = +2,
  118. osPriorityRealtime = +3,
  119. osPriorityError = 0x84
  120. } osPriority;
  121. /**
  122. * @brief Type of error codes.
  123. */
  124. typedef enum {
  125. osOK = 0,
  126. osEventSignal = 0x08,
  127. osEventMessage = 0x10,
  128. osEventMail = 0x20,
  129. osEventTimeout = 0x40,
  130. osErrorParameter = 0x80,
  131. osErrorResource = 0x81,
  132. osErrorTimeoutResource = 0xC1,
  133. osErrorISR = 0x82,
  134. osErrorISRRecursive = 0x83,
  135. osErrorPriority = 0x84,
  136. osErrorNoMemory = 0x85,
  137. osErrorValue = 0x86,
  138. osErrorOS = 0xFF,
  139. os_status_reserved = 0x7FFFFFFF
  140. } osStatus;
  141. /**
  142. * @brief Type of a timer mode.
  143. */
  144. typedef enum {
  145. osTimerOnce = 0,
  146. osTimerPeriodic = 1
  147. } os_timer_type;
  148. /**
  149. * @brief Type of thread functions.
  150. */
  151. typedef void (*os_pthread) (void const *argument);
  152. /**
  153. * @brief Type of timer callback.
  154. */
  155. typedef void (*os_ptimer) (void const *argument);
  156. /**
  157. * @brief Type of pointer to thread control block.
  158. */
  159. typedef thread_t *osThreadId;
  160. /**
  161. * @brief Type of pointer to timer control block.
  162. */
  163. typedef struct os_timer_cb {
  164. virtual_timer_t vt;
  165. os_timer_type type;
  166. os_ptimer ptimer;
  167. void *argument;
  168. uint32_t millisec;
  169. } *osTimerId;
  170. /**
  171. * @brief Type of pointer to mutex control block.
  172. */
  173. typedef binary_semaphore_t *osMutexId;
  174. /**
  175. * @brief Type of pointer to semaphore control block.
  176. */
  177. typedef semaphore_t *osSemaphoreId;
  178. /**
  179. * @brief Type of pointer to memory pool control block.
  180. */
  181. typedef memory_pool_t *osPoolId;
  182. /**
  183. * @brief Type of pointer to message queue control block.
  184. */
  185. typedef struct mailbox *osMessageQId;
  186. /**
  187. * @brief Type of an event.
  188. */
  189. typedef struct {
  190. osStatus status;
  191. union {
  192. uint32_t v;
  193. void *p;
  194. int32_t signals;
  195. } value;
  196. union {
  197. /* osMailQId mail_id;*/
  198. osMessageQId message_id;
  199. } def;
  200. } osEvent;
  201. /**
  202. * @brief Type of a thread definition block.
  203. */
  204. typedef struct os_thread_def {
  205. os_pthread pthread;
  206. osPriority tpriority;
  207. uint32_t stacksize;
  208. const char *name;
  209. } osThreadDef_t;
  210. /**
  211. * @brief Type of a timer definition block.
  212. */
  213. typedef struct os_timer_def {
  214. os_ptimer ptimer;
  215. } osTimerDef_t;
  216. /**
  217. * @brief Type of a mutex definition block.
  218. */
  219. typedef struct os_mutex_def {
  220. uint32_t dummy;
  221. } osMutexDef_t;
  222. /**
  223. * @brief Type of a semaphore definition block.
  224. */
  225. typedef struct os_semaphore_def {
  226. uint32_t dummy;
  227. } osSemaphoreDef_t;
  228. /**
  229. * @brief Type of a memory pool definition block.
  230. */
  231. typedef struct os_pool_def {
  232. uint32_t pool_sz;
  233. uint32_t item_sz;
  234. memory_pool_t *pool;
  235. void *items;
  236. } osPoolDef_t;
  237. /**
  238. * @brief Type of a message queue definition block.
  239. */
  240. typedef struct os_messageQ_def {
  241. uint32_t queue_sz;
  242. uint32_t item_sz;
  243. mailbox_t *mailbox;
  244. void *items;
  245. } osMessageQDef_t;
  246. /*===========================================================================*/
  247. /* Module macros. */
  248. /*===========================================================================*/
  249. /**
  250. * @brief Convert a microseconds value to a RTOS kernel system timer value.
  251. */
  252. #define osKernelSysTickMicroSec(microsec) (((uint64_t)microsec * \
  253. (osKernelSysTickFrequency)) / \
  254. 1000000)
  255. /**
  256. * @brief Create a Thread definition.
  257. */
  258. #if defined(osObjectsExternal)
  259. #define osThreadDef(thd, priority, stacksz, name) \
  260. extern const osThreadDef_t os_thread_def_##thd
  261. #else
  262. #define osThreadDef(thd, priority, stacksz, name) \
  263. const osThreadDef_t os_thread_def_##thd = { \
  264. (thd), \
  265. (priority), \
  266. (stacksz), \
  267. (name) \
  268. }
  269. #endif
  270. /**
  271. * @brief Access a Thread definition.
  272. */
  273. #define osThread(name) &os_thread_def_##name
  274. /**
  275. * @brief Define a Timer object.
  276. */
  277. #if defined(osObjectsExternal)
  278. #define osTimerDef(name, function) \
  279. extern const osTimerDef_t os_timer_def_##name
  280. #else
  281. #define osTimerDef(name, function) \
  282. const osTimerDef_t os_timer_def_##name = { \
  283. (function) \
  284. }
  285. #endif
  286. /**
  287. * @brief Access a Timer definition.
  288. */
  289. #define osTimer(name) &os_timer_def_##name
  290. /**
  291. * @brief Define a Mutex.
  292. */
  293. #if defined(osObjectsExternal)
  294. #define osMutexDef(name) extern const osMutexDef_t os_mutex_def_##name
  295. #else
  296. #define osMutexDef(name) const osMutexDef_t os_mutex_def_##name = {0}
  297. #endif
  298. /**
  299. * @brief Access a Mutex definition.
  300. */
  301. #define osMutex(name) &os_mutex_def_##name
  302. /**
  303. * @brief Define a Semaphore.
  304. */
  305. #if defined(osObjectsExternal)
  306. #define osSemaphoreDef(name) \
  307. extern const osSemaphoreDef_t os_semaphore_def_##name
  308. #else // define the object
  309. #define osSemaphoreDef(name) \
  310. const osSemaphoreDef_t os_semaphore_def_##name = {0}
  311. #endif
  312. /**
  313. * @brief Access a Semaphore definition.
  314. */
  315. #define osSemaphore(name) &os_semaphore_def_##name
  316. /**
  317. * @brief Define a Memory Pool.
  318. */
  319. #if defined(osObjectsExternal)
  320. #define osPoolDef(name, no, type) \
  321. extern const osPoolDef_t os_pool_def_##name
  322. #else
  323. #define osPoolDef(name, no, type) \
  324. static const type os_pool_buf_##name[no]; \
  325. static memory_pool_t os_pool_obj_##name; \
  326. const osPoolDef_t os_pool_def_##name = { \
  327. (no), \
  328. sizeof (type), \
  329. (void *)&os_pool_obj_##name, \
  330. (void *)&os_pool_buf_##name[0] \
  331. }
  332. #endif
  333. /**
  334. * @brief Access a Memory Pool definition.
  335. */
  336. #define osPool(name) &os_pool_def_##name
  337. /**
  338. * @brief Define a Message Queue.
  339. */
  340. #if defined(osObjectsExternal)
  341. #define osMessageQDef(name, queue_sz, type) \
  342. extern const osMessageQDef_t os_messageQ_def_##name
  343. #else
  344. #define osMessageQDef(name, queue_sz, type) \
  345. static const msg_t os_messageQ_buf_##name[queue_sz]; \
  346. static mailbox_t os_messageQ_obj_##name; \
  347. const osMessageQDef_t os_messageQ_def_##name = { \
  348. (queue_sz), \
  349. sizeof (type), \
  350. (void *)&os_messageQ_obj_##name, \
  351. (void *)&os_messageQ_buf_##name[0] \
  352. }
  353. #endif
  354. /**
  355. * @brief Access a Message Queue definition.
  356. */
  357. #define osMessageQ(name) &os_messageQ_def_##name
  358. /*===========================================================================*/
  359. /* External declarations. */
  360. /*===========================================================================*/
  361. extern int32_t cmsis_os_started;
  362. #ifdef __cplusplus
  363. extern "C" {
  364. #endif
  365. osStatus osKernelInitialize(void);
  366. osStatus osKernelStart(void);
  367. osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *argument);
  368. osStatus osThreadTerminate(osThreadId thread_id);
  369. osStatus osThreadSetPriority(osThreadId thread_id, osPriority newprio);
  370. /*osEvent osWait(uint32_t millisec);*/
  371. osTimerId osTimerCreate(const osTimerDef_t *timer_def,
  372. os_timer_type type,
  373. void *argument);
  374. osStatus osTimerStart(osTimerId timer_id, uint32_t millisec);
  375. osStatus osTimerStop(osTimerId timer_id);
  376. osStatus osTimerDelete(osTimerId timer_id);
  377. int32_t osSignalSet(osThreadId thread_id, int32_t signals);
  378. int32_t osSignalClear(osThreadId thread_id, int32_t signals);
  379. osEvent osSignalWait(int32_t signals, uint32_t millisec);
  380. osSemaphoreId osSemaphoreCreate(const osSemaphoreDef_t *semaphore_def,
  381. int32_t count);
  382. int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec);
  383. osStatus osSemaphoreRelease(osSemaphoreId semaphore_id);
  384. osStatus osSemaphoreDelete(osSemaphoreId semaphore_id);
  385. osMutexId osMutexCreate(const osMutexDef_t *mutex_def);
  386. osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec);
  387. osStatus osMutexRelease(osMutexId mutex_id);
  388. osStatus osMutexDelete(osMutexId mutex_id);
  389. osPoolId osPoolCreate(const osPoolDef_t *pool_def);
  390. void *osPoolAlloc(osPoolId pool_id);
  391. void *osPoolCAlloc(osPoolId pool_id);
  392. osStatus osPoolFree(osPoolId pool_id, void *block);
  393. osMessageQId osMessageCreate(const osMessageQDef_t *queue_def,
  394. osThreadId thread_id);
  395. osStatus osMessagePut(osMessageQId queue_id,
  396. uint32_t info,
  397. uint32_t millisec);
  398. osEvent osMessageGet(osMessageQId queue_id,
  399. uint32_t millisec);
  400. #ifdef __cplusplus
  401. }
  402. #endif
  403. /*===========================================================================*/
  404. /* Module inline functions. */
  405. /*===========================================================================*/
  406. /**
  407. * @brief To be or not to be.
  408. */
  409. static inline int32_t osKernelRunning(void) {
  410. return cmsis_os_started;
  411. }
  412. /**
  413. * @brief System ticks since start.
  414. */
  415. static inline uint32_t osKernelSysTick(void) {
  416. return (uint32_t)chVTGetSystemTimeX();
  417. }
  418. /**
  419. * @brief Returns the current thread.
  420. */
  421. static inline osThreadId osThreadGetId(void) {
  422. return (osThreadId)chThdGetSelfX();
  423. }
  424. /**
  425. * @brief Thread time slice yield.
  426. */
  427. static inline osStatus osThreadYield(void) {
  428. chThdYield();
  429. return osOK;
  430. }
  431. /**
  432. * @brief Returns priority of a thread.
  433. */
  434. static inline osPriority osThreadGetPriority(osThreadId thread_id) {
  435. return (osPriority)(NORMALPRIO - thread_id->prio);
  436. }
  437. /**
  438. * @brief Thread delay in milliseconds.
  439. */
  440. static inline osStatus osDelay(uint32_t millisec) {
  441. chThdSleepMilliseconds(millisec);
  442. return osOK;
  443. }
  444. #endif /* CMSIS_OS_H */
  445. /** @} */