altos: add initial support for TeleFireOne v2.0
[fw/altos] / src / kernel / ao_log_fireone.c
1 /*
2  * Copyright © 2017 Bdale Garbee <bdale@gag.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_log.h>
21 #include <ao_data.h>
22 #include <ao_flight.h>
23
24 static struct ao_log_firetwo log;
25
26 const uint8_t ao_log_format = AO_LOG_FORMAT_TELEFIRETWO;
27
28 static uint8_t
29 ao_log_csum(uint8_t *b) 
30 {
31         uint8_t sum = 0x5a;
32         uint8_t i;
33
34         for (i = 0; i < sizeof (struct ao_log_firetwo); i++)
35                 sum += *b++;
36         return -sum;
37 }
38
39 uint8_t
40 ao_log_firetwo(struct ao_log_firetwo *log) 
41 {
42         uint8_t wrote = 0;
43         /* set checksum */
44         log->csum = 0;
45         log->csum = ao_log_csum((uint8_t *) log);
46         ao_mutex_get(&ao_log_mutex); {
47                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
48                         ao_log_stop();
49                 if (ao_log_running) {
50                         wrote = 1;
51                         ao_storage_write(ao_log_current_pos,
52                                          log,
53                                          sizeof (struct ao_log_firetwo));
54                         ao_log_current_pos += sizeof (struct ao_log_firetwo);
55                 }
56         } ao_mutex_put(&ao_log_mutex);
57         return wrote;
58 }
59
60 #if HAS_ADC
61 static uint8_t  ao_log_data_pos;
62
63 /* a hack to make sure that ao_log_metrums fill the eeprom block in even units */
64 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_firetwo))] ;
65 #endif
66
67 void
68 ao_log(void)
69 {
70         uint16_t ao_flight_state = ao_flight_startup;
71
72         ao_storage_setup();
73
74         do {
75                 ao_log_scan();
76         
77                 while (!ao_log_running)
78                         ao_sleep(&ao_log_running);
79         
80                 log.type = AO_LOG_FLIGHT;
81                 log.tick = ao_time();
82                 log.u.flight.flight = ao_flight_number;
83                 ao_log_firetwo(&log);
84
85                 /* Write the whole contents of the ring to the log
86                 * when starting up.
87                 */
88                 ao_log_data_pos = ao_data_ring_next(ao_data_head);
89                 ao_log_state = ao_flight_startup;
90                 for (;;) {
91                         /* Write samples to EEPROM */
92                         while (ao_log_data_pos != ao_data_head) {
93                                 log.tick = ao_data_ring[ao_log_data_pos].tick;
94                                 log.type = AO_LOG_SENSOR;
95                                 log.u.sensor.pressure = ao_data_ring[ao_log_data_pos].adc.pressure;
96                                 log.u.sensor.thrust = ao_data_ring[ao_log_data_pos].adc.thrust;
97         //                      for (i = 0; i < 4; i++) {
98         //                              log.u.sensor.thermistor[i] = ao_data_ring[ao_log_data_pos].sensor.thermistor[i];
99         //                      }
100                                 ao_log_firetwo(&log);
101                                 ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
102                         }
103                         /* Write state change to EEPROM */
104                         if (ao_flight_state != ao_log_state) {
105                                 ao_log_state = ao_flight_state;
106                                 log.type = AO_LOG_STATE;
107                                 log.tick = ao_time();
108                                 log.u.state.state = ao_log_state;
109                                 log.u.state.reason = 0;
110                                 ao_log_firetwo(&log);
111         
112                                 if (ao_log_state == ao_flight_landed)
113                                         ao_log_stop();
114                         }
115         
116                         ao_log_flush();
117
118                         if (!ao_log_running) break;
119
120                         /* Wait for a while */
121                         ao_delay(AO_MS_TO_TICKS(100));
122                 }
123         } while (ao_log_running);
124 }
125