rtx_os.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. * Project: CMSIS-RTOS RTX
  21. * Title: RTX OS definitions
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #ifndef RTX_OS_H_
  26. #define RTX_OS_H_
  27. #include <stdint.h>
  28. #include <stddef.h>
  29. #include "cmsis_os2.h"
  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #endif
  34. /// Kernel Information
  35. #define osRtxVersionAPI 20010002 ///< API version (2.1.2)
  36. #define osRtxVersionKernel 50030000 ///< Kernel version (5.3.0)
  37. #define osRtxKernelId "RTX V5.3.0" ///< Kernel identification string
  38. // ==== Common definitions ====
  39. /// Object Identifier definitions
  40. #define osRtxIdInvalid 0x00U
  41. #define osRtxIdThread 0x01U
  42. #define osRtxIdTimer 0x02U
  43. #define osRtxIdEventFlags 0x03U
  44. #define osRtxIdMutex 0x04U
  45. #define osRtxIdSemaphore 0x05U
  46. #define osRtxIdMemoryPool 0x06U
  47. #define osRtxIdMessage 0x07U
  48. #define osRtxIdMessageQueue 0x08U
  49. /// Object State definitions (except for Threads and Timers)
  50. #define osRtxObjectInactive 0x00U
  51. #define osRtxObjectActive 0x01U
  52. /// Object Flags definitions
  53. #define osRtxFlagSystemObject 0x01U
  54. #define osRtxFlagSystemMemory 0x02U
  55. // ==== Kernel definitions ====
  56. /// Kernel State definitions
  57. #define osRtxKernelInactive ((uint8_t)osKernelInactive)
  58. #define osRtxKernelReady ((uint8_t)osKernelReady)
  59. #define osRtxKernelRunning ((uint8_t)osKernelRunning)
  60. #define osRtxKernelLocked ((uint8_t)osKernelLocked)
  61. #define osRtxKernelSuspended ((uint8_t)osKernelSuspended)
  62. // ==== Thread definitions ====
  63. /// Thread State definitions (extending osThreadState)
  64. #define osRtxThreadStateMask 0x0FU
  65. #define osRtxThreadInactive ((uint8_t)osThreadInactive)
  66. #define osRtxThreadReady ((uint8_t)osThreadReady)
  67. #define osRtxThreadRunning ((uint8_t)osThreadRunning)
  68. #define osRtxThreadBlocked ((uint8_t)osThreadBlocked)
  69. #define osRtxThreadTerminated ((uint8_t)osThreadTerminated)
  70. #define osRtxThreadWaitingDelay ((uint8_t)(osRtxThreadBlocked | 0x10U))
  71. #define osRtxThreadWaitingJoin ((uint8_t)(osRtxThreadBlocked | 0x20U))
  72. #define osRtxThreadWaitingThreadFlags ((uint8_t)(osRtxThreadBlocked | 0x30U))
  73. #define osRtxThreadWaitingEventFlags ((uint8_t)(osRtxThreadBlocked | 0x40U))
  74. #define osRtxThreadWaitingMutex ((uint8_t)(osRtxThreadBlocked | 0x50U))
  75. #define osRtxThreadWaitingSemaphore ((uint8_t)(osRtxThreadBlocked | 0x60U))
  76. #define osRtxThreadWaitingMemoryPool ((uint8_t)(osRtxThreadBlocked | 0x70U))
  77. #define osRtxThreadWaitingMessageGet ((uint8_t)(osRtxThreadBlocked | 0x80U))
  78. #define osRtxThreadWaitingMessagePut ((uint8_t)(osRtxThreadBlocked | 0x90U))
  79. /// Thread Flags definitions
  80. #define osRtxThreadFlagDefStack 0x10U ///< Default Stack flag
  81. /// Stack Marker definitions
  82. #define osRtxStackMagicWord 0xE25A2EA5U ///< Stack Magic Word (Stack Base)
  83. #define osRtxStackFillPattern 0xCCCCCCCCU ///< Stack Fill Pattern
  84. /// Thread Control Block
  85. typedef struct osRtxThread_s {
  86. uint8_t id; ///< Object Identifier
  87. uint8_t state; ///< Object State
  88. uint8_t flags; ///< Object Flags
  89. uint8_t attr; ///< Object Attributes
  90. const char *name; ///< Object Name
  91. struct osRtxThread_s *thread_next; ///< Link pointer to next Thread in Object list
  92. struct osRtxThread_s *thread_prev; ///< Link pointer to previous Thread in Object list
  93. struct osRtxThread_s *delay_next; ///< Link pointer to next Thread in Delay list
  94. struct osRtxThread_s *delay_prev; ///< Link pointer to previous Thread in Delay list
  95. struct osRtxThread_s *thread_join; ///< Thread waiting to Join
  96. uint32_t delay; ///< Delay Time
  97. int8_t priority; ///< Thread Priority
  98. int8_t priority_base; ///< Base Priority
  99. uint8_t stack_frame; ///< Stack Frame (EXC_RETURN[7..0])
  100. uint8_t flags_options; ///< Thread/Event Flags Options
  101. uint32_t wait_flags; ///< Waiting Thread/Event Flags
  102. uint32_t thread_flags; ///< Thread Flags
  103. struct osRtxMutex_s *mutex_list; ///< Link pointer to list of owned Mutexes
  104. void *stack_mem; ///< Stack Memory
  105. uint32_t stack_size; ///< Stack Size
  106. uint32_t sp; ///< Current Stack Pointer
  107. uint32_t thread_addr; ///< Thread entry address
  108. uint32_t tz_memory; ///< TrustZone Memory Identifier
  109. } osRtxThread_t;
  110. // ==== Timer definitions ====
  111. /// Timer State definitions
  112. #define osRtxTimerInactive 0x00U ///< Timer Inactive
  113. #define osRtxTimerStopped 0x01U ///< Timer Stopped
  114. #define osRtxTimerRunning 0x02U ///< Timer Running
  115. /// Timer Type definitions
  116. #define osRtxTimerPeriodic ((uint8_t)osTimerPeriodic)
  117. /// Timer Function Information
  118. typedef struct {
  119. osTimerFunc_t func; ///< Function Pointer
  120. void *arg; ///< Function Argument
  121. } osRtxTimerFinfo_t;
  122. /// Timer Control Block
  123. typedef struct osRtxTimer_s {
  124. uint8_t id; ///< Object Identifier
  125. uint8_t state; ///< Object State
  126. uint8_t flags; ///< Object Flags
  127. uint8_t type; ///< Timer Type (Periodic/One-shot)
  128. const char *name; ///< Object Name
  129. struct osRtxTimer_s *prev; ///< Pointer to previous active Timer
  130. struct osRtxTimer_s *next; ///< Pointer to next active Timer
  131. uint32_t tick; ///< Timer current Tick
  132. uint32_t load; ///< Timer Load value
  133. osRtxTimerFinfo_t finfo; ///< Timer Function Info
  134. } osRtxTimer_t;
  135. // ==== Event Flags definitions ====
  136. /// Event Flags Control Block
  137. typedef struct {
  138. uint8_t id; ///< Object Identifier
  139. uint8_t state; ///< Object State
  140. uint8_t flags; ///< Object Flags
  141. uint8_t reserved;
  142. const char *name; ///< Object Name
  143. osRtxThread_t *thread_list; ///< Waiting Threads List
  144. uint32_t event_flags; ///< Event Flags
  145. } osRtxEventFlags_t;
  146. // ==== Mutex definitions ====
  147. /// Mutex Control Block
  148. typedef struct osRtxMutex_s {
  149. uint8_t id; ///< Object Identifier
  150. uint8_t state; ///< Object State
  151. uint8_t flags; ///< Object Flags
  152. uint8_t attr; ///< Object Attributes
  153. const char *name; ///< Object Name
  154. osRtxThread_t *thread_list; ///< Waiting Threads List
  155. osRtxThread_t *owner_thread; ///< Owner Thread
  156. struct osRtxMutex_s *owner_prev; ///< Pointer to previous owned Mutex
  157. struct osRtxMutex_s *owner_next; ///< Pointer to next owned Mutex
  158. uint8_t lock; ///< Lock counter
  159. uint8_t padding[3];
  160. } osRtxMutex_t;
  161. // ==== Semaphore definitions ====
  162. /// Semaphore Control Block
  163. typedef struct {
  164. uint8_t id; ///< Object Identifier
  165. uint8_t state; ///< Object State
  166. uint8_t flags; ///< Object Flags
  167. uint8_t reserved;
  168. const char *name; ///< Object Name
  169. osRtxThread_t *thread_list; ///< Waiting Threads List
  170. uint16_t tokens; ///< Current number of tokens
  171. uint16_t max_tokens; ///< Maximum number of tokens
  172. } osRtxSemaphore_t;
  173. // ==== Memory Pool definitions ====
  174. /// Memory Pool Information
  175. typedef struct {
  176. uint32_t max_blocks; ///< Maximum number of Blocks
  177. uint32_t used_blocks; ///< Number of used Blocks
  178. uint32_t block_size; ///< Block Size
  179. void *block_base; ///< Block Memory Base Address
  180. void *block_lim; ///< Block Memory Limit Address
  181. void *block_free; ///< First free Block Address
  182. } osRtxMpInfo_t;
  183. /// Memory Pool Control Block
  184. typedef struct {
  185. uint8_t id; ///< Object Identifier
  186. uint8_t state; ///< Object State
  187. uint8_t flags; ///< Object Flags
  188. uint8_t reserved;
  189. const char *name; ///< Object Name
  190. osRtxThread_t *thread_list; ///< Waiting Threads List
  191. osRtxMpInfo_t mp_info; ///< Memory Pool Info
  192. } osRtxMemoryPool_t;
  193. // ==== Message Queue definitions ====
  194. /// Message Control Block
  195. typedef struct osRtxMessage_s {
  196. uint8_t id; ///< Object Identifier
  197. uint8_t state; ///< Object State
  198. uint8_t flags; ///< Object Flags
  199. uint8_t priority; ///< Message Priority
  200. struct osRtxMessage_s *prev; ///< Pointer to previous Message
  201. struct osRtxMessage_s *next; ///< Pointer to next Message
  202. } osRtxMessage_t;
  203. /// Message Queue Control Block
  204. typedef struct {
  205. uint8_t id; ///< Object Identifier
  206. uint8_t state; ///< Object State
  207. uint8_t flags; ///< Object Flags
  208. uint8_t reserved;
  209. const char *name; ///< Object Name
  210. osRtxThread_t *thread_list; ///< Waiting Threads List
  211. osRtxMpInfo_t mp_info; ///< Memory Pool Info
  212. uint32_t msg_size; ///< Message Size
  213. uint32_t msg_count; ///< Number of queued Messages
  214. osRtxMessage_t *msg_first; ///< Pointer to first Message
  215. osRtxMessage_t *msg_last; ///< Pointer to last Message
  216. } osRtxMessageQueue_t;
  217. // ==== Generic Object definitions ====
  218. /// Generic Object Control Block
  219. typedef struct {
  220. uint8_t id; ///< Object Identifier
  221. uint8_t state; ///< Object State
  222. uint8_t flags; ///< Object Flags
  223. uint8_t reserved;
  224. const char *name; ///< Object Name
  225. osRtxThread_t *thread_list; ///< Threads List
  226. } osRtxObject_t;
  227. // ==== OS Runtime Information definitions ====
  228. /// OS Runtime Information structure
  229. typedef struct {
  230. const char *os_id; ///< OS Identification
  231. uint32_t version; ///< OS Version
  232. struct { ///< Kernel Info
  233. uint8_t state; ///< State
  234. volatile uint8_t blocked; ///< Blocked
  235. uint8_t pendSV; ///< Pending SV
  236. uint8_t reserved;
  237. uint32_t tick; ///< Tick counter
  238. } kernel;
  239. int32_t tick_irqn; ///< Tick Timer IRQ Number
  240. struct { ///< Thread Info
  241. struct { ///< Thread Run Info
  242. osRtxThread_t *curr; ///< Current running Thread
  243. osRtxThread_t *next; ///< Next Thread to Run
  244. } run;
  245. osRtxObject_t ready; ///< Ready List Object
  246. osRtxThread_t *idle; ///< Idle Thread
  247. osRtxThread_t *delay_list; ///< Delay List
  248. osRtxThread_t *wait_list; ///< Wait List (no Timeout)
  249. osRtxThread_t *terminate_list; ///< Terminate Thread List
  250. struct { ///< Thread Round Robin Info
  251. osRtxThread_t *thread; ///< Round Robin Thread
  252. uint32_t tick; ///< Round Robin Time Tick
  253. uint32_t timeout; ///< Round Robin Timeout
  254. } robin;
  255. } thread;
  256. struct { ///< Timer Info
  257. osRtxTimer_t *list; ///< Active Timer List
  258. osRtxThread_t *thread; ///< Timer Thread
  259. osRtxMessageQueue_t *mq; ///< Timer Message Queue
  260. void (*tick)(void); ///< Timer Tick Function
  261. } timer;
  262. struct { ///< ISR Post Processing Queue
  263. uint16_t max; ///< Maximum Items
  264. uint16_t cnt; ///< Item Count
  265. uint16_t in; ///< Incoming Item Index
  266. uint16_t out; ///< Outgoing Item Index
  267. void **data; ///< Queue Data
  268. } isr_queue;
  269. struct { ///< ISR Post Processing functions
  270. void (*thread)(osRtxThread_t*); ///< Thread Post Processing function
  271. void (*event_flags)(osRtxEventFlags_t*); ///< Event Flags Post Processing function
  272. void (*semaphore)(osRtxSemaphore_t*); ///< Semaphore Post Processing function
  273. void (*memory_pool)(osRtxMemoryPool_t*); ///< Memory Pool Post Processing function
  274. void (*message)(osRtxMessage_t*); ///< Message Post Processing function
  275. } post_process;
  276. struct { ///< Memory Pools (Variable Block Size)
  277. void *stack; ///< Stack Memory
  278. void *mp_data; ///< Memory Pool Data Memory
  279. void *mq_data; ///< Message Queue Data Memory
  280. void *common; ///< Common Memory
  281. } mem;
  282. struct { ///< Memory Pools (Fixed Block Size)
  283. osRtxMpInfo_t *stack; ///< Stack for Threads
  284. osRtxMpInfo_t *thread; ///< Thread Control Blocks
  285. osRtxMpInfo_t *timer; ///< Timer Control Blocks
  286. osRtxMpInfo_t *event_flags; ///< Event Flags Control Blocks
  287. osRtxMpInfo_t *mutex; ///< Mutex Control Blocks
  288. osRtxMpInfo_t *semaphore; ///< Semaphore Control Blocks
  289. osRtxMpInfo_t *memory_pool; ///< Memory Pool Control Blocks
  290. osRtxMpInfo_t *message_queue; ///< Message Queue Control Blocks
  291. } mpi;
  292. } osRtxInfo_t;
  293. extern osRtxInfo_t osRtxInfo; ///< OS Runtime Information
  294. /// OS Runtime Object Memory Usage structure
  295. typedef struct {
  296. uint32_t cnt_alloc; ///< Counter for alloc
  297. uint32_t cnt_free; ///< Counter for free
  298. uint32_t max_used; ///< Maximum used
  299. } osRtxObjectMemUsage_t;
  300. /// OS Runtime Object Memory Usage variables
  301. extern osRtxObjectMemUsage_t osRtxThreadMemUsage;
  302. extern osRtxObjectMemUsage_t osRtxTimerMemUsage;
  303. extern osRtxObjectMemUsage_t osRtxEventFlagsMemUsage;
  304. extern osRtxObjectMemUsage_t osRtxMutexMemUsage;
  305. extern osRtxObjectMemUsage_t osRtxSemaphoreMemUsage;
  306. extern osRtxObjectMemUsage_t osRtxMemoryPoolMemUsage;
  307. extern osRtxObjectMemUsage_t osRtxMessageQueueMemUsage;
  308. // ==== OS API definitions ====
  309. /// Object Limits definitions
  310. #define osRtxThreadFlagsLimit 31U ///< number of Thread Flags available per thread
  311. #define osRtxEventFlagsLimit 31U ///< number of Event Flags available per object
  312. #define osRtxMutexLockLimit 255U ///< maximum number of recursive mutex locks
  313. #define osRtxSemaphoreTokenLimit 65535U ///< maximum number of tokens per semaphore
  314. /// Control Block sizes
  315. #define osRtxThreadCbSize sizeof(osRtxThread_t)
  316. #define osRtxTimerCbSize sizeof(osRtxTimer_t)
  317. #define osRtxEventFlagsCbSize sizeof(osRtxEventFlags_t)
  318. #define osRtxMutexCbSize sizeof(osRtxMutex_t)
  319. #define osRtxSemaphoreCbSize sizeof(osRtxSemaphore_t)
  320. #define osRtxMemoryPoolCbSize sizeof(osRtxMemoryPool_t)
  321. #define osRtxMessageQueueCbSize sizeof(osRtxMessageQueue_t)
  322. /// Memory size in bytes for Memory Pool storage.
  323. /// \param block_count maximum number of memory blocks in memory pool.
  324. /// \param block_size memory block size in bytes.
  325. #define osRtxMemoryPoolMemSize(block_count, block_size) \
  326. (4*(block_count)*(((block_size)+3)/4))
  327. /// Memory size in bytes for Message Queue storage.
  328. /// \param msg_count maximum number of messages in queue.
  329. /// \param msg_size maximum message size in bytes.
  330. #define osRtxMessageQueueMemSize(msg_count, msg_size) \
  331. (4*(msg_count)*(3+(((msg_size)+3)/4)))
  332. // ==== OS External Functions ====
  333. /// OS Error Codes
  334. #define osRtxErrorStackUnderflow 1U ///< Stack overflow, i.e. stack pointer below its lower memory limit for descending stacks.
  335. #define osRtxErrorISRQueueOverflow 2U ///< ISR Queue overflow detected when inserting object.
  336. #define osRtxErrorTimerQueueOverflow 3U ///< User Timer Callback Queue overflow detected for timer.
  337. #define osRtxErrorClibSpace 4U ///< Standard C/C++ library libspace not available: increase \c OS_THREAD_LIBSPACE_NUM.
  338. #define osRtxErrorClibMutex 5U ///< Standard C/C++ library mutex initialization failed.
  339. /// OS Error Callback function
  340. extern uint32_t osRtxErrorNotify (uint32_t code, void *object_id);
  341. /// OS Idle Thread
  342. extern void osRtxIdleThread (void *argument);
  343. /// OS Exception handlers
  344. extern void SVC_Handler (void);
  345. extern void PendSV_Handler (void);
  346. extern void SysTick_Handler (void);
  347. // ==== OS External Configuration ====
  348. /// OS Configuration flags
  349. #define osRtxConfigPrivilegedMode (1UL<<0) ///< Threads in Privileged mode
  350. #define osRtxConfigStackCheck (1UL<<1) ///< Stack overrun checking
  351. #define osRtxConfigStackWatermark (1UL<<2) ///< Stack usage Watermark
  352. /// OS Configuration structure
  353. typedef struct {
  354. uint32_t flags; ///< OS Configuration Flags
  355. uint32_t tick_freq; ///< Kernel Tick Frequency
  356. uint32_t robin_timeout; ///< Round Robin Timeout Tick
  357. struct { ///< ISR Post Processing Queue
  358. void **data; ///< Queue Data
  359. uint16_t max; ///< Maximum Items
  360. uint16_t padding;
  361. } isr_queue;
  362. struct { ///< Memory Pools (Variable Block Size)
  363. void *stack_addr; ///< Stack Memory Address
  364. uint32_t stack_size; ///< Stack Memory Size
  365. void *mp_data_addr; ///< Memory Pool Memory Address
  366. uint32_t mp_data_size; ///< Memory Pool Memory Size
  367. void *mq_data_addr; ///< Message Queue Data Memory Address
  368. uint32_t mq_data_size; ///< Message Queue Data Memory Size
  369. void *common_addr; ///< Common Memory Address
  370. uint32_t common_size; ///< Common Memory Size
  371. } mem;
  372. struct { ///< Memory Pools (Fixed Block Size)
  373. osRtxMpInfo_t *stack; ///< Stack for Threads
  374. osRtxMpInfo_t *thread; ///< Thread Control Blocks
  375. osRtxMpInfo_t *timer; ///< Timer Control Blocks
  376. osRtxMpInfo_t *event_flags; ///< Event Flags Control Blocks
  377. osRtxMpInfo_t *mutex; ///< Mutex Control Blocks
  378. osRtxMpInfo_t *semaphore; ///< Semaphore Control Blocks
  379. osRtxMpInfo_t *memory_pool; ///< Memory Pool Control Blocks
  380. osRtxMpInfo_t *message_queue; ///< Message Queue Control Blocks
  381. } mpi;
  382. uint32_t thread_stack_size; ///< Default Thread Stack Size
  383. const
  384. osThreadAttr_t *idle_thread_attr; ///< Idle Thread Attributes
  385. const
  386. osThreadAttr_t *timer_thread_attr; ///< Timer Thread Attributes
  387. const
  388. osMessageQueueAttr_t *timer_mq_attr; ///< Timer Message Queue Attributes
  389. uint32_t timer_mq_mcnt; ///< Timer Message Queue maximum Messages
  390. } osRtxConfig_t;
  391. extern const osRtxConfig_t osRtxConfig; ///< OS Configuration
  392. #ifdef __cplusplus
  393. }
  394. #endif
  395. #endif // RTX_OS_H_