/** * @brief Restore the pages to a known good state in case of page's status * corruption after a power loss. * @param None. * @retval - Flash error code: on write Flash error * - FLASH_COMPLETE: on success */ uint16_t EE_Init(void) { uint16_t PageStatus0 = 6, PageStatus1 = 6; uint16_t VarIdx = 0; uint16_t EepromStatus = 0, ReadStatus = 0; int16_t x = -1; HAL_StatusTypeDef FlashStatus; uint32_t SectorError = 0; FLASH_EraseInitTypeDef pEraseInit; /* Get Page0 status */ PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS); /* Get Page1 status */ PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS); pEraseInit.TypeErase = TYPEERASE_SECTORS; pEraseInit.Sector = PAGE0_ID; pEraseInit.NbSectors = 1; pEraseInit.VoltageRange = VOLTAGE_RANGE; /* Check for invalid header states and repair if necessary */ switch (PageStatus0) { case ERASED: if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ { /* Erase Page0 */ if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) { FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } } else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ { /* Erase Page0 */ if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) { FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } /* Mark Page1 as valid */ FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */ { /* Erase both Page0 and Page1 and set Page0 as valid page */ FlashStatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } break; case RECEIVE_DATA: if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */ { /* Transfer data from Page1 to Page0 */ for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) { if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx]) { x = VarIdx; } if (VarIdx != x) { /* Read the last variables' updates */ ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar); /* In case variable corresponding to the virtual address was found */ if (ReadStatus != 0x1) { /* Transfer the variable to the Page0 */ EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar); /* If program operation was failed, a Flash error code is returned */ if (EepromStatus != HAL_OK) { return EepromStatus; } } } } /* Mark Page0 as valid */ FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } pEraseInit.Sector = PAGE1_ID; pEraseInit.NbSectors = 1; pEraseInit.VoltageRange = VOLTAGE_RANGE; /* Erase Page1 */ if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) { FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } } else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */ { pEraseInit.Sector = PAGE1_ID; pEraseInit.NbSectors = 1; pEraseInit.VoltageRange = VOLTAGE_RANGE; /* Erase Page1 */ if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) { FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } /* Mark Page0 as valid */ FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } else /* Invalid state -> format eeprom */ { /* Erase both Page0 and Page1 and set Page0 as valid page */ FlashStatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } break; case VALID_PAGE: if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */ { /* Erase both Page0 and Page1 and set Page0 as valid page */ FlashStatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */ { pEraseInit.Sector = PAGE1_ID; pEraseInit.NbSectors = 1; pEraseInit.VoltageRange = VOLTAGE_RANGE; /* Erase Page1 */ if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) { FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } } else /* Page0 valid, Page1 receive */ { /* Transfer data from Page0 to Page1 */ for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) { if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx]) { x = VarIdx; } if (VarIdx != x) { /* Read the last variables' updates */ ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar); /* In case variable corresponding to the virtual address was found */ if (ReadStatus != 0x1) { /* Transfer the variable to the Page1 */ EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar); /* If program operation was failed, a Flash error code is returned */ if (EepromStatus != HAL_OK) { return EepromStatus; } } } } /* Mark Page1 as valid */ FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } pEraseInit.Sector = PAGE0_ID; pEraseInit.NbSectors = 1; pEraseInit.VoltageRange = VOLTAGE_RANGE; /* Erase Page0 */ if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) { FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } } break; default: /* Any other state -> format eeprom */ /* Erase both Page0 and Page1 and set Page0 as valid page */ FlashStatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } break; } return HAL_OK; }
/** * @brief Restore the pages to a known good state in case of page's status * corruption after a power loss. * @param None. * @retval - Flash error code: on write Flash error * - FLASH_COMPLETE: on success */ uint16_t EE_Init(void) { uint16_t pagestatus0 = 6, pagestatus1 = 6; uint16_t varidx = 0; uint16_t eepromstatus = 0, readstatus = 0; int16_t x = -1; HAL_StatusTypeDef flashstatus; uint32_t page_error = 0; FLASH_EraseInitTypeDef s_eraseinit; /* Get Page0 status */ pagestatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS); /* Get Page1 status */ pagestatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS); /* Fill EraseInit structure*/ s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES; s_eraseinit.PageAddress = PAGE0_ID; s_eraseinit.NbPages = 1; /* Check for invalid header states and repair if necessary */ switch (pagestatus0) { case ERASED: if (pagestatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ { /* Erase Page0 */ if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) { flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error); /* If erase operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } } else if (pagestatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ { /* Erase Page0 */ if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) { flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error); /* If erase operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } /* Mark Page1 as valid */ flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */ { /* Erase both Page0 and Page1 and set Page0 as valid page */ flashstatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } break; case RECEIVE_DATA: if (pagestatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */ { /* Transfer data from Page1 to Page0 */ for (varidx = 0; varidx < NB_OF_VAR; varidx++) { if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[varidx]) { x = varidx; } if (varidx != x) { /* Read the last variables' updates */ readstatus = EE_ReadVariable(VirtAddVarTab[varidx], &DataVar); /* In case variable corresponding to the virtual address was found */ if (readstatus != 0x1) { /* Transfer the variable to the Page0 */ eepromstatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[varidx], DataVar); /* If program operation was failed, a Flash error code is returned */ if (eepromstatus != HAL_OK) { return eepromstatus; } } } } /* Mark Page0 as valid */ flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES; s_eraseinit.PageAddress = PAGE1_ID; s_eraseinit.NbPages = 1; /* Erase Page1 */ if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) { flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error); /* If erase operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } } else if (pagestatus1 == ERASED) /* Page0 receive, Page1 erased */ { s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES; s_eraseinit.PageAddress = PAGE1_ID; s_eraseinit.NbPages = 1; /* Erase Page1 */ if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) { flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error); /* If erase operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } /* Mark Page0 as valid */ flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } else /* Invalid state -> format eeprom */ { /* Erase both Page0 and Page1 and set Page0 as valid page */ flashstatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } break; case VALID_PAGE: if (pagestatus1 == VALID_PAGE) /* Invalid state -> format eeprom */ { /* Erase both Page0 and Page1 and set Page0 as valid page */ flashstatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } else if (pagestatus1 == ERASED) /* Page0 valid, Page1 erased */ { s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES; s_eraseinit.PageAddress = PAGE1_ID; s_eraseinit.NbPages = 1; /* Erase Page1 */ if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) { flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error); /* If erase operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } } else /* Page0 valid, Page1 receive */ { /* Transfer data from Page0 to Page1 */ for (varidx = 0; varidx < NB_OF_VAR; varidx++) { if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[varidx]) { x = varidx; } if (varidx != x) { /* Read the last variables' updates */ readstatus = EE_ReadVariable(VirtAddVarTab[varidx], &DataVar); /* In case variable corresponding to the virtual address was found */ if (readstatus != 0x1) { /* Transfer the variable to the Page1 */ eepromstatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[varidx], DataVar); /* If program operation was failed, a Flash error code is returned */ if (eepromstatus != HAL_OK) { return eepromstatus; } } } } /* Mark Page1 as valid */ flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES; s_eraseinit.PageAddress = PAGE0_ID; s_eraseinit.NbPages = 1; /* Erase Page0 */ if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) { flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error); /* If erase operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } } break; default: /* Any other state -> format eeprom */ /* Erase both Page0 and Page1 and set Page0 as valid page */ flashstatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } break; } return HAL_OK; }
/** * @brief Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE * @param None * @retval Status of the last operation (Flash write or erase) done during * EEPROM formating */ static HAL_StatusTypeDef EE_Format(void) { HAL_StatusTypeDef FlashStatus = HAL_OK; uint32_t SectorError = 0; FLASH_EraseInitTypeDef pEraseInit; pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS; pEraseInit.Sector = PAGE0_ID; pEraseInit.NbSectors = 1; pEraseInit.VoltageRange = VOLTAGE_RANGE; /* Erase Page0 */ if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) { FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */ FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } pEraseInit.Sector = PAGE1_ID; /* Erase Page1 */ if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) { FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != HAL_OK) { return FlashStatus; } } return HAL_OK; }
/** * @brief Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE * @param None * @retval Status of the last operation (Flash write or erase) done during * EEPROM formating */ static HAL_StatusTypeDef EE_Format(void) { HAL_StatusTypeDef flashstatus = HAL_OK; uint32_t page_error = 0; FLASH_EraseInitTypeDef s_eraseinit; s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES; s_eraseinit.PageAddress = PAGE0_ID; s_eraseinit.NbPages = 1; /* Erase Page0 */ if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) { flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error); /* If erase operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */ flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } s_eraseinit.PageAddress = PAGE1_ID; /* Erase Page1 */ if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) { flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error); /* If erase operation was failed, a Flash error code is returned */ if (flashstatus != HAL_OK) { return flashstatus; } } return HAL_OK; }