Nrk-api-stack

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

1 1 Anthony Rowe
= Stack =
2 7 Anthony Rowe
[[TracNav(nrk-api-toc)]]
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
47 6 Anthony Rowe
=== nrk_stack_check ===
48 6 Anthony Rowe
|| void nrk_stack_check() ||
49 6 Anthony Rowe
|| ''Parameters'': none ||
50 6 Anthony Rowe
|| ''Return Values:'' none ||
51 6 Anthony Rowe
52 1 Anthony Rowe
This function will check the canary value of the stack. This is a number stored at
53 1 Anthony Rowe
the bottom of the stack that can flag an error if over written due to a stack overflow.
54 5 Anthony Rowe
This function is automatically called when NRK_STACK_CHECK is defined in
55 1 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.
56 1 Anthony Rowe
57 6 Anthony Rowe
58 6 Anthony Rowe
=== nrk_stack_check_pid ===
59 6 Anthony Rowe
|| void nrk_stack_check_pid(int8_t pid) ||
60 6 Anthony Rowe
|| ''Parameters'': int8_t pid is the pid of the task that will have its stack canary value checked ||
61 6 Anthony Rowe
|| ''Return Values:'' none ||
62 4 Anthony Rowe
63 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.
64 4 Anthony Rowe
65 4 Anthony Rowe
{{{
66 4 Anthony Rowe
#!c
67 4 Anthony Rowe
   if(nrk_stack_check_pid(nrk_get_pid())==NRK_OK)
68 4 Anthony Rowe
      nrk_kprintf( PSTR("Stack OK\r\n" ));
69 4 Anthony Rowe
   else
70 4 Anthony Rowe
      nrk_kprintf( PSTR("Stack BAD\r\n" ));
71 4 Anthony Rowe
}}}
72 4 Anthony Rowe
73 1 Anthony Rowe
74 1 Anthony Rowe
| [wiki:nrk-api Contents] | [wiki:nrk-api-signals-semaphores Signals and Semaphores] |