cancel
Showing results for 
Search instead for 
Did you mean: 

HI I'm using stm32f103zd and iwm getting after some hours hard fault How can I detect the reason and solve the problem?

mmell
Associate II
 
8 REPLIES 8

Start by having a proper Hard Fault handler so you can identify the code that is faulting, and the register/stack content.

Make sure you have a sufficiently large stack, and you understand how much of it is being used.

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

HI THANK YOU I'M DOUBLE THE STACK AND IT DID NOT PREVENT FROM HAPPENING -i using the iar workbench so I TRIED TO CATCH WITH IAR

Can you suggest to me what function to write in hard fault?

mmell
Associate II

and it seemed to happen when I'm using the printf or STRCAT function-I not sure

For strings make sure you understand the lengths, and that there is NUL termination.

In Keil we do this, changing startup_stm32xxxx.s to have calling code

HardFault_Handler\
                PROC
                EXPORT  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
 
                EXTERN  hard_fault_handler_c
                B       hard_fault_handler_c
                ENDP

//****************************************************************************
 
void hard_fault_handler_c(unsigned int * hardfault_args, unsigned int r4, unsigned int r5, unsigned int r6)
{
  printf ("\n[Hard Fault]\n"); // After Joseph Yiu
 
  printf ("r0 = %08X, r1 = %08X, r2 = %08X, r3 = %08X\n",
    hardfault_args[0], hardfault_args[1], hardfault_args[2], hardfault_args[3]);
  printf ("r4 = %08X, r5 = %08X, r6 = %08X, sp = %08X\n",
    r4, r5, r6, (unsigned int)&hardfault_args[8]);
  printf ("r12= %08X, lr = %08X, pc = %08X, psr= %08X\n",
    hardfault_args[4], hardfault_args[5], hardfault_args[6], hardfault_args[7]);
 
  printf ("bfar=%08X, cfsr=%08X, hfsr=%08X, dfsr=%08X, afsr=%08X\n",
    *((volatile unsigned int *)(0xE000ED38)),
    *((volatile unsigned int *)(0xE000ED28)),
    *((volatile unsigned int *)(0xE000ED2C)),
    *((volatile unsigned int *)(0xE000ED30)),
    *((volatile unsigned int *)(0xE000ED3C)) );
 
  while(1);
}
 
//****************************************************************************

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

Thank you ,Is there something that is concern on stm32f103zd ?

The causes a fairly constrained, and not specific to the STM32F103ZD

Make sure you don't have any errant pointers, or overflow buffers or local variables. Add sanity checking on pointer.

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

Thank for answering me on sunday-What register suppose to show me the problems?

You're going to be interested in the PC, the instructions there and immediately before and the registers those instructions are acting upon. They will provide the context of the failure/fault.

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