You create the first task using the ctl_task_init function which turns the main program into a task. This function takes a pointer to the task structure that represents the main task, it's priority and a name as parameters.
void ctl_task_init(CTL_TASK_t *task, unsigned char priority, char *name);
This function must be called before any other CrossWorks tasking library calls are made.
You can create other tasks using the ctl_task_run function which initialises a task structure and may cause a task switch to occur. This function has the following prototype.
void ctl_task_run(CTL_TASK_t *task, unsigned char priority, void (*entrypoint)(void *), void *parameter, char *name, unsigned stack_size_in_words, unsigned *stack);
The task, priority and name parameters are the same as for ctl_task_init. The entrypoint parameter is the function that the task will execute which has the parameter passed to it. The start of the memory used to implement the stack that the task will execute in is stack and the size of the memory is supplied in stack_size_in_words.
The following example turns main into a task and creates another task. The main task will be the lowest priority task that switches the CPU into a power save mode when it is scheduled - this satisfies the requirement of always having a task to execute and enables a simple power saving system to be implemented.
#include <ctl/ctl.h> void task1(void *p) { // task code } static CTL_TASK_t mainTask, task1Task; static unsigned task1Stack[64]; int main(void) { ctl_task_init(&mainTask, 0, "main"); ctl_task_run(&task1Task, 1, task1, 0, "task1", sizeof(task1Stack)/sizeof(unsigned), task1Stack); while (1) // go into low power mode }
Note the usage of sizeof when passing the stack size to the ctl_task_run call.