Showing results for 
Search instead for 
Did you mean: 

Unable to run basic LED toggle, Fails everytime


I tried setting up Nucleo board, and every time I debug the code, it goes to HardFault_Handler(). How do I solve this?

I assumed there could be a debugger issue and I tried all the firmware for ST Link. No luck. Can anyone help?debugstm.png

ST Employee


What board are you using ?

It is most likely to be a software error rather than a debug probe error.

At what point in your code the jump to the hardfault handler is happening ?

You should share your code as well, if you would like the community to check it.

Florian LR
ST Employee

Hi @siril and welcome to the STCommunity ! 

About your issue, it is a bit difficult to know where your issue comes from without more information.

There is a way for you to find out where is the issue. If you recreate a project, on the page where you select your target you can click on Example Selector and then enter your board name and select an example named GPIO_IOToggle that allows you to toggle a LED. That should work, and you can then compare this project setup with your previous one to understand what is missing.

I hope this will help you,

Best Regards

Florian LR

Pavel A.
Evangelist III

> every time I debug the code, it goes to HardFault_Handler()

Usually this means a bad stack pointer value. Please check your link script - what is the initial SP value? 

Also, to see the complete call stack in fault handlers (HardFault and others), change the default while(1) like following, to prevent the compiler optimizing away the return address:


void HardFault_Handler(void)
  static volatile char dummy=0;





I wouldn't make that variable static. Fault handlers require some stack anyway, so there is a very little chance that there is no stack space for a single additional variable. And most likely compilers will not store that variable on a stack memory at all and just keep it in a register.