os_port_cmsis_rtos2.h
Go to the documentation of this file.
1 /**
2  * @file os_port_cmsis_rtos2.h
3  * @brief RTOS abstraction layer (CMSIS-RTOS 2 / RTX v5)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2023 Oryx Embedded SARL. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 2.2.4
27  **/
28 
29 #ifndef _OS_PORT_CMSIS_RTOS2_H
30 #define _OS_PORT_CMSIS_RTOS2_H
31 
32 //Dependencies
33 #include "cmsis_os2.h"
34 
35 #ifdef RTE_CMSIS_RTOS2_RTX5
36  #include "rtx_os.h"
37 #endif
38 
39 #ifdef RTE_CMSIS_RTOS2_FreeRTOS
40  #include "freertos.h"
41 #endif
42 
43 
44 //Use static or dynamic memory allocation for tasks
45 #ifndef OS_STATIC_TASK_SUPPORT
46  #define OS_STATIC_TASK_SUPPORT DISABLED
47 #elif (OS_STATIC_TASK_SUPPORT != ENABLED && OS_STATIC_TASK_SUPPORT != DISABLED)
48  #error OS_STATIC_TASK_SUPPORT parameter is not valid
49 #endif
50 
51 //Invalid task identifier
52 #define OS_INVALID_TASK_ID NULL
53 //Self task identifier
54 #define OS_SELF_TASK_ID NULL
55 
56 //Task priority (normal)
57 #ifndef OS_TASK_PRIORITY_NORMAL
58  #define OS_TASK_PRIORITY_NORMAL osPriorityNormal
59 #endif
60 
61 //Task priority (high)
62 #ifndef OS_TASK_PRIORITY_HIGH
63  #define OS_TASK_PRIORITY_HIGH osPriorityAboveNormal
64 #endif
65 
66 //Milliseconds to system ticks
67 #ifndef OS_MS_TO_SYSTICKS
68  #define OS_MS_TO_SYSTICKS(n) (n)
69 #endif
70 
71 //System ticks to milliseconds
72 #ifndef OS_SYSTICKS_TO_MS
73  #define OS_SYSTICKS_TO_MS(n) (n)
74 #endif
75 
76 //Retrieve 64-bit system time (not implemented)
77 #ifndef osGetSystemTime64
78  #define osGetSystemTime64() osGetSystemTime()
79 #endif
80 
81 //Task prologue
82 #define osEnterTask()
83 //Task epilogue
84 #define osExitTask()
85 //Interrupt service routine prologue
86 #define osEnterIsr()
87 //Interrupt service routine epilogue
88 #define osExitIsr(flag)
89 
90 //C++ guard
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94 
95 
96 /**
97  * @brief System time
98  **/
99 
100 typedef uint32_t systime_t;
101 
102 
103 /**
104  * @brief Task identifier
105  **/
106 
107 typedef osThreadId_t OsTaskId;
108 
109 
110 /**
111  * @brief Task control block
112  **/
113 
114 typedef struct
115 {
116 #if defined(os_CMSIS_RTX)
117  os_thread_t cb;
118 #endif
119 #if defined(osRtxVersionKernel)
120  osRtxThread_t cb;
121 #endif
122 #if defined(configSUPPORT_STATIC_ALLOCATION)
123  StaticTask_t cb;
124 #endif
125  uint64_t dummy;
126 } OsTaskTcb;
127 
128 
129 /**
130  * @brief Stack data type
131  **/
132 
133 typedef uint32_t OsStackType;
134 
135 
136 /**
137  * @brief Event object
138  **/
139 
140 typedef struct
141 {
142  osEventFlagsId_t id;
143 #if defined(os_CMSIS_RTX)
144  os_event_flags_t cb;
145 #endif
146 #if defined(osRtxVersionKernel)
147  osRtxEventFlags_t cb;
148 #endif
149 } OsEvent;
150 
151 
152 /**
153  * @brief Semaphore object
154  **/
155 
156 typedef struct
157 {
158  osSemaphoreId_t id;
159 #if defined(os_CMSIS_RTX)
160  os_semaphore_t cb;
161 #endif
162 #if defined(osRtxVersionKernel)
163  osRtxSemaphore_t cb;
164 #endif
165 } OsSemaphore;
166 
167 
168 /**
169  * @brief Mutex object
170  **/
171 
172 typedef struct
173 {
174  osMutexId_t id;
175 #if defined(os_CMSIS_RTX)
176  os_mutex_t cb;
177 #endif
178 #if defined(osRtxVersionKernel)
179  osRtxMutex_t cb;
180 #endif
181 } OsMutex;
182 
183 
184 /**
185  * @brief Task routine
186  **/
187 
188 typedef void (*OsTaskCode)(void *param);
189 
190 
191 //Kernel management
192 void osInitKernel(void);
193 void osStartKernel(void);
194 
195 //Task management
196 OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode,
197  void *param, size_t stackSize, int_t priority);
198 
200  void *param, OsTaskTcb *tcb, OsStackType *stack, size_t stackSize,
201  int_t priority);
202 
203 void osDeleteTask(OsTaskId taskId);
204 void osDelayTask(systime_t delay);
205 void osSwitchTask(void);
206 void osSuspendAllTasks(void);
207 void osResumeAllTasks(void);
208 
209 //Event management
211 void osDeleteEvent(OsEvent *event);
212 void osSetEvent(OsEvent *event);
213 void osResetEvent(OsEvent *event);
214 bool_t osWaitForEvent(OsEvent *event, systime_t timeout);
216 
217 //Semaphore management
218 bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count);
219 void osDeleteSemaphore(OsSemaphore *semaphore);
220 bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout);
221 void osReleaseSemaphore(OsSemaphore *semaphore);
222 
223 //Mutex management
225 void osDeleteMutex(OsMutex *mutex);
226 void osAcquireMutex(OsMutex *mutex);
227 void osReleaseMutex(OsMutex *mutex);
228 
229 //System time
231 
232 //Memory management
233 void *osAllocMem(size_t size);
234 void osFreeMem(void *p);
235 
236 //C++ guard
237 #ifdef __cplusplus
238 }
239 #endif
240 
241 #endif
int bool_t
Definition: compiler_port.h:53
signed int int_t
Definition: compiler_port.h:49
osEventFlagsId_t id
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
uint8_t p
Definition: ndp.h:298
void osDelayTask(systime_t delay)
Delay routine.
void osDeleteTask(OsTaskId taskId)
Delete a task.
Event object.
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
void osSuspendAllTasks(void)
Suspend scheduler activity.
osMutexId_t id
char_t name[]
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
OsTaskId osCreateStaticTask(const char_t *name, OsTaskCode taskCode, void *param, OsTaskTcb *tcb, OsStackType *stack, size_t stackSize, int_t priority)
Create a task with statically allocated memory.
osSemaphoreId_t id
void * osAllocMem(size_t size)
Allocate a memory block.
Semaphore object.
void osStartKernel(void)
Start kernel.
void osDeleteEvent(OsEvent *event)
Delete an event object.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
uint32_t OsStackType
Stack data type.
mutex_t OsMutex
Mutex object.
void osInitKernel(void)
Kernel initialization.
binary_semaphore_t OsEvent
Event object.
uint32_t systime_t
System time.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
Task control block.
uint32_t OsStackType
Stack data type.
osThreadId_t OsTaskId
Task identifier.
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void osFreeMem(void *p)
Release a previously allocated memory block.
Mutex object.
uint32_t systime_t
System time.
char char_t
Definition: compiler_port.h:48
void(* OsTaskCode)(void *param)
Task routine.
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a task.
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
thread_t * OsTaskId
Task identifier.
OS_TASK OsTaskTcb
Task control block.
void osSwitchTask(void)
Yield control to the next task.
unsigned int uint_t
Definition: compiler_port.h:50
uint16_t priority
Definition: dns_common.h:249
systime_t osGetSystemTime(void)
Retrieve system time.
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void osResumeAllTasks(void)
Resume scheduler activity.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
semaphore_t OsSemaphore
Semaphore object.