HAL_FLASHEx_Erase error
I'm trying to store a simple runtime setting in flash memory of a stm32f103 with 64kb of flash.
In my testing I'm targeting the second to last page at 63 with address 0x0800FFE0
I can write fine to empty pages using HAL_FLASH_Program() but any call to HAL_FLASHEx_Erase() will throw this error....
"Error: Failed to read memory at 0xfffffffe"
I've been googling but can't seem to find a solution.
Here is my write function:
uint32_t Flash_Write_Data(uint32_t StartPageAddress, uint32_t *data) {
HAL_FLASH_Unlock();
// StartPageAddress = 0x0800FFE0, data = "hello"
int numberofwords = (strlen(data) / 4) + ((strlen(data) % 4) != 0);
static FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t PAGEError;
uint32_t StartPage = GetPage(StartPageAddress);
uint32_t EndPageAdress = StartPageAddress + numberofwords*4;
uint32_t EndPage = GetPage(EndPageAdress);
uint32_t NumberOfPages = ((EndPage - StartPage)/FLASH_PAGE_SIZE) +1;
EraseInitStruct.Banks = FLASH_BANK_1;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = StartPage;
EraseInitStruct.NbPages = NumberOfPages;
HAL_FLASH_Unlock();
// this will fail
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK){
return HAL_FLASH_GetError ();
}
int ctr = 0;
while (ctr < numberofwords) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartPageAddress, data[ctr]) == HAL_OK) {
StartPageAddress += 4;
ctr++;
} else {
return HAL_FLASH_GetError();
}
}
HAL_FLASH_Lock();
return 0;
}
