From c4b8aff07d5366cef2c7209729f6cd22fa67de0c Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 18 Jun 2019 23:16:12 -0700 Subject: [PATCH] altos/micropeak-v2: Erase log space at end of BOOST_DELAY We have to erase the log before the flight starts, so this seems like the latest possible moment. Signed-off-by: Keith Packard --- src/micropeak-v2.0/ao_micropeak.c | 12 +++++++++--- src/stmf0/ao_storage_stm.c | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/micropeak-v2.0/ao_micropeak.c b/src/micropeak-v2.0/ao_micropeak.c index dd86ba1c..0b9972e9 100644 --- a/src/micropeak-v2.0/ao_micropeak.c +++ b/src/micropeak-v2.0/ao_micropeak.c @@ -164,6 +164,13 @@ ao_battery_voltage(void) return 330 * stm_cal.vrefint_cal / vrefint; } +static void +ao_log_erase(void) +{ + uint32_t pos; + for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block) + ao_storage_erase(pos); +} uint8_t ao_on_battery; @@ -186,6 +193,7 @@ ao_micropeak(void) #if BOOST_DELAY ao_delay(BOOST_DELAY); #endif + ao_log_erase(); ao_microflight(); @@ -239,9 +247,7 @@ ao_log_delete(void) /* Look for the flight log matching the requested flight */ if (cmd_flight == 1 && ao_log_present()) { - uint32_t pos; - for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block) - ao_storage_erase(pos); + ao_log_erase(); puts("Erased"); return; } diff --git a/src/stmf0/ao_storage_stm.c b/src/stmf0/ao_storage_stm.c index 17c934a5..c6f1defe 100644 --- a/src/stmf0/ao_storage_stm.c +++ b/src/stmf0/ao_storage_stm.c @@ -127,9 +127,24 @@ _ao_flash_write(uint32_t pos, void *sv, uint16_t len) stm_flash.cr &= ~(1 << STM_FLASH_CR_PG); } +static bool +ao_storage_is_erased(uint32_t pos) +{ + uint16_t *flash = _ao_flash_addr(pos); + uint32_t i = ao_storage_block >> 1; + + while (i--) + if (*flash++ != 0xffff) + return false; + return true; +} + uint8_t ao_storage_erase(uint32_t pos) { + if (ao_storage_is_erased(pos)) + return 1; + ao_arch_block_interrupts(); ao_flash_unlock(); -- 2.30.2