rtx_lib.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 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. #if ((defined(__ARM_ARCH_8M_BASE__) && (__ARM_ARCH_8M_BASE__ != 0)) || \
  31. (defined(__ARM_ARCH_8M_MAIN__) && (__ARM_ARCH_8M_MAIN__ != 0)) || \
  32. (defined(__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ != 0)))
  33. #include "tz_context.h" // TrustZone Context API
  34. #endif
  35. #include "os_tick.h" // CMSIS OS Tick API
  36. #include "cmsis_os2.h" // CMSIS RTOS API
  37. #include "rtx_os.h" // RTX OS definitions
  38. #include "rtx_evr.h" // RTX Event Recorder definitions
  39. // ==== Library defines ====
  40. #define os_thread_t osRtxThread_t
  41. #define os_timer_t osRtxTimer_t
  42. #define os_timer_finfo_t osRtxTimerFinfo_t
  43. #define os_event_flags_t osRtxEventFlags_t
  44. #define os_mutex_t osRtxMutex_t
  45. #define os_semaphore_t osRtxSemaphore_t
  46. #define os_mp_info_t osRtxMpInfo_t
  47. #define os_memory_pool_t osRtxMemoryPool_t
  48. #define os_message_t osRtxMessage_t
  49. #define os_message_queue_t osRtxMessageQueue_t
  50. #define os_object_t osRtxObject_t
  51. // ==== Inline functions ====
  52. // Thread ID
  53. __STATIC_INLINE os_thread_t *osRtxThreadId (osThreadId_t thread_id) {
  54. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  55. return ((os_thread_t *)thread_id);
  56. }
  57. // Timer ID
  58. __STATIC_INLINE os_timer_t *osRtxTimerId (osTimerId_t timer_id) {
  59. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  60. return ((os_timer_t *)timer_id);
  61. }
  62. // Event Flags ID
  63. __STATIC_INLINE os_event_flags_t *osRtxEventFlagsId (osEventFlagsId_t ef_id) {
  64. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  65. return ((os_event_flags_t *)ef_id);
  66. }
  67. // Mutex ID
  68. __STATIC_INLINE os_mutex_t *osRtxMutexId (osMutexId_t mutex_id) {
  69. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  70. return ((os_mutex_t *)mutex_id);
  71. }
  72. // Semaphore ID
  73. __STATIC_INLINE os_semaphore_t *osRtxSemaphoreId (osSemaphoreId_t semaphore_id) {
  74. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  75. return ((os_semaphore_t *)semaphore_id);
  76. }
  77. // Memory Pool ID
  78. __STATIC_INLINE os_memory_pool_t *osRtxMemoryPoolId (osMemoryPoolId_t mp_id) {
  79. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  80. return ((os_memory_pool_t *)mp_id);
  81. }
  82. // Message Queue ID
  83. __STATIC_INLINE os_message_queue_t *osRtxMessageQueueId (osMessageQueueId_t mq_id) {
  84. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 2]
  85. return ((os_message_queue_t *)mq_id);
  86. }
  87. // Generic Object
  88. __STATIC_INLINE os_object_t *osRtxObject (void *object) {
  89. //lint -e{9079} -e{9087} "cast from pointer to void to pointer to object type" [MISRA Note 3]
  90. return ((os_object_t *)object);
  91. }
  92. // Thread Object
  93. __STATIC_INLINE os_thread_t *osRtxThreadObject (os_object_t *object) {
  94. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  95. return ((os_thread_t *)object);
  96. }
  97. // Timer Object
  98. __STATIC_INLINE os_timer_t *osRtxTimerObject (os_object_t *object) {
  99. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  100. return ((os_timer_t *)object);
  101. }
  102. // Event Flags Object
  103. __STATIC_INLINE os_event_flags_t *osRtxEventFlagsObject (os_object_t *object) {
  104. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  105. return ((os_event_flags_t *)object);
  106. }
  107. // Mutex Object
  108. __STATIC_INLINE os_mutex_t *osRtxMutexObject (os_object_t *object) {
  109. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  110. return ((os_mutex_t *)object);
  111. }
  112. // Semaphore Object
  113. __STATIC_INLINE os_semaphore_t *osRtxSemaphoreObject (os_object_t *object) {
  114. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  115. return ((os_semaphore_t *)object);
  116. }
  117. // Memory Pool Object
  118. __STATIC_INLINE os_memory_pool_t *osRtxMemoryPoolObject (os_object_t *object) {
  119. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  120. return ((os_memory_pool_t *)object);
  121. }
  122. // Message Queue Object
  123. __STATIC_INLINE os_message_queue_t *osRtxMessageQueueObject (os_object_t *object) {
  124. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  125. return ((os_message_queue_t *)object);
  126. }
  127. // Message Object
  128. __STATIC_INLINE os_message_t *osRtxMessageObject (os_object_t *object) {
  129. //lint -e{740} -e{826} -e{9087} "cast from pointer to generic object to specific object" [MISRA Note 4]
  130. return ((os_message_t *)object);
  131. }
  132. // Kernel State
  133. __STATIC_INLINE osKernelState_t osRtxKernelState (void) {
  134. //lint -e{9030} -e{9034} "cast to enum"
  135. return ((osKernelState_t)(osRtxInfo.kernel.state));
  136. }
  137. // Thread State
  138. __STATIC_INLINE osThreadState_t osRtxThreadState (const os_thread_t *thread) {
  139. uint8_t state = thread->state & osRtxThreadStateMask;
  140. //lint -e{9030} -e{9034} "cast to enum"
  141. return ((osThreadState_t)state);
  142. }
  143. // Thread Priority
  144. __STATIC_INLINE osPriority_t osRtxThreadPriority (const os_thread_t *thread) {
  145. //lint -e{9030} -e{9034} "cast to enum"
  146. return ((osPriority_t)thread->priority);
  147. }
  148. // Kernel Get State
  149. __STATIC_INLINE uint8_t osRtxKernelGetState (void) {
  150. return osRtxInfo.kernel.state;
  151. }
  152. // Thread Get/Set Running
  153. __STATIC_INLINE os_thread_t *osRtxThreadGetRunning (void) {
  154. return osRtxInfo.thread.run.curr;
  155. }
  156. __STATIC_INLINE void osRtxThreadSetRunning (os_thread_t *thread) {
  157. osRtxInfo.thread.run.curr = thread;
  158. }
  159. // ==== Library functions ====
  160. // Kernel Library functions
  161. extern void osRtxKernelPreInit (void);
  162. // Thread Library functions
  163. extern void osRtxThreadListPut (os_object_t *object, os_thread_t *thread);
  164. extern os_thread_t *osRtxThreadListGet (os_object_t *object);
  165. extern void osRtxThreadListSort (os_thread_t *thread);
  166. extern void osRtxThreadListRemove (os_thread_t *thread);
  167. extern void osRtxThreadReadyPut (os_thread_t *thread);
  168. extern void osRtxThreadDelayTick (void);
  169. extern uint32_t *osRtxThreadRegPtr (const os_thread_t *thread);
  170. extern void osRtxThreadSwitch (os_thread_t *thread);
  171. extern void osRtxThreadDispatch (os_thread_t *thread);
  172. extern void osRtxThreadWaitExit (os_thread_t *thread, uint32_t ret_val, bool_t dispatch);
  173. extern bool_t osRtxThreadWaitEnter (uint8_t state, uint32_t timeout);
  174. #ifdef RTX_STACK_CHECK
  175. extern bool_t osRtxThreadStackCheck (const os_thread_t *thread);
  176. #endif
  177. extern bool_t osRtxThreadStartup (void);
  178. // Timer Library functions
  179. extern int32_t osRtxTimerSetup (void);
  180. extern void osRtxTimerThread (void *argument);
  181. // Mutex Library functions
  182. extern void osRtxMutexOwnerRelease (os_mutex_t *mutex_list);
  183. extern void osRtxMutexOwnerRestore (const os_mutex_t *mutex, const os_thread_t *thread_wakeup);
  184. // Memory Heap Library functions
  185. extern uint32_t osRtxMemoryInit (void *mem, uint32_t size);
  186. extern void *osRtxMemoryAlloc(void *mem, uint32_t size, uint32_t type);
  187. extern uint32_t osRtxMemoryFree (void *mem, void *block);
  188. // Memory Pool Library functions
  189. extern uint32_t osRtxMemoryPoolInit (os_mp_info_t *mp_info, uint32_t block_count, uint32_t block_size, void *block_mem);
  190. extern void *osRtxMemoryPoolAlloc (os_mp_info_t *mp_info);
  191. extern osStatus_t osRtxMemoryPoolFree (os_mp_info_t *mp_info, void *block);
  192. // Message Queue Library functions
  193. extern int32_t osRtxMessageQueueTimerSetup (void);
  194. // System Library functions
  195. extern void osRtxTick_Handler (void);
  196. extern void osRtxPendSV_Handler (void);
  197. extern void osRtxPostProcess (os_object_t *object);
  198. #endif // RTX_LIB_H_