I have a board with STM32F4 running FreeRTOS (3 tasks on it), and i'm getting a HardFault every 15-50 min of use.
My hardware: 3 Encoders, 6 Analog In, 10 digital in and 3 PWM output for DC motors.
At start, I thought was some StackOverflow, than I implemmented uxTaskGetStackHighWaterMark();
for each task and checked that it's not it.
I then implemented some HardFault handlers:
void HardFault_Handler(void)
{
__asm volatile
(
" tst lr, #4 \n"
" ite eq \n"
" mrseq r0, msp \n"
" mrsne r0, psp \n"
" ldr r1, [r0, #24] \n"
" ldr r2, handler2_address_const \n"
" bx r2 \n"
" handler2_address_const: .word prvGetRegistersFromStack \n"
);
}
void prvGetRegistersFromStack( uint32_t *pulFaultStackAddress )
{
volatile uint32_t CFSRValue = SCB->CFSR;
volatile uint32_t HFSRValue = SCB->HFSR;
char stepError [100] = "";
if ((HFSRValue & (1 << 30)) != 0) {
CFSRValue >>= 16;
if((CFSRValue & (1 << 9)) != 0) strcpy(stepError," Divide by zero");
if((CFSRValue & (1 << 8)) != 0) strcpy(stepError," Unaligned access");
if((CFSRValue & (1 << 3)) != 0) strcpy(stepError," No coprocessor UsageFault" );
if((CFSRValue & (1 << 2)) != 0) strcpy(stepError," Invalid PC load UsageFault");
if((CFSRValue & (1 << 1)) != 0) strcpy(stepError," Invalid state");
if((CFSRValue & (1 << 0)) != 0) strcpy(stepError," Undefined instruction");
}
volatile uint32_t r0;
volatile uint32_t r1;
volatile uint32_t r2;
volatile uint32_t r3;
volatile uint32_t r12;
volatile uint32_t lr; /* Link register. */
volatile uint32_t pc; /* Program counter. */
volatile uint32_t psr;/* Program status register. */
r0 = pulFaultStackAddress[ 0 ];
r1 = pulFaultStackAddress[ 1 ];
r2 = pulFaultStackAddress[ 2 ];
r3 = pulFaultStackAddress[ 3 ];
r12 = pulFaultStackAddress[ 4 ];
lr = pulFaultStackAddress[ 5 ];
pc = pulFaultStackAddress[ 6 ];
psr = pulFaultStackAddress[ 7 ];
GPIO_WriteLed(0,1);
for(int i=0;i<=10;i++)
{
PWM_Change_DutyCycle(i,0);
}
for(;;);
}
And from this implementation, i got those results (each one was a HardFault, sometimes the PC was 0), that appear to be very random (to me):
1- if((CFSRValue & (1 << 1)) != 0) strcpy(stepError," Invalid state"); pc=0
2- if((CFSRValue & (1 << 0)) != 0) strcpy(stepError," Undefined instruction");
0800807d: ...IncrementTick+252 ldr r3, [r7, #8] - pc=134250621 - lr=2779096485
3- if((CFSRValue & (1 << 8)) != 0) strcpy(stepError," Unaligned access");
0800d63b: MX_ADC1_Init+290 ldr r3, [pc, #240] ; (0x800d72c <MX_ADC1_Init+532>)
4- if((CFSRValue & (1 << 1)) != 0) strcpy(stepError," Invalid state");
addr 0
5-080124c9: SysTick_Handler+8 bl 0x80072cc <osSystickHandler>
6- if((CFSRValue & (1 << 0)) != 0) strcpy(stepError," Undefined instruction");
08012521: SysTick_Handler+8 bl 0x80072cc <osSystickHandler>
Regards,