Skip to main content
Graduate II
November 4, 2023
Solved

Debug a running board by plugging Stlink/jlink after the flashing

  • November 4, 2023
  • 1 reply
  • 3639 views

Hi there, 

I may ask something **bleep** or impossible, but I want to try. 

Is there a way to debug (or at least look at the fault registers) a running MCU in a Fault state? 
In my case, I have an STM32H735g (so Cortex M7) which runs a FreeRTOS-based code (quite a complex project), and now I'm facing the splendid situation of random system crashes, not replicable, on on-field running devices that, of course, don't have a debugger plugged in. 

I'm wondering if there's a way to use tools like STM32cube Programmer in order to read the registers of a running MCU, or some magical trick done with other tools... 

 

Any help or suggestion is warmly accepted, 

thanks a lot 

 

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    Added the GNU startup.s assembler to cut-n-paste

    https://github.com/cturvey/RandomNinjaChef/blob/main/KeilHardFault.c#L26

    ; Equivalent for GNU/GAS used with STM32CubeIDE etc
    
     .section .text.HardFault_Handler,"ax",%progbits
    HardFault_Handler:
    
     /* Determine correct stack */
    
     TST lr, #4
     ITE EQ
     MRSEQ R0, MSP /* Read MSP (Main) */
     MRSNE R0, PSP /* Read PSP (Process) */
    
     MOV R1, R4
     MOV R2, R5
     MOV R3, R6
    
     B hard_fault_handler_c /* sourcer32@gmail.com */
    
     .size HardFault_Handler, .-HardFault_Handler

    1 reply

    Graduate II
    November 4, 2023

    Hot-swapping or hot-plugging can be hit or miss. Use those options in ST-LINK or J-LINK tools.

    Here we tend to use the serial port to output enough actionable data to debug/diagnose the situation. If you want to get a report after failure add a wait for getchar() and loop to reprint.

    https://github.com/cturvey/RandomNinjaChef/blob/main/KeilHardFault.c

    Also recommended to move to the version of Error_Handler() that outputs source file and line number info so those can be run to ground too.

    SScar.2Author
    Graduate II
    November 4, 2023

    Thanks for the quick reply!

    I see. Didn't know about hot-plugging options you're talking about, I'll search for them on the net..

    I have a few questions:

    1) didn't know about that error handler version, how does it works exactly?

    2) i see that it uses a snippet to insert inside the linker file of Keil. Did anybody created a Stm32cube version? 

     

    Thanks again

    Graduate II
    November 4, 2023

    startup.s I've likely posted GNU GAS format assembler before, shouldn't be too traumatic to port.

    ST some times used Error_Handler(__FILE__, __LINE__); form to understand WTH the fault came from, especially when there's dozens of potential sources. Also best to have some method of resolution or restart, as the while() loop basically bricks a product.

    /**
     * @brief This function is executed in case of error occurrence.
     * @PAram None
     * @retval None
     */
    void Error_Handler(const char *file, uint32_t line)
    {
     printf("Error %s %u\n", file, line);
    
     /* User may add here some code to deal with this error */
     while(1)
     {
     }
    }

    https://community.st.com/t5/stm32cubemx-mcus/stm32cubemx-v4-21-error-handler-definition-issues-in-main-h/td-p/419104