3
votes

I am working on a custom board with the STM32F107VCT processor. I am using FreeRTOS & lwIP

I am using tinyxml2 library and reading / writing to an EEPROM and running into strange corruption issues and crashes. I suspect I may be running out of memory. I am fairly new embedded electornics and FreeRTOS so am unsure where I should be looking.

I create a few threads like so:

osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 512); 
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
osThreadDef(RfTask, StartRfTask, osPriorityNormal, 0, 700); 
RfTaskHandle = osThreadCreate(osThread(RfTask), NULL);
osThreadDef(DiscoveryTask, StartDiscoveryTask, osPriorityNormal, 0, 256); 
DisoveryTaskHandle = osThreadCreate(osThread(DiscoveryTask), NULL);

Here are some of my defines related to heap / stack

FreeRTOS.h 
define configTOTAL_HEAP_SIZE ((size_t)1024*24)

Flash.id
_estack = 0x20010000; /* end of RAM */
_Min_Heap_Size = 0; /* required amount of heap */ 
_Min_Stack_Size = 0x200; /* required amount of stack */

I am not really following in my mind exactly what heap / stack theory is, and how it relates to the figures in flash.id and FreeRTOS.h

Could someone offer me an explanation please of how this all works, and any methods to check amount of ram available for the tasks etc?

I can share my code on gitlab if required

Thank you so much

2

2 Answers

2
votes

Can't comment on how your lwIP configuration is using memory, but as far as FreeRTOS is concerned:

The heap defined by your linker script is only used by FreeRTOS if you include heap_3.c in your build. All the other heap implementations that ship with FreeRTOS take memory from a statically allocated array. If all memory allocation is done using the FreeRTOS memory allocators then defining a heap in the linker script is wasting RAM as it won't get used. See http://www.freertos.org/a00111.html for more information.

[on an STM32] The stack defined by your linker script will be used by main() before the scheduler starts, and interrupts after the scheduler starts. FreeRTOS tasks do not use that stack as each task is allocated its own stack when it is created. See the API documentation for the xTaskCreate() and xTaskCreateStatic() API functions. http://www.freertos.org/a00019.html

When you are developing it is recommended to ensure configASSERT() is defined, and that you have stack overflow detection turned on. Googling for those terms along with "FreeRTOS" will find links for those too.

0
votes

any methods to check amount of ram available for the tasks etc?

In FreeRTOS, each task has data structure named TCB_t for storing its own Task Control Block (TCB), one structure member of TCB_t is pxStack, which indicates lowest address location of the stack memory of a task. In STM32, stack memory (also pointer $sp) grows down from high memory, if $sp goes lower than pxStack then stack overflow happens.

To check stack memory available to a specific task, you can simply check the value of pxStack and $sp (e.g. through OpenOCD / GDB) and see their difference.

You can read task.c for detail.