2 ******************************************************************************
\r
3 * @file EEPROM_Emulation/src/eeprom.c
\r
4 * @author MCD Application Team
\r
7 * @brief This file provides all the EEPROM emulation firmware functions.
\r
8 ******************************************************************************
\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
18 * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
\r
20 /** @addtogroup EEPROM_Emulation
\r
24 /* Includes ------------------------------------------------------------------*/
\r
27 /* Private typedef -----------------------------------------------------------*/
\r
28 /* Private define ------------------------------------------------------------*/
\r
29 /* Private macro -------------------------------------------------------------*/
\r
30 /* Private variables ---------------------------------------------------------*/
\r
32 FLASH_Status FlashStatus;
\r
35 // Virtual address defined by the user: 0xFFFF value is prohibited
\r
36 uint16_t VirtAddVarTab[NumbOfVar] = {
\r
52 /* Global variable used to store variable value in read sequence */
\r
53 uint16_t DataVar = 0;
\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
63 * @brief Restore the pages to a known good state in case of page's status
\r
64 * corruption after a power loss.
\r
66 * @retval - Flash error code: on write Flash error
\r
67 * - FLASH_COMPLETE: on success
\r
69 uint16_t EE_Init(void)
\r
71 uint16_t PageStatus0 = 6, PageStatus1 = 6;
\r
72 uint16_t VarIdx = 0;
\r
73 uint16_t EepromStatus = 0, ReadStatus = 0;
\r
75 uint16_t FlashStatus;
\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
82 /* Check for invalid header states and repair if necessary */
\r
83 switch (PageStatus0)
\r
86 if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
\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
96 else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */
\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
103 return FlashStatus;
\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
110 return FlashStatus;
\r
113 else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
\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
120 return FlashStatus;
\r
126 if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */
\r
128 /* Transfer data from Page1 to Page0 */
\r
129 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
\r
131 if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
\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
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
147 return EepromStatus;
\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
157 return FlashStatus;
\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
164 return FlashStatus;
\r
167 else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
\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
174 return FlashStatus;
\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
181 return FlashStatus;
\r
184 else /* Invalid state -> format eeprom */
\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
191 return FlashStatus;
\r
197 if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
\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
204 return FlashStatus;
\r
207 else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
\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
214 return FlashStatus;
\r
217 else /* Page0 valid, Page1 receive */
\r
219 /* Transfer data from Page0 to Page1 */
\r
220 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
\r
222 if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
\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
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
238 return EepromStatus;
\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
248 return FlashStatus;
\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
255 return FlashStatus;
\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
266 return FlashStatus;
\r
271 return FLASH_COMPLETE;
\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
284 uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data)
\r
286 uint16_t ValidPage = PAGE0;
\r
287 uint16_t AddressValue = 0x5555, ReadStatus = 1;
\r
288 uint32_t Address = 0x08010000, PageStartAddress = 0x08010000;
\r
290 /* Get active Page for read operation */
\r
291 ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
\r
293 /* Check if there is no valid page */
\r
294 if (ValidPage == NO_VALID_PAGE)
\r
296 return NO_VALID_PAGE;
\r
299 /* Get the valid Page start Address */
\r
300 PageStartAddress = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
\r
302 /* Get the valid Page end Address */
\r
303 Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
\r
305 /* Check each active page address starting from end */
\r
306 while (Address > (PageStartAddress + 2))
\r
308 /* Get the current location content to be compared with virtual address */
\r
309 AddressValue = (*(__IO uint16_t*)Address);
\r
311 /* Compare the read address with the virtual address */
\r
312 if (AddressValue == VirtAddress)
\r
314 /* Get content of Address-2 which is variable value */
\r
315 *Data = (*(__IO uint16_t*)(Address - 2));
\r
317 /* In case variable value is read, reset ReadStatus flag */
\r
324 /* Next address location */
\r
325 Address = Address - 4;
\r
329 /* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */
\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
343 uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
\r
345 uint16_t Status = 0;
\r
347 /* Write the variable virtual address and value in the EEPROM */
\r
348 Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
\r
350 /* In case the EEPROM active page is full */
\r
351 if (Status == PAGE_FULL)
\r
353 /* Perform Page transfer */
\r
354 Status = EE_PageTransfer(VirtAddress, Data);
\r
357 /* Return last operation status */
\r
362 * @brief Erases PAGE0 and PAGE1 and writes VALID_PAGE header to PAGE0
\r
364 * @retval Status of the last operation (Flash write or erase) done during
\r
367 static FLASH_Status EE_Format(void)
\r
369 FLASH_Status FlashStatus = FLASH_COMPLETE;
\r
372 FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
\r
374 /* If erase operation was failed, a Flash error code is returned */
\r
375 if (FlashStatus != FLASH_COMPLETE)
\r
377 return FlashStatus;
\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
383 /* If program operation was failed, a Flash error code is returned */
\r
384 if (FlashStatus != FLASH_COMPLETE)
\r
386 return FlashStatus;
\r
390 FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
\r
392 /* Return Page1 erase operation status */
\r
393 return FlashStatus;
\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
405 static uint16_t EE_FindValidPage(uint8_t Operation)
\r
407 uint16_t PageStatus0 = 6, PageStatus1 = 6;
\r
409 /* Get Page0 actual status */
\r
410 PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
\r
412 /* Get Page1 actual status */
\r
413 PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
\r
415 /* Write or read operation */
\r
418 case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */
\r
419 if (PageStatus1 == VALID_PAGE)
\r
421 /* Page0 receiving data */
\r
422 if (PageStatus0 == RECEIVE_DATA)
\r
424 return PAGE0; /* Page0 valid */
\r
428 return PAGE1; /* Page1 valid */
\r
431 else if (PageStatus0 == VALID_PAGE)
\r
433 /* Page1 receiving data */
\r
434 if (PageStatus1 == RECEIVE_DATA)
\r
436 return PAGE1; /* Page1 valid */
\r
440 return PAGE0; /* Page0 valid */
\r
445 return NO_VALID_PAGE; /* No valid Page */
\r
448 case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */
\r
449 if (PageStatus0 == VALID_PAGE)
\r
451 return PAGE0; /* Page0 valid */
\r
453 else if (PageStatus1 == VALID_PAGE)
\r
455 return PAGE1; /* Page1 valid */
\r
459 return NO_VALID_PAGE ; /* No valid Page */
\r
463 return PAGE0; /* Page0 valid */
\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
477 static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
\r
479 FLASH_Status FlashStatus = FLASH_COMPLETE;
\r
480 uint16_t ValidPage = PAGE0;
\r
481 uint32_t Address = 0x08010000, PageEndAddress = 0x080107FF;
\r
483 /* Get valid Page for write operation */
\r
484 ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
\r
486 /* Check if there is no valid page */
\r
487 if (ValidPage == NO_VALID_PAGE)
\r
489 return NO_VALID_PAGE;
\r
492 /* Get the valid Page start Address */
\r
493 Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
\r
495 /* Get the valid Page end Address */
\r
496 PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
\r
498 /* Check each active page address starting from begining */
\r
499 while (Address < PageEndAddress)
\r
501 /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
\r
502 if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF)
\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
509 return FlashStatus;
\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
518 /* Next address location */
\r
519 Address = Address + 4;
\r
523 /* Return PAGE_FULL in case the valid page is full */
\r
528 * @brief Transfers last updated variables data from the full Page to
\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
538 static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
\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
545 /* Get active Page for read operation */
\r
546 ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
\r
548 if (ValidPage == PAGE1) /* Page1 valid */
\r
550 /* New page address where variable will be moved to */
\r
551 NewPageAddress = PAGE0_BASE_ADDRESS;
\r
553 /* Old page address where variable will be taken from */
\r
554 OldPageAddress = PAGE1_BASE_ADDRESS;
\r
556 else if (ValidPage == PAGE0) /* Page0 valid */
\r
558 /* New page address where variable will be moved to */
\r
559 NewPageAddress = PAGE1_BASE_ADDRESS;
\r
561 /* Old page address where variable will be taken from */
\r
562 OldPageAddress = PAGE0_BASE_ADDRESS;
\r
566 return NO_VALID_PAGE; /* No valid Page */
\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
574 return FlashStatus;
\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
582 return EepromStatus;
\r
585 /* Transfer process: transfer variables from old to the new active page */
\r
586 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
\r
588 if (VirtAddVarTab[VarIdx] != VirtAddress) /* Check each variable except the one passed as parameter */
\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
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
600 return EepromStatus;
\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
611 return FlashStatus;
\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
619 return FlashStatus;
\r
622 /* Return last operation flash status */
\r
623 return FlashStatus;
\r
630 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
\r