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