Skip to content

Commit

Permalink
Merge pull request #762 from monkbroc/feature/eeprom-stability
Browse files Browse the repository at this point in the history
Improve EEPROM stability and persistence
  • Loading branch information
m-mcgowan committed Dec 22, 2015
2 parents c5951da + 0cd0812 commit d2b55ee
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions hal/src/stm32f2xx/eeprom_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ static uint16_t EEPROM_WriteVariable(uint16_t EepromAddress, uint16_t EepromData

void HAL_EEPROM_Init(void)
{
/* Unlock the Flash Program Erase controller so EEPROM_Init can
* format EEPROM in case it is invalid */
FLASH_Unlock();
EEPROM_Init();
FLASH_Lock();
}

size_t HAL_EEPROM_Length()
Expand All @@ -110,7 +114,9 @@ void HAL_EEPROM_Write(uint32_t address, uint8_t data)
{
if (address < EEPROM_SIZE)
{
FLASH_Unlock();
EEPROM_WriteVariable(address, data);
FLASH_Lock();
}
}

Expand Down Expand Up @@ -397,9 +403,6 @@ static uint16_t EEPROM_Init(void)
uint16_t FlashStatus;
uint16_t EepromDataVar = 0;

/* Unlock the Flash Program Erase controller */
FLASH_Unlock();

/* Calling this is here is critical on STM32F2 Devices Else Flash Operation Fails */
FLASH_ClearFlags();

Expand All @@ -414,13 +417,7 @@ static uint16_t EEPROM_Init(void)
case ERASED:
if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
{
/* Erase Page0 */
FlashStatus = FLASH_EraseSector(PAGE0_ID,VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
return FlashStatus;
}
/* Normal valid state -> nothing to do */
}
else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */
{
Expand Down Expand Up @@ -523,10 +520,10 @@ static uint16_t EEPROM_Init(void)
break;

case VALID_PAGE:
if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
if (PageStatus1 == VALID_PAGE) /* Harmless invalid state -> pick one page and format the other */
{
/* Erase both Page0 and Page1 and set Page0 as valid page */
FlashStatus = EEPROM_Format();
/* Erase Page0 */
FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE);
/* If erase/program operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
Expand All @@ -535,13 +532,7 @@ static uint16_t EEPROM_Init(void)
}
else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
{
/* Erase Page1 */
FlashStatus = FLASH_EraseSector(PAGE1_ID, VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
return FlashStatus;
}
/* Normal valid state -> nothing to do */
}
else /* Page0 valid, Page1 receive */
{
Expand Down

0 comments on commit d2b55ee

Please sign in to comment.