Question about Task Stack items

gastaronum wrote on Tuesday, January 03, 2012:

Hi,

MCU: EFM32 (EnergyMicro Cortex M3)
FreeRTOS 7.0.1

I check stack allocation in function xTaskGenericCreate() and pxPortInitialiseStack(), for example:

TOP of stack = 0x200008CC, aligned TOP of stack = 0x200008C8

Aligned TOP of Stack value – 4 bytes (0x200008C4)  portINITIAL_XPSR
Aligned TOP of Stack value – 8 bytes (0x200008C0) pointer to task function
Aligned TOP of Stack value – 12 bytes LR
Aligned TOP of Stack value – 16 bytes R12 (reserved for use in future)
Aligned TOP of Stack value – 20 bytes R3 (reserved for use in future)
Aligned TOP of Stack value – 24 bytes R2 (reserved for use in future)
Aligned TOP of Stack value – 28 bytes R1 (reserved for use in future)
Aligned TOP of Stack value – 32 bytes pointer to parameters, passed to the Task Function
Aligned TOP of Stack value – 36 bytes R11 (reserved for use in future)
Aligned TOP of Stack value – 40 bytes R10 (reserved for use in future)
Aligned TOP of Stack value – 44 bytes R9 (reserved for use in future)
Aligned TOP of Stack value – 48 bytes R8 (reserved for use in future)
Aligned TOP of Stack value – 52 bytes R7 (reserved for use in future)
Aligned TOP of Stack value – 56 bytes R6 (reserved for use in future)
Aligned TOP of Stack value – 60 bytes R5 (reserved for use in future)
Aligned TOP of Stack value – 64 bytes (0x20000888) R4 (reserved for use in future)

When the Task Function is called by system, firstly a values of R4 and LR is pushed to stack:

void TaskFunction(void *pvParameters)
{
TaskFunction:
      0x14c22: 0xb510         PUSH      {R4, LR}
      ……

Them pushed at address 0x200008C4 (LR) and 0x200008C0 (R4) and completely overwrite the portINITIAL_XPSR and pointer to the task function values.

Does it correct behavior?
Can I analyze the stack area (R1 … R12 values stored in the stack as reserved in pxPortInitialiseStack() function) after context switch or this area is “not for use”?

Best Regards and thanks for support.

rtel wrote on Tuesday, January 03, 2012:

The items are pushed onto the stack before the stack is started to allow the stack to be started.  That is, the items are popped from the stack during the processor starting the task.  Once they have been popped from the stack, and the task is running, the stack frame as initially configured is no longer required and a new stack frame is created by the task function entry code (the compiler generated prologue).  The tasks stack frame will overwrite the original stack frame, so this behaviour is correct.

Regards.