os_port_ucos3.c
Go to the documentation of this file.
1 /**
2  * @file os_port_ucos3.c
3  * @brief RTOS abstraction layer (Micrium uC/OS-III)
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 <string.h>
34 #include "os_port.h"
35 #include "os_port_ucos3.h"
36 #include "debug.h"
37 
38 //Forward declaration of functions
39 void osIdleTaskHook(void);
40 
41 //Variables
42 static OS_TCB *tcbTable[OS_PORT_MAX_TASKS];
43 static CPU_STK *stkTable[OS_PORT_MAX_TASKS];
44 
45 
46 /**
47  * @brief Kernel initialization
48  **/
49 
50 void osInitKernel(void)
51 {
52  OS_ERR err;
53 
54  //Initialize tables
55  memset(tcbTable, 0, sizeof(tcbTable));
56  memset(stkTable, 0, sizeof(stkTable));
57 
58  //Scheduler initialization
59  OSInit(&err);
60 
61  //Set idle task hook
62  OS_AppIdleTaskHookPtr = osIdleTaskHook;
63 }
64 
65 
66 /**
67  * @brief Start kernel
68  **/
69 
70 void osStartKernel(void)
71 {
72  OS_ERR err;
73 
74  //Start the scheduler
75  OSStart(&err);
76 }
77 
78 
79 /**
80  * @brief Create a static task
81  * @param[out] task Pointer to the task structure
82  * @param[in] name A name identifying the task
83  * @param[in] taskCode Pointer to the task entry function
84  * @param[in] param A pointer to a variable to be passed to the task
85  * @param[in] stack Pointer to the stack
86  * @param[in] stackSize The initial size of the stack, in words
87  * @param[in] priority The priority at which the task should run
88  * @return The function returns TRUE if the task was successfully
89  * created. Otherwise, FALSE is returned
90  **/
91 
93  void *param, void *stack, size_t stackSize, int_t priority)
94 {
95  OS_ERR err;
96  CPU_STK stackLimit;
97 
98  //The watermark limit is used to monitor and ensure that the stack does not overflow
99  stackLimit = stackSize / 10;
100 
101  //Create a new task
102  OSTaskCreate(task, (CPU_CHAR *) name, taskCode, param,
103  priority, stack, stackLimit, stackSize, 0, 1, NULL,
104  OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR, &err);
105 
106  //Check whether the task was successfully created
107  if(err == OS_ERR_NONE)
108  return TRUE;
109  else
110  return FALSE;
111 }
112 
113 
114 /**
115  * @brief Create a new task
116  * @param[in] name A name identifying the task
117  * @param[in] taskCode Pointer to the task entry function
118  * @param[in] param A pointer to a variable to be passed to the task
119  * @param[in] stackSize The initial size of the stack, in words
120  * @param[in] priority The priority at which the task should run
121  * @return If the function succeeds, the return value is a pointer to the
122  * new task. If the function fails, the return value is NULL
123  **/
124 
126  void *param, size_t stackSize, int_t priority)
127 {
128  OS_ERR err;
129  CPU_INT32U i;
130  CPU_STK stackLimit;
131  OS_TCB *task;
132  CPU_STK *stack;
133 
134  //The watermark limit is used to monitor and ensure that the stack does not overflow
135  stackLimit = stackSize / 10;
136 
137  //Enter critical section
139 
140  //Loop through TCB table
141  for(i = 0; i < OS_PORT_MAX_TASKS; i++)
142  {
143  //Check whether the current entry is free
144  if(tcbTable[i] == NULL)
145  break;
146  }
147 
148  //Any entry available in the table?
149  if(i < OS_PORT_MAX_TASKS)
150  {
151  //Allocate a memory block to hold the task's control block
152  task = osAllocMem(sizeof(OS_TCB));
153 
154  //Successful memory allocation?
155  if(task != NULL)
156  {
157  //Allocate a memory block to hold the task's stack
158  stack = osAllocMem(stackSize * sizeof(CPU_STK));
159 
160  //Successful memory allocation?
161  if(stack != NULL)
162  {
163  //Create a new task
164  OSTaskCreate(task, (CPU_CHAR *) name, taskCode, param,
165  priority, stack, stackLimit, stackSize, 0, 1, NULL,
166  OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR, &err);
167 
168  //Check the return value
169  if(err == OS_ERR_NONE)
170  {
171  //Save TCB base address
172  tcbTable[i] = task;
173  //Save stack base address
174  stkTable[i] = stack;
175  }
176  else
177  {
178  //Clean up side effects
179  osFreeMem(task);
180  osFreeMem(stack);
181  }
182  }
183  else
184  {
185  //Memory allocation failed
186  err = OS_ERR_MEM_FULL;
187  //Clean up side effects
188  osFreeMem(task);
189  }
190  }
191  else
192  {
193  //Memory allocation failed
194  err = OS_ERR_MEM_FULL;
195  }
196  }
197  else
198  {
199  //No entry available in the table
200  err = OS_ERR_MEM_FULL;
201  }
202 
203  //Leave critical section
205 
206  //Check whether the task was successfully created
207  if(err == OS_ERR_NONE)
208  return task;
209  else
210  return NULL;
211 }
212 
213 
214 /**
215  * @brief Delete a task
216  * @param[in] task Pointer to the task to be deleted
217  **/
218 
219 void osDeleteTask(OsTask *task)
220 {
221  OS_ERR err;
222 
223  //Delete the specified task
224  OSTaskDel(task, &err);
225 }
226 
227 
228 /**
229  * @brief Delay routine
230  * @param[in] delay Amount of time for which the calling task should block
231  **/
232 
234 {
235  OS_ERR err;
236 
237  //Delay the task for the specified duration
238  OSTimeDly(OS_MS_TO_SYSTICKS(delay), OS_OPT_TIME_DLY, &err);
239 }
240 
241 
242 /**
243  * @brief Yield control to the next task
244  **/
245 
246 void osSwitchTask(void)
247 {
248  //Force a context switch
249  OSSched();
250 }
251 
252 
253 /**
254  * @brief Suspend scheduler activity
255  **/
256 
258 {
259  OS_ERR err;
260 
261  //Make sure the operating system is running
262  if(OSRunning == OS_STATE_OS_RUNNING)
263  {
264  //Suspend scheduler activity
265  OSSchedLock(&err);
266  }
267 }
268 
269 
270 /**
271  * @brief Resume scheduler activity
272  **/
273 
275 {
276  OS_ERR err;
277 
278  //Make sure the operating system is running
279  if(OSRunning == OS_STATE_OS_RUNNING)
280  {
281  //Resume scheduler activity
282  OSSchedUnlock(&err);
283  }
284 }
285 
286 
287 /**
288  * @brief Create an event object
289  * @param[in] event Pointer to the event object
290  * @return The function returns TRUE if the event object was successfully
291  * created. Otherwise, FALSE is returned
292  **/
293 
295 {
296  OS_ERR err;
297 
298  //Create an event flag group
299  OSFlagCreate(event, "EVENT", 0, &err);
300 
301  //Check whether the event flag group was successfully created
302  if(err == OS_ERR_NONE)
303  return TRUE;
304  else
305  return FALSE;
306 }
307 
308 
309 /**
310  * @brief Delete an event object
311  * @param[in] event Pointer to the event object
312  **/
313 
314 void osDeleteEvent(OsEvent *event)
315 {
316  OS_ERR err;
317 
318  //Make sure the operating system is running
319  if(OSRunning == OS_STATE_OS_RUNNING)
320  {
321  //Properly dispose the event object
322  OSFlagDel(event, OS_OPT_DEL_ALWAYS, &err);
323  }
324 }
325 
326 
327 /**
328  * @brief Set the specified event object to the signaled state
329  * @param[in] event Pointer to the event object
330  **/
331 
332 void osSetEvent(OsEvent *event)
333 {
334  OS_ERR err;
335 
336  //Set the specified event to the signaled state
337  OSFlagPost(event, 1, OS_OPT_POST_FLAG_SET, &err);
338 }
339 
340 
341 /**
342  * @brief Set the specified event object to the nonsignaled state
343  * @param[in] event Pointer to the event object
344  **/
345 
346 void osResetEvent(OsEvent *event)
347 {
348  OS_ERR err;
349 
350  //Force the specified event to the nonsignaled state
351  OSFlagPost(event, 1, OS_OPT_POST_FLAG_CLR, &err);
352 }
353 
354 
355 /**
356  * @brief Wait until the specified event is in the signaled state
357  * @param[in] event Pointer to the event object
358  * @param[in] timeout Timeout interval
359  * @return The function returns TRUE if the state of the specified object is
360  * signaled. FALSE is returned if the timeout interval elapsed
361  **/
362 
364 {
365  OS_ERR err;
366 
367  //Wait until the specified event is in the signaled
368  //state or the timeout interval elapses
369  if(timeout == 0)
370  {
371  //Non-blocking call
372  OSFlagPend(event, 1, 0, OS_OPT_PEND_FLAG_SET_ANY |
373  OS_OPT_PEND_FLAG_CONSUME | OS_OPT_PEND_NON_BLOCKING, NULL, &err);
374  }
375  else if(timeout == INFINITE_DELAY)
376  {
377  //Infinite timeout period
378  OSFlagPend(event, 1, 0, OS_OPT_PEND_FLAG_SET_ANY |
379  OS_OPT_PEND_FLAG_CONSUME | OS_OPT_PEND_BLOCKING, NULL, &err);
380  }
381  else
382  {
383  //Wait until the specified event becomes set
384  OSFlagPend(event, 1, OS_MS_TO_SYSTICKS(timeout), OS_OPT_PEND_FLAG_SET_ANY |
385  OS_OPT_PEND_FLAG_CONSUME | OS_OPT_PEND_BLOCKING, NULL, &err);
386  }
387 
388  //Check whether the specified event is set
389  if(err == OS_ERR_NONE)
390  return TRUE;
391  else
392  return FALSE;
393 }
394 
395 
396 /**
397  * @brief Set an event object to the signaled state from an interrupt service routine
398  * @param[in] event Pointer to the event object
399  * @return TRUE if setting the event to signaled state caused a task to unblock
400  * and the unblocked task has a priority higher than the currently running task
401  **/
402 
404 {
405  OS_ERR err;
406 
407  //Set the specified event to the signaled state
408  OSFlagPost(event, 1, OS_OPT_POST_FLAG_SET, &err);
409 
410  //The return value is not relevant
411  return FALSE;
412 }
413 
414 
415 /**
416  * @brief Create a semaphore object
417  * @param[in] semaphore Pointer to the semaphore object
418  * @param[in] count The maximum count for the semaphore object. This value
419  * must be greater than zero
420  * @return The function returns TRUE if the semaphore was successfully
421  * created. Otherwise, FALSE is returned
422  **/
423 
425 {
426  OS_ERR err;
427 
428  //Create a semaphore
429  OSSemCreate(semaphore, "SEMAPHORE", count, &err);
430 
431  //Check whether the semaphore was successfully created
432  if(err == OS_ERR_NONE)
433  return TRUE;
434  else
435  return FALSE;
436 }
437 
438 
439 /**
440  * @brief Delete a semaphore object
441  * @param[in] semaphore Pointer to the semaphore object
442  **/
443 
445 {
446  OS_ERR err;
447 
448  //Make sure the operating system is running
449  if(OSRunning == OS_STATE_OS_RUNNING)
450  {
451  //Properly dispose the specified semaphore
452  OSSemDel(semaphore, OS_OPT_DEL_ALWAYS, &err);
453  }
454 }
455 
456 
457 /**
458  * @brief Wait for the specified semaphore to be available
459  * @param[in] semaphore Pointer to the semaphore object
460  * @param[in] timeout Timeout interval
461  * @return The function returns TRUE if the semaphore is available. FALSE is
462  * returned if the timeout interval elapsed
463  **/
464 
466 {
467  OS_ERR err;
468 
469  //Wait until the semaphore is available or the timeout interval elapses
470  if(timeout == 0)
471  {
472  //Non-blocking call
473  OSSemPend(semaphore, 0, OS_OPT_PEND_NON_BLOCKING, NULL, &err);
474  }
475  else if(timeout == INFINITE_DELAY)
476  {
477  //Infinite timeout period
478  OSSemPend(semaphore, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
479  }
480  else
481  {
482  //Wait until the specified semaphore becomes available
483  OSSemPend(semaphore, OS_MS_TO_SYSTICKS(timeout),
484  OS_OPT_PEND_BLOCKING, NULL, &err);
485  }
486 
487  //Check whether the specified semaphore is available
488  if(err == OS_ERR_NONE)
489  return TRUE;
490  else
491  return FALSE;
492 }
493 
494 
495 /**
496  * @brief Release the specified semaphore object
497  * @param[in] semaphore Pointer to the semaphore object
498  **/
499 
501 {
502  OS_ERR err;
503 
504  //Release the semaphore
505  OSSemPost(semaphore, OS_OPT_POST_1, &err);
506 }
507 
508 
509 /**
510  * @brief Create a mutex object
511  * @param[in] mutex Pointer to the mutex object
512  * @return The function returns TRUE if the mutex was successfully
513  * created. Otherwise, FALSE is returned
514  **/
515 
517 {
518  OS_ERR err;
519 
520  //Create a mutex
521  OSMutexCreate(mutex, "MUTEX", &err);
522 
523  //Check whether the mutex was successfully created
524  if(err == OS_ERR_NONE)
525  return TRUE;
526  else
527  return FALSE;
528 }
529 
530 
531 /**
532  * @brief Delete a mutex object
533  * @param[in] mutex Pointer to the mutex object
534  **/
535 
536 void osDeleteMutex(OsMutex *mutex)
537 {
538  OS_ERR err;
539 
540  //Make sure the operating system is running
541  if(OSRunning == OS_STATE_OS_RUNNING)
542  {
543  //Properly dispose the specified mutex
544  OSMutexDel(mutex, OS_OPT_DEL_ALWAYS, &err);
545  }
546 }
547 
548 
549 /**
550  * @brief Acquire ownership of the specified mutex object
551  * @param[in] mutex Pointer to the mutex object
552  **/
553 
555 {
556  OS_ERR err;
557 
558  //Obtain ownership of the mutex object
559  OSMutexPend(mutex, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
560 }
561 
562 
563 /**
564  * @brief Release ownership of the specified mutex object
565  * @param[in] mutex Pointer to the mutex object
566  **/
567 
569 {
570  OS_ERR err;
571 
572  //Release ownership of the mutex object
573  OSMutexPost(mutex, OS_OPT_POST_NONE, &err);
574 }
575 
576 
577 /**
578  * @brief Retrieve system time
579  * @return Number of milliseconds elapsed since the system was last started
580  **/
581 
583 {
584  OS_ERR err;
585  systime_t time;
586 
587  //Get current tick count
588  time = OSTimeGet(&err);
589 
590  //Convert system ticks to milliseconds
591  return OS_SYSTICKS_TO_MS(time);
592 }
593 
594 
595 /**
596  * @brief Allocate a memory block
597  * @param[in] size Bytes to allocate
598  * @return A pointer to the allocated memory block or NULL if
599  * there is insufficient memory available
600  **/
601 
602 void *osAllocMem(size_t size)
603 {
604  void *p;
605 
606  //Enter critical section
608  //Allocate a memory block
609  p = malloc(size);
610  //Leave critical section
612 
613  //Debug message
614  TRACE_DEBUG("Allocating %" PRIuSIZE " bytes at 0x%08" PRIXPTR "\r\n", size, (uintptr_t) p);
615 
616  //Return a pointer to the newly allocated memory block
617  return p;
618 }
619 
620 
621 /**
622  * @brief Release a previously allocated memory block
623  * @param[in] p Previously allocated memory block to be freed
624  **/
625 
626 void osFreeMem(void *p)
627 {
628  //Make sure the pointer is valid
629  if(p != NULL)
630  {
631  //Debug message
632  TRACE_DEBUG("Freeing memory at 0x%08" PRIXPTR "\r\n", (uintptr_t) p);
633 
634  //Enter critical section
636  //Free memory block
637  free(p);
638  //Leave critical section
640  }
641 }
642 
643 
644 /**
645  * @brief Idle task hook
646  **/
647 
648 void osIdleTaskHook(void)
649 {
650  uint_t i;
651 
652  //Loop through TCB table
653  for(i = 0; i < OS_PORT_MAX_TASKS; i++)
654  {
655  //Check whether current entry is used
656  if(tcbTable[i] != NULL)
657  {
658  //Wait for task termination
659  if(tcbTable[i]->TaskState == OS_TASK_STATE_DEL)
660  {
661  //Free previously allocated resources
662  osFreeMem(stkTable[i]);
663  osFreeMem(tcbTable[i]);
664 
665  //Mark the entry as free
666  stkTable[i] = NULL;
667  tcbTable[i] = NULL;
668  }
669  }
670  }
671 }
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
uint16_t priority
Definition: dns_common.h:219
uint32_t systime_t
Definition: compiler_port.h:44
char char_t
Definition: compiler_port.h:41
void osDeleteTask(OsTask *task)
Delete a task.
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
uint32_t time
bool_t osCreateEvent(OsEvent *event)
Create an event object.
Debugging facilities.
systime_t osGetSystemTime(void)
Retrieve system time.
uint8_t p
Definition: ndp.h:295
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
void osDeleteEvent(OsEvent *event)
Delete an event object.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osStartKernel(void)
Start kernel.
Definition: os_port_ucos3.c:70
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
Event object.
void osFreeMem(void *p)
Release a previously allocated memory block.
#define TRUE
Definition: os_port.h:48
void osSwitchTask(void)
Yield control to the next task.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
Task object.
signed int int_t
Definition: compiler_port.h:42
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
char_t name[]
bool_t osCreateStaticTask(OsTask *task, const char_t *name, OsTaskCode taskCode, void *param, void *stack, size_t stackSize, int_t priority)
Create a static task.
Definition: os_port_ucos3.c:92
#define OS_MS_TO_SYSTICKS(n)
#define INFINITE_DELAY
Definition: os_port.h:72
RTOS abstraction layer.
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
void(* OsTaskCode)(void *param)
Task routine.
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define OS_PORT_MAX_TASKS
void osResumeAllTasks(void)
Resume scheduler activity.
OsTask * osCreateTask(const char_t *name, OsTaskCode taskCode, void *param, size_t stackSize, int_t priority)
Create a new task.
unsigned int uint_t
Definition: compiler_port.h:43
#define OS_SYSTICKS_TO_MS(n)
#define PRIuSIZE
Definition: compiler_port.h:72
void osInitKernel(void)
Kernel initialization.
Definition: os_port_ucos3.c:50
Mutex object.
void osSuspendAllTasks(void)
Suspend scheduler activity.
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void * osAllocMem(size_t size)
Allocate a memory block.
Semaphore object.
RTOS abstraction layer (Micrium uC/OS-III)
#define FALSE
Definition: os_port.h:44
void osIdleTaskHook(void)
Idle task hook.
int bool_t
Definition: compiler_port.h:47
void osDelayTask(systime_t delay)
Delay routine.
#define TRACE_DEBUG(...)
Definition: debug.h:98