Rev 21 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 21 | Rev 22 | ||
---|---|---|---|
Line 47... | Line 47... | ||
47 | static FLASH_Status EE_Format(void); |
47 | static FLASH_Status EE_Format(void); |
48 | static uint16_t EE_FindValidPage(uint8_t Operation); |
48 | static uint16_t EE_FindValidPage(uint8_t Operation); |
49 | static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data); |
49 | static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data); |
50 | static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data); |
50 | static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data); |
51 | 51 | ||
- | 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 | } |
|
52 | /** |
68 | /** |
53 | * @brief Restore the pages to a known good state in case of page's status |
69 | * @brief Restore the pages to a known good state in case of page's status |
54 | * corruption after a power loss. |
70 | * corruption after a power loss. |
55 | * @param None. |
71 | * @param None. |
56 | * @retval - Flash error code: on write Flash error |
72 | * @retval - Flash error code: on write Flash error |
Line 74... | Line 90... | ||
74 | { |
90 | { |
75 | case ERASED: |
91 | case ERASED: |
76 | if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ |
92 | if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ |
77 | { |
93 | { |
78 | /* Erase Page0 */ |
94 | /* Erase Page0 */ |
79 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
- | |
80 | EraseInitStruct.PageAddress = PAGE0_BASE_ADDRESS; |
95 | FlashStatus = EE_ErasePages(PAGE0_BASE_ADDRESS,1); |
81 | EraseInitStruct.NbPages = 1; |
96 | if(FlashStatus!=HAL_FLASH_ERROR_NONE) |
82 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) |
- | |
83 | { |
97 | { |
84 | /* If erase operation was failed, a Flash error code is returned */ |
- | |
85 | FlashStatus = HAL_FLASH_GetError(); |
- | |
86 | return FlashStatus; |
98 | return FlashStatus; |
87 | } |
99 | } |
88 | } |
100 | } |
89 | else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ |
101 | else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ |
90 | { |
102 | { |
91 | /* Erase Page0 */ |
103 | /* Erase Page0 */ |
92 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
- | |
93 | EraseInitStruct.PageAddress = PAGE0_BASE_ADDRESS; |
104 | FlashStatus = EE_ErasePages(PAGE0_BASE_ADDRESS,1); |
94 | EraseInitStruct.NbPages = 1; |
105 | if(FlashStatus!=HAL_FLASH_ERROR_NONE) |
95 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) |
- | |
96 | { |
106 | { |
97 | /* If erase operation was failed, a Flash error code is returned */ |
- | |
98 | FlashStatus = HAL_FLASH_GetError(); |
- | |
99 | return FlashStatus; |
107 | return FlashStatus; |
100 | } |
108 | } |
101 | /* Mark Page1 as valid */ |
109 | /* Mark Page1 as valid */ |
102 | 110 | ||
103 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE1_BASE_ADDRESS, VALID_PAGE); |
111 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE1_BASE_ADDRESS, VALID_PAGE); |
104 | /* If program operation was failed, a Flash error code is returned */ |
112 | /* If program operation was failed, a Flash error code is returned */ |
105 | FlashStatus = HAL_FLASH_GetError(); |
113 | FlashStatus = HAL_FLASH_GetError(); |
Line 154... | Line 162... | ||
154 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
162 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
155 | { |
163 | { |
156 | return FlashStatus; |
164 | return FlashStatus; |
157 | } |
165 | } |
158 | /* Erase Page1 */ |
166 | /* Erase Page1 */ |
159 | FLASH_PageErase(PAGE1_BASE_ADDRESS); |
167 | FlashStatus = EE_ErasePages(PAGE1_BASE_ADDRESS,1); |
160 | /* If erase operation was failed, a Flash error code is returned */ |
- | |
161 | FlashStatus = HAL_FLASH_GetError(); |
- | |
162 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
168 | if(FlashStatus!=HAL_FLASH_ERROR_NONE) |
163 | { |
169 | { |
164 | return FlashStatus; |
170 | return FlashStatus; |
165 | } |
171 | } |
- | 172 | ||
166 | } |
173 | } |
167 | else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */ |
174 | else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */ |
168 | { |
175 | { |
169 | /* Erase Page1 */ |
176 | /* Erase Page1 */ |
170 | FLASH_PageErase(PAGE1_BASE_ADDRESS); |
177 | FlashStatus = EE_ErasePages(PAGE1_BASE_ADDRESS,1); |
171 | /* If erase operation was failed, a Flash error code is returned */ |
- | |
172 | FlashStatus = HAL_FLASH_GetError(); |
- | |
173 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
178 | if(FlashStatus!=HAL_FLASH_ERROR_NONE) |
174 | { |
179 | { |
175 | return FlashStatus; |
180 | return FlashStatus; |
176 | } |
181 | } |
177 | /* Mark Page0 as valid */ |
182 | /* Mark Page0 as valid */ |
178 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE0_BASE_ADDRESS, VALID_PAGE); |
183 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE0_BASE_ADDRESS, VALID_PAGE); |
179 | 184 | ||
180 | /* If program operation was failed, a Flash error code is returned */ |
185 | /* If program operation was failed, a Flash error code is returned */ |
181 | FlashStatus = HAL_FLASH_GetError(); |
186 | FlashStatus = HAL_FLASH_GetError(); |
182 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
187 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
Line 208... | Line 213... | ||
208 | } |
213 | } |
209 | } |
214 | } |
210 | else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */ |
215 | else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */ |
211 | { |
216 | { |
212 | /* Erase Page1 */ |
217 | /* Erase Page1 */ |
213 | FLASH_PageErase(PAGE1_BASE_ADDRESS); |
218 | FlashStatus = EE_ErasePages(PAGE1_BASE_ADDRESS,1); |
214 | FlashStatus = HAL_FLASH_GetError(); |
- | |
215 | /* If erase operation was failed, a Flash error code is returned */ |
- | |
216 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
219 | if(FlashStatus!=HAL_FLASH_ERROR_NONE) |
217 | { |
220 | { |
218 | return FlashStatus; |
221 | return FlashStatus; |
219 | } |
222 | } |
- | 223 | ||
220 | } |
224 | } |
221 | else /* Page0 valid, Page1 receive */ |
225 | else /* Page0 valid, Page1 receive */ |
222 | { |
226 | { |
223 | /* Transfer data from Page0 to Page1 */ |
227 | /* Transfer data from Page0 to Page1 */ |
224 | for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++) |
228 | for (VarIdx = 0; VarIdx < NumbOfVar; VarIdx++) |
Line 252... | Line 256... | ||
252 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
256 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
253 | { |
257 | { |
254 | return FlashStatus; |
258 | return FlashStatus; |
255 | } |
259 | } |
256 | /* Erase Page0 */ |
260 | /* Erase Page0 */ |
257 | FLASH_PageErase(PAGE0_BASE_ADDRESS); |
261 | FlashStatus = EE_ErasePages(PAGE0_BASE_ADDRESS,1); |
258 | /* If erase operation was failed, a Flash error code is returned */ |
- | |
259 | FlashStatus = HAL_FLASH_GetError(); |
- | |
260 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
262 | if(FlashStatus!=HAL_FLASH_ERROR_NONE) |
261 | { |
263 | { |
262 | return FlashStatus; |
264 | return FlashStatus; |
263 | } |
265 | } |
264 | } |
266 | } |
265 | break; |
267 | break; |
266 | 268 | ||
267 | default: /* Any other state -> format eeprom */ |
269 | default: /* Any other state -> format eeprom */ |
268 | /* Erase both Page0 and Page1 and set Page0 as valid page */ |
270 | /* Erase both Page0 and Page1 and set Page0 as valid page */ |
Line 375... | Line 377... | ||
375 | static FLASH_Status EE_Format(void) |
377 | static FLASH_Status EE_Format(void) |
376 | { |
378 | { |
377 | FLASH_Status FlashStatus = HAL_FLASH_ERROR_NONE; |
379 | FLASH_Status FlashStatus = HAL_FLASH_ERROR_NONE; |
378 | 380 | ||
379 | /* Erase Page0 and Page1 */ |
381 | /* Erase Page0 and Page1 */ |
380 | /* Fill EraseInit structure*/ |
- | |
381 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
- | |
382 | EraseInitStruct.PageAddress = PAGE0_BASE_ADDRESS; |
382 | FlashStatus = EE_ErasePages(PAGE0_BASE_ADDRESS,1); |
383 | EraseInitStruct.NbPages = 2; |
383 | if(FlashStatus!=HAL_FLASH_ERROR_NONE) |
384 | - | ||
385 | if(HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) |
- | |
386 | /* If erase operation was failed, a Flash error code is returned */ |
- | |
387 | { |
384 | { |
388 | FlashStatus = HAL_FLASH_GetError(); |
- | |
389 | return FlashStatus; |
385 | return FlashStatus; |
390 | } |
386 | } |
391 | - | ||
392 | 387 | ||
393 | /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */ |
388 | /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */ |
394 | uint32_t rc = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE0_BASE_ADDRESS, VALID_PAGE); |
389 | uint32_t rc = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE0_BASE_ADDRESS, VALID_PAGE); |
395 | /* If program operation was failed, a Flash error code is returned */ |
390 | /* If program operation was failed, a Flash error code is returned */ |
396 | 391 | ||
- | 392 | ||
- | 393 | ||
- | 394 | ||
397 | if (rc!= HAL_OK) |
395 | if (rc!= HAL_OK) |
398 | { |
396 | { |
399 | FlashStatus = HAL_FLASH_GetError(); |
397 | FlashStatus = HAL_FLASH_GetError(); |
400 | return FlashStatus; |
398 | return FlashStatus; |
401 | } |
399 | } |
402 | 400 | ||
- | 401 | FlashStatus = EE_ErasePages(PAGE1_BASE_ADDRESS,1); |
|
- | 402 | if(FlashStatus!=HAL_FLASH_ERROR_NONE) |
|
- | 403 | { |
|
- | 404 | return FlashStatus; |
|
- | 405 | } |
|
- | 406 | ||
- | 407 | ||
403 | /* Return Page1 erase operation status */ |
408 | /* Return Page1 erase operation status */ |
404 | return FlashStatus; |
409 | return FlashStatus; |
405 | } |
410 | } |
406 | 411 | ||
407 | /** |
412 | /** |
Line 617... | Line 622... | ||
617 | } |
622 | } |
618 | } |
623 | } |
619 | } |
624 | } |
620 | 625 | ||
621 | /* Erase the old Page: Set old Page status to ERASED status */ |
626 | /* Erase the old Page: Set old Page status to ERASED status */ |
622 | FLASH_PageErase(OldPageAddress); |
627 | FlashStatus = EE_ErasePages(OldPageAddress,1); |
623 | /* If erase operation was failed, a Flash error code is returned */ |
- | |
624 | FlashStatus = HAL_FLASH_GetError(); |
- | |
625 | if (FlashStatus != HAL_FLASH_ERROR_NONE) |
628 | if(FlashStatus!=HAL_FLASH_ERROR_NONE) |
626 | { |
629 | { |
627 | return FlashStatus; |
630 | return FlashStatus; |
628 | } |
631 | } |
629 | 632 | ||
630 | /* Set new Page status to VALID_PAGE status */ |
633 | /* Set new Page status to VALID_PAGE status */ |
631 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,NewPageAddress, VALID_PAGE); |
634 | HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,NewPageAddress, VALID_PAGE); |
632 | /* If program operation was failed, a Flash error code is returned */ |
635 | /* If program operation was failed, a Flash error code is returned */ |
633 | FlashStatus = HAL_FLASH_GetError(); |
636 | FlashStatus = HAL_FLASH_GetError(); |