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>
27 alt_t ground_alt, max_alt;
33 uint32_t icscr = stm_rcc.icscr;
35 /* Set MSI clock to desired range */
36 icscr &= ~(STM_RCC_ICSCR_MSIRANGE_MASK << STM_RCC_ICSCR_MSIRANGE);
37 icscr |= (AO_MSI_RANGE << STM_RCC_ICSCR_MSIRANGE);
38 stm_rcc.icscr = icscr;
40 /* Set vcore to 1.2V */
41 uint32_t cr = stm_pwr.cr;
42 cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
43 cr |= (STM_PWR_CR_VOS_1_2 << STM_PWR_CR_VOS);
50 static struct ao_ms5607_value value;
52 ao_ms5607_sample(&ao_ms5607_current);
53 ao_ms5607_convert(&ao_ms5607_current, &value);
58 ao_compute_height(void)
60 ground_alt = ao_pa_to_altitude(pa_ground);
61 max_alt = ao_pa_to_altitude(pa_min);
62 ao_max_height = max_alt - ground_alt;
69 for (i = 0; i < 5; i++) {
70 ao_led_on(AO_LED_REPORT);
71 ao_delay(AO_MS_TO_TICKS(80));
72 ao_led_off(AO_LED_REPORT);
73 ao_delay(AO_MS_TO_TICKS(80));
75 ao_delay(AO_MS_TO_TICKS(200));
84 * Table 40, entering standby mode
86 * SLEEPDEEP = 1 in M0 SCR
90 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGEN);
91 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
92 stm_scb.scr |= ((1 << STM_SCB_SCR_SLEEPDEEP) |
93 (1 << STM_SCB_SCR_SLEEPONEXIT));
94 stm_pwr.cr |= (1 << STM_PWR_CR_PDDS);
95 stm_pwr.csr &= ~(1 << STM_PWR_CSR_WUF);
96 ao_arch_wait_interrupt();
100 static bool log_stdout;
107 ao_led_off(AO_LED_REPORT);
111 for (i = 0; i < 4; i++)
112 ao_async_byte(stm_device_id.lot_num_0_3[i]);
113 for (i = 0; i < 3; i++)
114 ao_async_byte(stm_device_id.lot_num_4_6[i]);
116 ao_log_hex(stm_device_id.waf_num);
118 for (i = 0; i < 4; i++)
119 ao_log_hex(stm_device_id.unique_id[i]);
122 ao_lpuart1_disable();
130 for (pos = 0; pos < ao_storage_total; pos += STM_FLASH_PAGE_SIZE)
132 if (!ao_storage_device_is_erased(pos))
133 ao_storage_device_erase(pos);
140 /* Give the person a second to get their finger out of the way */
141 ao_delay(AO_MS_TO_TICKS(1000));
143 ao_log_micro_restore();
145 ao_report_altitude();
149 ao_delay(BOOST_DELAY);
155 ao_report_altitude();
159 void ao_async_byte(char c)
164 ao_lpuart1_putchar(c);
168 log_micro_dump_uart(void)
179 if (!ao_match_word("DoIt"))
184 const struct ao_cmds ao_micro_cmds[] = {
185 { log_micro_dump_uart, "l\0Dump log" },
186 { flight_mode, "F\0Flight mode" },
187 { power_down, "S\0Standby" },
188 { log_erase_cmd, "z <key>\0Erase. <key> is doit with D&I" },
197 ao_cmd_register(ao_micro_cmds);
211 /* Check the power supply voltage; it'll be 3.3V if
212 * the I/O board is connected
214 uint16_t vref = ao_adc_read_vref();
216 uint32_t vdda = 3 * stm_vrefint_cal.vrefint_cal * 1000 / vref;
218 /* Power supply > 3.25V means we're on USB power */