Erasing a page of flash on STM32L476RG
Hello everybody,
I'm working on an STM32L476RG. For the moment, I'm using a nucleo-476RG board.
I'm trying to write and read some data in the flash memory.
I'm trying to write at the address : 0x080FF800 (first address of last page of the memory).
I do the following operations : erase page / write 16 bytes / read 16 bytes.
When I'm in debug mode, I see that the erase operation doesn't work (flash memory contains the old data) and then writing operation doesn't work well.
By adding the instruction "monitor flash mass_erase" in my run configurations, I can see that the page is well erased and then writing and reading are working well.
So I think something is wrong in my code but I don't know what. Moreover, the HAL_FLASHEx_Erase function gave me HAL_OK as return (and it seems not to be right).
/* Déclarations et initialisations des variables */
volatile uint64_t data_to_FLASH[(strlen((char*)data)/8) + (int)((strlen((char*)data) % 8) != 0)];
memset((uint8_t*)data_to_FLASH, 0, strlen((char*)data_to_FLASH));
strcpy((char*)data_to_FLASH, (char*)data);
volatile uint32_t data_length = (strlen((char*)data_to_FLASH) / 8) + (int)((strlen((char*)data_to_FLASH) % 8) != 0);
volatile uint16_t pages = (strlen((char*)data)/page_size) + (int)((strlen((char*)data)%page_size) != 0);
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
/* Fill EraseInit structure */
FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Page = FLASH_STORAGE;
EraseInitStruct.NbPages = pages;
uint32_t PageError;
volatile uint32_t write_cnt=0, index=0;
volatile HAL_StatusTypeDef status;
status = HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);
while(index < data_length) {
if (status == HAL_OK) {
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, FLASH_STORAGE + write_cnt, data_to_FLASH[index]);
if(status == HAL_OK) {
write_cnt += 8;
index++;
}
}
}
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();