Issue when writing data to flash (data erasing on reboot?)
- February 16, 2021
- 1 reply
- 1356 views
Hello! I'm writing data to flash and ran into an odd anomaly where the data is erasing itself on reboot. I have one program writing data to flash and another reading and it keeps reading back nothing. I ended up writing the data using one program and then reading the data using the STM32CubeProgrammer. First read shows my data. When I disconnect and reconnect, it's all gone again.
My current code is fairly simple. I have a flash location with 10 pages set aside:
/* Memories definition */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 40K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 106k
FLAG_VAR (rx) : ORIGIN = 0x8015800, LENGTH = 2k
LOC_VAR (rx) : ORIGIN = 0x8016000, LENGTH = 20k
}I am currently only using 1 page for testing purposes though. I am effectively erasing the flash and then counting up in the byteArray by 1, then writing 2048 bytes (one page). It seems to be working ok (as I mentioned, it's writing correctly, it's just erasing it a second time).
Unfortunately, I'm not entirely sure what is happening, so I'm not really sure how to correct this issue. Any idea what could cause the page to erase itself?
If it helps, here's the code I'm using (there are lots of external variables so if I miss something, I apologize):
void find_flash_anomalies()
{
uint8_t tempvar = 0;
for(uint32_t i = 0; i < flash_array_size_bytes; i++)
{
byteArray[i] = tempvar;
tempvar = (tempvar+1) % 256;
}
uint8_t num_pages = 1;
for(uint8_t i = 0; i < num_pages; i++)
{
if(erase_pages(location_flash_address, 1) == HAL_OK)
{
program_pages_u8(location_flash_address, &byteArray[0], 2048);
}
}
}
uint32_t erase_pages(uint32_t start_address, uint8_t num_pages)
{
FLASH_EraseInitTypeDef EraseInitStruct;
HAL_FLASH_Unlock();
uint32_t res = -1;
uint32_t PAGEError = 0;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks = GetBank(start_address);
EraseInitStruct.Page = GetPage(start_address);
EraseInitStruct.NbPages = num_pages;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
{
res = -1;
}
else
{
res = HAL_OK;
}
return res;
}
uint32_t program_pages_u8(uint32_t start_address, uint8_t *data, uint16_t num_u8)
{
uint32_t address = start_address;
uint64_t temp_doubleword = 0;
uint8_t temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8 = 0;
for(uint16_t i=0; i < (num_u8); i+=8)
{
temp1 = *(uint8_t*)(data+i);
temp2 = *(uint8_t*)(data+i+1);
temp3 = *(uint8_t*)(data+i+2);
temp4 = *(uint8_t*)(data+i+3);
temp5 = *(uint8_t*)(data+i+4);
temp6 = *(uint8_t*)(data+i+5);
temp7 = *(uint8_t*)(data+i+6);
temp8 = *(uint8_t*)(data+i+7);
temp_doubleword = ((uint64_t)temp1 << 0) + ((uint64_t)temp2 << 8) + ((uint64_t)temp3 << 16) +((uint64_t)temp4 << 24)
+ ((uint64_t)temp5 << 32) + ((uint64_t)temp6 << 40) + ((uint64_t)temp7 << 48) +((uint64_t)temp8 << 56) ;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, temp_doubleword) == HAL_OK)
{
address = address + 8;
}
else
{
return -1;
}
}
return HAL_OK;
}Any help you can give me or point to would be greatly appreciated. Not sure if I'm somehow corrupting the flash and it's reinitializing it at the beginning or what the issue could be. I'm a bit baffled at this time.
Thanks for all your help!
