osal-FreeRTOS  0.2.2
osal-FreeRTOS
Functions
osal_FreeRTOS.c File Reference

OS Abstraction Layer FreeRTOS implementation. More...

#include <stdint.h>
#include <string.h>
#include "osal.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

Go to the source code of this file.

Functions

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. More...
 
OSAL_status_t OSAL_task_delete (OSAL_task_handle_t *handle)
 Delete an OS task and start it. More...
 
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. More...
 
OSAL_status_t OSAL_queue_delete (OSAL_queue_handle_t *handle)
 Delete an OS queue. More...
 
OSAL_status_t OSAL_queue_post (OSAL_queue_handle_t *handle, void *msg)
 Post a message in an OS queue. More...
 
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. More...
 
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. More...
 
OSAL_status_t OSAL_counter_semaphore_delete (OSAL_counter_semaphore_handle_t *handle)
 Delete an OS counter semaphore. More...
 
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 available or timeout occurred. Decrease the counter semaphore count value by 1 and block the current task if count value equals to 0. More...
 
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 the current task if count value. equals to 0. More...
 
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). More...
 
OSAL_status_t OSAL_binary_semaphore_delete (OSAL_binary_semaphore_handle_t *handle)
 Delete an OS binary semaphore. More...
 
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 or timeout occurred. Decrease the binary semaphore count value by 1 and block the current task if count value equals to 0. More...
 
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 current task if count value. equals to 0. More...
 
OSAL_status_t OSAL_mutex_create (uint8_t *name, OSAL_mutex_handle_t *handle)
 Create an OS mutex. More...
 
OSAL_status_t OSAL_mutex_delete (OSAL_mutex_handle_t *handle)
 Delete an OS mutex. More...
 
OSAL_status_t OSAL_mutex_take (OSAL_mutex_handle_t *handle, uint32_t timeout)
 Take operation on OS mutex. More...
 
OSAL_status_t OSAL_mutex_give (OSAL_mutex_handle_t *handle)
 Give operation on OS mutex. More...
 
OSAL_status_t OSAL_disable_context_switching (void)
 Disable the OS scheduler context switching. Prevent the OS from scheduling the current thread calling OSAL_disable_context_switching while the OS scheduling is already disable has an undefined behavior. This method may be called from an interrupt. More...
 
OSAL_status_t OSAL_enable_context_switching (void)
 Reenable the OS scheduling that was disabled by OSAL_disable_context_switching. This method may be called from an interrupt. More...
 
OSAL_status_t OSAL_sleep (uint32_t milliseconds)
 Asleep the current task during specified number of milliseconds. More...
 

Detailed Description

OS Abstraction Layer FreeRTOS implementation.

Author
MicroEJ Developer Team
Version
0.2.2
Date
8 December 2020

Definition in file osal_FreeRTOS.c.

Function Documentation

§ OSAL_binary_semaphore_create()

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).

Parameters
[in]namecounter semaphore name
[in]initial_countcounter semaphore initial count value
[in,out]handlepointer on a binary semaphore handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 262 of file osal_FreeRTOS.c.

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 }

§ OSAL_binary_semaphore_delete()

OSAL_status_t OSAL_binary_semaphore_delete ( OSAL_binary_semaphore_handle_t *  handle)

Delete an OS binary semaphore.

Parameters
[in]handlepointer on the binary semaphore handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 284 of file osal_FreeRTOS.c.

285 {
286  if(handle == NULL)
287  {
288  return OSAL_WRONG_ARGS;
289  }
290 
291  vSemaphoreDelete((SemaphoreHandle_t)*handle);
292  return OSAL_OK;
293 }

§ OSAL_binary_semaphore_give()

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 current task if count value. equals to 0.

Parameters
[in]handlepointer on the binary semaphore handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 328 of file osal_FreeRTOS.c.

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 }

§ OSAL_binary_semaphore_take()

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 or timeout occurred. Decrease the binary semaphore count value by 1 and block the current task if count value equals to 0.

Parameters
[in]handlepointer on the binary semaphore handle
[in]timeoutmaximum time to wait until the binary semaphore become available
Returns
operation status (
See also
OSAL_status_t)

Definition at line 305 of file osal_FreeRTOS.c.

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 }

§ OSAL_counter_semaphore_create()

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.

Parameters
[in]namecounter semaphore name
[in]initial_countcounter semaphore initial count value
[in]max_countcounter semaphore maximum count value
[in,out]handlepointer on a counter semaphore handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 173 of file osal_FreeRTOS.c.

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 }

§ OSAL_counter_semaphore_delete()

OSAL_status_t OSAL_counter_semaphore_delete ( OSAL_counter_semaphore_handle_t *  handle)

Delete an OS counter semaphore.

Parameters
[in]handlepointer on the counter semaphore handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 195 of file osal_FreeRTOS.c.

196 {
197  if(handle == NULL)
198  {
199  return OSAL_WRONG_ARGS;
200  }
201 
202  vSemaphoreDelete((SemaphoreHandle_t)*handle);
203  return OSAL_OK;
204 }

§ OSAL_counter_semaphore_give()

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 the current task if count value. equals to 0.

Parameters
[in]handlepointer on the counter semaphore handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 239 of file osal_FreeRTOS.c.

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 }

§ OSAL_counter_semaphore_take()

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 available or timeout occurred. Decrease the counter semaphore count value by 1 and block the current task if count value equals to 0.

