//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- void InitHostSystem(BYTE fPowerUpBoot) { BYTE ee_mode; //check EEPROM ee_mode = CheckEEPROM(); if(ee_mode==1) { //---------- if FW version is not matched, initialize EEPROM data ----------- InitHostWithNTSC(); DebugLevel = 3; #ifdef USE_SFLASH_EEPROM EE_Format(); EE_FindCurrInfo(); #endif InputMain = 0xff; // start with saved input InitializeEE(); //save all default EE values. DebugLevel = 0; SaveDebugLevelEE(DebugLevel); ee_mode = 0; } //read debug level DebugLevel = GetDebugLevelEE(); if((DebugLevel==0) && (fPowerUpBoot)) Printf("\n===> Debugging was OFF (%02bx)", DebugLevel); else ePrintf("\n===> Debugging is ON (%02bx)", DebugLevel); ePrintf("\nInitHostSystem(%bd)",fPowerUpBoot); //Init HW with default InitHostWithNTSC(); FP_Host_GpioDefault(); SSPLL_Host_PowerUp(ON); PrintSystemClockMsg("SSPLL_Host_PowerUp"); WriteHostPage(0); WriteHost(REG008, 0x89); //Output enable #if 0 //TODO: current method does not working. // try it with stable SSPLL code. See the ... //McuSpiClkSelect(MCUSPI_CLK_PCLK); WriteHostPage(PAGE4_CLOCK); WriteHost(REG4E1, 0x20 | (ReadHost(REG4E1) & 0x0F)); #endif //dump clock DumpClock(1); Printf("\nNow, enable the slave system...."); }
void setup_FLASH() { uint16_t tmp_eeprom_data_structure_version; FLASH_Unlock(); EE_Init(); EE_ReadVariable(VirtAddVarTab[index_eeprom_data_structure_version],&tmp_eeprom_data_structure_version); //eeprom_data_structure_version lesen if (tmp_eeprom_data_structure_version != eeprom_data_structure_version) { EE_Format(); EE_WriteVariable(VirtAddVarTab[index_eeprom_data_structure_version],eeprom_data_structure_version); } }
/** * @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; uint16_t FlashStatus; /* Get Page0 status */ PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS); /* Get Page1 status */ PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS); /* Check for invalid header states and repair if necessary */ switch(PageStatus0) { case ERASED: if(PageStatus1 == VALID_PAGE) { /* Page0 erased, Page1 valid */ /* Erase Page0 */ FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } else if(PageStatus1 == RECEIVE_DATA) { /* Page0 erased, Page1 receive */ /* Erase Page0 */ FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Mark Page1 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { return FlashStatus; } } break; case RECEIVE_DATA: if(PageStatus1 == VALID_PAGE) { /* Page0 receive, Page1 valid */ /* Transfer data from Page1 to Page0 */ for(VarIdx = 0; VarIdx < NumbOfVar; VarIdx++) { if((*(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx]) { //前两个字节存PageStatus,随后每4字节为一组,先存数据值(前2字节),再存虚拟地址(后2字节),所以+6,是判断第一个组的虚拟地址:jkl 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 != FLASH_COMPLETE) { return EepromStatus; } } } } /* Mark Page0 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Erase Page1 */ FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } else if(PageStatus1 == ERASED) { /* Page0 receive, Page1 erased */ /* Erase Page1 */ FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Mark Page0 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { return FlashStatus; } } else if(PageStatus1 == ERASED) { /* Page0 valid, Page1 erased */ /* Erase Page1 */ FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } else { /* Page0 valid, Page1 receive */ /* Transfer data from Page0 to Page1 */ for(VarIdx = 0; VarIdx < NumbOfVar; 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 != FLASH_COMPLETE) { return EepromStatus; } } } } /* Mark Page1 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Erase Page0 */ FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if(FlashStatus != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { return FlashStatus; } break; } return FLASH_COMPLETE; }
/** * @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; uint16_t FlashStatus; /* Get Page0 status */ PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS); /* Get Page1 status */ PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS); /* Check for invalid header states and repair if necessary */ switch (PageStatus0) { case ERASED: if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ { /* Erase Page0 */ FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ { /* Erase Page0 */ FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Mark Page1 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { return FlashStatus; } } break; case RECEIVE_DATA: if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */ { /* Transfer data from Page1 to Page0 */ /* First transfer the contiguous region of addresses */ for (VarIdx = 0; VarIdx < EE_MAX_CONTIGUOUS; VarIdx++) { EepromStatus = EE_TransferVariable(VarIdx, PAGE0_BASE_ADDRESS); if (EepromStatus != FLASH_COMPLETE) return EepromStatus; } /* Then the individual addresses in the address table */ for (VarIdx = 0; VarIdx < EE_NUM_ADDR; VarIdx++) { EepromStatus = EE_TransferVariable(ee_addr_table[VarIdx], PAGE0_BASE_ADDRESS); if (EepromStatus != FLASH_COMPLETE) return EepromStatus; } /* Mark Page0 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Erase Page1 */ FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */ { /* Erase Page1 */ FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Mark Page0 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { return FlashStatus; } } else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */ { /* Erase Page1 */ FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } else /* Page0 valid, Page1 receive */ { /* Transfer data from Page0 to Page1 */ /* First transfer the contiguous region of addresses */ for (VarIdx = 0; VarIdx < EE_MAX_CONTIGUOUS; VarIdx++) { EepromStatus = EE_TransferVariable(VarIdx, PAGE1_BASE_ADDRESS); if (EepromStatus != FLASH_COMPLETE) return EepromStatus; } /* Then the individual addresses in the address table */ for (VarIdx = 0; VarIdx < EE_NUM_ADDR; VarIdx++) { EepromStatus = EE_TransferVariable(ee_addr_table[VarIdx], PAGE1_BASE_ADDRESS); if (EepromStatus != FLASH_COMPLETE) return EepromStatus; } /* Mark Page1 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Erase Page0 */ FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { return FlashStatus; } break; } return FLASH_COMPLETE; }
/** * @brief COnfiguration of RTC Registers, Selection and Enabling of * RTC clock * @param None * @retval : None */ void RTC_Configuration() { uint16_t WaitForOscSource; /*Allow access to Backup Registers*/ PWR_BackupAccessCmd(ENABLE); if(BKP_ReadBackupRegister(BKP_DR1)==CONFIGURATION_RESET) { /*Enables the clock to Backup and power interface peripherals */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE); if(TotalMenuPointer == 1) { /* Backup Domain Reset */ BKP_DeInit(); s_DateStructVar.Month=DEFAULT_MONTH ; s_DateStructVar.Day=DEFAULT_DAY; s_DateStructVar.Year=DEFAULT_YEAR; SummerTimeCorrect = OCTOBER_FLAG_SET; BKP_WriteBackupRegister(BKP_DR7,SummerTimeCorrect); BKP_WriteBackupRegister(BKP_DR2,s_DateStructVar.Month); BKP_WriteBackupRegister(BKP_DR3,s_DateStructVar.Day); BKP_WriteBackupRegister(BKP_DR4,s_DateStructVar.Year); BKP_WriteBackupRegister(BKP_DR1, CONFIGURATION_DONE); LCD_Clear(Blue2); LCD_SetBackColor(Blue2); LCD_SetTextColor(White); LCD_DisplayString(Line2,Column0,"Please wait......"); LCD_DisplayString(Line4,Column0,"Synchronization may"); LCD_DisplayString(Line6,Column0,"take upto a minute"); EE_Format(); TamperNumber=0; BKP_WriteBackupRegister(BKP_DR5,TamperNumber); } /*Enable 32.768 kHz external oscillator */ RCC_LSEConfig(RCC_LSE_ON); for(WaitForOscSource=0;WaitForOscSource<5000;WaitForOscSource++) { } RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* RTC Enabled */ RCC_RTCCLKCmd(ENABLE); RTC_WaitForLastTask(); /*Wait for RTC registers synchronisation */ RTC_WaitForSynchro(); RTC_WaitForLastTask(); /* Setting RTC Interrupts-Seconds interrupt enabled */ /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC , ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); if(TotalMenuPointer==1 || TotalMenuPointer==6) { BKP_WriteBackupRegister(BKP_DR6, 1); /* Set RTC prescaler: set RTC period to 1 sec */ RTC_SetPrescaler(32765); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ /* Prescaler is set to 32766 instead of 32768 to compensate for lower as well as higher frequencies*/ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } if( TotalMenuPointer==7) { BKP_WriteBackupRegister(BKP_DR6, 2); /* Set RTC prescaler: set RTC period to 1 sec */ RTC_SetPrescaler(49); /* RTC period = RTCCLK/RTC_PR = (50 Hz)/(49+1) */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } if(TotalMenuPointer==8) { BKP_WriteBackupRegister(BKP_DR6, 3); /* Set RTC prescaler: set RTC period to 1 sec */ RTC_SetPrescaler(59); /* RTC period = RTCCLK/RTC_PR = (60 Hz)/(59+1) */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } /* Set default system time to 09 : 24 : 00 */ SetTime(DEFAULT_HOURS,DEFAULT_MINUTES,DEFAULT_SECONDS); BKP_WriteBackupRegister(BKP_DR1, CONFIGURATION_DONE); } else { /* PWR and BKP clocks selection */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); for(WaitForOscSource=0;WaitForOscSource<5000;WaitForOscSource++); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); RTC_WaitForLastTask(); } /* Check if how many days are elapsed in power down/Low Power Mode- Updates Date that many Times*/ CheckForDaysElapsed(); ClockSource = BKP_ReadBackupRegister(BKP_DR6); TamperNumber = BKP_ReadBackupRegister(BKP_DR5); s_DateStructVar.Month = BKP_ReadBackupRegister(BKP_DR2); s_DateStructVar.Day = BKP_ReadBackupRegister(BKP_DR3); s_DateStructVar.Year = BKP_ReadBackupRegister(BKP_DR4); SummerTimeCorrect = BKP_ReadBackupRegister(BKP_DR7); s_AlarmDateStructVar.Month = BKP_ReadBackupRegister(BKP_DR8); s_AlarmDateStructVar.Day = BKP_ReadBackupRegister(BKP_DR9); s_AlarmDateStructVar.Year = BKP_ReadBackupRegister(BKP_DR10); }
/** * @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) { /* Unlock the Flash Program Erase controller */ FLASH_Unlock(); /* We use a uint16_t counter here because that is the width of the address field, thus that many are supported. */ for (uint16_t i = 0; i < NUM_EEPROM_ADDRESSES; ++i) { EEPROM_ADDRESSES[i] = i; /* We will always do them sequentially */ } uint16_t PageStatus0 = 6, PageStatus1 = 6; uint16_t VarIdx = 0; uint16_t EepromStatus = 0, ReadStatus = 0; int16_t x = -1; uint16_t FlashStatus; /* Get Page0 status */ PageStatus0 = (*(__IO uint16_t*) PAGE0_BASE_ADDRESS ); /* Get Page1 status */ PageStatus1 = (*(__IO uint16_t*) PAGE1_BASE_ADDRESS ); /* Check for invalid header states and repair if necessary */ switch (PageStatus0) { case ERASED : if (PageStatus1 == VALID_PAGE ) /* Page0 erased, Page1 valid */ { /* Erase Page0 */ FlashStatus = FLASH_EraseSector(PAGE0_ID, FLASH_VOLTAGE_RANGE ); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } else if (PageStatus1 == RECEIVE_DATA ) /* Page0 erased, Page1 receive */ { /* Erase Page0 */ FlashStatus = FLASH_EraseSector(PAGE0_ID, FLASH_VOLTAGE_RANGE ); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Mark Page1 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE ); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { return FlashStatus; } } break; case RECEIVE_DATA : if (PageStatus1 == VALID_PAGE ) /* Page0 receive, Page1 valid */ { /* Transfer data from Page1 to Page0 */ for (VarIdx = 0; VarIdx < NUM_EEPROM_ADDRESSES ; VarIdx++) { if ((*(__IO uint16_t*) (PAGE0_BASE_ADDRESS + 6)) == EEPROM_ADDRESSES[VarIdx]) { x = VarIdx; } if (VarIdx != x) { /* Read the last variables' updates */ ReadStatus = EE_ReadVariable(EEPROM_ADDRESSES[VarIdx], &DataVar); /* In case variable corresponding to the virtual address was found */ if (ReadStatus != 0x1) { /* Transfer the variable to the Page0 */ EepromStatus = EE_VerifyPageFullWriteVariable(EEPROM_ADDRESSES[VarIdx], DataVar); /* If program operation was failed, a Flash error code is returned */ if (EepromStatus != FLASH_COMPLETE) { return EepromStatus; } } } } /* Mark Page0 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE ); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Erase Page1 */ FlashStatus = FLASH_EraseSector(PAGE1_ID, FLASH_VOLTAGE_RANGE ); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } else if (PageStatus1 == ERASED ) /* Page0 receive, Page1 erased */ { /* Erase Page1 */ FlashStatus = FLASH_EraseSector(PAGE1_ID, FLASH_VOLTAGE_RANGE ); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Mark Page0 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE ); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { return FlashStatus; } } else if (PageStatus1 == ERASED ) /* Page0 valid, Page1 erased */ { /* Erase Page1 */ FlashStatus = FLASH_EraseSector(PAGE1_ID, FLASH_VOLTAGE_RANGE ); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } else /* Page0 valid, Page1 receive */ { /* Transfer data from Page0 to Page1 */ for (VarIdx = 0; VarIdx < NUM_EEPROM_ADDRESSES ; VarIdx++) { if ((*(__IO uint16_t*) (PAGE1_BASE_ADDRESS + 6)) == EEPROM_ADDRESSES[VarIdx]) { x = VarIdx; } if (VarIdx != x) { /* Read the last variables' updates */ ReadStatus = EE_ReadVariable(EEPROM_ADDRESSES[VarIdx], &DataVar); /* In case variable corresponding to the virtual address was found */ if (ReadStatus != 0x1) { /* Transfer the variable to the Page1 */ EepromStatus = EE_VerifyPageFullWriteVariable(EEPROM_ADDRESSES[VarIdx], DataVar); /* If program operation was failed, a Flash error code is returned */ if (EepromStatus != FLASH_COMPLETE) { return EepromStatus; } } } } /* Mark Page1 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE ); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Erase Page0 */ FlashStatus = FLASH_EraseSector(PAGE0_ID, FLASH_VOLTAGE_RANGE ); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { 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 != FLASH_COMPLETE) { return FlashStatus; } break; } /* Lock the Flash Program Erase controller */ //FLASH_Lock(); return FLASH_COMPLETE; }
/** * @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; }