cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 HAL_FLASH_Program() not working as expected

Adam  Dibin
Associate
Posted on January 30, 2018 at 02:47

I'm trying to program FLASH using HAL_FLASH_Program() function. Precisely speaking, I've written a function which is expected to write two measurements to flash at a set time interval (e.g. 3 seconds). However, when called, the function manages to write only the first one while ignoring the second one. Can't HAL_FLASH_Program be used twice? What am I doing wrong? I just want to mention that I'm utterly new to STM32 programming, so any helpful suggestions would be much appreciated. Here is the code:

void writeFlash() {
mem = returnPointerToFirstEmptyAddressInSector(); Address = (uint32_t)mem; var1.f = Temperature; var2.f = SD; HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR | FLASH_FLAG_PGPERR); HAL_FLASH_Program(TYPEPROGRAM_WORD, Address, var1.i ); Address++; HAL_FLASH_Program(TYPEPROGRAM_WORD, Address, var2.i); HAL_FLASH_Lock();
}

1 REPLY 1
Doug Kehn
Senior
Posted on January 30, 2018 at 14:39

Before writing the second value to flash make sure the previous flash operation has completed. Wait for __HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) == RESET

It would be a good idea to make sure no flash operation is in progress before writing the first value too (before clearing the flags).