cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L412 flash section writing isn't permanent

CGadg
Associate III

It has worked at times but at others is becoming all 0xFFFF or whatever values become written into unused areas.

Currently it loses memory every time.

My code reads a block of settings and looks at one location to check if it's zero, which indicates no 'priming' of the settings is required. If not zero the settings are primed with default values with that bit cleared to zero. All locations are written to in the block, zero if not used. They are uint64_t values. Changes to settings all include that bit as zero, so the priming won't occur again. But on each startup that location is above zero.

I've tried adding __NOP() lines and delays immediately after writing also.

While the device is operating the settings are saved, but at power down they become overwritten.

 

The flash writing code is

void WriteSettings(void){
HAL_FLASH_Unlock();
   FirstPage = GetPage(FLASH_USER_START_ADDR);
   NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;  /* Get the number of pages to erase from 1st page */
   BankNumber = GetBank(FLASH_USER_START_ADDR);  /* Get the bank */
 
   EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;  EraseInitStruct.Banks = BankNumber;
   EraseInitStruct.Page = FirstPage;  EraseInitStruct.NbPages = NbOfPages;
 
   if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
   {while (1){HAL_Delay(100);  __NOP(); HAL_Delay(2000);}}// Error occurred while page erase.
 
   Address = FLASH_USER_START_ADDR;//PROGRAM THE FLASH WORD BY WORD
   while (Address < FLASH_USER_END_ADDR)
   {//HERE WRITE SPECIFIC DATA
  if(!CG_FlashCount){
  for(CG_FlashCount = 0; CG_FlashCount<(sizeof(CG_FlashItem))>>3; CG_FlashCount++){
  if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, CG_FlashItem[CG_FlashCount]) == HAL_OK){Address = Address + 8;}else{ while (1){ HAL_Delay(100);__NOP();HAL_Delay(2000);}}}
  }
//NOW THE REMAINING FLASH AREA
  if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, DATA_64) == HAL_OK){Address = Address + 8;}else{ while (1){ HAL_Delay(100);__NOP();HAL_Delay(2000);}}}/* Error occurred while writing data in Flash memory. User can add here some code to deal with this error */
 
   HAL_FLASH_Lock();/* Lock the Flash to disable the flash control register access (recommended to protect the FLASH memory against possible unwanted operation) *********/
}
1 ACCEPTED SOLUTION

Accepted Solutions
CGadg
Associate III

One last post but I will wait for comments:

The locations either side of the primer variable are unused. I wrote 7 and 8 to these. I could see those numbers in Cube Programmer.  It now seems to work OK! Does it need extra work if data zero is being written anywhere?

View solution in original post

4 REPLIES 4
CGadg
Associate III

I will add,  it must be a firmware issue because one board has been in the development rig and the others were programmed with slightly earlier code. They are fairing better or working. When I've programmed one of those with the latest code it suffers also. 

The default settings are fine. It's the triggering every time of these which is happening, showing the flash routine is somehow not working

CGadg
Associate III

The settings block is 50 locations of 64 bit = 3200 bits, and the block written to that I can see in Cube Programmer is 25 x 4 x 8 = 800 bits. I copied a code example and it seemed to work, I didn't check this mismatch. Should I decrease the read/write variables to uint16_t size? Still I don't know how it worked before whatever change happened.

At 0x0801D000 in Cube Programmer all are 0xFFFF... so that shows the flash isn't being overwritten by program code.

CGadg
Associate III

One last post but I will wait for comments:

The locations either side of the primer variable are unused. I wrote 7 and 8 to these. I could see those numbers in Cube Programmer.  It now seems to work OK! Does it need extra work if data zero is being written anywhere?

CGadg
Associate III

No posts from anyone!