/* Check each active page address starting from end */ while (Address > (PageStartAddress + 2)) {
/* Get the current location content to be compared with virtual address */ AddressValue = (*(__IO uint16_t*)Address);
/* Compare the read address with the virtual address */ if (AddressValue == VirtAddress) {
/* Get content of Address-2 which is variable value */ *Data = (*(__IO uint16_t*)(Address - 2));
/* In case variable value is read, reset ReadStatus flag */ ReadStatus = 0;
break; } else {
/* Next address location */ Address = Address - 4; } }
/* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */ return ReadStatus; } /**
* @brief Writes/upadtes variable data in EEPROM. * @param VirtAddress: Variable virtual address * @param Data: 16 bit data to be written * @retval Success or error status:
* - FLASH_COMPLETE: on success * - PAGE_FULL: if valid page is full
* - NO_VALID_PAGE: if no valid page was found * - Flash error code: on write Flash error */
uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data) {
uint16_t Status = 0;
/* Write the variable virtual address and value in the EEPROM */ Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
/* In case the EEPROM active page is full */ if (Status == PAGE_FULL) {
/* Perform Page transfer */
Status = EE_PageTransfer(VirtAddress, Data); }
/* Return last operation status */ return Status; } /**
* @brief Erases PAGE0 and PAGE1 and writes VALID_PAGE header to PAGE0 * @param None
* @retval Status of the last operation (Flash write or erase) done during * EEPROM formating */
static FLASH_Status EE_Format(void) {
FLASH_Status FlashStatus = FLASH_COMPLETE;
/* 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; }
/* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
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);
/* Return Page1 erase operation status */
return FlashStatus; } /**
* @brief Find valid Page for write or read operation
* @param Operation: operation to achieve on the valid page. * This parameter can be one of the following values:
* @arg READ_FROM_VALID_PAGE: read operation from valid page * @arg WRITE_IN_VALID_PAGE: write operation from valid page
* @retval Valid page number (PAGE0 or PAGE1) or NO_VALID_PAGE in case * of no valid page was found */
static uint16_t EE_FindValidPage(uint8_t Operation) {
uint16_t PageStatus0 = 6, PageStatus1 = 6;
/* Get Page0 actual status */
PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
/* Get Page1 actual status */
PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
/* Write or read operation */ switch (Operation) {
case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */ if (PageStatus1 == VALID_PAGE) {
/* Page0 receiving data */
if (PageStatus0 == RECEIVE_DATA) {
return PAGE0; /* Page0 valid */ } else {
return PAGE1; /* Page1 valid */ } }
else if (PageStatus0 == VALID_PAGE) {
/* Page1 receiving data */
if (PageStatus1 == RECEIVE_DATA) {
return PAGE1; /* Page1 valid */
} else {
return PAGE0; /* Page0 valid */ } } else {
return NO_VALID_PAGE; /* No valid Page */ }
case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */ if (PageStatus0 == VALID_PAGE) {
return PAGE0; /* Page0 valid */ }
else if (PageStatus1 == VALID_PAGE) {
return PAGE1; /* Page1 valid */ } else {
return NO_VALID_PAGE ; /* No valid Page */ }
default:
return PAGE0; /* Page0 valid */ } } /**
* @brief Verify if active page is full and Writes variable in EEPROM. * @param VirtAddress: 16 bit virtual address of the variable * @param Data: 16 bit data to be written as variable value * @retval Success or error status:
* - FLASH_COMPLETE: on success * - PAGE_FULL: if valid page is full
* - NO_VALID_PAGE: if no valid page was found * - Flash error code: on write Flash error */
static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) {
FLASH_Status FlashStatus = FLASH_COMPLETE; uint16_t ValidPage = PAGE0;
//uint32_t Address = 0x08010000,
uint32_t PageEndAddress = 0x080107FF;
/* Get valid Page for write operation */
ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
/* Check if there is no valid page */ if (ValidPage == NO_VALID_PAGE) {
return NO_VALID_PAGE; }
/* Get the valid Page start Address */
//Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE)); //Address = CurWrAddress;//当前写地址
/* Get the valid Page end Address */
PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
/* Check each active page address starting from begining */ while (CurWrAddress < PageEndAddress) {
/* Verify if Address and Address+2 contents are 0xFFFFFFFF */ if ((*(__IO uint32_t*)CurWrAddress) == 0xFFFFFFFF) {
/* Set variable data */
FlashStatus = FLASH_ProgramHalfWord(CurWrAddress, Data);
/* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) {
return FlashStatus; }
/* Set variable virtual address */
FlashStatus = FLASH_ProgramHalfWord(CurWrAddress + 2, VirtAddress);
CurWrAddress = CurWrAddress + 4; /* Return program operation status */ return FlashStatus; } else
{//修改后的算法是不会执行到这里的 /* Next address location */
CurWrAddress = CurWrAddress + 4;