cmsis_os.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Copyright (c) 2013-2017 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 template 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. /// \b osCMSIS identifies the CMSIS-RTOS API version.
  125. #define osCMSIS 0x20001U ///< API version (main[31:16].sub[15:0])
  126. /// \note CAN BE CHANGED: \b osCMSIS_KERNEL identifies the underlying RTOS kernel and version number.
  127. #define osCMSIS_KERNEL 0x10000U ///< RTOS identification and version (main[31:16].sub[15:0])
  128. /// \note CAN BE CHANGED: \b osKernelSystemId identifies the underlying RTOS kernel.
  129. #define osKernelSystemId "KERNEL V1.0" ///< RTOS identification string
  130. /// \note CAN BE CHANGED: \b osFeature_xxx identifies RTOS features.
  131. #define osFeature_MainThread 0 ///< main thread 1=main can be thread, 0=not available
  132. #define osFeature_Signals 16U ///< 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 1 ///< Memory Pools: 1=available, 0=not available
  137. #define osFeature_MessageQ 1 ///< Message Queues: 1=available, 0=not available
  138. #define osFeature_MailQ 1 ///< Mail Queues: 1=available, 0=not available
  139. #if (osCMSIS >= 0x20000U)
  140. #include "cmsis_os2.h"
  141. #else
  142. #include <stdint.h>
  143. #include <stddef.h>
  144. #endif
  145. #ifdef __cplusplus
  146. extern "C"
  147. {
  148. #endif
  149. // ==== Enumerations, structures, defines ====
  150. /// Priority values.
  151. #if (osCMSIS < 0x20000U)
  152. typedef enum {
  153. osPriorityIdle = -3, ///< Priority: idle (lowest)
  154. osPriorityLow = -2, ///< Priority: low
  155. osPriorityBelowNormal = -1, ///< Priority: below normal
  156. osPriorityNormal = 0, ///< Priority: normal (default)
  157. osPriorityAboveNormal = +1, ///< Priority: above normal
  158. osPriorityHigh = +2, ///< Priority: high
  159. osPriorityRealtime = +3, ///< Priority: realtime (highest)
  160. osPriorityError = 0x84, ///< System cannot determine priority or illegal priority.
  161. osPriorityReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
  162. } osPriority;
  163. #else
  164. #define osPriority osPriority_t
  165. #endif
  166. /// Entry point of a thread.
  167. typedef void (*os_pthread) (void const *argument);
  168. /// Entry point of a timer call back function.
  169. typedef void (*os_ptimer) (void const *argument);
  170. /// Timer type.
  171. #if (osCMSIS < 0x20000U)
  172. typedef enum {
  173. osTimerOnce = 0, ///< One-shot timer.
  174. osTimerPeriodic = 1 ///< Repeating timer.
  175. } os_timer_type;
  176. #else
  177. #define os_timer_type osTimerType_t
  178. #endif
  179. /// Timeout value.
  180. #define osWaitForever 0xFFFFFFFFU ///< Wait forever timeout value.
  181. /// Status code values returned by CMSIS-RTOS functions.
  182. #if (osCMSIS < 0x20000U)
  183. typedef enum {
  184. osOK = 0, ///< Function completed; no error or event occurred.
  185. osEventSignal = 0x08, ///< Function completed; signal event occurred.
  186. osEventMessage = 0x10, ///< Function completed; message event occurred.
  187. osEventMail = 0x20, ///< Function completed; mail event occurred.
  188. osEventTimeout = 0x40, ///< Function completed; timeout occurred.
  189. osErrorParameter = 0x80, ///< Parameter error: a mandatory parameter was missing or specified an incorrect object.
  190. osErrorResource = 0x81, ///< Resource not available: a specified resource was not available.
  191. osErrorTimeoutResource = 0xC1, ///< Resource not available within given time: a specified resource was not available within the timeout period.
  192. osErrorISR = 0x82, ///< Not allowed in ISR context: the function cannot be called from interrupt service routines.
  193. osErrorISRRecursive = 0x83, ///< Function called multiple times from ISR with same object.
  194. osErrorPriority = 0x84, ///< System cannot determine priority or thread has illegal priority.
  195. osErrorNoMemory = 0x85, ///< System is out of memory: it was impossible to allocate or reserve memory for the operation.
  196. osErrorValue = 0x86, ///< Value of a parameter is out of range.
  197. osErrorOS = 0xFF, ///< Unspecified RTOS error: run-time error but no other error message fits.
  198. osStatusReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
  199. } osStatus;
  200. #else
  201. typedef int32_t osStatus;
  202. #define osEventSignal (0x08)
  203. #define osEventMessage (0x10)
  204. #define osEventMail (0x20)
  205. #define osEventTimeout (0x40)
  206. #define osErrorOS osError
  207. #define osErrorTimeoutResource osErrorTimeout
  208. #define osErrorISRRecursive (-126)
  209. #define osErrorValue (-127)
  210. #define osErrorPriority (-128)
  211. #endif
  212. // >>> the following data type definitions may be adapted towards a specific RTOS
  213. /// Thread ID identifies the thread.
  214. /// \note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
  215. #if (osCMSIS < 0x20000U)
  216. typedef void *osThreadId;
  217. #else
  218. #define osThreadId osThreadId_t
  219. #endif
  220. /// Timer ID identifies the timer.
  221. /// \note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
  222. #if (osCMSIS < 0x20000U)
  223. typedef void *osTimerId;
  224. #else
  225. #define osTimerId osTimerId_t
  226. #endif
  227. /// Mutex ID identifies the mutex.
  228. /// \note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
  229. #if (osCMSIS < 0x20000U)
  230. typedef void *osMutexId;
  231. #else
  232. #define osMutexId osMutexId_t
  233. #endif
  234. /// Semaphore ID identifies the semaphore.
  235. /// \note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
  236. #if (osCMSIS < 0x20000U)
  237. typedef void *osSemaphoreId;
  238. #else
  239. #define osSemaphoreId osSemaphoreId_t
  240. #endif
  241. /// Pool ID identifies the memory pool.
  242. /// \note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
  243. typedef void *osPoolId;
  244. /// Message ID identifies the message queue.
  245. /// \note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
  246. typedef void *osMessageQId;
  247. /// Mail ID identifies the mail queue.
  248. /// \note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
  249. typedef void *osMailQId;
  250. /// Thread Definition structure contains startup information of a thread.
  251. /// \note CAN BE CHANGED: \b os_thread_def is implementation specific in every CMSIS-RTOS.
  252. #if (osCMSIS < 0x20000U)
  253. typedef struct os_thread_def {
  254. os_pthread pthread; ///< start address of thread function
  255. osPriority tpriority; ///< initial thread priority
  256. uint32_t instances; ///< maximum number of instances of that thread function
  257. uint32_t stacksize; ///< stack size requirements in bytes; 0 is default stack size
  258. } osThreadDef_t;
  259. #else
  260. typedef struct os_thread_def {
  261. os_pthread pthread; ///< start address of thread function
  262. osThreadAttr_t attr; ///< thread attributes
  263. } osThreadDef_t;
  264. #endif
  265. /// Timer Definition structure contains timer parameters.
  266. /// \note CAN BE CHANGED: \b os_timer_def is implementation specific in every CMSIS-RTOS.
  267. #if (osCMSIS < 0x20000U)
  268. typedef struct os_timer_def {
  269. os_ptimer ptimer; ///< start address of a timer function
  270. } osTimerDef_t;
  271. #else
  272. typedef struct os_timer_def {
  273. os_ptimer ptimer; ///< start address of a timer function
  274. osTimerAttr_t attr; ///< timer attributes
  275. } osTimerDef_t;
  276. #endif
  277. /// Mutex Definition structure contains setup information for a mutex.
  278. /// \note CAN BE CHANGED: \b os_mutex_def is implementation specific in every CMSIS-RTOS.
  279. #if (osCMSIS < 0x20000U)
  280. typedef struct os_mutex_def {
  281. uint32_t dummy; ///< dummy value
  282. } osMutexDef_t;
  283. #else
  284. #define osMutexDef_t osMutexAttr_t
  285. #endif
  286. /// Semaphore Definition structure contains setup information for a semaphore.
  287. /// \note CAN BE CHANGED: \b os_semaphore_def is implementation specific in every CMSIS-RTOS.
  288. #if (osCMSIS < 0x20000U)
  289. typedef struct os_semaphore_def {
  290. uint32_t dummy; ///< dummy value
  291. } osSemaphoreDef_t;
  292. #else
  293. #define osSemaphoreDef_t osSemaphoreAttr_t
  294. #endif
  295. /// Definition structure for memory block allocation.
  296. /// \note CAN BE CHANGED: \b os_pool_def is implementation specific in every CMSIS-RTOS.
  297. #if (osCMSIS < 0x20000U)
  298. typedef struct os_pool_def {
  299. uint32_t pool_sz; ///< number of items (elements) in the pool
  300. uint32_t item_sz; ///< size of an item
  301. void *pool; ///< pointer to memory for pool
  302. } osPoolDef_t;
  303. #else
  304. typedef struct os_pool_def {
  305. uint32_t pool_sz; ///< number of items (elements) in the pool
  306. uint32_t item_sz; ///< size of an item
  307. osMemoryPoolAttr_t attr; ///< memory pool attributes
  308. } osPoolDef_t;
  309. #endif
  310. /// Definition structure for message queue.
  311. /// \note CAN BE CHANGED: \b os_messageQ_def is implementation specific in every CMSIS-RTOS.
  312. #if (osCMSIS < 0x20000U)
  313. typedef struct os_messageQ_def {
  314. uint32_t queue_sz; ///< number of elements in the queue
  315. void *pool; ///< memory array for messages
  316. } osMessageQDef_t;
  317. #else
  318. typedef struct os_messageQ_def {
  319. uint32_t queue_sz; ///< number of elements in the queue
  320. osMessageQueueAttr_t attr; ///< message queue attributes
  321. } osMessageQDef_t;
  322. #endif
  323. /// Definition structure for mail queue.
  324. /// \note CAN BE CHANGED: \b os_mailQ_def is implementation specific in every CMSIS-RTOS.
  325. #if (osCMSIS < 0x20000U)
  326. typedef struct os_mailQ_def {
  327. uint32_t queue_sz; ///< number of elements in the queue
  328. uint32_t item_sz; ///< size of an item
  329. void *pool; ///< memory array for mail
  330. } osMailQDef_t;
  331. #else
  332. typedef struct os_mailQ_def {
  333. uint32_t queue_sz; ///< number of elements in the queue
  334. uint32_t item_sz; ///< size of an item
  335. void *mail; ///< pointer to mail
  336. osMemoryPoolAttr_t mp_attr; ///< memory pool attributes
  337. osMessageQueueAttr_t mq_attr; ///< message queue attributes
  338. } osMailQDef_t;
  339. #endif
  340. /// Event structure contains detailed information about an event.
  341. typedef struct {
  342. osStatus status; ///< status code: event or error information
  343. union {
  344. uint32_t v; ///< message as 32-bit value
  345. void *p; ///< message or mail as void pointer
  346. int32_t signals; ///< signal flags
  347. } value; ///< event value
  348. union {
  349. osMailQId mail_id; ///< mail id obtained by \ref osMailCreate
  350. osMessageQId message_id; ///< message id obtained by \ref osMessageCreate
  351. } def; ///< event definition
  352. } osEvent;
  353. // ==== Kernel Management Functions ====
  354. /// Initialize the RTOS Kernel for creating objects.
  355. /// \return status code that indicates the execution status of the function.
  356. #if (osCMSIS < 0x20000U)
  357. osStatus osKernelInitialize (void);
  358. #endif
  359. /// Start the RTOS Kernel scheduler.
  360. /// \return status code that indicates the execution status of the function.
  361. #if (osCMSIS < 0x20000U)
  362. osStatus osKernelStart (void);
  363. #endif
  364. /// Check if the RTOS kernel is already started.
  365. /// \return 0 RTOS is not started, 1 RTOS is started.
  366. #if (osCMSIS < 0x20000U)
  367. int32_t osKernelRunning(void);
  368. #endif
  369. #if (defined(osFeature_SysTick) && (osFeature_SysTick != 0)) // System Timer available
  370. /// Get the RTOS kernel system timer counter.
  371. /// \return RTOS kernel system timer as 32-bit value
  372. #if (osCMSIS < 0x20000U)
  373. uint32_t osKernelSysTick (void);
  374. #else
  375. #define osKernelSysTick osKernelGetSysTimerCount
  376. #endif
  377. /// The RTOS kernel system timer frequency in Hz.
  378. /// \note Reflects the system timer setting and is typically defined in a configuration file.
  379. #if (osCMSIS < 0x20000U)
  380. #define osKernelSysTickFrequency 100000000
  381. #endif
  382. /// Convert a microseconds value to a RTOS kernel system timer value.
  383. /// \param microsec time value in microseconds.
  384. /// \return time value normalized to the \ref osKernelSysTickFrequency
  385. #if (osCMSIS < 0x20000U)
  386. #define osKernelSysTickMicroSec(microsec) (((uint64_t)microsec * (osKernelSysTickFrequency)) / 1000000)
  387. #else
  388. #define osKernelSysTickMicroSec(microsec) (((uint64_t)microsec * osKernelGetSysTimerFreq()) / 1000000)
  389. #endif
  390. #endif // System Timer available
  391. // ==== Thread Management Functions ====
  392. /// Create a Thread Definition with function, priority, and stack requirements.
  393. /// \param name name of the thread function.
  394. /// \param priority initial priority of the thread function.
  395. /// \param instances number of possible thread instances.
  396. /// \param stacksz stack size (in bytes) requirements for the thread function.
  397. /// \note CAN BE CHANGED: The parameters to \b osThreadDef shall be consistent but the
  398. /// macro body is implementation specific in every CMSIS-RTOS.
  399. #if defined (osObjectsExternal) // object is external
  400. #define osThreadDef(name, priority, instances, stacksz) \
  401. extern const osThreadDef_t os_thread_def_##name
  402. #else // define the object
  403. #if (osCMSIS < 0x20000U)
  404. #define osThreadDef(name, priority, instances, stacksz) \
  405. const osThreadDef_t os_thread_def_##name = \
  406. { (name), (priority), (instances), (stacksz) }
  407. #else
  408. #define osThreadDef(name, priority, instances, stacksz) \
  409. const osThreadDef_t os_thread_def_##name = \
  410. { (name), \
  411. { NULL, osThreadDetached, NULL, 0U, NULL, 8*((stacksz+7)/8), (priority), 0U, 0U } }
  412. #endif
  413. #endif
  414. /// Access a Thread definition.
  415. /// \param name name of the thread definition object.
  416. /// \note CAN BE CHANGED: The parameter to \b osThread shall be consistent but the
  417. /// macro body is implementation specific in every CMSIS-RTOS.
  418. #define osThread(name) \
  419. &os_thread_def_##name
  420. /// Create a thread and add it to Active Threads and set it to state READY.
  421. /// \param[in] thread_def thread definition referenced with \ref osThread.
  422. /// \param[in] argument pointer that is passed to the thread function as start argument.
  423. /// \return thread ID for reference by other functions or NULL in case of error.
  424. osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument);
  425. /// Return the thread ID of the current running thread.
  426. /// \return thread ID for reference by other functions or NULL in case of error.
  427. #if (osCMSIS < 0x20000U)
  428. osThreadId osThreadGetId (void);
  429. #endif
  430. /// Change priority of a thread.
  431. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  432. /// \param[in] priority new priority value for the thread function.
  433. /// \return status code that indicates the execution status of the function.
  434. #if (osCMSIS < 0x20000U)
  435. osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority);
  436. #endif
  437. /// Get current priority of a thread.
  438. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  439. /// \return current priority value of the specified thread.
  440. #if (osCMSIS < 0x20000U)
  441. osPriority osThreadGetPriority (osThreadId thread_id);
  442. #endif
  443. /// Pass control to next thread that is in state \b READY.
  444. /// \return status code that indicates the execution status of the function.
  445. #if (osCMSIS < 0x20000U)
  446. osStatus osThreadYield (void);
  447. #endif
  448. /// Terminate execution of a thread.
  449. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  450. /// \return status code that indicates the execution status of the function.
  451. #if (osCMSIS < 0x20000U)
  452. osStatus osThreadTerminate (osThreadId thread_id);
  453. #endif
  454. // ==== Signal Management ====
  455. /// Set the specified Signal Flags of an active thread.
  456. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  457. /// \param[in] signals specifies the signal flags of the thread that should be set.
  458. /// \return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
  459. int32_t osSignalSet (osThreadId thread_id, int32_t signals);
  460. /// Clear the specified Signal Flags of an active thread.
  461. /// \param[in] thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
  462. /// \param[in] signals specifies the signal flags of the thread that shall be cleared.
  463. /// \return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters or call from ISR.
  464. int32_t osSignalClear (osThreadId thread_id, int32_t signals);
  465. /// Wait for one or more Signal Flags to become signaled for the current \b RUNNING thread.
  466. /// \param[in] signals wait until all specified signal flags set or 0 for any single signal flag.
  467. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  468. /// \return event flag information or error code.
  469. osEvent osSignalWait (int32_t signals, uint32_t millisec);
  470. // ==== Generic Wait Functions ====
  471. /// Wait for Timeout (Time Delay).
  472. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue "time delay" value
  473. /// \return status code that indicates the execution status of the function.
  474. #if (osCMSIS < 0x20000U)
  475. osStatus osDelay (uint32_t millisec);
  476. #endif
  477. #if (defined (osFeature_Wait) && (osFeature_Wait != 0)) // Generic Wait available
  478. /// Wait for Signal, Message, Mail, or Timeout.
  479. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  480. /// \return event that contains signal, message, or mail information or error code.
  481. osEvent osWait (uint32_t millisec);
  482. #endif // Generic Wait available
  483. // ==== Timer Management Functions ====
  484. /// Define a Timer object.
  485. /// \param name name of the timer object.
  486. /// \param function name of the timer call back function.
  487. /// \note CAN BE CHANGED: The parameter to \b osTimerDef shall be consistent but the
  488. /// macro body is implementation specific in every CMSIS-RTOS.
  489. #if defined (osObjectsExternal) // object is external
  490. #define osTimerDef(name, function) \
  491. extern const osTimerDef_t os_timer_def_##name
  492. #else // define the object
  493. #if (osCMSIS < 0x20000U)
  494. #define osTimerDef(name, function) \
  495. const osTimerDef_t os_timer_def_##name = { (function) }
  496. #else
  497. #define osTimerDef(name, function) \
  498. const osTimerDef_t os_timer_def_##name = \
  499. { (function), { NULL, 0U, NULL, 0U } }
  500. #endif
  501. #endif
  502. /// Access a Timer definition.
  503. /// \param name name of the timer object.
  504. /// \note CAN BE CHANGED: The parameter to \b osTimer shall be consistent but the
  505. /// macro body is implementation specific in every CMSIS-RTOS.
  506. #define osTimer(name) \
  507. &os_timer_def_##name
  508. /// Create and Initialize a timer.
  509. /// \param[in] timer_def timer object referenced with \ref osTimer.
  510. /// \param[in] type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior.
  511. /// \param[in] argument argument to the timer call back function.
  512. /// \return timer ID for reference by other functions or NULL in case of error.
  513. osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument);
  514. /// Start or restart a timer.
  515. /// \param[in] timer_id timer ID obtained by \ref osTimerCreate.
  516. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue "time delay" value of the timer.
  517. /// \return status code that indicates the execution status of the function.
  518. #if (osCMSIS < 0x20000U)
  519. osStatus osTimerStart (osTimerId timer_id, uint32_t millisec);
  520. #endif
  521. /// Stop a timer.
  522. /// \param[in] timer_id timer ID obtained by \ref osTimerCreate.
  523. /// \return status code that indicates the execution status of the function.
  524. #if (osCMSIS < 0x20000U)
  525. osStatus osTimerStop (osTimerId timer_id);
  526. #endif
  527. /// Delete a timer.
  528. /// \param[in] timer_id timer ID obtained by \ref osTimerCreate.
  529. /// \return status code that indicates the execution status of the function.
  530. #if (osCMSIS < 0x20000U)
  531. osStatus osTimerDelete (osTimerId timer_id);
  532. #endif
  533. // ==== Mutex Management Functions ====
  534. /// Define a Mutex.
  535. /// \param name name of the mutex object.
  536. /// \note CAN BE CHANGED: The parameter to \b osMutexDef shall be consistent but the
  537. /// macro body is implementation specific in every CMSIS-RTOS.
  538. #if defined (osObjectsExternal) // object is external
  539. #define osMutexDef(name) \
  540. extern const osMutexDef_t os_mutex_def_##name
  541. #else // define the object
  542. #if (osCMSIS < 0x20000U)
  543. #define osMutexDef(name) \
  544. const osMutexDef_t os_mutex_def_##name = { 0 }
  545. #else
  546. #define osMutexDef(name) \
  547. const osMutexDef_t os_mutex_def_##name = \
  548. { NULL, osMutexRecursive | osMutexPrioInherit | osMutexRobust, NULL, 0U }
  549. #endif
  550. #endif
  551. /// Access a Mutex definition.
  552. /// \param name name of the mutex object.
  553. /// \note CAN BE CHANGED: The parameter to \b osMutex shall be consistent but the
  554. /// macro body is implementation specific in every CMSIS-RTOS.
  555. #define osMutex(name) \
  556. &os_mutex_def_##name
  557. /// Create and Initialize a Mutex object.
  558. /// \param[in] mutex_def mutex definition referenced with \ref osMutex.
  559. /// \return mutex ID for reference by other functions or NULL in case of error.
  560. osMutexId osMutexCreate (const osMutexDef_t *mutex_def);
  561. /// Wait until a Mutex becomes available.
  562. /// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
  563. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  564. /// \return status code that indicates the execution status of the function.
  565. #if (osCMSIS < 0x20000U)
  566. osStatus osMutexWait (osMutexId mutex_id, uint32_t millisec);
  567. #else
  568. #define osMutexWait osMutexAcquire
  569. #endif
  570. /// Release a Mutex that was obtained by \ref osMutexWait.
  571. /// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
  572. /// \return status code that indicates the execution status of the function.
  573. #if (osCMSIS < 0x20000U)
  574. osStatus osMutexRelease (osMutexId mutex_id);
  575. #endif
  576. /// Delete a Mutex object.
  577. /// \param[in] mutex_id mutex ID obtained by \ref osMutexCreate.
  578. /// \return status code that indicates the execution status of the function.
  579. #if (osCMSIS < 0x20000U)
  580. osStatus osMutexDelete (osMutexId mutex_id);
  581. #endif
  582. // ==== Semaphore Management Functions ====
  583. #if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0U)) // Semaphore available
  584. /// Define a Semaphore object.
  585. /// \param name name of the semaphore object.
  586. /// \note CAN BE CHANGED: The parameter to \b osSemaphoreDef shall be consistent but the
  587. /// macro body is implementation specific in every CMSIS-RTOS.
  588. #if defined (osObjectsExternal) // object is external
  589. #define osSemaphoreDef(name) \
  590. extern const osSemaphoreDef_t os_semaphore_def_##name
  591. #else // define the object
  592. #if (osCMSIS < 0x20000U)
  593. #define osSemaphoreDef(name) \
  594. const osSemaphoreDef_t os_semaphore_def_##name = { 0 }
  595. #else
  596. #define osSemaphoreDef(name) \
  597. const osSemaphoreDef_t os_semaphore_def_##name = \
  598. { NULL, 0U, NULL, 0U }
  599. #endif
  600. #endif
  601. /// Access a Semaphore definition.
  602. /// \param name name of the semaphore object.
  603. /// \note CAN BE CHANGED: The parameter to \b osSemaphore shall be consistent but the
  604. /// macro body is implementation specific in every CMSIS-RTOS.
  605. #define osSemaphore(name) \
  606. &os_semaphore_def_##name
  607. /// Create and Initialize a Semaphore object.
  608. /// \param[in] semaphore_def semaphore definition referenced with \ref osSemaphore.
  609. /// \param[in] count maximum and initial number of available tokens.
  610. /// \return semaphore ID for reference by other functions or NULL in case of error.
  611. osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count);
  612. /// Wait until a Semaphore token becomes available.
  613. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate.
  614. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  615. /// \return number of available tokens, or -1 in case of incorrect parameters.
  616. int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec);
  617. /// Release a Semaphore token.
  618. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate.
  619. /// \return status code that indicates the execution status of the function.
  620. #if (osCMSIS < 0x20000U)
  621. osStatus osSemaphoreRelease (osSemaphoreId semaphore_id);
  622. #endif
  623. /// Delete a Semaphore object.
  624. /// \param[in] semaphore_id semaphore object referenced with \ref osSemaphoreCreate.
  625. /// \return status code that indicates the execution status of the function.
  626. #if (osCMSIS < 0x20000U)
  627. osStatus osSemaphoreDelete (osSemaphoreId semaphore_id);
  628. #endif
  629. #endif // Semaphore available
  630. // ==== Memory Pool Management Functions ====
  631. #if (defined(osFeature_Pool) && (osFeature_Pool != 0)) // Memory Pool available
  632. /// \brief Define a Memory Pool.
  633. /// \param name name of the memory pool.
  634. /// \param no maximum number of blocks (objects) in the memory pool.
  635. /// \param type data type of a single block (object).
  636. /// \note CAN BE CHANGED: The parameter to \b osPoolDef shall be consistent but the
  637. /// macro body is implementation specific in every CMSIS-RTOS.
  638. #if defined (osObjectsExternal) // object is external
  639. #define osPoolDef(name, no, type) \
  640. extern const osPoolDef_t os_pool_def_##name
  641. #else // define the object
  642. #if (osCMSIS < 0x20000U)
  643. #define osPoolDef(name, no, type) \
  644. const osPoolDef_t os_pool_def_##name = \
  645. { (no), sizeof(type), NULL }
  646. #else
  647. #define osPoolDef(name, no, type) \
  648. const osPoolDef_t os_pool_def_##name = \
  649. { (no), sizeof(type), { NULL, 0U, NULL, 0U, NULL, 0U } }
  650. #endif
  651. #endif
  652. /// \brief Access a Memory Pool definition.
  653. /// \param name name of the memory pool
  654. /// \note CAN BE CHANGED: The parameter to \b osPool shall be consistent but the
  655. /// macro body is implementation specific in every CMSIS-RTOS.
  656. #define osPool(name) \
  657. &os_pool_def_##name
  658. /// Create and Initialize a Memory Pool object.
  659. /// \param[in] pool_def memory pool definition referenced with \ref osPool.
  660. /// \return memory pool ID for reference by other functions or NULL in case of error.
  661. osPoolId osPoolCreate (const osPoolDef_t *pool_def);
  662. /// Allocate a memory block from a Memory Pool.
  663. /// \param[in] pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  664. /// \return address of the allocated memory block or NULL in case of no memory available.
  665. void *osPoolAlloc (osPoolId pool_id);
  666. /// Allocate a memory block from a Memory Pool and set memory block to zero.
  667. /// \param[in] pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  668. /// \return address of the allocated memory block or NULL in case of no memory available.
  669. void *osPoolCAlloc (osPoolId pool_id);
  670. /// Return an allocated memory block back to a Memory Pool.
  671. /// \param[in] pool_id memory pool ID obtain referenced with \ref osPoolCreate.
  672. /// \param[in] block address of the allocated memory block to be returned to the memory pool.
  673. /// \return status code that indicates the execution status of the function.
  674. osStatus osPoolFree (osPoolId pool_id, void *block);
  675. #endif // Memory Pool available
  676. // ==== Message Queue Management Functions ====
  677. #if (defined(osFeature_MessageQ) && (osFeature_MessageQ != 0)) // Message Queue available
  678. /// \brief Create a Message Queue Definition.
  679. /// \param name name of the queue.
  680. /// \param queue_sz maximum number of messages in the queue.
  681. /// \param type data type of a single message element (for debugger).
  682. /// \note CAN BE CHANGED: The parameter to \b osMessageQDef shall be consistent but the
  683. /// macro body is implementation specific in every CMSIS-RTOS.
  684. #if defined (osObjectsExternal) // object is external
  685. #define osMessageQDef(name, queue_sz, type) \
  686. extern const osMessageQDef_t os_messageQ_def_##name
  687. #else // define the object
  688. #if (osCMSIS < 0x20000U)
  689. #define osMessageQDef(name, queue_sz, type) \
  690. const osMessageQDef_t os_messageQ_def_##name = \
  691. { (queue_sz), NULL }
  692. #else
  693. #define osMessageQDef(name, queue_sz, type) \
  694. const osMessageQDef_t os_messageQ_def_##name = \
  695. { (queue_sz), { NULL, 0U, NULL, 0U, NULL, 0U } }
  696. #endif
  697. #endif
  698. /// \brief Access a Message Queue Definition.
  699. /// \param name name of the queue
  700. /// \note CAN BE CHANGED: The parameter to \b osMessageQ shall be consistent but the
  701. /// macro body is implementation specific in every CMSIS-RTOS.
  702. #define osMessageQ(name) \
  703. &os_messageQ_def_##name
  704. /// Create and Initialize a Message Queue object.
  705. /// \param[in] queue_def message queue definition referenced with \ref osMessageQ.
  706. /// \param[in] thread_id thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
  707. /// \return message queue ID for reference by other functions or NULL in case of error.
  708. osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id);
  709. /// Put a Message to a Queue.
  710. /// \param[in] queue_id message queue ID obtained with \ref osMessageCreate.
  711. /// \param[in] info message information.
  712. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  713. /// \return status code that indicates the execution status of the function.
  714. osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec);
  715. /// Get a Message from a Queue or timeout if Queue is empty.
  716. /// \param[in] queue_id message queue ID obtained with \ref osMessageCreate.
  717. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  718. /// \return event information that includes status code.
  719. osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec);
  720. #endif // Message Queue available
  721. // ==== Mail Queue Management Functions ====
  722. #if (defined(osFeature_MailQ) && (osFeature_MailQ != 0)) // Mail Queue available
  723. /// \brief Create a Mail Queue Definition.
  724. /// \param name name of the queue.
  725. /// \param queue_sz maximum number of mails in the queue.
  726. /// \param type data type of a single mail element.
  727. /// \note CAN BE CHANGED: The parameter to \b osMailQDef shall be consistent but the
  728. /// macro body is implementation specific in every CMSIS-RTOS.
  729. #if defined (osObjectsExternal) // object is external
  730. #define osMailQDef(name, queue_sz, type) \
  731. extern const osMailQDef_t os_mailQ_def_##name
  732. #else // define the object
  733. #if (osCMSIS < 0x20000U)
  734. #define osMailQDef(name, queue_sz, type) \
  735. const osMailQDef_t os_mailQ_def_##name = \
  736. { (queue_sz), sizeof(type), NULL }
  737. #else
  738. #define osMailQDef(name, queue_sz, type) \
  739. static void *os_mail_p_##name[2]; \
  740. const osMailQDef_t os_mailQ_def_##name = \
  741. { (queue_sz), sizeof(type), (&os_mail_p_##name), \
  742. { NULL, 0U, NULL, 0U, NULL, 0U }, \
  743. { NULL, 0U, NULL, 0U, NULL, 0U } }
  744. #endif
  745. #endif
  746. /// \brief Access a Mail Queue Definition.
  747. /// \param name name of the queue
  748. /// \note CAN BE CHANGED: The parameter to \b osMailQ shall be consistent but the
  749. /// macro body is implementation specific in every CMSIS-RTOS.
  750. #define osMailQ(name) \
  751. &os_mailQ_def_##name
  752. /// Create and Initialize a Mail Queue object.
  753. /// \param[in] queue_def mail queue definition referenced with \ref osMailQ.
  754. /// \param[in] thread_id thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
  755. /// \return mail queue ID for reference by other functions or NULL in case of error.
  756. osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id);
  757. /// Allocate a memory block for mail from a mail memory pool.
  758. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  759. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  760. /// \return pointer to memory block that can be filled with mail or NULL in case of error.
  761. void *osMailAlloc (osMailQId queue_id, uint32_t millisec);
  762. /// Allocate a memory block for mail from a mail memory pool and set memory block to zero.
  763. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  764. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
  765. /// \return pointer to memory block that can be filled with mail or NULL in case of error.
  766. void *osMailCAlloc (osMailQId queue_id, uint32_t millisec);
  767. /// Put a Mail into a Queue.
  768. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  769. /// \param[in] mail pointer to memory with mail to put into a queue.
  770. /// \return status code that indicates the execution status of the function.
  771. osStatus osMailPut (osMailQId queue_id, const void *mail);
  772. /// Get a Mail from a Queue or timeout if Queue is empty.
  773. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  774. /// \param[in] millisec \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  775. /// \return event information that includes status code.
  776. osEvent osMailGet (osMailQId queue_id, uint32_t millisec);
  777. /// Free a memory block by returning it to a mail memory pool.
  778. /// \param[in] queue_id mail queue ID obtained with \ref osMailCreate.
  779. /// \param[in] mail pointer to memory block that was obtained with \ref osMailGet.
  780. /// \return status code that indicates the execution status of the function.
  781. osStatus osMailFree (osMailQId queue_id, void *mail);
  782. #endif // Mail Queue available
  783. #ifdef __cplusplus
  784. }
  785. #endif
  786. #endif // CMSIS_OS_H_