os_port_cmsis_rtos2.c
Go to the documentation of this file.
1 /**
2  * @file os_port_cmsis_rtos2.c
3  * @brief RTOS abstraction layer (CMSIS-RTOS 2 / RTX v5)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.6
27  **/
28 
29 //Switch to the appropriate trace level
30 #define TRACE_LEVEL TRACE_LEVEL_OFF
31 
32 //Dependencies
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include "os_port.h"
36 #include "os_port_cmsis_rtos2.h"
37 #include "debug.h"
38 
39 
40 /**
41  * @brief Kernel initialization
42  **/
43 
44 void osInitKernel(void)
45 {
46  //Initialize the kernel
47  osKernelInitialize();
48 }
49 
50 
51 /**
52  * @brief Start kernel
53  **/
54 
55 void osStartKernel(void)
56 {
57  //Start the kernel
58  osKernelStart();
59 }
60 
61 
62 /**
63  * @brief Create a new task
64  * @param[in] name A name identifying the task
65  * @param[in] taskCode Pointer to the task entry function
66  * @param[in] param A pointer to a variable to be passed to the task
67  * @param[in] stackSize The initial size of the stack, in words
68  * @param[in] priority The priority at which the task should run
69  * @return If the function succeeds, the return value is a pointer to the
70  * new task. If the function fails, the return value is NULL
71  **/
72 
74  void *param, size_t stackSize, int_t priority)
75 {
76  osThreadId_t threadId;
77  osThreadAttr_t threadAttr;
78 
79  //Set thread attributes
80  threadAttr.name = name;
81  threadAttr.attr_bits = 0;
82  threadAttr.cb_mem = NULL;
83  threadAttr.cb_size = 0;
84  threadAttr.stack_mem = NULL;
85  threadAttr.stack_size = stackSize * sizeof(uint_t);
86  threadAttr.priority = (osPriority_t) priority;
87  threadAttr.tz_module = 0;
88  threadAttr.reserved = 0;
89 
90  //Create a new thread
91  threadId = osThreadNew(taskCode, param, &threadAttr);
92  //Return a handle to the newly created thread
93  return (OsTask *) threadId;
94 }
95 
96 
97 /**
98  * @brief Delete a task
99  * @param[in] task Pointer to the task to be deleted
100  **/
101 
102 void osDeleteTask(OsTask *task)
103 {
104  //Delete the specified thread
105  if(task == NULL)
106  osThreadExit();
107  else
108  osThreadTerminate((osThreadId_t) task);
109 }
110 
111 
112 /**
113  * @brief Delay routine
114  * @param[in] delay Amount of time for which the calling task should block
115  **/
116 
118 {
119  //Delay the thread for the specified duration
120  osDelay(OS_MS_TO_SYSTICKS(delay));
121 }
122 
123 
124 /**
125  * @brief Yield control to the next task
126  **/
127 
128 void osSwitchTask(void)
129 {
130  //Force a context switch
131  osThreadYield();
132 }
133 
134 
135 /**
136  * @brief Suspend scheduler activity
137  **/
138 
140 {
141  //Make sure the operating system is running
142  if(osKernelGetState() != osKernelInactive)
143  {
144  //Suspend all task switches
145  osKernelLock();
146  }
147 }
148 
149 
150 /**
151  * @brief Resume scheduler activity
152  **/
153 
155 {
156  //Make sure the operating system is running
157  if(osKernelGetState() != osKernelInactive)
158  {
159  //Resume lock all task switches
160  osKernelUnlock();
161  }
162 }
163 
164 
165 /**
166  * @brief Create an event object
167  * @param[in] event Pointer to the event object
168  * @return The function returns TRUE if the event object was successfully
169  * created. Otherwise, FALSE is returned
170  **/
171 
173 {
174  osEventFlagsAttr_t eventFlagsAttr;
175 
176  //Set event flags attributes
177  eventFlagsAttr.name = NULL;
178  eventFlagsAttr.attr_bits = 0;
179 
180 #if defined(os_CMSIS_RTX)
181  eventFlagsAttr.cb_mem = &event->cb;
182  eventFlagsAttr.cb_size = sizeof(os_event_flags_t);
183 #elif defined(osRtxVersionKernel)
184  eventFlagsAttr.cb_mem = &event->cb;
185  eventFlagsAttr.cb_size = sizeof(osRtxEventFlags_t);
186 #else
187  eventFlagsAttr.cb_mem = NULL;
188  eventFlagsAttr.cb_size = 0;
189 #endif
190 
191  //Create an event flags object
192  event->id = osEventFlagsNew(&eventFlagsAttr);
193 
194  //Check whether the returned semaphore ID is valid
195  if(event->id != NULL)
196  return TRUE;
197  else
198  return FALSE;
199 }
200 
201 
202 /**
203  * @brief Delete an event object
204  * @param[in] event Pointer to the event object
205  **/
206 
207 void osDeleteEvent(OsEvent *event)
208 {
209  //Make sure the event flags ID is valid
210  if(event->id != NULL)
211  {
212  //Properly dispose the event flags object
213  osEventFlagsDelete(event->id);
214  }
215 }
216 
217 
218 /**
219  * @brief Set the specified event object to the signaled state
220  * @param[in] event Pointer to the event object
221  **/
222 
223 void osSetEvent(OsEvent *event)
224 {
225  //Set the specified event to the signaled state
226  osEventFlagsSet(event->id, 1);
227 }
228 
229 
230 /**
231  * @brief Set the specified event object to the nonsignaled state
232  * @param[in] event Pointer to the event object
233  **/
234 
235 void osResetEvent(OsEvent *event)
236 {
237  //Force the specified event to the nonsignaled state
238  osEventFlagsClear(event->id, 1);
239 }
240 
241 
242 /**
243  * @brief Wait until the specified event is in the signaled state
244  * @param[in] event Pointer to the event object
245  * @param[in] timeout Timeout interval
246  * @return The function returns TRUE if the state of the specified object is
247  * signaled. FALSE is returned if the timeout interval elapsed
248  **/
249 
251 {
252  uint32_t flags;
253 
254  //Wait until the specified event is in the signaled state or the timeout
255  //interval elapses
256  if(timeout == INFINITE_DELAY)
257  {
258  //Infinite timeout period
259  flags = osEventFlagsWait(event->id, 1, osFlagsWaitAny, osWaitForever);
260  }
261  else
262  {
263  //Wait for the specified time interval
264  flags = osEventFlagsWait(event->id, 1, osFlagsWaitAny,
265  OS_MS_TO_SYSTICKS(timeout));
266  }
267 
268  //The function returns the event flags before clearing or an error code
269  if(flags == 1)
270  return TRUE;
271  else
272  return FALSE;
273 }
274 
275 
276 /**
277  * @brief Set an event object to the signaled state from an interrupt service routine
278  * @param[in] event Pointer to the event object
279  * @return TRUE if setting the event to signaled state caused a task to unblock
280  * and the unblocked task has a priority higher than the currently running task
281  **/
282 
284 {
285  //Set the specified event to the signaled state
286  osEventFlagsSet(event->id, 1);
287 
288  //The return value is not relevant
289  return FALSE;
290 }
291 
292 
293 /**
294  * @brief Create a semaphore object
295  * @param[in] semaphore Pointer to the semaphore object
296  * @param[in] count The maximum count for the semaphore object. This value
297  * must be greater than zero
298  * @return The function returns TRUE if the semaphore was successfully
299  * created. Otherwise, FALSE is returned
300  **/
301 
303 {
304  osSemaphoreAttr_t semaphoreAttr;
305 
306  //Set semaphore attributes
307  semaphoreAttr.name = NULL;
308  semaphoreAttr.attr_bits = 0;
309 
310 #if defined(os_CMSIS_RTX)
311  semaphoreAttr.cb_mem = &semaphore->cb;
312  semaphoreAttr.cb_size = sizeof(os_semaphore_t);
313 #elif defined(osRtxVersionKernel)
314  semaphoreAttr.cb_mem = &semaphore->cb;
315  semaphoreAttr.cb_size = sizeof(osRtxSemaphore_t);
316 #else
317  semaphoreAttr.cb_mem = NULL;
318  semaphoreAttr.cb_size = 0;
319 #endif
320 
321  //Create a semaphore object
322  semaphore->id = osSemaphoreNew(count, count, &semaphoreAttr);
323 
324  //Check whether the returned semaphore ID is valid
325  if(semaphore->id != NULL)
326  return TRUE;
327  else
328  return FALSE;
329 }
330 
331 
332 /**
333  * @brief Delete a semaphore object
334  * @param[in] semaphore Pointer to the semaphore object
335  **/
336 
338 {
339  //Make sure the semaphore ID is valid
340  if(semaphore->id != NULL)
341  {
342  //Properly dispose the specified semaphore
343  osSemaphoreDelete(semaphore->id);
344  }
345 }
346 
347 
348 /**
349  * @brief Wait for the specified semaphore to be available
350  * @param[in] semaphore Pointer to the semaphore object
351  * @param[in] timeout Timeout interval
352  * @return The function returns TRUE if the semaphore is available. FALSE is
353  * returned if the timeout interval elapsed
354  **/
355 
357 {
358  osStatus_t status;
359 
360  //Wait until the semaphore is available or the timeout interval elapses
361  if(timeout == INFINITE_DELAY)
362  {
363  //Infinite timeout period
364  status = osSemaphoreAcquire(semaphore->id, osWaitForever);
365  }
366  else
367  {
368  //Wait for the specified time interval
369  status = osSemaphoreAcquire(semaphore->id, OS_MS_TO_SYSTICKS(timeout));
370  }
371 
372  //Check return value
373  if(status == osOK)
374  return TRUE;
375  else
376  return FALSE;
377 }
378 
379 
380 /**
381  * @brief Release the specified semaphore object
382  * @param[in] semaphore Pointer to the semaphore object
383  **/
384 
386 {
387  //Release the semaphore
388  osSemaphoreRelease(semaphore->id);
389 }
390 
391 
392 /**
393  * @brief Create a mutex object
394  * @param[in] mutex Pointer to the mutex object
395  * @return The function returns TRUE if the mutex was successfully
396  * created. Otherwise, FALSE is returned
397  **/
398 
400 {
401  osMutexAttr_t mutexAttr;
402 
403  //Set mutex attributes
404  mutexAttr.name = NULL;
405  mutexAttr.attr_bits = 0;
406 
407 #if defined(os_CMSIS_RTX)
408  mutexAttr.cb_mem = &mutex->cb;
409  mutexAttr.cb_size = sizeof(os_mutex_t);
410 #elif defined(osRtxVersionKernel)
411  mutexAttr.cb_mem = &mutex->cb;
412  mutexAttr.cb_size = sizeof(osRtxMutex_t);
413 #else
414  mutexAttr.cb_mem = NULL;
415  mutexAttr.cb_size = 0;
416 #endif
417 
418  //Create a mutex object
419  mutex->id = osMutexNew(&mutexAttr);
420 
421  //Check whether the returned mutex ID is valid
422  if(mutex->id != NULL)
423  return TRUE;
424  else
425  return FALSE;
426 }
427 
428 
429 /**
430  * @brief Delete a mutex object
431  * @param[in] mutex Pointer to the mutex object
432  **/
433 
434 void osDeleteMutex(OsMutex *mutex)
435 {
436  //Make sure the mutex ID is valid
437  if(mutex->id != NULL)
438  {
439  //Properly dispose the specified mutex
440  osMutexDelete(mutex->id);
441  }
442 }
443 
444 
445 /**
446  * @brief Acquire ownership of the specified mutex object
447  * @param[in] mutex Pointer to the mutex object
448  **/
449 
451 {
452  //Obtain ownership of the mutex object
453  osMutexAcquire(mutex->id, osWaitForever);
454 }
455 
456 
457 /**
458  * @brief Release ownership of the specified mutex object
459  * @param[in] mutex Pointer to the mutex object
460  **/
461 
463 {
464  //Release ownership of the mutex object
465  osMutexRelease(mutex->id);
466 }
467 
468 
469 /**
470  * @brief Retrieve system time
471  * @return Number of milliseconds elapsed since the system was last started
472  **/
473 
475 {
476  systime_t time;
477 
478  //Get current tick count
479  time = osKernelGetTickCount();
480 
481  //Convert system ticks to milliseconds
482  return OS_SYSTICKS_TO_MS(time);
483 }
484 
485 
486 /**
487  * @brief Allocate a memory block
488  * @param[in] size Bytes to allocate
489  * @return A pointer to the allocated memory block or NULL if
490  * there is insufficient memory available
491  **/
492 
493 void *osAllocMem(size_t size)
494 {
495  void *p;
496 
497  //Enter critical section
499  //Allocate a memory block
500  p = malloc(size);
501  //Leave critical section
503 
504  //Debug message
505  TRACE_DEBUG("Allocating %u bytes at 0x%08X\r\n", size, (uint_t) p);
506 
507  //Return a pointer to the newly allocated memory block
508  return p;
509 }
510 
511 
512 /**
513  * @brief Release a previously allocated memory block
514  * @param[in] p Previously allocated memory block to be freed
515  **/
516 
517 void osFreeMem(void *p)
518 {
519  //Make sure the pointer is valid
520  if(p != NULL)
521  {
522  //Debug message
523  TRACE_DEBUG("Freeing memory at 0x%08X\r\n", (uint_t) p);
524 
525  //Enter critical section
527  //Free memory block
528  free(p);
529  //Leave critical section
531  }
532 }
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
int bool_t
Definition: compiler_port.h:49
RTOS abstraction layer (CMSIS-RTOS 2 / RTX v5)
signed int int_t
Definition: compiler_port.h:44
void osDeleteEvent(OsEvent *event)
Delete an event object.
uint8_t p
Definition: ndp.h:298
#define TRUE
Definition: os_port.h:50
Event object.
void osResumeAllTasks(void)
Resume scheduler activity.
char_t name[]
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
osSemaphoreId id
Semaphore object.
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
#define FALSE
Definition: os_port.h:46
void osSuspendAllTasks(void)
Suspend scheduler activity.
void osSwitchTask(void)
Yield control to the next task.
Task object.
osSemaphoreId id
#define OS_SYSTICKS_TO_MS(n)
#define OS_MS_TO_SYSTICKS(n)
osMutexId id
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
OsTask * osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a new task.
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
void osDeleteTask(OsTask *task)
Delete a task.
void osInitKernel(void)
Kernel initialization.
Mutex object.
uint8_t flags
Definition: tcp.h:314
#define TRACE_DEBUG(...)
Definition: debug.h:106
char char_t
Definition: compiler_port.h:43
void * osAllocMem(size_t size)
Allocate a memory block.
uint32_t time
systime_t osGetSystemTime(void)
Retrieve system time.
void osStartKernel(void)
Start kernel.
void(* OsTaskCode)(void *param)
Task routine.
void osDelayTask(systime_t delay)
Delay routine.
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
void osFreeMem(void *p)
Release a previously allocated memory block.
unsigned int uint_t
Definition: compiler_port.h:45
uint16_t priority
Definition: dns_common.h:221
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
RTOS abstraction layer.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
uint32_t systime_t
Definition: compiler_port.h:46
Debugging facilities.
#define INFINITE_DELAY
Definition: os_port.h:74
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.