os_port_freertos.c
Go to the documentation of this file.
1 /**
2  * @file os_port_freertos.c
3  * @brief RTOS abstraction layer (FreeRTOS)
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 "os_port.h"
34 #include "os_port_freertos.h"
35 #include "debug.h"
36 
37 
38 /**
39  * @brief Kernel initialization
40  **/
41 
42 void osInitKernel(void)
43 {
44 }
45 
46 
47 /**
48  * @brief Start kernel
49  **/
50 
51 void osStartKernel(void)
52 {
53  //Start the scheduler
54  vTaskStartScheduler();
55 }
56 
57 
58 /**
59  * @brief Create a new task
60  * @param[in] name A name identifying the task
61  * @param[in] taskCode Pointer to the task entry function
62  * @param[in] param A pointer to a variable to be passed to the task
63  * @param[in] stackSize The initial size of the stack, in words
64  * @param[in] priority The priority at which the task should run
65  * @return If the function succeeds, the return value is a pointer to the
66  * new task. If the function fails, the return value is NULL
67  **/
68 
70  void *param, size_t stackSize, int_t priority)
71 {
72  portBASE_TYPE status;
73  xTaskHandle task = NULL;
74 
75  //Create a new task
76  status = xTaskCreate((pdTASK_CODE) taskCode,
77  name, stackSize, param, priority, &task);
78 
79  //Check whether the task was successfully created
80  if(status == pdPASS)
81  return task;
82  else
83  return NULL;
84 }
85 
86 
87 /**
88  * @brief Delete a task
89  * @param[in] task Pointer to the task to be deleted
90  **/
91 
92 void osDeleteTask(OsTask *task)
93 {
94  //Delete the specified task
95  vTaskDelete((xTaskHandle) task);
96 }
97 
98 
99 /**
100  * @brief Delay routine
101  * @param[in] delay Amount of time for which the calling task should block
102  **/
103 
105 {
106  //Delay the task for the specified duration
107  vTaskDelay(OS_MS_TO_SYSTICKS(delay));
108 }
109 
110 
111 /**
112  * @brief Yield control to the next task
113  **/
114 
115 void osSwitchTask(void)
116 {
117  //Force a context switch
118  taskYIELD();
119 }
120 
121 
122 /**
123  * @brief Suspend scheduler activity
124  **/
125 
127 {
128  //Make sure the operating system is running
129  if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
130  {
131  //Suspend all tasks
132  vTaskSuspendAll();
133  }
134 }
135 
136 
137 /**
138  * @brief Resume scheduler activity
139  **/
140 
142 {
143  //Make sure the operating system is running
144  if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
145  {
146  //Resume all tasks
147  xTaskResumeAll();
148  }
149 }
150 
151 
152 /**
153  * @brief Create an event object
154  * @param[in] event Pointer to the event object
155  * @return The function returns TRUE if the event object was successfully
156  * created. Otherwise, FALSE is returned
157  **/
158 
160 {
161  //Create a binary semaphore
162  vSemaphoreCreateBinary(event->handle);
163 
164  //Check whether the returned handle is valid
165  if(event->handle != NULL)
166  {
167  //Force the event to the nonsignaled state
168  xSemaphoreTake(event->handle, 0);
169  //Event successfully created
170  return TRUE;
171  }
172  else
173  {
174  //Failed to create event object
175  return FALSE;
176  }
177 }
178 
179 
180 /**
181  * @brief Delete an event object
182  * @param[in] event Pointer to the event object
183  **/
184 
185 void osDeleteEvent(OsEvent *event)
186 {
187  //Make sure the handle is valid
188  if(event->handle != NULL)
189  {
190  //Properly dispose the event object
191  vSemaphoreDelete(event->handle);
192  }
193 }
194 
195 
196 /**
197  * @brief Set the specified event object to the signaled state
198  * @param[in] event Pointer to the event object
199  **/
200 
201 void osSetEvent(OsEvent *event)
202 {
203  //Set the specified event to the signaled state
204  xSemaphoreGive(event->handle);
205 }
206 
207 
208 /**
209  * @brief Set the specified event object to the nonsignaled state
210  * @param[in] event Pointer to the event object
211  **/
212 
213 void osResetEvent(OsEvent *event)
214 {
215  //Force the specified event to the nonsignaled state
216  xSemaphoreTake(event->handle, 0);
217 }
218 
219 
220 /**
221  * @brief Wait until the specified event is in the signaled state
222  * @param[in] event Pointer to the event object
223  * @param[in] timeout Timeout interval
224  * @return The function returns TRUE if the state of the specified object is
225  * signaled. FALSE is returned if the timeout interval elapsed
226  **/
227 
229 {
230  portBASE_TYPE ret;
231 
232  //Wait until the specified event is in the signaled state
233  if(timeout == INFINITE_DELAY)
234  {
235  //Infinite timeout period
236  ret = xSemaphoreTake(event->handle, portMAX_DELAY);
237  }
238  else
239  {
240  //Wait for the specified time interval
241  ret = xSemaphoreTake(event->handle, OS_MS_TO_SYSTICKS(timeout));
242  }
243 
244  //The return value tells whether the event is set
245  return ret;
246 }
247 
248 
249 /**
250  * @brief Set an event object to the signaled state from an interrupt service routine
251  * @param[in] event Pointer to the event object
252  * @return TRUE if setting the event to signaled state caused a task to unblock
253  * and the unblocked task has a priority higher than the currently running task
254  **/
255 
257 {
258  portBASE_TYPE flag = FALSE;
259 
260  //Set the specified event to the signaled state
261  xSemaphoreGiveFromISR(event->handle, &flag);
262 
263  //A higher priority task has been woken?
264  return flag;
265 }
266 
267 
268 /**
269  * @brief Create a semaphore object
270  * @param[in] semaphore Pointer to the semaphore object
271  * @param[in] count The maximum count for the semaphore object. This value
272  * must be greater than zero
273  * @return The function returns TRUE if the semaphore was successfully
274  * created. Otherwise, FALSE is returned
275  **/
276 
278 {
279  //Create a semaphore
280  semaphore->handle = xSemaphoreCreateCounting(count, count);
281 
282  //Check whether the returned handle is valid
283  if(semaphore->handle != NULL)
284  return TRUE;
285  else
286  return FALSE;
287 }
288 
289 
290 /**
291  * @brief Delete a semaphore object
292  * @param[in] semaphore Pointer to the semaphore object
293  **/
294 
296 {
297  //Make sure the handle is valid
298  if(semaphore->handle != NULL)
299  {
300  //Properly dispose the specified semaphore
301  vSemaphoreDelete(semaphore->handle);
302  }
303 }
304 
305 
306 /**
307  * @brief Wait for the specified semaphore to be available
308  * @param[in] semaphore Pointer to the semaphore object
309  * @param[in] timeout Timeout interval
310  * @return The function returns TRUE if the semaphore is available. FALSE is
311  * returned if the timeout interval elapsed
312  **/
313 
315 {
316  portBASE_TYPE ret;
317 
318  //Wait until the specified semaphore becomes available
319  if(timeout == INFINITE_DELAY)
320  {
321  //Infinite timeout period
322  ret = xSemaphoreTake(semaphore->handle, portMAX_DELAY);
323  }
324  else
325  {
326  //Wait for the specified time interval
327  ret = xSemaphoreTake(semaphore->handle, OS_MS_TO_SYSTICKS(timeout));
328  }
329 
330  //The return value tells whether the semaphore is available
331  return ret;
332 }
333 
334 
335 /**
336  * @brief Release the specified semaphore object
337  * @param[in] semaphore Pointer to the semaphore object
338  **/
339 
341 {
342  //Release the semaphore
343  xSemaphoreGive(semaphore->handle);
344 }
345 
346 
347 /**
348  * @brief Create a mutex object
349  * @param[in] mutex Pointer to the mutex object
350  * @return The function returns TRUE if the mutex was successfully
351  * created. Otherwise, FALSE is returned
352  **/
353 
355 {
356  //Create a mutex object
357  mutex->handle = xSemaphoreCreateMutex();
358 
359  //Check whether the returned handle is valid
360  if(mutex->handle != NULL)
361  return TRUE;
362  else
363  return FALSE;
364 }
365 
366 
367 /**
368  * @brief Delete a mutex object
369  * @param[in] mutex Pointer to the mutex object
370  **/
371 
372 void osDeleteMutex(OsMutex *mutex)
373 {
374  //Make sure the handle is valid
375  if(mutex->handle != NULL)
376  {
377  //Properly dispose the specified mutex
378  vSemaphoreDelete(mutex->handle);
379  }
380 }
381 
382 
383 /**
384  * @brief Acquire ownership of the specified mutex object
385  * @param[in] mutex Pointer to the mutex object
386  **/
387 
389 {
390  //Obtain ownership of the mutex object
391  xSemaphoreTake(mutex->handle, portMAX_DELAY);
392 }
393 
394 
395 /**
396  * @brief Release ownership of the specified mutex object
397  * @param[in] mutex Pointer to the mutex object
398  **/
399 
401 {
402  //Release ownership of the mutex object
403  xSemaphoreGive(mutex->handle);
404 }
405 
406 
407 /**
408  * @brief Retrieve system time
409  * @return Number of milliseconds elapsed since the system was last started
410  **/
411 
413 {
414  systime_t time;
415 
416  //Get current tick count
417  time = xTaskGetTickCount();
418 
419  //Convert system ticks to milliseconds
420  return OS_SYSTICKS_TO_MS(time);
421 }
422 
423 
424 /**
425  * @brief Allocate a memory block
426  * @param[in] size Bytes to allocate
427  * @return A pointer to the allocated memory block or NULL if
428  * there is insufficient memory available
429  **/
430 
431 void *osAllocMem(size_t size)
432 {
433  void *p;
434 
435  //Allocate a memory block
436  p = pvPortMalloc(size);
437 
438  //Debug message
439  TRACE_DEBUG("Allocating %" PRIuSIZE " bytes at 0x%08" PRIXPTR "\r\n", size, (uintptr_t) p);
440 
441  //Return a pointer to the newly allocated memory block
442  return p;
443 }
444 
445 
446 /**
447  * @brief Release a previously allocated memory block
448  * @param[in] p Previously allocated memory block to be freed
449  **/
450 
451 void osFreeMem(void *p)
452 {
453  //Make sure the pointer is valid
454  if(p != NULL)
455  {
456  //Debug message
457  TRACE_DEBUG("Freeing memory at 0x%08" PRIXPTR "\r\n", (uintptr_t) p);
458 
459  //Free memory block
460  vPortFree(p);
461  }
462 }
463 
464 
465 #if 0
466 
467 /**
468  * @brief FreeRTOS stack overflow hook
469  **/
470 
471 void vApplicationStackOverflowHook(xTaskHandle pxTask, char *pcTaskName)
472 {
473  (void) pcTaskName;
474  (void) pxTask;
475 
476  taskDISABLE_INTERRUPTS();
477  while(1);
478 }
479 
480 
481 /**
482  * @brief Trap FreeRTOS errors
483  **/
484 
485 void vAssertCalled(const char *pcFile, unsigned long ulLine)
486 {
487  volatile unsigned long ul = 0;
488 
489  (void) pcFile;
490  (void) ulLine;
491 
492  taskENTER_CRITICAL();
493 
494  //Set ul to a non-zero value using the debugger to step out of this function
495  while(ul == 0)
496  {
497  portNOP();
498  }
499 
500  taskEXIT_CRITICAL();
501 }
502 
503 #endif
uint16_t priority
Definition: dns_common.h:219
uint32_t systime_t
Definition: compiler_port.h:44
void osDeleteTask(OsTask *task)
Delete a task.
char char_t
Definition: compiler_port.h:41
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
uint32_t time
bool_t osCreateEvent(OsEvent *event)
Create an event object.
Debugging facilities.
uint8_t p
Definition: ndp.h:295
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
xSemaphoreHandle handle
void osStartKernel(void)
Start kernel.
xSemaphoreHandle handle
void osDelayTask(systime_t delay)
Delay routine.
Event object.
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
systime_t osGetSystemTime(void)
Retrieve system time.
#define TRUE
Definition: os_port.h:48
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
Task object.
signed int int_t
Definition: compiler_port.h:42
char_t name[]
#define OS_MS_TO_SYSTICKS(n)
#define INFINITE_DELAY
Definition: os_port.h:72
RTOS abstraction layer.
void osSuspendAllTasks(void)
Suspend scheduler activity.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
void osFreeMem(void *p)
Release a previously allocated memory block.
void osResumeAllTasks(void)
Resume scheduler activity.
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
void(* OsTaskCode)(void *param)
Task routine.
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
unsigned int uint_t
Definition: compiler_port.h:43
#define OS_SYSTICKS_TO_MS(n)
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
#define PRIuSIZE
Definition: compiler_port.h:72
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
Mutex object.
xSemaphoreHandle handle
void osDeleteEvent(OsEvent *event)
Delete an event object.
RTOS abstraction layer (FreeRTOS)
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
void osSwitchTask(void)
Yield control to the next task.
void * osAllocMem(size_t size)
Allocate a memory block.
Semaphore object.
#define FALSE
Definition: os_port.h:44
int bool_t
Definition: compiler_port.h:47
void osInitKernel(void)
Kernel initialization.
#define TRACE_DEBUG(...)
Definition: debug.h:98
OsTask * osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a new task.