Description
Tasks must be created in order for µC/OS-III to recognize them as tasks. You create a task by calling OSTaskCreate()
and by providing arguments specifying to µC/OS-III how the task will be managed. Tasks are always created in the ready-to-run state.
Tasks can be created either prior to the start of multitasking (i.e., before calling OSStart()
), or by a running task. A task cannot be created by an ISR. A task must either be written as an infinite loop, or delete itself once completed. If the task code returns by mistake, µC/OS-III will terminate the task by calling OSTaskDel((OS_TCB *)0, &err))
. At Micrium, we like the “while (DEF_ON)
” to implement infinite loops because, by convention, we use a while
loop when we don’t know how many iterations a loop will do. This is the case of an infinite loop. We prefer to use for
loops when we know how many iterations a loop will do.
Files
os.h/os_task.c
Prototype
Task as an infinite loop:
Run to completion task:
Arguments
p_tcb
is a pointer to the task’s OS_TCB
to use. It is assumed that storage for the TCB of the task will be allocated by the user code. You can declare a “global” variable as follows, and pass a pointer to this variable to OSTaskCreate()
:
...
If OS_SAFETY_CRITICAL_IEC61508
is defined: you called this after calling OSSafetyCriticalStart()
and thus you are no longer allowed to create additional kernel objects.
Returned Value
None
Notes/Warnings
- The stack must be declared with the
CPU_STK
type. - A task must always invoke one of the services provided by µC/OS-III to wait for time to expire, suspend the task, or wait on an object (wait on a message queue, event flag, mutex, semaphore, a signal or a message to be sent directly to the task). This allows other tasks to gain control of the CPU.
- You should not use task priorities
0
,1
,OS_CFG_PRIO_MAX-2
andOS_CFG_PRIO_MAX-1
because they are reserved for use by µC/OS-III.
Example Usage
OSTaskCreate()
can be called from main()
(in C), or a previously created task.