Nrk-api-stack

Version 8 (Anthony Rowe, 10/18/2008 01:03 am)

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