rtx_os.h 21 KB

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