annotate eeprom.c @ 87:e30fe4bb8011

Add missing prototype
author Daniel O'Connor <darius@dons.net.au>
date Fri, 13 Mar 2015 11:36:26 +1030
parents 4c1db877452b
children
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 */
78
4c1db877452b Add small note.
Daniel O'Connor <darius@dons.net.au>
parents: 77
diff changeset
44 /* XXX: These appear to just be used by EE_Init and EE_PageTransfer to know what
4c1db877452b Add small note.
Daniel O'Connor <darius@dons.net.au>
parents: 77
diff changeset
45 * to copy over
4c1db877452b Add small note.
Daniel O'Connor <darius@dons.net.au>
parents: 77
diff changeset
46 */
76
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 const uint16_t VirtAddVarTab[NumbOfVar] = { 1000, 1001, 1002 };
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 /* Private function prototypes -----------------------------------------------*/
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 /* Private functions ---------------------------------------------------------*/
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 static uint16_t EE_FindValidPage(uint8_t Operation);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 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
53 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
54
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 * @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
57 * corruption after a power loss.
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 * @param None.
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 * @retval - Flash error code: on write Flash error
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 * - FLASH_COMPLETE: on success
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 uint16_t EE_Init(void)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 uint16_t PageStatus0 = 6, PageStatus1 = 6;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 uint16_t VarIdx = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 uint16_t EepromStatus = 0, ReadStatus = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 int16_t x = -1;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 uint16_t FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 /* Get Page0 status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 PageStatus0 = *(__IO uint16_t*)PAGE0_BASE_ADDRESS;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 /* Get Page1 status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 PageStatus1 = *(__IO uint16_t*)PAGE1_BASE_ADDRESS;
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 /* 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
76 switch (PageStatus0)
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 case ERASED:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 /* Erase Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 /* 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
84 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 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
90 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 /* Erase Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 /* 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
94 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 /* Mark Page1 as valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 /* 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
101 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 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
107 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 /* 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
109 FlashStatus = EE_Format();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 /* 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
111 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 break;
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 case RECEIVE_DATA:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */
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 /* Transfer data from Page1 to Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 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
125 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 x = VarIdx;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 if (VarIdx != x)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 /* Read the last variables' updates */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 /* 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
133 if (ReadStatus != 0x1)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 /* Transfer the variable to the Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 /* 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
138 if (EepromStatus != FLASH_COMPLETE)
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 return EepromStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 /* Mark Page0 as valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 /* 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
148 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 /* Erase Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 /* 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
155 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 /* Erase Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 /* 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
165 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 /* Mark Page0 as valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 /* 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
172 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 else /* Invalid state -> format eeprom */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 /* 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
180 FlashStatus = EE_Format();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 /* 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
182 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 break;
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 case VALID_PAGE:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 /* 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
193 FlashStatus = EE_Format();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 /* 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
195 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 /* Erase Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 /* 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
205 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210 else /* Page0 valid, Page1 receive */
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 /* Transfer data from Page0 to Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 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
216 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 x = VarIdx;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 if (VarIdx != x)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 /* Read the last variables' updates */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 /* 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
224 if (ReadStatus != 0x1)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 /* Transfer the variable to the Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 /* 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
229 if (EepromStatus != FLASH_COMPLETE)
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 return EepromStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 /* Mark Page1 as valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 /* 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
239 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 /* Erase Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244 FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245 /* 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
246 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 break;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 default: /* Any other state -> format eeprom */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254 /* 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
255 FlashStatus = EE_Format();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 /* 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
257 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
259 return FlashStatus;
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 break;
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 return FLASH_COMPLETE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268 * @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
269 * the passed virtual address
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 * @param VirtAddress: Variable virtual address
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 * @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
272 * @retval Success or error status:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273 * - 0: if variable was found
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 * - 1: if the variable was not found
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275 * - 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
276 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 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
278 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279 uint16_t ValidPage = PAGE0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280 uint16_t AddressValue = 0x5555, ReadStatus = 1;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
281 uint32_t Address = 0x08010000, PageStartAddress = 0x08010000;
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 /* Get active Page for read operation */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284 ValidPage = EE_FindValidPage(READ_FROM_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 /* Check if there is no valid page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287 if (ValidPage == NO_VALID_PAGE)
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 return NO_VALID_PAGE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
290 }
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 start Address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
293 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
294
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
295 /* Get the valid Page end Address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
296 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
297
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
298 /* Check each active page address starting from end */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
299 while (Address > (PageStartAddress + 2))
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 /* 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
302 AddressValue = (*(__IO uint16_t*)Address);
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 /* Compare the read address with the virtual address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
305 if (AddressValue == VirtAddress)
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 /* 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
308 *Data = (*(__IO uint16_t*)(Address - 2));
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 /* 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
311 ReadStatus = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
312
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
313 break;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
314 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
315 else
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 /* Next address location */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
318 Address = Address - 4;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
319 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
320 }
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 /* 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
323 return ReadStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
324 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
325
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
326 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
327 * @brief Writes/upadtes variable data in EEPROM.
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
328 * @param VirtAddress: Variable virtual address
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
329 * @param Data: 16 bit data to be written
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
330 * @retval Success or error status:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
331 * - FLASH_COMPLETE: on success
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
332 * - PAGE_FULL: if valid page is full
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
333 * - 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
334 * - Flash error code: on write Flash error
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
335 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
336 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
337 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
338 uint16_t Status = 0;
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 /* 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
341 Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
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 /* In case the EEPROM active page is full */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
344 if (Status == PAGE_FULL)
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 /* Perform Page transfer */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
347 Status = EE_PageTransfer(VirtAddress, Data);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
348 }
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 /* Return last operation status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
351 return Status;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
352 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
353
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
354 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
355 * @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
356 * @param None
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
357 * @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
358 * EEPROM formating
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
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
361 FLASH_Status EE_Format(void)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
362 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
363 FLASH_Status FlashStatus = FLASH_COMPLETE;
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 /* Erase Page0 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
366 FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
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 /* 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
369 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
372 }
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 /* 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
375 FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);
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 /* 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
378 if (FlashStatus != FLASH_COMPLETE)
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 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
381 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
382
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
383 /* Erase Page1 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
384 FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
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 /* Return Page1 erase operation status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
387 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
388 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
389
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
390 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
391 * @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
392 * @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
393 * 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
394 * @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
395 * @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
396 * @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
397 * of no valid page was found
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
398 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
399 static uint16_t EE_FindValidPage(uint8_t Operation)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
400 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
401 uint16_t PageStatus0 = 6, PageStatus1 = 6;
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 Page0 actual status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
404 PageStatus0 = (*(__IO uint16_t*)PAGE0_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 /* Get Page1 actual status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
407 PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
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 /* Write or read operation */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
410 switch (Operation)
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 case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
413 if (PageStatus1 == VALID_PAGE)
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 /* Page0 receiving data */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
416 if (PageStatus0 == RECEIVE_DATA)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
417 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
418 return PAGE0; /* Page0 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
419 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
420 else
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
421 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
422 return PAGE1; /* Page1 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
423 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
424 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
425 else if (PageStatus0 == VALID_PAGE)
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 /* Page1 receiving data */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
428 if (PageStatus1 == RECEIVE_DATA)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
429 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
430 return PAGE1; /* Page1 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
431 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
432 else
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 return PAGE0; /* Page0 valid */
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
437 else
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 return NO_VALID_PAGE; /* No valid Page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
440 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
441
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
442 case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
443 if (PageStatus0 == VALID_PAGE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
444 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
445 return PAGE0; /* Page0 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
446 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
447 else if (PageStatus1 == VALID_PAGE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
448 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
449 return PAGE1; /* Page1 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
450 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
451 else
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 return NO_VALID_PAGE ; /* No valid Page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
454 }
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 default:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
457 return PAGE0; /* Page0 valid */
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
460
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
461 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
462 * @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
463 * @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
464 * @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
465 * @retval Success or error status:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
466 * - FLASH_COMPLETE: on success
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
467 * - PAGE_FULL: if valid page is full
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
468 * - 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
469 * - Flash error code: on write Flash error
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
470 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
471 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
472 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
473 FLASH_Status FlashStatus = FLASH_COMPLETE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
474 uint16_t ValidPage = PAGE0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
475 uint32_t Address = 0x08010000, PageEndAddress = 0x080107FF;
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 /* Get valid Page for write operation */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
478 ValidPage = EE_FindValidPage(WRITE_IN_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 /* Check if there is no valid page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
481 if (ValidPage == NO_VALID_PAGE)
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 return NO_VALID_PAGE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
484 }
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 start Address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
487 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
488
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
489 /* Get the valid Page end Address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
490 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
491
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
492 /* Check each active page address starting from begining */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
493 while (Address < PageEndAddress)
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 /* 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
496 if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
497 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
498 /* Set variable data */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
499 FlashStatus = FLASH_ProgramHalfWord(Address, Data);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
500 /* 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
501 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
502 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
503 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
504 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
505 /* Set variable virtual address */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
506 FlashStatus = FLASH_ProgramHalfWord(Address + 2, VirtAddress);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
507 /* Return program operation status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
508 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
509 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
510 else
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 /* Next address location */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
513 Address = Address + 4;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
514 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
515 }
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 /* 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
518 return PAGE_FULL;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
519 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
520
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
521 /**
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
522 * @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
523 * an empty one.
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
524 * @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
525 * @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
526 * @retval Success or error status:
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
527 * - FLASH_COMPLETE: on success
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
528 * - PAGE_FULL: if valid page is full
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
529 * - 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
530 * - Flash error code: on write Flash error
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
531 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
532 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
533 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
534 FLASH_Status FlashStatus = FLASH_COMPLETE;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
535 uint32_t NewPageAddress = 0x080103FF, OldPageAddress = 0x08010000;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
536 uint16_t ValidPage = PAGE0, VarIdx = 0;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
537 uint16_t EepromStatus = 0, ReadStatus = 0;
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 /* Get active Page for read operation */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
540 ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
541
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
542 if (ValidPage == PAGE1) /* Page1 valid */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
543 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
544 /* New page address where variable will be moved to */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
545 NewPageAddress = PAGE0_BASE_ADDRESS;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
546
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
547 /* Old page address where variable will be taken from */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
548 OldPageAddress = PAGE1_BASE_ADDRESS;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
549 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
550 else if (ValidPage == PAGE0) /* Page0 valid */
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 /* 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
553 NewPageAddress = PAGE1_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 /* 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
556 OldPageAddress = PAGE0_BASE_ADDRESS;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
557 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
558 else
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
559 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
560 return NO_VALID_PAGE; /* No valid Page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
561 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
562
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
563 /* 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
564 FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, RECEIVE_DATA);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
565 /* If program operation was failed, a Flash error code is returned */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
566 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
567 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
568 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
569 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
570
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
571 /* 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
572 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
573 /* 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
574 if (EepromStatus != FLASH_COMPLETE)
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 return EepromStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
577 }
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 /* 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
580 for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
581 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
582 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
583 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
584 /* Read the other last variable updates */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
585 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
586 /* 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
587 if (ReadStatus != 0x1)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
588 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
589 /* Transfer the variable to the new active page */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
590 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
591 /* 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
592 if (EepromStatus != FLASH_COMPLETE)
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 return EepromStatus;
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 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
598 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
599
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
600 /* 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
601 FlashStatus = FLASH_ErasePage(OldPageAddress);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
602 /* 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
603 if (FlashStatus != FLASH_COMPLETE)
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
604 {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
605 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
606 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
607
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
608 /* Set new Page status to VALID_PAGE status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
609 FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, VALID_PAGE);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
610 /* 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
611 if (FlashStatus != FLASH_COMPLETE)
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 FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
614 }
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 /* Return last operation flash status */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
617 return FlashStatus;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
618 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
619
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
620 void
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
621 FLASH_UnlockClear(void) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
622 FLASH_Unlock();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
623 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
624 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
625
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
626 void
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
627 EE_Cmd(int argc, char **argv) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
628 uint16_t adr, val, err;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
629
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
630 if (argc == 0) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
631 fputs("No command specified\n", stdout);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
632 return;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
633 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
634
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
635 if (!strcmp(argv[0], "in")) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
636 FLASH_UnlockClear();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
637 printf("Init returned %d\n", EE_Init());
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
638 FLASH_Lock();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
639 } else if (!strcmp(argv[0], "fm")) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
640 FLASH_UnlockClear();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
641 printf("Format returned %d\n", EE_Format());
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
642 FLASH_Lock();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
643 } else if (!strcmp(argv[0], "rd")) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
644 if (argc != 2) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
645 fputs("Incorrect number of arguments\n", stdout);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
646 return;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
647 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
648 adr = atoi(argv[1]);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
649 err = EE_ReadVariable(adr, &val);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
650 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
651 } else if (!strcmp(argv[0], "wr")) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
652 if (argc != 3) {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
653 fputs("Incorrect number of arguments\n", stdout);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
654 return;
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
655 }
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
656 adr = atoi(argv[1]);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
657 val = atoi(argv[2]);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
658 FLASH_UnlockClear();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
659 err = EE_WriteVariable(adr, val);
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
660 FLASH_Lock();
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
661 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
662 } else {
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
663 fputs("Unknown command\n", stdout);
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 * @}
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
669 */
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
670
f1c9a51e368a Add EEPROM emulation from AN2594.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
671 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/