2024-11-04 10:39 AM
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;
}
2024-11-04 11:01 AM
The 2nd to last page is 0x08007800
0x0800FFE0 is not a valid start of a page
2024-11-04 02:25 PM
Hmm I think my original address is wrong but how did you get 0x08007800?
I'm looking at the memory, the last address is 0x0800FFFF so wouldn't the start of this page be at (-1023 bytes) 0x0800FC00 ? I'm working with 64kb version.
Either way, no matter what address I try to erase I get "Error: Failed to read memory at 0xfffffffe" in the stm32cubeide console. I can write just fine to empty locations.
2024-11-04 02:47 PM
Ahh figured it out, turns out my StartPage() function was returning the wrong value. Got things mixed up after combining couple examples.
2024-11-04 04:42 PM
@pulsar wrote:Hmm I think my original address is wrong but how did you get 0x08007800?
I'm looking at the memory, the last address is 0x0800FFFF so wouldn't the start of this page be at (-1023 bytes) 0x0800FC00 ? I'm working with 64kb version.
Either way, no matter what address I try to erase I get "Error: Failed to read memory at 0xfffffffe" in the stm32cubeide console. I can write just fine to empty locations.
You said "second to last page" so that is 2K. But my mistake, I meant to write 0x0800F800.
Had you just merely said "last page" then yes it would be 0x0800FC00