AltosTelemetryReader: actually open serial port
[fw/altos] / src / ao_log.c
1 /*
2  * Copyright © 2009 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 static __pdata uint32_t ao_log_current_pos;
21 static __pdata uint32_t ao_log_start_pos;
22 static __xdata uint8_t  ao_log_running;
23 static __xdata uint8_t  ao_log_mutex;
24
25 static uint8_t
26 ao_log_csum(__xdata uint8_t *b) __reentrant
27 {
28         uint8_t sum = 0x5a;
29         uint8_t i;
30
31         for (i = 0; i < sizeof (struct ao_log_record); i++)
32                 sum += *b++;
33         return -sum;
34 }
35
36 uint8_t
37 ao_log_data(__xdata struct ao_log_record *log) __reentrant
38 {
39         uint8_t wrote = 0;
40         /* set checksum */
41         log->csum = 0;
42         log->csum = ao_log_csum((__xdata uint8_t *) log);
43         ao_mutex_get(&ao_log_mutex); {
44                 if (ao_log_running) {
45                         wrote = 1;
46                         ao_ee_write(ao_log_current_pos,
47                                     (uint8_t *) log,
48                                     sizeof (struct ao_log_record));
49                         ao_log_current_pos += sizeof (struct ao_log_record);
50                         if (ao_log_current_pos >= AO_EE_DATA_SIZE)
51                                 ao_log_current_pos = 0;
52                         if (ao_log_current_pos == ao_log_start_pos)
53                                 ao_log_running = 0;
54                 }
55         } ao_mutex_put(&ao_log_mutex);
56         return wrote;
57 }
58
59 void
60 ao_log_flush(void)
61 {
62         ao_ee_flush();
63 }
64
65 __xdata struct ao_log_record log;
66 __xdata uint16_t ao_flight_number;
67
68 static uint8_t
69 ao_log_dump_check_data(void)
70 {
71         if (ao_log_csum((uint8_t *) &log) != 0)
72                 return 0;
73         return 1;
74 }
75
76 static void
77 ao_log_scan(void)
78 {
79         if (!ao_ee_read(0, (uint8_t *) &log, sizeof (struct ao_log_record)))
80                 ao_panic(AO_PANIC_LOG);
81         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT) {
82                 ao_flight_number = log.u.flight.flight + 1;
83                 if (ao_flight_number == 0)
84                         ao_flight_number = 1;
85         } else {
86                 ao_flight_number = 1;
87         }
88         ao_wakeup(&ao_flight_number);
89 }
90
91 __xdata uint8_t ao_log_adc_pos;
92 __xdata enum flight_state ao_log_state;
93
94 /* a hack to make sure that ao_log_records fill the eeprom block in even units */
95 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_record))] ;
96
97 void
98 ao_log(void)
99 {
100         ao_log_scan();
101
102         while (!ao_log_running)
103                 ao_sleep(&ao_log_running);
104
105         log.type = AO_LOG_FLIGHT;
106         log.tick = ao_flight_tick;
107         log.u.flight.ground_accel = ao_ground_accel;
108         log.u.flight.flight = ao_flight_number;
109         ao_log_data(&log);
110
111         /* Write the whole contents of the ring to the log
112          * when starting up.
113          */
114         ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
115         for (;;) {
116                 /* Write samples to EEPROM */
117                 while (ao_log_adc_pos != ao_adc_head) {
118                         log.type = AO_LOG_SENSOR;
119                         log.tick = ao_adc_ring[ao_log_adc_pos].tick;
120                         log.u.sensor.accel = ao_adc_ring[ao_log_adc_pos].accel;
121                         log.u.sensor.pres = ao_adc_ring[ao_log_adc_pos].pres;
122                         ao_log_data(&log);
123                         if ((ao_log_adc_pos & 0x1f) == 0) {
124                                 log.type = AO_LOG_TEMP_VOLT;
125                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
126                                 log.u.temp_volt.temp = ao_adc_ring[ao_log_adc_pos].temp;
127                                 log.u.temp_volt.v_batt = ao_adc_ring[ao_log_adc_pos].v_batt;
128                                 ao_log_data(&log);
129                                 log.type = AO_LOG_DEPLOY;
130                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
131                                 log.u.deploy.drogue = ao_adc_ring[ao_log_adc_pos].sense_d;
132                                 log.u.deploy.main = ao_adc_ring[ao_log_adc_pos].sense_m;
133                                 ao_log_data(&log);
134                         }
135                         ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
136                 }
137                 /* Write state change to EEPROM */
138                 if (ao_flight_state != ao_log_state) {
139                         ao_log_state = ao_flight_state;
140                         log.type = AO_LOG_STATE;
141                         log.tick = ao_flight_tick;
142                         log.u.state.state = ao_log_state;
143                         log.u.state.reason = 0;
144                         ao_log_data(&log);
145
146                         if (ao_log_state == ao_flight_landed)
147                                 ao_log_stop();
148                 }
149
150                 /* Wait for a while */
151                 ao_delay(AO_MS_TO_TICKS(100));
152
153                 /* Stop logging when told to */
154                 while (!ao_log_running)
155                         ao_sleep(&ao_log_running);
156         }
157 }
158
159 void
160 ao_log_start(void)
161 {
162         /* start logging */
163         ao_log_running = 1;
164         ao_wakeup(&ao_log_running);
165 }
166
167 void
168 ao_log_stop(void)
169 {
170         ao_log_running = 0;
171         ao_log_flush();
172 }
173
174 static __xdata struct ao_task ao_log_task;
175
176 void
177 ao_log_init(void)
178 {
179         ao_log_running = 0;
180
181         /* For now, just log the flight starting at the begining of eeprom */
182         ao_log_start_pos = 0;
183         ao_log_current_pos = ao_log_start_pos;
184         ao_log_state = ao_flight_invalid;
185
186         /* Create a task to log events to eeprom */
187         ao_add_task(&ao_log_task, ao_log, "log");
188 }