+void
+ao_pa_get(void)
+{
+ static struct ao_ms5607_value value;
+
+ ao_ms5607_sample(&ao_ms5607_current);
+ ao_ms5607_convert(&ao_ms5607_current, &value);
+ pa = value.pres;
+}
+
+static void
+ao_compute_height(void)
+{
+ ground_alt = ao_pa_to_altitude(pa_ground);
+ max_alt = ao_pa_to_altitude(pa_min);
+ ao_max_height = max_alt - ground_alt;
+}
+
+static void
+ao_pips(void)
+{
+ uint8_t i;
+ for (i = 0; i < 5; i++) {
+ ao_led_on(AO_LED_REPORT);
+ ao_delay(AO_MS_TO_TICKS(80));
+ ao_led_off(AO_LED_REPORT);
+ ao_delay(AO_MS_TO_TICKS(80));
+ }
+ ao_delay(AO_MS_TO_TICKS(200));
+}
+
+static void
+power_down(void)
+{
+ ao_timer_stop();
+ for(;;) {
+ /*
+ * Table 40, entering standby mode
+ *
+ * SLEEPDEEP = 1 in M0 SCR
+ * PDDS = 1
+ * WUF = 0
+ */
+ stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGEN);
+ stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
+ stm_scb.scr |= ((1 << STM_SCB_SCR_SLEEPDEEP) |
+ (1 << STM_SCB_SCR_SLEEPONEXIT));
+ stm_pwr.cr |= (1 << STM_PWR_CR_PDDS);
+ stm_pwr.csr &= ~(1 << STM_PWR_CSR_WUF);
+ ao_arch_wait_interrupt();
+ }
+}
+
+static bool log_stdout;
+
+static void
+log_micro_dump(void)
+{
+ int i;
+ if (!log_stdout) {
+ ao_led_off(AO_LED_REPORT);
+ ao_lpuart1_enable();
+ }
+ ao_log_micro_dump();
+ for (i = 0; i < 4; i++)
+ ao_async_byte(stm_device_id.lot_num_0_3[i]);
+ for (i = 0; i < 3; i++)
+ ao_async_byte(stm_device_id.lot_num_4_6[i]);
+ ao_async_byte('-');
+ ao_log_hex(stm_device_id.waf_num);
+ ao_async_byte('-');
+ for (i = 0; i < 4; i++)
+ ao_log_hex(stm_device_id.unique_id[i]);
+ ao_log_newline();
+ if (!log_stdout)
+ ao_lpuart1_disable();
+}
+
+static void
+log_erase(void)
+{
+ uint32_t pos;
+
+ for (pos = 0; pos < ao_storage_total; pos += STM_FLASH_PAGE_SIZE)
+ {
+ if (!ao_storage_device_is_erased(pos))
+ ao_storage_device_erase(pos);
+ }
+}
+
+static void
+flight_mode(void)
+{
+ /* Give the person a second to get their finger out of the way */
+ ao_delay(AO_MS_TO_TICKS(1000));
+
+ ao_log_micro_restore();
+ ao_compute_height();
+ ao_report_altitude();
+ ao_pips();
+ log_micro_dump();
+#if BOOST_DELAY
+ ao_delay(BOOST_DELAY);
+#endif
+ log_erase();
+ ao_microflight();
+ ao_log_micro_save();
+ ao_compute_height();
+ ao_report_altitude();
+ power_down();
+}
+
+void ao_async_byte(char c)
+{
+ if (log_stdout)
+ putchar(c);
+ else
+ ao_lpuart1_putchar(c);
+}
+