os_port_chibios.c
Go to the documentation of this file.
1 /**
2  * @file os_port_chibios.c
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 //Switch to the appropriate trace level
28 #define TRACE_LEVEL TRACE_LEVEL_OFF
29 
30 //Dependencies
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "os_port.h"
35 #include "os_port_chibios.h"
36 #include "debug.h"
37 
38 //Variables
39 static OsTask taskTable[OS_PORT_MAX_TASKS];
40 static uint_t *waTable[OS_PORT_MAX_TASKS];
41 
42 
43 /**
44  * @brief Kernel initialization
45  **/
46 
47 void osInitKernel(void)
48 {
49  //Initialize tables
50  memset(taskTable, 0, sizeof(taskTable));
51  memset(waTable, 0, sizeof(waTable));
52 
53  //Kernel initialization
54  chSysInit();
55 }
56 
57 
58 /**
59  * @brief Start kernel
60  **/
61 
62 void osStartKernel(void)
63 {
64  //Terminate the main thread
65  chThdExit(MSG_OK);
66 }
67 
68 
69 /**
70  * @brief Create a static task
71  * @param[out] task Pointer to the task structure
72  * @param[in] name A name identifying the task
73  * @param[in] taskCode Pointer to the task entry function
74  * @param[in] param A pointer to a variable to be passed to the task
75  * @param[in] stack Pointer to the stack
76  * @param[in] stackSize The initial size of the stack, in words
77  * @param[in] priority The priority at which the task should run
78  * @return The function returns TRUE if the task was successfully
79  * created. Otherwise, FALSE is returned
80  **/
81 
83  void *param, void *stack, size_t stackSize, int_t priority)
84 {
85  //Compute the size of the working area in bytes
86  stackSize *= sizeof(uint_t);
87 
88  //Create a new task
89  task->tp = chThdCreateStatic(stack, stackSize,
90  priority, (tfunc_t) taskCode, param);
91 
92  //Check whether the task was successfully created
93  if(task->tp != NULL)
94  return TRUE;
95  else
96  return FALSE;
97 }
98 
99 
100 /**
101  * @brief Create a new task
102  * @param[in] name A name identifying the task
103  * @param[in] taskCode Pointer to the task entry function
104  * @param[in] param A pointer to a variable to be passed to the task
105  * @param[in] stackSize The initial size of the stack, in words
106  * @param[in] priority The priority at which the task should run
107  * @return If the function succeeds, the return value is a pointer to the
108  * new task. If the function fails, the return value is NULL
109  **/
110 
112  void *param, size_t stackSize, int_t priority)
113 {
114  uint_t i;
115  void *wa;
116  OsTask *task = NULL;
117 
118  //Compute the size of the stack in bytes
119  stackSize *= sizeof(uint_t);
120 
121  //Allocate a memory block to hold the working area
122  wa = osAllocMem(THD_WORKING_AREA_SIZE(stackSize));
123 
124  //Successful memory allocation?
125  if(wa != NULL)
126  {
127  //Enter critical section
128  chSysLock();
129 
130  //Loop through task table
131  for(i = 0; i < OS_PORT_MAX_TASKS; i++)
132  {
133  //Check whether the current entry is free
134  if(taskTable[i].tp == NULL)
135  break;
136  }
137 
138  //Any entry available in the table?
139  if(i < OS_PORT_MAX_TASKS)
140  {
141  //Create a new task
142  taskTable[i].tp = chThdCreateI(wa, THD_WORKING_AREA_SIZE(stackSize),
143  priority, (tfunc_t) taskCode, param);
144 
145  //Check whether the task was successfully created
146  if(taskTable[i].tp != NULL)
147  {
148  //Insert the newly created task in the ready list
149  chSchWakeupS(taskTable[i].tp, MSG_OK);
150 
151  //Save task pointer
152  task = &taskTable[i];
153  //Save working area base address
154  waTable[i] = wa;
155 
156  //Leave critical section
157  chSysUnlock();
158  }
159  else
160  {
161  //Leave critical section
162  chSysUnlock();
163  //Clean up side effects
164  osFreeMem(wa);
165  }
166  }
167  else
168  {
169  //Leave critical section
170  chSysUnlock();
171  //No entry available in the table
172  osFreeMem(wa);
173  }
174  }
175 
176  //Return a pointer to the newly created task
177  return task;
178 }
179 
180 
181 /**
182  * @brief Delete a task
183  * @param[in] task Pointer to the task to be deleted
184  **/
185 
186 void osDeleteTask(OsTask *task)
187 {
188  //Delete the specified task
189  if(task == NULL)
190  chThdExit(MSG_OK);
191  else
192  chThdTerminate(task->tp);
193 }
194 
195 
196 /**
197  * @brief Delay routine
198  * @param[in] delay Amount of time for which the calling task should block
199  **/
200 
202 {
203  //Delay the task for the specified duration
204  chThdSleep(OS_MS_TO_SYSTICKS(delay));
205 }
206 
207 
208 /**
209  * @brief Yield control to the next task
210  **/
211 
212 void osSwitchTask(void)
213 {
214  //Force a context switch
215  chThdYield();
216 }
217 
218 
219 /**
220  * @brief Suspend scheduler activity
221  **/
222 
224 {
225  //Suspend scheduler activity
226  chSysLock();
227 }
228 
229 
230 /**
231  * @brief Resume scheduler activity
232  **/
233 
235 {
236  //Resume scheduler activity
237  chSysUnlock();
238 }
239 
240 
241 /**
242  * @brief Create an event object
243  * @param[in] event Pointer to the event object
244  * @return The function returns TRUE if the event object was successfully
245  * created. Otherwise, FALSE is returned
246  **/
247 
249 {
250  //Initialize the binary semaphore object
251  chBSemObjectInit(event, TRUE);
252 
253  //Event successfully created
254  return TRUE;
255 }
256 
257 
258 /**
259  * @brief Delete an event object
260  * @param[in] event Pointer to the event object
261  **/
262 
263 void osDeleteEvent(OsEvent *event)
264 {
265  //No resource to release
266 }
267 
268 
269 /**
270  * @brief Set the specified event object to the signaled state
271  * @param[in] event Pointer to the event object
272  **/
273 
274 void osSetEvent(OsEvent *event)
275 {
276  //Set the specified event to the signaled state
277  chBSemSignal(event);
278 }
279 
280 
281 /**
282  * @brief Set the specified event object to the nonsignaled state
283  * @param[in] event Pointer to the event object
284  **/
285 
286 void osResetEvent(OsEvent *event)
287 {
288  //Force the specified event to the nonsignaled state
289  chBSemReset(event, TRUE);
290 }
291 
292 
293 /**
294  * @brief Wait until the specified event is in the signaled state
295  * @param[in] event Pointer to the event object
296  * @param[in] timeout Timeout interval
297  * @return The function returns TRUE if the state of the specified object is
298  * signaled. FALSE is returned if the timeout interval elapsed
299  **/
300 
302 {
303  msg_t msg;
304 
305  //Wait until the specified event is in the signaled
306  //state or the timeout interval elapses
307  if(timeout == 0)
308  {
309  //Non-blocking call
310  msg = chBSemWaitTimeout(event, TIME_IMMEDIATE);
311  }
312  else if(timeout == INFINITE_DELAY)
313  {
314  //Infinite timeout period
315  msg = chBSemWaitTimeout(event, TIME_INFINITE);
316  }
317  else
318  {
319  //Wait until the specified event becomes set
320  msg = chBSemWaitTimeout(event, OS_MS_TO_SYSTICKS(timeout));
321  }
322 
323  //Check whether the specified event is set
324  if(msg == MSG_OK)
325  return TRUE;
326  else
327  return FALSE;
328 }
329 
330 
331 /**
332  * @brief Set an event object to the signaled state from an interrupt service routine
333  * @param[in] event Pointer to the event object
334  * @return TRUE if setting the event to signaled state caused a task to unblock
335  * and the unblocked task has a priority higher than the currently running task
336  **/
337 
339 {
340  //Set the specified event to the signaled state
341  chBSemSignalI(event);
342 
343  //The return value is not relevant
344  return FALSE;
345 }
346 
347 
348 /**
349  * @brief Create a semaphore object
350  * @param[in] semaphore Pointer to the semaphore object
351  * @param[in] count The maximum count for the semaphore object. This value
352  * must be greater than zero
353  * @return The function returns TRUE if the semaphore was successfully
354  * created. Otherwise, FALSE is returned
355  **/
356 
358 {
359  //Initialize the semaphore object
360  chSemObjectInit(semaphore, count);
361 
362  //Semaphore successfully created
363  return TRUE;
364 }
365 
366 
367 /**
368  * @brief Delete a semaphore object
369  * @param[in] semaphore Pointer to the semaphore object
370  **/
371 
373 {
374  //No resource to release
375 }
376 
377 
378 /**
379  * @brief Wait for the specified semaphore to be available
380  * @param[in] semaphore Pointer to the semaphore object
381  * @param[in] timeout Timeout interval
382  * @return The function returns TRUE if the semaphore is available. FALSE is
383  * returned if the timeout interval elapsed
384  **/
385 
387 {
388  msg_t msg;
389 
390  //Wait until the semaphore is available or the timeout interval elapses
391  if(timeout == 0)
392  {
393  //Non-blocking call
394  msg = chSemWaitTimeout(semaphore, TIME_IMMEDIATE);
395  }
396  else if(timeout == INFINITE_DELAY)
397  {
398  //Infinite timeout period
399  msg = chSemWaitTimeout(semaphore, TIME_INFINITE);
400  }
401  else
402  {
403  //Wait until the specified semaphore becomes available
404  msg = chSemWaitTimeout(semaphore, OS_MS_TO_SYSTICKS(timeout));
405  }
406 
407  //Check whether the specified semaphore is available
408  if(msg == MSG_OK)
409  return TRUE;
410  else
411  return FALSE;
412 }
413 
414 
415 /**
416  * @brief Release the specified semaphore object
417  * @param[in] semaphore Pointer to the semaphore object
418  **/
419 
421 {
422  //Release the semaphore
423  chSemSignal(semaphore);
424 }
425 
426 
427 /**
428  * @brief Create a mutex object
429  * @param[in] mutex Pointer to the mutex object
430  * @return The function returns TRUE if the mutex was successfully
431  * created. Otherwise, FALSE is returned
432  **/
433 
435 {
436  //Initialize the mutex object
437  chMtxObjectInit(mutex);
438 
439  //Mutex successfully created
440  return TRUE;
441 }
442 
443 
444 /**
445  * @brief Delete a mutex object
446  * @param[in] mutex Pointer to the mutex object
447  **/
448 
449 void osDeleteMutex(OsMutex *mutex)
450 {
451  //No resource to release
452 }
453 
454 
455 /**
456  * @brief Acquire ownership of the specified mutex object
457  * @param[in] mutex Pointer to the mutex object
458  **/
459 
461 {
462  //Obtain ownership of the mutex object
463  chMtxLock(mutex);
464 }
465 
466 
467 /**
468  * @brief Release ownership of the specified mutex object
469  * @param[in] mutex Pointer to the mutex object
470  **/
471 
473 {
474  //Release ownership of the mutex object
475 #if (CH_KERNEL_MAJOR < 3)
476  chMtxUnlock();
477 #else
478  chMtxUnlock(mutex);
479 #endif
480 }
481 
482 
483 /**
484  * @brief Retrieve system time
485  * @return Number of milliseconds elapsed since the system was last started
486  **/
487 
489 {
490  systime_t time;
491 
492  //Get current tick count
494 
495  //Convert system ticks to milliseconds
496  return OS_SYSTICKS_TO_MS(time);
497 }
498 
499 
500 /**
501  * @brief Allocate a memory block
502  * @param[in] size Bytes to allocate
503  * @return A pointer to the allocated memory block or NULL if
504  * there is insufficient memory available
505  **/
506 
507 void *osAllocMem(size_t size)
508 {
509  void *p;
510 
511  //Allocate a memory block
512  p = chHeapAlloc(NULL, size);
513 
514  //Debug message
515  TRACE_DEBUG("Allocating %" PRIuSIZE " bytes at 0x%08" PRIXPTR "\r\n", size, (uintptr_t) p);
516 
517  //Return a pointer to the newly allocated memory block
518  return p;
519 }
520 
521 
522 /**
523  * @brief Release a previously allocated memory block
524  * @param[in] p Previously allocated memory block to be freed
525  **/
526 
527 void osFreeMem(void *p)
528 {
529  //Make sure the pointer is valid
530  if(p != NULL)
531  {
532  //Debug message
533  TRACE_DEBUG("Freeing memory at 0x%08" PRIXPTR "\r\n", (uintptr_t) p);
534 
535  //Free memory block
536  chHeapFree(p);
537  }
538 }
539 
540 
541 /**
542  * @brief Idle loop hook
543  **/
544 
545 void osIdleLoopHook(void)
546 {
547  uint_t i;
548 
549  //Loop through task table
550  for(i = 0; i < OS_PORT_MAX_TASKS; i++)
551  {
552  //Check whether current entry is used
553  if(taskTable[i].tp != NULL)
554  {
555  //Wait for task termination
556  if(chThdTerminatedX(taskTable[i].tp))
557  {
558  //Free working area
559  osFreeMem(waTable[i]);
560 
561  //Mark the entry as free
562  waTable[i] = NULL;
563  taskTable[i].tp = NULL;
564  }
565  }
566  }
567 }
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
uint16_t priority
Definition: dns_common.h:219
uint32_t systime_t
Definition: compiler_port.h:44
void osSwitchTask(void)
Yield control to the next task.
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
char char_t
Definition: compiler_port.h:41
#define chVTGetSystemTime
systime_t osGetSystemTime(void)
Retrieve system time.
uint32_t time
void osFreeMem(void *p)
Release a previously allocated memory block.
#define chBSemObjectInit
RTOS abstraction layer (ChibiOS/RT)
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore 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 osResumeAllTasks(void)
Resume scheduler activity.
Debugging facilities.
uint8_t p
Definition: ndp.h:295
#define MSG_OK
OsTask * osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a new task.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
Event object.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
#define TRUE
Definition: os_port.h:48
void osSuspendAllTasks(void)
Suspend scheduler activity.
void osInitKernel(void)
Kernel initialization.
#define THD_WORKING_AREA_SIZE
Task object.
signed int int_t
Definition: compiler_port.h:42
char_t name[]
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
#define OS_MS_TO_SYSTICKS(n)
#define INFINITE_DELAY
Definition: os_port.h:72
RTOS abstraction layer.
void osDelayTask(systime_t delay)
Delay routine.
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define chSemObjectInit
void(* OsTaskCode)(void *param)
Task routine.
#define OS_PORT_MAX_TASKS
void * osAllocMem(size_t size)
Allocate a memory block.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
#define chThdTerminatedX
unsigned int uint_t
Definition: compiler_port.h:43
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define OS_SYSTICKS_TO_MS(n)
void osIdleLoopHook(void)
Idle loop hook.
#define PRIuSIZE
Definition: compiler_port.h:72
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
Mutex object.
void osDeleteEvent(OsEvent *event)
Delete an event object.
void osDeleteTask(OsTask *task)
Delete a task.
Semaphore object.
void osStartKernel(void)
Start kernel.
#define FALSE
Definition: os_port.h:44
int bool_t
Definition: compiler_port.h:47
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
#define chMtxObjectInit
thread_t * tp
#define TRACE_DEBUG(...)
Definition: debug.h:98