cmsis_os.h 36 KB

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