Subversion Repositories DashDisplay

Rev

Rev 18 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    EEPROM_Emulation/src/eeprom.c
  4.   * @author  MCD Application Team
  5.   * @version V3.1.0
  6.   * @date    07/27/2009
  7.   * @brief   This file provides all the EEPROM emulation firmware functions.
  8.   ******************************************************************************
  9.   * @copy
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  19.   */
  20. /** @addtogroup EEPROM_Emulation
  21.   * @{
  22.   */
  23.  
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "eeprom.h"
  26.  
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* Private define ------------------------------------------------------------*/
  29. /* Private macro -------------------------------------------------------------*/
  30. /* Private variables ---------------------------------------------------------*/
  31.  
  32.  
  33.  FlashPage_t __attribute__((section(".EEPROM_Data"))) FlashPage[2];
  34.  
  35.  
  36. /* Global variable used to store variable value in read sequence */
  37. uint16_t DataVar = 0;
  38.  
  39. /* Virtual address defined by the user: 0xFFFF value is prohibited */
  40. extern uint16_t VirtAddVarTab[NumbOfVar];
  41.  
  42. /* Private function prototypes -----------------------------------------------*/
  43. /* Private functions ---------------------------------------------------------*/
  44. static FLASH_Status EE_Format(void);
  45. static uint16_t EE_FindValidPage(uint8_t Operation);
  46. static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data);
  47. static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data);
  48.  
  49. /**
  50.   * @brief  Restore the pages to a known good state in case of page's status
  51.   *   corruption after a power loss.
  52.   * @param  None.
  53.   * @retval - Flash error code: on write Flash error
  54.   *         - FLASH_COMPLETE: on success
  55.   */
  56. uint16_t EE_Init(void)
  57. {
  58.   uint16_t PageStatus0 = 6, PageStatus1 = 6;
  59.   uint16_t VarIdx = 0;
  60.   uint16_t EepromStatus = 0, ReadStatus = 0;
  61.   int16_t x = -1;
  62.   uint16_t  FlashStatus;
  63.  
  64.   /* Get Page0 status */
  65.   PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
  66.   /* Get Page1 status */
  67.   PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
  68.  
  69.   /* Check for invalid header states and repair if necessary */
  70.   switch (PageStatus0)
  71.   {
  72.     case ERASED:
  73.       if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
  74.       {
  75.         /* Erase Page0 */
  76.         FLASH_PageErase(PAGE0_BASE_ADDRESS);
  77.         /* If erase operation was failed, a Flash error code is returned */
  78.         FlashStatus = HAL_FLASH_GetError();
  79.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  80.         {
  81.           return FlashStatus;
  82.         }
  83.       }
  84.       else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */
  85.       {
  86.         /* Erase Page0 */
  87.         FLASH_PageErase(PAGE0_BASE_ADDRESS);
  88.         /* If erase operation was failed, a Flash error code is returned */
  89.         FlashStatus = HAL_FLASH_GetError();
  90.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  91.         {
  92.           return FlashStatus;
  93.         }
  94.         /* Mark Page1 as valid */
  95.  
  96.         HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE1_BASE_ADDRESS, VALID_PAGE);
  97.         /* If program operation was failed, a Flash error code is returned */
  98.         FlashStatus = HAL_FLASH_GetError();
  99.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  100.         {
  101.           return FlashStatus;
  102.         }
  103.       }
  104.       else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
  105.       {
  106.         /* Erase both Page0 and Page1 and set Page0 as valid page */
  107.         FlashStatus = EE_Format();
  108.         /* If erase/program operation was failed, a Flash error code is returned */
  109.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  110.         {
  111.           return FlashStatus;
  112.         }
  113.       }
  114.       break;
  115.  
  116.     case RECEIVE_DATA:
  117.       if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */
  118.       {
  119.         /* Transfer data from Page1 to Page0 */
  120.         for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
  121.         {
  122.           if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
  123.           {
  124.             x = VarIdx;
  125.           }
  126.           if (VarIdx != x)
  127.           {
  128.             /* Read the last variables' updates */
  129.             ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
  130.             /* In case variable corresponding to the virtual address was found */
  131.             if (ReadStatus != 0x1)
  132.             {
  133.               /* Transfer the variable to the Page0 */
  134.               EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
  135.               /* If program operation was failed, a Flash error code is returned */
  136.               if (EepromStatus != HAL_FLASH_ERROR_NONE)
  137.               {
  138.                 return EepromStatus;
  139.               }
  140.             }
  141.           }
  142.         }
  143.         /* Mark Page0 as valid */
  144.         HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE0_BASE_ADDRESS, VALID_PAGE);
  145.         /* If program operation was failed, a Flash error code is returned */
  146.         FlashStatus = HAL_FLASH_GetError();
  147.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  148.         {
  149.           return FlashStatus;
  150.         }
  151.         /* Erase Page1 */
  152.         FLASH_PageErase(PAGE1_BASE_ADDRESS);
  153.         /* If erase operation was failed, a Flash error code is returned */
  154.         FlashStatus = HAL_FLASH_GetError();
  155.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  156.             {
  157.           return FlashStatus;
  158.         }
  159.       }
  160.       else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
  161.       {
  162.         /* Erase Page1 */
  163.         FLASH_PageErase(PAGE1_BASE_ADDRESS);
  164.         /* If erase operation was failed, a Flash error code is returned */
  165.         FlashStatus = HAL_FLASH_GetError();
  166.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  167.         {
  168.           return FlashStatus;
  169.         }
  170.         /* Mark Page0 as valid */
  171.         HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE0_BASE_ADDRESS, VALID_PAGE);
  172.  
  173.         /* If program operation was failed, a Flash error code is returned */
  174.         FlashStatus = HAL_FLASH_GetError();
  175.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  176.         {
  177.           return FlashStatus;
  178.         }
  179.       }
  180.       else /* Invalid state -> format eeprom */
  181.       {
  182.         /* Erase both Page0 and Page1 and set Page0 as valid page */
  183.         FlashStatus = EE_Format();
  184.         /* If erase/program operation was failed, a Flash error code is returned */
  185.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  186.         {
  187.           return FlashStatus;
  188.         }
  189.       }
  190.       break;
  191.  
  192.     case VALID_PAGE:
  193.       if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
  194.       {
  195.         /* Erase both Page0 and Page1 and set Page0 as valid page */
  196.         FlashStatus = EE_Format();
  197.         /* If erase/program operation was failed, a Flash error code is returned */
  198.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  199.         {
  200.           return FlashStatus;
  201.         }
  202.       }
  203.       else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
  204.       {
  205.         /* Erase Page1 */
  206.         FLASH_PageErase(PAGE1_BASE_ADDRESS);
  207.         FlashStatus = HAL_FLASH_GetError();
  208.         /* If erase operation was failed, a Flash error code is returned */
  209.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  210.         {
  211.           return FlashStatus;
  212.         }
  213.       }
  214.       else /* Page0 valid, Page1 receive */
  215.       {
  216.         /* Transfer data from Page0 to Page1 */
  217.         for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
  218.         {
  219.           if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
  220.           {
  221.             x = VarIdx;
  222.           }
  223.           if (VarIdx != x)
  224.           {
  225.             /* Read the last variables' updates */
  226.             ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
  227.             /* In case variable corresponding to the virtual address was found */
  228.             if (ReadStatus != 0x1)
  229.             {
  230.               /* Transfer the variable to the Page1 */
  231.               EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
  232.               /* If program operation was failed, a Flash error code is returned */
  233.               if (EepromStatus != HAL_FLASH_ERROR_NONE)
  234.               {
  235.                 return EepromStatus;
  236.               }
  237.             }
  238.           }
  239.         }
  240.         /* Mark Page1 as valid */
  241.  
  242.         HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE1_BASE_ADDRESS, VALID_PAGE);
  243.         /* If program operation was failed, a Flash error code is returned */
  244.         FlashStatus = HAL_FLASH_GetError();
  245.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  246.         {
  247.           return FlashStatus;
  248.         }
  249.         /* Erase Page0 */
  250.         FLASH_PageErase(PAGE0_BASE_ADDRESS);
  251.         /* If erase operation was failed, a Flash error code is returned */
  252.         FlashStatus = HAL_FLASH_GetError();
  253.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  254.         {
  255.           return FlashStatus;
  256.         }
  257.       }
  258.       break;
  259.  
  260.     default:  /* Any other state -> format eeprom */
  261.       /* Erase both Page0 and Page1 and set Page0 as valid page */
  262.       FlashStatus = EE_Format();
  263.       /* If erase/program operation was failed, a Flash error code is returned */
  264.       if (FlashStatus != HAL_FLASH_ERROR_NONE)
  265.       {
  266.         return FlashStatus;
  267.       }
  268.       break;
  269.   }
  270.  
  271.   return HAL_FLASH_ERROR_NONE;
  272. }
  273.  
  274. /**
  275.   * @brief  Returns the last stored variable data, if found, which correspond to
  276.   *   the passed virtual address
  277.   * @param  VirtAddress: Variable virtual address
  278.   * @param  Data: Global variable contains the read variable value
  279.   * @retval Success or error status:
  280.   *           - 0: if variable was found
  281.   *           - 1: if the variable was not found
  282.   *           - NO_VALID_PAGE: if no valid page was found.
  283.   */
  284. uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data)
  285. {
  286.   uint16_t ValidPage = PAGE0;
  287.   uint16_t AddressValue = 0x5555, ReadStatus = 1;
  288.   uint32_t Address = 0x08010000, PageStartAddress = 0x08010000;
  289.  
  290.   /* Get active Page for read operation */
  291.   ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
  292.  
  293.   /* Check if there is no valid page */
  294.   if (ValidPage == NO_VALID_PAGE)
  295.   {
  296.     return  NO_VALID_PAGE;
  297.   }
  298.  
  299.   /* Get the valid Page start Address */
  300.   PageStartAddress = (uint32_t)(PAGE0_BASE_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
  301.  
  302.   /* Get the valid Page end Address */
  303.   Address = (uint32_t)((PAGE0_BASE_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
  304.  
  305.   /* Check each active page address starting from end */
  306.   while (Address > (PageStartAddress + 2))
  307.   {
  308.     /* Get the current location content to be compared with virtual address */
  309.     AddressValue = (*(__IO uint16_t*)Address);
  310.  
  311.     /* Compare the read address with the virtual address */
  312.     if (AddressValue == VirtAddress)
  313.     {
  314.       /* Get content of Address-2 which is variable value */
  315.       *Data = (*(__IO uint16_t*)(Address - 2));
  316.  
  317.       /* In case variable value is read, reset ReadStatus flag */
  318.       ReadStatus = 0;
  319.  
  320.       break;
  321.     }
  322.     else
  323.     {
  324.       /* Next address location */
  325.       Address = Address - 4;
  326.     }
  327.   }
  328.  
  329.   /* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */
  330.   return ReadStatus;
  331. }
  332.  
  333. /**
  334.   * @brief  Writes/upadtes variable data in EEPROM.
  335.   * @param  VirtAddress: Variable virtual address
  336.   * @param  Data: 16 bit data to be written
  337.   * @retval Success or error status:
  338.   *           - FLASH_COMPLETE: on success
  339.   *           - PAGE_FULL: if valid page is full
  340.   *           - NO_VALID_PAGE: if no valid page was found
  341.   *           - Flash error code: on write Flash error
  342.   */
  343. uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
  344. {
  345.   uint16_t Status = 0;
  346.  
  347.   /* Write the variable virtual address and value in the EEPROM */
  348.   Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
  349.  
  350.   /* In case the EEPROM active page is full */
  351.   if (Status == PAGE_FULL)
  352.   {
  353.     /* Perform Page transfer */
  354.     Status = EE_PageTransfer(VirtAddress, Data);
  355.   }
  356.  
  357.   /* Return last operation status */
  358.   return Status;
  359. }
  360.  
  361. /**
  362.   * @brief  Erases PAGE0 and PAGE1 and writes VALID_PAGE header to PAGE0
  363.   * @param  None
  364.   * @retval Status of the last operation (Flash write or erase) done during
  365.   *         EEPROM formating
  366.   */
  367. static FLASH_Status EE_Format(void)
  368. {
  369.   FLASH_Status FlashStatus = HAL_FLASH_ERROR_NONE;
  370.  
  371.   /* Erase Page0 */
  372.   FLASH_PageErase(PAGE0_BASE_ADDRESS);
  373.  
  374.   /* If erase operation was failed, a Flash error code is returned */
  375.   FlashStatus = HAL_FLASH_GetError();
  376.   if (FlashStatus != HAL_FLASH_ERROR_NONE)
  377.   {
  378.     return FlashStatus;
  379.   }
  380.  
  381.   /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
  382.   HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE0_BASE_ADDRESS, VALID_PAGE);
  383.    /* If program operation was failed, a Flash error code is returned */
  384.   FlashStatus = HAL_FLASH_GetError();
  385.     if (FlashStatus != HAL_FLASH_ERROR_NONE)
  386.    {
  387.      return FlashStatus;
  388.    }
  389.  
  390.  
  391.   /* Erase Page1 */
  392.   FLASH_PageErase(PAGE1_BASE_ADDRESS);
  393.   FlashStatus = HAL_FLASH_GetError();
  394.  
  395.   /* Return Page1 erase operation status */
  396.   return FlashStatus;
  397. }
  398.  
  399. /**
  400.   * @brief  Find valid Page for write or read operation
  401.   * @param  Operation: operation to achieve on the valid page.
  402.   *   This parameter can be one of the following values:
  403.   *     @arg READ_FROM_VALID_PAGE: read operation from valid page
  404.   *     @arg WRITE_IN_VALID_PAGE: write operation from valid page
  405.   * @retval Valid page number (PAGE0 or PAGE1) or NO_VALID_PAGE in case
  406.   *   of no valid page was found
  407.   */
  408. static uint16_t EE_FindValidPage(uint8_t Operation)
  409. {
  410.   uint16_t PageStatus0 = 6, PageStatus1 = 6;
  411.  
  412.   /* Get Page0 actual status */
  413.   PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
  414.  
  415.   /* Get Page1 actual status */
  416.   PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
  417.  
  418.   /* Write or read operation */
  419.   switch (Operation)
  420.   {
  421.     case WRITE_IN_VALID_PAGE:   /* ---- Write operation ---- */
  422.       if (PageStatus1 == VALID_PAGE)
  423.       {
  424.         /* Page0 receiving data */
  425.         if (PageStatus0 == RECEIVE_DATA)
  426.         {
  427.           return PAGE0;         /* Page0 valid */
  428.         }
  429.         else
  430.         {
  431.           return PAGE1;         /* Page1 valid */
  432.         }
  433.       }
  434.       else if (PageStatus0 == VALID_PAGE)
  435.       {
  436.         /* Page1 receiving data */
  437.         if (PageStatus1 == RECEIVE_DATA)
  438.         {
  439.           return PAGE1;         /* Page1 valid */
  440.         }
  441.         else
  442.         {
  443.           return PAGE0;         /* Page0 valid */
  444.         }
  445.       }
  446.       else
  447.       {
  448.         return NO_VALID_PAGE;   /* No valid Page */
  449.       }
  450.  
  451.     case READ_FROM_VALID_PAGE:  /* ---- Read operation ---- */
  452.       if (PageStatus0 == VALID_PAGE)
  453.       {
  454.         return PAGE0;           /* Page0 valid */
  455.       }
  456.       else if (PageStatus1 == VALID_PAGE)
  457.       {
  458.         return PAGE1;           /* Page1 valid */
  459.       }
  460.       else
  461.       {
  462.         return NO_VALID_PAGE ;  /* No valid Page */
  463.       }
  464.  
  465.     default:
  466.       return PAGE0;             /* Page0 valid */
  467.   }
  468. }
  469.  
  470. /**
  471.   * @brief  Verify if active page is full and Writes variable in EEPROM.
  472.   * @param  VirtAddress: 16 bit virtual address of the variable
  473.   * @param  Data: 16 bit data to be written as variable value
  474.   * @retval Success or error status:
  475.   *           - FLASH_COMPLETE: on success
  476.   *           - PAGE_FULL: if valid page is full
  477.   *           - NO_VALID_PAGE: if no valid page was found
  478.   *           - Flash error code: on write Flash error
  479.   */
  480. static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
  481. {
  482.   FLASH_Status FlashStatus = HAL_FLASH_ERROR_NONE;
  483.   uint16_t ValidPage = PAGE0;
  484.   uint32_t Address = 0x08010000, PageEndAddress = 0x080107FF;
  485.  
  486.   /* Get valid Page for write operation */
  487.   ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
  488.  
  489.   /* Check if there is no valid page */
  490.   if (ValidPage == NO_VALID_PAGE)
  491.   {
  492.     return  NO_VALID_PAGE;
  493.   }
  494.  
  495.   /* Get the valid Page start Address */
  496.   Address = (uint32_t)(PAGE0_BASE_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
  497.  
  498.   /* Get the valid Page end Address */
  499.   PageEndAddress = (uint32_t)((PAGE0_BASE_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
  500.  
  501.   /* Check each active page address starting from begining */
  502.   while (Address < PageEndAddress)
  503.   {
  504.     /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
  505.     if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF)
  506.     {
  507.       /* Set variable data */
  508.        HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Address, Data);
  509.        /* If program operation was failed, a Flash error code is returned */
  510.       FlashStatus = HAL_FLASH_GetError();
  511.         if (FlashStatus != HAL_FLASH_ERROR_NONE)
  512.        {
  513.          return FlashStatus;
  514.        }
  515.      /* Set variable virtual address */
  516.  
  517.       HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Address + 2 ,VirtAddress);
  518.        /* If program operation was failed, a Flash error code is returned */
  519.       FlashStatus = HAL_FLASH_GetError();
  520.       /* Return program operation status */
  521.       return FlashStatus;
  522.     }
  523.     else
  524.     {
  525.       /* Next address location */
  526.       Address = Address + 4;
  527.     }
  528.   }
  529.  
  530.   /* Return PAGE_FULL in case the valid page is full */
  531.   return PAGE_FULL;
  532. }
  533.  
  534. /**
  535.   * @brief  Transfers last updated variables data from the full Page to
  536.   *   an empty one.
  537.   * @param  VirtAddress: 16 bit virtual address of the variable
  538.   * @param  Data: 16 bit data to be written as variable value
  539.   * @retval Success or error status:
  540.   *           - FLASH_COMPLETE: on success
  541.   *           - PAGE_FULL: if valid page is full
  542.   *           - NO_VALID_PAGE: if no valid page was found
  543.   *           - Flash error code: on write Flash error
  544.   */
  545. static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
  546. {
  547.   FLASH_Status FlashStatus = HAL_FLASH_ERROR_NONE;
  548.   uint32_t NewPageAddress = 0x080103FF, OldPageAddress = 0x08010000;
  549.   uint16_t ValidPage = PAGE0, VarIdx = 0;
  550.   uint16_t EepromStatus = 0, ReadStatus = 0;
  551.  
  552.   /* Get active Page for read operation */
  553.   ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
  554.  
  555.   if (ValidPage == PAGE1)       /* Page1 valid */
  556.   {
  557.     /* New page address where variable will be moved to */
  558.     NewPageAddress = PAGE0_BASE_ADDRESS;
  559.  
  560.     /* Old page address where variable will be taken from */
  561.     OldPageAddress = PAGE1_BASE_ADDRESS;
  562.   }
  563.   else if (ValidPage == PAGE0)  /* Page0 valid */
  564.   {
  565.     /* New page address where variable will be moved to */
  566.     NewPageAddress = PAGE1_BASE_ADDRESS;
  567.  
  568.     /* Old page address where variable will be taken from */
  569.     OldPageAddress = PAGE0_BASE_ADDRESS;
  570.   }
  571.   else
  572.   {
  573.     return NO_VALID_PAGE;       /* No valid Page */
  574.   }
  575.  
  576.   /* Set the new Page status to RECEIVE_DATA status */
  577.   HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,NewPageAddress,RECEIVE_DATA);
  578.   FlashStatus = HAL_FLASH_GetError();
  579.   /* If program operation was failed, a Flash error code is returned */
  580.   if (FlashStatus !=  HAL_FLASH_ERROR_NONE)
  581.   {
  582.     return FlashStatus;
  583.   }
  584.  
  585.   /* Write the variable passed as parameter in the new active page */
  586.   EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
  587.   /* If program operation was failed, a Flash error code is returned */
  588.   if (EepromStatus !=  HAL_FLASH_ERROR_NONE)
  589.   {
  590.     return EepromStatus;
  591.   }
  592.  
  593.   /* Transfer process: transfer variables from old to the new active page */
  594.   for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
  595.   {
  596.     if (VirtAddVarTab[VarIdx] != VirtAddress)  /* Check each variable except the one passed as parameter */
  597.     {
  598.       /* Read the other last variable updates */
  599.       ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
  600.       /* In case variable corresponding to the virtual address was found */
  601.       if (ReadStatus != 0x1)
  602.       {
  603.         /* Transfer the variable to the new active page */
  604.         EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
  605.         /* If program operation was failed, a Flash error code is returned */
  606.         if (EepromStatus != HAL_FLASH_ERROR_NONE)
  607.         {
  608.           return EepromStatus;
  609.         }
  610.       }
  611.     }
  612.   }
  613.  
  614.   /* Erase the old Page: Set old Page status to ERASED status */
  615.   FLASH_PageErase(OldPageAddress);
  616.   /* If erase operation was failed, a Flash error code is returned */
  617.   FlashStatus = HAL_FLASH_GetError();
  618.   if (FlashStatus !=  HAL_FLASH_ERROR_NONE)
  619.   {
  620.     return FlashStatus;
  621.   }
  622.  
  623.   /* Set new Page status to VALID_PAGE status */
  624.   HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,NewPageAddress, VALID_PAGE);
  625.   /* If program operation was failed, a Flash error code is returned */
  626.   FlashStatus = HAL_FLASH_GetError();
  627.   if (FlashStatus !=  HAL_FLASH_ERROR_NONE)
  628.   {
  629.     return FlashStatus;
  630.   }
  631.  
  632.   /* Return last operation flash status */
  633.   return FlashStatus;
  634. }
  635.  
  636. /**
  637.   * @}
  638.   */
  639.  
  640. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
  641.