cancel
Showing results for 
Search instead for 
Did you mean: 

The program enters HardFault _ Handler, but no valid stack information can be found

xtxChino
Associate II

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

xtxChino_0-1745909283961.png

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.

xtxChino_1-1745909305063.png

 
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.

13 REPLIES 13

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

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.

xtxChino
Associate II

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.

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.