cancel
Showing results for 
Search instead for 
Did you mean: 

FLASH is Zeroed Automatically!

Ala
Senior

hello

I am using a STM32f030c8t6. it takes 2 ADC and shows them on a seven segment. I set some parameters in FLASH and save them. my problem is that it suddenly stopped saving values and just returns 0. I noticed this happened just as I detached ADC inputs and after that it is not saving any parameter anymore! even when I attach ADC inputs back.

why suddenly FLASH memory is 0ed?

10 REPLIES 10

honestly uint16 or 32 can be replaced in the code by manipulating it.

I tried to write/read the FLASH using the codes I developed below

for write

uint8_t write_data_to_flash(int32_t *data,uint32_t size)
{
	/* Unlock the Flash to enable the flash control register access *************/
  HAL_FLASH_Unlock();
 
  /* Erase the user Flash area
    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
 
  /* Fill EraseInit structure*/
  EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
  EraseInitStruct.PageAddress = FLASH_USER_START_ADDR;
  EraseInitStruct.NbPages     = 1;//(FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
 
  /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
     you have to make sure that these data are rewritten before they are accessed during code
     execution. If this cannot be done safely, it is recommended to flush the caches by setting the
     DCRST and ICRST bits in the FLASH_CR register. */
  if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  {
    /*
      Error occurred while page erase.
      User can add here some code to deal with this error.
      PAGEError will contain the faulty page and then to know the code error on this page,
      user can call function 'HAL_FLASH_GetError()'
    */
		a1=-1;
	}
	/* Program the user Flash area word by word
    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
 
  Address = FLASH_USER_START_ADDR;
 
  for(uint32_t i=0;i<size;i++)
  {
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, flash_buffer_w[i]) == HAL_OK)
    {
      Address = Address + 4;
    }
   else
    {
      /* Error occurred while writing data in Flash memory.
         User can add here some code to deal with this error */
      a1=-2;
    }
  }
 
  /* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation) *********/
  HAL_FLASH_Lock();
}

and for read

void 		read_data_from_flash(int32_t *buf,uint32_t size)
{
	/* Check if the programmed data is OK
	MemoryProgramStatus = 0: data programmed correctly
	MemoryProgramStatus != 0: number of words not programmed correctly ******/
  Address = FLASH_USER_START_ADDR;
 
  for(uint32_t i=0;i<size;i++)
  {
    *buf++ = *(__IO uint32_t *)Address;
    Address = Address + 4;
  }
 
}

these were working just fine, but then again suddenly as I powered the board, I noticed the whole FLASH is 0ed...

I'm confused...