2025-04-28 11:48 PM
Hello, my program runs to the HardFault _ Handler function, I usually use interrupt check whether there is a function variable overflow, but this time the jump function is irregular, I do not know how to judge, please help me, I will provide the relevant information you need
It finally jumped into the DMA function, but I had no idea why it would trigger HardFault _ Handler, let alone call the UART _ WaitOnFlagUntilTimeout function
Sometimes, my program will jump directly from UART _ WaitOnFlagUntilTimeout function to HardFault _ Handler, in the following line, and I know through debugging that the huart 's Instance is NULL, which makes me not understand.
if (Timeout != HAL_MAX_DELAY)
{
if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))//this line jump to HardFault_Handler
{
return HAL_TIMEOUT;
}
I am using STM32G431RBT6, using three UART and its DMA.
Thanks.
Solved! Go to Solution.
2025-04-29 11:09 PM
Okay, I will try to increase the Minimum Stack Size to 0x800 in CubeMX. However, since tomorrow is the beginning of China 's Labor Day holiday until May 5, I may need 6 days to update the progress. Thank you for your help
2025-04-30 12:06 AM
In fact, default stack sizes that toolchains/IDEs are setting for projects are more or less "random".
Statically estimating stack usage is quite difficult, so they usually go with "that value works for the average application".
On a related note ...
Several RTOSes have runtime stack check routines implemented that can be enabled.
Those can easily be done manually, by filling the stack with a known pattern (famous ones' are "DDDD.DDDD" and "DEAD.BEEF") in the startup code, and regularly check the top of the stack at runtime.
2025-05-06 3:13 AM
I found the problem is indeed a stack overflow, but it is not the reason why the allocation is too small.
I used sprintf when formatting the string content and sending it, but my char string space is too small, which leads to an overflow. I believed too much in the size of my char array and didn 't understand the mechanics of sprintf that led to this error. Thank you to everyone involved in this discussion.
2025-05-06 4:02 AM
If you are interested you can look at actual CLib implementation of "printf", and how many lines of code certain formatting options actually cost. Or try to implement a printf-style formatting function yourself.