Parameters
[in]handlepointer on the counter semaphore handle
[in]timeoutmaximum time to wait until the counter semaphore become available
Returns
operation status (
See also
OSAL_status_t)

Definition at line 216 of file osal_FreeRTOS.c.

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 }

§ OSAL_disable_context_switching()

OSAL_status_t OSAL_disable_context_switching ( void  )

Disable the OS scheduler context switching. Prevent the OS from scheduling the current thread calling OSAL_disable_context_switching while the OS scheduling is already disable has an undefined behavior. This method may be called from an interrupt.

Returns
operation status (
See also
OSAL_status_t)

Definition at line 435 of file osal_FreeRTOS.c.

436 {
437  vTaskSuspendAll();
438  return OSAL_OK;
439 }

§ OSAL_enable_context_switching()

OSAL_status_t OSAL_enable_context_switching ( void  )

Reenable the OS scheduling that was disabled by OSAL_disable_context_switching. This method may be called from an interrupt.

Returns
operation status (
See also
OSAL_status_t)

Definition at line 447 of file osal_FreeRTOS.c.

448 {
449  xTaskResumeAll();
450  return OSAL_OK;
451 }

§ OSAL_mutex_create()

OSAL_status_t OSAL_mutex_create ( uint8_t *  name,
OSAL_mutex_handle_t *  handle 
)

Create an OS mutex.

Parameters
[in]namemutex name
[in,out]handlepointer on a mutex handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 350 of file osal_FreeRTOS.c.

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 }

§ OSAL_mutex_delete()

OSAL_status_t OSAL_mutex_delete ( OSAL_mutex_handle_t *  handle)

Delete an OS mutex.

Parameters
[in]handlepointer on the mutex handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 372 of file osal_FreeRTOS.c.

373 {
374  if(handle == NULL)
375  {
376  return OSAL_WRONG_ARGS;
377  }
378 
379  vSemaphoreDelete((SemaphoreHandle_t)*handle);
380  return OSAL_OK;
381 }

§ OSAL_mutex_give()

OSAL_status_t OSAL_mutex_give ( OSAL_mutex_handle_t *  handle)

Give operation on OS mutex.

Parameters
[in]handlepointer on the mutex handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 413 of file osal_FreeRTOS.c.

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 }

§ OSAL_mutex_take()

OSAL_status_t OSAL_mutex_take ( OSAL_mutex_handle_t *  handle,
uint32_t  timeout 
)

Take operation on OS mutex.

Parameters
[in]handlepointer on the mutex handle
[in]timeoutmaximum time to wait until the mutex become available
Returns
operation status (
See also
OSAL_status_t)

Definition at line 391 of file osal_FreeRTOS.c.

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 }

§ OSAL_queue_create()

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.

Parameters
[in,out]handlepointer on a queue handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 86 of file osal_FreeRTOS.c.

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 }

§ OSAL_queue_delete()

OSAL_status_t OSAL_queue_delete ( OSAL_queue_handle_t *  handle)

Delete an OS queue.

Parameters
[in]handlepointer on the queue handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 110 of file osal_FreeRTOS.c.

111 {
112  if(handle == NULL)
113  {
114  return OSAL_WRONG_ARGS;
115  }
116 
117  vQueueDelete(*handle);
118  return OSAL_OK;
119 }

§ OSAL_queue_fetch()

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.

Parameters
[in]handlepointer on the queue handle
[in,out]msgmessage fetched in the OS queue
[in]timeoutmaximum time to wait for message arrival
Returns
operation status (
See also
OSAL_status_t)

Definition at line 148 of file osal_FreeRTOS.c.

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 }

§ OSAL_queue_post()

OSAL_status_t OSAL_queue_post ( OSAL_queue_handle_t *  handle,
void *  msg 
)

Post a message in an OS queue.

Parameters
[in]handlepointer on the queue handle
[in]msgmessage to post in the message queue
Returns
operation status (
See also
OSAL_status_t)

Definition at line 129 of file osal_FreeRTOS.c.

130 {
131  if(xQueueSend(*handle, &msg, 0) != pdTRUE)
132  {
133  return OSAL_NOMEM;
134  }
135 
136  return OSAL_OK;
137 }

§ OSAL_sleep()

OSAL_status_t OSAL_sleep ( uint32_t  milliseconds)

Asleep the current task during specified number of milliseconds.

Parameters
[in]millisecondsnumber of milliseconds
Returns
operation status (
See also
OSAL_status_t)

Definition at line 460 of file osal_FreeRTOS.c.

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 }

§ OSAL_task_create()

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.

Parameters
[in]entry_pointfunction called at task startup
[in]namethe task name
[in]stacktask stack declared using OSAL_task_stack_declare() macro
[in]prioritytask priority
[in]parameterstask entry parameters. NULL if no entry parameters
[in,out]handlepointer on a task handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 39 of file osal_FreeRTOS.c.

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 }

§ OSAL_task_delete()

OSAL_status_t OSAL_task_delete ( OSAL_task_handle_t *  handle)

Delete an OS task and start it.

Parameters
[in]handlepointer on the task handle
Returns
operation status (
See also
OSAL_status_t)

Definition at line 67 of file osal_FreeRTOS.c.

68 {
69  if(handle == NULL)
70  {
71  return OSAL_WRONG_ARGS;
72  }
73 
74  vTaskDelete((TaskHandle_t)*handle);
75 
76  return OSAL_OK;
77 }