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