2 * Copyright © 2020 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include <ao_micropeak.h>
22 #include <ao_adc_stm32l0.h>
23 #include <ao_report_micro.h>
24 #include <ao_log_micro.h>
26 alt_t ground_alt, max_alt;
32 uint32_t icscr = stm_rcc.icscr;
34 /* Set MSI clock to desired range */
35 icscr &= ~(STM_RCC_ICSCR_MSIRANGE_MASK << STM_RCC_ICSCR_MSIRANGE);
36 icscr |= (AO_MSI_RANGE << STM_RCC_ICSCR_MSIRANGE);
37 stm_rcc.icscr = icscr;
39 /* Set vcore to 1.2V */
40 uint32_t cr = stm_pwr.cr;
41 cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
42 cr |= (STM_PWR_CR_VOS_1_2 << STM_PWR_CR_VOS);
49 static struct ao_ms5607_value value;
51 ao_ms5607_sample(&ao_ms5607_current);
52 ao_ms5607_convert(&ao_ms5607_current, &value);
53 pa = (uint32_t) value.pres;
57 ao_compute_height(void)
59 ground_alt = ao_pa_to_altitude((pres_t) pa_ground);
60 max_alt = ao_pa_to_altitude((pres_t) pa_min);
61 ao_max_height = max_alt - ground_alt;
68 for (i = 0; i < 5; i++) {
69 ao_led_on(AO_LED_REPORT);
70 ao_delay(AO_MS_TO_TICKS(80));
71 ao_led_off(AO_LED_REPORT);
72 ao_delay(AO_MS_TO_TICKS(80));
74 ao_delay(AO_MS_TO_TICKS(200));
83 * Table 40, entering standby mode
85 * SLEEPDEEP = 1 in M0 SCR
89 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGEN);
90 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
91 stm_scb.scr |= ((1 << STM_SCB_SCR_SLEEPDEEP) |
92 (1 << STM_SCB_SCR_SLEEPONEXIT));
93 stm_pwr.cr |= (1 << STM_PWR_CR_PDDS);
94 stm_pwr.csr &= ~(1UL << STM_PWR_CSR_WUF);
95 ao_arch_wait_interrupt();
99 static bool log_stdout;
106 ao_led_off(AO_LED_REPORT);
110 for (i = 0; i < 4; i++)
111 ao_async_byte(stm_device_id.lot_num_0_3[i]);
112 for (i = 0; i < 3; i++)
113 ao_async_byte(stm_device_id.lot_num_4_6[i]);
115 ao_log_hex(stm_device_id.waf_num);
117 for (i = 0; i < 4; i++)
118 ao_log_hex(stm_device_id.unique_id[i]);
121 ao_lpuart1_disable();
129 for (pos = 0; pos < ao_storage_total; pos += STM_FLASH_PAGE_SIZE)
131 if (!ao_storage_device_is_erased(pos))
132 ao_storage_device_erase(pos);
139 /* Give the person a second to get their finger out of the way */
140 ao_delay(AO_MS_TO_TICKS(1000));
142 ao_log_micro_restore();
144 ao_report_altitude();
148 ao_delay(BOOST_DELAY);
154 ao_report_altitude();
158 void ao_async_byte(char c)
163 ao_lpuart1_putchar(c);
167 log_micro_dump_uart(void)
178 if (!ao_match_word("DoIt"))
183 const struct ao_cmds ao_micro_cmds[] = {
184 { log_micro_dump_uart, "l\0Dump log" },
185 { flight_mode, "F\0Flight mode" },
186 { power_down, "S\0Standby" },
187 { log_erase_cmd, "z <key>\0Erase. <key> is doit with D&I" },
196 ao_cmd_register(ao_micro_cmds);
210 /* Check the power supply voltage; it'll be 3.3V if
211 * the I/O board is connected
213 uint16_t vref = ao_adc_read_vref();
215 uint32_t vdda = 3UL * stm_vrefint_cal.vrefint_cal * 1000 / vref;
217 /* Power supply > 3.25V means we're on USB power */