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-2026 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.6.0
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 //Invalid task identifier
44 #define OS_INVALID_TASK_ID NULL
45 //Self task identifier
46 #define OS_SELF_TASK_ID NULL
47 
48 //Task priority (normal)
49 #ifndef OS_TASK_PRIORITY_NORMAL
50  #define OS_TASK_PRIORITY_NORMAL (tskIDLE_PRIORITY + 1)
51 #endif
52 
53 //Task priority (high)
54 #ifndef OS_TASK_PRIORITY_HIGH
55  #define OS_TASK_PRIORITY_HIGH (tskIDLE_PRIORITY + 2)
56 #endif
57 
58 //Milliseconds to system ticks
59 #ifndef OS_MS_TO_SYSTICKS
60  #define OS_MS_TO_SYSTICKS(n) (n)
61 #endif
62 
63 //System ticks to milliseconds
64 #ifndef OS_SYSTICKS_TO_MS
65  #define OS_SYSTICKS_TO_MS(n) (n)
66 #endif
67 
68 //Retrieve 64-bit system time (not implemented)
69 #ifndef osGetSystemTime64
70  #define osGetSystemTime64() osGetSystemTime()
71 #endif
72 
73 //Task prologue
74 #ifndef osEnterTask
75  #define osEnterTask()
76 #endif
77 
78 //Task epilogue
79 #ifndef osExitTask
80  #define osExitTask()
81 #endif
82 
83 //Interrupt service routine prologue
84 #ifndef osEnterIsr
85  #if defined(portENTER_SWITCHING_ISR)
86  #define osEnterIsr() portENTER_SWITCHING_ISR()
87  #else
88  #define osEnterIsr()
89  #endif
90 #endif
91 
92 //Interrupt service routine epilogue
93 #ifndef osExitIsr
94  #if defined(__ADSPSC8xx__)
95  #define osExitIsr(flag) portYIELD_FROM_ISR(flag)
96  #elif defined(__XTENSA__)
97  #define osExitIsr(flag) if(flag) portYIELD_FROM_ISR()
98  #elif defined(portEXIT_SWITCHING_ISR)
99  #define osExitIsr(flag) portEXIT_SWITCHING_ISR()
100  #elif defined(portEND_SWITCHING_ISR)
101  #define osExitIsr(flag) portEND_SWITCHING_ISR(flag)
102  #elif defined(portYIELD_FROM_ISR)
103  #define osExitIsr(flag) portYIELD_FROM_ISR(flag)
104  #else
105  #define osExitIsr(flag)
106  #endif
107 #endif
108 
109 //Static object allocation
110 #ifndef configSUPPORT_STATIC_ALLOCATION
111  #define configSUPPORT_STATIC_ALLOCATION 0
112 #endif
113 
114 //C++ guard
115 #ifdef __cplusplus
116 extern "C" {
117 #endif
118 
119 
120 /**
121  * @brief System time
122  **/
123 
124 typedef TickType_t systime_t;
125 
126 
127 /**
128  * @brief Task identifier
129  **/
130 
131 typedef TaskHandle_t OsTaskId;
132 
133 
134 /**
135  * @brief Task parameters
136  **/
137 
138 typedef struct
139 {
140 #if (configSUPPORT_STATIC_ALLOCATION == 1)
141  StaticTask_t *tcb;
142 #ifdef IDF_VER
143  uint32_t *stack;
144 #else
145  StackType_t *stack;
146 #endif
147 #endif
148  size_t stackSize;
151 
152 
153 /**
154  * @brief Event object
155  **/
156 
157 typedef struct
158 {
159  SemaphoreHandle_t handle;
160 #if (configSUPPORT_STATIC_ALLOCATION == 1)
161  StaticSemaphore_t buffer;
162 #endif
163 } OsEvent;
164 
165 
166 /**
167  * @brief Semaphore object
168  **/
169 
170 typedef struct
171 {
172  SemaphoreHandle_t handle;
173 #if (configSUPPORT_STATIC_ALLOCATION == 1)
174  StaticSemaphore_t buffer;
175 #endif
176 } OsSemaphore;
177 
178 
179 /**
180  * @brief Mutex object
181  **/
182 
183 typedef struct
184 {
185  SemaphoreHandle_t handle;
186 #if (configSUPPORT_STATIC_ALLOCATION == 1)
187  StaticSemaphore_t buffer;
188 #endif
189 } OsMutex;
190 
191 
192 /**
193  * @brief Task routine
194  **/
195 
196 typedef void (*OsTaskCode)(void *arg);
197 
198 
199 //Default task parameters
201 
202 //Kernel management
203 void osInitKernel(void);
204 void osStartKernel(void);
205 
206 //Task management
207 OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg,
208  const OsTaskParameters *params);
209 
210 void osDeleteTask(OsTaskId taskId);
211 void osDelayTask(systime_t delay);
212 void osSwitchTask(void);
213 void osSuspendAllTasks(void);
214 void osResumeAllTasks(void);
215 
216 //Event management
218 void osDeleteEvent(OsEvent *event);
219 void osSetEvent(OsEvent *event);
220 void osResetEvent(OsEvent *event);
221 bool_t osWaitForEvent(OsEvent *event, systime_t timeout);
223 
224 //Semaphore management
225 bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count);
226 void osDeleteSemaphore(OsSemaphore *semaphore);
227 bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout);
228 void osReleaseSemaphore(OsSemaphore *semaphore);
229 
230 //Mutex management
232 void osDeleteMutex(OsMutex *mutex);
233 void osAcquireMutex(OsMutex *mutex);
234 void osReleaseMutex(OsMutex *mutex);
235 
236 //System time
238 
239 //Memory management
240 void *osAllocMem(size_t size);
241 void osFreeMem(void *p);
242 
243 //C++ guard
244 #ifdef __cplusplus
245 }
246 #endif
247 
248 #endif
systime_t osGetSystemTime(void)
Retrieve system time.
int bool_t
Definition: compiler_port.h:63
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
SemaphoreHandle_t handle
uint8_t p
Definition: ndp.h:300
Event object.
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
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.
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.
TaskHandle_t OsTaskId
Task identifier.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
Task parameters.
void osDelayTask(systime_t delay)
Delay routine.
void(* OsTaskCode)(void *arg)
Task routine.
Mutex object.
uint32_t systime_t
System time.
char char_t
Definition: compiler_port.h:55
void osInitKernel(void)
Kernel initialization.
SemaphoreHandle_t handle
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
TickType_t systime_t
System time.
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
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:57
uint16_t priority
Definition: dns_common.h:268
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.