User`s manual

Dynamic C Users Manual digi.com 75
#define OS_TICKS_PER_SEC 32
...
OSInit();
...
OSStart();
5.10.1.2 Task Creation
In a µC/OS-II application, stacks are declared as static arrays, and the address of either the top or bottom
(depending on the CPU) of the stack is passed to OSTaskCreate. In a Rabbit-based system, the
Dynamic C development environment provides a superior stack allocation mechanism that µC/OS-II
incorporates. Rather than declaring stacks as static arrays, the number of stacks of particular sizes are
declared, and when a task is created using either OSTaskCreate or OSTaskCreateExt, only the
size of the stack is passed, not the memory address. This mechanism allows a large number of stacks to be
defined without using up root RAM.
There are five macros located in ucos2.lib that define the number of stacks needed of five different
sizes. To have three 256-byte stacks, one 512-byte stack, two 1024-byte stacks, one 2048-byte stack, and
no 4096-byte stacks, the following macro definitions would be used:
#define STACK_CNT_256 3 // number of 256 byte stacks
#define STACK_CNT_512 1 // number of 512 byte stacks
#define STACK_CNT_1K 2 // number of 1K stacks
#define STACK_CNT_2K 1 // number of 2K stacks
#define STACK_CNT_4K 0 // number of 4K stacks
These macros can be placed into each µC/OS-II application so that the number of each size stack can be
customized based on the needs of the application. Suppose that an application needs 5 tasks, and each task
has a consecutively larger stack. The macros and calls to OSTaskCreate would look as follows
#define STACK_CNT_256 2 // number of 256 byte stacks
#define STACK_CNT_512 2 // number of 512 byte stacks
#define STACK_CNT_1K 1 // number of 1K stacks
#define STACK_CNT_2K 1 // number of 2K stacks
#define STACK_CNT_4K 1 // number of 4K stacks
OSTaskCreate(task1, NULL, 256, 0);
OSTaskCreate(task2, NULL, 512, 1);
OSTaskCreate(task3, NULL, 1024, 2);
OSTaskCreate(task4, NULL, 2048, 3);
OSTaskCreate(task5, NULL, 4096, 4);
Note that STACK_CNT_256 is set to 2 instead of 1. µC/OS-II always creates an idle task which runs
when no other tasks are in the ready state. Note also that there are two 512 byte stacks instead of one. This
is because the program is given a 512 byte stack. If the application utilizes the µC/OS-II statistics task,
then the number of 512 byte stacks would have to be set to 3. (Statistic task creation can be enabled and
disabled via the macro OS_TASK_STAT_EN which is located in ucos2.lib). If only 6 stacks were
declared, one of the calls to OSTaskCreate would fail.