rtx_lib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2013-2023 Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * -----------------------------------------------------------------------------
  19. *
  20. * Project: CMSIS-RTOS RTX
  21. * Title: RTX Library definitions
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #ifndef RTX_LIB_H_
  26. #define RTX_LIB_H_
  27. #include <string.h>
  28. #include "rtx_def.h" // RTX Configuration definitions
  29. #include "rtx_core_c.h" // Cortex core definitions
  30. #ifdef RTX_TZ_CONTEXT
  31. #include "tz_context.h" // TrustZone Context API
  32. #endif
  33. #include "os_tick.h" // CMSIS OS Tick API
  34. #include "cmsis_os2.h" // CMSIS RTOS API
  35. #include "rtx_os.h" // RTX OS definitions
  36. #include "rtx_evr.h" // RTX Event Recorder definitions
  37. // ==== Library defines ====
  38. #define os_thread_t osRtxThread_t
  39. #define os_timer_t osRtxTimer_t
  40. #define os_timer_finfo_t osRtxTimerFinfo_t
  41. #define os_event_flags_t osRtxEventFlags_t
  42. #define os_mutex_t osRtxMutex_t
  43. #define os_semaphore_t osRtxSemaphore_t
  44. #define os_mp_info_t osRtxMpInfo_t
  45. #define os_memory_pool_t osRtxMemoryPool_t
  46. #define os_message_t osRtxMessage_t
  47. #define os_message_queue_t osRtxMessageQueue_t
  48. #define os_object_t osRtxObject_t
  49. // ==== Library sections ====
  50. #if defined(__CC_ARM) || \
  51. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  52. // Referenced through linker
  53. //lint -esym(528, __os_thread_cb_start__, __os_thread_cb_length__)
  54. //lint -esym(528, __os_timer_cb_start__, __os_timer_cb_length__)
  55. //lint -esym(528, __os_evflags_cb_start__, __os_evflags_cb_length__)
  56. //lint -esym(528, __os_mutex_cb_start__, __os_mutex_cb_length__)
  57. //lint -esym(528, __os_semaphore_cb_start__, __os_semaphore_cb_length__)
  58. //lint -esym(528, __os_mempool_cb_start__, __os_mempool_cb_length__)
  59. //lint -esym(528, __os_msgqueue_cb_start__, __os_msgqueue_cb_length__)
  60. // Accessed through linker
  61. //lint -esym(551, __os_thread_cb_start__, __os_thread_cb_length__)
  62. //lint -esym(551, __os_timer_cb_start__, __os_timer_cb_length__)
  63. //lint -esym(551, __os_evflags_cb_start__, __os_evflags_cb_length__)
  64. //lint -esym(551, __os_mutex_cb_start__, __os_mutex_cb_length__)
  65. //lint -esym(551, __os_semaphore_cb_start__, __os_semaphore_cb_length__)
  66. //lint -esym(551, __os_mempool_cb_start__, __os_mempool_cb_length__)
  67. //lint -esym(551, __os_msgqueue_cb_start__, __os_msgqueue_cb_length__)
  68. // Initialized through linker
  69. //lint -esym(728, __os_thread_cb_start__, __os_thread_cb_length__)
  70. //lint -esym(728, __os_timer_cb_start__, __os_timer_cb_length__)
  71. //lint -esym(728, __os_evflags_cb_start__, __os_evflags_cb_length__)
  72. //lint -esym(728, __os_mutex_cb_start__, __os_mutex_cb_length__)
  73. //lint -esym(728, __os_semaphore_cb_start__, __os_semaphore_cb_length__)
  74. //lint -esym(728, __os_mempool_cb_start__, __os_mempool_cb_length__)
  75. //lint -esym(728, __os_msgqueue_cb_start__, __os_msgqueue_cb_length__)
  76. // Global scope
  77. //lint -esym(9003, __os_thread_cb_start__, __os_thread_cb_length__)
  78. //lint -esym(9003, __os_timer_cb_start__, __os_timer_cb_length__)
  79. //lint -esym(9003, __os_evflags_cb_start__, __os_evflags_cb_length__)
  80. //lint -esym(9003, __os_mutex_cb_start__, __os_mutex_cb_length__)
  81. //lint -esym(9003, __os_semaphore_cb_start__, __os_semaphore_cb_length__)
  82. //lint -esym(9003, __os_mempool_cb_start__, __os_mempool_cb_length__)
  83. //lint -esym(9003, __os_msgqueue_cb_start__, __os_msgqueue_cb_length__)
  84. static const uint32_t __os_thread_cb_start__ __attribute__((weakref(".bss.os.thread.cb$$Base")));
  85. static const uint32_t __os_thread_cb_length__ __attribute__((weakref(".bss.os.thread.cb$$Length")));
  86. static const uint32_t __os_timer_cb_start__ __attribute__((weakref(".bss.os.timer.cb$$Base")));
  87. static const uint32_t __os_timer_cb_length__ __attribute__((weakref(".bss.os.timer.cb$$Length")));
  88. static const uint32_t __os_evflags_cb_start__ __attribute__((weakref(".bss.os.evflags.cb$$Base")));
  89. static const uint32_t __os_evflags_cb_length__ __attribute__((weakref(".bss.os.evflags.cb$$Length")));
  90. static const uint32_t __os_mutex_cb_start__ __attribute__((weakref(".bss.os.mutex.cb$$Base")));
  91. static const uint32_t __os_mutex_cb_length__ __attribute__((weakref(".bss.os.mutex.cb$$Length")));
  92. static const uint32_t __os_semaphore_cb_start__ __attribute__((weakref(".bss.os.semaphore.cb$$Base")));
  93. static const uint32_t __os_semaphore_cb_length__ __attribute__((weakref(".bss.os.semaphore.cb$$Length")));
  94. static const uint32_t __os_mempool_cb_start__ __attribute__((weakref(".bss.os.mempool.cb$$Base")));
  95. static const uint32_t __os_mempool_cb_length__ __attribute__((weakref(".bss.os.mempool.cb$$Length")));
  96. static const uint32_t __os_msgqueue_cb_start__ __attribute__((weakref(".bss.os.msgqueue.cb$$Base")));
  97. static const uint32_t __os_msgqueue_cb_length__ __attribute__((weakref(".bss.os.msgqueue.cb$$Length")));
  98. #else
  99. extern const uint32_t __os_thread_cb_start__ __attribute__((weak));
  100. extern const uint32_t __os_thread_cb_length__ __attribute__((weak));
  101. extern const uint32_t __os_timer_cb_start__ __attribute__((weak));
  102. extern const uint32_t __os_timer_cb_length__ __attribute__((weak));
  103. extern const uint32_t __os_evflags_cb_start__ __attribute__((weak));
  104. extern const uint32_t __os_evflags_cb_length__ __attribute__((weak));
  105. extern const uint32_t __os_mutex_cb_start__ __attribute__((weak));
  106. extern const uint32_t __os_mutex_cb_length__ __attribute__((weak));
  107. extern const uint32_t __os_semaphore_cb_start__ __attribute__((weak));
  108. extern const uint32_t __os_semaphore_cb_length__ __attribute__((weak));
  109. extern const uint32_t __os_mempool_cb_start__ __attribute__((weak));
  110. extern const uint32_t __os_mempool_cb_length__ __attribute__((weak));
  111. extern const uint32_t __os_msgqueue_cb_start__ __attribute__((weak));
  112. extern const uint32_t __os_msgqueue_cb_length__ __attribute__((weak));
  113. #endif
  114. // ==== Inline functions ====
  115. // Thread ID
  116. __STATIC_INLINE os_thread_t *osRtxThreadId (osThreadId_t thread_id) {
  117. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  118. return ((os_thread_t *)thread_id);
  119. }
  120. // Timer ID
  121. __STATIC_INLINE os_timer_t *osRtxTimerId (osTimerId_t timer_id) {
  122. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  123. return ((os_timer_t *)timer_id);
  124. }
  125. // Event Flags ID
  126. __STATIC_INLINE os_event_flags_t *osRtxEventFlagsId (osEventFlagsId_t ef_id) {
  127. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  128. return ((os_event_flags_t *)ef_id);
  129. }
  130. // Mutex ID
  131. __STATIC_INLINE os_mutex_t *osRtxMutexId (osMutexId_t mutex_id) {
  132. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  133. return ((os_mutex_t *)mutex_id);
  134. }
  135. // Semaphore ID
  136. __STATIC_INLINE os_semaphore_t *osRtxSemaphoreId (osSemaphoreId_t semaphore_id) {
  137. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  138. return ((os_semaphore_t *)semaphore_id);
  139. }
  140. // Memory Pool ID
  141. __STATIC_INLINE os_memory_pool_t *osRtxMemoryPoolId (osMemoryPoolId_t mp_id) {
  142. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  143. return ((os_memory_pool_t *)mp_id);
  144. }
  145. // Message Queue ID
  146. __STATIC_INLINE os_message_queue_t *osRtxMessageQueueId (osMessageQueueId_t mq_id) {
  147. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  148. return ((os_message_queue_t *)mq_id);
  149. }
  150. // Generic Object
  151. __STATIC_INLINE os_object_t *osRtxObject (void *object) {
  152. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 3]
  153. return ((os_object_t *)object);
  154. }
  155. // Thread Object
  156. __STATIC_INLINE os_thread_t *osRtxThreadObject (os_object_t *object) {
  157. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  158. return ((os_thread_t *)object);
  159. }
  160. // Timer Object
  161. __STATIC_INLINE os_timer_t *osRtxTimerObject (os_object_t *object) {
  162. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  163. return ((os_timer_t *)object);
  164. }
  165. // Event Flags Object
  166. __STATIC_INLINE os_event_flags_t *osRtxEventFlagsObject (os_object_t *object) {
  167. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  168. return ((os_event_flags_t *)object);
  169. }
  170. // Mutex Object
  171. __STATIC_INLINE os_mutex_t *osRtxMutexObject (os_object_t *object) {
  172. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  173. return ((os_mutex_t *)object);
  174. }
  175. // Semaphore Object
  176. __STATIC_INLINE os_semaphore_t *osRtxSemaphoreObject (os_object_t *object) {
  177. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  178. return ((os_semaphore_t *)object);
  179. }
  180. // Memory Pool Object
  181. __STATIC_INLINE os_memory_pool_t *osRtxMemoryPoolObject (os_object_t *object) {
  182. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  183. return ((os_memory_pool_t *)object);
  184. }
  185. // Message Queue Object
  186. __STATIC_INLINE os_message_queue_t *osRtxMessageQueueObject (os_object_t *object) {
  187. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  188. return ((os_message_queue_t *)object);
  189. }
  190. // Message Object
  191. __STATIC_INLINE os_message_t *osRtxMessageObject (os_object_t *object) {
  192. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  193. return ((os_message_t *)object);
  194. }
  195. // Kernel State
  196. __STATIC_INLINE osKernelState_t osRtxKernelState (void) {
  197. //lint -e{9030} -e{9034} "cast to enum"
  198. return ((osKernelState_t)(osRtxInfo.kernel.state));
  199. }
  200. // Thread State
  201. __STATIC_INLINE osThreadState_t osRtxThreadState (const os_thread_t *thread) {
  202. uint8_t state = thread->state & osRtxThreadStateMask;
  203. //lint -e{9030} -e{9034} "cast to enum"
  204. return ((osThreadState_t)state);
  205. }
  206. // Thread Priority
  207. __STATIC_INLINE osPriority_t osRtxThreadPriority (const os_thread_t *thread) {
  208. //lint -e{9030} -e{9034} "cast to enum"
  209. return ((osPriority_t)thread->priority);
  210. }
  211. // Kernel Get State
  212. __STATIC_INLINE uint8_t osRtxKernelGetState (void) {
  213. return osRtxInfo.kernel.state;
  214. }
  215. // Thread Get/Set Running
  216. __STATIC_INLINE os_thread_t *osRtxThreadGetRunning (void) {
  217. return osRtxInfo.thread.run.curr;
  218. }
  219. __STATIC_INLINE void osRtxThreadSetRunning (os_thread_t *thread) {
  220. osRtxInfo.thread.run.curr = thread;
  221. }
  222. // ==== Library functions ====
  223. // Kernel Library functions
  224. extern void osRtxKernelBeforeInit (void);
  225. // Thread Library functions
  226. extern void osRtxThreadListPut (os_object_t *object, os_thread_t *thread);
  227. extern os_thread_t *osRtxThreadListGet (os_object_t *object);
  228. extern void osRtxThreadListSort (os_thread_t *thread);
  229. extern void osRtxThreadListRemove (os_thread_t *thread);
  230. extern void osRtxThreadReadyPut (os_thread_t *thread);
  231. //lint -esym(759,osRtxThreadDelayRemove) "Prototype in header"
  232. //lint -esym(765,osRtxThreadDelayRemove) "Global scope"
  233. extern void osRtxThreadDelayRemove (os_thread_t *thread);
  234. extern void osRtxThreadDelayTick (void);
  235. extern uint32_t *osRtxThreadRegPtr (const os_thread_t *thread);
  236. extern void osRtxThreadSwitch (os_thread_t *thread);
  237. extern void osRtxThreadDispatch (os_thread_t *thread);
  238. extern void osRtxThreadWaitExit (os_thread_t *thread, uint32_t ret_val, bool_t dispatch);
  239. extern bool_t osRtxThreadWaitEnter (uint8_t state, uint32_t timeout);
  240. #ifdef RTX_STACK_CHECK
  241. extern bool_t osRtxThreadStackCheck (const os_thread_t *thread);
  242. #endif
  243. #ifdef RTX_THREAD_WATCHDOG
  244. //lint -esym(759,osRtxThreadWatchdogRemove) "Prototype in header"
  245. //lint -esym(765,osRtxThreadWatchdogRemove) "Global scope"
  246. extern void osRtxThreadWatchdogRemove(const os_thread_t *thread);
  247. extern void osRtxThreadWatchdogTick (void);
  248. #endif
  249. //lint -esym(759,osRtxThreadJoinWakeup) "Prototype in header"
  250. //lint -esym(765,osRtxThreadJoinWakeup) "Global scope"
  251. extern void osRtxThreadJoinWakeup (const os_thread_t *thread);
  252. //lint -esym(759,osRtxThreadDestroy) "Prototype in header"
  253. //lint -esym(765,osRtxThreadDestroy) "Global scope"
  254. extern void osRtxThreadDestroy (os_thread_t *thread);
  255. extern void osRtxThreadBeforeFree (os_thread_t *thread);
  256. extern bool_t osRtxThreadStartup (void);
  257. // Timer Library functions
  258. extern int32_t osRtxTimerSetup (void);
  259. extern void osRtxTimerThread (void *argument);
  260. #ifdef RTX_SAFETY_CLASS
  261. extern void osRtxTimerDeleteClass (uint32_t safety_class, uint32_t mode);
  262. #endif
  263. // Mutex Library functions
  264. extern void osRtxMutexOwnerRelease (os_mutex_t *mutex_list);
  265. extern void osRtxMutexOwnerRestore (const os_mutex_t *mutex, const os_thread_t *thread_wakeup);
  266. #ifdef RTX_SAFETY_CLASS
  267. extern void osRtxMutexDeleteClass (uint32_t safety_class, uint32_t mode);
  268. #endif
  269. // Semaphore Library functions
  270. #ifdef RTX_SAFETY_CLASS
  271. extern void osRtxSemaphoreDeleteClass (uint32_t safety_class, uint32_t mode);
  272. #endif
  273. // Event Flags Library functions
  274. #ifdef RTX_SAFETY_CLASS
  275. extern void osRtxEventFlagsDeleteClass(uint32_t safety_class, uint32_t mode);
  276. #endif
  277. // Memory Heap Library functions
  278. extern uint32_t osRtxMemoryInit (void *mem, uint32_t size);
  279. extern void *osRtxMemoryAlloc(void *mem, uint32_t size, uint32_t type);
  280. extern uint32_t osRtxMemoryFree (void *mem, void *block);
  281. // Memory Pool Library functions
  282. extern uint32_t osRtxMemoryPoolInit (os_mp_info_t *mp_info, uint32_t block_count, uint32_t block_size, void *block_mem);
  283. extern void *osRtxMemoryPoolAlloc (os_mp_info_t *mp_info);
  284. extern osStatus_t osRtxMemoryPoolFree (os_mp_info_t *mp_info, void *block);
  285. #ifdef RTX_SAFETY_CLASS
  286. extern void osRtxMemoryPoolDeleteClass(uint32_t safety_class, uint32_t mode);
  287. #endif
  288. // Message Queue Library functions
  289. extern int32_t osRtxMessageQueueTimerSetup (void);
  290. #ifdef RTX_SAFETY_CLASS
  291. extern void osRtxMessageQueueDeleteClass(uint32_t safety_class, uint32_t mode);
  292. #endif
  293. // System Library functions
  294. extern void osRtxTick_Handler (void);
  295. extern void osRtxPendSV_Handler (void);
  296. extern void osRtxPostProcess (os_object_t *object);
  297. #endif // RTX_LIB_H_