cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4 hardfault

jhasa.2
Associate III

hi

I use stm32f4 micro but an external interrupt pin

when i increase frequency of the external signal connected to interrupt pin, the micro jump to hardfault

why? how I can detect the solution?

jhasa2_0-1697953350580.png

jhasa2_1-1697953356273.pngjhasa2_2-1697953358673.png

 

35 REPLIES 35

how i can found it?

 

char received_string_data[4];

 

Look at this https://www.tutorialspoint.com/c_standard_library/c_function_strncpy.htm

Your destination array size is less.

>how i can found it?

A good question, thank you ))

The compiler should give a warning :  warning: 'strncpy' writing 12 bytes into a region of size 4 overflows the destination [-Wstringop-overflow=]

But I've tested with several gcc versions on godbolt and was surprised that this warning shows up only starting with gcc v.12.something. Not in 11 and below, even with explicit -Wstringop-overflow option.

So ... Update to the latest gcc. If you cannot do this, use 3rd party tools for code analysis (which may be even better and robust).

Learn to debug. Like in other computer games, your character grows stronger and opens new skills when you play long enough))

jhasa.2
Associate III

i remove an 60000 byte array

why compiler does not warn or error when we use huge array in local functions?

Neither the compiler or linker track call-stacks and stack usage, most of this occurs dynamically as the code runs, not a static allocation which the linker must fulfill.

You should not have more local/auto variables that exceed the available RAM under the stack pointer.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

The received_string_data variable is global in that code, but that doesn't change the fact that the code writes beyond that buffer.

jhasa2_0-1698835083040.png

the program hard fault ....

how I can solve ?

the my code has been attached!.

jhasa.2
Associate III

the full project has been attached

jhasa.2
Associate III

can every body check my code? or help me?

Everyone explained to you what is wrong with your array. Think of it this way, you have a tennis ball container (array)  that can only hold 3 tennis balls (values), max. You're basically trying to fit 4 or more balls into a container. So when you try to fit 4 balls in a container, all it's going to do is burst (hard fault).

 

So in your instance you're trying to put 5 or more values into an array that can fit only 4 values. Get the idea what's wrong now?

If smoke escapes your device, put the smoke back in. It'll still work as a conversation piece. If you find my answers useful, click the Accept as Solution button so that way others can see the solution.