cancel
Showing results for 
Search instead for 
Did you mean: 

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

SScar.2
Senior

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 

 

1 ACCEPTED SOLUTION

Accepted Solutions

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
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5

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.

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

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

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

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

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
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Sorry for the late response, I missed the notifications.

Thanks for your time and the good answers, I found the hotplug configurations on CubeIDE, and they seem to work fine. Also, the code you posted is really good, thanks!