cmsis_os.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /* --------------------------------------------------------------------------
  2. * Portions Copyright © 2017 STMicroelectronics International N.V. All rights reserved.
  3. * Portions Copyright (c) 2013-2017 ARM Limited. All rights reserved.
  4. * --------------------------------------------------------------------------
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the License); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Project: CMSIS-RTOS API
  21. * Title: cmsis_os.h header file
  22. *
  23. * Version 0.02
  24. * Initial Proposal Phase
  25. * Version 0.03
  26. * osKernelStart added, optional feature: main started as thread
  27. * osSemaphores have standard behavior
  28. * osTimerCreate does not start the timer, added osTimerStart
  29. * osThreadPass is renamed to osThreadYield
  30. * Version 1.01
  31. * Support for C++ interface
  32. * - const attribute removed from the osXxxxDef_t typedefs
  33. * - const attribute added to the osXxxxDef macros
  34. * Added: osTimerDelete, osMutexDelete, osSemaphoreDelete
  35. * Added: osKernelInitialize
  36. * Version 1.02
  37. * Control functions for short timeouts in microsecond resolution:
  38. * Added: osKernelSysTick, osKernelSysTickFrequency, osKernelSysTickMicroSec
  39. * Removed: osSignalGet
  40. * Version 2.0.0
  41. * OS objects creation without macros (dynamic creation and resource allocation):
  42. * - added: osXxxxNew functions which replace osXxxxCreate
  43. * - added: osXxxxAttr_t structures
  44. * - deprecated: osXxxxCreate functions, osXxxxDef_t structures
  45. * - deprecated: osXxxxDef and osXxxx macros
  46. * osStatus codes simplified and renamed to osStatus_t
  47. * osEvent return structure deprecated
  48. * Kernel:
  49. * - added: osKernelInfo_t and osKernelGetInfo
  50. * - added: osKernelState_t and osKernelGetState (replaces osKernelRunning)
  51. * - added: osKernelLock, osKernelUnlock
  52. * - added: osKernelSuspend, osKernelResume
  53. * - added: osKernelGetTickCount, osKernelGetTickFreq
  54. * - renamed osKernelSysTick to osKernelGetSysTimerCount
  55. * - replaced osKernelSysTickFrequency with osKernelGetSysTimerFreq
  56. * - deprecated osKernelSysTickMicroSec
  57. * Thread:
  58. * - extended number of thread priorities
  59. * - renamed osPrioriry to osPrioriry_t
  60. * - replaced osThreadCreate with osThreadNew
  61. * - added: osThreadGetName
  62. * - added: osThreadState_t and osThreadGetState
  63. * - added: osThreadGetStackSize, osThreadGetStackSpace
  64. * - added: osThreadSuspend, osThreadResume
  65. * - added: osThreadJoin, osThreadDetach, osThreadExit
  66. * - added: osThreadGetCount, osThreadEnumerate
  67. * - added: Thread Flags (moved from Signals)
  68. * Signals:
  69. * - renamed osSignals to osThreadFlags (moved to Thread Flags)
  70. * - changed return value of Set/Clear/Wait functions
  71. * - Clear function limited to current running thread
  72. * - extended Wait function (options)
  73. * - added: osThreadFlagsGet
  74. * Event Flags:
  75. * - added new independent object for handling Event Flags
  76. * Delay and Wait functions:
  77. * - added: osDelayUntil
  78. * - deprecated: osWait
  79. * Timer:
  80. * - replaced osTimerCreate with osTimerNew
  81. * - added: osTimerGetName, osTimerIsRunning
  82. * Mutex:
  83. * - extended: attributes (Recursive, Priority Inherit, Robust)
  84. * - replaced osMutexCreate with osMutexNew
  85. * - renamed osMutexWait to osMutexAcquire
  86. * - added: osMutexGetName, osMutexGetOwner
  87. * Semaphore:
  88. * - extended: maximum and initial token count
  89. * - replaced osSemaphoreCreate with osSemaphoreNew
  90. * - renamed osSemaphoreWait to osSemaphoreAcquire (changed return value)
  91. * - added: osSemaphoreGetName, osSemaphoreGetCount
  92. * Memory Pool:
  93. * - using osMemoryPool prefix instead of osPool
  94. * - replaced osPoolCreate with osMemoryPoolNew
  95. * - extended osMemoryPoolAlloc (timeout)
  96. * - added: osMemoryPoolGetName
  97. * - added: osMemoryPoolGetCapacity, osMemoryPoolGetBlockSize
  98. * - added: osMemoryPoolGetCount, osMemoryPoolGetSpace
  99. * - added: osMemoryPoolDelete
  100. * - deprecated: osPoolCAlloc
  101. * Message Queue:
  102. * - extended: fixed size message instead of a single 32-bit value
  103. * - using osMessageQueue prefix instead of osMessage
  104. * - replaced osMessageCreate with osMessageQueueNew
  105. * - updated: osMessageQueuePut, osMessageQueueGet
  106. * - added: osMessageQueueGetName
  107. * - added: osMessageQueueGetCapacity, osMessageQueueGetMsgSize
  108. * - added: osMessageQueueGetCount, osMessageQueueGetSpace
  109. * - added: osMessageQueueReset, osMessageQueueDelete
  110. * Mail Queue:
  111. * - deprecated (superseded by extended Message Queue functionality)
  112. * Version 2.1.0
  113. * Support for critical and uncritical sections (nesting safe):
  114. * - updated: osKernelLock, osKernelUnlock
  115. * - added: osKernelRestoreLock
  116. * Updated Thread and Event Flags:
  117. * - changed flags parameter and return type from int32_t to uint32_t
  118. *---------------------------------------------------------------------------*/
  119. #ifndef CMSIS_OS_H_
  120. #define CMSIS_OS_H_
  121. #define osCMSIS 0x20001U ///< API version (main[31:16].sub[15:0])
  122. #define osCMSIS_FreeRTOS 0xA0001U ///< RTOS identification and version (main[31:16].sub[15:0])
  123. #define osKernelSystemId "FreeRTOS V10.0.1" ///< RTOS identification string
  124. #define osFeature_MainThread 0 ///< main thread 1=main can be thread, 0=not available
  125. #define osFeature_Signals 24U ///< maximum number of Signal Flags available per thread
  126. #define osFeature_Semaphore 65535U ///< maximum count for \ref osSemaphoreCreate function
  127. #define osFeature_Wait 0 ///< osWait function: 1=available, 0=not available
  128. #define osFeature_SysTick 1 ///< osKernelSysTick functions: 1=available, 0=not available
  129. #define osFeature_Pool 0 ///< Memory Pools: 1=available, 0=not available
  130. #define osFeature_MessageQ 1 ///< Message Queues: 1=available, 0=not available
  131. #define osFeature_MailQ 0 ///< Mail Queues: 1=available, 0=not available
  132. #if defined(__CC_ARM)
  133. #define os_InRegs __value_in_regs
  134. #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  135. #define os_InRegs __attribute__((value_in_regs))
  136. #else
  137. #define os_InRegs
  138. #endif
  139. #include "cmsis_os2.h"
  140. #include "FreeRTOS.h"
  141. #ifdef __cplusplus
  142. extern "C"
  143. {
  144. #endif
  145. // ==== Enumerations, structures, defines ====
  146. /// Priority values.
  147. #if (osCMSIS < 0x20000U)
  148. typedef enum {
  149. osPriorityIdle = -3, ///< Priority: idle (lowest)
  150. osPriorityLow = -2, ///< Priority: low
  151. osPriorityBelowNormal = -1, ///< Priority: below normal
  152. osPriorityNormal = 0, ///< Priority: normal (default)
  153. osPriorityAboveNormal = +1, ///< Priority: above normal
  154. osPriorityHigh = +2, ///< Priority: high
  155. osPriorityRealtime = +3, ///< Priority: realtime (highest)
  156. osPriorityError = 0x84, ///< System cannot determine priority or illegal priority.
  157. osPriorityReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
  158. } osPriority;
  159. #else
  160. #define osPriority osPriority_t
  161. #endif
  162. /// Entry point of a thread.
  163. typedef void (*os_pthread) (void const *argument);
  164. /// Entry point of a timer call back function.
  165. typedef void (*os_ptimer) (void const *argument);
  166. /// Timer type.
  167. #if (osCMSIS < 0x20000U)
  168. typedef enum {
  169. osTimerOnce = 0, ///< One-shot timer.
  170. osTimerPeriodic = 1 ///< Repeating timer.
  171. } os_timer_type;
  172. #else
  173. #define os_timer_type osTimerType_t
  174. #endif
  175. /// Timeout value.
  176. #define osWaitForever 0xFFFFFFFFU ///< Wait forever timeout value.
  177. /// Status code values returned by CMSIS-RTOS functions.
  178. #if (osCMSIS < 0x20000U)
  179. typedef enum {
  180. osOK = 0, ///< Function completed; no error or event occurred.
  181. osEventSignal = 0x08, ///< Function completed; signal event occurred.
  182. osEventMessage = 0x10, ///< Function completed; message event occurred.
  183. osEventMail = 0x20, ///< Function completed; mail event occurred.
  184. osEventTimeout = 0x40, ///< Function completed; timeout occurred.
  185. osErrorParameter = 0x80, ///< Parameter error: a mandatory parameter was missing or specified an incorrect object.
  186. osErrorResource = 0x81, ///< Resource not available: a specified resource was not available.
  187. osErrorTimeoutResource = 0xC1, ///< Resource not available within given time: a specified resource was not available within the timeout period.
  188. osErrorISR = 0x82, ///< Not allowed in ISR context: the function cannot be called from interrupt service routines.
  189. osErrorISRRecursive = 0x83, ///< Function called multiple times from ISR with same object.
  190. osErrorPriority = 0x84, ///< System cannot determine priority or thread has illegal priority.
  191. osErrorNoMemory = 0x85, ///< System is out of memory: it was impossible to allocate or reserve memory for the operation.
  192. osErrorValue = 0x86, ///< Value of a parameter is out of range.
  193. osErrorOS = 0xFF, ///< Unspecified RTOS error: run-time error but no other error message fits.
  194. osStatusReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
  195. } osStatus;
  196. #else
  197. typedef int32_t osStatus;
  198. #define osEventSignal (0x08)
  199. #define osEventMessage (0x10)
  200. #define osEventMail (0x20)
  201. #define osEventTimeout (0x40)
  202. #define osErrorOS osError
  203. #define osErrorTimeoutResource osErrorTimeout
  204. #define osErrorISRRecursive (-126)
  205. #define osErrorValue (-127)
  206. #define osErrorPriority (-128)
  207. #endif
  208. // >>> the following data type definitions may be adapted towards a specific RTOS
  209. /// Thread ID identifies the thread.
  210. #if (osCMSIS < 0x20000U)
  211. typedef void *osThreadId;
  212. #else
  213. #define osThreadId osThreadId_t
  214. #endif
  215. /// Timer ID identifies the timer.
  216. #if (osCMSIS < 0x20000U)
  217. typedef void *osTimerId;
  218. #else
  219. #define osTimerId osTimerId_t
  220. #endif
  221. /// Mutex ID identifies the mutex.
  222. #if (osCMSIS < 0x20000U)
  223. typedef void *osMutexId;
  224. #else
  225. #define osMutexId osMutexId_t
  226. #endif
  227. /// Semaphore ID identifies the semaphore.
  228. #if (osCMSIS < 0x20000U)
  229. typedef void *osSemaphoreId;
  230. #else
  231. #define osSemaphoreId osSemaphoreId_t
  232. #endif
  233. /// Pool ID identifies the memory pool.
  234. typedef void *osPoolId;
  235. /// Message ID identifies the message queue.
  236. typedef void *osMessageQId;
  237. /// Mail ID identifies the mail queue.
  238. typedef void *osMailQId;
  239. /// Thread Definition structure contains startup information of a thread.
  240. #if (osCMSIS < 0x20000U)
  241. typedef struct os_thread_def {
  242. os_pthread pthread; ///< start address of thread function
  243. osPriority tpriority; ///< initial thread priority
  244. uint32_t instances; ///< maximum number of instances of that thread function
  245. uint32_t stacksize; ///< stack size requirements in bytes; 0 is default stack size
  246. } osThreadDef_t;
  247. #else
  248. typedef struct os_thread_def {
  249. os_pthread pthread; ///< start address of thread function
  250. osThreadAttr_t attr; ///< thread attributes
  251. } osThreadDef_t;
  252. #endif
  253. /// Timer Definition structure contains timer parameters.
  254. #if (osCMSIS < 0x20000U)
  255. typedef struct os_timer_def {
  256. os_ptimer ptimer; ///< start address of a timer function
  257. } osTimerDef_t;
  258. #else
  259. typedef struct os_timer_def {
  260. os_ptimer ptimer; ///< start address of a timer function
  261. osTimerAttr_t attr; ///< timer attributes
  262. } osTimerDef_t;
  263. #endif
  264. /// Mutex Definition structure contains setup information for a mutex.
  265. #if (osCMSIS < 0x20000U)
  266. typedef struct os_mutex_def {
  267. uint32_t dummy; ///< dummy value
  268. } osMutexDef_t;
  269. #else
  270. #define osMutexDef_t osMutexAttr_t
  271. #endif
  272. /// Semaphore Definition structure contains setup information for a semaphore.
  273. #if (osCMSIS < 0x20000U)
  274. typedef struct os_semaphore_def {
  275. uint32_t dummy; ///< dummy value
  276. } osSemaphoreDef_t;
  277. #else
  278. #define osSemaphoreDef_t osSemaphoreAttr_t
  279. #endif
  280. /// Definition structure for memory block allocation.
  281. #if (osCMSIS < 0x20000U)
  282. typedef struct os_pool_def {
  283. uint32_t pool_sz; ///< number of items (elements) in the pool
  284. uint32_t item_sz; ///< size of an item
  285. void *pool; ///< pointer to memory for pool
  286. } osPoolDef_t;
  287. #else
  288. typedef struct os_pool_def {
  289. uint32_t pool_sz; ///< number of items (elements) in the pool
  290. uint32_t item_sz; ///< size of an item
  291. osMemoryPoolAttr_t attr; ///< memory pool attributes
  292. } osPoolDef_t;
  293. #endif
  294. /// Definition structure for message queue.
  295. #if (osCMSIS < 0x20000U)
  296. typedef struct os_messageQ_def {
  297. uint32_t queue_sz; ///< number of elements in the queue
  298. void *pool; ///< memory array for messages
  299. } osMessageQDef_t;
  300. #else
  301. typedef struct os_messageQ_def {
  302. uint32_t queue_sz; ///< number of elements in the queue
  303. osMessageQueueAttr_t attr; ///< message queue attributes
  304. } osMessageQDef_t;
  305. #endif
  306. /// Definition structure for mail queue.
  307. #if (osCMSIS < 0x20000U)
  308. typedef struct os_mailQ_def {
  309. uint32_t queue_sz; ///< number of elements in the queue
  310. uint32_t item_sz; ///< size of an item
  311. void *pool; ///< memory array for mail
  312. } osMailQDef_t;
  313. #else
  314. typedef struct os_mailQ_def {
  315. uint32_t queue_sz; ///< number of elements in the queue
  316. uint32_t item_sz; ///< size of an item
  317. void *mail; ///< pointer to mail
  318. osMemoryPoolAttr_t mp_attr; ///< memory pool attributes
  319. osMessageQueueAttr_t mq_attr; ///< message queue attributes
  320. } osMailQDef_t;
  321. #endif
  322. /// Event structure contains detailed information about an event.
  323. typedef struct {
  324. osStatus status; ///< status code: event or error information
  325. union {
  326. uint32_t v; ///< message as 32-bit value
  327. void *p; ///< message or mail as void pointer
  328. int32_t signals; ///< signal flags
  329. } value; ///< event value
  330. union {
  331. osMailQId mail_id; ///< mail id obtained by \ref osMailCreate
  332. osMessageQId message_id; ///< message id obtained by \ref osMessageCreate
  333. } def; ///< event definition
  334. } osEvent;
  335. // ==== Kernel Management Functions ====
  336. /// Initialize the RTOS Kernel for creating objects.
  337. /// \return status code that indicates the execution status of the function.
  338. #if (osCMSIS < 0x20000U)
  339. osStatus osKernelInitialize (void);
  340. #endif
  341. /// Start the RTOS Kernel scheduler.
  342. /// \return status code that indicates the execution status of the function.
  343. #if (osCMSIS < 0x20000U)
  344. osStatus osKernelStart (void);
  345. #endif
  346. /// Check if the RTOS kernel is already started.
  347. /// \return 0 RTOS is not started, 1 RTOS is started.
  348. #if (osCMSIS < 0x20000U)
  349. int32_t osKernelRunning(void);
  350. #endif
  351. #if (defined(osFeature_SysTick) && (osFeature_SysTick != 0)) // System Timer available
  352. /// Get the RTOS kernel system timer counter.
  353. /// \return RTOS kernel system timer as 32-bit value
  354. #if (osCMSIS < 0x20000U)
  355. uint32_t osKernelSysTick (void);
  356. #else
  357. #define osKernelSysTick osKernelGetSysTimerCount
  358. #endif
  359. /// The RTOS kernel system timer frequency in Hz.
  360. /// \note Reflects the system timer setting and is typically defined in a configuration file.
  361. #if (osCMSIS < 0x20000U)
  362. #define osKernelSysTickFrequency 100000000
  363. #endif
  364. /// Convert a microseconds value to a RTOS kernel system timer value.
  365. /// \param microsec time value in microseconds.
  366. /// \return time value normalized to the \ref osKernelSysTickFrequency
  367. #if (osCMSIS < 0x20000U)
  368. #define osKernelSysTickMicroSec(microsec) (((uint64_t)microsec * (osKernelSysTickFrequency)) / 1000000)
  369. #else
  370. #define osKernelSysTickMicroSec(microsec) (((uint64_t)microsec * osKernelGetSysTimerFreq()) / 1000000)
  371. #endif
  372. #endif // System Timer available
  373. // ==== Thread Management Functions ====
  374. /// Create a Thread Definition with function, priority, and stack requirements.
  375. /// \param name name of the thread function.
  376. /// \param priority initial priority of the thread function.
  377. /// \param instances number of possible thread instances (used to statically allocate memory).
  378. /// \param stacksz stack size (in bytes) requirements for the thread function.
  379. #if defined (osObjectsExternal) // object is external
  380. #define osThreadDef(name, priority, instances, stacksz) \
  381. extern const osThreadDef_t os_thread_def_##name
  382. #else // define the object
  383. #define osThreadDef(name, priority, instances, stacksz) \
  384. static uint32_t os_thread_stack##name[(stacksz)?(((stacksz+3)/4)):1]; \
  385. static StaticTask_t os_thread_cb_##name; \
  386. const osThreadDef_t os_thread_def_##name = \
  387. { (name), \
  388. { NULL, osThreadDetached, \
  389. (instances == 1) ? (&os_thread_cb_##name) : NULL,\
  390. (instances == 1) ? sizeof(StaticTask_t) : 0U, \
  391. ((stacksz) && (instances == 1)) ? (&os_thread_stack##name) : NULL, \
  392. 4*((stacksz+3)/4), \
  393. (priority), 0U, 0U } }
  394. #endif
  395. /// Access a Thread definition.
  396. /// \param name name of the thread definition object.
  397. #define osThread(name) \
  398. &os_thread_def_##name
  399. /// Create a thread and add it to Active Threads and set it to state READY.
  400. /// \param[in] thread_def thread definition referenced with \ref osThread.
  401. /// \param[in] argument pointer that is passed to the thread function as start argument.
  402. /// \return thread ID for reference by other functions or NULL in case of error.
  403. osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument);
  404. /// Return the thread ID of the current running thread.
  405. /// \return thread ID for reference by other functions or NULL in case of error.
  406. #if (osCMSIS < 0x20000U)
  407. osThreadId osThreadGetId (void);
  408. #endif
  409. /// Change priority of a thread.
  410. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  411. /// \param[in] priority new priority value for the thread function.
  412. /// \return status code that indicates the execution status of the function.
  413. #if (osCMSIS < 0x20000U)
  414. osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority);
  415. #endif
  416. /// Get current priority of a thread.
  417. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  418. /// \return current priority value of the specified thread.
  419. #if (osCMSIS < 0x20000U)
  420. osPriority osThreadGetPriority (osThreadId thread_id);
  421. #endif
  422. /// Pass control to next thread that is in state \b READY.
  423. /// \return status code that indicates the execution status of the function.
  424. #if (osCMSIS < 0x20000U)
  425. osStatus osThreadYield (void);
  426. #endif
  427. /// Terminate execution of a thread.
  428. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  429. /// \return status code that indicates the execution status of the function.
  430. #if (osCMSIS < 0x20000U)
  431. osStatus osThreadTerminate (osThreadId thread_id);
  432. #endif
  433. // ==== Signal Management ====
  434. /// Set the specified Signal Flags of an active thread.
  435. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  436. /// \param[in] signals specifies the signal flags of the thread that should be set.
  437. /// \return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
  438. int32_t osSignalSet (osThreadId thread_id, int32_t signals);
  439. /// Clear the specified Signal Flags of an active thread.
  440. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  441. /// \param[in] signals specifies the signal flags of the thread that shall be cleared.
  442. /// \return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters or call from ISR.
  443. int32_t osSignalClear (osThreadId thread_id, int32_t signals);
  444. /// Wait for one or more Signal Flags to become signaled for the current \b RUNNING thread.
  445. /// \param[in] signals wait until all specified signal flags set or 0 for any single signal flag.
  446. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  447. /// \return event flag information or error code.
  448. os_InRegs osEvent osSignalWait (int32_t signals, uint32_t millisec);
  449. // ==== Generic Wait Functions ====
  450. /// Wait for Timeout (Time Delay).
  451. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue "time delay" value
  452. /// \return status code that indicates the execution status of the function.
  453. #if (osCMSIS < 0x20000U)
  454. osStatus osDelay (uint32_t millisec);
  455. #endif
  456. #if (defined (osFeature_Wait) && (osFeature_Wait != 0)) // Generic Wait available
  457. /// Wait for Signal, Message, Mail, or Timeout.
  458. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  459. /// \return event that contains signal, message, or mail information or error code.
  460. os_InRegs osEvent osWait (uint32_t millisec);
  461. #endif // Generic Wait available
  462. // ==== Timer Management Functions ====
  463. /// Define a Timer object.
  464. /// \param name name of the timer object.
  465. /// \param function name of the timer call back function.
  466. #if defined (osObjectsExternal) // object is external
  467. #define osTimerDef(name, function) \
  468. extern const osTimerDef_t os_timer_def_##name
  469. #else // define the object
  470. #define osTimerDef(name, function) \
  471. static StaticTimer_t os_timer_cb_##name; \
  472. const osTimerDef_t os_timer_def_##name = \
  473. { (function), { NULL, 0U, (&os_timer_cb_##name), sizeof(StaticTimer_t) } }
  474. #endif
  475. /// Access a Timer definition.
  476. /// \param name name of the timer object.
  477. #define osTimer(name) \
  478. &os_timer_def_##name
  479. /// Create and Initialize a timer.
  480. /// \param[in] timer_def timer object referenced with \ref osTimer.
  481. /// \param[in] type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior.
  482. /// \param[in] argument argument to the timer call back function.
  483. /// \return timer ID for reference by other functions or NULL in case of error.
  484. osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument);
  485. /// Start or restart a timer.
  486. /// \param[in] timer_id timer ID obtained by \ref osTimerCreate.
  487. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue "time delay" value of the timer.
  488. /// \return status code that indicates the execution status of the function.
  489. #if (osCMSIS < 0x20000U)
  490. osStatus osTimerStart (osTimerId timer_id, uint32_t millisec);
  491. #endif
  492. /// Stop a timer.
  493. /// \param[in] timer_id timer ID obtained by \ref osTimerCreate.
  494. /// \return status code that indicates the execution status of the function.
  495. #if (osCMSIS < 0x20000U)
  496. osStatus osTimerStop (osTimerId timer_id);
  497. #endif
  498. /// Delete a timer.
  499. /// \param[in] timer_id timer ID obtained by \ref osTimerCreate.
  500. /// \return status code that indicates the execution status of the function.
  501. #if (osCMSIS < 0x20000U)
  502. osStatus osTimerDelete (osTimerId timer_id);
  503. #endif
  504. // ==== Mutex Management Functions ====
  505. /// Define a Mutex.
  506. /// \param name name of the mutex object.
  507. #if defined (osObjectsExternal) // object is external
  508. #define osMutexDef(name) \
  509. extern const osMutexDef_t os_mutex_def_##name
  510. #else // define the object
  511. #define osMutexDef(name) \
  512. static StaticSemaphore_t os_mutex_cb_##name; \
  513. const osMutexDef_t os_mutex_def_##name = \
  514. { NULL, osMutexRecursive | osMutexPrioInherit, (&os_mutex_cb_##name), sizeof(StaticSemaphore_t) }
  515. #endif
  516. /// Access a Mutex definition.
  517. /// \param name name of the mutex object.
  518. #define osMutex(name) \
  519. &os_mutex_def_##name
  520. /// Create and Initialize a Mutex object.
  521. /// \param[in] mutex_def mutex definition referenced with \ref osMutex.
  522. /// \return mutex ID for reference by other functions or NULL in case of error.
  523. osMutexId osMutexCreate (const osMutexDef_t *mutex_def);
  524. /// Wait until a Mutex becomes available.
  525. /// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
  526. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  527. /// \return status code that indicates the execution status of the function.
  528. #if (osCMSIS < 0x20000U)
  529. osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec);
  530. #else
  531. #define osMutexWait osMutexAcquire
  532. #endif
  533. /// Release a Mutex that was obtained by \ref osMutexWait.
  534. /// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
  535. /// \return status code that indicates the execution status of the function.
  536. #if (osCMSIS < 0x20000U)
  537. osStatus osMutexRelease (osMutexId mutex_id);
  538. #endif
  539. /// Delete a Mutex object.
  540. /// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
  541. /// \return status code that indicates the execution status of the function.
  542. #if (osCMSIS < 0x20000U)
  543. osStatus osMutexDelete (osMutexId mutex_id);
  544. #endif
  545. // ==== Semaphore Management Functions ====
  546. #if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0U)) // Semaphore available
  547. /// Define a Semaphore object.
  548. /// \param name name of the semaphore object.
  549. #if defined (osObjectsExternal) // object is external
  550. #define osSemaphoreDef(name) \
  551. extern const osSemaphoreDef_t os_semaphore_def_##name
  552. #else // define the object
  553. #define osSemaphoreDef(name) \
  554. static StaticSemaphore_t os_semaphore_cb_##name; \
  555. const osSemaphoreDef_t os_semaphore_def_##name = \
  556. { NULL, 0U, (&os_semaphore_cb_##name), sizeof(StaticSemaphore_t) }
  557. #endif
  558. /// Access a Semaphore definition.
  559. /// \param name name of the semaphore object.
  560. #define osSemaphore(name) \
  561. &os_semaphore_def_##name
  562. /// Create and Initialize a Semaphore object.
  563. /// \param[in] semaphore_def semaphore definition referenced with \ref osSemaphore.
  564. /// \param[in] count maximum and initial number of available tokens.
  565. /// \return semaphore ID for reference by other functions or NULL in case of error.
  566. osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count);
  567. /// Wait until a Semaphore token becomes available.
  568. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate.
  569. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  570. /// \return number of available tokens, or -1 in case of incorrect parameters.
  571. int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec);
  572. /// Release a Semaphore token.
  573. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate.
  574. /// \return status code that indicates the execution status of the function.
  575. #if (osCMSIS < 0x20000U)
  576. osStatus osSemaphoreRelease (osSemaphoreId semaphore_id);
  577. #endif
  578. /// Delete a Semaphore object.
  579. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate.
  580. /// \return status code that indicates the execution status of the function.
  581. #if (osCMSIS < 0x20000U)
  582. osStatus osSemaphoreDelete (osSemaphoreId semaphore_id);
  583. #endif
  584. #endif // Semaphore available
  585. // ==== Memory Pool Management Functions ====
  586. #if (defined(osFeature_Pool) && (osFeature_Pool != 0)) // Memory Pool available
  587. /// \brief Define a Memory Pool.
  588. /// \param name name of the memory pool.
  589. /// \param no maximum number of blocks (objects) in the memory pool.
  590. /// \param type data type of a single block (object).
  591. #if defined (osObjectsExternal) // object is external
  592. #define osPoolDef(name, no, type) \
  593. extern const osPoolDef_t os_pool_def_##name
  594. #else // define the object
  595. #define osPoolDef(name, no, type) \
  596. const osPoolDef_t os_pool_def_##name = \
  597. { (no), sizeof(type), NULL }
  598. #endif
  599. /// \brief Access a Memory Pool definition.
  600. /// \param name name of the memory pool
  601. #define osPool(name) \
  602. &os_pool_def_##name
  603. /// Create and Initialize a Memory Pool object.
  604. /// \param[in] pool_def memory pool definition referenced with \ref osPool.
  605. /// \return memory pool ID for reference by other functions or NULL in case of error.
  606. osPoolId osPoolCreate (const osPoolDef_t *pool_def);
  607. /// Allocate a memory block from a Memory Pool.
  608. /// \param[in] pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  609. /// \return address of the allocated memory block or NULL in case of no memory available.
  610. void *osPoolAlloc (osPoolId pool_id);
  611. /// Allocate a memory block from a Memory Pool and set memory block to zero.
  612. /// \param[in] pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  613. /// \return address of the allocated memory block or NULL in case of no memory available.
  614. void *osPoolCAlloc (osPoolId pool_id);
  615. /// Return an allocated memory block back to a Memory Pool.
  616. /// \param[in] pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  617. /// \param[in] block address of the allocated memory block to be returned to the memory pool.
  618. /// \return status code that indicates the execution status of the function.
  619. osStatus osPoolFree (osPoolId pool_id, void *block);
  620. #endif // Memory Pool available
  621. // ==== Message Queue Management Functions ====
  622. #if (defined(osFeature_MessageQ) && (osFeature_MessageQ != 0)) // Message Queue available
  623. /// \brief Create a Message Queue Definition.
  624. /// \param name name of the queue.
  625. /// \param queue_sz maximum number of messages in the queue.
  626. /// \param type data type of a single message element (for debugger).
  627. #if defined (osObjectsExternal) // object is external
  628. #define osMessageQDef(name, queue_sz, type) \
  629. extern const osMessageQDef_t os_messageQ_def_##name
  630. #else // define the object
  631. #define osMessageQDef(name, queue_sz, type) \
  632. static StaticQueue_t os_mq_cb_##name; \
  633. static uint32_t os_mq_data_##name[(queue_sz) * sizeof(type)]; \
  634. const osMessageQDef_t os_messageQ_def_##name = \
  635. { (queue_sz), \
  636. { NULL, 0U, (&os_mq_cb_##name), sizeof(StaticQueue_t), \
  637. (&os_mq_data_##name), sizeof(os_mq_data_##name) } }
  638. #endif
  639. /// \brief Access a Message Queue Definition.
  640. /// \param name name of the queue
  641. #define osMessageQ(name) \
  642. &os_messageQ_def_##name
  643. /// Create and Initialize a Message Queue object.
  644. /// \param[in] queue_def message queue definition referenced with \ref osMessageQ.
  645. /// \param[in] thread_id thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
  646. /// \return message queue ID for reference by other functions or NULL in case of error.
  647. osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id);
  648. /// Put a Message to a Queue.
  649. /// \param[in] queue_id message queue ID obtained with \ref osMessageCreate.
  650. /// \param[in] info message information.
  651. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  652. /// \return status code that indicates the execution status of the function.
  653. osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec);
  654. /// Get a Message from a Queue or timeout if Queue is empty.
  655. /// \param[in] queue_id message queue ID obtained with \ref osMessageCreate.
  656. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  657. /// \return event information that includes status code.
  658. os_InRegs osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec);
  659. #endif // Message Queue available
  660. // ==== Mail Queue Management Functions ====
  661. #if (defined(osFeature_MailQ) && (osFeature_MailQ != 0)) // Mail Queue available
  662. /// \brief Create a Mail Queue Definition.
  663. /// \param name name of the queue.
  664. /// \param queue_sz maximum number of mails in the queue.
  665. /// \param type data type of a single mail element.
  666. #if defined (osObjectsExternal) // object is external
  667. #define osMailQDef(name, queue_sz, type) \
  668. extern const osMailQDef_t os_mailQ_def_##name
  669. #else // define the object
  670. #define osMailQDef(name, queue_sz, type) \
  671. const osMailQDef_t os_mailQ_def_##name = \
  672. { (queue_sz), sizeof(type), NULL }
  673. #endif
  674. /// \brief Access a Mail Queue Definition.
  675. /// \param name name of the queue
  676. #define osMailQ(name) \
  677. &os_mailQ_def_##name
  678. /// Create and Initialize a Mail Queue object.
  679. /// \param[in] queue_def mail queue definition referenced with \ref osMailQ.
  680. /// \param[in] thread_id thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
  681. /// \return mail queue ID for reference by other functions or NULL in case of error.
  682. osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id);
  683. /// Allocate a memory block for mail from a mail memory pool.
  684. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  685. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  686. /// \return pointer to memory block that can be filled with mail or NULL in case of error.
  687. void *osMailAlloc (osMailQId queue_id, uint32_t millisec);
  688. /// Allocate a memory block for mail from a mail memory pool and set memory block to zero.
  689. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  690. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  691. /// \return pointer to memory block that can be filled with mail or NULL in case of error.
  692. void *osMailCAlloc (osMailQId queue_id, uint32_t millisec);
  693. /// Put a Mail into a Queue.
  694. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  695. /// \param[in] mail pointer to memory with mail to put into a queue.
  696. /// \return status code that indicates the execution status of the function.
  697. osStatus osMailPut (osMailQId queue_id, const void *mail);
  698. /// Get a Mail from a Queue or timeout if Queue is empty.
  699. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  700. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  701. /// \return event information that includes status code.
  702. os_InRegs osEvent osMailGet (osMailQId queue_id, uint32_t millisec);
  703. /// Free a memory block by returning it to a mail memory pool.
  704. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  705. /// \param[in] mail pointer to memory block that was obtained with \ref osMailGet.
  706. /// \return status code that indicates the execution status of the function.
  707. osStatus osMailFree (osMailQId queue_id, void *mail);
  708. #endif // Mail Queue available
  709. #ifdef __cplusplus
  710. }
  711. #endif
  712. #endif // CMSIS_OS_H_