os_port_freertos.h
Go to the documentation of this file.
1 /**
2  * @file os_port_freertos.h
3  * @brief RTOS abstraction layer (FreeRTOS)
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_FREERTOS_H
30 #define _OS_PORT_FREERTOS_H
31 
32 //Dependencies
33 #ifdef IDF_VER
34  #include "freertos/FreeRTOS.h"
35  #include "freertos/task.h"
36  #include "freertos/semphr.h"
37 #else
38  #include "FreeRTOS.h"
39  #include "task.h"
40  #include "semphr.h"
41 #endif
42 
43 //Use static or dynamic memory allocation for tasks
44 #ifndef OS_STATIC_TASK_SUPPORT
45  #define OS_STATIC_TASK_SUPPORT DISABLED
46 #elif (OS_STATIC_TASK_SUPPORT != ENABLED && OS_STATIC_TASK_SUPPORT != DISABLED)
47  #error OS_STATIC_TASK_SUPPORT parameter is not valid
48 #endif
49 
50 //Invalid task identifier
51 #define OS_INVALID_TASK_ID NULL
52 //Self task identifier
53 #define OS_SELF_TASK_ID NULL
54 
55 //Task priority (normal)
56 #ifndef OS_TASK_PRIORITY_NORMAL
57  #define OS_TASK_PRIORITY_NORMAL (tskIDLE_PRIORITY + 1)
58 #endif
59 
60 //Task priority (high)
61 #ifndef OS_TASK_PRIORITY_HIGH
62  #define OS_TASK_PRIORITY_HIGH (tskIDLE_PRIORITY + 2)
63 #endif
64 
65 //Milliseconds to system ticks
66 #ifndef OS_MS_TO_SYSTICKS
67  #define OS_MS_TO_SYSTICKS(n) (n)
68 #endif
69 
70 //System ticks to milliseconds
71 #ifndef OS_SYSTICKS_TO_MS
72  #define OS_SYSTICKS_TO_MS(n) (n)
73 #endif
74 
75 //Retrieve 64-bit system time (not implemented)
76 #ifndef osGetSystemTime64
77  #define osGetSystemTime64() osGetSystemTime()
78 #endif
79 
80 //Task prologue
81 #ifndef osEnterTask
82  #define osEnterTask()
83 #endif
84 
85 //Task epilogue
86 #ifndef osExitTask
87  #define osExitTask()
88 #endif
89 
90 //Interrupt service routine prologue
91 #ifndef osEnterIsr
92  #if defined(portENTER_SWITCHING_ISR)
93  #define osEnterIsr() portENTER_SWITCHING_ISR()
94  #else
95  #define osEnterIsr()
96  #endif
97 #endif
98 
99 //Interrupt service routine epilogue
100 #ifndef osExitIsr
101  #if defined(__XTENSA__)
102  #define osExitIsr(flag) if(flag) portYIELD_FROM_ISR()
103  #elif defined(portEXIT_SWITCHING_ISR)
104  #define osExitIsr(flag) portEXIT_SWITCHING_ISR()
105  #elif defined(portEND_SWITCHING_ISR)
106  #define osExitIsr(flag) portEND_SWITCHING_ISR(flag)
107  #elif defined(portYIELD_FROM_ISR)
108  #define osExitIsr(flag) portYIELD_FROM_ISR(flag)
109  #else
110  #define osExitIsr(flag)
111  #endif
112 #endif
113 
114 //Static object allocation
115 #ifndef configSUPPORT_STATIC_ALLOCATION
116  #define configSUPPORT_STATIC_ALLOCATION 0
117 #endif
118 
119 //C++ guard
120 #ifdef __cplusplus
121 extern "C" {
122 #endif
123 
124 
125 /**
126  * @brief System time
127  **/
128 
129 typedef TickType_t systime_t;
130 
131 
132 /**
133  * @brief Task identifier
134  **/
135 
136 typedef TaskHandle_t OsTaskId;
137 
138 
139 /**
140  * @brief Task control block
141  **/
142 
143 #if (configSUPPORT_STATIC_ALLOCATION == 1)
144 typedef StaticTask_t OsTaskTcb;
145 #else
146 typedef void OsTaskTcb;
147 #endif
148 
149 
150 /**
151  * @brief Stack data type
152  **/
153 
154 #ifdef IDF_VER
155 typedef uint32_t OsStackType;
156 #else
157 typedef StackType_t OsStackType;
158 #endif
159 
160 
161 /**
162  * @brief Event object
163  **/
164 
165 typedef struct
166 {
167  SemaphoreHandle_t handle;
168 #if (configSUPPORT_STATIC_ALLOCATION == 1)
169  StaticSemaphore_t buffer;
170 #endif
171 } OsEvent;
172 
173 
174 /**
175  * @brief Semaphore object
176  **/
177 
178 typedef struct
179 {
180  SemaphoreHandle_t handle;
181 #if (configSUPPORT_STATIC_ALLOCATION == 1)
182  StaticSemaphore_t buffer;
183 #endif
184 } OsSemaphore;
185 
186 
187 /**
188  * @brief Mutex object
189  **/
190 
191 typedef struct
192 {
193  SemaphoreHandle_t handle;
194 #if (configSUPPORT_STATIC_ALLOCATION == 1)
195  StaticSemaphore_t buffer;
196 #endif
197 } OsMutex;
198 
199 
200 /**
201  * @brief Task routine
202  **/
203 
204 typedef void (*OsTaskCode)(void *param);
205 
206 
207 //Kernel management
208 void osInitKernel(void);
209 void osStartKernel(void);
210 
211 //Task management
212 OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode,
213  void *param, size_t stackSize, int_t priority);
214 
216  void *param, OsTaskTcb *tcb, OsStackType *stack, size_t stackSize,
217  int_t priority);
218 
219 void osDeleteTask(OsTaskId taskId);
220 void osDelayTask(systime_t delay);
221 void osSwitchTask(void);
222 void osSuspendAllTasks(void);
223 void osResumeAllTasks(void);
224 
225 //Event management
227 void osDeleteEvent(OsEvent *event);
228 void osSetEvent(OsEvent *event);
229 void osResetEvent(OsEvent *event);
230 bool_t osWaitForEvent(OsEvent *event, systime_t timeout);
232 
233 //Semaphore management
234 bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count);
235 void osDeleteSemaphore(OsSemaphore *semaphore);
236 bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout);
237 void osReleaseSemaphore(OsSemaphore *semaphore);
238 
239 //Mutex management
241 void osDeleteMutex(OsMutex *mutex);
242 void osAcquireMutex(OsMutex *mutex);
243 void osReleaseMutex(OsMutex *mutex);
244 
245 //System time
247 
248 //Memory management
249 void *osAllocMem(size_t size);
250 void osFreeMem(void *p);
251 
252 //C++ guard
253 #ifdef __cplusplus
254 }
255 #endif
256 
257 #endif
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a task.
systime_t osGetSystemTime(void)
Retrieve system time.
int bool_t
Definition: compiler_port.h:53
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
signed int int_t
Definition: compiler_port.h:49
SemaphoreHandle_t handle
uint8_t p
Definition: ndp.h:298
Event object.
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete 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.
char_t name[]
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
Semaphore object.
void * osAllocMem(size_t size)
Allocate a memory block.
void osSuspendAllTasks(void)
Suspend scheduler activity.
void OsTaskTcb
Task control block.
mutex_t OsMutex
Mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
binary_semaphore_t OsEvent
Event object.
void osSwitchTask(void)
Yield control to the next task.
StackType_t OsStackType
Stack data type.
Task control block.
TaskHandle_t OsTaskId
Task identifier.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
uint32_t OsStackType
Stack data type.
void osDelayTask(systime_t delay)
Delay routine.
Mutex object.
uint32_t systime_t
System time.
char char_t
Definition: compiler_port.h:48
void osInitKernel(void)
Kernel initialization.
SemaphoreHandle_t handle
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
void(* OsTaskCode)(void *param)
Task routine.
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
TickType_t systime_t
System time.
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
void osStartKernel(void)
Start kernel.
void osDeleteEvent(OsEvent *event)
Delete an event object.
void osResumeAllTasks(void)
Resume scheduler activity.
void osFreeMem(void *p)
Release a previously allocated memory block.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
thread_t * OsTaskId
Task identifier.
unsigned int uint_t
Definition: compiler_port.h:50
uint16_t priority
Definition: dns_common.h:249
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
void osDeleteTask(OsTaskId taskId)
Delete a task.
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
SemaphoreHandle_t handle
semaphore_t OsSemaphore
Semaphore object.