cmsis_os.h 40 KB

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