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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * @author Oryx Embedded SARL (www.oryx-embedded.com)
24  * @version 1.9.0
25  **/
26 
27 #ifndef _OS_PORT_CHIBIOS_H
28 #define _OS_PORT_CHIBIOS_H
29 
30 //Dependencies
31 #include "ch.h"
32 
33 //Maximum number of tasks that can be dynamically created
34 #ifndef OS_PORT_MAX_TASKS
35  #define OS_PORT_MAX_TASKS 16
36 #elif (OS_PORT_MAX_TASKS < 1)
37  #error OS_PORT_MAX_TASKS parameter is not valid
38 #endif
39 
40 //Task priority (normal)
41 #ifndef OS_TASK_PRIORITY_NORMAL
42  #define OS_TASK_PRIORITY_NORMAL NORMALPRIO
43 #endif
44 
45 //Task priority (high)
46 #ifndef OS_TASK_PRIORITY_HIGH
47  #define OS_TASK_PRIORITY_HIGH HIGHPRIO
48 #endif
49 
50 //Milliseconds to system ticks
51 #ifndef OS_MS_TO_SYSTICKS
52  #define OS_MS_TO_SYSTICKS(n) (n)
53 #endif
54 
55 //System ticks to milliseconds
56 #ifndef OS_SYSTICKS_TO_MS
57  #define OS_SYSTICKS_TO_MS(n) (n)
58 #endif
59 
60 //Enter interrupt service routine
61 #define osEnterIsr() CH_IRQ_PROLOGUE(); chSysLockFromISR()
62 
63 //Leave interrupt service routine
64 #define osExitIsr(flag) chSysUnlockFromISR(); CH_IRQ_EPILOGUE()
65 
66 //Check kernel version
67 #if (CH_KERNEL_MAJOR < 3)
68  #define thread_t Thread
69  #define semaphore_t Semaphore
70  #define binary_semaphore_t BinarySemaphore
71  #define mutex_t Mutex
72  #define chThdTerminatedX chThdTerminated
73  #define chSemObjectInit chSemInit
74  #define chBSemObjectInit chBSemInit
75  #define chMtxObjectInit chMtxInit
76  #define chVTGetSystemTime chTimeNow
77  #define chSysLockFromISR chSysLockFromIsr
78  #define chSysUnlockFromISR chSysUnlockFromIsr
79  #define THD_WORKING_AREA_SIZE THD_WA_SIZE
80  #define MSG_OK RDY_OK
81 #endif
82 
83 //C++ guard
84 #ifdef __cplusplus
85  extern "C" {
86 #endif
87 
88 
89 /**
90  * @brief Task object
91  **/
92 
93 typedef struct
94 {
96 } OsTask;
97 
98 
99 /**
100  * @brief Event object
101  **/
102 
104 
105 
106 /**
107  * @brief Semaphore object
108  **/
109 
111 
112 
113 /**
114  * @brief Mutex object
115  **/
116 
117 typedef mutex_t OsMutex;
118 
119 
120 /**
121  * @brief Task routine
122  **/
123 
124 typedef void (*OsTaskCode)(void *param);
125 
126 
127 //Kernel management
128 void osInitKernel(void);
129 void osStartKernel(void);
130 
131 //Task management
132 bool_t osCreateStaticTask(OsTask *task, const char_t *name, OsTaskCode taskCode,
133  void *param, void *stack, size_t stackSize, int_t priority);
134 
135 OsTask *osCreateTask(const char_t *name, OsTaskCode taskCode,
136  void *param, size_t stackSize, int_t priority);
137 
138 void osDeleteTask(OsTask *task);
139 void osDelayTask(systime_t delay);
140 void osSwitchTask(void);
141 void osSuspendAllTasks(void);
142 void osResumeAllTasks(void);
143 
144 //Event management
146 void osDeleteEvent(OsEvent *event);
147 void osSetEvent(OsEvent *event);
148 void osResetEvent(OsEvent *event);
149 bool_t osWaitForEvent(OsEvent *event, systime_t timeout);
151 
152 //Semaphore management
153 bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count);
154 void osDeleteSemaphore(OsSemaphore *semaphore);
155 bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout);
156 void osReleaseSemaphore(OsSemaphore *semaphore);
157 
158 //Mutex management
160 void osDeleteMutex(OsMutex *mutex);
161 void osAcquireMutex(OsMutex *mutex);
162 void osReleaseMutex(OsMutex *mutex);
163 
164 //System time
166 
167 //Memory management
168 void *osAllocMem(size_t size);
169 void osFreeMem(void *p);
170 
171 //C++ guard
172 #ifdef __cplusplus
173  }
174 #endif
175 
176 #endif
uint16_t priority
Definition: dns_common.h:219
uint32_t systime_t
Definition: compiler_port.h:44
char char_t
Definition: compiler_port.h:41
void osStartKernel(void)
Start kernel.
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
void osResumeAllTasks(void)
Resume scheduler activity.
#define semaphore_t
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
uint8_t p
Definition: ndp.h:295
systime_t osGetSystemTime(void)
Retrieve system time.
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
binary_semaphore_t OsEvent
Event object.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
void osSuspendAllTasks(void)
Suspend scheduler activity.
Event object.
void osFreeMem(void *p)
Release a previously allocated memory block.
#define mutex_t
void osDelayTask(systime_t delay)
Delay routine.
Task object.
signed int int_t
Definition: compiler_port.h:42
char_t name[]
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
mutex_t OsMutex
Mutex object.
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
void osDeleteTask(OsTask *task)
Delete a task.
OsTask * osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a new task.
void(* OsTaskCode)(void *param)
Task routine.
#define binary_semaphore_t
void osInitKernel(void)
Kernel initialization.
void osSwitchTask(void)
Yield control to the next task.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
unsigned int uint_t
Definition: compiler_port.h:43
semaphore_t OsSemaphore
Semaphore object.
Mutex object.
#define thread_t
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
void * osAllocMem(size_t size)
Allocate a memory block.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void osDeleteEvent(OsEvent *event)
Delete an event object.
bool_t osCreateStaticTask(OsTask *task, const char_t *name, OsTaskCode taskCode, void *param, void *stack, size_t stackSize, int_t priority)
Create a static task.
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
Semaphore object.
int bool_t
Definition: compiler_port.h:47
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
#define OsTask
Task object.
thread_t * tp