FreeRTOS on PIC32MX795 - Heap Stack issues

andrewfrazer wrote on Wednesday, March 16, 2016:

Hi, i’m very new to using RTOS , but have got some things working now, but have hit my first big hurdle. Certainly i expect that this is becuase i dont’ know somethign i need to.

I have use the tcp_server_example in that shipped with harmony and made some mofications to it . My issue came when i needed to use some large arrays, ( 12000 bytes or so )… My compiler complained and ater a bit of tracking things down, i found that the Heap had been set to 58368, which seemed very large So, i changed that down, the code compiled but it crashed out…

After some more reading an playing i found out that with a Heap size at least 50000 was needed to make it work. ( somehwere between 49500 and 5000 )… i was adjusting the heap size in teh linker…

The issue i have is that with the heap requiring so much space, i dont’ have much left in teh stack and not enough to hold the data that i am proccesing…

This all works in out of the box with a PIC-32MZ, that has lots of more memory obviously…

What in FreeRTOS is using all the RAM, and what ( if anythgn ) can i do to reduce how much it is using.?

rtel wrote on Wednesday, March 16, 2016:

Prior to FreeRTOS V9, FreeRTOS would allocate all objects from the heap. These are in the most part very small allocations, but the stacks used by tasks are also allocated from the heap, and those can be quite large. The first thing to do will be to determine if any tasks are being allocated a stack size that is much larger than necessary.

There is a FreeRTOS plug-in for MPLAB but I’m not sure if it shows the stack sizes. If not, you can use the uxTaskGetSystemState() API function.

It is also advisable to ensure you have stack overflow and malloc failed hooks defined in case you set the stack too small, or set the heap too small, respectively.

In Harmony the size of the stacks used by tasks created by the framework are set in the graphical configuration interface - it may be that the defaults are larger than necessary.

andrewfrazer wrote on Wednesday, March 16, 2016:

Im using version 8.2.3, which is what is bundled with the most recent version of Microchip Harmony, and basing my work on the same that is provided ( tcpip_client_server ). I have however removed all but one of the services, so it only is running the udp server as that is all i need.

in FreeRTOS.h there is this defination

#define configTOTAL_HEAP_SIZE                   ( ( size_t ) 40960 )

the Project defintaitons have a linker size set of 58368 bytes.

I instlled the Plugin, and it does show the stack sizes… See the screen shot below; I do belive the stack overflow and malloc failed hooks are defined, as when things stop the code ends up in the generalexceptionhander function, sitting in the SYS_DEBUG_Breakpoint() endless loop.

You said the stacks used by the tasks are allocated from the heap… I can see 25616 bytes being used the 9 tasks. I know if i change the heap to less than 50,000 it will stop. So give or take theres another 25000 odd bytes that i have no idea where they are being used.

With a heap this big, I end up running out of memory for my stack.

Here is what is defined size wise for the tasks that are running. The size of the stacks in the plugin tool seem much bigger than what is defined, or are these somethign different.

/* Create OS Thread for Sys Tasks. */
    xTaskCreate((TaskFunction_t) _SYS_Tasks,
                "Sys Tasks",
                1024, NULL, 4, NULL);


    /* Create task for file system state machine*/
    /* Create OS Thread for SYS_FS Tasks. */
    xTaskCreate((TaskFunction_t) _SYS_FS_Tasks,
                "SYS_FS Tasks",
                2048, NULL, 1, NULL);

    /* Create OS Thread for SYS_CONSOLE Instance 0 Tasks. */
    xTaskCreate((TaskFunction_t) _SYS_CONSOLE_IDX0_Tasks,
                "SYS_CONSOLE Instance 0 Tasks",
                1024, NULL, 8, NULL);


    /* Create OS Thread for Sys Command Tasks. */
    xTaskCreate((TaskFunction_t) _SYS_COMMAND_Tasks,
                "Sys Command Tasks",
                1024, NULL, 7, NULL);



 
    /* Create task for gfx state machine*/
    /* Create OS Thread for USB Tasks. */
    xTaskCreate((TaskFunction_t) _USB_Tasks,
                "USB Tasks",
                1024, NULL, 1, NULL);


    /* Create task for TCPIP state machine*/
    /* Create OS Thread for TCPIP Tasks. */
    xTaskCreate((TaskFunction_t) _TCPIP_Tasks,
                "TCPIP Tasks",
                1024, NULL, 9, NULL);

    /* Create OS Thread for APP Tasks. */
    xTaskCreate((TaskFunction_t) _APP_Tasks,
                "APP Tasks",
                512, NULL, 1, NULL);


    /* Create OS Thread for APP3 Tasks. */ 
    xTaskCreate((TaskFunction_t) _APP3_Tasks,
                "APP3 Tasks",
                512, NULL, 6, NULL); 

    
    xTaskCreate((TaskFunction_t) _Flash_Leds,
                "Flash Leds",
                100, NULL, 4, NULL);
    ```

rtel wrote on Thursday, March 17, 2016:

When you create a task, calling xTaskCreate(), the stack sizes are specified in words - so on a PIC32 a stack size of 100 words is 400 bytes. The screen shot is showing the sizes in bytes I think. Some of the tasks (the FS task in particular) do seem to be using a lot of stack space - but presumably necessarily so.

FreeRTOS+Trace will trace memory allocations, perhaps that will be useful? I think there is a PIC32 version.