osal-FreeRTOS  0.2.2
osal-FreeRTOS
osal_FreeRTOS.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2017 IS2T. All rights reserved
5  * This library is provided in source code for use, modification and test, subject to license terms
6  * Any modification of the source code will break IS2T warranties on the whole library
7  */
8 
17 #include <stdint.h>
18 #include <string.h>
19 #include "osal.h"
20 #include "FreeRTOS.h"
21 #include "task.h"
22 #include "semphr.h"
23 
24 
25 static TickType_t OSAL_FreeRTOS_convert_time_to_tick(uint32_t milliseconds);
26 
39 OSAL_status_t OSAL_task_create(OSAL_task_entry_point_t entry_point, uint8_t* name, OSAL_task_stack_t stack, int32_t priority, void* parameters, OSAL_task_handle_t* handle)
40 {
41  if(handle == NULL)
42  {
43  return OSAL_WRONG_ARGS;
44  }
45 
46  int32_t stack_size = (int)stack;
47 
48  if(xTaskCreate((TaskFunction_t)entry_point, (char const*) name,
49  (stack_size/(sizeof( portSTACK_TYPE ))),
50  parameters,
51  (unsigned portBASE_TYPE) priority,
52  (TaskHandle_t*) handle) != pdPASS)
53  {
54  return OSAL_ERROR;
55  }
56 
57  return OSAL_OK;
58 }
59 
67 OSAL_status_t OSAL_task_delete(OSAL_task_handle_t* handle)
68 {
69  if(handle == NULL)
70  {
71  return OSAL_WRONG_ARGS;
72  }
73 
74  vTaskDelete((TaskHandle_t)*handle);
75 
76  return OSAL_OK;
77 }
78 
86 OSAL_status_t OSAL_queue_create(uint8_t* name, uint32_t size, OSAL_queue_handle_t* handle)
87 {
88  if(handle == NULL)
89  {
90  return OSAL_WRONG_ARGS;
91  }
92 
93  //Create Queue
94  *handle = xQueueCreate(size, sizeof(void*));
95  if((*handle) == NULL)
96  {
97  return OSAL_ERROR;
98  }
99 
100  return OSAL_OK;
101 }
102 
110 OSAL_status_t OSAL_queue_delete(OSAL_queue_handle_t* handle)
111 {
112  if(handle == NULL)
113  {
114  return OSAL_WRONG_ARGS;
115  }
116 
117  vQueueDelete(*handle);
118  return OSAL_OK;
119 }
120 
129 OSAL_status_t OSAL_queue_post(OSAL_queue_handle_t* handle, void* msg)
130 {
131  if(xQueueSend(*handle, &msg, 0) != pdTRUE)
132  {
133  return OSAL_NOMEM;
134  }
135 
136  return OSAL_OK;
137 }
138 
148 OSAL_status_t OSAL_queue_fetch(OSAL_queue_handle_t* handle, void** msg, uint32_t timeout)
149 {
150  if(handle == NULL)
151  {
152  return OSAL_WRONG_ARGS;
153  }
154 
155  TickType_t timeout_in_tick = OSAL_FreeRTOS_convert_time_to_tick(timeout);
156  if(xQueueReceive((QueueHandle_t)*handle, msg, timeout_in_tick) != pdTRUE)
157  {
158  return OSAL_ERROR;
159  }
160  return OSAL_OK;
161 }
162 
173 OSAL_status_t OSAL_counter_semaphore_create(uint8_t* name, uint32_t initial_count, uint32_t max_count, OSAL_counter_semaphore_handle_t* handle)
174 {
175  if(handle == NULL)
176  {
177  return OSAL_WRONG_ARGS;
178  }
179 
180  *handle = (OSAL_binary_semaphore_handle_t)xSemaphoreCreateCounting(max_count, initial_count);
181  if((*handle) == NULL)
182  {
183  return OSAL_NOMEM;
184  }
185  return OSAL_OK;
186 }
187 
195 OSAL_status_t OSAL_counter_semaphore_delete(OSAL_counter_semaphore_handle_t* handle)
196 {
197  if(handle == NULL)
198  {
199  return OSAL_WRONG_ARGS;
200  }
201 
202  vSemaphoreDelete((SemaphoreHandle_t)*handle);
203  return OSAL_OK;
204 }
205 
216 OSAL_status_t OSAL_counter_semaphore_take(OSAL_counter_semaphore_handle_t* handle, uint32_t timeout)
217 {
218  if(handle == NULL)
219  {
220  return OSAL_WRONG_ARGS;
221  }
222 
223  TickType_t timeout_in_tick = OSAL_FreeRTOS_convert_time_to_tick(timeout);
224  if(xSemaphoreTake((SemaphoreHandle_t)*handle, timeout_in_tick) != pdTRUE)
225  {
226  return OSAL_ERROR;
227  }
228  return OSAL_OK;
229 }
230 
239 OSAL_status_t OSAL_counter_semaphore_give(OSAL_counter_semaphore_handle_t* handle)
240 {
241  if(handle == NULL)
242  {
243  return OSAL_WRONG_ARGS;
244  }
245 
246  if(xSemaphoreGive((SemaphoreHandle_t)*handle) != pdTRUE)
247  {
248  return OSAL_ERROR;
249  }
250  return OSAL_OK;
251 }
252 
262 OSAL_status_t OSAL_binary_semaphore_create(uint8_t* name, uint32_t initial_count, OSAL_binary_semaphore_handle_t* handle)
263 {
264  if(handle == NULL)
265  {
266  return OSAL_WRONG_ARGS;
267  }
268 
269  *handle = (OSAL_binary_semaphore_handle_t)xSemaphoreCreateBinary();
270  if((*handle) == NULL)
271  {
272  return OSAL_NOMEM;
273  }
274  return OSAL_OK;
275 }
276 
284 OSAL_status_t OSAL_binary_semaphore_delete(OSAL_binary_semaphore_handle_t* handle)
285 {
286  if(handle == NULL)
287  {
288  return OSAL_WRONG_ARGS;
289  }
290 
291  vSemaphoreDelete((SemaphoreHandle_t)*handle);
292  return OSAL_OK;
293 }
294 
305 OSAL_status_t OSAL_binary_semaphore_take(OSAL_binary_semaphore_handle_t* handle, uint32_t timeout)
306 {
307  if(handle == NULL)
308  {
309  return OSAL_WRONG_ARGS;
310  }
311 
312  TickType_t timeout_in_tick = OSAL_FreeRTOS_convert_time_to_tick(timeout);
313  if(xSemaphoreTake((SemaphoreHandle_t)*handle, timeout_in_tick) != pdTRUE)
314  {
315  return OSAL_ERROR;
316  }
317  return OSAL_OK;
318 }
319 
328 OSAL_status_t OSAL_binary_semaphore_give(OSAL_binary_semaphore_handle_t* handle)
329 {
330  if(handle == NULL)
331  {
332  return OSAL_WRONG_ARGS;
333  }
334 
335  if(xSemaphoreGive((SemaphoreHandle_t)*handle) != pdTRUE)
336  {
337  return OSAL_ERROR;
338  }
339  return OSAL_OK;
340 }
341 
350 OSAL_status_t OSAL_mutex_create(uint8_t* name, OSAL_mutex_handle_t* handle)
351 {
352  if(handle == NULL)
353  {
354  return OSAL_WRONG_ARGS;
355  }
356 
357  *handle = (OSAL_mutex_handle_t)xSemaphoreCreateMutex();
358  if((*handle) == NULL)
359  {
360  return OSAL_NOMEM;
361  }
362  return OSAL_OK;
363 }
364 
372 OSAL_status_t OSAL_mutex_delete(OSAL_mutex_handle_t* handle)
373 {
374  if(handle == NULL)
375  {
376  return OSAL_WRONG_ARGS;
377  }
378 
379  vSemaphoreDelete((SemaphoreHandle_t)*handle);
380  return OSAL_OK;
381 }
382 
391 OSAL_status_t OSAL_mutex_take(OSAL_mutex_handle_t* handle, uint32_t timeout)
392 {
393  if(handle == NULL)
394  {
395  return OSAL_WRONG_ARGS;
396  }
397 
398  TickType_t timeout_in_tick = OSAL_FreeRTOS_convert_time_to_tick(timeout);
399  if(xSemaphoreTake((SemaphoreHandle_t)*handle, timeout_in_tick) != pdTRUE)
400  {
401  return OSAL_ERROR;
402  }
403  return OSAL_OK;
404 }
405 
413 OSAL_status_t OSAL_mutex_give(OSAL_mutex_handle_t* handle)
414 {
415  if(handle == NULL)
416  {
417  return OSAL_WRONG_ARGS;
418  }
419 
420  if(xSemaphoreGive((SemaphoreHandle_t)*handle) != pdTRUE)
421  {
422  return OSAL_ERROR;
423  }
424  return OSAL_OK;
425 }
426 
436 {
437  vTaskSuspendAll();
438  return OSAL_OK;
439 }
440 
447 OSAL_status_t OSAL_enable_context_switching(void)
448 {
449  xTaskResumeAll();
450  return OSAL_OK;
451 }
452 
460 OSAL_status_t OSAL_sleep(uint32_t milliseconds)
461 {
462  TickType_t delay_in_ticks = OSAL_FreeRTOS_convert_time_to_tick(milliseconds);
463 
464  vTaskDelay(delay_in_ticks);
465  return OSAL_OK;
466 }
467 
468 static TickType_t OSAL_FreeRTOS_convert_time_to_tick(uint32_t milliseconds)
469 {
470  TickType_t time_to_tick_timeout;
471 
472  if(milliseconds == OSAL_INFINITE_TIME)
473  {
474  time_to_tick_timeout = portMAX_DELAY;
475  }
476  else
477  {
478  time_to_tick_timeout = milliseconds / portTICK_PERIOD_MS;
479  }
480  return time_to_tick_timeout;
481 }
OSAL_status_t OSAL_queue_create(uint8_t *name, uint32_t size, OSAL_queue_handle_t *handle)
Create an OS queue with a predefined queue size.
Definition: osal_FreeRTOS.c:86
OSAL_status_t OSAL_task_delete(OSAL_task_handle_t *handle)
Delete an OS task and start it.
Definition: osal_FreeRTOS.c:67
OSAL_status_t OSAL_queue_post(OSAL_queue_handle_t *handle, void *msg)
Post a message in an OS queue.
OSAL_status_t OSAL_enable_context_switching(void)
Reenable the OS scheduling that was disabled by OSAL_disable_context_switching. This method may be ca...
OSAL_status_t OSAL_task_create(OSAL_task_entry_point_t entry_point, uint8_t *name, OSAL_task_stack_t stack, int32_t priority, void *parameters, OSAL_task_handle_t *handle)
Create an OS task and start it.
Definition: osal_FreeRTOS.c:39
OSAL_status_t OSAL_sleep(uint32_t milliseconds)
Asleep the current task during specified number of milliseconds.
OSAL_status_t OSAL_queue_fetch(OSAL_queue_handle_t *handle, void **msg, uint32_t timeout)
Fetch a message from an OS queue. Blocks until a message arrived or a timeout occurred.
OSAL_status_t OSAL_binary_semaphore_give(OSAL_binary_semaphore_handle_t *handle)
Give operation on OS binary semaphore. Increase the binary semaphore count value by 1 and unblock the...
OSAL_status_t OSAL_counter_semaphore_delete(OSAL_counter_semaphore_handle_t *handle)
Delete an OS counter semaphore.
OSAL_status_t OSAL_counter_semaphore_create(uint8_t *name, uint32_t initial_count, uint32_t max_count, OSAL_counter_semaphore_handle_t *handle)
Create an OS counter semaphore with a semaphore count initial value.
OSAL_status_t OSAL_disable_context_switching(void)
Disable the OS scheduler context switching. Prevent the OS from scheduling the current thread calling...
OSAL_status_t OSAL_counter_semaphore_take(OSAL_counter_semaphore_handle_t *handle, uint32_t timeout)
Take operation on OS counter semaphore. Block the current task until counter semaphore become availab...
int32_t OSAL_task_stack_t
OS task stack.
OSAL_status_t OSAL_binary_semaphore_delete(OSAL_binary_semaphore_handle_t *handle)
Delete an OS binary semaphore.
OSAL_status_t OSAL_binary_semaphore_create(uint8_t *name, uint32_t initial_count, OSAL_binary_semaphore_handle_t *handle)
Create an OS binary semaphore with a semaphore count initial value (0 or 1).
OSAL_status_t OSAL_counter_semaphore_give(OSAL_counter_semaphore_handle_t *handle)
Give operation on OS counter semaphore. Increase the counter semaphore count value by 1 and unblock t...
OSAL_status_t OSAL_queue_delete(OSAL_queue_handle_t *handle)
Delete an OS queue.
OSAL_status_t OSAL_mutex_give(OSAL_mutex_handle_t *handle)
Give operation on OS mutex.
OSAL_status_t OSAL_binary_semaphore_take(OSAL_binary_semaphore_handle_t *handle, uint32_t timeout)
Take operation on OS binary semaphore. Block the current task until binary semaphore become available...
OSAL_status_t OSAL_mutex_take(OSAL_mutex_handle_t *handle, uint32_t timeout)
Take operation on OS mutex.
OSAL_status_t OSAL_mutex_create(uint8_t *name, OSAL_mutex_handle_t *handle)
Create an OS mutex.
OSAL_status_t OSAL_mutex_delete(OSAL_mutex_handle_t *handle)
Delete an OS mutex.