Skip to main content
Visitor II
October 10, 2023
Question

Unable to run basic LED toggle, Fails everytime

  • October 10, 2023
  • 3 replies
  • 1870 views

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

    This topic has been closed for replies.

    3 replies

    Technical Moderator
    October 10, 2023

    Hello,

    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.

    ST Employee
    October 10, 2023

    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

    Super User
    October 10, 2023

    > 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;
     while(!dummy)
     {
     }
    }

     

     

     

     

    Graduate II
    October 10, 2023

    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.