Src/eeprom.c
/**
  * @brief  Transfers last updated variables data from the full Page to
  *   an empty one.
  * @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
  */
@brief  Transfers last updated variables data from the full Page to
an empty one.
@brief Transfers last updated variables data from the full Page to an empty one .
static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
{
  FLASH_Status FlashStatus = FLASH_COMPLETE;
  uint32_t NewPageAddress = 0x080103FF, OldPageAddress = 0x08010000;
  uint16_t ValidPage = PAGE0, VarIdx = 0;
  uint16_t EepromStatus = 0, ReadStatus = 0;
  ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
  if (ValidPage == PAGE1)       
  {
    NewPageAddress = PAGE0_BASE_ADDRESS;
    OldPageAddress = PAGE1_BASE_ADDRESS;
  }
  else if (ValidPage == PAGE0)  
  {
    NewPageAddress = PAGE1_BASE_ADDRESS;
    OldPageAddress = PAGE0_BASE_ADDRESS;
  }
  else
  {
    return NO_VALID_PAGE;       
  }
  FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, RECEIVE_DATA);
  if (FlashStatus != FLASH_COMPLETE)
  {
    return FlashStatus;
  }
  EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
  if (EepromStatus != FLASH_COMPLETE)
  {
    return EepromStatus;
  }
  for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
  {
    if (VirtAddVarTab[VarIdx] != VirtAddress)  
    {
      ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
      if (ReadStatus != 0x1)
      {
        EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
        if (EepromStatus != FLASH_COMPLETE)
        {
          return EepromStatus;
        }
      }
    }
  }
  FlashStatus = FLASH_ErasePage(OldPageAddress);
  if (FlashStatus != FLASH_COMPLETE)
  {
    return FlashStatus;
  }
  FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, VALID_PAGE);
  if (FlashStatus != FLASH_COMPLETE)
  {
    return FlashStatus;
  }
  return FlashStatus;
}
static uint16_t EE_PageTransfer ( uint16_t VirtAddress , uint16_t Data ) { FLASH_Status FlashStatus = FLASH_COMPLETE ; uint32_t NewPageAddress = 0x080103FF , OldPageAddress = 0x08010000 ; uint16_t ValidPage = PAGE0 , VarIdx = 0 ; uint16_t EepromStatus = 0 , ReadStatus = 0 ; ValidPage = EE_FindValidPage ( READ_FROM_VALID_PAGE ) ; if ( ValidPage == PAGE1 ) { NewPageAddress = PAGE0_BASE_ADDRESS ; OldPageAddress = PAGE1_BASE_ADDRESS ; } else if ( ValidPage == PAGE0 ) { NewPageAddress = PAGE1_BASE_ADDRESS ; OldPageAddress = PAGE0_BASE_ADDRESS ; } else { return NO_VALID_PAGE ; } FlashStatus = FLASH_ProgramHalfWord ( NewPageAddress , RECEIVE_DATA ) ; if ( FlashStatus != FLASH_COMPLETE ) { return FlashStatus ; } EepromStatus = EE_VerifyPageFullWriteVariable ( VirtAddress , Data ) ; if ( EepromStatus != FLASH_COMPLETE ) { return EepromStatus ; } for ( VarIdx = 0 ; VarIdx < NB_OF_VAR ; VarIdx ++ ) { if ( VirtAddVarTab [ VarIdx ] != VirtAddress ) { ReadStatus = EE_ReadVariable ( VirtAddVarTab [ VarIdx ] , & DataVar ) ; if ( ReadStatus != 0x1 ) { EepromStatus = EE_VerifyPageFullWriteVariable ( VirtAddVarTab [ VarIdx ] , DataVar ) ; if ( EepromStatus != FLASH_COMPLETE ) { return EepromStatus ; } } } } FlashStatus = FLASH_ErasePage ( OldPageAddress ) ; if ( FlashStatus != FLASH_COMPLETE ) { return FlashStatus ; } FlashStatus = FLASH_ProgramHalfWord ( NewPageAddress , VALID_PAGE ) ; if ( FlashStatus != FLASH_COMPLETE ) { return FlashStatus ; } return FlashStatus ; }
@brief  Transfers last updated variables data from the full Page to
   an empty one.
@brief Transfers last updated variables data from the full Page to an empty one .
/* Get active Page for read operation */ /* Page1 valid */ /* New page address where variable will be moved to */ /* Old page address where variable will be taken from */ /* Page0 valid */ /* New page address where variable will be moved to */ /* Old page address where variable will be taken from */ /* No valid Page */ /* Set the new Page status to RECEIVE_DATA status */ /* If program operation was failed, a Flash error code is returned */ /* Write the variable passed as parameter in the new active page */ /* If program operation was failed, a Flash error code is returned */ /* Transfer process: transfer variables from old to the new active page */ /* Check each variable except the one passed as parameter */ /* Read the other last variable updates */ /* In case variable corresponding to the virtual address was found */ /* Transfer the variable to the new active page */ /* If program operation was failed, a Flash error code is returned */ /* Erase the old Page: Set old Page status to ERASED status */ /* If erase operation was failed, a Flash error code is returned */ /* Set new Page status to VALID_PAGE status */ /* If program operation was failed, a Flash error code is returned */ /* Return last operation flash status */