|
@@ -8,8 +8,8 @@
|
8
|
8
|
******************************************************************************
|
9
|
9
|
* @attention
|
10
|
10
|
*
|
11
|
|
- * <h2><center>© Copyright © 2016 STMicroelectronics International N.V.
|
12
|
|
- * All rights reserved.</center></h2>
|
|
11
|
+ * Copyright © 2016 STMicroelectronics International N.V.
|
|
12
|
+ * All rights reserved.
|
13
|
13
|
*
|
14
|
14
|
* Redistribution and use in source and binary forms, with or without
|
15
|
15
|
* modification, are permitted, provided that the following conditions are met:
|
|
@@ -47,14 +47,11 @@
|
47
|
47
|
/** @addtogroup EEPROM_Emulation
|
48
|
48
|
* @{
|
49
|
49
|
*/
|
50
|
|
-#if defined(STM32GENERIC) && (defined(STM32F4))
|
|
50
|
+#if defined(STM32GENERIC) && defined(STM32F4)
|
51
|
51
|
|
52
|
52
|
/* Includes ------------------------------------------------------------------*/
|
53
|
53
|
#include "eeprom_emul.h"
|
54
|
54
|
|
55
|
|
-/* Private typedef -----------------------------------------------------------*/
|
56
|
|
-/* Private define ------------------------------------------------------------*/
|
57
|
|
-/* Private macro -------------------------------------------------------------*/
|
58
|
55
|
/* Private variables ---------------------------------------------------------*/
|
59
|
56
|
|
60
|
57
|
/* Global variable used to store variable value in read sequence */
|
|
@@ -79,76 +76,66 @@ static uint16_t EE_VerifyPageFullyErased(uint32_t Address);
|
79
|
76
|
* - FLASH_COMPLETE: on success
|
80
|
77
|
*/
|
81
|
78
|
uint16_t EE_Initialize(void) {
|
82
|
|
- uint16_t PageStatus0 = 6, PageStatus1 = 6;
|
83
|
|
- uint16_t VarIdx = 0;
|
84
|
|
- uint16_t EepromStatus = 0, ReadStatus = 0;
|
85
|
|
- int16_t x = -1;
|
86
|
|
- HAL_StatusTypeDef FlashStatus;
|
87
|
|
- uint32_t SectorError = 0;
|
88
|
|
- FLASH_EraseInitTypeDef pEraseInit;
|
89
|
|
-
|
90
|
|
-
|
91
|
|
- /* Get Page0 status */
|
92
|
|
- PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
|
93
|
|
- /* Get Page1 status */
|
94
|
|
- PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
|
|
79
|
+ /* Get Page0 and Page1 status */
|
|
80
|
+ uint16_t PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS),
|
|
81
|
+ PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
|
95
|
82
|
|
|
83
|
+ FLASH_EraseInitTypeDef pEraseInit;
|
96
|
84
|
pEraseInit.TypeErase = TYPEERASE_SECTORS;
|
97
|
85
|
pEraseInit.Sector = PAGE0_ID;
|
98
|
86
|
pEraseInit.NbSectors = 1;
|
99
|
87
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
100
|
88
|
|
101
|
89
|
/* Check for invalid header states and repair if necessary */
|
|
90
|
+ uint32_t SectorError;
|
102
|
91
|
switch (PageStatus0) {
|
103
|
92
|
case ERASED:
|
104
|
93
|
if (PageStatus1 == VALID_PAGE) { /* Page0 erased, Page1 valid */
|
105
|
|
- /* Erase Page0 */
|
|
94
|
+ /* Erase Page0 */
|
106
|
95
|
if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
|
107
|
|
- FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
108
|
|
- /* If erase operation was failed, a Flash error code is returned */
|
109
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
96
|
+ /* As the last operation, simply return the result */
|
|
97
|
+ return HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
110
|
98
|
}
|
111
|
99
|
}
|
112
|
100
|
else if (PageStatus1 == RECEIVE_DATA) { /* Page0 erased, Page1 receive */
|
113
|
101
|
/* Erase Page0 */
|
114
|
102
|
if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
|
115
|
|
- FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
|
103
|
+ HAL_StatusTypeDef fStat = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
116
|
104
|
/* If erase operation was failed, a Flash error code is returned */
|
117
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
105
|
+ if (fStat != HAL_OK) return fStat;
|
118
|
106
|
}
|
119
|
107
|
/* Mark Page1 as valid */
|
120
|
|
- FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
|
121
|
|
- /* If program operation was failed, a Flash error code is returned */
|
122
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
108
|
+ /* As the last operation, simply return the result */
|
|
109
|
+ return HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
|
123
|
110
|
}
|
124
|
111
|
else { /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
|
125
|
112
|
/* Erase both Page0 and Page1 and set Page0 as valid page */
|
126
|
|
- FlashStatus = EE_Format();
|
127
|
|
- /* If erase/program operation was failed, a Flash error code is returned */
|
128
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
113
|
+ /* As the last operation, simply return the result */
|
|
114
|
+ return EE_Format();
|
129
|
115
|
}
|
130
|
116
|
break;
|
131
|
117
|
|
132
|
118
|
case RECEIVE_DATA:
|
133
|
119
|
if (PageStatus1 == VALID_PAGE) { /* Page0 receive, Page1 valid */
|
134
|
120
|
/* Transfer data from Page1 to Page0 */
|
135
|
|
- for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
|
|
121
|
+ int16_t x = -1;
|
|
122
|
+ for (uint16_t VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
|
136
|
123
|
if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
|
137
|
124
|
x = VarIdx;
|
138
|
125
|
if (VarIdx != x) {
|
139
|
126
|
/* Read the last variables' updates */
|
140
|
|
- ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
|
|
127
|
+ uint16_t ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
|
141
|
128
|
/* In case variable corresponding to the virtual address was found */
|
142
|
129
|
if (ReadStatus != 0x1) {
|
143
|
130
|
/* Transfer the variable to the Page0 */
|
144
|
|
- EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
|
|
131
|
+ uint16_t EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
|
145
|
132
|
/* If program operation was failed, a Flash error code is returned */
|
146
|
133
|
if (EepromStatus != HAL_OK) return EepromStatus;
|
147
|
134
|
}
|
148
|
135
|
}
|
149
|
136
|
}
|
150
|
137
|
/* Mark Page0 as valid */
|
151
|
|
- FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
|
|
138
|
+ HAL_StatusTypeDef FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
|
152
|
139
|
/* If program operation was failed, a Flash error code is returned */
|
153
|
140
|
if (FlashStatus != HAL_OK) return FlashStatus;
|
154
|
141
|
pEraseInit.Sector = PAGE1_ID;
|
|
@@ -156,9 +143,8 @@ uint16_t EE_Initialize(void) {
|
156
|
143
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
157
|
144
|
/* Erase Page1 */
|
158
|
145
|
if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
|
159
|
|
- FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
160
|
|
- /* If erase operation was failed, a Flash error code is returned */
|
161
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
146
|
+ /* As the last operation, simply return the result */
|
|
147
|
+ return HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
162
|
148
|
}
|
163
|
149
|
}
|
164
|
150
|
else if (PageStatus1 == ERASED) { /* Page0 receive, Page1 erased */
|
|
@@ -167,20 +153,18 @@ uint16_t EE_Initialize(void) {
|
167
|
153
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
168
|
154
|
/* Erase Page1 */
|
169
|
155
|
if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
|
170
|
|
- FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
|
156
|
+ HAL_StatusTypeDef fStat = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
171
|
157
|
/* If erase operation was failed, a Flash error code is returned */
|
172
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
158
|
+ if (fStat != HAL_OK) return fStat;
|
173
|
159
|
}
|
174
|
160
|
/* Mark Page0 as valid */
|
175
|
|
- FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
|
176
|
|
- /* If program operation was failed, a Flash error code is returned */
|
177
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
161
|
+ /* As the last operation, simply return the result */
|
|
162
|
+ return HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
|
178
|
163
|
}
|
179
|
164
|
else { /* Invalid state -> format eeprom */
|
180
|
165
|
/* Erase both Page0 and Page1 and set Page0 as valid page */
|
181
|
|
- FlashStatus = EE_Format();
|
182
|
|
- /* If erase/program operation was failed, a Flash error code is returned */
|
183
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
166
|
+ /* As the last operation, simply return the result */
|
|
167
|
+ return EE_Format();
|
184
|
168
|
}
|
185
|
169
|
break;
|
186
|
170
|
|
|
@@ -204,17 +188,18 @@ uint16_t EE_Initialize(void) {
|
204
|
188
|
}
|
205
|
189
|
else { /* Page0 valid, Page1 receive */
|
206
|
190
|
/* Transfer data from Page0 to Page1 */
|
207
|
|
- for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
|
|
191
|
+ int16_t x = -1;
|
|
192
|
+ for (uint16_t VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
|
208
|
193
|
if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
|
209
|
194
|
x = VarIdx;
|
210
|
195
|
|
211
|
196
|
if (VarIdx != x) {
|
212
|
197
|
/* Read the last variables' updates */
|
213
|
|
- ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
|
|
198
|
+ uint16_t ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
|
214
|
199
|
/* In case variable corresponding to the virtual address was found */
|
215
|
200
|
if (ReadStatus != 0x1) {
|
216
|
201
|
/* Transfer the variable to the Page1 */
|
217
|
|
- EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
|
|
202
|
+ uint16_t EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
|
218
|
203
|
/* If program operation was failed, a Flash error code is returned */
|
219
|
204
|
if (EepromStatus != HAL_OK) return EepromStatus;
|
220
|
205
|
}
|
|
@@ -229,19 +214,16 @@ uint16_t EE_Initialize(void) {
|
229
|
214
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
230
|
215
|
/* Erase Page0 */
|
231
|
216
|
if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
|
232
|
|
- FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
233
|
|
- /* If erase operation was failed, a Flash error code is returned */
|
234
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
217
|
+ /* As the last operation, simply return the result */
|
|
218
|
+ return HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
235
|
219
|
}
|
236
|
220
|
}
|
237
|
221
|
break;
|
238
|
222
|
|
239
|
223
|
default: /* Any other state -> format eeprom */
|
240
|
224
|
/* Erase both Page0 and Page1 and set Page0 as valid page */
|
241
|
|
- FlashStatus = EE_Format();
|
242
|
|
- /* If erase/program operation was failed, a Flash error code is returned */
|
243
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
244
|
|
- break;
|
|
225
|
+ /* As the last operation, simply return the result */
|
|
226
|
+ return EE_Format();
|
245
|
227
|
}
|
246
|
228
|
|
247
|
229
|
return HAL_OK;
|
|
@@ -259,11 +241,10 @@ uint16_t EE_Initialize(void) {
|
259
|
241
|
*/
|
260
|
242
|
uint16_t EE_VerifyPageFullyErased(uint32_t Address) {
|
261
|
243
|
uint32_t ReadStatus = 1;
|
262
|
|
- uint16_t AddressValue = 0x5555;
|
263
|
244
|
/* Check each active page address starting from end */
|
264
|
245
|
while (Address <= PAGE0_END_ADDRESS) {
|
265
|
246
|
/* Get the current location content to be compared with virtual address */
|
266
|
|
- AddressValue = (*(__IO uint16_t*)Address);
|
|
247
|
+ uint16_t AddressValue = (*(__IO uint16_t*)Address);
|
267
|
248
|
/* Compare the read address with the virtual address */
|
268
|
249
|
if (AddressValue != ERASED) {
|
269
|
250
|
/* In case variable value is read, reset ReadStatus flag */
|
|
@@ -288,26 +269,22 @@ uint16_t EE_VerifyPageFullyErased(uint32_t Address) {
|
288
|
269
|
* - NO_VALID_PAGE: if no valid page was found.
|
289
|
270
|
*/
|
290
|
271
|
uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) {
|
291
|
|
- uint16_t ValidPage = PAGE0;
|
292
|
|
- uint16_t AddressValue = 0x5555, ReadStatus = 1;
|
293
|
|
- uint32_t Address = EEPROM_START_ADDRESS, PageStartAddress = EEPROM_START_ADDRESS;
|
|
272
|
+ uint16_t ReadStatus = 1;
|
294
|
273
|
|
295
|
274
|
/* Get active Page for read operation */
|
296
|
|
- ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
|
|
275
|
+ uint16_t ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
|
297
|
276
|
|
298
|
277
|
/* Check if there is no valid page */
|
299
|
278
|
if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
|
300
|
279
|
|
301
|
|
- /* Get the valid Page start Address */
|
302
|
|
- PageStartAddress = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
|
303
|
|
-
|
304
|
|
- /* Get the valid Page end Address */
|
305
|
|
- Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
|
|
280
|
+ /* Get the valid Page start and end Addresses */
|
|
281
|
+ uint32_t PageStartAddress = uint32_t(EEPROM_START_ADDRESS) + uint32_t(ValidPage * (PAGE_SIZE)),
|
|
282
|
+ Address = PageStartAddress + PAGE_SIZE - 2;
|
306
|
283
|
|
307
|
284
|
/* Check each active page address starting from end */
|
308
|
|
- while (Address > (PageStartAddress + 2)) {
|
|
285
|
+ while (Address > PageStartAddress + 2) {
|
309
|
286
|
/* Get the current location content to be compared with virtual address */
|
310
|
|
- AddressValue = (*(__IO uint16_t*)Address);
|
|
287
|
+ uint16_t AddressValue = (*(__IO uint16_t*)Address);
|
311
|
288
|
|
312
|
289
|
/* Compare the read address with the virtual address */
|
313
|
290
|
if (AddressValue == VirtAddress) {
|
|
@@ -353,16 +330,17 @@ uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data) {
|
353
|
330
|
* EEPROM formating
|
354
|
331
|
*/
|
355
|
332
|
static HAL_StatusTypeDef EE_Format(void) {
|
356
|
|
- HAL_StatusTypeDef FlashStatus = HAL_OK;
|
357
|
|
- uint32_t SectorError = 0;
|
358
|
333
|
FLASH_EraseInitTypeDef pEraseInit;
|
359
|
|
-
|
360
|
334
|
pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
|
361
|
335
|
pEraseInit.Sector = PAGE0_ID;
|
362
|
336
|
pEraseInit.NbSectors = 1;
|
363
|
337
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
|
338
|
+
|
|
339
|
+ HAL_StatusTypeDef FlashStatus; // = HAL_OK
|
|
340
|
+
|
364
|
341
|
/* Erase Page0 */
|
365
|
342
|
if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
|
|
343
|
+ uint32_t SectorError;
|
366
|
344
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
367
|
345
|
/* If erase operation was failed, a Flash error code is returned */
|
368
|
346
|
if (FlashStatus != HAL_OK) return FlashStatus;
|
|
@@ -375,9 +353,9 @@ static HAL_StatusTypeDef EE_Format(void) {
|
375
|
353
|
pEraseInit.Sector = PAGE1_ID;
|
376
|
354
|
/* Erase Page1 */
|
377
|
355
|
if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
|
378
|
|
- FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
379
|
|
- /* If erase operation was failed, a Flash error code is returned */
|
380
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
|
356
|
+ /* As the last operation, just return the result code */
|
|
357
|
+ uint32_t SectorError;
|
|
358
|
+ return HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
381
|
359
|
}
|
382
|
360
|
|
383
|
361
|
return HAL_OK;
|
|
@@ -393,26 +371,20 @@ static HAL_StatusTypeDef EE_Format(void) {
|
393
|
371
|
* of no valid page was found
|
394
|
372
|
*/
|
395
|
373
|
static uint16_t EE_FindValidPage(uint8_t Operation) {
|
396
|
|
- uint16_t PageStatus0 = 6, PageStatus1 = 6;
|
397
|
|
-
|
398
|
|
- /* Get Page0 actual status */
|
399
|
|
- PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
|
400
|
|
-
|
401
|
|
- /* Get Page1 actual status */
|
402
|
|
- PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
|
|
374
|
+ /* Get Page0 and Page1 actual status */
|
|
375
|
+ uint16_t PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS),
|
|
376
|
+ PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
|
403
|
377
|
|
404
|
378
|
/* Write or read operation */
|
405
|
379
|
switch (Operation) {
|
406
|
380
|
case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */
|
407
|
381
|
if (PageStatus1 == VALID_PAGE) {
|
408
|
382
|
/* Page0 receiving data */
|
409
|
|
- if (PageStatus0 == RECEIVE_DATA) return PAGE0; /* Page0 valid */
|
410
|
|
- else return PAGE1; /* Page1 valid */
|
|
383
|
+ return (PageStatus0 == RECEIVE_DATA) ? PAGE0 : PAGE1;
|
411
|
384
|
}
|
412
|
385
|
else if (PageStatus0 == VALID_PAGE) {
|
413
|
386
|
/* Page1 receiving data */
|
414
|
|
- if (PageStatus1 == RECEIVE_DATA) return PAGE1; /* Page1 valid */
|
415
|
|
- else return PAGE0; /* Page0 valid */
|
|
387
|
+ return (PageStatus1 == RECEIVE_DATA) ? PAGE1 : PAGE0;
|
416
|
388
|
}
|
417
|
389
|
else
|
418
|
390
|
return NO_VALID_PAGE; /* No valid Page */
|
|
@@ -441,34 +413,26 @@ static uint16_t EE_FindValidPage(uint8_t Operation) {
|
441
|
413
|
* - Flash error code: on write Flash error
|
442
|
414
|
*/
|
443
|
415
|
static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) {
|
444
|
|
- HAL_StatusTypeDef FlashStatus = HAL_OK;
|
445
|
|
- uint16_t ValidPage = PAGE0;
|
446
|
|
- uint32_t Address = EEPROM_START_ADDRESS, PageEndAddress = EEPROM_START_ADDRESS+PAGE_SIZE;
|
447
|
|
-
|
448
|
416
|
/* Get valid Page for write operation */
|
449
|
|
- ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
|
|
417
|
+ uint16_t ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
|
450
|
418
|
|
451
|
419
|
/* Check if there is no valid page */
|
452
|
420
|
if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
|
453
|
421
|
|
454
|
|
- /* Get the valid Page start Address */
|
455
|
|
- Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
|
456
|
|
-
|
457
|
|
- /* Get the valid Page end Address */
|
458
|
|
- PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 1) + (uint32_t)((ValidPage + 1) * PAGE_SIZE));
|
|
422
|
+ /* Get the valid Page start and end Addresses */
|
|
423
|
+ uint32_t Address = uint32_t(EEPROM_START_ADDRESS) + uint32_t(ValidPage * (PAGE_SIZE)),
|
|
424
|
+ PageEndAddress = Address + PAGE_SIZE - 1;
|
459
|
425
|
|
460
|
426
|
/* Check each active page address starting from begining */
|
461
|
427
|
while (Address < PageEndAddress) {
|
462
|
428
|
/* Verify if Address and Address+2 contents are 0xFFFFFFFF */
|
463
|
429
|
if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF) {
|
464
|
430
|
/* Set variable data */
|
465
|
|
- FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address, Data);
|
|
431
|
+ HAL_StatusTypeDef FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address, Data);
|
466
|
432
|
/* If program operation was failed, a Flash error code is returned */
|
467
|
433
|
if (FlashStatus != HAL_OK) return FlashStatus;
|
468
|
|
- /* Set variable virtual address */
|
469
|
|
- FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address + 2, VirtAddress);
|
470
|
|
- /* Return program operation status */
|
471
|
|
- return FlashStatus;
|
|
434
|
+ /* Set variable virtual address, return status */
|
|
435
|
+ return HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address + 2, VirtAddress);
|
472
|
436
|
}
|
473
|
437
|
else /* Next address location */
|
474
|
438
|
Address += 4;
|
|
@@ -490,16 +454,10 @@ static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Da
|
490
|
454
|
* - Flash error code: on write Flash error
|
491
|
455
|
*/
|
492
|
456
|
static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) {
|
493
|
|
- HAL_StatusTypeDef FlashStatus = HAL_OK;
|
494
|
|
- uint32_t NewPageAddress = EEPROM_START_ADDRESS;
|
495
|
|
- uint16_t OldPageId=0;
|
496
|
|
- uint16_t ValidPage = PAGE0, VarIdx = 0;
|
497
|
|
- uint16_t EepromStatus = 0, ReadStatus = 0;
|
498
|
|
- uint32_t SectorError = 0;
|
499
|
|
- FLASH_EraseInitTypeDef pEraseInit;
|
500
|
|
-
|
501
|
457
|
/* Get active Page for read operation */
|
502
|
|
- ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
|
|
458
|
+ uint16_t ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
|
|
459
|
+ uint32_t NewPageAddress = EEPROM_START_ADDRESS;
|
|
460
|
+ uint16_t OldPageId = 0;
|
503
|
461
|
|
504
|
462
|
if (ValidPage == PAGE1) { /* Page1 valid */
|
505
|
463
|
/* New page address where variable will be moved to */
|
|
@@ -517,20 +475,20 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) {
|
517
|
475
|
return NO_VALID_PAGE; /* No valid Page */
|
518
|
476
|
|
519
|
477
|
/* Set the new Page status to RECEIVE_DATA status */
|
520
|
|
- FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, RECEIVE_DATA);
|
|
478
|
+ HAL_StatusTypeDef FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, RECEIVE_DATA);
|
521
|
479
|
/* If program operation was failed, a Flash error code is returned */
|
522
|
480
|
if (FlashStatus != HAL_OK) return FlashStatus;
|
523
|
481
|
|
524
|
482
|
/* Write the variable passed as parameter in the new active page */
|
525
|
|
- EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
|
|
483
|
+ uint16_t EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
|
526
|
484
|
/* If program operation was failed, a Flash error code is returned */
|
527
|
485
|
if (EepromStatus != HAL_OK) return EepromStatus;
|
528
|
486
|
|
529
|
487
|
/* Transfer process: transfer variables from old to the new active page */
|
530
|
|
- for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
|
|
488
|
+ for (uint16_t VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
|
531
|
489
|
if (VirtAddVarTab[VarIdx] != VirtAddress) { /* Check each variable except the one passed as parameter */
|
532
|
490
|
/* Read the other last variable updates */
|
533
|
|
- ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
|
|
491
|
+ uint16_t ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
|
534
|
492
|
/* In case variable corresponding to the virtual address was found */
|
535
|
493
|
if (ReadStatus != 0x1) {
|
536
|
494
|
/* Transfer the variable to the new active page */
|
|
@@ -541,26 +499,24 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) {
|
541
|
499
|
}
|
542
|
500
|
}
|
543
|
501
|
|
|
502
|
+ FLASH_EraseInitTypeDef pEraseInit;
|
544
|
503
|
pEraseInit.TypeErase = TYPEERASE_SECTORS;
|
545
|
504
|
pEraseInit.Sector = OldPageId;
|
546
|
505
|
pEraseInit.NbSectors = 1;
|
547
|
506
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
548
|
507
|
|
549
|
508
|
/* Erase the old Page: Set old Page status to ERASED status */
|
|
509
|
+ uint32_t SectorError;
|
550
|
510
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
551
|
511
|
/* If erase operation was failed, a Flash error code is returned */
|
552
|
512
|
if (FlashStatus != HAL_OK) return FlashStatus;
|
553
|
513
|
|
554
|
514
|
/* Set new Page status to VALID_PAGE status */
|
555
|
|
- FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, VALID_PAGE);
|
556
|
|
- /* If program operation was failed, a Flash error code is returned */
|
557
|
|
- if (FlashStatus != HAL_OK) return FlashStatus;
|
558
|
|
-
|
559
|
|
- /* Return last operation flash status */
|
560
|
|
- return FlashStatus;
|
|
515
|
+ /* As the last operation, just return the result code */
|
|
516
|
+ return HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, VALID_PAGE);
|
561
|
517
|
}
|
562
|
518
|
|
563
|
|
-#endif // STM32F4 || STM32F4xx
|
|
519
|
+#endif // STM32GENERIC && STM32F4
|
564
|
520
|
|
565
|
521
|
/**
|
566
|
522
|
* @}
|