cocoOS  4.0.0
docTasks.h File Reference

Go to the source code of this file.

Detailed Description

Tasks
The main building block of your application is the task. The tasks should be created in the main function of the application before starting the os. The number of created tasks must not exceed the defined value N_TASKS in os_defines.h. A task is created this way:

Msg_t queue[Q_SIZE];
taskId = task_create( taskProc, prio, queue, Q_SIZE, msgSize );

The return value, taskId, above is just an uint8_t that can be used to identify the different tasks.
taskProc is a function pointer to the task procedure that is executed when the task is scheduled to run.
prio is the task priority in range 1-254, where 1 is highest priority. The kernel always lets the task with highest priority execute first. There can not be two tasks with same priority.
queue is a pointer to a message queue for the task. If the specified size is 0, no message queue is allocated for the task, and the queue parameter is ignored.
Q_SIZE is the length of the message queue. Set to 0 if no queue should be used.

msgSize is the size of the message type held in the message queue. Ignored of no queue is used.

Task Procedures
The code section of a task procedure must begin and end with the task_open() and task_close() macros respectively. A very simple task is shown below:

void task(void) {
...
}

This task will execute only once. When the task_close() line is executed, the task is put in the killed state and will not be executed again. If the task has to synchronize with some external event, we can put a blocking call in the middle:

void task(void) {
...
event_wait(evt);
...
}

This task will run to the event_wait() call and then block. The kernel will schedule other tasks to execute and eventually the event will be signalled which will put the task back to ready state again. Execution will be resumed after the event_wait() call. Once the task_close() statement is reached, the task will be killed and will not be executed again.

If we want a task to be excuted periodically we must put the code into an endless loop:

void task(void) {
for (;;) {
...
...
task_wait(TASK_DELAY_TICKS);
}
}

Another example of a task waiting for characters to be received on the serial port:

void task(void) {
uint8_t data;
for (;;) {
event_wait(rxEvt);
uart_get(&data);
...
}
}

The rxEvt may be signalled from the uart ISR using event_ISR_signal(rxEvt).

Task API
The task API consists of the following functions and macros: