Pic24 Stack usage

Hi, Just started using freeRTOS yesterday and am so far VERY impressed :-). There is one item (so far) which I am having difficulty getting my head around however, and thats the stack usage vs free.
I have a VERY simple background task which just flashes an LED then waits using vTaskDelay. I added some diagnostic code to view the TCB – to try to understand the stack requirement/usage (as it crashed with the default minimal stack setting).
The first time I call my debug code – uxTaskGetStackHighWaterMark returns 266 (decimal) – my stack size is fixed at 300. The second and subsequent times I call, it tells me 107. The code is in a simple loop.
I could understand that the high watermark could be this after a task switch, so how do I get the ‘free’ figure ??.
The first entry in the TCB (apparently the current SP ??) is :
0x0dfe at the first call
0x0d02 on subsequent calls
According to (my understanding of) the TCB – the first TCB entry is supposed to be the current Top Of Stack  :
volatile portSTACK_TYPE *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE STRUCT. */ Also – the ‘other’ TCB entries don’t seem to add up (to me anyway) :
*pxStack = 0x0c8e
*pxEndOfStack = 0x0ee4
Should these two figures be 300 decimal words ? apart – as they are +- 2.
The Pic stack is supposed to grow upwards – so would this be from 0c8e -> 0d02.
Sorry but I am well confused here now – as the numbers reported don’t seem to add up.
NB I am not new to RTOS’s in general and have been working with embedded multi-taskers written in Forth for almost 30 years :-O.
Any advice gratefully accepted :-O.
Many Thanks
Regards
Graham

Pic24 Stack usage

The PIC24 is ‘different’ in that the stack grows up from low to high memory.  I think all (?) the other supported architectures have the stack the other way around.  That means these stack functions are not so well exercised on the PIC24. uxTaskGetStackHighWaterMark() should return the minimum number of words (NOT Bytes) that have existed in the stack since the task was created.  Therefore the closer the value is to zero the closer the task has come to overflowing its stack.  I think the FreeRTOS viewer in MPLAB displays the high water mark value for each task. You can use the configCHECK_FOR_STACK_OVERFLOW to automatically catch stack overflows – set it to 2 for the best sensitivity.  I think this should work on PIC24 too. The first item in the TCB points to the top of the associated task stack, but only when the task is not running.  Its effectively the saved stack pointer for the task. Your pxStack and pxEndOfStack seem to be the correct distance apart.  You say your stack is 300 words and the two pointers point 600 bytes apart, which is as expected on a 16 bit part.  There is a discrepancy of 2 which probably comes from an (unnecessary)  alignment adjustment but I would have to step through the code to be sure. The actual used stack at any time could be obtained from the current stack pointer value (direct from the stack pointer register) minus pxCurrentTCB->pxStack.  Remember this will give you bytes, not words. Regards.

Pic24 Stack usage

Richard,
Many thanks for the excellent response :-). I did suspect that the discrepancy was because the task was running, but it still didn’t seem to make sense that the value stored as TOS had changed so much and didn’t come back down again ;-O.
Having thought it over some more I have decided to use the idle hook to take a simple snapshot of all SP’s (ie for each task) and store these into global vars – then I can simply view these from within a background worker task ;-)).
Maybe I should try to understand about coroutines as these seem to be called from within idle ??.
Thanks again for the prompt reply.
Regards
Graham

Pic24 Stack usage

Look at the vTaskList() function too http://www.freertos.org/a00021.html#vTaskList

Pic24 Stack usage

Hmmmm interesting – but it doesn’t seem to work for the PIC :-((. BgTask          R       3       0       1 IDLE            R       0       0       2 UART            S       1       0       0 Seems to tell me that the stacks are all FULL :-O – so I guess that it doesn’t take into account that the PIC grows upwards :-(. Looks like just a little more work is needed to make this RTOS more PIC-friendly – pity as its great – and these debugging level issues in no way detract from its usefulness. Although I (personally) find the naming convention a tad unwieldy – with all the various prefixes somewhat confusing.
I would tweak the code but am always very wary of changing third-party code – as an upgrade would cause my own changes to be lost :-(.
Thanks for the reply however – very useful ;-). Regards Graham