I am guessing the stack is used for interrupt processing, saving registers etc, but what is the heap used for. What’s the best way of working out the size necessary for each of these?
The heap size defined in the linker file will generally be for the standard C library malloc function, which is likely not thread-safe unless you have taken the steps to make it so. (or the environment has been specially created for FreeRTOS and has taken the steps by default to make it safe).
As to figuring out how much space you need, you really need to know your application and its needs, or start big and see what is left after testing.
To be on the safe side, you may want to consult with your project map file to find out what _Min_Heap_Size resolves to and what identifiers are located in that range. Some eco systems and/or HALs employ non standard and sometimes subtle deviations for their own startup code.
I did a bit of research and found that stdio - printf when used to print variables uses the heap. If printf just prints fixed text with no variables it does not use the heap. So the size of the heap must be determined by the number of variables to be printed within any printf function. So your most complex printf function determines minimum heap size if no other dynamic memory is used. If your program doesn’t use dynamic memory and you don’t printf variables the heap can be set to zero.
printf("Fred") => no heap
printf("Fred %0d, fredvar) => heap used
Thank you for sharing your solution. As @richard-damon has already mentioned, if you are calling printf from multiple threads (and therefore, malloc from multiple threads) make sure either it is thread-safe or you have taken the steps required by your C library to make it thread-safe.
printf (stdio.h 5.3 (Berkeley) isn’t thread safe, but I am only using it one task at a time so I haven’t had any problems. Would a mutex be a safe way of using this printf to avoid re-entry issues? If so I would like to test this for future use.