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