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