2022-11-25 03:06 AM
Hello Community,
I'm developing a program for a STM32 L051R6T6 with CubeIDE.on bare metal, using HAL driver functions.
It reads some cyclic data from two USARTS using DMA.
The maincycle does processing of the rxTelegrams, transmits and does some logging on a third LPUART.
I now I'm experiencing problems with longer logstrings. They are allocated from heap with malloc with necessary size (500 Bytes in my case and checked for NULL). But while the maincycle fills it with logging content, a DMA Interrupt occures and process the DMAData from one of the USARTs. In the Interrupt routine a function is called and when I step with the debugger from this function header to the first instruction, my allocated logbuffer from the maincycle is overwritten with 24 Bytes near the end.
My questions are:
Shouldn't the allocated memory from the maincycle also be protected from overwriting in Interrupt routines? If not: how could I avoid such behavior.
What is done, when I jump from the function header to the first instruction in debugger. Is this the creation step of the values of function parameters ( the content of the 24 Bytes doesnt look like the parameters, as far as i can tell).
Of course there may be memory leaks in the program, but in this case all seems to be valid.
Thank you.
2022-11-25 05:44 AM
Hi @Wolfgang Rißler ,
from your description it almost looks like a textbook example of an overrun problem. Try looking to the map file produced by the linker if you see something suspicious.
You mention memory protection. There is no memory protection by default, it has to be configured and enabled. The MPU can be used to guard memory boundaries. It throws an interrupt and you can observe whrere exactly the problem occurs.
BR,
J
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2022-11-25 09:58 AM
Rule #1: do not use dynamic memory allocation for simple task on small microcontrollers. You won't gain anything and get some troubles instead.
2022-11-28 12:38 AM
Hello and thank you for your replys,
I know, that heap allocation is generally not a good idea. It's the only place, where I do this, to save memory. and performance. (So I dont need to copy the logstrings).
It looks like the RAM is on its limit and so I shortened the quite long logmessages. So its seems to work for the moment.
Also when I experimented with long messages as a test, I got jumps to the ErrorHandler. But I was wondering, that overwriting allocated memoryareas (like described above) could happen, without ErrorHandler exceptions.
Is there a easy way to monitor the memory usage? Something like HAL_Get_Free_Memory_Amount()?
2022-11-28 02:04 AM
Determine what is causing it to enter ErrorHandle(), suggest using the source file and line reporting variant to speed diagnosis.
Perhaps look at the heap allocation functions in the C library, or simply walk the heap structures which are typically a linked list.
Perhaps wrap malloc() free() to track use and misuse.
Avoid using heap functions in interrupt or callback context.