altos/micropeak-v2.0: Finish hardware bring-up
[fw/altos] / src / micropeak-v2.0 / ao_micro.c
1 /*
2  * Copyright © 2020 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_exti.h>
21 #include <ao_micropeak.h>
22 #include <ao_adc_stm32l0.h>
23
24
25 uint32_t pa;
26
27 static void
28 ao_msi_init(void)
29 {
30         uint32_t icscr = stm_rcc.icscr;
31
32         /* Set MSI clock to desired range */
33         icscr &= ~(STM_RCC_ICSCR_MSIRANGE_MASK << STM_RCC_ICSCR_MSIRANGE);
34         icscr |= (AO_MSI_RANGE << STM_RCC_ICSCR_MSIRANGE);
35         stm_rcc.icscr = icscr;
36
37         /* Set vcore to 1.2V */
38         uint32_t cr = stm_pwr.cr;
39         cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
40         cr |= (STM_PWR_CR_VOS_1_2 << STM_PWR_CR_VOS);
41         stm_pwr.cr = cr;
42 }
43
44 static void
45 list_flights(void)
46 {
47         printf("flight %d start %x end %x\n",
48                1, 0 >> 8, ao_storage_total >> 8);
49 }
50
51 const struct ao_cmds ao_micro_cmds[] = {
52         { list_flights, "l\0List flights" },
53         {}
54 };
55
56 void
57 ao_pa_get(void)
58 {
59         static struct ao_ms5607_value   value;
60
61         ao_ms5607_sample(&ao_ms5607_current);
62         ao_ms5607_convert(&ao_ms5607_current, &value);
63         pa = value.pres;
64 }
65
66 int
67 main(void)
68 {
69         ao_msi_init();
70
71         ao_led_init();
72         ao_led_on(AO_LED_ORANGE);
73
74         ao_timer_init();
75         ao_spi_init();
76         ao_ms5607_init();
77         ao_ms5607_setup();
78         ao_storage_init();
79
80         uint16_t vref = ao_adc_read_vref();
81
82         uint32_t vdda = 3 * stm_vrefint_cal.vrefint_cal * 1000 / vref;
83         ao_led_off(AO_LED_ORANGE);
84
85         /* Power supply > 3.25V means we're on USB power */
86         if (vdda > 3250) {
87                 ao_serial_init();
88                 ao_cmd_init();
89                 ao_cmd_register(ao_micro_cmds);
90                 ao_cmd();
91         } else {
92                 ao_microflight();
93         }
94 }