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
MM..1
Chief II

Maybe better is show your code used to manipulate flash. ... your question is little black cbox

In STM32F0xx, erased FLASH is all 0xFF.

JW

you're right, I should say 0

here is a snippet

in main() I read my values

HAL_FLASH_Unlock();
EE_Init();
EE_ReadVariable(CAL_L_m3_FLASH,&flash_buffer_r[CAL_L_m3_FLASH]);

for writing, first I try to erase the whole page using this code

uint32_t PAGEError = 0;
/*Variable used for Erase procedure*/
static FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = EEPROM_START_ADDRESS;
EraseInitStruct.NbPages     = 1;
											HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);

and then in write I use this code

EE_WriteVariable(CAL_L_m3_FLASH,cal_l_m3_d);

which in detail is

uint16_t EE_WriteVariable(uint16_t VirtAddress, volatile uint32_t Data)
{
  uint16_t Status = 0;
	
  /* Write the variable virtual address and value in the EEPROM */
  Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
 
  /* In case the EEPROM active page is full */
  if (Status == PAGE_FULL)
  {
    /* Perform Page transfer */
    Status = EE_PageTransfer(VirtAddress, Data);
  }
 
  /* Return last operation status */
  return Status;
}

more detail

static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
{
  HAL_StatusTypeDef flashstatus = HAL_OK;
  uint16_t validpage = PAGE0;
  uint32_t address = EEPROM_START_ADDRESS, pageendaddress = EEPROM_START_ADDRESS+PAGE_SIZE;
 
  /* Get valid Page for write operation */
  validpage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
 
  /* Check if there is no valid page */
  if (validpage == NO_VALID_PAGE)
  {
    return  NO_VALID_PAGE;
  }
 
  /* Get the valid Page start address */
  address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(validpage * PAGE_SIZE));
 
  /* Get the valid Page end address */
  pageendaddress = (uint32_t)((EEPROM_START_ADDRESS - 1) + (uint32_t)((validpage + 1) * PAGE_SIZE));
 
  /* Check each active page address starting from begining */
  while (address < pageendaddress)
  {
    /* Verify if address and address+2 contents are 0xFFFFFFFF */
    if ((*(__IO uint32_t*)address) == 0xFFFFFFFF)
    {
      /* Set variable data */
      flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address, Data);       
      /* If program operation was failed, a Flash error code is returned */
      if (flashstatus != HAL_OK)
      {
        return flashstatus;
      }
      /* Set variable virtual address */
      flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address + 2, VirtAddress);       
      /* Return program operation status */
      return flashstatus;
    }
    else
    {
      /* Next address location */
      address = address + 4;
    }
  }
 
  /* Return PAGE_FULL in case the valid page is full */
  return PAGE_FULL;
}

TDK
Guru

I seem to recall FLASH showing up as 0's is also an indication that the link between the software and the chip has failed. If you power down everything, restart and connect with STM32CubeProgrammer, does it still show as 0? What software/program exactly is showing it as 0 or are you inferring that from "if ((*(__IO uint32_t*)address) == 0xFFFFFFFF)" not triggering?

If you feel a post has answered your question, please click "Accept as Solution".
MM..1
Chief II

Primary basic prog practice check return status

status = EE_WriteVariable(CAL_L_m3_FLASH,cal_l_m3_d);

IF ...

dear @TDK

yes it still shows 0. even with a restart. seems like when the addresses are 0, they are not 0xFFFFFFFF any more, so not able to write on them and read them 0 as they are zeroed

sounds good! I tried this and I cant recall any good out of it

Debug your code .... For example normal from github

/*
 * Writes/upadtes variable data in EEPROM.
 * param VirtAddress: Variable virtual address
 * param Data: 16 bit data to be written
 * retval : - Success or error status:
 * - FLASH_COMPLETE: on success,
 * - PAGE_FULL: if valid page is full
 * - NO_VALID_PAGE: if no valid page was found
 * - Flash error code: on write Flash error
 */
static uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
{

data must be uint16 ...