annotate eeprom.c @ 77:7dd28b96a716

Add note about where it came from.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 22 Apr 2013 23:39:28 +0930
parents f1c9a51e368a
children 4c1db877452b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
77
7dd28b96a716 Add note about where it came from.
Daniel O'Connor <darius@dons.net.au>
parents: 76
diff changeset
1 /* From
7dd28b96a716 Add note about where it came from.
Daniel O'Connor <darius@dons.net.au>
parents: 76
diff changeset
2 * http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/PF257846
7dd28b96a716 Add note about where it came from.
Daniel O'Connor <darius@dons.net.au>
parents: 76
diff changeset
3 * stsw-stm32010.zip
7dd28b96a716 Add note about where it came from.
Daniel O'Connor <darius@dons.net.au>
parents: 76
diff changeset
4 */
7dd28b96a716 Add note about where it came from.
Daniel O'Connor <darius@dons.net.au>
parents: 76
diff changeset
5
76
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 ******************************************************************************
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 * @file EEPROM_Emulation/src/eeprom.c
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 * @author MCD Application Team
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 * @version V3.1.0
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 * @date 07/27/2009
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 * @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
13 ******************************************************************************
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 * @copy
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 *
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 * 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
17 * 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
18 * 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
19 * 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
20 * 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
21 * 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
22 *
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 /** @addtogroup EEPROM_Emulation
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 * @{
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 /* Includes ------------------------------------------------------------------*/
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 #include <stdio.h>
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 #include <stdlib.h>
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 #include <string.h>
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 #include "eeprom.h"
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 /* Private typedef -----------------------------------------------------------*/
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 /* Private define ------------------------------------------------------------*/
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 /* Private macro -------------------------------------------------------------*/
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 /* Private variables ---------------------------------------------------------*/
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 /* 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
41 uint16_t DataVar = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 /* 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
44 const uint16_t VirtAddVarTab[NumbOfVar] = { 1000, 1001, 1002 };
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 /* Private function prototypes -----------------------------------------------*/
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 /* Private functions ---------------------------------------------------------*/
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 static uint16_t EE_FindValidPage(uint8_t Operation);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 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
50 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
51
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 * @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
54 * corruption after a power loss.
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 * @param None.
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 * @retval - Flash error code: on write Flash error
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 * - FLASH_COMPLETE: on success
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 uint16_t EE_Init(void)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 uint16_t PageStatus0 = 6, PageStatus1 = 6;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 uint16_t VarIdx = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 uint16_t EepromStatus = 0, ReadStatus = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 int16_t x = -1;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 uint16_t FlashStatus;
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 /* Get Page0 status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 PageStatus0 = *(__IO uint16_t*)PAGE0_BASE_ADDRESS;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 /* Get Page1 status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 PageStatus1 = *(__IO uint16_t*)PAGE1_BASE_ADDRESS;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 /* 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
73 switch (PageStatus0)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 case ERASED:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
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 /* Erase Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 /* 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
81 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 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
87 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 /* Erase Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 /* 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
91 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 return FlashStatus;
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 /* Mark Page1 as valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 /* 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
98 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 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
104 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 /* 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
106 FlashStatus = EE_Format();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 /* 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
108 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 }
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 break;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 case RECEIVE_DATA:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */
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 /* Transfer data from Page1 to Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 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
122 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 x = VarIdx;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 if (VarIdx != x)
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 /* Read the last variables' updates */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 /* 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
130 if (ReadStatus != 0x1)
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 /* Transfer the variable to the Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 /* 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
135 if (EepromStatus != FLASH_COMPLETE)
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 return EepromStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 }
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 /* Mark Page0 as valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 /* 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
145 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 return FlashStatus;
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 /* Erase Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 /* 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
152 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
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 /* Erase Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 /* 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
162 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 return FlashStatus;
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 /* Mark Page0 as valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 /* 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
169 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 else /* Invalid state -> format eeprom */
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 /* 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
177 FlashStatus = EE_Format();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 /* 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
179 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 }
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 break;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 case VALID_PAGE:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
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 /* 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
190 FlashStatus = EE_Format();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 /* 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
192 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
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 /* Erase Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 /* 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
202 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 }
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 else /* Page0 valid, Page1 receive */
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 /* Transfer data from Page0 to Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 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
213 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 x = VarIdx;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 if (VarIdx != x)
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 /* Read the last variables' updates */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 /* 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
221 if (ReadStatus != 0x1)
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 /* Transfer the variable to the Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 /* 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
226 if (EepromStatus != FLASH_COMPLETE)
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 return EepromStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 }
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 /* Mark Page1 as valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 /* 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
236 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 return FlashStatus;
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 /* Erase Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 /* 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
243 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
247 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 break;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250 default: /* Any other state -> format eeprom */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 /* 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
252 FlashStatus = EE_Format();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 /* 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
254 if (FlashStatus != FLASH_COMPLETE)
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 FlashStatus;
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 break;
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
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
261 return FLASH_COMPLETE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265 * @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
266 * the passed virtual address
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267 * @param VirtAddress: Variable virtual address
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268 * @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
269 * @retval Success or error status:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 * - 0: if variable was found
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 * - 1: if the variable was not found
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272 * - 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
273 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 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
275 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276 uint16_t ValidPage = PAGE0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 uint16_t AddressValue = 0x5555, ReadStatus = 1;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278 uint32_t Address = 0x08010000, PageStartAddress = 0x08010000;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280 /* Get active Page for read operation */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
281 ValidPage = EE_FindValidPage(READ_FROM_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 /* Check if there is no valid page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284 if (ValidPage == NO_VALID_PAGE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
285 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
286 return NO_VALID_PAGE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
288
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289 /* Get the valid Page start Address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
290 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
291
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
292 /* Get the valid Page end Address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
293 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
294
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
295 /* Check each active page address starting from end */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
296 while (Address > (PageStartAddress + 2))
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
297 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
298 /* 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
299 AddressValue = (*(__IO uint16_t*)Address);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
300
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
301 /* Compare the read address with the virtual address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
302 if (AddressValue == VirtAddress)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
303 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
304 /* 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
305 *Data = (*(__IO uint16_t*)(Address - 2));
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 /* 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
308 ReadStatus = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
309
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
310 break;
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 else
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 /* Next address location */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
315 Address = Address - 4;
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 /* 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
320 return ReadStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
321 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
322
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
323 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
324 * @brief Writes/upadtes variable data in EEPROM.
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
325 * @param VirtAddress: Variable virtual address
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
326 * @param Data: 16 bit data to be written
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
327 * @retval Success or error status:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
328 * - FLASH_COMPLETE: on success
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
329 * - PAGE_FULL: if valid page is full
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
330 * - 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
331 * - Flash error code: on write Flash error
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
332 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
333 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
334 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
335 uint16_t Status = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
336
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
337 /* 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
338 Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
339
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
340 /* In case the EEPROM active page is full */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
341 if (Status == PAGE_FULL)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
342 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
343 /* Perform Page transfer */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
344 Status = EE_PageTransfer(VirtAddress, Data);
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 /* Return last operation status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
348 return Status;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
349 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
350
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 * @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
353 * @param None
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
354 * @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
355 * EEPROM formating
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
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
358 FLASH_Status EE_Format(void)
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 FLASH_Status FlashStatus = FLASH_COMPLETE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
361
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
362 /* Erase Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
363 FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
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 /* 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
366 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
367 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
368 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
369 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
370
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
371 /* 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
372 FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);
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 /* 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
375 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
376 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
377 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
378 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
379
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
380 /* Erase Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
381 FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
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 /* Return Page1 erase operation status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
384 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
385 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
386
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
387 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
388 * @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
389 * @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
390 * 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
391 * @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
392 * @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
393 * @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
394 * of no valid page was found
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
395 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
396 static uint16_t EE_FindValidPage(uint8_t Operation)
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 uint16_t PageStatus0 = 6, PageStatus1 = 6;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
399
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
400 /* Get Page0 actual status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
401 PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
402
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
403 /* Get Page1 actual status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
404 PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
405
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
406 /* Write or read operation */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
407 switch (Operation)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
408 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
409 case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
410 if (PageStatus1 == VALID_PAGE)
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 /* Page0 receiving data */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
413 if (PageStatus0 == RECEIVE_DATA)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
414 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
415 return PAGE0; /* Page0 valid */
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
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 return PAGE1; /* Page1 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
420 }
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 else if (PageStatus0 == VALID_PAGE)
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 /* Page1 receiving data */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
425 if (PageStatus1 == RECEIVE_DATA)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
426 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
427 return PAGE1; /* Page1 valid */
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 PAGE0; /* Page0 valid */
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 else
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
435 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
436 return NO_VALID_PAGE; /* No valid Page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
437 }
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 case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
440 if (PageStatus0 == VALID_PAGE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
441 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
442 return PAGE0; /* Page0 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
443 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
444 else if (PageStatus1 == VALID_PAGE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
445 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
446 return PAGE1; /* Page1 valid */
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 else
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
449 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
450 return NO_VALID_PAGE ; /* No valid Page */
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 default:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
454 return PAGE0; /* Page0 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
455 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
456 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
457
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
458 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
459 * @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
460 * @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
461 * @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
462 * @retval Success or error status:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
463 * - FLASH_COMPLETE: on success
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
464 * - PAGE_FULL: if valid page is full
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
465 * - 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
466 * - Flash error code: on write Flash error
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
467 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
468 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
469 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
470 FLASH_Status FlashStatus = FLASH_COMPLETE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
471 uint16_t ValidPage = PAGE0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
472 uint32_t Address = 0x08010000, PageEndAddress = 0x080107FF;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
473
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
474 /* Get valid Page for write operation */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
475 ValidPage = EE_FindValidPage(WRITE_IN_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 /* Check if there is no valid page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
478 if (ValidPage == NO_VALID_PAGE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
479 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
480 return NO_VALID_PAGE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
481 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
482
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
483 /* Get the valid Page start Address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
484 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
485
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
486 /* Get the valid Page end Address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
487 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
488
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
489 /* Check each active page address starting from begining */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
490 while (Address < PageEndAddress)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
491 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
492 /* 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
493 if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF)
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 /* Set variable data */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
496 FlashStatus = FLASH_ProgramHalfWord(Address, Data);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
497 /* 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
498 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
499 {
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 /* Set variable virtual address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
503 FlashStatus = FLASH_ProgramHalfWord(Address + 2, VirtAddress);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
504 /* Return program operation status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
505 return FlashStatus;
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 else
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 /* Next address location */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
510 Address = Address + 4;
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 /* 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
515 return PAGE_FULL;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
516 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
517
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
518 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
519 * @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
520 * an empty one.
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
521 * @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
522 * @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
523 * @retval Success or error status:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
524 * - FLASH_COMPLETE: on success
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
525 * - PAGE_FULL: if valid page is full
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
526 * - 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
527 * - Flash error code: on write Flash error
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
528 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
529 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
530 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
531 FLASH_Status FlashStatus = FLASH_COMPLETE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
532 uint32_t NewPageAddress = 0x080103FF, OldPageAddress = 0x08010000;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
533 uint16_t ValidPage = PAGE0, VarIdx = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
534 uint16_t EepromStatus = 0, ReadStatus = 0;
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 /* Get active Page for read operation */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
537 ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
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 if (ValidPage == PAGE1) /* Page1 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
540 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
541 /* 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
542 NewPageAddress = PAGE0_BASE_ADDRESS;
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 /* 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
545 OldPageAddress = 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 else if (ValidPage == PAGE0) /* Page0 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
548 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
549 /* 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
550 NewPageAddress = PAGE1_BASE_ADDRESS;
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 /* 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
553 OldPageAddress = PAGE0_BASE_ADDRESS;
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 else
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
556 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
557 return NO_VALID_PAGE; /* No valid Page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
558 }
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 /* 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
561 FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, RECEIVE_DATA);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
562 /* 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
563 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
564 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
565 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
566 }
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 /* 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
569 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
570 /* 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
571 if (EepromStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
572 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
573 return EepromStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
574 }
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 /* 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
577 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
578 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
579 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
580 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
581 /* Read the other last variable updates */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
582 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
583 /* 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
584 if (ReadStatus != 0x1)
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 /* Transfer the variable to the new active page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
587 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
588 /* 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
589 if (EepromStatus != FLASH_COMPLETE)
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 return EepromStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
592 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
593 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
594 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
595 }
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 /* 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
598 FlashStatus = FLASH_ErasePage(OldPageAddress);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
599 /* 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
600 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
601 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
602 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
603 }
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 /* Set new Page status to VALID_PAGE status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
606 FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
607 /* 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
608 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
609 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
610 return FlashStatus;
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
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
613 /* Return last operation flash status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
614 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
615 }
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 void
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
618 FLASH_UnlockClear(void) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
619 FLASH_Unlock();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
620 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
621 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
622
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
623 void
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
624 EE_Cmd(int argc, char **argv) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
625 uint16_t adr, val, err;
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 (argc == 0) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
628 fputs("No command specified\n", stdout);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
629 return;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
630 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
631
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
632 if (!strcmp(argv[0], "in")) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
633 FLASH_UnlockClear();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
634 printf("Init returned %d\n", EE_Init());
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
635 FLASH_Lock();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
636 } else if (!strcmp(argv[0], "fm")) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
637 FLASH_UnlockClear();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
638 printf("Format returned %d\n", EE_Format());
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
639 FLASH_Lock();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
640 } else if (!strcmp(argv[0], "rd")) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
641 if (argc != 2) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
642 fputs("Incorrect number of arguments\n", stdout);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
643 return;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
644 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
645 adr = atoi(argv[1]);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
646 err = EE_ReadVariable(adr, &val);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
647 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
648 } else if (!strcmp(argv[0], "wr")) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
649 if (argc != 3) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
650 fputs("Incorrect number of arguments\n", stdout);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
651 return;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
652 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
653 adr = atoi(argv[1]);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
654 val = atoi(argv[2]);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
655 FLASH_UnlockClear();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
656 err = EE_WriteVariable(adr, val);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
657 FLASH_Lock();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
658 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
659 } else {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
660 fputs("Unknown command\n", stdout);
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
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
664 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
665 * @}
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
666 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
667
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
668 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/