STM32 EEPROM emulation AN2594

Committer:
olympux
Date:
Sun Sep 28 17:34:08 2014 +0000
Revision:
4:535d7082fbb2
Parent:
eeprom.c@0:0b8e4689a075
Rename .c into .cpp

Who changed what in which revision?

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