AnsweredAssumed Answered

STM32F105 eeprom simulation

Question asked by lemos.hugo on Jan 13, 2015
Latest reply on Jan 13, 2015 by lemos.hugo
Hello

I am try write a configuration in a microcontroller STM32F105.

I am using stsw-stm32010 code base example.

The example work properly.
But when I make my code don't work.

I want write my config data from a memory address and after I want read then.
I don't have problems with read.
But i can't over write the memory position. It is like i write all the data in the same memory position. And i don't understand it.

bool EscribeDatosEnFlash(char  *Datos, uint32_t offset)
{
  u32 i;
  u32 tamanho = IndexArrayFin; //strlen(Datos);
  uint16_t estado;
//  uint16_t VarValue = 0;
   
  //  ***********************************************************
  //  * 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
  //  ************************************************************
     
  FLASH_Unlock();//unlock flash writing
   
  EE_Init();
   
  for (i = 0; i < tamanho; i++)
  {       
    estado = EE_EscribeVariable((EEPROM_START_ADDRESS+i), Datos[i]);       
  
   
  FLASH_Lock();//lock the flash for writing
   
  if (estado ==  FLASH_COMPLETE) return true;
  if (estado ==  PAGE_FULL) return false;
  if (estado ==  NO_VALID_PAGE) return false;  
  else return true; 
}
 
 
/**
  * @brief  Writes/upadtes variable data in EEPROM.
  * @param  Direcion: Variable address
  * @param  Dato: 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
  */
uint16_t EE_EscribeVariable(uint32_t Direcion, uint16_t Dato)
{
   
  uint16_t Status = 0;
 
  // Write the variable virtual address and value in the EEPROM
  Status = EE_VerificaYEscribeVariable(Direcion, Dato);
 
  // In case the EEPROM active page is full
  if (Status == PAGE_FULL)
  {
    // Perform Page transfer
    Status = EE_PageTransfer(Direcion, Dato);
  }
 
  // Return last operation status
  return Status;
}
 
/**
  * @brief  Verifica si la pagina y Escribe en la EEPROM.
  * @param  Direcion: Variable address
  * @param  Dato: 16 bit data to be written as variable value
  * @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
  */
 uint16_t EE_VerificaYEscribeVariable(uint32_t DirecionIn, uint16_t Dato)
{
  FLASH_Status FlashStatus = FLASH_COMPLETE;
  uint16_t ValidPage = PAGE0;
  uint32_t Address = DirecionIn;
 
  /* 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)(DirecionIn + (uint32_t)(ValidPage * PAGE_SIZE));
 
  FlashStatus = FLASH_ProgramHalfWord(Address, Dato);
  return FlashStatus;
}
 
/**
  * @brief  Verify if active page is full and Writes variable in EEPROM.
  * @param  VirtAddress: 16 bit virtual address of the variable
  * @param  Data: 16 bit data to be written as variable value
  * @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_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
{
  FLASH_Status FlashStatus = FLASH_COMPLETE;
  uint16_t ValidPage = PAGE0;
  uint32_t Address = 0x08010000, PageEndAddress = 0x080107FF;
 
  /* 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 - 2) + (uint32_t)((1 + ValidPage) * 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 = FLASH_ProgramHalfWord(Address, Data);
      /* If program operation was failed, a Flash error code is returned */
      if (FlashStatus != FLASH_COMPLETE)
      {
        return FlashStatus;
      }
      /* Set variable virtual address */
      FlashStatus = FLASH_ProgramHalfWord(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;
}

Some One can help me???

Best Regards

Outcomes