2019-03-05 12:52 PM
Hi, I have a problem with HardFault_Handler.
Sometimes, when I'm writing into the flash it happens.
After many days on searching still I don't understand why it happens and what it means.
Has someone got a suggestiom for me?
2019-03-07 05:52 AM
http://www.keil.com/appnotes/files/apnt209.pdf
Quick glance: looks like usage fault/unaligned.
2019-03-07 07:36 AM
2019-03-07 08:47 AM
FTLOG, pick tools you understand how to use, look at the surrounding code and pretend to be a chameleon.
/* In startup.s */
/* Add */
.section .text.HardFault_Handler,"ax",%progbits
HardFault_Handler:
TST LR, #4
ITE EQ
MRSEQ R0, MSP
MRSNE R0, PSP
MOV R1, LR
bl HardFault_Handler_C
.size HardFault_Handler, .-HardFault_Handler
/* Comment Out
.weak HardFault_Handler
.thumb_set HardFault_Handler,Default_Handler
*/
/* In stm32xyz_it.c */
// Comment Out
//void HardFault_Handler(void)
//{
// /* Go to infinite loop when Hard Fault exception occurs */
// while (1)
// {
// }
//}
You're going to want to know the code that faulted, and the registers it was acting upon.
Do sanity checking, that a) the memory is aligned, b) it is within range, and c) blank
Checking a pointer for 64-bit alignment
if (((unit32_t)ptr &7) !=0) puts("Pointer Alignment Fails");
2019-03-10 06:06 AM
Thanks for the detailed and helpful answer. I really appreciate your timely and correct advice.
Here's the output:
I'm writing on page boundaries and I always write 64 bits at a time (though I see that the HAL function can be easily adapted to write 32 bits).
Even when I just write 16 bytes, it breaks in the HardFault_Handler.
If I enter the function with the debugger and step over each line, including if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_ptr, dataf) == HAL_OK)
then burning to the flash succeeds. I suspect something to do with clocks or other peripherals.
I wrote bootloader code successfully - but except for one GPIO to show a blinking LED, the bootloader uses all of the STM's defaults on power-up.
Now I'm in the part of the code in which I receive the new FW to be burned over RS485 communication, and write it in the 2nd half of the flash (from 0x8044000 until 0x807FFFF). Even though I disable all interrupts, something is not allowing the flash to be burned correctly.
Whatever light you can shed on this would be appreciated.
Thanks,
Mechi
2019-03-10 07:06 AM
The dump is helpful - I'm checking the code around LR and PC...
2019-03-10 07:40 AM
And an alignment error it was...
I tried to cast a BYTE array to a 64-byte pointer.
Now with the memcpy to a 64-bit variable (without the cast, there is no HardFault.