Nrk-api-stack

Version 11 (Anthony Rowe, 06/12/2013 03:22 am)

1 8 Anthony Rowe
h1. Stack
2 8 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 8 Anthony Rowe
<pre>
12 8 Anthony Rowe
<code class="c">
13 1 Anthony Rowe
// defined globally...
14 1 Anthony Rowe
NRK_STK Stack1[NRK_APP_STACKSIZE];
15 9 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 9 Anthony Rowe
TaskOne.task = Task1;
20 9 Anthony Rowe
TaskOne.Ptos = (void *) &Stack1[NRK_APP_STACKSIZE-1];
21 9 Anthony Rowe
TaskOne.Pbos = (void *) &Stack1r0;
22 9 Anthony Rowe
TaskOne.prio = 2;
23 9 Anthony Rowe
TaskOne.FirstActivation = TRUE;
24 9 Anthony Rowe
TaskOne.Type = BASIC_TASK;
25 9 Anthony Rowe
TaskOne.SchType = PREEMPTIVE;
26 9 Anthony Rowe
TaskOne.period.secs = 0;
27 9 Anthony Rowe
TaskOne.period.nano_secs = 500*NANOS_PER_MS;
28 9 Anthony Rowe
TaskOne.cpu_reserve.secs = 0;
29 9 Anthony Rowe
TaskOne.cpu_reserve.nano_secs = 100*NANOS_PER_MS;
30 9 Anthony Rowe
TaskOne.offset.secs = 0;
31 9 Anthony Rowe
TaskOne.offset.nano_secs= 0;
32 1 Anthony Rowe
nrk_activate_task (&TaskOne);
33 8 Anthony Rowe
</code></pre>
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
47 1 Anthony Rowe
48 1 Anthony Rowe
h3. nrk_stack_check
49 8 Anthony Rowe
50 9 Anthony Rowe
void nrk_stack_check() 
51 8 Anthony Rowe
52 9 Anthony Rowe
| _Parameters_: | none |
53 9 Anthony Rowe
| _Return Values:_ | none |
54 9 Anthony Rowe
55 6 Anthony Rowe
This function will check the canary value of the stack. This is a number stored at
56 1 Anthony Rowe
the bottom of the stack that can flag an error if over written due to a stack overflow.
57 1 Anthony Rowe
This function is automatically called when NRK_STACK_CHECK is defined in
58 5 Anthony Rowe
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.
59 8 Anthony Rowe
60 1 Anthony Rowe
61 6 Anthony Rowe
62 8 Anthony Rowe
h3. nrk_stack_check_pid
63 8 Anthony Rowe
64 9 Anthony Rowe
void nrk_stack_check_pid(int8_t pid) 
65 9 Anthony Rowe
66 9 Anthony Rowe
| _Parameters_: | int8_t pid is the pid of the task that will have its stack canary value checked |
67 9 Anthony Rowe
| _Return Values:_ | none |
68 4 Anthony Rowe
69 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.
70 4 Anthony Rowe
71 8 Anthony Rowe
<pre>
72 8 Anthony Rowe
<code class="c">
73 4 Anthony Rowe
   if(nrk_stack_check_pid(nrk_get_pid())==NRK_OK)
74 4 Anthony Rowe
      nrk_kprintf( PSTR("Stack OK\r\n" ));
75 4 Anthony Rowe
   else
76 4 Anthony Rowe
      nrk_kprintf( PSTR("Stack BAD\r\n" ));
77 8 Anthony Rowe
</code></pre>
78 1 Anthony Rowe
79 11 Anthony Rowe
| Prev: [[nrk-api|Contents]] |  Next: [[Nrk-api-signals-semaphores | Signals]]  | [[Nrk-api|Top]] |