cmsis_os.h 39 KB

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