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-2024 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 2.4.4
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 //Default task parameters
41 {
42  NULL, //Task control block
43  NULL, //Stack
44  256, //Size of the stack
45  osPriorityNormal //Task priority
46 };
47 
48 
49 /**
50  * @brief Kernel initialization
51  **/
52 
53 void osInitKernel(void)
54 {
55  //Initialize the kernel
56  osKernelInitialize();
57 }
58 
59 
60 /**
61  * @brief Start kernel
62  **/
63 
64 void osStartKernel(void)
65 {
66  //Start the kernel
67  osKernelStart();
68 }
69 
70 
71 /**
72  * @brief Create a task
73  * @param[in] name NULL-terminated string identifying the task
74  * @param[in] taskCode Pointer to the task entry function
75  * @param[in] arg Argument passed to the task function
76  * @param[in] params Task parameters
77  * @return Task identifier referencing the newly created task
78  **/
79 
80 OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg,
81  const OsTaskParameters *params)
82 {
83  osThreadId_t threadId;
84  osThreadAttr_t threadAttr;
85 
86  //Initialize thread attributes
87  memset(&threadAttr, 0, sizeof(threadAttr));
88 
89  //Set thread attributes
90  threadAttr.name = name;
91  threadAttr.attr_bits = 0;
92  threadAttr.stack_mem = params->stack;
93  threadAttr.stack_size = params->stackSize * sizeof(uint32_t);
94  threadAttr.priority = (osPriority_t) params->priority;
95  threadAttr.tz_module = 0;
96 
97  //Static allocation?
98  if(params->cb != NULL)
99  {
100 #if defined(os_CMSIS_RTX)
101  threadAttr.cb_mem = params->cb;
102  threadAttr.cb_size = sizeof(os_thread_t);
103 #elif defined(osRtxVersionKernel)
104  threadAttr.cb_mem = params->cb;
105  threadAttr.cb_size = sizeof(osRtxThread_t);
106 #elif defined(configSUPPORT_STATIC_ALLOCATION)
107  threadAttr.cb_mem = params->cb;
108  threadAttr.cb_size = sizeof(StaticTask_t);
109 #else
110  threadAttr.cb_mem = NULL;
111  threadAttr.cb_size = 0;
112 #endif
113  }
114 
115  //Create a new thread
116  threadId = osThreadNew(taskCode, arg, &threadAttr);
117 
118  //Return the handle referencing the newly created thread
119  return (OsTaskId) threadId;
120 }
121 
122 
123 /**
124  * @brief Delete a task
125  * @param[in] taskId Task identifier referencing the task to be deleted
126  **/
127 
128 void osDeleteTask(OsTaskId taskId)
129 {
130  //Delete the specified thread
131  if(taskId == OS_SELF_TASK_ID)
132  {
133  osThreadExit();
134  }
135  else
136  {
137  osThreadTerminate((osThreadId_t) taskId);
138  }
139 }
140 
141 
142 /**
143  * @brief Delay routine
144  * @param[in] delay Amount of time for which the calling task should block
145  **/
146 
148 {
149  //Delay the thread for the specified duration
150  osDelay(OS_MS_TO_SYSTICKS(delay));
151 }
152 
153 
154 /**
155  * @brief Yield control to the next task
156  **/
157 
158 void osSwitchTask(void)
159 {
160  //Force a context switch
161  osThreadYield();
162 }
163 
164 
165 /**
166  * @brief Suspend scheduler activity
167  **/
168 
170 {
171  //Make sure the operating system is running
172  if(osKernelGetState() != osKernelInactive)
173  {
174  //Suspend all task switches
175  osKernelLock();
176  }
177 }
178 
179 
180 /**
181  * @brief Resume scheduler activity
182  **/
183 
185 {
186  //Make sure the operating system is running
187  if(osKernelGetState() != osKernelInactive)
188  {
189  //Resume lock all task switches
190  osKernelUnlock();
191  }
192 }
193 
194 
195 /**
196  * @brief Create an event object
197  * @param[in] event Pointer to the event object
198  * @return The function returns TRUE if the event object was successfully
199  * created. Otherwise, FALSE is returned
200  **/
201 
203 {
204  osEventFlagsAttr_t eventFlagsAttr;
205 
206  //Set event flags attributes
207  eventFlagsAttr.name = NULL;
208  eventFlagsAttr.attr_bits = 0;
209 
210 #if defined(os_CMSIS_RTX)
211  eventFlagsAttr.cb_mem = &event->cb;
212  eventFlagsAttr.cb_size = sizeof(os_event_flags_t);
213 #elif defined(osRtxVersionKernel)
214  eventFlagsAttr.cb_mem = &event->cb;
215  eventFlagsAttr.cb_size = sizeof(osRtxEventFlags_t);
216 #else
217  eventFlagsAttr.cb_mem = NULL;
218  eventFlagsAttr.cb_size = 0;
219 #endif
220 
221  //Create an event flags object
222  event->id = osEventFlagsNew(&eventFlagsAttr);
223 
224  //Check whether the returned semaphore ID is valid
225  if(event->id != NULL)
226  {
227  return TRUE;
228  }
229  else
230  {
231  return FALSE;
232  }
233 }
234 
235 
236 /**
237  * @brief Delete an event object
238  * @param[in] event Pointer to the event object
239  **/
240 
241 void osDeleteEvent(OsEvent *event)
242 {
243  //Make sure the event flags ID is valid
244  if(event->id != NULL)
245  {
246  //Properly dispose the event flags object
247  osEventFlagsDelete(event->id);
248  }
249 }
250 
251 
252 /**
253  * @brief Set the specified event object to the signaled state
254  * @param[in] event Pointer to the event object
255  **/
256 
257 void osSetEvent(OsEvent *event)
258 {
259  //Set the specified event to the signaled state
260  osEventFlagsSet(event->id, 1);
261 }
262 
263 
264 /**
265  * @brief Set the specified event object to the nonsignaled state
266  * @param[in] event Pointer to the event object
267  **/
268 
269 void osResetEvent(OsEvent *event)
270 {
271  //Force the specified event to the nonsignaled state
272  osEventFlagsClear(event->id, 1);
273 }
274 
275 
276 /**
277  * @brief Wait until the specified event is in the signaled state
278  * @param[in] event Pointer to the event object
279  * @param[in] timeout Timeout interval
280  * @return The function returns TRUE if the state of the specified object is
281  * signaled. FALSE is returned if the timeout interval elapsed
282  **/
283 
285 {
286  uint32_t flags;
287 
288  //Wait until the specified event is in the signaled state or the timeout
289  //interval elapses
290  if(timeout == INFINITE_DELAY)
291  {
292  //Infinite timeout period
293  flags = osEventFlagsWait(event->id, 1, osFlagsWaitAny, osWaitForever);
294  }
295  else
296  {
297  //Wait for the specified time interval
298  flags = osEventFlagsWait(event->id, 1, osFlagsWaitAny,
299  OS_MS_TO_SYSTICKS(timeout));
300  }
301 
302  //The function returns the event flags before clearing or an error code
303  if(flags == 1)
304  {
305  return TRUE;
306  }
307  else
308  {
309  return FALSE;
310  }
311 }
312 
313 
314 /**
315  * @brief Set an event object to the signaled state from an interrupt service routine
316  * @param[in] event Pointer to the event object
317  * @return TRUE if setting the event to signaled state caused a task to unblock
318  * and the unblocked task has a priority higher than the currently running task
319  **/
320 
322 {
323  //Set the specified event to the signaled state
324  osEventFlagsSet(event->id, 1);
325 
326  //The return value is not relevant
327  return FALSE;
328 }
329 
330 
331 /**
332  * @brief Create a semaphore object
333  * @param[in] semaphore Pointer to the semaphore object
334  * @param[in] count The maximum count for the semaphore object. This value
335  * must be greater than zero
336  * @return The function returns TRUE if the semaphore was successfully
337  * created. Otherwise, FALSE is returned
338  **/
339 
341 {
342  osSemaphoreAttr_t semaphoreAttr;
343 
344  //Set semaphore attributes
345  semaphoreAttr.name = NULL;
346  semaphoreAttr.attr_bits = 0;
347 
348 #if defined(os_CMSIS_RTX)
349  semaphoreAttr.cb_mem = &semaphore->cb;
350  semaphoreAttr.cb_size = sizeof(os_semaphore_t);
351 #elif defined(osRtxVersionKernel)
352  semaphoreAttr.cb_mem = &semaphore->cb;
353  semaphoreAttr.cb_size = sizeof(osRtxSemaphore_t);
354 #else
355  semaphoreAttr.cb_mem = NULL;
356  semaphoreAttr.cb_size = 0;
357 #endif
358 
359  //Create a semaphore object
360  semaphore->id = osSemaphoreNew(count, count, &semaphoreAttr);
361 
362  //Check whether the returned semaphore ID is valid
363  if(semaphore->id != NULL)
364  {
365  return TRUE;
366  }
367  else
368  {
369  return FALSE;
370  }
371 }
372 
373 
374 /**
375  * @brief Delete a semaphore object
376  * @param[in] semaphore Pointer to the semaphore object
377  **/
378 
380 {
381  //Make sure the semaphore ID is valid
382  if(semaphore->id != NULL)
383  {
384  //Properly dispose the specified semaphore
385  osSemaphoreDelete(semaphore->id);
386  }
387 }
388 
389 
390 /**
391  * @brief Wait for the specified semaphore to be available
392  * @param[in] semaphore Pointer to the semaphore object
393  * @param[in] timeout Timeout interval
394  * @return The function returns TRUE if the semaphore is available. FALSE is
395  * returned if the timeout interval elapsed
396  **/
397 
399 {
400  osStatus_t status;
401 
402  //Wait until the semaphore is available or the timeout interval elapses
403  if(timeout == INFINITE_DELAY)
404  {
405  //Infinite timeout period
406  status = osSemaphoreAcquire(semaphore->id, osWaitForever);
407  }
408  else
409  {
410  //Wait for the specified time interval
411  status = osSemaphoreAcquire(semaphore->id, OS_MS_TO_SYSTICKS(timeout));
412  }
413 
414  //Check return value
415  if(status == osOK)
416  {
417  return TRUE;
418  }
419  else
420  {
421  return FALSE;
422  }
423 }
424 
425 
426 /**
427  * @brief Release the specified semaphore object
428  * @param[in] semaphore Pointer to the semaphore object
429  **/
430 
432 {
433  //Release the semaphore
434  osSemaphoreRelease(semaphore->id);
435 }
436 
437 
438 /**
439  * @brief Create a mutex object
440  * @param[in] mutex Pointer to the mutex object
441  * @return The function returns TRUE if the mutex was successfully
442  * created. Otherwise, FALSE is returned
443  **/
444 
446 {
447  osMutexAttr_t mutexAttr;
448 
449  //Set mutex attributes
450  mutexAttr.name = NULL;
451  mutexAttr.attr_bits = 0;
452 
453 #if defined(os_CMSIS_RTX)
454  mutexAttr.cb_mem = &mutex->cb;
455  mutexAttr.cb_size = sizeof(os_mutex_t);
456 #elif defined(osRtxVersionKernel)
457  mutexAttr.cb_mem = &mutex->cb;
458  mutexAttr.cb_size = sizeof(osRtxMutex_t);
459 #else
460  mutexAttr.cb_mem = NULL;
461  mutexAttr.cb_size = 0;
462 #endif
463 
464  //Create a mutex object
465  mutex->id = osMutexNew(&mutexAttr);
466 
467  //Check whether the returned mutex ID is valid
468  if(mutex->id != NULL)
469  {
470  return TRUE;
471  }
472  else
473  {
474  return FALSE;
475  }
476 }
477 
478 
479 /**
480  * @brief Delete a mutex object
481  * @param[in] mutex Pointer to the mutex object
482  **/
483 
484 void osDeleteMutex(OsMutex *mutex)
485 {
486  //Make sure the mutex ID is valid
487  if(mutex->id != NULL)
488  {
489  //Properly dispose the specified mutex
490  osMutexDelete(mutex->id);
491  }
492 }
493 
494 
495 /**
496  * @brief Acquire ownership of the specified mutex object
497  * @param[in] mutex Pointer to the mutex object
498  **/
499 
501 {
502  //Obtain ownership of the mutex object
503  osMutexAcquire(mutex->id, osWaitForever);
504 }
505 
506 
507 /**
508  * @brief Release ownership of the specified mutex object
509  * @param[in] mutex Pointer to the mutex object
510  **/
511 
513 {
514  //Release ownership of the mutex object
515  osMutexRelease(mutex->id);
516 }
517 
518 
519 /**
520  * @brief Retrieve system time
521  * @return Number of milliseconds elapsed since the system was last started
522  **/
523 
525 {
526  systime_t time;
527 
528  //Get current tick count
529  time = osKernelGetTickCount();
530 
531  //Convert system ticks to milliseconds
532  return OS_SYSTICKS_TO_MS(time);
533 }
534 
535 
536 /**
537  * @brief Allocate a memory block
538  * @param[in] size Bytes to allocate
539  * @return A pointer to the allocated memory block or NULL if
540  * there is insufficient memory available
541  **/
542 
543 __weak_func void *osAllocMem(size_t size)
544 {
545  void *p;
546 
547  //Enter critical section
549  //Allocate a memory block
550  p = malloc(size);
551  //Leave critical section
553 
554  //Debug message
555  TRACE_DEBUG("Allocating %" PRIuSIZE " bytes at 0x%08" PRIXPTR "\r\n",
556  size, (uintptr_t) p);
557 
558  //Return a pointer to the newly allocated memory block
559  return p;
560 }
561 
562 
563 /**
564  * @brief Release a previously allocated memory block
565  * @param[in] p Previously allocated memory block to be freed
566  **/
567 
568 __weak_func void osFreeMem(void *p)
569 {
570  //Make sure the pointer is valid
571  if(p != NULL)
572  {
573  //Debug message
574  TRACE_DEBUG("Freeing memory at 0x%08" PRIXPTR "\r\n", (uintptr_t) p);
575 
576  //Enter critical section
578  //Free memory block
579  free(p);
580  //Leave critical section
582  }
583 }
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
int bool_t
Definition: compiler_port.h:53
__weak_func void * osAllocMem(size_t size)
Allocate a memory block.
RTOS abstraction layer (CMSIS-RTOS 2 / RTX v5)
void osDeleteEvent(OsEvent *event)
Delete an event object.
uint8_t p
Definition: ndp.h:300
#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
#define OS_SELF_TASK_ID
Semaphore object.
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
#define FALSE
Definition: os_port.h:46
void osDeleteTask(OsTaskId taskId)
Delete a task.
void(* OsTaskCode)(void *arg)
Task routine.
void osSuspendAllTasks(void)
Suspend scheduler activity.
void osSwitchTask(void)
Yield control to the next task.
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.
Task parameters.
__weak_func void osFreeMem(void *p)
Release a previously allocated memory block.
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 osInitKernel(void)
Kernel initialization.
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
Mutex object.
uint32_t systime_t
System time.
uint8_t flags
Definition: tcp.h:351
#define TRACE_DEBUG(...)
Definition: debug.h:107
char char_t
Definition: compiler_port.h:48
uint32_t time
systime_t osGetSystemTime(void)
Retrieve system time.
void osStartKernel(void)
Start kernel.
void osDelayTask(systime_t delay)
Delay routine.
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
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.
thread_t * OsTaskId
Task identifier.
#define PRIuSIZE
unsigned int uint_t
Definition: compiler_port.h:50
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.
Debugging facilities.
#define INFINITE_DELAY
Definition: os_port.h:75
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.