cmsis_os.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* ----------------------------------------------------------------------
  2. * $Date: 5. February 2013
  3. * $Revision: V1.02
  4. *
  5. * Project: CMSIS-RTOS API
  6. * Title: cmsis_os.h RTX header file
  7. *
  8. * Version 0.02
  9. * Initial Proposal Phase
  10. * Version 0.03
  11. * osKernelStart added, optional feature: main started as thread
  12. * osSemaphores have standard behavior
  13. * osTimerCreate does not start the timer, added osTimerStart
  14. * osThreadPass is renamed to osThreadYield
  15. * Version 1.01
  16. * Support for C++ interface
  17. * - const attribute removed from the osXxxxDef_t typedef's
  18. * - const attribute added to the osXxxxDef macros
  19. * Added: osTimerDelete, osMutexDelete, osSemaphoreDelete
  20. * Added: osKernelInitialize
  21. * Version 1.02
  22. * Control functions for short timeouts in microsecond resolution:
  23. * Added: osKernelSysTick, osKernelSysTickFrequency, osKernelSysTickMicroSec
  24. * Removed: osSignalGet
  25. *----------------------------------------------------------------------------
  26. *
  27. * Copyright (c) 2013-2017 ARM LIMITED. All rights reserved.
  28. *
  29. * SPDX-License-Identifier: Apache-2.0
  30. *
  31. * Licensed under the Apache License, Version 2.0 (the License); you may
  32. * not use this file except in compliance with the License.
  33. * You may obtain a copy of the License at
  34. *
  35. * www.apache.org/licenses/LICENSE-2.0
  36. *
  37. * Unless required by applicable law or agreed to in writing, software
  38. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  39. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  40. * See the License for the specific language governing permissions and
  41. * limitations under the License.
  42. *---------------------------------------------------------------------------*/
  43. #ifndef _CMSIS_OS_H
  44. #define _CMSIS_OS_H
  45. #define osCMSIS 0x10002U ///< CMSIS-RTOS API version (main [31:16] .sub [15:0])
  46. #define osCMSIS_RTX ((4<<16)|82) ///< RTOS identification and version (main [31:16] .sub [15:0])
  47. #define osKernelSystemId "RTX V4.82" ///< RTOS identification string
  48. #define osFeature_MainThread 1 ///< main can be thread
  49. #define osFeature_Pool 1 ///< Memory Pools available
  50. #define osFeature_MailQ 1 ///< Mail Queues available
  51. #define osFeature_MessageQ 1 ///< Message Queues available
  52. #define osFeature_Signals 16 ///< 16 Signal Flags available per thread
  53. #define osFeature_Semaphore 65535 ///< Maximum count for \ref osSemaphoreCreate function
  54. #define osFeature_Wait 0 ///< osWait not available
  55. #define osFeature_SysTick 1 ///< osKernelSysTick functions available
  56. #if defined(__CC_ARM)
  57. #define os_InRegs __value_in_regs // Compiler specific: force struct in registers
  58. #else
  59. #define os_InRegs
  60. #endif
  61. #if defined(__CC_ARM)
  62. #define __NO_RETURN __declspec(noreturn)
  63. #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  64. #define __NO_RETURN __attribute__((noreturn))
  65. #elif defined(__GNUC__)
  66. #define __NO_RETURN __attribute__((noreturn))
  67. #elif defined(__ICCARM__)
  68. #define __NO_RETURN __noreturn
  69. #else
  70. #define __NO_RETURN
  71. #endif
  72. #include <stdint.h>
  73. #include <stddef.h>
  74. #ifdef __cplusplus
  75. extern "C"
  76. {
  77. #endif
  78. // ==== Enumeration, structures, defines ====
  79. /// Priority used for thread control.
  80. typedef enum {
  81. osPriorityIdle = -3, ///< priority: idle (lowest)
  82. osPriorityLow = -2, ///< priority: low
  83. osPriorityBelowNormal = -1, ///< priority: below normal
  84. osPriorityNormal = 0, ///< priority: normal (default)
  85. osPriorityAboveNormal = +1, ///< priority: above normal
  86. osPriorityHigh = +2, ///< priority: high
  87. osPriorityRealtime = +3, ///< priority: realtime (highest)
  88. osPriorityError = 0x84, ///< system cannot determine priority or thread has illegal priority
  89. os_priority_reserved = 0x7FFFFFFF ///< prevent from enum down-size compiler optimization.
  90. } osPriority;
  91. /// Timeout value.
  92. #define osWaitForever 0xFFFFFFFFU ///< wait forever timeout value
  93. /// Status code values returned by CMSIS-RTOS functions.
  94. typedef enum {
  95. osOK = 0, ///< function completed; no error or event occurred.
  96. osEventSignal = 0x08, ///< function completed; signal event occurred.
  97. osEventMessage = 0x10, ///< function completed; message event occurred.
  98. osEventMail = 0x20, ///< function completed; mail event occurred.
  99. osEventTimeout = 0x40, ///< function completed; timeout occurred.
  100. osErrorParameter = 0x80, ///< parameter error: a mandatory parameter was missing or specified an incorrect object.
  101. osErrorResource = 0x81, ///< resource not available: a specified resource was not available.
  102. osErrorTimeoutResource = 0xC1, ///< resource not available within given time: a specified resource was not available within the timeout period.
  103. osErrorISR = 0x82, ///< not allowed in ISR context: the function cannot be called from interrupt service routines.
  104. osErrorISRRecursive = 0x83, ///< function called multiple times from ISR with same object.
  105. osErrorPriority = 0x84, ///< system cannot determine priority or thread has illegal priority.
  106. osErrorNoMemory = 0x85, ///< system is out of memory: it was impossible to allocate or reserve memory for the operation.
  107. osErrorValue = 0x86, ///< value of a parameter is out of range.
  108. osErrorOS = 0xFF, ///< unspecified RTOS error: run-time error but no other error message fits.
  109. os_status_reserved = 0x7FFFFFFF ///< prevent from enum down-size compiler optimization.
  110. } osStatus;
  111. /// Timer type value for the timer definition.
  112. typedef enum {
  113. osTimerOnce = 0, ///< one-shot timer
  114. osTimerPeriodic = 1 ///< repeating timer
  115. } os_timer_type;
  116. /// Entry point of a thread.
  117. typedef void (*os_pthread) (void const *argument);
  118. /// Entry point of a timer call back function.
  119. typedef void (*os_ptimer) (void const *argument);
  120. // >>> the following data type definitions may shall adapted towards a specific RTOS
  121. /// Thread ID identifies the thread (pointer to a thread control block).
  122. typedef struct os_thread_cb *osThreadId;
  123. /// Timer ID identifies the timer (pointer to a timer control block).
  124. typedef struct os_timer_cb *osTimerId;
  125. /// Mutex ID identifies the mutex (pointer to a mutex control block).
  126. typedef struct os_mutex_cb *osMutexId;
  127. /// Semaphore ID identifies the semaphore (pointer to a semaphore control block).
  128. typedef struct os_semaphore_cb *osSemaphoreId;
  129. /// Pool ID identifies the memory pool (pointer to a memory pool control block).
  130. typedef struct os_pool_cb *osPoolId;
  131. /// Message ID identifies the message queue (pointer to a message queue control block).
  132. typedef struct os_messageQ_cb *osMessageQId;
  133. /// Mail ID identifies the mail queue (pointer to a mail queue control block).
  134. typedef struct os_mailQ_cb *osMailQId;
  135. /// Thread Definition structure contains startup information of a thread.
  136. typedef struct os_thread_def {
  137. os_pthread pthread; ///< start address of thread function
  138. osPriority tpriority; ///< initial thread priority
  139. uint32_t instances; ///< maximum number of instances of that thread function
  140. uint32_t stacksize; ///< stack size requirements in bytes; 0 is default stack size
  141. } osThreadDef_t;
  142. /// Timer Definition structure contains timer parameters.
  143. typedef struct os_timer_def {
  144. os_ptimer ptimer; ///< start address of a timer function
  145. void *timer; ///< pointer to internal data
  146. } osTimerDef_t;
  147. /// Mutex Definition structure contains setup information for a mutex.
  148. typedef struct os_mutex_def {
  149. void *mutex; ///< pointer to internal data
  150. } osMutexDef_t;
  151. /// Semaphore Definition structure contains setup information for a semaphore.
  152. typedef struct os_semaphore_def {
  153. void *semaphore; ///< pointer to internal data
  154. } osSemaphoreDef_t;
  155. /// Definition structure for memory block allocation.
  156. typedef struct os_pool_def {
  157. uint32_t pool_sz; ///< number of items (elements) in the pool
  158. uint32_t item_sz; ///< size of an item
  159. void *pool; ///< pointer to memory for pool
  160. } osPoolDef_t;
  161. /// Definition structure for message queue.
  162. typedef struct os_messageQ_def {
  163. uint32_t queue_sz; ///< number of elements in the queue
  164. void *pool; ///< memory array for messages
  165. } osMessageQDef_t;
  166. /// Definition structure for mail queue.
  167. typedef struct os_mailQ_def {
  168. uint32_t queue_sz; ///< number of elements in the queue
  169. uint32_t item_sz; ///< size of an item
  170. void *pool; ///< memory array for mail
  171. } osMailQDef_t;
  172. /// Event structure contains detailed information about an event.
  173. typedef struct {
  174. osStatus status; ///< status code: event or error information
  175. union {
  176. uint32_t v; ///< message as 32-bit value
  177. void *p; ///< message or mail as void pointer
  178. int32_t signals; ///< signal flags
  179. } value; ///< event value
  180. union {
  181. osMailQId mail_id; ///< mail id obtained by \ref osMailCreate
  182. osMessageQId message_id; ///< message id obtained by \ref osMessageCreate
  183. } def; ///< event definition
  184. } osEvent;
  185. // ==== Kernel Control Functions ====
  186. /// Initialize the RTOS Kernel for creating objects.
  187. /// \return status code that indicates the execution status of the function.
  188. osStatus osKernelInitialize (void);
  189. /// Start the RTOS Kernel.
  190. /// \return status code that indicates the execution status of the function.
  191. osStatus osKernelStart (void);
  192. /// Check if the RTOS kernel is already started.
  193. /// \return 0 RTOS is not started, 1 RTOS is started.
  194. int32_t osKernelRunning(void);
  195. #if (defined (osFeature_SysTick) && (osFeature_SysTick != 0)) // System Timer available
  196. /// \cond INTERNAL_VARIABLES
  197. extern uint32_t const os_tickfreq;
  198. extern uint16_t const os_tickus_i;
  199. extern uint16_t const os_tickus_f;
  200. /// \endcond
  201. /// Get the RTOS kernel system timer counter.
  202. /// \return RTOS kernel system timer as 32-bit value
  203. uint32_t osKernelSysTick (void);
  204. /// The RTOS kernel system timer frequency in Hz.
  205. /// \note Reflects the system timer setting and is typically defined in a configuration file.
  206. #define osKernelSysTickFrequency os_tickfreq
  207. /// Convert a microseconds value to a RTOS kernel system timer value.
  208. /// \param microsec time value in microseconds.
  209. /// \return time value normalized to the \ref osKernelSysTickFrequency
  210. /*
  211. #define osKernelSysTickMicroSec(microsec) (((uint64_t)microsec * (osKernelSysTickFrequency)) / 1000000)
  212. */
  213. #define osKernelSysTickMicroSec(microsec) ((microsec * os_tickus_i) + ((microsec * os_tickus_f) >> 16))
  214. #endif // System Timer available
  215. // ==== Thread Management ====
  216. /// Create a Thread Definition with function, priority, and stack requirements.
  217. /// \param name name of the thread function.
  218. /// \param priority initial priority of the thread function.
  219. /// \param instances number of possible thread instances.
  220. /// \param stacksz stack size (in bytes) requirements for the thread function.
  221. /// macro body is implementation specific in every CMSIS-RTOS.
  222. #if defined (osObjectsExternal) // object is external
  223. #define osThreadDef(name, priority, instances, stacksz) \
  224. extern const osThreadDef_t os_thread_def_##name
  225. #else // define the object
  226. #define osThreadDef(name, priority, instances, stacksz) \
  227. const osThreadDef_t os_thread_def_##name = \
  228. { (name), (priority), (instances), (stacksz) }
  229. #endif
  230. /// Access a Thread definition.
  231. /// \param name name of the thread definition object.
  232. /// macro body is implementation specific in every CMSIS-RTOS.
  233. #define osThread(name) \
  234. &os_thread_def_##name
  235. /// Create a thread and add it to Active Threads and set it to state READY.
  236. /// \param[in] thread_def thread definition referenced with \ref osThread.
  237. /// \param[in] argument pointer that is passed to the thread function as start argument.
  238. /// \return thread ID for reference by other functions or NULL in case of error.
  239. osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument);
  240. /// Return the thread ID of the current running thread.
  241. /// \return thread ID for reference by other functions or NULL in case of error.
  242. osThreadId osThreadGetId (void);
  243. /// Terminate execution of a thread and remove it from Active Threads.
  244. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  245. /// \return status code that indicates the execution status of the function.
  246. osStatus osThreadTerminate (osThreadId thread_id);
  247. /// Pass control to next thread that is in state \b READY.
  248. /// \return status code that indicates the execution status of the function.
  249. osStatus osThreadYield (void);
  250. /// Change priority of an active thread.
  251. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  252. /// \param[in] priority new priority value for the thread function.
  253. /// \return status code that indicates the execution status of the function.
  254. osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority);
  255. /// Get current priority of an active thread.
  256. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  257. /// \return current priority value of the thread function.
  258. osPriority osThreadGetPriority (osThreadId thread_id);
  259. // ==== Generic Wait Functions ====
  260. /// Wait for Timeout (Time Delay).
  261. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue "Time delay" value
  262. /// \return status code that indicates the execution status of the function.
  263. osStatus osDelay (uint32_t millisec);
  264. #if (defined (osFeature_Wait) && (osFeature_Wait != 0)) // Generic Wait available
  265. /// Wait for Signal, Message, Mail, or Timeout.
  266. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  267. /// \return event that contains signal, message, or mail information or error code.
  268. os_InRegs osEvent osWait (uint32_t millisec);
  269. #endif // Generic Wait available
  270. // ==== Timer Management Functions ====
  271. /// Define a Timer object.
  272. /// \param name name of the timer object.
  273. /// \param function name of the timer call back function.
  274. #if defined (osObjectsExternal) // object is external
  275. #define osTimerDef(name, function) \
  276. extern const osTimerDef_t os_timer_def_##name
  277. #else // define the object
  278. #define osTimerDef(name, function) \
  279. uint32_t os_timer_cb_##name[6]; \
  280. const osTimerDef_t os_timer_def_##name = \
  281. { (function), (os_timer_cb_##name) }
  282. #endif
  283. /// Access a Timer definition.
  284. /// \param name name of the timer object.
  285. #define osTimer(name) \
  286. &os_timer_def_##name
  287. /// Create a timer.
  288. /// \param[in] timer_def timer object referenced with \ref osTimer.
  289. /// \param[in] type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior.
  290. /// \param[in] argument argument to the timer call back function.
  291. /// \return timer ID for reference by other functions or NULL in case of error.
  292. osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument);
  293. /// Start or restart a timer.
  294. /// \param[in] timer_id timer ID obtained by \ref osTimerCreate.
  295. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue "Time delay" value of the timer.
  296. /// \return status code that indicates the execution status of the function.
  297. osStatus osTimerStart (osTimerId timer_id, uint32_t millisec);
  298. /// Stop the timer.
  299. /// \param[in] timer_id timer ID obtained by \ref osTimerCreate.
  300. /// \return status code that indicates the execution status of the function.
  301. osStatus osTimerStop (osTimerId timer_id);
  302. /// Delete a timer that was created by \ref osTimerCreate.
  303. /// \param[in] timer_id timer ID obtained by \ref osTimerCreate.
  304. /// \return status code that indicates the execution status of the function.
  305. osStatus osTimerDelete (osTimerId timer_id);
  306. // ==== Signal Management ====
  307. /// Set the specified Signal Flags of an active thread.
  308. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  309. /// \param[in] signals specifies the signal flags of the thread that should be set.
  310. /// \return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
  311. int32_t osSignalSet (osThreadId thread_id, int32_t signals);
  312. /// Clear the specified Signal Flags of an active thread.
  313. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  314. /// \param[in] signals specifies the signal flags of the thread that shall be cleared.
  315. /// \return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters or call from ISR.
  316. int32_t osSignalClear (osThreadId thread_id, int32_t signals);
  317. /// Wait for one or more Signal Flags to become signaled for the current \b RUNNING thread.
  318. /// \param[in] signals wait until all specified signal flags set or 0 for any single signal flag.
  319. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  320. /// \return event flag information or error code.
  321. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  322. #define osSignalWait __osSignalWait
  323. osEvent __osSignalWait (int32_t signals, uint32_t millisec);
  324. #else
  325. os_InRegs osEvent osSignalWait (int32_t signals, uint32_t millisec);
  326. #endif
  327. // ==== Mutex Management ====
  328. /// Define a Mutex.
  329. /// \param name name of the mutex object.
  330. #if defined (osObjectsExternal) // object is external
  331. #define osMutexDef(name) \
  332. extern const osMutexDef_t os_mutex_def_##name
  333. #else // define the object
  334. #define osMutexDef(name) \
  335. uint32_t os_mutex_cb_##name[4] = { 0 }; \
  336. const osMutexDef_t os_mutex_def_##name = { (os_mutex_cb_##name) }
  337. #endif
  338. /// Access a Mutex definition.
  339. /// \param name name of the mutex object.
  340. #define osMutex(name) \
  341. &os_mutex_def_##name
  342. /// Create and Initialize a Mutex object.
  343. /// \param[in] mutex_def mutex definition referenced with \ref osMutex.
  344. /// \return mutex ID for reference by other functions or NULL in case of error.
  345. osMutexId osMutexCreate (const osMutexDef_t *mutex_def);
  346. /// Wait until a Mutex becomes available.
  347. /// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
  348. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  349. /// \return status code that indicates the execution status of the function.
  350. osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec);
  351. /// Release a Mutex that was obtained by \ref osMutexWait.
  352. /// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
  353. /// \return status code that indicates the execution status of the function.
  354. osStatus osMutexRelease (osMutexId mutex_id);
  355. /// Delete a Mutex that was created by \ref osMutexCreate.
  356. /// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
  357. /// \return status code that indicates the execution status of the function.
  358. osStatus osMutexDelete (osMutexId mutex_id);
  359. // ==== Semaphore Management Functions ====
  360. #if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0)) // Semaphore available
  361. /// Define a Semaphore object.
  362. /// \param name name of the semaphore object.
  363. #if defined (osObjectsExternal) // object is external
  364. #define osSemaphoreDef(name) \
  365. extern const osSemaphoreDef_t os_semaphore_def_##name
  366. #else // define the object
  367. #define osSemaphoreDef(name) \
  368. uint32_t os_semaphore_cb_##name[2] = { 0 }; \
  369. const osSemaphoreDef_t os_semaphore_def_##name = { (os_semaphore_cb_##name) }
  370. #endif
  371. /// Access a Semaphore definition.
  372. /// \param name name of the semaphore object.
  373. #define osSemaphore(name) \
  374. &os_semaphore_def_##name
  375. /// Create and Initialize a Semaphore object used for managing resources.
  376. /// \param[in] semaphore_def semaphore definition referenced with \ref osSemaphore.
  377. /// \param[in] count number of available resources.
  378. /// \return semaphore ID for reference by other functions or NULL in case of error.
  379. osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count);
  380. /// Wait until a Semaphore token becomes available.
  381. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate.
  382. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  383. /// \return number of available tokens, or -1 in case of incorrect parameters.
  384. int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec);
  385. /// Release a Semaphore token.
  386. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate.
  387. /// \return status code that indicates the execution status of the function.
  388. osStatus osSemaphoreRelease (osSemaphoreId semaphore_id);
  389. /// Delete a Semaphore that was created by \ref osSemaphoreCreate.
  390. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate.
  391. /// \return status code that indicates the execution status of the function.
  392. osStatus osSemaphoreDelete (osSemaphoreId semaphore_id);
  393. #endif // Semaphore available
  394. // ==== Memory Pool Management Functions ====
  395. #if (defined (osFeature_Pool) && (osFeature_Pool != 0)) // Memory Pool Management available
  396. /// \brief Define a Memory Pool.
  397. /// \param name name of the memory pool.
  398. /// \param no maximum number of blocks (objects) in the memory pool.
  399. /// \param type data type of a single block (object).
  400. #if defined (osObjectsExternal) // object is external
  401. #define osPoolDef(name, no, type) \
  402. extern const osPoolDef_t os_pool_def_##name
  403. #else // define the object
  404. #define osPoolDef(name, no, type) \
  405. uint32_t os_pool_m_##name[3+((sizeof(type)+3)/4)*(no)]; \
  406. const osPoolDef_t os_pool_def_##name = \
  407. { (no), sizeof(type), (os_pool_m_##name) }
  408. #endif
  409. /// \brief Access a Memory Pool definition.
  410. /// \param name name of the memory pool
  411. #define osPool(name) \
  412. &os_pool_def_##name
  413. /// Create and Initialize a memory pool.
  414. /// \param[in] pool_def memory pool definition referenced with \ref osPool.
  415. /// \return memory pool ID for reference by other functions or NULL in case of error.
  416. osPoolId osPoolCreate (const osPoolDef_t *pool_def);
  417. /// Allocate a memory block from a memory pool.
  418. /// \param[in] pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  419. /// \return address of the allocated memory block or NULL in case of no memory available.
  420. void *osPoolAlloc (osPoolId pool_id);
  421. /// Allocate a memory block from a memory pool and set memory block to zero.
  422. /// \param[in] pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  423. /// \return address of the allocated memory block or NULL in case of no memory available.
  424. void *osPoolCAlloc (osPoolId pool_id);
  425. /// Return an allocated memory block back to a specific memory pool.
  426. /// \param[in] pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  427. /// \param[in] block address of the allocated memory block that is returned to the memory pool.
  428. /// \return status code that indicates the execution status of the function.
  429. osStatus osPoolFree (osPoolId pool_id, void *block);
  430. #endif // Memory Pool Management available
  431. // ==== Message Queue Management Functions ====
  432. #if (defined (osFeature_MessageQ) && (osFeature_MessageQ != 0)) // Message Queues available
  433. /// \brief Create a Message Queue Definition.
  434. /// \param name name of the queue.
  435. /// \param queue_sz maximum number of messages in the queue.
  436. /// \param type data type of a single message element (for debugger).
  437. #if defined (osObjectsExternal) // object is external
  438. #define osMessageQDef(name, queue_sz, type) \
  439. extern const osMessageQDef_t os_messageQ_def_##name
  440. #else // define the object
  441. #define osMessageQDef(name, queue_sz, type) \
  442. uint32_t os_messageQ_q_##name[4+(queue_sz)] = { 0 }; \
  443. const osMessageQDef_t os_messageQ_def_##name = \
  444. { (queue_sz), (os_messageQ_q_##name) }
  445. #endif
  446. /// \brief Access a Message Queue Definition.
  447. /// \param name name of the queue
  448. #define osMessageQ(name) \
  449. &os_messageQ_def_##name
  450. /// Create and Initialize a Message Queue.
  451. /// \param[in] queue_def queue definition referenced with \ref osMessageQ.
  452. /// \param[in] thread_id thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
  453. /// \return message queue ID for reference by other functions or NULL in case of error.
  454. osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id);
  455. /// Put a Message to a Queue.
  456. /// \param[in] queue_id message queue ID obtained with \ref osMessageCreate.
  457. /// \param[in] info message information.
  458. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  459. /// \return status code that indicates the execution status of the function.
  460. osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec);
  461. /// Get a Message or Wait for a Message from a Queue.
  462. /// \param[in] queue_id message queue ID obtained with \ref osMessageCreate.
  463. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  464. /// \return event information that includes status code.
  465. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  466. #define osMessageGet __osMessageGet
  467. osEvent __osMessageGet (osMessageQId queue_id, uint32_t millisec);
  468. #else
  469. os_InRegs osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec);
  470. #endif
  471. #endif // Message Queues available
  472. // ==== Mail Queue Management Functions ====
  473. #if (defined (osFeature_MailQ) && (osFeature_MailQ != 0)) // Mail Queues available
  474. /// \brief Create a Mail Queue Definition.
  475. /// \param name name of the queue
  476. /// \param queue_sz maximum number of messages in queue
  477. /// \param type data type of a single message element
  478. #if defined (osObjectsExternal) // object is external
  479. #define osMailQDef(name, queue_sz, type) \
  480. extern const osMailQDef_t os_mailQ_def_##name
  481. #else // define the object
  482. #define osMailQDef(name, queue_sz, type) \
  483. uint32_t os_mailQ_q_##name[4+(queue_sz)] = { 0 }; \
  484. uint32_t os_mailQ_m_##name[3+((sizeof(type)+3)/4)*(queue_sz)]; \
  485. void * os_mailQ_p_##name[2] = { (os_mailQ_q_##name), os_mailQ_m_##name }; \
  486. const osMailQDef_t os_mailQ_def_##name = \
  487. { (queue_sz), sizeof(type), (os_mailQ_p_##name) }
  488. #endif
  489. /// \brief Access a Mail Queue Definition.
  490. /// \param name name of the queue
  491. #define osMailQ(name) \
  492. &os_mailQ_def_##name
  493. /// Create and Initialize mail queue.
  494. /// \param[in] queue_def reference to the mail queue definition obtain with \ref osMailQ
  495. /// \param[in] thread_id thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
  496. /// \return mail queue ID for reference by other functions or NULL in case of error.
  497. osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id);
  498. /// Allocate a memory block from a mail.
  499. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  500. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  501. /// \return pointer to memory block that can be filled with mail or NULL in case of error.
  502. void *osMailAlloc (osMailQId queue_id, uint32_t millisec);
  503. /// Allocate a memory block from a mail and set memory block to zero.
  504. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  505. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  506. /// \return pointer to memory block that can be filled with mail or NULL in case of error.
  507. void *osMailCAlloc (osMailQId queue_id, uint32_t millisec);
  508. /// Put a mail to a queue.
  509. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  510. /// \param[in] mail memory block previously allocated with \ref osMailAlloc or \ref osMailCAlloc.
  511. /// \return status code that indicates the execution status of the function.
  512. osStatus osMailPut (osMailQId queue_id, void *mail);
  513. /// Get a mail from a queue.
  514. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  515. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  516. /// \return event that contains mail information or error code.
  517. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  518. #define osMailGet __osMailGet
  519. osEvent __osMailGet (osMailQId queue_id, uint32_t millisec);
  520. #else
  521. os_InRegs osEvent osMailGet (osMailQId queue_id, uint32_t millisec);
  522. #endif
  523. /// Free a memory block from a mail.
  524. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  525. /// \param[in] mail pointer to the memory block that was obtained with \ref osMailGet.
  526. /// \return status code that indicates the execution status of the function.
  527. osStatus osMailFree (osMailQId queue_id, void *mail);
  528. #endif // Mail Queues available
  529. // ==== RTX Extensions ====
  530. /// Suspend the RTX task scheduler.
  531. /// \return number of ticks, for how long the system can sleep or power-down.
  532. uint32_t os_suspend (void);
  533. /// Resume the RTX task scheduler.
  534. /// \param[in] sleep_time specifies how long the system was in sleep or power-down mode.
  535. void os_resume (uint32_t sleep_time);
  536. /// OS idle demon (running when no other thread is ready to run).
  537. __NO_RETURN void os_idle_demon (void);
  538. /// OS error callback (called when a runtime error is detected).
  539. /// \param[in] error_code actual error code that has been detected.
  540. __NO_RETURN void os_error (uint32_t error_code);
  541. #ifdef __cplusplus
  542. }
  543. #endif
  544. #endif // _CMSIS_OS_H