altos/micropeak-v2: Erase log space at end of BOOST_DELAY
authorKeith Packard <keithp@keithp.com>
Wed, 19 Jun 2019 06:16:12 +0000 (23:16 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 19 Jun 2019 06:16:12 +0000 (23:16 -0700)
We have to erase the log before the flight starts, so this seems like
the latest possible moment.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/micropeak-v2.0/ao_micropeak.c
src/stmf0/ao_storage_stm.c

index dd86ba1c70c2368fbbde98da38a7194bc79a8491..0b9972e908b1687e8741f2187fed64211b056806 100644 (file)
@@ -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;
        }
index 17c934a53e5b4a06f3ec9b45c523a55c5bf5191c..c6f1defe8e67b3f8efe97a3424804e9eac470126 100644 (file)
@@ -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();