Browse Source

Tweak STM32F4/7 eeprom emulation (#14563)

Scott Lahteine 6 years ago
parent
commit
f990ebfb09
No account linked to committer's email address

+ 77
- 121
Marlin/src/HAL/HAL_STM32F4/EEPROM_Emul/eeprom_emul.cpp View File

8
   ******************************************************************************
8
   ******************************************************************************
9
   * @attention
9
   * @attention
10
   *
10
   *
11
-  * <h2><center>&copy; 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
   * Redistribution and use in source and binary forms, with or without
14
   * Redistribution and use in source and binary forms, with or without
15
   * modification, are permitted, provided that the following conditions are met:
15
   * modification, are permitted, provided that the following conditions are met:
47
 /** @addtogroup EEPROM_Emulation
47
 /** @addtogroup EEPROM_Emulation
48
   * @{
48
   * @{
49
   */
49
   */
50
-#if defined(STM32GENERIC) && (defined(STM32F4))
50
+#if defined(STM32GENERIC) && defined(STM32F4)
51
 
51
 
52
 /* Includes ------------------------------------------------------------------*/
52
 /* Includes ------------------------------------------------------------------*/
53
 #include "eeprom_emul.h"
53
 #include "eeprom_emul.h"
54
 
54
 
55
-/* Private typedef -----------------------------------------------------------*/
56
-/* Private define ------------------------------------------------------------*/
57
-/* Private macro -------------------------------------------------------------*/
58
 /* Private variables ---------------------------------------------------------*/
55
 /* Private variables ---------------------------------------------------------*/
59
 
56
 
60
 /* Global variable used to store variable value in read sequence */
57
 /* Global variable used to store variable value in read sequence */
79
   *         - FLASH_COMPLETE: on success
76
   *         - FLASH_COMPLETE: on success
80
   */
77
   */
81
 uint16_t EE_Initialize(void) {
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
   pEraseInit.TypeErase = TYPEERASE_SECTORS;
84
   pEraseInit.TypeErase = TYPEERASE_SECTORS;
97
   pEraseInit.Sector = PAGE0_ID;
85
   pEraseInit.Sector = PAGE0_ID;
98
   pEraseInit.NbSectors = 1;
86
   pEraseInit.NbSectors = 1;
99
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
87
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
100
 
88
 
101
   /* Check for invalid header states and repair if necessary */
89
   /* Check for invalid header states and repair if necessary */
90
+  uint32_t SectorError;
102
   switch (PageStatus0) {
91
   switch (PageStatus0) {
103
     case ERASED:
92
     case ERASED:
104
       if (PageStatus1 == VALID_PAGE) { /* Page0 erased, Page1 valid */
93
       if (PageStatus1 == VALID_PAGE) { /* Page0 erased, Page1 valid */
105
-          /* Erase Page0 */
94
+        /* Erase Page0 */
106
         if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
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
       else if (PageStatus1 == RECEIVE_DATA) { /* Page0 erased, Page1 receive */
100
       else if (PageStatus1 == RECEIVE_DATA) { /* Page0 erased, Page1 receive */
113
         /* Erase Page0 */
101
         /* Erase Page0 */
114
         if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
102
         if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
115
-          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
103
+          HAL_StatusTypeDef fStat = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
116
           /* If erase operation was failed, a Flash error code is returned */
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
         /* Mark Page1 as valid */
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
       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 */
125
         /* Erase both Page0 and Page1 and set Page0 as valid page */
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
       break;
116
       break;
131
 
117
 
132
     case RECEIVE_DATA:
118
     case RECEIVE_DATA:
133
       if (PageStatus1 == VALID_PAGE) { /* Page0 receive, Page1 valid */
119
       if (PageStatus1 == VALID_PAGE) { /* Page0 receive, Page1 valid */
134
         /* Transfer data from Page1 to Page0 */
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
           if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
123
           if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
137
             x = VarIdx;
124
             x = VarIdx;
138
           if (VarIdx != x) {
125
           if (VarIdx != x) {
139
             /* Read the last variables' updates */
126
             /* Read the last variables' updates */
140
-            ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
127
+            uint16_t ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
141
             /* In case variable corresponding to the virtual address was found */
128
             /* In case variable corresponding to the virtual address was found */
142
             if (ReadStatus != 0x1) {
129
             if (ReadStatus != 0x1) {
143
               /* Transfer the variable to the Page0 */
130
               /* Transfer the variable to the Page0 */
144
-              EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
131
+              uint16_t EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
145
               /* If program operation was failed, a Flash error code is returned */
132
               /* If program operation was failed, a Flash error code is returned */
146
               if (EepromStatus != HAL_OK) return EepromStatus;
133
               if (EepromStatus != HAL_OK) return EepromStatus;
147
             }
134
             }
148
           }
135
           }
149
         }
136
         }
150
         /* Mark Page0 as valid */
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
         /* If program operation was failed, a Flash error code is returned */
139
         /* If program operation was failed, a Flash error code is returned */
153
         if (FlashStatus != HAL_OK) return FlashStatus;
140
         if (FlashStatus != HAL_OK) return FlashStatus;
154
         pEraseInit.Sector = PAGE1_ID;
141
         pEraseInit.Sector = PAGE1_ID;
156
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
143
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
157
         /* Erase Page1 */
144
         /* Erase Page1 */
158
         if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
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
       else if (PageStatus1 == ERASED) { /* Page0 receive, Page1 erased */
150
       else if (PageStatus1 == ERASED) { /* Page0 receive, Page1 erased */
167
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
153
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
168
         /* Erase Page1 */
154
         /* Erase Page1 */
169
         if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
155
         if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
170
-          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
156
+          HAL_StatusTypeDef fStat = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
171
           /* If erase operation was failed, a Flash error code is returned */
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
         /* Mark Page0 as valid */
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
       else { /* Invalid state -> format eeprom */
164
       else { /* Invalid state -> format eeprom */
180
         /* Erase both Page0 and Page1 and set Page0 as valid page */
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
       break;
169
       break;
186
 
170
 
204
       }
188
       }
205
       else { /* Page0 valid, Page1 receive */
189
       else { /* Page0 valid, Page1 receive */
206
         /* Transfer data from Page0 to Page1 */
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
           if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
193
           if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
209
             x = VarIdx;
194
             x = VarIdx;
210
 
195
 
211
           if (VarIdx != x) {
196
           if (VarIdx != x) {
212
             /* Read the last variables' updates */
197
             /* Read the last variables' updates */
213
-            ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
198
+            uint16_t ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
214
             /* In case variable corresponding to the virtual address was found */
199
             /* In case variable corresponding to the virtual address was found */
215
             if (ReadStatus != 0x1) {
200
             if (ReadStatus != 0x1) {
216
               /* Transfer the variable to the Page1 */
201
               /* Transfer the variable to the Page1 */
217
-              EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
202
+              uint16_t EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
218
               /* If program operation was failed, a Flash error code is returned */
203
               /* If program operation was failed, a Flash error code is returned */
219
               if (EepromStatus != HAL_OK) return EepromStatus;
204
               if (EepromStatus != HAL_OK) return EepromStatus;
220
             }
205
             }
229
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
214
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
230
         /* Erase Page0 */
215
         /* Erase Page0 */
231
         if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
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
       break;
221
       break;
238
 
222
 
239
     default:  /* Any other state -> format eeprom */
223
     default:  /* Any other state -> format eeprom */
240
       /* Erase both Page0 and Page1 and set Page0 as valid page */
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
   return HAL_OK;
229
   return HAL_OK;
259
  */
241
  */
260
 uint16_t EE_VerifyPageFullyErased(uint32_t Address) {
242
 uint16_t EE_VerifyPageFullyErased(uint32_t Address) {
261
   uint32_t ReadStatus = 1;
243
   uint32_t ReadStatus = 1;
262
-  uint16_t AddressValue = 0x5555;
263
   /* Check each active page address starting from end */
244
   /* Check each active page address starting from end */
264
   while (Address <= PAGE0_END_ADDRESS) {
245
   while (Address <= PAGE0_END_ADDRESS) {
265
     /* Get the current location content to be compared with virtual address */
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
     /* Compare the read address with the virtual address */
248
     /* Compare the read address with the virtual address */
268
     if (AddressValue != ERASED) {
249
     if (AddressValue != ERASED) {
269
       /* In case variable value is read, reset ReadStatus flag */
250
       /* In case variable value is read, reset ReadStatus flag */
288
  *           - NO_VALID_PAGE: if no valid page was found.
269
  *           - NO_VALID_PAGE: if no valid page was found.
289
  */
270
  */
290
 uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) {
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
   /* Get active Page for read operation */
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
   /* Check if there is no valid page */
277
   /* Check if there is no valid page */
299
   if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
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
   /* Check each active page address starting from end */
284
   /* Check each active page address starting from end */
308
-  while (Address > (PageStartAddress + 2)) {
285
+  while (Address > PageStartAddress + 2) {
309
     /* Get the current location content to be compared with virtual address */
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
     /* Compare the read address with the virtual address */
289
     /* Compare the read address with the virtual address */
313
     if (AddressValue == VirtAddress) {
290
     if (AddressValue == VirtAddress) {
353
  *         EEPROM formating
330
  *         EEPROM formating
354
  */
331
  */
355
 static HAL_StatusTypeDef EE_Format(void) {
332
 static HAL_StatusTypeDef EE_Format(void) {
356
-  HAL_StatusTypeDef FlashStatus = HAL_OK;
357
-  uint32_t SectorError = 0;
358
   FLASH_EraseInitTypeDef pEraseInit;
333
   FLASH_EraseInitTypeDef pEraseInit;
359
-
360
   pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
334
   pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
361
   pEraseInit.Sector = PAGE0_ID;
335
   pEraseInit.Sector = PAGE0_ID;
362
   pEraseInit.NbSectors = 1;
336
   pEraseInit.NbSectors = 1;
363
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
337
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
338
+
339
+  HAL_StatusTypeDef FlashStatus; // = HAL_OK
340
+
364
   /* Erase Page0 */
341
   /* Erase Page0 */
365
   if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
342
   if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
343
+    uint32_t SectorError;
366
     FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
344
     FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
367
     /* If erase operation was failed, a Flash error code is returned */
345
     /* If erase operation was failed, a Flash error code is returned */
368
     if (FlashStatus != HAL_OK) return FlashStatus;
346
     if (FlashStatus != HAL_OK) return FlashStatus;
375
   pEraseInit.Sector = PAGE1_ID;
353
   pEraseInit.Sector = PAGE1_ID;
376
   /* Erase Page1 */
354
   /* Erase Page1 */
377
   if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
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
   return HAL_OK;
361
   return HAL_OK;
393
  *   of no valid page was found
371
  *   of no valid page was found
394
  */
372
  */
395
 static uint16_t EE_FindValidPage(uint8_t Operation) {
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
   /* Write or read operation */
378
   /* Write or read operation */
405
   switch (Operation) {
379
   switch (Operation) {
406
     case WRITE_IN_VALID_PAGE:   /* ---- Write operation ---- */
380
     case WRITE_IN_VALID_PAGE:   /* ---- Write operation ---- */
407
       if (PageStatus1 == VALID_PAGE) {
381
       if (PageStatus1 == VALID_PAGE) {
408
         /* Page0 receiving data */
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
       else if (PageStatus0 == VALID_PAGE) {
385
       else if (PageStatus0 == VALID_PAGE) {
413
         /* Page1 receiving data */
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
       else
389
       else
418
         return NO_VALID_PAGE;   /* No valid Page */
390
         return NO_VALID_PAGE;   /* No valid Page */
441
  *           - Flash error code: on write Flash error
413
  *           - Flash error code: on write Flash error
442
  */
414
  */
443
 static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) {
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
   /* Get valid Page for write operation */
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
   /* Check if there is no valid page */
419
   /* Check if there is no valid page */
452
   if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
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
   /* Check each active page address starting from begining */
426
   /* Check each active page address starting from begining */
461
   while (Address < PageEndAddress) {
427
   while (Address < PageEndAddress) {
462
     /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
428
     /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
463
     if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF) {
429
     if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF) {
464
       /* Set variable data */
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
       /* If program operation was failed, a Flash error code is returned */
432
       /* If program operation was failed, a Flash error code is returned */
467
       if (FlashStatus != HAL_OK) return FlashStatus;
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
     else /* Next address location */
437
     else /* Next address location */
474
       Address += 4;
438
       Address += 4;
490
  *           - Flash error code: on write Flash error
454
  *           - Flash error code: on write Flash error
491
  */
455
  */
492
 static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) {
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
   /* Get active Page for read operation */
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
   if (ValidPage == PAGE1) {     /* Page1 valid */
462
   if (ValidPage == PAGE1) {     /* Page1 valid */
505
     /* New page address where variable will be moved to */
463
     /* New page address where variable will be moved to */
517
     return NO_VALID_PAGE;       /* No valid Page */
475
     return NO_VALID_PAGE;       /* No valid Page */
518
 
476
 
519
   /* Set the new Page status to RECEIVE_DATA status */
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
   /* If program operation was failed, a Flash error code is returned */
479
   /* If program operation was failed, a Flash error code is returned */
522
   if (FlashStatus != HAL_OK) return FlashStatus;
480
   if (FlashStatus != HAL_OK) return FlashStatus;
523
 
481
 
524
   /* Write the variable passed as parameter in the new active page */
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
   /* If program operation was failed, a Flash error code is returned */
484
   /* If program operation was failed, a Flash error code is returned */
527
   if (EepromStatus != HAL_OK) return EepromStatus;
485
   if (EepromStatus != HAL_OK) return EepromStatus;
528
 
486
 
529
   /* Transfer process: transfer variables from old to the new active page */
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
     if (VirtAddVarTab[VarIdx] != VirtAddress) { /* Check each variable except the one passed as parameter */
489
     if (VirtAddVarTab[VarIdx] != VirtAddress) { /* Check each variable except the one passed as parameter */
532
       /* Read the other last variable updates */
490
       /* Read the other last variable updates */
533
-      ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
491
+      uint16_t ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
534
       /* In case variable corresponding to the virtual address was found */
492
       /* In case variable corresponding to the virtual address was found */
535
       if (ReadStatus != 0x1) {
493
       if (ReadStatus != 0x1) {
536
         /* Transfer the variable to the new active page */
494
         /* Transfer the variable to the new active page */
541
     }
499
     }
542
   }
500
   }
543
 
501
 
502
+  FLASH_EraseInitTypeDef pEraseInit;
544
   pEraseInit.TypeErase = TYPEERASE_SECTORS;
503
   pEraseInit.TypeErase = TYPEERASE_SECTORS;
545
   pEraseInit.Sector = OldPageId;
504
   pEraseInit.Sector = OldPageId;
546
   pEraseInit.NbSectors = 1;
505
   pEraseInit.NbSectors = 1;
547
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
506
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
548
 
507
 
549
   /* Erase the old Page: Set old Page status to ERASED status */
508
   /* Erase the old Page: Set old Page status to ERASED status */
509
+  uint32_t SectorError;
550
   FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
510
   FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
551
   /* If erase operation was failed, a Flash error code is returned */
511
   /* If erase operation was failed, a Flash error code is returned */
552
   if (FlashStatus != HAL_OK) return FlashStatus;
512
   if (FlashStatus != HAL_OK) return FlashStatus;
553
 
513
 
554
   /* Set new Page status to VALID_PAGE status */
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
  * @}

+ 24
- 26
Marlin/src/HAL/HAL_STM32F4/EEPROM_Emul/eeprom_emul.h View File

53
 
53
 
54
 /* Exported constants --------------------------------------------------------*/
54
 /* Exported constants --------------------------------------------------------*/
55
 /* EEPROM emulation firmware error codes */
55
 /* EEPROM emulation firmware error codes */
56
-#define EE_OK      (uint32_t)HAL_OK
57
-#define EE_ERROR   (uint32_t)HAL_ERROR
58
-#define EE_BUSY    (uint32_t)HAL_BUSY
59
-#define EE_TIMEOUT (uint32_t)HAL_TIMEOUT
56
+#define EE_OK      uint32_t(HAL_OK)
57
+#define EE_ERROR   uint32_t(HAL_ERROR)
58
+#define EE_BUSY    uint32_t(HAL_BUSY)
59
+#define EE_TIMEOUT uint32_t(HAL_TIMEOUT)
60
 
60
 
61
 /* Define the size of the sectors to be used */
61
 /* Define the size of the sectors to be used */
62
-#define PAGE_SIZE               (uint32_t)0x4000  /* Page size = 16KByte */
62
+#define PAGE_SIZE             uint32_t(0x4000)  /* Page size = 16KByte */
63
 
63
 
64
 /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
64
 /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
65
    be done by word  */
65
    be done by word  */
66
-#define VOLTAGE_RANGE           (uint8_t)VOLTAGE_RANGE_3
66
+#define VOLTAGE_RANGE         uint8_t(VOLTAGE_RANGE_3)
67
 
67
 
68
 /* EEPROM start address in Flash */
68
 /* EEPROM start address in Flash */
69
-#define EEPROM_START_ADDRESS  ((uint32_t)0x08078000) /* EEPROM emulation start address:
70
-                                                  after 480KByte of used Flash memory */
69
+#define EEPROM_START_ADDRESS  uint32_t(0x08078000) /* EEPROM emulation start address:
70
+                                                      after 480KByte of used Flash memory */
71
 
71
 
72
 /* Pages 0 and 1 base and end addresses */
72
 /* Pages 0 and 1 base and end addresses */
73
-#define PAGE0_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + 0x0000))
74
-#define PAGE0_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + PAGE_SIZE - 1))
75
-#define PAGE0_ID               FLASH_SECTOR_1
73
+#define PAGE0_BASE_ADDRESS    uint32_t(EEPROM_START_ADDRESS + 0x0000)
74
+#define PAGE0_END_ADDRESS     uint32_t(EEPROM_START_ADDRESS + PAGE_SIZE - 1)
75
+#define PAGE0_ID              FLASH_SECTOR_1
76
 
76
 
77
-#define PAGE1_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + 0x4000))
78
-#define PAGE1_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + 2 * (PAGE_SIZE) - 1))
79
-#define PAGE1_ID               FLASH_SECTOR_2
77
+#define PAGE1_BASE_ADDRESS    uint32_t(EEPROM_START_ADDRESS + 0x4000)
78
+#define PAGE1_END_ADDRESS     uint32_t(EEPROM_START_ADDRESS + 2 * (PAGE_SIZE) - 1)
79
+#define PAGE1_ID              FLASH_SECTOR_2
80
 
80
 
81
 /* Used Flash pages for EEPROM emulation */
81
 /* Used Flash pages for EEPROM emulation */
82
-#define PAGE0                 ((uint16_t)0x0000)
83
-#define PAGE1                 ((uint16_t)0x0001) /* Page nb between PAGE0_BASE_ADDRESS & PAGE1_BASE_ADDRESS*/
82
+#define PAGE0                 uint16_t(0x0000)
83
+#define PAGE1                 uint16_t(0x0001) /* Page nb between PAGE0_BASE_ADDRESS & PAGE1_BASE_ADDRESS*/
84
 
84
 
85
 /* No valid page define */
85
 /* No valid page define */
86
-#define NO_VALID_PAGE         ((uint16_t)0x00AB)
86
+#define NO_VALID_PAGE         uint16_t(0x00AB)
87
 
87
 
88
 /* Page status definitions */
88
 /* Page status definitions */
89
-#define ERASED                ((uint16_t)0xFFFF)     /* Page is empty */
90
-#define RECEIVE_DATA          ((uint16_t)0xEEEE)     /* Page is marked to receive data */
91
-#define VALID_PAGE            ((uint16_t)0x0000)     /* Page containing valid data */
89
+#define ERASED                uint16_t(0xFFFF)     /* Page is empty */
90
+#define RECEIVE_DATA          uint16_t(0xEEEE)     /* Page is marked to receive data */
91
+#define VALID_PAGE            uint16_t(0x0000)     /* Page containing valid data */
92
 
92
 
93
 /* Valid pages in read and write defines */
93
 /* Valid pages in read and write defines */
94
-#define READ_FROM_VALID_PAGE  ((uint8_t)0x00)
95
-#define WRITE_IN_VALID_PAGE   ((uint8_t)0x01)
94
+#define READ_FROM_VALID_PAGE  uint8_t(0x00)
95
+#define WRITE_IN_VALID_PAGE   uint8_t(0x01)
96
 
96
 
97
 /* Page full define */
97
 /* Page full define */
98
-#define PAGE_FULL             ((uint8_t)0x80)
98
+#define PAGE_FULL             uint8_t(0x80)
99
 
99
 
100
 /* Variables' number */
100
 /* Variables' number */
101
-#define NB_OF_VAR             ((uint16_t)4096)
101
+#define NB_OF_VAR             uint16_t(4096)
102
 
102
 
103
-/* Exported types ------------------------------------------------------------*/
104
-/* Exported macro ------------------------------------------------------------*/
105
 /* Exported functions ------------------------------------------------------- */
103
 /* Exported functions ------------------------------------------------------- */
106
 uint16_t EE_Initialize(void);
104
 uint16_t EE_Initialize(void);
107
 uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);
105
 uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);

+ 13
- 16
Marlin/src/HAL/HAL_STM32F4/EmulatedEeprom.cpp View File

17
  *
17
  *
18
  */
18
  */
19
 
19
 
20
-#if defined(STM32GENERIC) && (defined(STM32F4))
21
-
22
 /**
20
 /**
23
- * Description: functions for I2C connected external EEPROM.
21
+ * Description: Functions for a Flash emulated EEPROM
24
  * Not platform dependent.
22
  * Not platform dependent.
25
  */
23
  */
26
 
24
 
25
+#if defined(STM32GENERIC) && defined(STM32F4)
26
+
27
 #include "../../inc/MarlinConfig.h"
27
 #include "../../inc/MarlinConfig.h"
28
 
28
 
29
 #if ENABLED(EEPROM_SETTINGS) && NONE(I2C_EEPROM, SPI_EEPROM)
29
 #if ENABLED(EEPROM_SETTINGS) && NONE(I2C_EEPROM, SPI_EEPROM)
69
 }
69
 }
70
 
70
 
71
 void eeprom_write_byte(uint8_t *pos, unsigned char value) {
71
 void eeprom_write_byte(uint8_t *pos, unsigned char value) {
72
-  uint16_t eeprom_address = (unsigned) pos;
72
+  uint16_t eeprom_address = unsigned(pos);
73
 
73
 
74
   eeprom_init();
74
   eeprom_init();
75
 
75
 
76
   HAL_FLASH_Unlock();
76
   HAL_FLASH_Unlock();
77
   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
77
   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
78
 
78
 
79
-  if (EE_WriteVariable(eeprom_address, (uint16_t) value) != EE_OK)
80
-      for (;;) HAL_Delay(1); // Spin forever until watchdog reset
79
+  if (EE_WriteVariable(eeprom_address, uint16_t(value)) != EE_OK)
80
+    for (;;) HAL_Delay(1); // Spin forever until watchdog reset
81
 
81
 
82
   HAL_FLASH_Lock();
82
   HAL_FLASH_Lock();
83
 }
83
 }
84
 
84
 
85
 uint8_t eeprom_read_byte(uint8_t *pos) {
85
 uint8_t eeprom_read_byte(uint8_t *pos) {
86
-  uint16_t data = 0xFF;
87
-  uint16_t eeprom_address = (unsigned)pos;
88
-
89
   eeprom_init();
86
   eeprom_init();
90
 
87
 
91
-  if (EE_ReadVariable(eeprom_address, &data) != EE_OK) {
92
-    return (unsigned char)data;
93
-  }
94
-  return (unsigned char)data;
88
+  uint16_t data = 0xFF;
89
+  uint16_t eeprom_address = unsigned(pos);
90
+  (void)EE_ReadVariable(eeprom_address, &data); // Data unchanged on error
91
+
92
+  return uint8_t(data);
95
 }
93
 }
96
 
94
 
97
 void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
95
 void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
98
-  uint16_t data = 0xFF;
99
-  uint16_t eeprom_address = (unsigned) __src;
100
-
101
   eeprom_init();
96
   eeprom_init();
102
 
97
 
98
+  uint16_t data = 0xFF;
99
+  uint16_t eeprom_address = (unsigned)__src;
103
   for (uint8_t c = 0; c < __n; c++) {
100
   for (uint8_t c = 0; c < __n; c++) {
104
     EE_ReadVariable(eeprom_address+c, &data);
101
     EE_ReadVariable(eeprom_address+c, &data);
105
     *((uint8_t*)__dst + c) = data;
102
     *((uint8_t*)__dst + c) = data;

+ 75
- 119
Marlin/src/HAL/HAL_STM32F7/EEPROM_Emul/eeprom_emul.cpp View File

8
   ******************************************************************************
8
   ******************************************************************************
9
   * @attention
9
   * @attention
10
   *
10
   *
11
-  * <h2><center>&copy; 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
   * Redistribution and use in source and binary forms, with or without
14
   * Redistribution and use in source and binary forms, with or without
15
   * modification, are permitted, provided that the following conditions are met:
15
   * modification, are permitted, provided that the following conditions are met:
52
 /* Includes ------------------------------------------------------------------*/
52
 /* Includes ------------------------------------------------------------------*/
53
 #include "eeprom_emul.h"
53
 #include "eeprom_emul.h"
54
 
54
 
55
-/* Private typedef -----------------------------------------------------------*/
56
-/* Private define ------------------------------------------------------------*/
57
-/* Private macro -------------------------------------------------------------*/
58
 /* Private variables ---------------------------------------------------------*/
55
 /* Private variables ---------------------------------------------------------*/
59
 
56
 
60
 /* Global variable used to store variable value in read sequence */
57
 /* Global variable used to store variable value in read sequence */
79
   *         - FLASH_COMPLETE: on success
76
   *         - FLASH_COMPLETE: on success
80
   */
77
   */
81
 uint16_t EE_Initialize(void) {
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
   pEraseInit.TypeErase = TYPEERASE_SECTORS;
84
   pEraseInit.TypeErase = TYPEERASE_SECTORS;
97
   pEraseInit.Sector = PAGE0_ID;
85
   pEraseInit.Sector = PAGE0_ID;
98
   pEraseInit.NbSectors = 1;
86
   pEraseInit.NbSectors = 1;
99
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
87
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
100
 
88
 
101
   /* Check for invalid header states and repair if necessary */
89
   /* Check for invalid header states and repair if necessary */
90
+  uint32_t SectorError;
102
   switch (PageStatus0) {
91
   switch (PageStatus0) {
103
     case ERASED:
92
     case ERASED:
104
       if (PageStatus1 == VALID_PAGE) { /* Page0 erased, Page1 valid */
93
       if (PageStatus1 == VALID_PAGE) { /* Page0 erased, Page1 valid */
105
-          /* Erase Page0 */
94
+        /* Erase Page0 */
106
         if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
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
       else if (PageStatus1 == RECEIVE_DATA) { /* Page0 erased, Page1 receive */
100
       else if (PageStatus1 == RECEIVE_DATA) { /* Page0 erased, Page1 receive */
113
         /* Erase Page0 */
101
         /* Erase Page0 */
114
         if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
102
         if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
115
-          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
103
+          HAL_StatusTypeDef fStat = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
116
           /* If erase operation was failed, a Flash error code is returned */
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
         /* Mark Page1 as valid */
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
       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 */
125
         /* Erase both Page0 and Page1 and set Page0 as valid page */
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
       break;
116
       break;
131
 
117
 
132
     case RECEIVE_DATA:
118
     case RECEIVE_DATA:
133
       if (PageStatus1 == VALID_PAGE) { /* Page0 receive, Page1 valid */
119
       if (PageStatus1 == VALID_PAGE) { /* Page0 receive, Page1 valid */
134
         /* Transfer data from Page1 to Page0 */
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
           if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
123
           if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
137
             x = VarIdx;
124
             x = VarIdx;
138
           if (VarIdx != x) {
125
           if (VarIdx != x) {
139
             /* Read the last variables' updates */
126
             /* Read the last variables' updates */
140
-            ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
127
+            uint16_t ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
141
             /* In case variable corresponding to the virtual address was found */
128
             /* In case variable corresponding to the virtual address was found */
142
             if (ReadStatus != 0x1) {
129
             if (ReadStatus != 0x1) {
143
               /* Transfer the variable to the Page0 */
130
               /* Transfer the variable to the Page0 */
144
-              EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
131
+              uint16_t EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
145
               /* If program operation was failed, a Flash error code is returned */
132
               /* If program operation was failed, a Flash error code is returned */
146
               if (EepromStatus != HAL_OK) return EepromStatus;
133
               if (EepromStatus != HAL_OK) return EepromStatus;
147
             }
134
             }
148
           }
135
           }
149
         }
136
         }
150
         /* Mark Page0 as valid */
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
         /* If program operation was failed, a Flash error code is returned */
139
         /* If program operation was failed, a Flash error code is returned */
153
         if (FlashStatus != HAL_OK) return FlashStatus;
140
         if (FlashStatus != HAL_OK) return FlashStatus;
154
         pEraseInit.Sector = PAGE1_ID;
141
         pEraseInit.Sector = PAGE1_ID;
156
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
143
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
157
         /* Erase Page1 */
144
         /* Erase Page1 */
158
         if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
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
       else if (PageStatus1 == ERASED) { /* Page0 receive, Page1 erased */
150
       else if (PageStatus1 == ERASED) { /* Page0 receive, Page1 erased */
167
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
153
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
168
         /* Erase Page1 */
154
         /* Erase Page1 */
169
         if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
155
         if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
170
-          FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
156
+          HAL_StatusTypeDef fStat = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
171
           /* If erase operation was failed, a Flash error code is returned */
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
         /* Mark Page0 as valid */
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
       else { /* Invalid state -> format eeprom */
164
       else { /* Invalid state -> format eeprom */
180
         /* Erase both Page0 and Page1 and set Page0 as valid page */
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
       break;
169
       break;
186
 
170
 
204
       }
188
       }
205
       else { /* Page0 valid, Page1 receive */
189
       else { /* Page0 valid, Page1 receive */
206
         /* Transfer data from Page0 to Page1 */
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
           if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
193
           if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
209
             x = VarIdx;
194
             x = VarIdx;
210
 
195
 
211
           if (VarIdx != x) {
196
           if (VarIdx != x) {
212
             /* Read the last variables' updates */
197
             /* Read the last variables' updates */
213
-            ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
198
+            uint16_t ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
214
             /* In case variable corresponding to the virtual address was found */
199
             /* In case variable corresponding to the virtual address was found */
215
             if (ReadStatus != 0x1) {
200
             if (ReadStatus != 0x1) {
216
               /* Transfer the variable to the Page1 */
201
               /* Transfer the variable to the Page1 */
217
-              EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
202
+              uint16_t EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
218
               /* If program operation was failed, a Flash error code is returned */
203
               /* If program operation was failed, a Flash error code is returned */
219
               if (EepromStatus != HAL_OK) return EepromStatus;
204
               if (EepromStatus != HAL_OK) return EepromStatus;
220
             }
205
             }
229
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
214
         pEraseInit.VoltageRange = VOLTAGE_RANGE;
230
         /* Erase Page0 */
215
         /* Erase Page0 */
231
         if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
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
       break;
221
       break;
238
 
222
 
239
     default:  /* Any other state -> format eeprom */
223
     default:  /* Any other state -> format eeprom */
240
       /* Erase both Page0 and Page1 and set Page0 as valid page */
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
   return HAL_OK;
229
   return HAL_OK;
259
  */
241
  */
260
 uint16_t EE_VerifyPageFullyErased(uint32_t Address) {
242
 uint16_t EE_VerifyPageFullyErased(uint32_t Address) {
261
   uint32_t ReadStatus = 1;
243
   uint32_t ReadStatus = 1;
262
-  uint16_t AddressValue = 0x5555;
263
   /* Check each active page address starting from end */
244
   /* Check each active page address starting from end */
264
   while (Address <= PAGE0_END_ADDRESS) {
245
   while (Address <= PAGE0_END_ADDRESS) {
265
     /* Get the current location content to be compared with virtual address */
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
     /* Compare the read address with the virtual address */
248
     /* Compare the read address with the virtual address */
268
     if (AddressValue != ERASED) {
249
     if (AddressValue != ERASED) {
269
       /* In case variable value is read, reset ReadStatus flag */
250
       /* In case variable value is read, reset ReadStatus flag */
288
  *           - NO_VALID_PAGE: if no valid page was found.
269
  *           - NO_VALID_PAGE: if no valid page was found.
289
  */
270
  */
290
 uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) {
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
   /* Get active Page for read operation */
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
   /* Check if there is no valid page */
277
   /* Check if there is no valid page */
299
   if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
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
   /* Check each active page address starting from end */
284
   /* Check each active page address starting from end */
308
-  while (Address > (PageStartAddress + 2)) {
285
+  while (Address > PageStartAddress + 2) {
309
     /* Get the current location content to be compared with virtual address */
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
     /* Compare the read address with the virtual address */
289
     /* Compare the read address with the virtual address */
313
     if (AddressValue == VirtAddress) {
290
     if (AddressValue == VirtAddress) {
353
  *         EEPROM formating
330
  *         EEPROM formating
354
  */
331
  */
355
 static HAL_StatusTypeDef EE_Format(void) {
332
 static HAL_StatusTypeDef EE_Format(void) {
356
-  HAL_StatusTypeDef FlashStatus = HAL_OK;
357
-  uint32_t SectorError = 0;
358
   FLASH_EraseInitTypeDef pEraseInit;
333
   FLASH_EraseInitTypeDef pEraseInit;
359
-
360
   pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
334
   pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
361
   pEraseInit.Sector = PAGE0_ID;
335
   pEraseInit.Sector = PAGE0_ID;
362
   pEraseInit.NbSectors = 1;
336
   pEraseInit.NbSectors = 1;
363
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
337
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
338
+
339
+  HAL_StatusTypeDef FlashStatus; // = HAL_OK
340
+
364
   /* Erase Page0 */
341
   /* Erase Page0 */
365
   if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
342
   if (!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) {
343
+    uint32_t SectorError;
366
     FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
344
     FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
367
     /* If erase operation was failed, a Flash error code is returned */
345
     /* If erase operation was failed, a Flash error code is returned */
368
     if (FlashStatus != HAL_OK) return FlashStatus;
346
     if (FlashStatus != HAL_OK) return FlashStatus;
375
   pEraseInit.Sector = PAGE1_ID;
353
   pEraseInit.Sector = PAGE1_ID;
376
   /* Erase Page1 */
354
   /* Erase Page1 */
377
   if (!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) {
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
   return HAL_OK;
361
   return HAL_OK;
393
  *   of no valid page was found
371
  *   of no valid page was found
394
  */
372
  */
395
 static uint16_t EE_FindValidPage(uint8_t Operation) {
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
   /* Write or read operation */
378
   /* Write or read operation */
405
   switch (Operation) {
379
   switch (Operation) {
406
     case WRITE_IN_VALID_PAGE:   /* ---- Write operation ---- */
380
     case WRITE_IN_VALID_PAGE:   /* ---- Write operation ---- */
407
       if (PageStatus1 == VALID_PAGE) {
381
       if (PageStatus1 == VALID_PAGE) {
408
         /* Page0 receiving data */
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
       else if (PageStatus0 == VALID_PAGE) {
385
       else if (PageStatus0 == VALID_PAGE) {
413
         /* Page1 receiving data */
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
       else
389
       else
418
         return NO_VALID_PAGE;   /* No valid Page */
390
         return NO_VALID_PAGE;   /* No valid Page */
441
  *           - Flash error code: on write Flash error
413
  *           - Flash error code: on write Flash error
442
  */
414
  */
443
 static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) {
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
   /* Get valid Page for write operation */
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
   /* Check if there is no valid page */
419
   /* Check if there is no valid page */
452
   if (ValidPage == NO_VALID_PAGE) return NO_VALID_PAGE;
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
   /* Check each active page address starting from begining */
426
   /* Check each active page address starting from begining */
461
   while (Address < PageEndAddress) {
427
   while (Address < PageEndAddress) {
462
     /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
428
     /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
463
     if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF) {
429
     if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF) {
464
       /* Set variable data */
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
       /* If program operation was failed, a Flash error code is returned */
432
       /* If program operation was failed, a Flash error code is returned */
467
       if (FlashStatus != HAL_OK) return FlashStatus;
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
     else /* Next address location */
437
     else /* Next address location */
474
       Address += 4;
438
       Address += 4;
490
  *           - Flash error code: on write Flash error
454
  *           - Flash error code: on write Flash error
491
  */
455
  */
492
 static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) {
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
   /* Get active Page for read operation */
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
   if (ValidPage == PAGE1) {     /* Page1 valid */
462
   if (ValidPage == PAGE1) {     /* Page1 valid */
505
     /* New page address where variable will be moved to */
463
     /* New page address where variable will be moved to */
517
     return NO_VALID_PAGE;       /* No valid Page */
475
     return NO_VALID_PAGE;       /* No valid Page */
518
 
476
 
519
   /* Set the new Page status to RECEIVE_DATA status */
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
   /* If program operation was failed, a Flash error code is returned */
479
   /* If program operation was failed, a Flash error code is returned */
522
   if (FlashStatus != HAL_OK) return FlashStatus;
480
   if (FlashStatus != HAL_OK) return FlashStatus;
523
 
481
 
524
   /* Write the variable passed as parameter in the new active page */
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
   /* If program operation was failed, a Flash error code is returned */
484
   /* If program operation was failed, a Flash error code is returned */
527
   if (EepromStatus != HAL_OK) return EepromStatus;
485
   if (EepromStatus != HAL_OK) return EepromStatus;
528
 
486
 
529
   /* Transfer process: transfer variables from old to the new active page */
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
     if (VirtAddVarTab[VarIdx] != VirtAddress) { /* Check each variable except the one passed as parameter */
489
     if (VirtAddVarTab[VarIdx] != VirtAddress) { /* Check each variable except the one passed as parameter */
532
       /* Read the other last variable updates */
490
       /* Read the other last variable updates */
533
-      ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
491
+      uint16_t ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
534
       /* In case variable corresponding to the virtual address was found */
492
       /* In case variable corresponding to the virtual address was found */
535
       if (ReadStatus != 0x1) {
493
       if (ReadStatus != 0x1) {
536
         /* Transfer the variable to the new active page */
494
         /* Transfer the variable to the new active page */
541
     }
499
     }
542
   }
500
   }
543
 
501
 
502
+  FLASH_EraseInitTypeDef pEraseInit;
544
   pEraseInit.TypeErase = TYPEERASE_SECTORS;
503
   pEraseInit.TypeErase = TYPEERASE_SECTORS;
545
   pEraseInit.Sector = OldPageId;
504
   pEraseInit.Sector = OldPageId;
546
   pEraseInit.NbSectors = 1;
505
   pEraseInit.NbSectors = 1;
547
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
506
   pEraseInit.VoltageRange = VOLTAGE_RANGE;
548
 
507
 
549
   /* Erase the old Page: Set old Page status to ERASED status */
508
   /* Erase the old Page: Set old Page status to ERASED status */
509
+  uint32_t SectorError;
550
   FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
510
   FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
551
   /* If erase operation was failed, a Flash error code is returned */
511
   /* If erase operation was failed, a Flash error code is returned */
552
   if (FlashStatus != HAL_OK) return FlashStatus;
512
   if (FlashStatus != HAL_OK) return FlashStatus;
553
 
513
 
554
   /* Set new Page status to VALID_PAGE status */
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 // STM32F7
519
 #endif // STM32F7

+ 25
- 27
Marlin/src/HAL/HAL_STM32F7/EEPROM_Emul/eeprom_emul.h View File

53
 
53
 
54
 /* Exported constants --------------------------------------------------------*/
54
 /* Exported constants --------------------------------------------------------*/
55
 /* EEPROM emulation firmware error codes */
55
 /* EEPROM emulation firmware error codes */
56
-#define EE_OK      (uint32_t)HAL_OK
57
-#define EE_ERROR   (uint32_t)HAL_ERROR
58
-#define EE_BUSY    (uint32_t)HAL_BUSY
59
-#define EE_TIMEOUT (uint32_t)HAL_TIMEOUT
56
+#define EE_OK      uint32_t(HAL_OK)
57
+#define EE_ERROR   uint32_t(HAL_ERROR)
58
+#define EE_BUSY    uint32_t(HAL_BUSY)
59
+#define EE_TIMEOUT uint32_t(HAL_TIMEOUT)
60
 
60
 
61
 /* Define the size of the sectors to be used */
61
 /* Define the size of the sectors to be used */
62
-#define PAGE_SIZE               (uint32_t)0x4000  /* Page size = 16KByte */
62
+#define PAGE_SIZE             uint32_t(0x4000)  /* Page size = 16KByte */
63
 
63
 
64
 /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
64
 /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
65
    be done by word  */
65
    be done by word  */
66
-#define VOLTAGE_RANGE           (uint8_t)VOLTAGE_RANGE_3
66
+#define VOLTAGE_RANGE         uint8_t(VOLTAGE_RANGE_3)
67
 
67
 
68
 /* EEPROM start address in Flash */
68
 /* EEPROM start address in Flash */
69
-#define EEPROM_START_ADDRESS  ((uint32_t)0x08100000) /* EEPROM emulation start address:
70
-                                                  from sector2 : after 16KByte of used
71
-                                                  Flash memory */
69
+#define EEPROM_START_ADDRESS  uint32_t(0x08100000) /* EEPROM emulation start address:
70
+                                                      from sector2 : after 16KByte of used
71
+                                                      Flash memory */
72
 
72
 
73
 /* Pages 0 and 1 base and end addresses */
73
 /* Pages 0 and 1 base and end addresses */
74
-#define PAGE0_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + 0x0000))
75
-#define PAGE0_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)))
76
-#define PAGE0_ID               FLASH_SECTOR_1
74
+#define PAGE0_BASE_ADDRESS    uint32_t(EEPROM_START_ADDRESS + 0x0000)
75
+#define PAGE0_END_ADDRESS     uint32_t(EEPROM_START_ADDRESS + PAGE_SIZE - 1)
76
+#define PAGE0_ID              FLASH_SECTOR_1
77
 
77
 
78
-#define PAGE1_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + 0x4000))
79
-#define PAGE1_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1)))
80
-#define PAGE1_ID               FLASH_SECTOR_2
78
+#define PAGE1_BASE_ADDRESS    uint32_t(EEPROM_START_ADDRESS + 0x4000)
79
+#define PAGE1_END_ADDRESS     uint32_t(EEPROM_START_ADDRESS + 2 * (PAGE_SIZE) - 1)
80
+#define PAGE1_ID              FLASH_SECTOR_2
81
 
81
 
82
 /* Used Flash pages for EEPROM emulation */
82
 /* Used Flash pages for EEPROM emulation */
83
-#define PAGE0                 ((uint16_t)0x0000)
84
-#define PAGE1                 ((uint16_t)0x0001) /* Page nb between PAGE0_BASE_ADDRESS & PAGE1_BASE_ADDRESS*/
83
+#define PAGE0                 uint16_t(0x0000)
84
+#define PAGE1                 uint16_t(0x0001) /* Page nb between PAGE0_BASE_ADDRESS & PAGE1_BASE_ADDRESS*/
85
 
85
 
86
 /* No valid page define */
86
 /* No valid page define */
87
-#define NO_VALID_PAGE         ((uint16_t)0x00AB)
87
+#define NO_VALID_PAGE         uint16_t(0x00AB)
88
 
88
 
89
 /* Page status definitions */
89
 /* Page status definitions */
90
-#define ERASED                ((uint16_t)0xFFFF)     /* Page is empty */
91
-#define RECEIVE_DATA          ((uint16_t)0xEEEE)     /* Page is marked to receive data */
92
-#define VALID_PAGE            ((uint16_t)0x0000)     /* Page containing valid data */
90
+#define ERASED                uint16_t(0xFFFF)     /* Page is empty */
91
+#define RECEIVE_DATA          uint16_t(0xEEEE)     /* Page is marked to receive data */
92
+#define VALID_PAGE            uint16_t(0x0000)     /* Page containing valid data */
93
 
93
 
94
 /* Valid pages in read and write defines */
94
 /* Valid pages in read and write defines */
95
-#define READ_FROM_VALID_PAGE  ((uint8_t)0x00)
96
-#define WRITE_IN_VALID_PAGE   ((uint8_t)0x01)
95
+#define READ_FROM_VALID_PAGE  uint8_t(0x00)
96
+#define WRITE_IN_VALID_PAGE   uint8_t(0x01)
97
 
97
 
98
 /* Page full define */
98
 /* Page full define */
99
-#define PAGE_FULL             ((uint8_t)0x80)
99
+#define PAGE_FULL             uint8_t(0x80)
100
 
100
 
101
 /* Variables' number */
101
 /* Variables' number */
102
-#define NB_OF_VAR             ((uint16_t)4096)
102
+#define NB_OF_VAR             uint16_t(4096)
103
 
103
 
104
-/* Exported types ------------------------------------------------------------*/
105
-/* Exported macro ------------------------------------------------------------*/
106
 /* Exported functions ------------------------------------------------------- */
104
 /* Exported functions ------------------------------------------------------- */
107
 uint16_t EE_Initialize(void);
105
 uint16_t EE_Initialize(void);
108
 uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);
106
 uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);

+ 10
- 12
Marlin/src/HAL/HAL_STM32F7/EmulatedEeprom.cpp View File

20
 #ifdef STM32F7
20
 #ifdef STM32F7
21
 
21
 
22
 /**
22
 /**
23
- * Description: functions for I2C connected external EEPROM.
23
+ * Description: Functions for a Flash emulated EEPROM
24
  * Not platform dependent.
24
  * Not platform dependent.
25
  */
25
  */
26
 
26
 
67
 }
67
 }
68
 
68
 
69
 void eeprom_write_byte(uint8_t *pos, unsigned char value) {
69
 void eeprom_write_byte(uint8_t *pos, unsigned char value) {
70
-  uint16_t eeprom_address = (unsigned) pos;
70
+  uint16_t eeprom_address = unsigned(pos);
71
 
71
 
72
   eeprom_init();
72
   eeprom_init();
73
 
73
 
74
   HAL_FLASH_Unlock();
74
   HAL_FLASH_Unlock();
75
   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
75
   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
76
 
76
 
77
-  if (EE_WriteVariable(eeprom_address, (uint16_t) value) != EE_OK)
78
-      for (;;) HAL_Delay(1); // Spin forever until watchdog reset
77
+  if (EE_WriteVariable(eeprom_address, uint16_t(value)) != EE_OK)
78
+    for (;;) HAL_Delay(1); // Spin forever until watchdog reset
79
 
79
 
80
   HAL_FLASH_Lock();
80
   HAL_FLASH_Lock();
81
 }
81
 }
82
 
82
 
83
 uint8_t eeprom_read_byte(uint8_t *pos) {
83
 uint8_t eeprom_read_byte(uint8_t *pos) {
84
-  uint16_t data = 0xFF;
85
-  uint16_t eeprom_address = (unsigned)pos;
86
-
87
   eeprom_init();
84
   eeprom_init();
88
 
85
 
89
-  if (EE_ReadVariable(eeprom_address, &data) != EE_OK) {
90
-    return (unsigned char)data;
91
-  }
92
-  return (unsigned char)data;
86
+  uint16_t data = 0xFF;
87
+  uint16_t eeprom_address = unsigned(pos);
88
+  (void)EE_ReadVariable(eeprom_address, &data); // Data unchanged on error
89
+
90
+  return uint8_t(data);
93
 }
91
 }
94
 
92
 
95
 void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
93
 void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
96
   uint16_t data = 0xFF;
94
   uint16_t data = 0xFF;
97
-  uint16_t eeprom_address = (unsigned) __src;
95
+  uint16_t eeprom_address = unsigned(__src);
98
 
96
 
99
   eeprom_init();
97
   eeprom_init();
100
 
98
 

+ 1
- 2
Marlin/src/HAL/shared/I2cEeprom.cpp View File

95
   }
95
   }
96
 }
96
 }
97
 
97
 
98
-
99
 uint8_t eeprom_read_byte(uint8_t *pos) {
98
 uint8_t eeprom_read_byte(uint8_t *pos) {
100
   unsigned eeprom_address = (unsigned)pos;
99
   unsigned eeprom_address = (unsigned)pos;
101
 
100
 
109
   return Wire.available() ? Wire.read() : 0xFF;
108
   return Wire.available() ? Wire.read() : 0xFF;
110
 }
109
 }
111
 
110
 
112
-// maybe let's not read more than 30 or 32 bytes at a time!
111
+// Don't read more than 30..32 bytes at a time!
113
 void eeprom_read_block(void* pos, const void* eeprom_address, size_t n) {
112
 void eeprom_read_block(void* pos, const void* eeprom_address, size_t n) {
114
   eeprom_init();
113
   eeprom_init();
115
 
114
 

Loading…
Cancel
Save