diff --git a/flight/pios/common/pios_flash_eeprom.c b/flight/pios/common/pios_flash_eeprom.c index 2a31a81ef..69389cdb4 100644 --- a/flight/pios/common/pios_flash_eeprom.c +++ b/flight/pios/common/pios_flash_eeprom.c @@ -32,6 +32,10 @@ #ifdef PIOS_INCLUDE_FLASH_EEPROM +#ifndef PIOS_EEPROM_EMULATED_SECTOR_SIZE +#define PIOS_EEPROM_EMULATED_SECTOR_SIZE 256 +#endif + #include #include enum pios_eeprom_dev_magic { @@ -393,8 +397,24 @@ static int32_t PIOS_Flash_EEPROM_EraseSector(uintptr_t flash_id, uint32_t addr) const uint8_t buf[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; - return PIOS_Flash_EEPROM_Write(flash_dev, addr, buf, sizeof(buf)); + uint32_t current = addr; + while (current - addr < PIOS_EEPROM_EMULATED_SECTOR_SIZE) { + uint32_t erase_size = (MIN(sizeof(buf), flash_dev->cfg->page_len)); + erase_size = MIN(erase_size, (PIOS_EEPROM_EMULATED_SECTOR_SIZE - current + addr)); + int32_t res = PIOS_Flash_EEPROM_Write(flash_dev, current, buf, erase_size); + if (!res) { + return res; + } + current += erase_size; + } + return 0; } /**