Rev 16 | Rev 21 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 16 | Rev 18 | ||
|---|---|---|---|
| Line 30... | Line 30... | ||
| 30 | /* Private variables ---------------------------------------------------------*/ |
30 | /* Private variables ---------------------------------------------------------*/ |
| 31 | 31 | ||
| 32 | 32 | ||
| 33 | FlashPage_t __attribute__((section(".EEPROM_Data"))) FlashPage[2]; |
33 | FlashPage_t __attribute__((section(".EEPROM_Data"))) FlashPage[2]; |
| 34 | 34 | ||
| - | 35 | static FLASH_EraseInitTypeDef EraseInitStruct; |
|
| - | 36 | static uint32_t PAGEError; |
|
| - | 37 | ||
| 35 | 38 | ||
| 36 | /* Global variable used to store variable value in read sequence */ |
39 | /* Global variable used to store variable value in read sequence */ |
| 37 | uint16_t DataVar = 0; |
40 | uint16_t DataVar = 0; |
| 38 | 41 | ||
| 39 | /* Virtual address defined by the user: 0xFFFF value is prohibited */ |
42 | /* Virtual address defined by the user: 0xFFFF value is prohibited */ |
| Line 71... | Line 74... | ||
| 71 | { |
74 | { |
| 72 | case ERASED: |
75 | case ERASED: |
| 73 | if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ |
76 | if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ |
| 74 | { |
77 | { |
| 75 | /* Erase Page0 */ |
78 | /* Erase Page0 */ |
| 76 | FLASH_PageErase(PAGE0_BASE_ADDRESS); |
79 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
| 77 | /* If erase operation was failed, a Flash error code is returned */ |
80 | EraseInitStruct.PageAddress = PAGE0_BASE_ADDRESS; |
| 78 | FlashStatus = HAL_FLASH_GetError(); |
81 | EraseInitStruct.NbPages = 1; |
| 79 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
82 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) |
| 80 | { |
83 | { |
| - | 84 | /* If erase operation was failed, a Flash error code is returned */ |
|
| - | 85 | FlashStatus = HAL_FLASH_GetError(); |
|
| 81 | return FlashStatus; |
86 | return FlashStatus; |
| 82 | } |
87 | } |
| 83 | } |
88 | } |
| 84 | else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ |
89 | else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ |
| 85 | { |
90 | { |
| 86 | /* Erase Page0 */ |
91 | /* Erase Page0 */ |
| 87 | FLASH_PageErase(PAGE0_BASE_ADDRESS); |
92 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
| 88 | /* If erase operation was failed, a Flash error code is returned */ |
93 | EraseInitStruct.PageAddress = PAGE0_BASE_ADDRESS; |
| 89 | FlashStatus = HAL_FLASH_GetError(); |
94 | EraseInitStruct.NbPages = 1; |
| 90 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
95 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) |
| 91 | { |
96 | { |
| - | 97 | /* If erase operation was failed, a Flash error code is returned */ |
|
| - | 98 | FlashStatus = HAL_FLASH_GetError(); |
|
| 92 | return FlashStatus; |
99 | return FlashStatus; |
| 93 | } |
100 | } |
| 94 | /* Mark Page1 as valid */ |
101 | /* Mark Page1 as valid */ |
| 95 | 102 | ||
| 96 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE1_BASE_ADDRESS, VALID_PAGE); |
103 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE1_BASE_ADDRESS, VALID_PAGE); |
| 97 | /* If program operation was failed, a Flash error code is returned */ |
104 | /* If program operation was failed, a Flash error code is returned */ |
| 98 | FlashStatus = HAL_FLASH_GetError(); |
105 | FlashStatus = HAL_FLASH_GetError(); |
| 99 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
106 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
| 100 | { |
107 | { |
| 101 | return FlashStatus; |
108 | return FlashStatus; |
| 102 | } |
109 | } |
| 103 | } |
110 | } |
| 104 | else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */ |
111 | else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */ |
| 105 | { |
112 | { |
| 106 | /* Erase both Page0 and Page1 and set Page0 as valid page */ |
113 | /* Erase both Page0 and Page1 and set Page0 as valid page */ |
| 107 | FlashStatus = EE_Format(); |
114 | FlashStatus = EE_Format(); |
| 108 | /* If erase/program operation was failed, a Flash error code is returned */ |
115 | /* If erase/program operation was failed, a Flash error code is returned */ |
| 109 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
116 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
| 110 | { |
117 | { |
| 111 | return FlashStatus; |
118 | return FlashStatus; |
| 112 | } |
119 | } |
| 113 | } |
120 | } |
| 114 | break; |
121 | break; |
| 115 | 122 | ||
| 116 | case RECEIVE_DATA: |
123 | case RECEIVE_DATA: |
| Line 362... | Line 369... | ||
| 362 | * @brief Erases PAGE0 and PAGE1 and writes VALID_PAGE header to PAGE0 |
369 | * @brief Erases PAGE0 and PAGE1 and writes VALID_PAGE header to PAGE0 |
| 363 | * @param None |
370 | * @param None |
| 364 | * @retval Status of the last operation (Flash write or erase) done during |
371 | * @retval Status of the last operation (Flash write or erase) done during |
| 365 | * EEPROM formating |
372 | * EEPROM formating |
| 366 | */ |
373 | */ |
| - | 374 | ||
| 367 | static FLASH_Status EE_Format(void) |
375 | static FLASH_Status EE_Format(void) |
| 368 | { |
376 | { |
| 369 | FLASH_Status FlashStatus = HAL_FLASH_ERROR_NONE; |
377 | FLASH_Status FlashStatus = HAL_FLASH_ERROR_NONE; |
| 370 | 378 | ||
| 371 | /* Erase Page0 */ |
379 | /* Erase Page0 and Page1 */ |
| - | 380 | /* Fill EraseInit structure*/ |
|
| - | 381 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
|
| 372 | FLASH_PageErase(PAGE0_BASE_ADDRESS); |
382 | EraseInitStruct.PageAddress = PAGE0_BASE_ADDRESS; |
| - | 383 | EraseInitStruct.NbPages = 2; |
|
| 373 | 384 | ||
| - | 385 | if(HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) |
|
| 374 | /* If erase operation was failed, a Flash error code is returned */ |
386 | /* If erase operation was failed, a Flash error code is returned */ |
| 375 | FlashStatus = HAL_FLASH_GetError(); |
- | |
| 376 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
- | |
| 377 | { |
387 | { |
| - | 388 | FlashStatus = HAL_FLASH_GetError(); |
|
| 378 | return FlashStatus; |
389 | return FlashStatus; |
| 379 | } |
390 | } |
| 380 | 391 | ||
| - | 392 | ||
| 381 | /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */ |
393 | /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */ |
| 382 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE0_BASE_ADDRESS, VALID_PAGE); |
394 | uint32_t rc = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE0_BASE_ADDRESS, VALID_PAGE); |
| 383 | /* If program operation was failed, a Flash error code is returned */ |
395 | /* If program operation was failed, a Flash error code is returned */ |
| 384 | FlashStatus = HAL_FLASH_GetError(); |
- | |
| - | 396 | ||
| 385 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
397 | if (rc!= HAL_OK) |
| 386 | { |
398 | { |
| - | 399 | FlashStatus = HAL_FLASH_GetError(); |
|
| 387 | return FlashStatus; |
400 | return FlashStatus; |
| 388 | } |
401 | } |
| 389 | 402 | ||
| 390 | - | ||
| 391 | /* Erase Page1 */ |
- | |
| 392 | FLASH_PageErase(PAGE1_BASE_ADDRESS); |
- | |
| 393 | FlashStatus = HAL_FLASH_GetError(); |
- | |
| 394 | - | ||
| 395 | /* Return Page1 erase operation status */ |
403 | /* Return Page1 erase operation status */ |
| 396 | return FlashStatus; |
404 | return FlashStatus; |
| 397 | } |
405 | } |
| 398 | 406 | ||
| 399 | /** |
407 | /** |
| Line 511... | Line 519... | ||
| 511 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
519 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
| 512 | { |
520 | { |
| 513 | return FlashStatus; |
521 | return FlashStatus; |
| 514 | } |
522 | } |
| 515 | /* Set variable virtual address */ |
523 | /* Set variable virtual address */ |
| 516 | - | ||
| - | 524 | HAL_FLASH_Unlock(); |
|
| 517 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Address + 2 ,VirtAddress); |
525 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Address + 2 ,VirtAddress); |
| 518 | /* If program operation was failed, a Flash error code is returned */ |
526 | /* If program operation was failed, a Flash error code is returned */ |
| 519 | FlashStatus = HAL_FLASH_GetError(); |
527 | FlashStatus = HAL_FLASH_GetError(); |
| 520 | /* Return program operation status */ |
528 | /* Return program operation status */ |
| 521 | return FlashStatus; |
529 | return FlashStatus; |