rtx_os.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright (c) 2013-2016 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. * http://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. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #endif
  33. /// Kernel Information
  34. #define os_CMSIS_API 20000000 ///< API version (2.0.0)
  35. #define os_CMSIS_RTX 50000000 ///< Kernel version (5.0.0)
  36. #define os_KernelId "RTX V5.0.0" ///< Kernel identification string
  37. // ==== Common definitions ====
  38. /// Object Identifier definitions
  39. #define os_IdInvalid 0x00U
  40. #define os_IdThread 0x01U
  41. #define os_IdTimer 0x02U
  42. #define os_IdEventFlags 0x03U
  43. #define os_IdMutex 0x04U
  44. #define os_IdSemaphore 0x05U
  45. #define os_IdMemoryPool 0x06U
  46. #define os_IdMessage 0x07U
  47. #define os_IdMessageQueue 0x08U
  48. /// Object State definitions (except for Threads and Timers)
  49. #define os_ObjectInactive 0x00U
  50. #define os_ObjectActive 0x01U
  51. /// Object Flags definitions
  52. #define os_FlagSystemObject 0x01U
  53. #define os_FlagSystemMemory 0x02U
  54. // ==== Kernel definitions ====
  55. /// Kernel State definitions
  56. #define os_KernelInactive ((uint8_t)osKernelInactive)
  57. #define os_KernelReady ((uint8_t)osKernelReady)
  58. #define os_KernelRunning ((uint8_t)osKernelRunning)
  59. #define os_KernelLocked ((uint8_t)osKernelLocked)
  60. #define os_KernelSuspended ((uint8_t)osKernelSuspended)
  61. // ==== Thread definitions ====
  62. /// Thread State definitions (extending osThreadState)
  63. #define os_ThreadStateMask 0x0FU
  64. #define os_ThreadInactive ((uint8_t)osThreadInactive)
  65. #define os_ThreadReady ((uint8_t)osThreadReady)
  66. #define os_ThreadRunning ((uint8_t)osThreadRunning)
  67. #define os_ThreadWaiting ((uint8_t)osThreadWaiting)
  68. #define os_ThreadSuspended ((uint8_t)osThreadSuspended)
  69. #define os_ThreadTerminated ((uint8_t)osThreadTerminated)
  70. #define os_ThreadWaitingDelay (os_ThreadWaiting | 0x10U)
  71. #define os_ThreadWaitingJoin (os_ThreadWaiting | 0x20U)
  72. #define os_ThreadWaitingThreadFlags (os_ThreadWaiting | 0x30U)
  73. #define os_ThreadWaitingEventFlags (os_ThreadWaiting | 0x40U)
  74. #define os_ThreadWaitingMutex (os_ThreadWaiting | 0x50U)
  75. #define os_ThreadWaitingSemaphore (os_ThreadWaiting | 0x60U)
  76. #define os_ThreadWaitingMemoryPool (os_ThreadWaiting | 0x70U)
  77. #define os_ThreadWaitingMessageGet (os_ThreadWaiting | 0x80U)
  78. #define os_ThreadWaitingMessagePut (os_ThreadWaiting | 0x90U)
  79. /// Thread Flags definitions
  80. #define os_ThreadFlagDefStack 0x10U ///< Default Stack flag
  81. #define os_ThreadFlagExitPtr 0x20U ///< Exit Pointer flag
  82. /// Stack Frame definitions
  83. #define os_StackFrameBasic 0x00U ///< Stack Frame: Basic (CPU)
  84. #define os_StackFrameExtended 0x01U ///< Stack Frame: Extended (CPU and FPU)
  85. /// Stack Marker definitions
  86. #define os_StackMagicWord 0xE25A2EA5U ///< Stack Magic Word (Stack Base)
  87. #define os_StackFillPattern 0xCCCCCCCCU ///< Stack Fill Pattern
  88. /// Thread Control Block
  89. typedef struct os_thread_s {
  90. uint8_t id; ///< Object Identifier
  91. uint8_t state; ///< Object State
  92. uint8_t flags; ///< Object Flags
  93. uint8_t attr; ///< Object Attributes
  94. const char *name; ///< Object Name
  95. struct os_thread_s *thread_next; ///< Link pointer to next Thread in Object list
  96. struct os_thread_s *thread_prev; ///< Link pointer to previous Thread in Object list
  97. struct os_thread_s *delay_next; ///< Link pointer to next Thread in Delay list
  98. struct os_thread_s *delay_prev; ///< Link pointer to previous Thread in Delay list
  99. struct os_thread_s *thread_join; ///< Thread waiting to Join
  100. uint32_t delay; ///< Delay Time
  101. int8_t priority; ///< Thread Priority
  102. int8_t priority_base; ///< Base Priority
  103. uint8_t stack_frame; ///< Stack Frame
  104. uint8_t flags_options; ///< Thread/Event Flags Options
  105. int32_t wait_flags; ///< Waiting Thread/Event Flags
  106. int32_t thread_flags; ///< Thread Flags
  107. struct os_mutex_s *mutex_list; ///< Link pointer to list of owned Mutexes
  108. void *stack_mem; ///< Stack Memory
  109. uint32_t stack_size; ///< Stack Size
  110. uint32_t sp; ///< Current Stack Pointer
  111. } os_thread_t;
  112. // ==== Timer definitions ====
  113. /// Timer State definitions
  114. #define os_TimerInactive 0x00U ///< Timer Inactive
  115. #define os_TimerStopped 0x01U ///< Timer Stopped
  116. #define os_TimerRunning 0x02U ///< Timer Running
  117. /// Timer Type definitions
  118. #define os_TimerPeriodic ((uint8_t)osTimerPeriodic)
  119. /// Timer Function Information
  120. typedef struct os_timer_finfo_s {
  121. void *fp; ///< Function Pointer
  122. void *arg; ///< Function Argument
  123. } os_timer_finfo_t;
  124. /// Timer Control Block
  125. typedef struct os_timer_s {
  126. uint8_t id; ///< Object Identifier
  127. uint8_t state; ///< Object State
  128. uint8_t flags; ///< Object Flags
  129. uint8_t type; ///< Timer Type (Periodic/One-shot)
  130. const char *name; ///< Object Name
  131. struct os_timer_s *prev; ///< Pointer to previous active Timer
  132. struct os_timer_s *next; ///< Pointer to next active Timer
  133. uint32_t tick; ///< Timer current Tick
  134. uint32_t load; ///< Timer Load value
  135. os_timer_finfo_t finfo; ///< Timer Function Info
  136. } os_timer_t;
  137. // ==== Event Flags definitions ====
  138. /// Event Flags Control Block
  139. typedef struct os_event_flags_s {
  140. uint8_t id; ///< Object Identifier
  141. uint8_t state; ///< Object State
  142. uint8_t flags; ///< Object Flags
  143. uint8_t reserved;
  144. const char *name; ///< Object Name
  145. os_thread_t *thread_list; ///< Waiting Threads List
  146. int32_t event_flags; ///< Event Flags
  147. } os_event_flags_t;
  148. // ==== Mutex definitions ====
  149. /// Mutex Control Block
  150. typedef struct os_mutex_s {
  151. uint8_t id; ///< Object Identifier
  152. uint8_t state; ///< Object State
  153. uint8_t flags; ///< Object Flags
  154. uint8_t attr; ///< Object Attributes
  155. const char *name; ///< Object Name
  156. os_thread_t *thread_list; ///< Waiting Threads List
  157. os_thread_t *owner_thread; ///< Owner Thread
  158. struct os_mutex_s *owner_prev; ///< Pointer to previous owned Mutex
  159. struct os_mutex_s *owner_next; ///< Pointer to next owned Mutex
  160. uint8_t lock; ///< Lock counter
  161. uint8_t padding[3];
  162. } os_mutex_t;
  163. // ==== Semaphore definitions ====
  164. /// Semaphore Control Block
  165. typedef struct os_semaphore_s {
  166. uint8_t id; ///< Object Identifier
  167. uint8_t state; ///< Object State
  168. uint8_t flags; ///< Object Flags
  169. uint8_t reserved;
  170. const char *name; ///< Object Name
  171. os_thread_t *thread_list; ///< Waiting Threads List
  172. uint16_t tokens; ///< Current number of tokens
  173. uint16_t max_tokens; ///< Maximum number of tokens
  174. } os_semaphore_t;
  175. // ==== Memory Pool definitions ====
  176. /// Memory Pool Information
  177. typedef struct os_mp_info_s {
  178. uint32_t max_blocks; ///< Maximum number of Blocks
  179. uint32_t used_blocks; ///< Number of used Blocks
  180. uint32_t block_size; ///< Block Size
  181. void *block_base; ///< Block Memory Base Address
  182. void *block_lim; ///< Block Memory Limit Address
  183. void *block_free; ///< First free Block Address
  184. } os_mp_info_t;
  185. /// Memory Pool Control Block
  186. typedef struct os_memory_pool_s {
  187. uint8_t id; ///< Object Identifier
  188. uint8_t state; ///< Object State
  189. uint8_t flags; ///< Object Flags
  190. uint8_t reserved;
  191. const char *name; ///< Object Name
  192. os_thread_t *thread_list; ///< Waiting Threads List
  193. os_mp_info_t mp_info; ///< Memory Pool Info
  194. } os_memory_pool_t;
  195. // ==== Message Queue definitions ====
  196. /// Message Control Block
  197. typedef struct os_message_s {
  198. uint8_t id; ///< Object Identifier
  199. uint8_t state; ///< Object State
  200. uint8_t flags; ///< Object Flags
  201. uint8_t priority; ///< Message Priority
  202. struct os_message_s *prev; ///< Pointer to previous Message
  203. struct os_message_s *next; ///< Pointer to next Message
  204. } os_message_t;
  205. /// Message Queue Control Block
  206. typedef struct os_message_queue_s {
  207. uint8_t id; ///< Object Identifier
  208. uint8_t state; ///< Object State
  209. uint8_t flags; ///< Object Flags
  210. uint8_t reserved;
  211. const char *name; ///< Object Name
  212. os_thread_t *thread_list; ///< Waiting Threads List
  213. os_mp_info_t mp_info; ///< Memory Pool Info
  214. uint32_t msg_size; ///< Message Size
  215. uint32_t msg_count; ///< Number of queued Messages
  216. os_message_t *msg_first; ///< Pointer to first Message
  217. os_message_t *msg_last; ///< Pointer to last Message
  218. } os_message_queue_t;
  219. // ==== Generic Object definitions ====
  220. /// Generic Object Control Block
  221. typedef struct os_object_s {
  222. uint8_t id; ///< Object Identifier
  223. uint8_t state; ///< Object State
  224. uint8_t flags; ///< Object Flags
  225. uint8_t reserved;
  226. const char *name; ///< Object Name
  227. os_thread_t *thread_list; ///< Threads List
  228. } os_object_t;
  229. // ==== OS Runtime Information definitions ====
  230. /// OS Runtime Information structure
  231. typedef struct {
  232. const char *os_id; ///< OS Identification
  233. uint32_t version; ///< OS Version
  234. struct { ///< Kernel Info
  235. uint8_t state; ///< State
  236. volatile uint8_t blocked; ///< Blocked
  237. uint8_t pendISR; ///< Pending ISR (SV and SysTick)
  238. uint8_t pendSV; ///< Pending SV
  239. uint32_t usec_ticks; ///< 1 microsec ticks * 2^16
  240. uint64_t time; ///< Time in millisec
  241. } kernel;
  242. int32_t tick_irqn; ///< Tick Timer IRQ Number
  243. struct { ///< Thread Info
  244. struct { ///< Thread Run Info
  245. os_thread_t *curr; ///< Current running Thread
  246. os_thread_t *next; ///< Next Thread to Run
  247. } run;
  248. volatile os_object_t ready; ///< Ready List Object
  249. os_thread_t *idle; ///< Idle Thread
  250. os_thread_t *delay_list; ///< Delay List
  251. os_thread_t *suspended_list; ///< Suspended Thread List
  252. os_thread_t *terminated_list; ///< Terminated Thread List
  253. struct { ///< Thread Round Robin Info
  254. os_thread_t *thread; ///< Round Robin Thread
  255. uint32_t tick; ///< Round Robin Time Tick
  256. uint32_t timeout; ///< Round Robin Timeout
  257. } robin;
  258. } thread;
  259. struct { ///< Timer Info
  260. os_timer_t *list; ///< Active Timer List
  261. os_thread_t *thread; ///< Timer Thread
  262. os_message_queue_t *mq; ///< Timer Message Queue
  263. } timer;
  264. struct { ///< ISR Post Processing Queue
  265. uint16_t max; ///< Maximum Items
  266. uint16_t cnt; ///< Item Count
  267. uint16_t in; ///< Incoming Item Index
  268. uint16_t out; ///< Outgoing Item Index
  269. void **data; ///< Queue Data
  270. } isr_queue;
  271. struct { ///< ISR Post Processing functions
  272. void (*thread)(os_thread_t*); ///< Thread Post Processing function
  273. void (*event_flags)(os_event_flags_t*); ///< Event Flags Post Processing function
  274. void (*semaphore)(os_semaphore_t*); ///< Semaphore Post Processing function
  275. void (*memory_pool)(os_memory_pool_t*); ///< Memory Pool Post Processing function
  276. void (*message_queue)(os_message_t*); ///< MEssage Queue Post Processing function
  277. } post_process;
  278. struct { ///< Memory Pools (Variable Block Size)
  279. void *cb; ///< Control Blocks Memory
  280. void *data; ///< Data Memory
  281. void *stack; ///< Stack Memory
  282. void *common; ///< Common Memory Address
  283. } mem;
  284. struct { ///< Memory Pools (Fixed Block Size)
  285. os_mp_info_t *stack; ///< Stack for Threads
  286. os_mp_info_t *thread; ///< Thread Control Blocks
  287. os_mp_info_t *timer; ///< Timer Control Blocks
  288. os_mp_info_t *event_flags; ///< Event Flags Control Blocks
  289. os_mp_info_t *mutex; ///< Mutex Control Blocks
  290. os_mp_info_t *semaphore; ///< Semaphore Control Blocks
  291. os_mp_info_t *memory_pool; ///< Memory Pool Control Blocks
  292. os_mp_info_t *message_queue; ///< Message Queue Control Blocks
  293. } mpi;
  294. } os_info_t;
  295. extern os_info_t os_Info; ///< OS Runtime Information
  296. // ==== OS API definitions ====
  297. /// Object Limits definitions
  298. #define os_ThreadFlagsLimit 31U ///< number of Thread Flags available per thread
  299. #define os_EventFlagsLimit 31U ///< number of Event Flags available per object
  300. #define os_MutexLockLimit 255U ///< maximum number of recursive mutex locks
  301. #define os_SemaphoreTokenLimit 65535U ///< maximum number of tokens per semaphore
  302. /// Control Block sizes
  303. #define os_ThreadCbSize sizeof(os_thread_t)
  304. #define os_TimerCbSize sizeof(os_timer_t)
  305. #define os_EventFlagsCbSize sizeof(os_event_flags_t)
  306. #define os_MutexCbSize sizeof(os_mutex_t)
  307. #define os_SemaphoreCbSize sizeof(os_semaphore_t)
  308. #define os_MemoryPoolCbSize sizeof(os_memory_pool_t)
  309. #define os_MessageQueueCbSize sizeof(os_message_queue_t)
  310. /// Memory size in bytes for Memory Pool storage.
  311. /// \param block_count maximum number of memory blocks in memory pool.
  312. /// \param block_size memory block size in bytes.
  313. #define os_MemoryPoolMemSize(block_count, block_size) \
  314. (4*(block_count)*(((block_size)+3)/4))
  315. /// Memory size in bytes for Message Queue storage.
  316. /// \param msg_count maximum number of messages in queue.
  317. /// \param msg_size maximum message size in bytes.
  318. #define os_MessageQueueMemSize(msg_count, msg_size) \
  319. (4*(msg_count)*(3+(((msg_size)+3)/4)))
  320. // ==== OS External Functions ====
  321. /// OS Error Codes
  322. #define os_ErrorStackUnderflow 1U
  323. #define os_ErrorISRQueueOverflow 2U
  324. #define os_ErrorTimerQueueOverflow 3U
  325. #define os_ErrorClibSpace 4U
  326. #define os_ErrorClibMutex 5U
  327. /// OS Error Callback function
  328. extern uint32_t os_Error (uint32_t code, void *object_id);
  329. /// OS Idle Thread
  330. extern void *os_IdleThread (void *argument);
  331. /// OS Exception handlers
  332. extern void SVC_Handler (void);
  333. extern void PendSV_Handler (void);
  334. extern void SysTick_Handler (void);
  335. /// OS Tick functions (default implementation uses SysTick)
  336. /// Setup Tick Timer.
  337. /// \return tick timer IRQ number.
  338. extern int32_t os_TickSetup (void);
  339. /// Enable Tick Timer.
  340. extern void os_TickEnable (void);
  341. /// Disable Tick Timer.
  342. extern void os_TickDisable (void);
  343. /// Acknowledge Tick Timer IRQ.
  344. extern void os_TickAckIRQ (void);
  345. /// Get Tick Timer Value.
  346. /// \return tick timer value.
  347. extern uint32_t os_TickGetVal (void);
  348. /// Convert microseconds value to Tick Timer value.
  349. /// \param microsec time value in microseconds.
  350. /// \return time value normalized to timer ticks.
  351. extern uint32_t os_TickMicroSec (uint32_t microsec);
  352. // ==== OS External Configuration ====
  353. /// OS Configuration flags
  354. #define os_ConfigPrivilegedMode (1UL<<0) ///< Threads in Privileged mode
  355. #define os_ConfigStackWatermark (1UL<<1) ///< Stack usage Watermark
  356. /// OS Configuration structure
  357. typedef struct {
  358. uint32_t flags; ///< OS Configuration Flags
  359. uint32_t robin_timeout; ///< Round Robin Timeout Tick
  360. struct { ///< ISR Post Processing Queue
  361. void **data; ///< Queue Data
  362. uint16_t max; ///< Maximum Items
  363. uint16_t padding;
  364. } isr_queue;
  365. struct { ///< Memory Pools (Variable Block Size)
  366. void *cb_addr; ///< Control Blocks Memory Address
  367. uint32_t cb_size; ///< Control Blocks Memory Size
  368. void *data_addr; ///< Data Memory Address
  369. uint32_t data_size; ///< Data Memory Size
  370. void *stack_addr; ///< Stack Memory Address
  371. uint32_t stack_size; ///< Stack Memory Size
  372. void *common_addr; ///< Common Memory Address
  373. uint32_t common_size; ///< Common Memory Size
  374. } mem;
  375. struct { ///< Memory Pools (Fixed Block Size)
  376. os_mp_info_t *stack; ///< Stack for Threads
  377. os_mp_info_t *thread; ///< Thread Control Blocks
  378. os_mp_info_t *timer; ///< Timer Control Blocks
  379. os_mp_info_t *event_flags; ///< Event Flags Control Blocks
  380. os_mp_info_t *mutex; ///< Mutex Control Blocks
  381. os_mp_info_t *semaphore; ///< Semaphore Control Blocks
  382. os_mp_info_t *memory_pool; ///< Memory Pool Control Blocks
  383. os_mp_info_t *message_queue; ///< Message Queue Control Blocks
  384. } mpi;
  385. uint32_t thread_stack_size; ///< Default Thread Stack Size
  386. const
  387. struct osThreadAttr_s *idle_thread_attr; ///< Idle Thread Attributes
  388. const
  389. struct osThreadAttr_s *timer_thread_attr; ///< Timer Thread Attributes
  390. const
  391. struct osMessageQueueAttr_s *timer_mq_attr; ///< Timer Message Queue Attributes
  392. uint32_t timer_mq_mcnt; ///< Timer Message Queue maximum Messages
  393. } os_config_t;
  394. extern const os_config_t os_Config; ///< OS Configuration
  395. #ifdef __cplusplus
  396. }
  397. #endif
  398. #endif // __RTX_OS_H