FlashEE.c 24 KB

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