altos/test: Adjust CRC error rate after FEC fix
[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 #include <ao_report_micro.h>
24 #include <ao_log_micro.h>
25
26 alt_t           ground_alt, max_alt;
27 alt_t           ao_max_height;
28
29 static void
30 ao_msi_init(void)
31 {
32         uint32_t icscr = stm_rcc.icscr;
33
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;
38
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);
43         stm_pwr.cr = cr;
44 }
45
46 void
47 ao_pa_get(void)
48 {
49         static struct ao_ms5607_value   value;
50
51         ao_ms5607_sample(&ao_ms5607_current);
52         ao_ms5607_convert(&ao_ms5607_current, &value);
53         pa = value.pres;
54 }
55
56 static void
57 ao_compute_height(void)
58 {
59         ground_alt = ao_pa_to_altitude(pa_ground);
60         max_alt = ao_pa_to_altitude(pa_min);
61         ao_max_height = max_alt - ground_alt;
62 }
63
64 static void
65 ao_pips(void)
66 {
67         uint8_t i;
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));
73         }
74         ao_delay(AO_MS_TO_TICKS(200));
75 }
76
77 static void
78 power_down(void)
79 {
80         ao_timer_stop();
81         for(;;) {
82                 /*
83                  * Table 40, entering standby mode
84                  *
85                  * SLEEPDEEP = 1 in M0 SCR
86                  * PDDS = 1
87                  * WUF = 0
88                  */
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 &= ~(1 << STM_PWR_CSR_WUF);
95                 ao_arch_wait_interrupt();
96         }
97 }
98
99 static bool log_stdout;
100
101 static void
102 log_micro_dump(void)
103 {
104         int i;
105         if (!log_stdout) {
106                 ao_led_off(AO_LED_REPORT);
107                 ao_lpuart1_enable();
108         }
109         ao_log_micro_dump();
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]);
114         ao_async_byte('-');
115         ao_log_hex(stm_device_id.waf_num);
116         ao_async_byte('-');
117         for (i = 0; i < 4; i++)
118                 ao_log_hex(stm_device_id.unique_id[i]);
119         ao_log_newline();
120         if (!log_stdout)
121                 ao_lpuart1_disable();
122 }
123
124 static void
125 log_erase(void)
126 {
127         uint32_t        pos;
128
129         for (pos = 0; pos < ao_storage_total; pos += STM_FLASH_PAGE_SIZE)
130         {
131                 if (!ao_storage_device_is_erased(pos))
132                         ao_storage_device_erase(pos);
133         }
134 }
135
136 static void
137 flight_mode(void)
138 {
139         /* Give the person a second to get their finger out of the way */
140         ao_delay(AO_MS_TO_TICKS(1000));
141
142         ao_log_micro_restore();
143         ao_compute_height();
144         ao_report_altitude();
145         ao_pips();
146         log_micro_dump();
147 #if BOOST_DELAY 
148         ao_delay(BOOST_DELAY);
149 #endif
150         log_erase();
151         ao_microflight();
152         ao_log_micro_save();
153         ao_compute_height();
154         ao_report_altitude();
155         power_down();
156 }
157
158 void ao_async_byte(char c)
159 {
160         if (log_stdout)
161                 putchar(c);
162         else
163                 ao_lpuart1_putchar(c);
164 }
165
166 static void
167 log_micro_dump_uart(void)
168 {
169         log_stdout = true;
170         log_micro_dump();
171         log_stdout = false;
172 }
173
174 static void
175 log_erase_cmd(void)
176 {
177         ao_cmd_white();
178         if (!ao_match_word("DoIt"))
179                 return;
180         log_erase();
181 }
182
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" },
188         {}
189 };
190
191 static void
192 cmd_mode(void)
193 {
194         ao_serial_init();
195         ao_cmd_init();
196         ao_cmd_register(ao_micro_cmds);
197         ao_cmd();
198 }
199
200 int
201 main(void)
202 {
203         ao_msi_init();
204         ao_led_init();
205         ao_timer_init();
206         ao_spi_init();
207         ao_ms5607_init();
208         ao_ms5607_setup();
209
210         /* Check the power supply voltage; it'll be 3.3V if
211          * the I/O board is connected
212          */
213         uint16_t vref = ao_adc_read_vref();
214
215         uint32_t vdda = 3 * stm_vrefint_cal.vrefint_cal * 1000 / vref;
216
217         /* Power supply > 3.25V means we're on USB power */
218         if (vdda > 3250) {
219                 cmd_mode();
220         } else {
221                 flight_mode();
222         }
223 }