altos/micropeak-v2.0: Functionally complete.
authorKeith Packard <keithp@keithp.com>
Sat, 8 Aug 2020 03:15:32 +0000 (20:15 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 8 Aug 2020 03:30:11 +0000 (20:30 -0700)
Get flight mode working correctly.
Offer log dumping from serial link.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/micropeak-v2.0/Makefile
src/micropeak-v2.0/ao_micro.c
src/micropeak-v2.0/ao_pins.h

index 1a9c07fd7d522d15fcc9cf896ab9c8351f8d2225..a662e49b16e61f1b87ab9e8efd535c3fc4227264 100644 (file)
@@ -32,6 +32,7 @@ ALTOS_SRC = \
        ao_interrupt.c \
        ao_panic.c \
        ao_micro.c \
+       ao_report_micro.c \
        ao_stdio.c \
        ao_notask.c \
        ao_serial_stm.c \
@@ -47,7 +48,6 @@ ALTOS_SRC = \
        ao_product.c \
        ao_romconfig.c \
        ao_flash_stm32l0.c \
-       ao_storage.c \
        ao_microflight.c \
        ao_log_micro.c \
        ao_microkalman.c
@@ -56,7 +56,7 @@ IDPRODUCT=0x14
 PRODUCT=MicroPeak-v2.0
 PRODUCT_DEF=-DMICROPEAK
 
-CFLAGS = $(PRODUCT_DEF) $(STML0_CFLAGS) -Wl,--gc-sections -ffunction-sections
+CFLAGS = $(PRODUCT_DEF) $(STML0_CFLAGS) -Wl,--gc-sections -ffunction-sections -fdata-sections
 
 PROGNAME=micropeak-v2.0
 PROG=$(PROGNAME)-$(VERSION).elf
index 26daa5209936b6941c7ab980548bfa58bed9547e..5a21d6582a0e0fd24b1f75d07c2329a4d2059433 100644 (file)
 #include <ao_exti.h>
 #include <ao_micropeak.h>
 #include <ao_adc_stm32l0.h>
+#include <ao_report_micro.h>
+#include <ao_log_micro.h>
 
-
-uint32_t pa;
+uint32_t       pa;
+alt_t          ground_alt, max_alt;
+alt_t          ao_max_height;
 
 static void
 ao_msi_init(void)
@@ -41,54 +44,181 @@ ao_msi_init(void)
        stm_pwr.cr = cr;
 }
 
+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);
+}
+
 static void
-list_flights(void)
+log_micro_dump_uart(void)
 {
-       printf("flight %d start %x end %x\n",
-              1, 0 >> 8, ao_storage_total >> 8);
+       log_stdout = true;
+       log_micro_dump();
+       log_stdout = false;
+}
+
+static void
+log_erase_cmd(void)
+{
+       ao_cmd_white();
+       if (!ao_match_word("DoIt"))
+               return;
+       log_erase();
 }
 
 const struct ao_cmds ao_micro_cmds[] = {
-       { list_flights, "l\0List flights" },
+       { log_micro_dump_uart, "l\0Dump log" },
+       { flight_mode, "F\0Flight mode" },
+       { power_down, "S\0Standby" },
+       { log_erase_cmd, "z <key>\0Erase. <key> is doit with D&I" },
        {}
 };
 
-void
-ao_pa_get(void)
+static void
+cmd_mode(void)
 {
-       static struct ao_ms5607_value   value;
-
-       ao_ms5607_sample(&ao_ms5607_current);
-       ao_ms5607_convert(&ao_ms5607_current, &value);
-       pa = value.pres;
+       ao_serial_init();
+       ao_cmd_init();
+       ao_cmd_register(ao_micro_cmds);
+       ao_cmd();
 }
 
 int
 main(void)
 {
        ao_msi_init();
-
        ao_led_init();
-       ao_led_on(AO_LED_ORANGE);
-
        ao_timer_init();
        ao_spi_init();
        ao_ms5607_init();
        ao_ms5607_setup();
-       ao_storage_init();
 
+       /* Check the power supply voltage; it'll be 3.3V if
+        * the I/O board is connected
+        */
        uint16_t vref = ao_adc_read_vref();
 
        uint32_t vdda = 3 * stm_vrefint_cal.vrefint_cal * 1000 / vref;
-       ao_led_off(AO_LED_ORANGE);
 
        /* Power supply > 3.25V means we're on USB power */
        if (vdda > 3250) {
-               ao_serial_init();
-               ao_cmd_init();
-               ao_cmd_register(ao_micro_cmds);
-               ao_cmd();
+               cmd_mode();
        } else {
-               ao_microflight();
+               flight_mode();
        }
 }
index 8cc1efcb5a2bf69066dfe016f3163e03a9f1a7bc..951083bd19ea2d7aca468088880faae5ef24596c 100644 (file)
@@ -121,22 +121,16 @@ ao_power_off(void) {
 
 extern alt_t ao_max_height;
 
-#define ao_async_stop() do {                                   \
-               ao_serial2_drain();                             \
-               stm_moder_set(&stm_gpioa, 2, STM_MODER_OUTPUT); \
-               ao_serial_shutdown();                           \
-       } while (0)
-
-#define ao_async_start() do {                                          \
-               ao_serial_init();                                       \
-               stm_moder_set(&stm_gpioa, 2, STM_MODER_ALTERNATE);      \
-               ao_delay(AO_MS_TO_TICKS(100));                          \
-       } while (0)
-
-#define ao_async_byte(b) ao_serial2_putchar((char) (b))
-
-#define ao_eeprom_read(pos, ptr, size) ao_storage_read(pos, ptr, size)
-#define ao_eeprom_write(pos, ptr, size) ao_storage_write(pos, ptr, size)
+#define ao_async_stop()
+#define ao_async_start()
+
+#define LOG_MICRO_ASYNC 0
+
+void ao_async_byte(char c);
+
+#define ao_eeprom_read(pos, ptr, size) ao_storage_device_read(pos, ptr, size)
+#define ao_eeprom_write(pos, ptr, size) ao_storage_device_write(pos, ptr, size)
+#define N_SAMPLES_TYPE uint32_t
 #define MAX_LOG_OFFSET ao_storage_total
 #define ao_storage_log_max ao_storage_total