mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-21 07:15:18 +03:00
Cleanup EEPROM writing code.
Avoid goto/label usage by using a loop instead, remove nesting, improve and simplify logic, use 8 bit type for attempt counter.
This commit is contained in:
parent
bd95c38ff0
commit
54424bf3fb
1 changed files with 9 additions and 14 deletions
19
src/config.c
19
src/config.c
|
@ -111,7 +111,7 @@ void writeEEPROM(void)
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
uint8_t chk = 0;
|
uint8_t chk = 0;
|
||||||
const uint8_t *p;
|
const uint8_t *p;
|
||||||
int tries = 0;
|
int8_t attemptsRemaining = 3;
|
||||||
|
|
||||||
// prepare checksum/version constants
|
// prepare checksum/version constants
|
||||||
mcfg.version = EEPROM_CONF_VERSION;
|
mcfg.version = EEPROM_CONF_VERSION;
|
||||||
|
@ -126,27 +126,22 @@ void writeEEPROM(void)
|
||||||
mcfg.chk = chk;
|
mcfg.chk = chk;
|
||||||
|
|
||||||
// write it
|
// write it
|
||||||
retry:
|
|
||||||
FLASH_Unlock();
|
FLASH_Unlock();
|
||||||
|
while (attemptsRemaining--) {
|
||||||
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
|
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
|
||||||
|
|
||||||
if (FLASH_ErasePage(FLASH_WRITE_ADDR) == FLASH_COMPLETE) {
|
status = FLASH_ErasePage(FLASH_WRITE_ADDR);
|
||||||
for (i = 0; i < sizeof(master_t); i += 4) {
|
for (i = 0; i < sizeof(master_t) && status == FLASH_COMPLETE; i += 4) {
|
||||||
status = FLASH_ProgramWord(FLASH_WRITE_ADDR + i, *(uint32_t *) ((char *)&mcfg + i));
|
status = FLASH_ProgramWord(FLASH_WRITE_ADDR + i, *(uint32_t *) ((char *)&mcfg + i));
|
||||||
if (status != FLASH_COMPLETE) {
|
|
||||||
FLASH_Lock();
|
|
||||||
tries++;
|
|
||||||
if (tries < 3)
|
|
||||||
goto retry;
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
if (status == FLASH_COMPLETE) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FLASH_Lock();
|
FLASH_Lock();
|
||||||
|
|
||||||
// Flash write failed - just die now
|
// Flash write failed - just die now
|
||||||
if (tries == 3 || !isEEPROMContentValid()) {
|
if (status != FLASH_COMPLETE || !isEEPROMContentValid()) {
|
||||||
failureMode(10);
|
failureMode(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue