annotate eeprom.c @ 76:f1c9a51e368a

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