Version 3 (Anthony Rowe, 05/10/2007 06:30 pm) → Version 4/12 (Anthony Rowe, 06/08/2007 06:53 pm)

= Stack =

The stack for each task is separate and defined by the user. The kernel stack is located
at the end of RAM and is not included in the BSS section of the code. On Atmel
processors, the stack grows down. A Nano-RK stack is defined with the NRKSTK
type. When a context swap occurs, each task stores its current state (32 registers + 1
status register) on its own stack. This means that each application stack must be at least
33 bytes longer than what the task locally requires.

// defined globally...
nrk_task_type TaskOne;
void Task1(void);
// Pass the task structure the stack upon creation
TaskOne.task = Task1;
TaskOne.Ptos = (void *) &Stack1[NRK_APP_STACKSIZE-1];
TaskOne.Pbos = (void *) &Stack1[0];
TaskOne.prio = 2;
TaskOne.FirstActivation = TRUE;
TaskOne.Type = BASIC_TASK;
TaskOne.SchType = PREEMPTIVE;
TaskOne.period.secs = 0;
TaskOne.period.nano_secs = 500*NANOS_PER_MS;
TaskOne.cpu_reserve.secs = 0;
TaskOne.cpu_reserve.nano_secs = 100*NANOS_PER_MS;
TaskOne.offset.secs = 0;
TaskOne.offset.nano_secs= 0;
nrk_activate_task (&TaskOne);

Example of setting up an applications stack.

When you compile, make sure that the .data section, .bss section and the kernel
stack size combined is less than the total memory in the system. Application stacks
will appear in the .bss region.
Given that the atmel is a Harvard architecture, special instructions must be used to
access addresses in ROM. This causes issues when you try to printf constant strings
from ROM. GCC works around this problem by copying your string into RAM. This
will use LOTS of RAM. If you have constant strings, use nrk_kprintf.

'''void nrk_stack_check( )'''

This function will check the canary value of the stack. This is a number stored at
the bottom of the stack that can flag an error if over written due to a stack overflow.
This function is automatically called when NRK_STACK_CHECK is defined in
nrk_cfg.h on any suspend system calls.

'''int8_t nrk_stack_check_pid(int8_t pid)'''

This functions returns NRK_OK if the stack of process pid has a correct canary value, or NRK_ERROR if it has been overwritten. Before returning error, this function will reset the canary value so that the kernel check does not detect it.

nrk_kprintf( PSTR("Stack OK\r\n" ));
nrk_kprintf( PSTR("Stack BAD\r\n" ));

| [wiki:nrk-api Contents] | [wiki:nrk-api-signals-semaphores Signals and Semaphores] |