cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_Flash_Program triggers HardFault exception

DunaDridri
Associate II

Hello everyone,

I'm facing a weird issue about Flash programming on a STM32g0b1 microcontroller on IAR Workbench for ARM.

 

Context

I'm working on a bootloader project. I am able to lock, unlock, erase and program flash at the beginning of my program. Following a state machine, this is working well. 

 

Issue

When it comes to save a new bunch of data for the second time (I always program by block of 256 bytes so 32 double-words which is required by the library), it triggers a HardFault exception

 

Remark 1

If I go step by step in debug mode, I can program double-word by double-word without any exception. But if I run the program without going step by step (going over the function or without triggering any breakpoint), I automatically trigger the exception. And when the exception is triggered, no double-word has been written to flash so it is cancelled from the beginning

 

Remark 2

What is weird is that I'm able to fully erase flash (without touching the program of course) and to program few datas at different location without any issue. It only comes to it when reaching this special part of my program. And as I said, in any case, I'm writing 256 bytes (divided in 32 double-words)

 

Remark 3

I checked FLASH_SR and FLASH_CR register, it seems to not detect any issue. Also, interrupts are correctly disable before performing a program. I also increase Heap/stack size and nothing changed.

 

 

I'm starting to run out of idea. Can you help me to find a solution please?

Thanks in advance and have a good day !

 

Adrien

 

 

10 REPLIES 10
Augusto_Sala
Associate

Just to help other people in this situation, I had a similar issue where calling HAL_FLASH_Program would trigger an hardfault without even entering the function. The problem, in my case, was not in the call itself but in the casting of data. I solved it by aligning the data I was writing to a 32 bit address.

In my case (on an SMT32L073):

HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, *((uint32_t*)&(buffer[i])));

would fail since buffer[i] was not aligned. Writing a constant number would succeed and after making sure that buffer[i] was always at a 32 bit aligned address, the function started working as expected.