cmsis_os.h 38 KB

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