cancel
Showing results for 
Search instead for 
Did you mean: 

Issue when writing data to flash (data erasing on reboot?)

JayDev
Senior II

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!

1 ACCEPTED SOLUTION

Accepted Solutions

There's not really a mode of behaviour that does this

Make sure your own erase code isn't firing, only execute it with direct user interaction. ie you enter a command or code/password

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

There's not really a mode of behaviour that does this

Make sure your own erase code isn't firing, only execute it with direct user interaction. ie you enter a command or code/password

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I did find the issue and it's a fairly dumb one (and I think might've related to previous advice of yours, actually). I have two programs, one for programming the flash data, one for reading the data. It sounds like when I programmed the "reading data" project, the project was getting powered down and powered back up (at least I assume so) and, in that time, it was starting the previous "programming" code for a brief period of time, but just enough to actually wipe the data.

I had moved the "read data" to a button press as you had previously recommended but I hadn't done the same with the "program data", which seemed to be causing the issue. Resolving this seeems to have fixed that issue (still have another one but I have to do a little more investigation to see if I can find the root cause).

Anyway, just wanted to say thanks for all the advice!