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.

 1// defined globally...
 2NRK_STK Stack1[NRK_APP_STACKSIZE];
 3nrk_task_type TaskOne;
 4void Task1(void);
 5...
 6// Pass the task structure the stack upon creation
 7TaskOne.task = Task1;
 8TaskOne.Ptos = (void *) &Stack1[NRK_APP_STACKSIZE-1];
 9TaskOne.Pbos = (void *) &Stack1r0;
10TaskOne.prio = 2;
11TaskOne.FirstActivation = TRUE;
12TaskOne.Type = BASIC_TASK;
13TaskOne.SchType = PREEMPTIVE;
14TaskOne.period.secs = 0;
15TaskOne.period.nano_secs = 500*NANOS_PER_MS;
16TaskOne.cpu_reserve.secs = 0;
17TaskOne.cpu_reserve.nano_secs = 100*NANOS_PER_MS;
18TaskOne.offset.secs = 0;
19TaskOne.offset.nano_secs= 0;
20nrk_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.

nrk_stack_check

void nrk_stack_check()

Parameters: none
Return Values: none

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. Since this function generates the stack overflow kernel panic without allowing the application to perform further actions, applications should use nrk_stack_check_pid() if they wish to do error handeling.

nrk_stack_check_pid

void nrk_stack_check_pid(int8_t pid)

Parameters: int8_t pid is the pid of the task that will have its stack canary value checked
Return Values: none

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.

1   if(nrk_stack_check_pid(nrk_get_pid())==NRK_OK)
2      nrk_kprintf( PSTR("Stack OK\r\n" ));
3   else
4      nrk_kprintf( PSTR("Stack BAD\r\n" ));
Prev: Contents Next: Signals Top