Don't re-initialize the landing range data at each apogee detect sample
[fw/altos] / 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(uint8_t *b)
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 void
37 ao_log_data(struct ao_log_record *log)
38 {
39         /* set checksum */
40         log->csum = 0;
41         log->csum = ao_log_csum((uint8_t *) log);
42         ao_mutex_get(&ao_log_mutex); {
43                 if (ao_log_running) {
44                         ao_ee_write(ao_log_current_pos,
45                                     (uint8_t *) log,
46                                     sizeof (struct ao_log_record));
47                         ao_log_current_pos += sizeof (struct ao_log_record);
48                         if (ao_log_current_pos >= AO_EE_DATA_SIZE)
49                                 ao_log_current_pos = 0;
50                         if (ao_log_current_pos == ao_log_start_pos)
51                                 ao_log_running = 0;
52                 }
53         } ao_mutex_put(&ao_log_mutex);
54 }
55
56 void
57 ao_log_flush(void)
58 {
59         ao_ee_flush();
60 }
61
62 __xdata struct ao_log_record ao_log_dump;
63 static __xdata uint16_t ao_log_dump_flight;
64 static __xdata uint32_t ao_log_dump_pos;
65
66 static uint8_t
67 ao_log_dump_check_data(void)
68 {
69         if (ao_log_csum((uint8_t *) &ao_log_dump) != 0)
70                 return 0;
71         return 1;
72 }
73
74 static uint8_t
75 ao_log_dump_scan(void)
76 {
77         if (!ao_ee_read(0, (uint8_t *) &ao_log_dump, sizeof (struct ao_log_record)))
78                 ao_panic(AO_PANIC_LOG);
79         if (ao_log_dump_check_data() && ao_log_dump.type == AO_LOG_FLIGHT) {
80                 ao_log_dump_flight = ao_log_dump.u.flight.flight;
81                 return 1;
82         } else {
83                 ao_log_dump_flight = 0;
84                 return 0;
85         }
86 }
87
88 uint8_t
89 ao_log_dump_first(void)
90 {
91         ao_log_dump_pos = 0;
92         if (!ao_log_dump_scan())
93                 return 0;
94         return 1;
95 }
96
97 uint8_t
98 ao_log_dump_next(void)
99 {
100         ao_log_dump_pos += sizeof (struct ao_log_record);
101         if (ao_log_dump_pos >= AO_EE_DEVICE_SIZE)
102                 return 0;
103         if (!ao_ee_read(ao_log_dump_pos, (uint8_t *) &ao_log_dump,
104                         sizeof (struct ao_log_record)))
105                 return 0;
106         return ao_log_dump_check_data();
107 }
108
109 __xdata uint8_t ao_log_adc_pos;
110 __xdata enum flight_state ao_log_state;
111
112 void
113 ao_log(void)
114 {
115         static __xdata struct ao_log_record     log;
116         
117         ao_log_dump_scan();
118
119         while (!ao_log_running)
120                 ao_sleep(&ao_log_running);
121         
122         log.type = AO_LOG_FLIGHT;
123         log.tick = ao_flight_tick;
124         log.u.flight.ground_accel = ao_ground_accel;
125         log.u.flight.flight = ao_log_dump_flight + 1;
126         ao_log_data(&log);
127         for (;;) {
128                 while (!ao_log_running)
129                         ao_sleep(&ao_log_running);
130
131                 /* Write samples to EEPROM */
132                 while (ao_log_adc_pos != ao_adc_head) {
133                         log.type = AO_LOG_SENSOR;
134                         log.tick = ao_adc_ring[ao_log_adc_pos].tick;
135                         log.u.sensor.accel = ao_adc_ring[ao_log_adc_pos].accel;
136                         log.u.sensor.pres = ao_adc_ring[ao_log_adc_pos].pres;
137                         ao_log_data(&log);
138                         if ((ao_log_adc_pos & 0x1f) == 0) {
139                                 log.type = AO_LOG_TEMP_VOLT;
140                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
141                                 log.u.temp_volt.temp = ao_adc_ring[ao_log_adc_pos].temp;
142                                 log.u.temp_volt.v_batt = ao_adc_ring[ao_log_adc_pos].v_batt;
143                                 ao_log_data(&log);
144                                 log.type = AO_LOG_DEPLOY;
145                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
146                                 log.u.deploy.drogue = ao_adc_ring[ao_log_adc_pos].sense_d;
147                                 log.u.deploy.main = ao_adc_ring[ao_log_adc_pos].sense_m;
148                                 ao_log_data(&log);
149                         }
150                         ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
151                 }
152                 /* Write state change to EEPROM */
153                 if (ao_flight_state != ao_log_state) {
154                         ao_log_state = ao_flight_state;
155                         log.type = AO_LOG_STATE;
156                         log.tick = ao_flight_tick;
157                         log.u.state.state = ao_log_state;
158                         log.u.state.reason = 0;
159                         ao_log_data(&log);
160
161                         if (ao_log_state == ao_flight_landed)
162                                 ao_log_stop();
163                 }
164                 
165                 /* Wait for a while */
166                 ao_delay(AO_MS_TO_TICKS(100));
167         }
168 }
169
170 void
171 ao_log_start(void)
172 {
173         /* start logging */
174         ao_log_running = 1;
175         ao_wakeup(&ao_log_running);
176 }
177
178 void
179 ao_log_stop(void)
180 {
181         ao_log_running = 0;
182         ao_log_flush();
183 }
184
185 static void
186 dump_log(void)
187 {
188         __xdata uint8_t more;
189
190         for (more = ao_log_dump_first(); more; more = ao_log_dump_next()) {
191                 printf("%c %4x %4x %4x\n",
192                        ao_log_dump.type,
193                        ao_log_dump.tick,
194                        ao_log_dump.u.anon.d0,
195                        ao_log_dump.u.anon.d1);
196         }
197 }
198
199 __code struct ao_cmds ao_log_cmds[] = {
200         { 'l',  dump_log,               "l                                  Dump last flight log" },
201         { 0, dump_log, NULL },
202 };
203
204 static __xdata struct ao_task ao_log_task;
205
206 void
207 ao_log_init(void)
208 {
209         ao_log_running = 0;
210
211         /* For now, just log the flight starting at the begining of eeprom */
212         ao_log_start_pos = 0;
213         ao_log_current_pos = ao_log_start_pos;
214         ao_log_state = ao_flight_invalid;
215
216         /* Create a task to log events to eeprom */
217         ao_add_task(&ao_log_task, ao_log, "log");
218         ao_cmd_register(&ao_log_cmds[0]);
219 }