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

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

@@ -53,55 +53,53 @@
53 53
 
54 54
 /* Exported constants --------------------------------------------------------*/
55 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 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 64
 /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
65 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 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 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 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 85
 /* No valid page define */
86
-#define NO_VALID_PAGE         ((uint16_t)0x00AB)
86
+#define NO_VALID_PAGE         uint16_t(0x00AB)
87 87
 
88 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 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 97
 /* Page full define */
98
-#define PAGE_FULL             ((uint8_t)0x80)
98
+#define PAGE_FULL             uint8_t(0x80)
99 99
 
100 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 103
 /* Exported functions ------------------------------------------------------- */
106 104
 uint16_t EE_Initialize(void);
107 105
 uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);

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

@@ -17,13 +17,13 @@
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 22
  * Not platform dependent.
25 23
  */
26 24
 
25
+#if defined(STM32GENERIC) && defined(STM32F4)
26
+
27 27
 #include "../../inc/MarlinConfig.h"
28 28
 
29 29
 #if ENABLED(EEPROM_SETTINGS) && NONE(I2C_EEPROM, SPI_EEPROM)
@@ -69,37 +69,34 @@ void eeprom_init() {
69 69
 }
70 70
 
71 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 74
   eeprom_init();
75 75
 
76 76
   HAL_FLASH_Unlock();
77 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 82
   HAL_FLASH_Lock();
83 83
 }
84 84
 
85 85
 uint8_t eeprom_read_byte(uint8_t *pos) {
86
-  uint16_t data = 0xFF;
87
-  uint16_t eeprom_address = (unsigned)pos;
88
-
89 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 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 96
   eeprom_init();
102 97
 
98
+  uint16_t data = 0xFF;
99
+  uint16_t eeprom_address = (unsigned)__src;
103 100
   for (uint8_t c = 0; c < __n; c++) {
104 101
     EE_ReadVariable(eeprom_address+c, &data);
105 102
     *((uint8_t*)__dst + c) = data;

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

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

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

@@ -53,56 +53,54 @@
53 53
 
54 54
 /* Exported constants --------------------------------------------------------*/
55 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 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 64
 /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
65 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 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 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 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 86
 /* No valid page define */
87
-#define NO_VALID_PAGE         ((uint16_t)0x00AB)
87
+#define NO_VALID_PAGE         uint16_t(0x00AB)
88 88
 
89 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 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 98
 /* Page full define */
99
-#define PAGE_FULL             ((uint8_t)0x80)
99
+#define PAGE_FULL             uint8_t(0x80)
100 100
 
101 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 104
 /* Exported functions ------------------------------------------------------- */
107 105
 uint16_t EE_Initialize(void);
108 106
 uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);

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

@@ -20,7 +20,7 @@
20 20
 #ifdef STM32F7
21 21
 
22 22
 /**
23
- * Description: functions for I2C connected external EEPROM.
23
+ * Description: Functions for a Flash emulated EEPROM
24 24
  * Not platform dependent.
25 25
  */
26 26
 
@@ -67,34 +67,32 @@ void eeprom_init() {
67 67
 }
68 68
 
69 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 72
   eeprom_init();
73 73
 
74 74
   HAL_FLASH_Unlock();
75 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 80
   HAL_FLASH_Lock();
81 81
 }
82 82
 
83 83
 uint8_t eeprom_read_byte(uint8_t *pos) {
84
-  uint16_t data = 0xFF;
85
-  uint16_t eeprom_address = (unsigned)pos;
86
-
87 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 93
 void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
96 94
   uint16_t data = 0xFF;
97
-  uint16_t eeprom_address = (unsigned) __src;
95
+  uint16_t eeprom_address = unsigned(__src);
98 96
 
99 97
   eeprom_init();
100 98
 

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

@@ -95,7 +95,6 @@ void eeprom_update_block(const void *pos, void* eeprom_address, size_t n) {
95 95
   }
96 96
 }
97 97
 
98
-
99 98
 uint8_t eeprom_read_byte(uint8_t *pos) {
100 99
   unsigned eeprom_address = (unsigned)pos;
101 100
 
@@ -109,7 +108,7 @@ uint8_t eeprom_read_byte(uint8_t *pos) {
109 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 112
 void eeprom_read_block(void* pos, const void* eeprom_address, size_t n) {
114 113
   eeprom_init();
115 114
 

Loading…
Cancel
Save