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-2024 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.4.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(__XTENSA__)
95  #define osExitIsr(flag) if(flag) portYIELD_FROM_ISR()
96  #elif defined(portEXIT_SWITCHING_ISR)
97  #define osExitIsr(flag) portEXIT_SWITCHING_ISR()
98  #elif defined(portEND_SWITCHING_ISR)
99  #define osExitIsr(flag) portEND_SWITCHING_ISR(flag)
100  #elif defined(portYIELD_FROM_ISR)
101  #define osExitIsr(flag) portYIELD_FROM_ISR(flag)
102  #else
103  #define osExitIsr(flag)
104  #endif
105 #endif
106 
107 //Static object allocation
108 #ifndef configSUPPORT_STATIC_ALLOCATION
109  #define configSUPPORT_STATIC_ALLOCATION 0
110 #endif
111 
112 //C++ guard
113 #ifdef __cplusplus
114 extern "C" {
115 #endif
116 
117 
118 /**
119  * @brief System time
120  **/
121 
122 typedef TickType_t systime_t;
123 
124 
125 /**
126  * @brief Task identifier
127  **/
128 
129 typedef TaskHandle_t OsTaskId;
130 
131 
132 /**
133  * @brief Task parameters
134  **/
135 
136 typedef struct
137 {
138 #if (configSUPPORT_STATIC_ALLOCATION == 1)
139  StaticTask_t *tcb;
140 #ifdef IDF_VER
141  uint32_t *stack;
142 #else
143  StackType_t *stack;
144 #endif
145 #endif
146  size_t stackSize;
149 
150 
151 /**
152  * @brief Event object
153  **/
154 
155 typedef struct
156 {
157  SemaphoreHandle_t handle;
158 #if (configSUPPORT_STATIC_ALLOCATION == 1)
159  StaticSemaphore_t buffer;
160 #endif
161 } OsEvent;
162 
163 
164 /**
165  * @brief Semaphore object
166  **/
167 
168 typedef struct
169 {
170  SemaphoreHandle_t handle;
171 #if (configSUPPORT_STATIC_ALLOCATION == 1)
172  StaticSemaphore_t buffer;
173 #endif
174 } OsSemaphore;
175 
176 
177 /**
178  * @brief Mutex object
179  **/
180 
181 typedef struct
182 {
183  SemaphoreHandle_t handle;
184 #if (configSUPPORT_STATIC_ALLOCATION == 1)
185  StaticSemaphore_t buffer;
186 #endif
187 } OsMutex;
188 
189 
190 /**
191  * @brief Task routine
192  **/
193 
194 typedef void (*OsTaskCode)(void *arg);
195 
196 
197 //Default task parameters
199 
200 //Kernel management
201 void osInitKernel(void);
202 void osStartKernel(void);
203 
204 //Task management
205 OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg,
206  const OsTaskParameters *params);
207 
208 void osDeleteTask(OsTaskId taskId);
209 void osDelayTask(systime_t delay);
210 void osSwitchTask(void);
211 void osSuspendAllTasks(void);
212 void osResumeAllTasks(void);
213 
214 //Event management
216 void osDeleteEvent(OsEvent *event);
217 void osSetEvent(OsEvent *event);
218 void osResetEvent(OsEvent *event);
219 bool_t osWaitForEvent(OsEvent *event, systime_t timeout);
221 
222 //Semaphore management
223 bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count);
224 void osDeleteSemaphore(OsSemaphore *semaphore);
225 bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout);
226 void osReleaseSemaphore(OsSemaphore *semaphore);
227 
228 //Mutex management
230 void osDeleteMutex(OsMutex *mutex);
231 void osAcquireMutex(OsMutex *mutex);
232 void osReleaseMutex(OsMutex *mutex);
233 
234 //System time
236 
237 //Memory management
238 void *osAllocMem(size_t size);
239 void osFreeMem(void *p);
240 
241 //C++ guard
242 #ifdef __cplusplus
243 }
244 #endif
245 
246 #endif
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
uint16_t priority
Definition: dns_common.h:265
uint8_t p
Definition: ndp.h:300
binary_semaphore_t OsEvent
Event object.
semaphore_t OsSemaphore
Semaphore object.
uint32_t systime_t
System time.
mutex_t OsMutex
Mutex object.
thread_t * OsTaskId
Task identifier.
void osSwitchTask(void)
Yield control to the next task.
void osResumeAllTasks(void)
Resume scheduler activity.
void(* OsTaskCode)(void *arg)
Task routine.
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
void osDeleteEvent(OsEvent *event)
Delete an event object.
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
TaskHandle_t OsTaskId
Task identifier.
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osFreeMem(void *p)
Release a previously allocated memory block.
void osDelayTask(systime_t delay)
Delay routine.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
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 osDeleteTask(OsTaskId taskId)
Delete a task.
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
systime_t osGetSystemTime(void)
Retrieve system time.
TickType_t systime_t
System time.
void osSuspendAllTasks(void)
Suspend scheduler activity.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
void osStartKernel(void)
Start kernel.
void * osAllocMem(size_t size)
Allocate a memory block.
void osInitKernel(void)
Kernel initialization.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
char_t name[]
Event object.
SemaphoreHandle_t handle
Mutex object.
SemaphoreHandle_t handle
Semaphore object.
SemaphoreHandle_t handle
Task parameters.