Nrk-api-stack

Version 4 (Anthony Rowe, 06/08/2007 06:53 pm)

1 1 Anthony Rowe
= Stack =
2 1 Anthony Rowe
3 1 Anthony Rowe
The stack for each task is separate and defined by the user. The kernel stack is located
4 1 Anthony Rowe
at the end of RAM and is not included in the BSS section of the code. On Atmel
5 1 Anthony Rowe
processors, the stack grows down. A Nano-RK stack is defined with the NRKSTK
6 1 Anthony Rowe
type. When a context swap occurs, each task stores its current state (32 registers + 1
7 1 Anthony Rowe
status register) on its own stack. This means that each application stack must be at least
8 1 Anthony Rowe
33 bytes longer than what the task locally requires.
9 1 Anthony Rowe
10 1 Anthony Rowe
11 1 Anthony Rowe
{{{
12 3 Anthony Rowe
#!c
13 1 Anthony Rowe
// defined globally...
14 1 Anthony Rowe
NRK_STK Stack1[NRK_APP_STACKSIZE];
15 1 Anthony Rowe
nrk_task_type TaskOne;
16 1 Anthony Rowe
void Task1(void);
17 1 Anthony Rowe
...
18 1 Anthony Rowe
// Pass the task structure the stack upon creation
19 1 Anthony Rowe
TaskOne.task = Task1;
20 1 Anthony Rowe
TaskOne.Ptos = (void *) &Stack1[NRK_APP_STACKSIZE-1];
21 1 Anthony Rowe
TaskOne.Pbos = (void *) &Stack1[0];
22 1 Anthony Rowe
TaskOne.prio = 2;
23 1 Anthony Rowe
TaskOne.FirstActivation = TRUE;
24 1 Anthony Rowe
TaskOne.Type = BASIC_TASK;
25 1 Anthony Rowe
TaskOne.SchType = PREEMPTIVE;
26 1 Anthony Rowe
TaskOne.period.secs = 0;
27 1 Anthony Rowe
TaskOne.period.nano_secs = 500*NANOS_PER_MS;
28 1 Anthony Rowe
TaskOne.cpu_reserve.secs = 0;
29 1 Anthony Rowe
TaskOne.cpu_reserve.nano_secs = 100*NANOS_PER_MS;
30 1 Anthony Rowe
TaskOne.offset.secs = 0;
31 1 Anthony Rowe
TaskOne.offset.nano_secs= 0;
32 1 Anthony Rowe
nrk_activate_task (&TaskOne);
33 1 Anthony Rowe
}}}
34 1 Anthony Rowe
35 1 Anthony Rowe
Example of setting up an applications stack.
36 1 Anthony Rowe
37 1 Anthony Rowe
38 1 Anthony Rowe
When you compile, make sure that the .data section, .bss section and the kernel
39 1 Anthony Rowe
stack size combined is less than the total memory in the system. Application stacks
40 1 Anthony Rowe
will appear in the .bss region.
41 1 Anthony Rowe
Given that the atmel is a Harvard architecture, special instructions must be used to
42 1 Anthony Rowe
access addresses in ROM. This causes issues when you try to printf constant strings
43 1 Anthony Rowe
from ROM. GCC works around this problem by copying your string into RAM. This
44 1 Anthony Rowe
will use LOTS of RAM. If you have constant strings, use nrk_kprintf.
45 1 Anthony Rowe
46 1 Anthony Rowe
'''void nrk_stack_check( )'''
47 2 Anthony Rowe
48 1 Anthony Rowe
This function will check the canary value of the stack. This is a number stored at
49 1 Anthony Rowe
the bottom of the stack that can flag an error if over written due to a stack overflow.
50 1 Anthony Rowe
This function is automatically called when NRK_STACK_CHECK is defined in
51 1 Anthony Rowe
nrk_cfg.h on any suspend system calls.
52 1 Anthony Rowe
53 4 Anthony Rowe
'''int8_t nrk_stack_check_pid(int8_t pid)'''
54 4 Anthony Rowe
55 4 Anthony Rowe
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.
56 4 Anthony Rowe
57 4 Anthony Rowe
{{{
58 4 Anthony Rowe
#!c
59 4 Anthony Rowe
   if(nrk_stack_check_pid(nrk_get_pid())==NRK_OK)
60 4 Anthony Rowe
      nrk_kprintf( PSTR("Stack OK\r\n" ));
61 4 Anthony Rowe
   else
62 4 Anthony Rowe
      nrk_kprintf( PSTR("Stack BAD\r\n" ));
63 4 Anthony Rowe
}}}
64 4 Anthony Rowe
65 1 Anthony Rowe
66 1 Anthony Rowe
| [wiki:nrk-api Contents] | [wiki:nrk-api-signals-semaphores Signals and Semaphores] |