cmsis_os2.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * Copyright (c) 2013-2023 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: 3. April 2023
  21. * $Revision: V2.2.0
  22. *
  23. * Project: CMSIS-RTOS2 API
  24. * Title: cmsis_os2.h header file
  25. *
  26. * Version 2.2.0
  27. * Added support for Process Isolation (Functional Safety):
  28. * - Kernel Management: osKernelProtect, osKernelDestroyClass
  29. * - Thread Management: osThreadGetClass, osThreadGetZone,
  30. * osThreadSuspendClass, osThreadResumeClass
  31. * osThreadTerminateZone,
  32. * osThreadFeedWatchdog,
  33. * osThreadProtectPrivileged
  34. * - Thread attributes: osThreadZone, osThreadUnprivileged/osThreadPrivileged
  35. * - Object attributes: osSafetyClass
  36. * - Handler functions: osWatchdogAlarm_Handler
  37. * - Zone Management: osZoneSetup_Callback
  38. * - Exception Faults: osFaultResume
  39. * Additional functions allowed to be called from Interrupt Service Routines:
  40. * - osThreadGetName, osTimerGetName, osEventFlagsGetName, osMutexGetName,
  41. * osSemaphoreGetName, osMemoryPoolGetName, osMessageQueueGetName
  42. * Version 2.1.3
  43. * Additional functions allowed to be called from Interrupt Service Routines:
  44. * - osThreadGetId
  45. * Version 2.1.2
  46. * Additional functions allowed to be called from Interrupt Service Routines:
  47. * - osKernelGetInfo, osKernelGetState
  48. * Version 2.1.1
  49. * Additional functions allowed to be called from Interrupt Service Routines:
  50. * - osKernelGetTickCount, osKernelGetTickFreq
  51. * Changed Kernel Tick type to uint32_t:
  52. * - updated: osKernelGetTickCount, osDelayUntil
  53. * Version 2.1.0
  54. * Support for critical and uncritical sections (nesting safe):
  55. * - updated: osKernelLock, osKernelUnlock
  56. * - added: osKernelRestoreLock
  57. * Updated Thread and Event Flags:
  58. * - changed flags parameter and return type from int32_t to uint32_t
  59. * Version 2.0.0
  60. * Initial Release
  61. *---------------------------------------------------------------------------*/
  62. #ifndef CMSIS_OS2_H_
  63. #define CMSIS_OS2_H_
  64. #ifndef __NO_RETURN
  65. #if defined(__CC_ARM)
  66. #define __NO_RETURN __declspec(noreturn)
  67. #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  68. #define __NO_RETURN __attribute__((__noreturn__))
  69. #elif defined(__GNUC__)
  70. #define __NO_RETURN __attribute__((__noreturn__))
  71. #elif defined(__ICCARM__)
  72. #define __NO_RETURN __noreturn
  73. #else
  74. #define __NO_RETURN
  75. #endif
  76. #endif
  77. #include <stdint.h>
  78. #include <stddef.h>
  79. #ifdef __cplusplus
  80. extern "C"
  81. {
  82. #endif
  83. // ==== Enumerations, structures, defines ====
  84. /// Version information.
  85. typedef struct {
  86. uint32_t api; ///< API version (major.minor.rev: mmnnnrrrr dec).
  87. uint32_t kernel; ///< Kernel version (major.minor.rev: mmnnnrrrr dec).
  88. } osVersion_t;
  89. /// Kernel state.
  90. typedef enum {
  91. osKernelInactive = 0, ///< Inactive.
  92. osKernelReady = 1, ///< Ready.
  93. osKernelRunning = 2, ///< Running.
  94. osKernelLocked = 3, ///< Locked.
  95. osKernelSuspended = 4, ///< Suspended.
  96. osKernelError = -1, ///< Error.
  97. osKernelReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
  98. } osKernelState_t;
  99. /// Thread state.
  100. typedef enum {
  101. osThreadInactive = 0, ///< Inactive.
  102. osThreadReady = 1, ///< Ready.
  103. osThreadRunning = 2, ///< Running.
  104. osThreadBlocked = 3, ///< Blocked.
  105. osThreadTerminated = 4, ///< Terminated.
  106. osThreadError = -1, ///< Error.
  107. osThreadReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
  108. } osThreadState_t;
  109. /// Priority values.
  110. typedef enum {
  111. osPriorityNone = 0, ///< No priority (not initialized).
  112. osPriorityIdle = 1, ///< Reserved for Idle thread.
  113. osPriorityLow = 8, ///< Priority: low
  114. osPriorityLow1 = 8+1, ///< Priority: low + 1
  115. osPriorityLow2 = 8+2, ///< Priority: low + 2
  116. osPriorityLow3 = 8+3, ///< Priority: low + 3
  117. osPriorityLow4 = 8+4, ///< Priority: low + 4
  118. osPriorityLow5 = 8+5, ///< Priority: low + 5
  119. osPriorityLow6 = 8+6, ///< Priority: low + 6
  120. osPriorityLow7 = 8+7, ///< Priority: low + 7
  121. osPriorityBelowNormal = 16, ///< Priority: below normal
  122. osPriorityBelowNormal1 = 16+1, ///< Priority: below normal + 1
  123. osPriorityBelowNormal2 = 16+2, ///< Priority: below normal + 2
  124. osPriorityBelowNormal3 = 16+3, ///< Priority: below normal + 3
  125. osPriorityBelowNormal4 = 16+4, ///< Priority: below normal + 4
  126. osPriorityBelowNormal5 = 16+5, ///< Priority: below normal + 5
  127. osPriorityBelowNormal6 = 16+6, ///< Priority: below normal + 6
  128. osPriorityBelowNormal7 = 16+7, ///< Priority: below normal + 7
  129. osPriorityNormal = 24, ///< Priority: normal
  130. osPriorityNormal1 = 24+1, ///< Priority: normal + 1
  131. osPriorityNormal2 = 24+2, ///< Priority: normal + 2
  132. osPriorityNormal3 = 24+3, ///< Priority: normal + 3
  133. osPriorityNormal4 = 24+4, ///< Priority: normal + 4
  134. osPriorityNormal5 = 24+5, ///< Priority: normal + 5
  135. osPriorityNormal6 = 24+6, ///< Priority: normal + 6
  136. osPriorityNormal7 = 24+7, ///< Priority: normal + 7
  137. osPriorityAboveNormal = 32, ///< Priority: above normal
  138. osPriorityAboveNormal1 = 32+1, ///< Priority: above normal + 1
  139. osPriorityAboveNormal2 = 32+2, ///< Priority: above normal + 2
  140. osPriorityAboveNormal3 = 32+3, ///< Priority: above normal + 3
  141. osPriorityAboveNormal4 = 32+4, ///< Priority: above normal + 4
  142. osPriorityAboveNormal5 = 32+5, ///< Priority: above normal + 5
  143. osPriorityAboveNormal6 = 32+6, ///< Priority: above normal + 6
  144. osPriorityAboveNormal7 = 32+7, ///< Priority: above normal + 7
  145. osPriorityHigh = 40, ///< Priority: high
  146. osPriorityHigh1 = 40+1, ///< Priority: high + 1
  147. osPriorityHigh2 = 40+2, ///< Priority: high + 2
  148. osPriorityHigh3 = 40+3, ///< Priority: high + 3
  149. osPriorityHigh4 = 40+4, ///< Priority: high + 4
  150. osPriorityHigh5 = 40+5, ///< Priority: high + 5
  151. osPriorityHigh6 = 40+6, ///< Priority: high + 6
  152. osPriorityHigh7 = 40+7, ///< Priority: high + 7
  153. osPriorityRealtime = 48, ///< Priority: realtime
  154. osPriorityRealtime1 = 48+1, ///< Priority: realtime + 1
  155. osPriorityRealtime2 = 48+2, ///< Priority: realtime + 2
  156. osPriorityRealtime3 = 48+3, ///< Priority: realtime + 3
  157. osPriorityRealtime4 = 48+4, ///< Priority: realtime + 4
  158. osPriorityRealtime5 = 48+5, ///< Priority: realtime + 5
  159. osPriorityRealtime6 = 48+6, ///< Priority: realtime + 6
  160. osPriorityRealtime7 = 48+7, ///< Priority: realtime + 7
  161. osPriorityISR = 56, ///< Reserved for ISR deferred thread.
  162. osPriorityError = -1, ///< System cannot determine priority or illegal priority.
  163. osPriorityReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
  164. } osPriority_t;
  165. /// Entry point of a thread.
  166. typedef void (*osThreadFunc_t) (void *argument);
  167. /// Timer callback function.
  168. typedef void (*osTimerFunc_t) (void *argument);
  169. /// Timer type.
  170. typedef enum {
  171. osTimerOnce = 0, ///< One-shot timer.
  172. osTimerPeriodic = 1 ///< Repeating timer.
  173. } osTimerType_t;
  174. // Timeout value.
  175. #define osWaitForever 0xFFFFFFFFU ///< Wait forever timeout value.
  176. // Flags options (\ref osThreadFlagsWait and \ref osEventFlagsWait).
  177. #define osFlagsWaitAny 0x00000000U ///< Wait for any flag (default).
  178. #define osFlagsWaitAll 0x00000001U ///< Wait for all flags.
  179. #define osFlagsNoClear 0x00000002U ///< Do not clear flags which have been specified to wait for.
  180. // Flags errors (returned by osThreadFlagsXxxx and osEventFlagsXxxx).
  181. #define osFlagsError 0x80000000U ///< Error indicator.
  182. #define osFlagsErrorUnknown 0xFFFFFFFFU ///< osError (-1).
  183. #define osFlagsErrorTimeout 0xFFFFFFFEU ///< osErrorTimeout (-2).
  184. #define osFlagsErrorResource 0xFFFFFFFDU ///< osErrorResource (-3).
  185. #define osFlagsErrorParameter 0xFFFFFFFCU ///< osErrorParameter (-4).
  186. #define osFlagsErrorISR 0xFFFFFFFAU ///< osErrorISR (-6).
  187. #define osFlagsErrorSafetyClass 0xFFFFFFF9U ///< osErrorSafetyClass (-7).
  188. // Thread attributes (attr_bits in \ref osThreadAttr_t).
  189. #define osThreadDetached 0x00000000U ///< Thread created in detached mode (default)
  190. #define osThreadJoinable 0x00000001U ///< Thread created in joinable mode
  191. #define osThreadUnprivileged 0x00000002U ///< Thread runs in unprivileged mode
  192. #define osThreadPrivileged 0x00000004U ///< Thread runs in privileged mode
  193. #define osThreadZone_Pos 8U ///< MPU protected zone position
  194. #define osThreadZone_Msk (0x3FUL << osThreadZone_Pos) ///< MPU protected zone mask
  195. #define osThreadZone_Valid (0x80UL << osThreadZone_Pos) ///< MPU protected zone valid flag
  196. #define osThreadZone(n) ((((n) << osThreadZone_Pos) & osThreadZone_Msk) | \
  197. osThreadZone_Valid) ///< MPU protected zone
  198. // Mutex attributes (attr_bits in \ref osMutexAttr_t).
  199. #define osMutexRecursive 0x00000001U ///< Recursive mutex.
  200. #define osMutexPrioInherit 0x00000002U ///< Priority inherit protocol.
  201. #define osMutexRobust 0x00000008U ///< Robust mutex.
  202. // Object attributes (attr_bits in all objects)
  203. #define osSafetyClass_Pos 16U ///< Safety class position
  204. #define osSafetyClass_Msk (0x0FUL << osSafetyClass_Pos) ///< Safety class mask
  205. #define osSafetyClass_Valid (0x10UL << osSafetyClass_Pos) ///< Safety class valid flag
  206. #define osSafetyClass(n) ((((n) << osSafetyClass_Pos) & osSafetyClass_Msk) | \
  207. osSafetyClass_Valid) ///< Safety class
  208. // Safety mode (\ref osThreadSuspendClass, \ref osThreadResumeClass and \ref osKernelDestroyClass).
  209. #define osSafetyWithSameClass 0x00000001U ///< Objects with same safety class.
  210. #define osSafetyWithLowerClass 0x00000002U ///< Objects with lower safety class.
  211. // Error indication (returned by \ref osThreadGetClass and \ref osThreadGetZone).
  212. #define osErrorId 0xFFFFFFFFU ///< osError (-1).
  213. /// Status code values returned by CMSIS-RTOS functions.
  214. typedef enum {
  215. osOK = 0, ///< Operation completed successfully.
  216. osError = -1, ///< Unspecified RTOS error: run-time error but no other error message fits.
  217. osErrorTimeout = -2, ///< Operation not completed within the timeout period.
  218. osErrorResource = -3, ///< Resource not available.
  219. osErrorParameter = -4, ///< Parameter error.
  220. osErrorNoMemory = -5, ///< System is out of memory: it was impossible to allocate or reserve memory for the operation.
  221. osErrorISR = -6, ///< Not allowed in ISR context: the function cannot be called from interrupt service routines.
  222. osErrorSafetyClass = -7, ///< Operation denied because of safety class violation.
  223. osStatusReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
  224. } osStatus_t;
  225. /// \details Thread ID identifies the thread.
  226. typedef void *osThreadId_t;
  227. /// \details Timer ID identifies the timer.
  228. typedef void *osTimerId_t;
  229. /// \details Event Flags ID identifies the event flags.
  230. typedef void *osEventFlagsId_t;
  231. /// \details Mutex ID identifies the mutex.
  232. typedef void *osMutexId_t;
  233. /// \details Semaphore ID identifies the semaphore.
  234. typedef void *osSemaphoreId_t;
  235. /// \details Memory Pool ID identifies the memory pool.
  236. typedef void *osMemoryPoolId_t;
  237. /// \details Message Queue ID identifies the message queue.
  238. typedef void *osMessageQueueId_t;
  239. #ifndef TZ_MODULEID_T
  240. #define TZ_MODULEID_T
  241. /// \details Data type that identifies secure software modules called by a process.
  242. typedef uint32_t TZ_ModuleId_t;
  243. #endif
  244. /// Attributes structure for thread.
  245. typedef struct {
  246. const char *name; ///< name of the thread
  247. uint32_t attr_bits; ///< attribute bits
  248. void *cb_mem; ///< memory for control block
  249. uint32_t cb_size; ///< size of provided memory for control block
  250. void *stack_mem; ///< memory for stack
  251. uint32_t stack_size; ///< size of stack
  252. osPriority_t priority; ///< initial thread priority (default: osPriorityNormal)
  253. TZ_ModuleId_t tz_module; ///< TrustZone module identifier
  254. uint32_t reserved; ///< reserved (must be 0)
  255. } osThreadAttr_t;
  256. /// Attributes structure for timer.
  257. typedef struct {
  258. const char *name; ///< name of the timer
  259. uint32_t attr_bits; ///< attribute bits
  260. void *cb_mem; ///< memory for control block
  261. uint32_t cb_size; ///< size of provided memory for control block
  262. } osTimerAttr_t;
  263. /// Attributes structure for event flags.
  264. typedef struct {
  265. const char *name; ///< name of the event flags
  266. uint32_t attr_bits; ///< attribute bits
  267. void *cb_mem; ///< memory for control block
  268. uint32_t cb_size; ///< size of provided memory for control block
  269. } osEventFlagsAttr_t;
  270. /// Attributes structure for mutex.
  271. typedef struct {
  272. const char *name; ///< name of the mutex
  273. uint32_t attr_bits; ///< attribute bits
  274. void *cb_mem; ///< memory for control block
  275. uint32_t cb_size; ///< size of provided memory for control block
  276. } osMutexAttr_t;
  277. /// Attributes structure for semaphore.
  278. typedef struct {
  279. const char *name; ///< name of the semaphore
  280. uint32_t attr_bits; ///< attribute bits
  281. void *cb_mem; ///< memory for control block
  282. uint32_t cb_size; ///< size of provided memory for control block
  283. } osSemaphoreAttr_t;
  284. /// Attributes structure for memory pool.
  285. typedef struct {
  286. const char *name; ///< name of the memory pool
  287. uint32_t attr_bits; ///< attribute bits
  288. void *cb_mem; ///< memory for control block
  289. uint32_t cb_size; ///< size of provided memory for control block
  290. void *mp_mem; ///< memory for data storage
  291. uint32_t mp_size; ///< size of provided memory for data storage
  292. } osMemoryPoolAttr_t;
  293. /// Attributes structure for message queue.
  294. typedef struct {
  295. const char *name; ///< name of the message queue
  296. uint32_t attr_bits; ///< attribute bits
  297. void *cb_mem; ///< memory for control block
  298. uint32_t cb_size; ///< size of provided memory for control block
  299. void *mq_mem; ///< memory for data storage
  300. uint32_t mq_size; ///< size of provided memory for data storage
  301. } osMessageQueueAttr_t;
  302. // ==== Kernel Management Functions ====
  303. /// Initialize the RTOS Kernel.
  304. /// \return status code that indicates the execution status of the function.
  305. osStatus_t osKernelInitialize (void);
  306. /// Get RTOS Kernel Information.
  307. /// \param[out] version pointer to buffer for retrieving version information.
  308. /// \param[out] id_buf pointer to buffer for retrieving kernel identification string.
  309. /// \param[in] id_size size of buffer for kernel identification string.
  310. /// \return status code that indicates the execution status of the function.
  311. osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size);
  312. /// Get the current RTOS Kernel state.
  313. /// \return current RTOS Kernel state.
  314. osKernelState_t osKernelGetState (void);
  315. /// Start the RTOS Kernel scheduler.
  316. /// \return status code that indicates the execution status of the function.
  317. osStatus_t osKernelStart (void);
  318. /// Lock the RTOS Kernel scheduler.
  319. /// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
  320. int32_t osKernelLock (void);
  321. /// Unlock the RTOS Kernel scheduler.
  322. /// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
  323. int32_t osKernelUnlock (void);
  324. /// Restore the RTOS Kernel scheduler lock state.
  325. /// \param[in] lock lock state obtained by \ref osKernelLock or \ref osKernelUnlock.
  326. /// \return new lock state (1 - locked, 0 - not locked, error code if negative).
  327. int32_t osKernelRestoreLock (int32_t lock);
  328. /// Suspend the RTOS Kernel scheduler.
  329. /// \return time in ticks, for how long the system can sleep or power-down.
  330. uint32_t osKernelSuspend (void);
  331. /// Resume the RTOS Kernel scheduler.
  332. /// \param[in] sleep_ticks time in ticks for how long the system was in sleep or power-down mode.
  333. void osKernelResume (uint32_t sleep_ticks);
  334. /// Protect the RTOS Kernel scheduler access.
  335. /// \param[in] safety_class safety class.
  336. /// \return status code that indicates the execution status of the function.
  337. osStatus_t osKernelProtect (uint32_t safety_class);
  338. /// Destroy objects for specified safety classes.
  339. /// \param[in] safety_class safety class.
  340. /// \param[in] mode safety mode.
  341. /// \return status code that indicates the execution status of the function.
  342. osStatus_t osKernelDestroyClass (uint32_t safety_class, uint32_t mode);
  343. /// Get the RTOS kernel tick count.
  344. /// \return RTOS kernel current tick count.
  345. uint32_t osKernelGetTickCount (void);
  346. /// Get the RTOS kernel tick frequency.
  347. /// \return frequency of the kernel tick in hertz, i.e. kernel ticks per second.
  348. uint32_t osKernelGetTickFreq (void);
  349. /// Get the RTOS kernel system timer count.
  350. /// \return RTOS kernel current system timer count as 32-bit value.
  351. uint32_t osKernelGetSysTimerCount (void);
  352. /// Get the RTOS kernel system timer frequency.
  353. /// \return frequency of the system timer in hertz, i.e. timer ticks per second.
  354. uint32_t osKernelGetSysTimerFreq (void);
  355. // ==== Thread Management Functions ====
  356. /// Create a thread and add it to Active Threads.
  357. /// \param[in] func thread function.
  358. /// \param[in] argument pointer that is passed to the thread function as start argument.
  359. /// \param[in] attr thread attributes; NULL: default values.
  360. /// \return thread ID for reference by other functions or NULL in case of error.
  361. osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr);
  362. /// Get name of a thread.
  363. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  364. /// \return name as null-terminated string.
  365. const char *osThreadGetName (osThreadId_t thread_id);
  366. /// Get safety class of a thread.
  367. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  368. /// \return safety class of the specified thread.
  369. uint32_t osThreadGetClass (osThreadId_t thread_id);
  370. /// Get MPU protected zone of a thread.
  371. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  372. /// \return MPU protected zone of the specified thread.
  373. uint32_t osThreadGetZone (osThreadId_t thread_id);
  374. /// Return the thread ID of the current running thread.
  375. /// \return thread ID for reference by other functions or NULL in case of error.
  376. osThreadId_t osThreadGetId (void);
  377. /// Get current thread state of a thread.
  378. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  379. /// \return current thread state of the specified thread.
  380. osThreadState_t osThreadGetState (osThreadId_t thread_id);
  381. /// Get stack size of a thread.
  382. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  383. /// \return stack size in bytes.
  384. uint32_t osThreadGetStackSize (osThreadId_t thread_id);
  385. /// Get available stack space of a thread based on stack watermark recording during execution.
  386. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  387. /// \return remaining stack space in bytes.
  388. uint32_t osThreadGetStackSpace (osThreadId_t thread_id);
  389. /// Change priority of a thread.
  390. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  391. /// \param[in] priority new priority value for the thread function.
  392. /// \return status code that indicates the execution status of the function.
  393. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority);
  394. /// Get current priority of a thread.
  395. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  396. /// \return current priority value of the specified thread.
  397. osPriority_t osThreadGetPriority (osThreadId_t thread_id);
  398. /// Pass control to next thread that is in state \b READY.
  399. /// \return status code that indicates the execution status of the function.
  400. osStatus_t osThreadYield (void);
  401. /// Suspend execution of a thread.
  402. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  403. /// \return status code that indicates the execution status of the function.
  404. osStatus_t osThreadSuspend (osThreadId_t thread_id);
  405. /// Resume execution of a thread.
  406. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  407. /// \return status code that indicates the execution status of the function.
  408. osStatus_t osThreadResume (osThreadId_t thread_id);
  409. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  410. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  411. /// \return status code that indicates the execution status of the function.
  412. osStatus_t osThreadDetach (osThreadId_t thread_id);
  413. /// Wait for specified thread to terminate.
  414. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  415. /// \return status code that indicates the execution status of the function.
  416. osStatus_t osThreadJoin (osThreadId_t thread_id);
  417. /// Terminate execution of current running thread.
  418. __NO_RETURN void osThreadExit (void);
  419. /// Terminate execution of a thread.
  420. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  421. /// \return status code that indicates the execution status of the function.
  422. osStatus_t osThreadTerminate (osThreadId_t thread_id);
  423. /// Feed watchdog of the current running thread.
  424. /// \param[in] ticks \ref kernelTimer "time ticks" value until the thread watchdog expires, or 0 to stop the watchdog
  425. /// \return status code that indicates the execution status of the function.
  426. osStatus_t osThreadFeedWatchdog (uint32_t ticks);
  427. /// Protect creation of privileged threads.
  428. /// \return status code that indicates the execution status of the function.
  429. osStatus_t osThreadProtectPrivileged (void);
  430. /// Suspend execution of threads for specified safety classes.
  431. /// \param[in] safety_class safety class.
  432. /// \param[in] mode safety mode.
  433. /// \return status code that indicates the execution status of the function.
  434. osStatus_t osThreadSuspendClass (uint32_t safety_class, uint32_t mode);
  435. /// Resume execution of threads for specified safety classes.
  436. /// \param[in] safety_class safety class.
  437. /// \param[in] mode safety mode.
  438. /// \return status code that indicates the execution status of the function.
  439. osStatus_t osThreadResumeClass (uint32_t safety_class, uint32_t mode);
  440. /// Terminate execution of threads assigned to a specified MPU protected zone.
  441. /// \param[in] zone MPU protected zone.
  442. /// \return status code that indicates the execution status of the function.
  443. osStatus_t osThreadTerminateZone (uint32_t zone);
  444. /// Get number of active threads.
  445. /// \return number of active threads.
  446. uint32_t osThreadGetCount (void);
  447. /// Enumerate active threads.
  448. /// \param[out] thread_array pointer to array for retrieving thread IDs.
  449. /// \param[in] array_items maximum number of items in array for retrieving thread IDs.
  450. /// \return number of enumerated threads.
  451. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items);
  452. // ==== Thread Flags Functions ====
  453. /// Set the specified Thread Flags of a thread.
  454. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  455. /// \param[in] flags specifies the flags of the thread that shall be set.
  456. /// \return thread flags after setting or error code if highest bit set.
  457. uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags);
  458. /// Clear the specified Thread Flags of current running thread.
  459. /// \param[in] flags specifies the flags of the thread that shall be cleared.
  460. /// \return thread flags before clearing or error code if highest bit set.
  461. uint32_t osThreadFlagsClear (uint32_t flags);
  462. /// Get the current Thread Flags of current running thread.
  463. /// \return current thread flags.
  464. uint32_t osThreadFlagsGet (void);
  465. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  466. /// \param[in] flags specifies the flags to wait for.
  467. /// \param[in] options specifies flags options (osFlagsXxxx).
  468. /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  469. /// \return thread flags before clearing or error code if highest bit set.
  470. uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout);
  471. // ==== Generic Wait Functions ====
  472. /// Wait for Timeout (Time Delay).
  473. /// \param[in] ticks \ref CMSIS_RTOS_TimeOutValue "time ticks" value
  474. /// \return status code that indicates the execution status of the function.
  475. osStatus_t osDelay (uint32_t ticks);
  476. /// Wait until specified time.
  477. /// \param[in] ticks absolute time in ticks
  478. /// \return status code that indicates the execution status of the function.
  479. osStatus_t osDelayUntil (uint32_t ticks);
  480. // ==== Timer Management Functions ====
  481. /// Create and Initialize a timer.
  482. /// \param[in] func function pointer to callback function.
  483. /// \param[in] type \ref osTimerOnce for one-shot or \ref osTimerPeriodic for periodic behavior.
  484. /// \param[in] argument argument to the timer callback function.
  485. /// \param[in] attr timer attributes; NULL: default values.
  486. /// \return timer ID for reference by other functions or NULL in case of error.
  487. osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr);
  488. /// Get name of a timer.
  489. /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
  490. /// \return name as null-terminated string.
  491. const char *osTimerGetName (osTimerId_t timer_id);
  492. /// Start or restart a timer.
  493. /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
  494. /// \param[in] ticks \ref CMSIS_RTOS_TimeOutValue "time ticks" value of the timer.
  495. /// \return status code that indicates the execution status of the function.
  496. osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks);
  497. /// Stop a timer.
  498. /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
  499. /// \return status code that indicates the execution status of the function.
  500. osStatus_t osTimerStop (osTimerId_t timer_id);
  501. /// Check if a timer is running.
  502. /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
  503. /// \return 0 not running, 1 running.
  504. uint32_t osTimerIsRunning (osTimerId_t timer_id);
  505. /// Delete a timer.
  506. /// \param[in] timer_id timer ID obtained by \ref osTimerNew.
  507. /// \return status code that indicates the execution status of the function.
  508. osStatus_t osTimerDelete (osTimerId_t timer_id);
  509. // ==== Event Flags Management Functions ====
  510. /// Create and Initialize an Event Flags object.
  511. /// \param[in] attr event flags attributes; NULL: default values.
  512. /// \return event flags ID for reference by other functions or NULL in case of error.
  513. osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr);
  514. /// Get name of an Event Flags object.
  515. /// \param[in] ef_id event flags ID obtained by \ref osEventFlagsNew.
  516. /// \return name as null-terminated string.
  517. const char *osEventFlagsGetName (osEventFlagsId_t ef_id);
  518. /// Set the specified Event Flags.
  519. /// \param[in] ef_id event flags ID obtained by \ref osEventFlagsNew.
  520. /// \param[in] flags specifies the flags that shall be set.
  521. /// \return event flags after setting or error code if highest bit set.
  522. uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags);
  523. /// Clear the specified Event Flags.
  524. /// \param[in] ef_id event flags ID obtained by \ref osEventFlagsNew.
  525. /// \param[in] flags specifies the flags that shall be cleared.
  526. /// \return event flags before clearing or error code if highest bit set.
  527. uint32_t osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags);
  528. /// Get the current Event Flags.
  529. /// \param[in] ef_id event flags ID obtained by \ref osEventFlagsNew.
  530. /// \return current event flags.
  531. uint32_t osEventFlagsGet (osEventFlagsId_t ef_id);
  532. /// Wait for one or more Event Flags to become signaled.
  533. /// \param[in] ef_id event flags ID obtained by \ref osEventFlagsNew.
  534. /// \param[in] flags specifies the flags to wait for.
  535. /// \param[in] options specifies flags options (osFlagsXxxx).
  536. /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  537. /// \return event flags before clearing or error code if highest bit set.
  538. uint32_t osEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout);
  539. /// Delete an Event Flags object.
  540. /// \param[in] ef_id event flags ID obtained by \ref osEventFlagsNew.
  541. /// \return status code that indicates the execution status of the function.
  542. osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id);
  543. // ==== Mutex Management Functions ====
  544. /// Create and Initialize a Mutex object.
  545. /// \param[in] attr mutex attributes; NULL: default values.
  546. /// \return mutex ID for reference by other functions or NULL in case of error.
  547. osMutexId_t osMutexNew (const osMutexAttr_t *attr);
  548. /// Get name of a Mutex object.
  549. /// \param[in] mutex_id mutex ID obtained by \ref osMutexNew.
  550. /// \return name as null-terminated string.
  551. const char *osMutexGetName (osMutexId_t mutex_id);
  552. /// Acquire a Mutex or timeout if it is locked.
  553. /// \param[in] mutex_id mutex ID obtained by \ref osMutexNew.
  554. /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  555. /// \return status code that indicates the execution status of the function.
  556. osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout);
  557. /// Release a Mutex that was acquired by \ref osMutexAcquire.
  558. /// \param[in] mutex_id mutex ID obtained by \ref osMutexNew.
  559. /// \return status code that indicates the execution status of the function.
  560. osStatus_t osMutexRelease (osMutexId_t mutex_id);
  561. /// Get Thread which owns a Mutex object.
  562. /// \param[in] mutex_id mutex ID obtained by \ref osMutexNew.
  563. /// \return thread ID of owner thread or NULL when mutex was not acquired.
  564. osThreadId_t osMutexGetOwner (osMutexId_t mutex_id);
  565. /// Delete a Mutex object.
  566. /// \param[in] mutex_id mutex ID obtained by \ref osMutexNew.
  567. /// \return status code that indicates the execution status of the function.
  568. osStatus_t osMutexDelete (osMutexId_t mutex_id);
  569. // ==== Semaphore Management Functions ====
  570. /// Create and Initialize a Semaphore object.
  571. /// \param[in] max_count maximum number of available tokens.
  572. /// \param[in] initial_count initial number of available tokens.
  573. /// \param[in] attr semaphore attributes; NULL: default values.
  574. /// \return semaphore ID for reference by other functions or NULL in case of error.
  575. osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr);
  576. /// Get name of a Semaphore object.
  577. /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
  578. /// \return name as null-terminated string.
  579. const char *osSemaphoreGetName (osSemaphoreId_t semaphore_id);
  580. /// Acquire a Semaphore token or timeout if no tokens are available.
  581. /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
  582. /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  583. /// \return status code that indicates the execution status of the function.
  584. osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout);
  585. /// Release a Semaphore token up to the initial maximum count.
  586. /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
  587. /// \return status code that indicates the execution status of the function.
  588. osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id);
  589. /// Get current Semaphore token count.
  590. /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
  591. /// \return number of tokens available.
  592. uint32_t osSemaphoreGetCount (osSemaphoreId_t semaphore_id);
  593. /// Delete a Semaphore object.
  594. /// \param[in] semaphore_id semaphore ID obtained by \ref osSemaphoreNew.
  595. /// \return status code that indicates the execution status of the function.
  596. osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id);
  597. // ==== Memory Pool Management Functions ====
  598. /// Create and Initialize a Memory Pool object.
  599. /// \param[in] block_count maximum number of memory blocks in memory pool.
  600. /// \param[in] block_size memory block size in bytes.
  601. /// \param[in] attr memory pool attributes; NULL: default values.
  602. /// \return memory pool ID for reference by other functions or NULL in case of error.
  603. osMemoryPoolId_t osMemoryPoolNew (uint32_t block_count, uint32_t block_size, const osMemoryPoolAttr_t *attr);
  604. /// Get name of a Memory Pool object.
  605. /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
  606. /// \return name as null-terminated string.
  607. const char *osMemoryPoolGetName (osMemoryPoolId_t mp_id);
  608. /// Allocate a memory block from a Memory Pool.
  609. /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
  610. /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  611. /// \return address of the allocated memory block or NULL in case of no memory is available.
  612. void *osMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout);
  613. /// Return an allocated memory block back to a Memory Pool.
  614. /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
  615. /// \param[in] block address of the allocated memory block to be returned to the memory pool.
  616. /// \return status code that indicates the execution status of the function.
  617. osStatus_t osMemoryPoolFree (osMemoryPoolId_t mp_id, void *block);
  618. /// Get maximum number of memory blocks in a Memory Pool.
  619. /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
  620. /// \return maximum number of memory blocks.
  621. uint32_t osMemoryPoolGetCapacity (osMemoryPoolId_t mp_id);
  622. /// Get memory block size in a Memory Pool.
  623. /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
  624. /// \return memory block size in bytes.
  625. uint32_t osMemoryPoolGetBlockSize (osMemoryPoolId_t mp_id);
  626. /// Get number of memory blocks used in a Memory Pool.
  627. /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
  628. /// \return number of memory blocks used.
  629. uint32_t osMemoryPoolGetCount (osMemoryPoolId_t mp_id);
  630. /// Get number of memory blocks available in a Memory Pool.
  631. /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
  632. /// \return number of memory blocks available.
  633. uint32_t osMemoryPoolGetSpace (osMemoryPoolId_t mp_id);
  634. /// Delete a Memory Pool object.
  635. /// \param[in] mp_id memory pool ID obtained by \ref osMemoryPoolNew.
  636. /// \return status code that indicates the execution status of the function.
  637. osStatus_t osMemoryPoolDelete (osMemoryPoolId_t mp_id);
  638. // ==== Message Queue Management Functions ====
  639. /// Create and Initialize a Message Queue object.
  640. /// \param[in] msg_count maximum number of messages in queue.
  641. /// \param[in] msg_size maximum message size in bytes.
  642. /// \param[in] attr message queue attributes; NULL: default values.
  643. /// \return message queue ID for reference by other functions or NULL in case of error.
  644. osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);
  645. /// Get name of a Message Queue object.
  646. /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
  647. /// \return name as null-terminated string.
  648. const char *osMessageQueueGetName (osMessageQueueId_t mq_id);
  649. /// Put a Message into a Queue or timeout if Queue is full.
  650. /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
  651. /// \param[in] msg_ptr pointer to buffer with message to put into a queue.
  652. /// \param[in] msg_prio message priority.
  653. /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  654. /// \return status code that indicates the execution status of the function.
  655. osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout);
  656. /// Get a Message from a Queue or timeout if Queue is empty.
  657. /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
  658. /// \param[out] msg_ptr pointer to buffer for message to get from a queue.
  659. /// \param[out] msg_prio pointer to buffer for message priority or NULL.
  660. /// \param[in] timeout \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  661. /// \return status code that indicates the execution status of the function.
  662. osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout);
  663. /// Get maximum number of messages in a Message Queue.
  664. /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
  665. /// \return maximum number of messages.
  666. uint32_t osMessageQueueGetCapacity (osMessageQueueId_t mq_id);
  667. /// Get maximum message size in a Message Queue.
  668. /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
  669. /// \return maximum message size in bytes.
  670. uint32_t osMessageQueueGetMsgSize (osMessageQueueId_t mq_id);
  671. /// Get number of queued messages in a Message Queue.
  672. /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
  673. /// \return number of queued messages.
  674. uint32_t osMessageQueueGetCount (osMessageQueueId_t mq_id);
  675. /// Get number of available slots for messages in a Message Queue.
  676. /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
  677. /// \return number of available slots for messages.
  678. uint32_t osMessageQueueGetSpace (osMessageQueueId_t mq_id);
  679. /// Reset a Message Queue to initial empty state.
  680. /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
  681. /// \return status code that indicates the execution status of the function.
  682. osStatus_t osMessageQueueReset (osMessageQueueId_t mq_id);
  683. /// Delete a Message Queue object.
  684. /// \param[in] mq_id message queue ID obtained by \ref osMessageQueueNew.
  685. /// \return status code that indicates the execution status of the function.
  686. osStatus_t osMessageQueueDelete (osMessageQueueId_t mq_id);
  687. // ==== Handler Functions ====
  688. /// Handler for expired thread watchdogs.
  689. /// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  690. /// \return new watchdog reload value or 0 to stop the watchdog.
  691. uint32_t osWatchdogAlarm_Handler (osThreadId_t thread_id);
  692. // ==== Zone Management Function ====
  693. /// Setup MPU protected zone (called when zone changes).
  694. /// \param[in] zone zone number.
  695. void osZoneSetup_Callback (uint32_t zone);
  696. // ==== Exception Faults ====
  697. /// Resume normal operation when exiting exception faults
  698. void osFaultResume (void);
  699. #ifdef __cplusplus
  700. }
  701. #endif
  702. #endif // CMSIS_OS2_H_