|
@@ -78,8 +78,7 @@ static uint16_t EE_VerifyPageFullyErased(uint32_t Address);
|
78
|
78
|
* @retval - Flash error code: on write Flash error
|
79
|
79
|
* - FLASH_COMPLETE: on success
|
80
|
80
|
*/
|
81
|
|
-uint16_t EE_Initialise(void)
|
82
|
|
-{
|
|
81
|
+uint16_t EE_Initialise(void) {
|
83
|
82
|
uint16_t PageStatus0 = 6, PageStatus1 = 6;
|
84
|
83
|
uint16_t VarIdx = 0;
|
85
|
84
|
uint16_t EepromStatus = 0, ReadStatus = 0;
|
|
@@ -100,209 +99,141 @@ uint16_t EE_Initialise(void)
|
100
|
99
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
101
|
100
|
|
102
|
101
|
/* Check for invalid header states and repair if necessary */
|
103
|
|
- switch (PageStatus0)
|
104
|
|
- {
|
|
102
|
+ switch (PageStatus0) {
|
105
|
103
|
case ERASED:
|
106
|
|
- if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
|
107
|
|
- {
|
|
104
|
+ if (PageStatus1 == VALID_PAGE) { /* Page0 erased, Page1 valid */
|
108
|
105
|
/* Erase Page0 */
|
109
|
|
- if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
|
110
|
|
- {
|
|
106
|
+ if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
|
111
|
107
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
112
|
108
|
/* If erase operation was failed, a Flash error code is returned */
|
113
|
|
- if (FlashStatus != HAL_OK)
|
114
|
|
- {
|
|
109
|
+ if (FlashStatus != HAL_OK) {
|
115
|
110
|
return FlashStatus;
|
116
|
111
|
}
|
117
|
112
|
}
|
118
|
113
|
}
|
119
|
|
- else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */
|
120
|
|
- {
|
|
114
|
+ else if (PageStatus1 == RECEIVE_DATA) { /* Page0 erased, Page1 receive */
|
121
|
115
|
/* Erase Page0 */
|
122
|
|
- if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
|
123
|
|
- {
|
|
116
|
+ if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
|
124
|
117
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
125
|
118
|
/* If erase operation was failed, a Flash error code is returned */
|
126
|
|
- if (FlashStatus != HAL_OK)
|
127
|
|
- {
|
128
|
|
- return FlashStatus;
|
129
|
|
- }
|
|
119
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
130
|
120
|
}
|
131
|
121
|
/* Mark Page1 as valid */
|
132
|
122
|
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
|
133
|
123
|
/* If program operation was failed, a Flash error code is returned */
|
134
|
|
- if (FlashStatus != HAL_OK)
|
135
|
|
- {
|
136
|
|
- return FlashStatus;
|
137
|
|
- }
|
|
124
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
138
|
125
|
}
|
139
|
|
- else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
|
140
|
|
- {
|
|
126
|
+ else { /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
|
141
|
127
|
/* Erase both Page0 and Page1 and set Page0 as valid page */
|
142
|
128
|
FlashStatus = EE_Format();
|
143
|
129
|
/* If erase/program operation was failed, a Flash error code is returned */
|
144
|
|
- if (FlashStatus != HAL_OK)
|
145
|
|
- {
|
146
|
|
- return FlashStatus;
|
147
|
|
- }
|
|
130
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
148
|
131
|
}
|
149
|
132
|
break;
|
150
|
133
|
|
151
|
134
|
case RECEIVE_DATA:
|
152
|
|
- if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */
|
153
|
|
- {
|
|
135
|
+ if (PageStatus1 == VALID_PAGE) { /* Page0 receive, Page1 valid */
|
154
|
136
|
/* Transfer data from Page1 to Page0 */
|
155
|
|
- for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
|
156
|
|
- {
|
|
137
|
+ for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
|
157
|
138
|
if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
|
158
|
|
- {
|
159
|
139
|
x = VarIdx;
|
160
|
|
- }
|
161
|
|
- if (VarIdx != x)
|
162
|
|
- {
|
|
140
|
+ if (VarIdx != x) {
|
163
|
141
|
/* Read the last variables' updates */
|
164
|
142
|
ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
|
165
|
143
|
/* In case variable corresponding to the virtual address was found */
|
166
|
|
- if (ReadStatus != 0x1)
|
167
|
|
- {
|
|
144
|
+ if (ReadStatus != 0x1) {
|
168
|
145
|
/* Transfer the variable to the Page0 */
|
169
|
146
|
EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
|
170
|
147
|
/* If program operation was failed, a Flash error code is returned */
|
171
|
|
- if (EepromStatus != HAL_OK)
|
172
|
|
- {
|
173
|
|
- return EepromStatus;
|
174
|
|
- }
|
|
148
|
+ if (EepromStatus != HAL_OK) return EepromStatus;
|
175
|
149
|
}
|
176
|
150
|
}
|
177
|
151
|
}
|
178
|
152
|
/* Mark Page0 as valid */
|
179
|
153
|
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
|
180
|
154
|
/* If program operation was failed, a Flash error code is returned */
|
181
|
|
- if (FlashStatus != HAL_OK)
|
182
|
|
- {
|
183
|
|
- return FlashStatus;
|
184
|
|
- }
|
|
155
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
185
|
156
|
pEraseInit.Sector = PAGE1_ID;
|
186
|
157
|
pEraseInit.NbSectors = 1;
|
187
|
158
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
188
|
159
|
/* Erase Page1 */
|
189
|
|
- if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
|
190
|
|
- {
|
|
160
|
+ if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
|
191
|
161
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
192
|
162
|
/* If erase operation was failed, a Flash error code is returned */
|
193
|
|
- if (FlashStatus != HAL_OK)
|
194
|
|
- {
|
195
|
|
- return FlashStatus;
|
196
|
|
- }
|
|
163
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
197
|
164
|
}
|
198
|
165
|
}
|
199
|
|
- else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
|
200
|
|
- {
|
|
166
|
+ else if (PageStatus1 == ERASED) { /* Page0 receive, Page1 erased */
|
201
|
167
|
pEraseInit.Sector = PAGE1_ID;
|
202
|
168
|
pEraseInit.NbSectors = 1;
|
203
|
169
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
204
|
170
|
/* Erase Page1 */
|
205
|
|
- if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
|
206
|
|
- {
|
|
171
|
+ if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
|
207
|
172
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
208
|
173
|
/* If erase operation was failed, a Flash error code is returned */
|
209
|
|
- if (FlashStatus != HAL_OK)
|
210
|
|
- {
|
211
|
|
- return FlashStatus;
|
212
|
|
- }
|
|
174
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
213
|
175
|
}
|
214
|
176
|
/* Mark Page0 as valid */
|
215
|
177
|
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
|
216
|
178
|
/* If program operation was failed, a Flash error code is returned */
|
217
|
|
- if (FlashStatus != HAL_OK)
|
218
|
|
- {
|
219
|
|
- return FlashStatus;
|
220
|
|
- }
|
|
179
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
221
|
180
|
}
|
222
|
|
- else /* Invalid state -> format eeprom */
|
223
|
|
- {
|
|
181
|
+ else { /* Invalid state -> format eeprom */
|
224
|
182
|
/* Erase both Page0 and Page1 and set Page0 as valid page */
|
225
|
183
|
FlashStatus = EE_Format();
|
226
|
184
|
/* If erase/program operation was failed, a Flash error code is returned */
|
227
|
|
- if (FlashStatus != HAL_OK)
|
228
|
|
- {
|
229
|
|
- return FlashStatus;
|
230
|
|
- }
|
|
185
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
231
|
186
|
}
|
232
|
187
|
break;
|
233
|
188
|
|
234
|
189
|
case VALID_PAGE:
|
235
|
|
- if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
|
236
|
|
- {
|
|
190
|
+ if (PageStatus1 == VALID_PAGE) { /* Invalid state -> format eeprom */
|
237
|
191
|
/* Erase both Page0 and Page1 and set Page0 as valid page */
|
238
|
192
|
FlashStatus = EE_Format();
|
239
|
193
|
/* If erase/program operation was failed, a Flash error code is returned */
|
240
|
|
- if (FlashStatus != HAL_OK)
|
241
|
|
- {
|
242
|
|
- return FlashStatus;
|
243
|
|
- }
|
|
194
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
244
|
195
|
}
|
245
|
|
- else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
|
246
|
|
- {
|
|
196
|
+ else if (PageStatus1 == ERASED) { /* Page0 valid, Page1 erased */
|
247
|
197
|
pEraseInit.Sector = PAGE1_ID;
|
248
|
198
|
pEraseInit.NbSectors = 1;
|
249
|
199
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
250
|
200
|
/* Erase Page1 */
|
251
|
|
- if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
|
252
|
|
- {
|
|
201
|
+ if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
|
253
|
202
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
254
|
203
|
/* If erase operation was failed, a Flash error code is returned */
|
255
|
|
- if (FlashStatus != HAL_OK)
|
256
|
|
- {
|
257
|
|
- return FlashStatus;
|
258
|
|
- }
|
|
204
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
259
|
205
|
}
|
260
|
206
|
}
|
261
|
|
- else /* Page0 valid, Page1 receive */
|
262
|
|
- {
|
|
207
|
+ else { /* Page0 valid, Page1 receive */
|
263
|
208
|
/* Transfer data from Page0 to Page1 */
|
264
|
|
- for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
|
265
|
|
- {
|
|
209
|
+ for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
|
266
|
210
|
if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
|
267
|
|
- {
|
268
|
211
|
x = VarIdx;
|
269
|
|
- }
|
270
|
|
- if (VarIdx != x)
|
271
|
|
- {
|
|
212
|
+
|
|
213
|
+ if (VarIdx != x) {
|
272
|
214
|
/* Read the last variables' updates */
|
273
|
215
|
ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
|
274
|
216
|
/* In case variable corresponding to the virtual address was found */
|
275
|
|
- if (ReadStatus != 0x1)
|
276
|
|
- {
|
|
217
|
+ if (ReadStatus != 0x1) {
|
277
|
218
|
/* Transfer the variable to the Page1 */
|
278
|
219
|
EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
|
279
|
220
|
/* If program operation was failed, a Flash error code is returned */
|
280
|
|
- if (EepromStatus != HAL_OK)
|
281
|
|
- {
|
282
|
|
- return EepromStatus;
|
283
|
|
- }
|
|
221
|
+ if (EepromStatus != HAL_OK) return EepromStatus;
|
284
|
222
|
}
|
285
|
223
|
}
|
286
|
224
|
}
|
287
|
225
|
/* Mark Page1 as valid */
|
288
|
226
|
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
|
289
|
227
|
/* If program operation was failed, a Flash error code is returned */
|
290
|
|
- if (FlashStatus != HAL_OK)
|
291
|
|
- {
|
292
|
|
- return FlashStatus;
|
293
|
|
- }
|
|
228
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
294
|
229
|
pEraseInit.Sector = PAGE0_ID;
|
295
|
230
|
pEraseInit.NbSectors = 1;
|
296
|
231
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
297
|
232
|
/* Erase Page0 */
|
298
|
|
- if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
|
299
|
|
- {
|
|
233
|
+ if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
|
300
|
234
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
301
|
235
|
/* If erase operation was failed, a Flash error code is returned */
|
302
|
|
- if (FlashStatus != HAL_OK)
|
303
|
|
- {
|
304
|
|
- return FlashStatus;
|
305
|
|
- }
|
|
236
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
306
|
237
|
}
|
307
|
238
|
}
|
308
|
239
|
break;
|
|
@@ -311,10 +242,7 @@ uint16_t EE_Initialise(void)
|
311
|
242
|
/* Erase both Page0 and Page1 and set Page0 as valid page */
|
312
|
243
|
FlashStatus = EE_Format();
|
313
|
244
|
/* If erase/program operation was failed, a Flash error code is returned */
|
314
|
|
- if (FlashStatus != HAL_OK)
|
315
|
|
- {
|
316
|
|
- return FlashStatus;
|
317
|
|
- }
|
|
245
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
318
|
246
|
break;
|
319
|
247
|
}
|
320
|
248
|
|
|
@@ -322,55 +250,46 @@ uint16_t EE_Initialise(void)
|
322
|
250
|
}
|
323
|
251
|
|
324
|
252
|
/**
|
325
|
|
- * @brief Verify if specified page is fully erased.
|
326
|
|
- * @param Address: page address
|
327
|
|
- * This parameter can be one of the following values:
|
328
|
|
- * @arg PAGE0_BASE_ADDRESS: Page0 base address
|
329
|
|
- * @arg PAGE1_BASE_ADDRESS: Page1 base address
|
330
|
|
- * @retval page fully erased status:
|
331
|
|
- * - 0: if Page not erased
|
332
|
|
- * - 1: if Page erased
|
333
|
|
- */
|
334
|
|
-uint16_t EE_VerifyPageFullyErased(uint32_t Address)
|
335
|
|
-{
|
|
253
|
+ * @brief Verify if specified page is fully erased.
|
|
254
|
+ * @param Address: page address
|
|
255
|
+ * This parameter can be one of the following values:
|
|
256
|
+ * @arg PAGE0_BASE_ADDRESS: Page0 base address
|
|
257
|
+ * @arg PAGE1_BASE_ADDRESS: Page1 base address
|
|
258
|
+ * @retval page fully erased status:
|
|
259
|
+ * - 0: if Page not erased
|
|
260
|
+ * - 1: if Page erased
|
|
261
|
+ */
|
|
262
|
+uint16_t EE_VerifyPageFullyErased(uint32_t Address) {
|
336
|
263
|
uint32_t ReadStatus = 1;
|
337
|
264
|
uint16_t AddressValue = 0x5555;
|
338
|
|
-
|
339
|
265
|
/* Check each active page address starting from end */
|
340
|
|
- while (Address <= PAGE0_END_ADDRESS)
|
341
|
|
- {
|
|
266
|
+ while (Address <= PAGE0_END_ADDRESS) {
|
342
|
267
|
/* Get the current location content to be compared with virtual address */
|
343
|
268
|
AddressValue = (*(__IO uint16_t*)Address);
|
344
|
|
-
|
345
|
269
|
/* Compare the read address with the virtual address */
|
346
|
|
- if (AddressValue != ERASED)
|
347
|
|
- {
|
348
|
|
-
|
|
270
|
+ if (AddressValue != ERASED) {
|
349
|
271
|
/* In case variable value is read, reset ReadStatus flag */
|
350
|
272
|
ReadStatus = 0;
|
351
|
|
-
|
352
|
273
|
break;
|
353
|
274
|
}
|
354
|
275
|
/* Next address location */
|
355
|
|
- Address = Address + 4;
|
|
276
|
+ Address += 4;
|
356
|
277
|
}
|
357
|
|
-
|
358
|
278
|
/* Return ReadStatus value: (0: Page not erased, 1: Sector erased) */
|
359
|
279
|
return ReadStatus;
|
360
|
280
|
}
|
361
|
281
|
|
362
|
282
|
/**
|
363
|
|
- * @brief Returns the last stored variable data, if found, which correspond to
|
364
|
|
- * the passed virtual address
|
365
|
|
- * @param VirtAddress: Variable virtual address
|
366
|
|
- * @param Data: Global variable contains the read variable value
|
367
|
|
- * @retval Success or error status:
|
368
|
|
- * - 0: if variable was found
|
369
|
|
- * - 1: if the variable was not found
|
370
|
|
- * - NO_VALID_PAGE: if no valid page was found.
|
371
|
|
- */
|
372
|
|
-uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data)
|
373
|
|
-{
|
|
283
|
+ * @brief Returns the last stored variable data, if found, which correspond to
|
|
284
|
+ * the passed virtual address
|
|
285
|
+ * @param VirtAddress: Variable virtual address
|
|
286
|
+ * @param Data: Global variable contains the read variable value
|
|
287
|
+ * @retval Success or error status:
|
|
288
|
+ * - 0: if variable was found
|
|
289
|
+ * - 1: if the variable was not found
|
|
290
|
+ * - NO_VALID_PAGE: if no valid page was found.
|
|
291
|
+ */
|
|
292
|
+uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) {
|
374
|
293
|
uint16_t ValidPage = PAGE0;
|
375
|
294
|
uint16_t AddressValue = 0x5555, ReadStatus = 1;
|
376
|
295
|
uint32_t Address = EEPROM_START_ADDRESS, PageStartAddress = EEPROM_START_ADDRESS;
|
|
@@ -379,10 +298,7 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data)
|
379
|
298
|
ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
|
380
|
299
|
|
381
|
300
|
/* Check if there is no valid page */
|
382
|
|
- if (ValidPage == NO_VALID_PAGE)
|
383
|
|
- {
|
384
|
|
- return NO_VALID_PAGE;
|
385
|
|
- }
|
|
301
|
+ if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
|
386
|
302
|
|
387
|
303
|
/* Get the valid Page start Address */
|
388
|
304
|
PageStartAddress = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
|
|
@@ -391,69 +307,54 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data)
|
391
|
307
|
Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
|
392
|
308
|
|
393
|
309
|
/* Check each active page address starting from end */
|
394
|
|
- while (Address > (PageStartAddress + 2))
|
395
|
|
- {
|
|
310
|
+ while (Address > (PageStartAddress + 2)) {
|
396
|
311
|
/* Get the current location content to be compared with virtual address */
|
397
|
312
|
AddressValue = (*(__IO uint16_t*)Address);
|
398
|
313
|
|
399
|
314
|
/* Compare the read address with the virtual address */
|
400
|
|
- if (AddressValue == VirtAddress)
|
401
|
|
- {
|
|
315
|
+ if (AddressValue == VirtAddress) {
|
402
|
316
|
/* Get content of Address-2 which is variable value */
|
403
|
317
|
*Data = (*(__IO uint16_t*)(Address - 2));
|
404
|
|
-
|
405
|
318
|
/* In case variable value is read, reset ReadStatus flag */
|
406
|
319
|
ReadStatus = 0;
|
407
|
|
-
|
408
|
320
|
break;
|
409
|
321
|
}
|
410
|
|
- else
|
411
|
|
- {
|
412
|
|
- /* Next address location */
|
413
|
|
- Address = Address - 4;
|
414
|
|
- }
|
|
322
|
+ else /* Next address location */
|
|
323
|
+ Address -= 4;
|
415
|
324
|
}
|
416
|
|
-
|
417
|
325
|
/* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */
|
418
|
326
|
return ReadStatus;
|
419
|
327
|
}
|
420
|
328
|
|
421
|
329
|
/**
|
422
|
|
- * @brief Writes/upadtes variable data in EEPROM.
|
423
|
|
- * @param VirtAddress: Variable virtual address
|
424
|
|
- * @param Data: 16 bit data to be written
|
425
|
|
- * @retval Success or error status:
|
426
|
|
- * - FLASH_COMPLETE: on success
|
427
|
|
- * - PAGE_FULL: if valid page is full
|
428
|
|
- * - NO_VALID_PAGE: if no valid page was found
|
429
|
|
- * - Flash error code: on write Flash error
|
430
|
|
- */
|
431
|
|
-uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
|
432
|
|
-{
|
433
|
|
- uint16_t Status = 0;
|
434
|
|
-
|
|
330
|
+ * @brief Writes/upadtes variable data in EEPROM.
|
|
331
|
+ * @param VirtAddress: Variable virtual address
|
|
332
|
+ * @param Data: 16 bit data to be written
|
|
333
|
+ * @retval Success or error status:
|
|
334
|
+ * - FLASH_COMPLETE: on success
|
|
335
|
+ * - PAGE_FULL: if valid page is full
|
|
336
|
+ * - NO_VALID_PAGE: if no valid page was found
|
|
337
|
+ * - Flash error code: on write Flash error
|
|
338
|
+ */
|
|
339
|
+uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data) {
|
435
|
340
|
/* Write the variable virtual address and value in the EEPROM */
|
436
|
|
- Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
|
|
341
|
+ uint16_t Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
|
437
|
342
|
|
438
|
343
|
/* In case the EEPROM active page is full */
|
439
|
|
- if (Status == PAGE_FULL)
|
440
|
|
- {
|
441
|
|
- /* Perform Page transfer */
|
|
344
|
+ if (Status == PAGE_FULL) /* Perform Page transfer */
|
442
|
345
|
Status = EE_PageTransfer(VirtAddress, Data);
|
443
|
|
- }
|
444
|
346
|
|
445
|
347
|
/* Return last operation status */
|
446
|
348
|
return Status;
|
447
|
349
|
}
|
448
|
350
|
|
449
|
351
|
/**
|
450
|
|
- * @brief Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE
|
451
|
|
- * @param None
|
452
|
|
- * @retval Status of the last operation (Flash write or erase) done during
|
453
|
|
- * EEPROM formating
|
454
|
|
- */
|
455
|
|
-static HAL_StatusTypeDef EE_Format(void)
|
456
|
|
-{
|
|
352
|
+ * @brief Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE
|
|
353
|
+ * @param None
|
|
354
|
+ * @retval Status of the last operation (Flash write or erase) done during
|
|
355
|
+ * EEPROM formating
|
|
356
|
+ */
|
|
357
|
+static HAL_StatusTypeDef EE_Format(void) {
|
457
|
358
|
HAL_StatusTypeDef FlashStatus = HAL_OK;
|
458
|
359
|
uint32_t SectorError = 0;
|
459
|
360
|
FLASH_EraseInitTypeDef pEraseInit;
|
|
@@ -463,49 +364,37 @@ static HAL_StatusTypeDef EE_Format(void)
|
463
|
364
|
pEraseInit.NbSectors = 1;
|
464
|
365
|
pEraseInit.VoltageRange = VOLTAGE_RANGE;
|
465
|
366
|
/* Erase Page0 */
|
466
|
|
- if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
|
467
|
|
- {
|
|
367
|
+ if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
|
468
|
368
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
469
|
369
|
/* If erase operation was failed, a Flash error code is returned */
|
470
|
|
- if (FlashStatus != HAL_OK)
|
471
|
|
- {
|
472
|
|
- return FlashStatus;
|
473
|
|
- }
|
|
370
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
474
|
371
|
}
|
475
|
372
|
/* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
|
476
|
373
|
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
|
477
|
374
|
/* If program operation was failed, a Flash error code is returned */
|
478
|
|
- if (FlashStatus != HAL_OK)
|
479
|
|
- {
|
480
|
|
- return FlashStatus;
|
481
|
|
- }
|
|
375
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
482
|
376
|
|
483
|
377
|
pEraseInit.Sector = PAGE1_ID;
|
484
|
378
|
/* Erase Page1 */
|
485
|
|
- if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
|
486
|
|
- {
|
|
379
|
+ if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
|
487
|
380
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
488
|
381
|
/* If erase operation was failed, a Flash error code is returned */
|
489
|
|
- if (FlashStatus != HAL_OK)
|
490
|
|
- {
|
491
|
|
- return FlashStatus;
|
492
|
|
- }
|
|
382
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
493
|
383
|
}
|
494
|
384
|
|
495
|
385
|
return HAL_OK;
|
496
|
386
|
}
|
497
|
387
|
|
498
|
388
|
/**
|
499
|
|
- * @brief Find valid Page for write or read operation
|
500
|
|
- * @param Operation: operation to achieve on the valid page.
|
501
|
|
- * This parameter can be one of the following values:
|
502
|
|
- * @arg READ_FROM_VALID_PAGE: read operation from valid page
|
503
|
|
- * @arg WRITE_IN_VALID_PAGE: write operation from valid page
|
504
|
|
- * @retval Valid page number (PAGE or PAGE1) or NO_VALID_PAGE in case
|
505
|
|
- * of no valid page was found
|
506
|
|
- */
|
507
|
|
-static uint16_t EE_FindValidPage(uint8_t Operation)
|
508
|
|
-{
|
|
389
|
+ * @brief Find valid Page for write or read operation
|
|
390
|
+ * @param Operation: operation to achieve on the valid page.
|
|
391
|
+ * This parameter can be one of the following values:
|
|
392
|
+ * @arg READ_FROM_VALID_PAGE: read operation from valid page
|
|
393
|
+ * @arg WRITE_IN_VALID_PAGE: write operation from valid page
|
|
394
|
+ * @retval Valid page number (PAGE or PAGE1) or NO_VALID_PAGE in case
|
|
395
|
+ * of no valid page was found
|
|
396
|
+ */
|
|
397
|
+static uint16_t EE_FindValidPage(uint8_t Operation) {
|
509
|
398
|
uint16_t PageStatus0 = 6, PageStatus1 = 6;
|
510
|
399
|
|
511
|
400
|
/* Get Page0 actual status */
|
|
@@ -515,51 +404,28 @@ static uint16_t EE_FindValidPage(uint8_t Operation)
|
515
|
404
|
PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
|
516
|
405
|
|
517
|
406
|
/* Write or read operation */
|
518
|
|
- switch (Operation)
|
519
|
|
- {
|
|
407
|
+ switch (Operation) {
|
520
|
408
|
case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */
|
521
|
|
- if (PageStatus1 == VALID_PAGE)
|
522
|
|
- {
|
|
409
|
+ if (PageStatus1 == VALID_PAGE) {
|
523
|
410
|
/* Page0 receiving data */
|
524
|
|
- if (PageStatus0 == RECEIVE_DATA)
|
525
|
|
- {
|
526
|
|
- return PAGE0; /* Page0 valid */
|
527
|
|
- }
|
528
|
|
- else
|
529
|
|
- {
|
530
|
|
- return PAGE1; /* Page1 valid */
|
531
|
|
- }
|
|
411
|
+ if (PageStatus0 == RECEIVE_DATA) return PAGE0; /* Page0 valid */
|
|
412
|
+ else return PAGE1; /* Page1 valid */
|
532
|
413
|
}
|
533
|
|
- else if (PageStatus0 == VALID_PAGE)
|
534
|
|
- {
|
|
414
|
+ else if (PageStatus0 == VALID_PAGE) {
|
535
|
415
|
/* Page1 receiving data */
|
536
|
|
- if (PageStatus1 == RECEIVE_DATA)
|
537
|
|
- {
|
538
|
|
- return PAGE1; /* Page1 valid */
|
539
|
|
- }
|
540
|
|
- else
|
541
|
|
- {
|
542
|
|
- return PAGE0; /* Page0 valid */
|
543
|
|
- }
|
|
416
|
+ if (PageStatus1 == RECEIVE_DATA) return PAGE1; /* Page1 valid */
|
|
417
|
+ else return PAGE0; /* Page0 valid */
|
544
|
418
|
}
|
545
|
419
|
else
|
546
|
|
- {
|
547
|
420
|
return NO_VALID_PAGE; /* No valid Page */
|
548
|
|
- }
|
549
|
421
|
|
550
|
422
|
case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */
|
551
|
423
|
if (PageStatus0 == VALID_PAGE)
|
552
|
|
- {
|
553
|
424
|
return PAGE0; /* Page0 valid */
|
554
|
|
- }
|
555
|
425
|
else if (PageStatus1 == VALID_PAGE)
|
556
|
|
- {
|
557
|
426
|
return PAGE1; /* Page1 valid */
|
558
|
|
- }
|
559
|
427
|
else
|
560
|
|
- {
|
561
|
|
- return NO_VALID_PAGE ; /* No valid Page */
|
562
|
|
- }
|
|
428
|
+ return NO_VALID_PAGE; /* No valid Page */
|
563
|
429
|
|
564
|
430
|
default:
|
565
|
431
|
return PAGE0; /* Page0 valid */
|
|
@@ -567,17 +433,16 @@ static uint16_t EE_FindValidPage(uint8_t Operation)
|
567
|
433
|
}
|
568
|
434
|
|
569
|
435
|
/**
|
570
|
|
- * @brief Verify if active page is full and Writes variable in EEPROM.
|
571
|
|
- * @param VirtAddress: 16 bit virtual address of the variable
|
572
|
|
- * @param Data: 16 bit data to be written as variable value
|
573
|
|
- * @retval Success or error status:
|
574
|
|
- * - FLASH_COMPLETE: on success
|
575
|
|
- * - PAGE_FULL: if valid page is full
|
576
|
|
- * - NO_VALID_PAGE: if no valid page was found
|
577
|
|
- * - Flash error code: on write Flash error
|
578
|
|
- */
|
579
|
|
-static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
|
580
|
|
-{
|
|
436
|
+ * @brief Verify if active page is full and Writes variable in EEPROM.
|
|
437
|
+ * @param VirtAddress: 16 bit virtual address of the variable
|
|
438
|
+ * @param Data: 16 bit data to be written as variable value
|
|
439
|
+ * @retval Success or error status:
|
|
440
|
+ * - FLASH_COMPLETE: on success
|
|
441
|
+ * - PAGE_FULL: if valid page is full
|
|
442
|
+ * - NO_VALID_PAGE: if no valid page was found
|
|
443
|
+ * - Flash error code: on write Flash error
|
|
444
|
+ */
|
|
445
|
+static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) {
|
581
|
446
|
HAL_StatusTypeDef FlashStatus = HAL_OK;
|
582
|
447
|
uint16_t ValidPage = PAGE0;
|
583
|
448
|
uint32_t Address = EEPROM_START_ADDRESS, PageEndAddress = EEPROM_START_ADDRESS+PAGE_SIZE;
|
|
@@ -586,10 +451,7 @@ static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Da
|
586
|
451
|
ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
|
587
|
452
|
|
588
|
453
|
/* Check if there is no valid page */
|
589
|
|
- if (ValidPage == NO_VALID_PAGE)
|
590
|
|
- {
|
591
|
|
- return NO_VALID_PAGE;
|
592
|
|
- }
|
|
454
|
+ if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
|
593
|
455
|
|
594
|
456
|
/* Get the valid Page start Address */
|
595
|
457
|
Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
|
|
@@ -598,28 +460,20 @@ static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Da
|
598
|
460
|
PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 1) + (uint32_t)((ValidPage + 1) * PAGE_SIZE));
|
599
|
461
|
|
600
|
462
|
/* Check each active page address starting from begining */
|
601
|
|
- while (Address < PageEndAddress)
|
602
|
|
- {
|
|
463
|
+ while (Address < PageEndAddress) {
|
603
|
464
|
/* Verify if Address and Address+2 contents are 0xFFFFFFFF */
|
604
|
|
- if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF)
|
605
|
|
- {
|
|
465
|
+ if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF) {
|
606
|
466
|
/* Set variable data */
|
607
|
467
|
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address, Data);
|
608
|
468
|
/* If program operation was failed, a Flash error code is returned */
|
609
|
|
- if (FlashStatus != HAL_OK)
|
610
|
|
- {
|
611
|
|
- return FlashStatus;
|
612
|
|
- }
|
|
469
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
613
|
470
|
/* Set variable virtual address */
|
614
|
471
|
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address + 2, VirtAddress);
|
615
|
472
|
/* Return program operation status */
|
616
|
473
|
return FlashStatus;
|
617
|
474
|
}
|
618
|
|
- else
|
619
|
|
- {
|
620
|
|
- /* Next address location */
|
621
|
|
- Address = Address + 4;
|
622
|
|
- }
|
|
475
|
+ else /* Next address location */
|
|
476
|
+ Address += 4;
|
623
|
477
|
}
|
624
|
478
|
|
625
|
479
|
/* Return PAGE_FULL in case the valid page is full */
|
|
@@ -627,18 +481,17 @@ static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Da
|
627
|
481
|
}
|
628
|
482
|
|
629
|
483
|
/**
|
630
|
|
- * @brief Transfers last updated variables data from the full Page to
|
631
|
|
- * an empty one.
|
632
|
|
- * @param VirtAddress: 16 bit virtual address of the variable
|
633
|
|
- * @param Data: 16 bit data to be written as variable value
|
634
|
|
- * @retval Success or error status:
|
635
|
|
- * - FLASH_COMPLETE: on success
|
636
|
|
- * - PAGE_FULL: if valid page is full
|
637
|
|
- * - NO_VALID_PAGE: if no valid page was found
|
638
|
|
- * - Flash error code: on write Flash error
|
639
|
|
- */
|
640
|
|
-static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
|
641
|
|
-{
|
|
484
|
+ * @brief Transfers last updated variables data from the full Page to
|
|
485
|
+ * an empty one.
|
|
486
|
+ * @param VirtAddress: 16 bit virtual address of the variable
|
|
487
|
+ * @param Data: 16 bit data to be written as variable value
|
|
488
|
+ * @retval Success or error status:
|
|
489
|
+ * - FLASH_COMPLETE: on success
|
|
490
|
+ * - PAGE_FULL: if valid page is full
|
|
491
|
+ * - NO_VALID_PAGE: if no valid page was found
|
|
492
|
+ * - Flash error code: on write Flash error
|
|
493
|
+ */
|
|
494
|
+static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) {
|
642
|
495
|
HAL_StatusTypeDef FlashStatus = HAL_OK;
|
643
|
496
|
uint32_t NewPageAddress = EEPROM_START_ADDRESS;
|
644
|
497
|
uint16_t OldPageId=0;
|
|
@@ -650,60 +503,42 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
|
650
|
503
|
/* Get active Page for read operation */
|
651
|
504
|
ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
|
652
|
505
|
|
653
|
|
- if (ValidPage == PAGE1) /* Page1 valid */
|
654
|
|
- {
|
|
506
|
+ if (ValidPage == PAGE1) { /* Page1 valid */
|
655
|
507
|
/* New page address where variable will be moved to */
|
656
|
508
|
NewPageAddress = PAGE0_BASE_ADDRESS;
|
657
|
|
-
|
658
|
509
|
/* Old page ID where variable will be taken from */
|
659
|
510
|
OldPageId = PAGE1_ID;
|
660
|
511
|
}
|
661
|
|
- else if (ValidPage == PAGE0) /* Page0 valid */
|
662
|
|
- {
|
|
512
|
+ else if (ValidPage == PAGE0) { /* Page0 valid */
|
663
|
513
|
/* New page address where variable will be moved to */
|
664
|
514
|
NewPageAddress = PAGE1_BASE_ADDRESS;
|
665
|
|
-
|
666
|
515
|
/* Old page ID where variable will be taken from */
|
667
|
516
|
OldPageId = PAGE0_ID;
|
668
|
517
|
}
|
669
|
518
|
else
|
670
|
|
- {
|
671
|
519
|
return NO_VALID_PAGE; /* No valid Page */
|
672
|
|
- }
|
673
|
520
|
|
674
|
521
|
/* Set the new Page status to RECEIVE_DATA status */
|
675
|
522
|
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, RECEIVE_DATA);
|
676
|
523
|
/* If program operation was failed, a Flash error code is returned */
|
677
|
|
- if (FlashStatus != HAL_OK)
|
678
|
|
- {
|
679
|
|
- return FlashStatus;
|
680
|
|
- }
|
|
524
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
681
|
525
|
|
682
|
526
|
/* Write the variable passed as parameter in the new active page */
|
683
|
527
|
EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
|
684
|
528
|
/* If program operation was failed, a Flash error code is returned */
|
685
|
|
- if (EepromStatus != HAL_OK)
|
686
|
|
- {
|
687
|
|
- return EepromStatus;
|
688
|
|
- }
|
|
529
|
+ if (EepromStatus != HAL_OK) return EepromStatus;
|
689
|
530
|
|
690
|
531
|
/* Transfer process: transfer variables from old to the new active page */
|
691
|
|
- for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
|
692
|
|
- {
|
693
|
|
- if (VirtAddVarTab[VarIdx] != VirtAddress) /* Check each variable except the one passed as parameter */
|
694
|
|
- {
|
|
532
|
+ for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) {
|
|
533
|
+ if (VirtAddVarTab[VarIdx] != VirtAddress) { /* Check each variable except the one passed as parameter */
|
695
|
534
|
/* Read the other last variable updates */
|
696
|
535
|
ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
|
697
|
536
|
/* In case variable corresponding to the virtual address was found */
|
698
|
|
- if (ReadStatus != 0x1)
|
699
|
|
- {
|
|
537
|
+ if (ReadStatus != 0x1) {
|
700
|
538
|
/* Transfer the variable to the new active page */
|
701
|
539
|
EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
|
702
|
540
|
/* If program operation was failed, a Flash error code is returned */
|
703
|
|
- if (EepromStatus != HAL_OK)
|
704
|
|
- {
|
705
|
|
- return EepromStatus;
|
706
|
|
- }
|
|
541
|
+ if (EepromStatus != HAL_OK) return EepromStatus;
|
707
|
542
|
}
|
708
|
543
|
}
|
709
|
544
|
}
|
|
@@ -716,18 +551,12 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
|
716
|
551
|
/* Erase the old Page: Set old Page status to ERASED status */
|
717
|
552
|
FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
|
718
|
553
|
/* If erase operation was failed, a Flash error code is returned */
|
719
|
|
- if (FlashStatus != HAL_OK)
|
720
|
|
- {
|
721
|
|
- return FlashStatus;
|
722
|
|
- }
|
|
554
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
723
|
555
|
|
724
|
556
|
/* Set new Page status to VALID_PAGE status */
|
725
|
557
|
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, VALID_PAGE);
|
726
|
558
|
/* If program operation was failed, a Flash error code is returned */
|
727
|
|
- if (FlashStatus != HAL_OK)
|
728
|
|
- {
|
729
|
|
- return FlashStatus;
|
730
|
|
- }
|
|
559
|
+ if (FlashStatus != HAL_OK) return FlashStatus;
|
731
|
560
|
|
732
|
561
|
/* Return last operation flash status */
|
733
|
562
|
return FlashStatus;
|
|
@@ -736,7 +565,7 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
|
736
|
565
|
#endif // STM32F7
|
737
|
566
|
|
738
|
567
|
/**
|
739
|
|
- * @}
|
740
|
|
- */
|
|
568
|
+ * @}
|
|
569
|
+ */
|
741
|
570
|
|
742
|
571
|
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
|