Open FFBoard
Open source force feedback firmware
eeprom.c
Go to the documentation of this file.
1
49/* Includes ------------------------------------------------------------------*/
50#include "eeprom.h"
51#include "eeprom_addresses.h"
52#ifdef USE_EEPROM_EMULATION
53/* Private typedef -----------------------------------------------------------*/
54/* Private define ------------------------------------------------------------*/
55/* Private macro -------------------------------------------------------------*/
56/* Private variables ---------------------------------------------------------*/
57
58/* Global variable used to store variable value in read sequence */
59uint16_t DataVar = 0;
60
61/* Virtual address defined by the user: 0xFFFF value is prohibited */
62//extern uint16_t VirtAddVarTab[NB_OF_VAR];
63
64/* Private function prototypes -----------------------------------------------*/
65/* Private functions ---------------------------------------------------------*/
66//static HAL_StatusTypeDef EE_Format(void);
67static uint16_t EE_FindValidPage(uint8_t Operation);
68static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data);
69static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data);
70static uint16_t EE_VerifyPageFullyErased(uint32_t Address);
71
79uint16_t EE_Init(void)
80{
81 uint16_t PageStatus0 = 6, PageStatus1 = 6;
82 uint16_t VarIdx = 0;
83 uint16_t EepromStatus = 0, ReadStatus = 0;
84 int16_t x = -1;
85 HAL_StatusTypeDef FlashStatus;
86 uint32_t SectorError = 0;
87 FLASH_EraseInitTypeDef pEraseInit;
88
89
90 /* Get Page0 status */
91 PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
92 /* Get Page1 status */
93 PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
94
95 pEraseInit.TypeErase = TYPEERASE_SECTORS;
96 pEraseInit.Sector = PAGE0_ID;
97 pEraseInit.NbSectors = 1;
98 pEraseInit.VoltageRange = VOLTAGE_RANGE;
99
100 /* Check for invalid header states and repair if necessary */
101 switch (PageStatus0)
102 {
103 case ERASED:
104 if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
105 {
106 /* Erase Page0 */
107 if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
108 {
109 FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
110 /* If erase operation was failed, a Flash error code is returned */
111 if (FlashStatus != HAL_OK)
112 {
113 return FlashStatus;
114 }
115 }
116 }
117 else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */
118 {
119 /* Erase Page0 */
120 if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
121 {
122 FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
123 /* If erase operation was failed, a Flash error code is returned */
124 if (FlashStatus != HAL_OK)
125 {
126 return FlashStatus;
127 }
128 }
129 /* Mark Page1 as valid */
130 FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
131 /* If program operation was failed, a Flash error code is returned */
132 if (FlashStatus != HAL_OK)
133 {
134 return FlashStatus;
135 }
136 }
137 else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
138 {
139 /* Erase both Page0 and Page1 and set Page0 as valid page */
140 FlashStatus = EE_Format();
141 /* If erase/program operation was failed, a Flash error code is returned */
142 if (FlashStatus != HAL_OK)
143 {
144 return FlashStatus;
145 }
146 }
147 break;
148
149 case RECEIVE_DATA:
150 if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */
151 {
152 /* Transfer data from Page1 to Page0 */
153 for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
154 {
155 if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
156 {
157 x = VarIdx;
158 }
159 if (VarIdx != x)
160 {
161 /* Read the last variables' updates */
162 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
163 /* In case variable corresponding to the virtual address was found */
164 if (ReadStatus != 0x1)
165 {
166 /* Transfer the variable to the Page0 */
168 /* If program operation was failed, a Flash error code is returned */
169 if (EepromStatus != HAL_OK)
170 {
171 return EepromStatus;
172 }
173 }
174 }
175 }
176 /* Mark Page0 as valid */
177 FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
178 /* If program operation was failed, a Flash error code is returned */
179 if (FlashStatus != HAL_OK)
180 {
181 return FlashStatus;
182 }
183 pEraseInit.Sector = PAGE1_ID;
184 pEraseInit.NbSectors = 1;
185 pEraseInit.VoltageRange = VOLTAGE_RANGE;
186 /* Erase Page1 */
187 if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
188 {
189 FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
190 /* If erase operation was failed, a Flash error code is returned */
191 if (FlashStatus != HAL_OK)
192 {
193 return FlashStatus;
194 }
195 }
196 }
197 else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
198 {
199 pEraseInit.Sector = PAGE1_ID;
200 pEraseInit.NbSectors = 1;
201 pEraseInit.VoltageRange = VOLTAGE_RANGE;
202 /* Erase Page1 */
203 if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
204 {
205 FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
206 /* If erase operation was failed, a Flash error code is returned */
207 if (FlashStatus != HAL_OK)
208 {
209 return FlashStatus;
210 }
211 }
212 /* Mark Page0 as valid */
213 FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
214 /* If program operation was failed, a Flash error code is returned */
215 if (FlashStatus != HAL_OK)
216 {
217 return FlashStatus;
218 }
219 }
220 else /* Invalid state -> format eeprom */
221 {
222 /* Erase both Page0 and Page1 and set Page0 as valid page */
223 FlashStatus = EE_Format();
224 /* If erase/program operation was failed, a Flash error code is returned */
225 if (FlashStatus != HAL_OK)
226 {
227 return FlashStatus;
228 }
229 }
230 break;
231
232 case VALID_PAGE:
233 if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
234 {
235 /* Erase both Page0 and Page1 and set Page0 as valid page */
236 FlashStatus = EE_Format();
237 /* If erase/program operation was failed, a Flash error code is returned */
238 if (FlashStatus != HAL_OK)
239 {
240 return FlashStatus;
241 }
242 }
243 else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
244 {
245 pEraseInit.Sector = PAGE1_ID;
246 pEraseInit.NbSectors = 1;
247 pEraseInit.VoltageRange = VOLTAGE_RANGE;
248 /* Erase Page1 */
249 if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
250 {
251 FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
252 /* If erase operation was failed, a Flash error code is returned */
253 if (FlashStatus != HAL_OK)
254 {
255 return FlashStatus;
256 }
257 }
258 }
259 else /* Page0 valid, Page1 receive */
260 {
261 /* Transfer data from Page0 to Page1 */
262 for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
263 {
264 if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
265 {
266 x = VarIdx;
267 }
268 if (VarIdx != x)
269 {
270 /* Read the last variables' updates */
271 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
272 /* In case variable corresponding to the virtual address was found */
273 if (ReadStatus != 0x1)
274 {
275 /* Transfer the variable to the Page1 */
277 /* If program operation was failed, a Flash error code is returned */
278 if (EepromStatus != HAL_OK)
279 {
280 return EepromStatus;
281 }
282 }
283 }
284 }
285 /* Mark Page1 as valid */
286 FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
287 /* If program operation was failed, a Flash error code is returned */
288 if (FlashStatus != HAL_OK)
289 {
290 return FlashStatus;
291 }
292 pEraseInit.Sector = PAGE0_ID;
293 pEraseInit.NbSectors = 1;
294 pEraseInit.VoltageRange = VOLTAGE_RANGE;
295 /* Erase Page0 */
296 if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
297 {
298 FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
299 /* If erase operation was failed, a Flash error code is returned */
300 if (FlashStatus != HAL_OK)
301 {
302 return FlashStatus;
303 }
304 }
305 }
306 break;
307
308 default: /* Any other state -> format eeprom */
309 /* Erase both Page0 and Page1 and set Page0 as valid page */
310 FlashStatus = EE_Format();
311 /* If erase/program operation was failed, a Flash error code is returned */
312 if (FlashStatus != HAL_OK)
313 {
314 return FlashStatus;
315 }
316 break;
317 }
318
319 return HAL_OK;
320}
321
332uint16_t EE_VerifyPageFullyErased(uint32_t Address)
333{
334 uint32_t ReadStatus = 1;
335 uint16_t AddressValue = 0x5555;
336
337 /* Check each active page address starting from end */
338 while (Address <= PAGE0_END_ADDRESS)
339 {
340 /* Get the current location content to be compared with virtual address */
341 AddressValue = (*(__IO uint16_t*)Address);
342
343 /* Compare the read address with the virtual address */
344 if (AddressValue != ERASED)
345 {
346
347 /* In case variable value is read, reset ReadStatus flag */
348 ReadStatus = 0;
349
350 break;
351 }
352 /* Next address location */
353 Address = Address + 4;
354 }
355
356 /* Return ReadStatus value: (0: Page not erased, 1: Sector erased) */
357 return ReadStatus;
358}
359
370uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data)
371{
372 uint16_t ValidPage = PAGE0;
373 uint16_t AddressValue = 0x5555, ReadStatus = 1;
374 uint32_t Address = EEPROM_START_ADDRESS, PageStartAddress = EEPROM_START_ADDRESS;
375
376 /* Get active Page for read operation */
377 ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
378
379 /* Check if there is no valid page */
380 if (ValidPage == NO_VALID_PAGE)
381 {
382 return NO_VALID_PAGE;
383 }
384
385 /* Get the valid Page start Address */
386 PageStartAddress = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
387
388 /* Get the valid Page end Address */
389 Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
390
391 /* Check each active page address starting from end */
392 while (Address > (PageStartAddress + 2))
393 {
394 /* Get the current location content to be compared with virtual address */
395 AddressValue = (*(__IO uint16_t*)Address);
396
397 /* Compare the read address with the virtual address */
398 if (AddressValue == VirtAddress)
399 {
400 /* Get content of Address-2 which is variable value */
401 *Data = (*(__IO uint16_t*)(Address - 2));
402
403 /* In case variable value is read, reset ReadStatus flag */
404 ReadStatus = 0;
405
406 break;
407 }
408 else
409 {
410 /* Next address location */
411 Address = Address - 4;
412 }
413 }
414
415 /* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */
416 return ReadStatus;
417}
418
429uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
430{
431 uint16_t Status = 0;
432
433 /* Write the variable virtual address and value in the EEPROM */
434 Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
435
436 /* In case the EEPROM active page is full */
437 if (Status == PAGE_FULL)
438 {
439 /* Perform Page transfer */
440 Status = EE_PageTransfer(VirtAddress, Data);
441 }
442
443 /* Return last operation status */
444 return Status;
445}
446
453HAL_StatusTypeDef EE_Format(void)
454{
455 HAL_StatusTypeDef FlashStatus = HAL_OK;
456 uint32_t SectorError = 0;
457 FLASH_EraseInitTypeDef pEraseInit;
458
459 pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
460 pEraseInit.Sector = PAGE0_ID;
461 pEraseInit.NbSectors = 1;
462 pEraseInit.VoltageRange = VOLTAGE_RANGE;
463 /* Erase Page0 */
464 if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
465 {
466 FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
467 /* If erase operation was failed, a Flash error code is returned */
468 if (FlashStatus != HAL_OK)
469 {
470 return FlashStatus;
471 }
472 }
473 /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
474 FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
475 /* If program operation was failed, a Flash error code is returned */
476 if (FlashStatus != HAL_OK)
477 {
478 return FlashStatus;
479 }
480
481 pEraseInit.Sector = PAGE1_ID;
482 /* Erase Page1 */
483 if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
484 {
485 FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
486 /* If erase operation was failed, a Flash error code is returned */
487 if (FlashStatus != HAL_OK)
488 {
489 return FlashStatus;
490 }
491 }
492
493 return HAL_OK;
494}
495
505static uint16_t EE_FindValidPage(uint8_t Operation)
506{
507 uint16_t PageStatus0 = 6, PageStatus1 = 6;
508
509 /* Get Page0 actual status */
510 PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
511
512 /* Get Page1 actual status */
513 PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
514
515 /* Write or read operation */
516 switch (Operation)
517 {
518 case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */
519 if (PageStatus1 == VALID_PAGE)
520 {
521 /* Page0 receiving data */
522 if (PageStatus0 == RECEIVE_DATA)
523 {
524 return PAGE0; /* Page0 valid */
525 }
526 else
527 {
528 return PAGE1; /* Page1 valid */
529 }
530 }
531 else if (PageStatus0 == VALID_PAGE)
532 {
533 /* Page1 receiving data */
534 if (PageStatus1 == RECEIVE_DATA)
535 {
536 return PAGE1; /* Page1 valid */
537 }
538 else
539 {
540 return PAGE0; /* Page0 valid */
541 }
542 }
543 else
544 {
545 return NO_VALID_PAGE; /* No valid Page */
546 }
547
548 case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */
549 if (PageStatus0 == VALID_PAGE)
550 {
551 return PAGE0; /* Page0 valid */
552 }
553 else if (PageStatus1 == VALID_PAGE)
554 {
555 return PAGE1; /* Page1 valid */
556 }
557 else
558 {
559 return NO_VALID_PAGE ; /* No valid Page */
560 }
561
562 default:
563 return PAGE0; /* Page0 valid */
564 }
565}
566
577static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
578{
579 HAL_StatusTypeDef FlashStatus = HAL_OK;
580 uint16_t ValidPage = PAGE0;
581 uint32_t Address = EEPROM_START_ADDRESS, PageEndAddress = EEPROM_START_ADDRESS+PAGE_SIZE;
582
583 /* Get valid Page for write operation */
584 ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
585
586 /* Check if there is no valid page */
587 if (ValidPage == NO_VALID_PAGE)
588 {
589 return NO_VALID_PAGE;
590 }
591
592 /* Get the valid Page start Address */
593 Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
594
595 /* Get the valid Page end Address */
596 PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 1) + (uint32_t)((ValidPage + 1) * PAGE_SIZE));
597
598 /* Check each active page address starting from begining */
599 while (Address < PageEndAddress)
600 {
601 /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
602 if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF)
603 {
604 /* Set variable data */
605 FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address, Data);
606 /* If program operation was failed, a Flash error code is returned */
607 if (FlashStatus != HAL_OK)
608 {
609 return FlashStatus;
610 }
611 /* Set variable virtual address */
612 FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address + 2, VirtAddress);
613 /* Return program operation status */
614 return FlashStatus;
615 }
616 else
617 {
618 /* Next address location */
619 Address = Address + 4;
620 }
621 }
622
623 /* Return PAGE_FULL in case the valid page is full */
624 return PAGE_FULL;
625}
626
638static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
639{
640 HAL_StatusTypeDef FlashStatus = HAL_OK;
641 uint32_t NewPageAddress = EEPROM_START_ADDRESS;
642 uint16_t OldPageId=0;
643 uint16_t ValidPage = PAGE0, VarIdx = 0;
644 uint16_t EepromStatus = 0, ReadStatus = 0;
645 uint32_t SectorError = 0;
646 FLASH_EraseInitTypeDef pEraseInit;
647
648 /* Get active Page for read operation */
649 ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
650
651 if (ValidPage == PAGE1) /* Page1 valid */
652 {
653 /* New page address where variable will be moved to */
654 NewPageAddress = PAGE0_BASE_ADDRESS;
655
656 /* Old page ID where variable will be taken from */
657 OldPageId = PAGE1_ID;
658 }
659 else if (ValidPage == PAGE0) /* Page0 valid */
660 {
661 /* New page address where variable will be moved to */
662 NewPageAddress = PAGE1_BASE_ADDRESS;
663
664 /* Old page ID where variable will be taken from */
665 OldPageId = PAGE0_ID;
666 }
667 else
668 {
669 return NO_VALID_PAGE; /* No valid Page */
670 }
671
672 /* Set the new Page status to RECEIVE_DATA status */
673 FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, RECEIVE_DATA);
674 /* If program operation was failed, a Flash error code is returned */
675 if (FlashStatus != HAL_OK)
676 {
677 return FlashStatus;
678 }
679
680 /* Write the variable passed as parameter in the new active page */
681 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
682 /* If program operation was failed, a Flash error code is returned */
683 if (EepromStatus != HAL_OK)
684 {
685 return EepromStatus;
686 }
687
688 /* Transfer process: transfer variables from old to the new active page */
689 for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
690 {
691 if (VirtAddVarTab[VarIdx] != VirtAddress) /* Check each variable except the one passed as parameter */
692 {
693 /* Read the other last variable updates */
694 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
695 /* In case variable corresponding to the virtual address was found */
696 if (ReadStatus != 0x1)
697 {
698 /* Transfer the variable to the new active page */
700 /* If program operation was failed, a Flash error code is returned */
701 if (EepromStatus != HAL_OK)
702 {
703 return EepromStatus;
704 }
705 }
706 }
707 }
708
709 pEraseInit.TypeErase = TYPEERASE_SECTORS;
710 pEraseInit.Sector = OldPageId;
711 pEraseInit.NbSectors = 1;
712 pEraseInit.VoltageRange = VOLTAGE_RANGE;
713
714 /* Erase the old Page: Set old Page status to ERASED status */
715 FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
716 /* If erase operation was failed, a Flash error code is returned */
717 if (FlashStatus != HAL_OK)
718 {
719 return FlashStatus;
720 }
721
722 /* Set new Page status to VALID_PAGE status */
723 FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, VALID_PAGE);
724 /* If program operation was failed, a Flash error code is returned */
725 if (FlashStatus != HAL_OK)
726 {
727 return FlashStatus;
728 }
729
730 /* Return last operation flash status */
731 return FlashStatus;
732}
733
737#endif
738/******************* (C) COPYRIGHT 2017 STMicroelectronics *****END OF FILE****/
const uint16_t VirtAddVarTab[NB_OF_VAR]
uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t *Data)
Returns the last stored variable data, if found, which correspond to the passed virtual address.
Definition: eeprom.c:370
uint16_t DataVar
Definition: eeprom.c:59
uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
Writes/upadtes variable data in EEPROM.
Definition: eeprom.c:429
HAL_StatusTypeDef EE_Format(void)
Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE.
Definition: eeprom.c:453
static uint16_t EE_FindValidPage(uint8_t Operation)
Find valid Page for write or read operation.
Definition: eeprom.c:505
static uint16_t EE_VerifyPageFullyErased(uint32_t Address)
Verify if specified page is fully erased.
Definition: eeprom.c:332
uint16_t EE_Init(void)
Restore the pages to a known good state in case of page's status corruption after a power loss.
Definition: eeprom.c:79
static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
Transfers last updated variables data from the full Page to an empty one.
Definition: eeprom.c:638
static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
Verify if active page is full and Writes variable in EEPROM.
Definition: eeprom.c:577