os_port_chibios.h
Go to the documentation of this file.
1 /**
2  * @file os_port_chibios.h
3  * @brief RTOS abstraction layer (ChibiOS/RT)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2022 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.0
27  **/
28 
29 #ifndef _OS_PORT_CHIBIOS_H
30 #define _OS_PORT_CHIBIOS_H
31 
32 //Dependencies
33 #include "ch.h"
34 
35 //Use static or dynamic memory allocation for tasks
36 #ifndef OS_STATIC_TASK_SUPPORT
37  #define OS_STATIC_TASK_SUPPORT DISABLED
38 #elif (OS_STATIC_TASK_SUPPORT != ENABLED && OS_STATIC_TASK_SUPPORT != DISABLED)
39  #error OS_STATIC_TASK_SUPPORT parameter is not valid
40 #endif
41 
42 //Invalid task identifier
43 #define OS_INVALID_TASK_ID NULL
44 //Self task identifier
45 #define OS_SELF_TASK_ID NULL
46 
47 //Task priority (normal)
48 #ifndef OS_TASK_PRIORITY_NORMAL
49  #define OS_TASK_PRIORITY_NORMAL NORMALPRIO
50 #endif
51 
52 //Task priority (high)
53 #ifndef OS_TASK_PRIORITY_HIGH
54  #define OS_TASK_PRIORITY_HIGH HIGHPRIO
55 #endif
56 
57 //Milliseconds to system ticks
58 #ifndef OS_MS_TO_SYSTICKS
59  #define OS_MS_TO_SYSTICKS(n) (n)
60 #endif
61 
62 //System ticks to milliseconds
63 #ifndef OS_SYSTICKS_TO_MS
64  #define OS_SYSTICKS_TO_MS(n) (n)
65 #endif
66 
67 //Retrieve 64-bit system time (not implemented)
68 #ifndef osGetSystemTime64
69  #define osGetSystemTime64() osGetSystemTime()
70 #endif
71 
72 //Task prologue
73 #define osEnterTask()
74 //Task epilogue
75 #define osExitTask()
76 //Interrupt service routine prologue
77 #define osEnterIsr() CH_IRQ_PROLOGUE(); chSysLockFromISR()
78 //Interrupt service routine epilogue
79 #define osExitIsr(flag) chSysUnlockFromISR(); CH_IRQ_EPILOGUE()
80 
81 //Check kernel version
82 #if (CH_KERNEL_MAJOR < 3)
83  #define thread_t Thread
84  #define semaphore_t Semaphore
85  #define binary_semaphore_t BinarySemaphore
86  #define mutex_t Mutex
87  #define chThdTerminatedX chThdTerminated
88  #define chSemObjectInit chSemInit
89  #define chBSemObjectInit chBSemInit
90  #define chMtxObjectInit chMtxInit
91  #define chVTGetSystemTime chTimeNow
92  #define chSysLockFromISR chSysLockFromIsr
93  #define chSysUnlockFromISR chSysUnlockFromIsr
94  #define THD_WORKING_AREA_SIZE THD_WA_SIZE
95  #define MSG_OK RDY_OK
96 #endif
97 
98 //C++ guard
99 #ifdef __cplusplus
100 extern "C" {
101 #endif
102 
103 
104 /**
105  * @brief System time
106  **/
107 
108 typedef uint32_t systime_t;
109 
110 
111 /**
112  * @brief Task identifier
113  **/
114 
116 
117 
118 /**
119  * @brief Task control block
120  **/
121 
122 typedef struct
123 {
124  uint64_t dummy;
125 } OsTaskTcb;
126 
127 
128 /**
129  * @brief Stack data type
130  **/
131 
132 typedef uint32_t OsStackType;
133 
134 
135 /**
136  * @brief Event object
137  **/
138 
140 
141 
142 /**
143  * @brief Semaphore object
144  **/
145 
147 
148 
149 /**
150  * @brief Mutex object
151  **/
152 
153 typedef mutex_t OsMutex;
154 
155 
156 /**
157  * @brief Task routine
158  **/
159 
160 typedef void (*OsTaskCode)(void *param);
161 
162 
163 //Kernel management
164 void osInitKernel(void);
165 void osStartKernel(void);
166 
167 //Task management
168 OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode,
169  void *param, size_t stackSize, int_t priority);
170 
172  void *param, OsTaskTcb *tcb, OsStackType *stack, size_t stackSize,
173  int_t priority);
174 
175 void osDeleteTask(OsTaskId taskId);
176 void osDelayTask(systime_t delay);
177 void osSwitchTask(void);
178 void osSuspendAllTasks(void);
179 void osResumeAllTasks(void);
180 
181 //Event management
183 void osDeleteEvent(OsEvent *event);
184 void osSetEvent(OsEvent *event);
185 void osResetEvent(OsEvent *event);
186 bool_t osWaitForEvent(OsEvent *event, systime_t timeout);
188 
189 //Semaphore management
190 bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count);
191 void osDeleteSemaphore(OsSemaphore *semaphore);
192 bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout);
193 void osReleaseSemaphore(OsSemaphore *semaphore);
194 
195 //Mutex management
197 void osDeleteMutex(OsMutex *mutex);
198 void osAcquireMutex(OsMutex *mutex);
199 void osReleaseMutex(OsMutex *mutex);
200 
201 //System time
203 
204 //Memory management
205 void *osAllocMem(size_t size);
206 void osFreeMem(void *p);
207 
208 //C++ guard
209 #ifdef __cplusplus
210 }
211 #endif
212 
213 #endif
systime_t osGetSystemTime(void)
Retrieve system time.
int bool_t
Definition: compiler_port.h:48
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a task.
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
signed int int_t
Definition: compiler_port.h:44
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
uint8_t p
Definition: ndp.h:298
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
Event object.
void osFreeMem(void *p)
Release a previously allocated memory block.
void osSwitchTask(void)
Yield control to the next task.
char_t name[]
uint64_t dummy
void osStartKernel(void)
Start kernel.
Semaphore object.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
#define binary_semaphore_t
mutex_t OsMutex
Mutex object.
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
binary_semaphore_t OsEvent
Event 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.
Task control block.
uint32_t OsStackType
Stack data type.
void osResumeAllTasks(void)
Resume scheduler activity.
void osDeleteEvent(OsEvent *event)
Delete an event object.
void osDeleteTask(OsTaskId taskId)
Delete a task.
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
#define mutex_t
#define thread_t
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
Mutex object.
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
uint32_t systime_t
System time.
void osSuspendAllTasks(void)
Suspend scheduler activity.
char char_t
Definition: compiler_port.h:43
void * osAllocMem(size_t size)
Allocate a memory block.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
void osDelayTask(systime_t delay)
Delay routine.
void(* OsTaskCode)(void *param)
Task routine.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
void osInitKernel(void)
Kernel initialization.
thread_t * OsTaskId
Task identifier.
OS_TASK OsTaskTcb
Task control block.
unsigned int uint_t
Definition: compiler_port.h:45
uint16_t priority
Definition: dns_common.h:249
#define semaphore_t
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
semaphore_t OsSemaphore
Semaphore object.