altos: Make serial, usb, beeper and accelerometer optional components
[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_end_pos;
22 static __pdata uint32_t ao_log_start_pos;
23 static __xdata uint8_t  ao_log_running;
24 static __xdata uint8_t  ao_log_mutex;
25
26 static uint8_t
27 ao_log_csum(__xdata uint8_t *b) __reentrant
28 {
29         uint8_t sum = 0x5a;
30         uint8_t i;
31
32         for (i = 0; i < sizeof (struct ao_log_record); i++)
33                 sum += *b++;
34         return -sum;
35 }
36
37 uint8_t
38 ao_log_data(__xdata struct ao_log_record *log) __reentrant
39 {
40         uint8_t wrote = 0;
41         /* set checksum */
42         log->csum = 0;
43         log->csum = ao_log_csum((__xdata uint8_t *) log);
44         ao_mutex_get(&ao_log_mutex); {
45                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
46                         ao_log_stop();
47                 if (ao_log_running) {
48                         wrote = 1;
49                         ao_storage_write(ao_log_current_pos,
50                                          log,
51                                          sizeof (struct ao_log_record));
52                         ao_log_current_pos += sizeof (struct ao_log_record);
53                 }
54         } ao_mutex_put(&ao_log_mutex);
55         return wrote;
56 }
57
58 void
59 ao_log_flush(void)
60 {
61         ao_storage_flush();
62 }
63
64 static void ao_log_scan(void);
65
66 __xdata struct ao_log_record log;
67 __xdata uint16_t ao_flight_number;
68
69 static uint8_t
70 ao_log_dump_check_data(void)
71 {
72         if (ao_log_csum((uint8_t *) &log) != 0)
73                 return 0;
74         return 1;
75 }
76
77 __xdata uint8_t ao_log_adc_pos;
78 __xdata enum flight_state ao_log_state;
79
80 /* a hack to make sure that ao_log_records fill the eeprom block in even units */
81 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_record))] ;
82
83 void
84 ao_log(void)
85 {
86         ao_storage_setup();
87
88         ao_log_scan();
89
90         while (!ao_log_running)
91                 ao_sleep(&ao_log_running);
92
93         log.type = AO_LOG_FLIGHT;
94         log.tick = ao_flight_tick;
95 #if HAS_ACCEL
96         log.u.flight.ground_accel = ao_ground_accel;
97 #endif
98         log.u.flight.flight = ao_flight_number;
99         ao_log_data(&log);
100
101         /* Write the whole contents of the ring to the log
102          * when starting up.
103          */
104         ao_log_adc_pos = ao_adc_ring_next(ao_flight_adc);
105         for (;;) {
106                 /* Write samples to EEPROM */
107                 while (ao_log_adc_pos != ao_flight_adc) {
108                         log.type = AO_LOG_SENSOR;
109                         log.tick = ao_adc_ring[ao_log_adc_pos].tick;
110                         log.u.sensor.accel = ao_adc_ring[ao_log_adc_pos].accel;
111                         log.u.sensor.pres = ao_adc_ring[ao_log_adc_pos].pres;
112                         ao_log_data(&log);
113                         if ((ao_log_adc_pos & 0x1f) == 0) {
114                                 log.type = AO_LOG_TEMP_VOLT;
115                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
116                                 log.u.temp_volt.temp = ao_adc_ring[ao_log_adc_pos].temp;
117                                 log.u.temp_volt.v_batt = ao_adc_ring[ao_log_adc_pos].v_batt;
118                                 ao_log_data(&log);
119                                 log.type = AO_LOG_DEPLOY;
120                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
121                                 log.u.deploy.drogue = ao_adc_ring[ao_log_adc_pos].sense_d;
122                                 log.u.deploy.main = ao_adc_ring[ao_log_adc_pos].sense_m;
123                                 ao_log_data(&log);
124                         }
125                         ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
126                 }
127                 /* Write state change to EEPROM */
128                 if (ao_flight_state != ao_log_state) {
129                         ao_log_state = ao_flight_state;
130                         log.type = AO_LOG_STATE;
131                         log.tick = ao_flight_tick;
132                         log.u.state.state = ao_log_state;
133                         log.u.state.reason = 0;
134                         ao_log_data(&log);
135
136                         if (ao_log_state == ao_flight_landed)
137                                 ao_log_stop();
138                 }
139
140                 /* Wait for a while */
141                 ao_delay(AO_MS_TO_TICKS(100));
142
143                 /* Stop logging when told to */
144                 while (!ao_log_running)
145                         ao_sleep(&ao_log_running);
146         }
147 }
148
149 /*
150  * When erasing a flight log, make sure the config block
151  * has an up-to-date version of the current flight number
152  */
153
154 struct ao_log_erase {
155         uint8_t unused;
156         uint16_t flight;
157 };
158
159 static __xdata struct ao_log_erase erase;
160
161 #define LOG_MAX_ERASE   16
162
163 static uint32_t
164 ao_log_erase_pos(uint8_t i)
165 {
166         return i * sizeof (struct ao_log_erase) + AO_STORAGE_ERASE_LOG;
167 }
168
169 void
170 ao_log_write_erase(uint8_t pos)
171 {
172         erase.unused = 0x00;
173         erase.flight = ao_flight_number;
174         ao_storage_write(ao_log_erase_pos(pos),  &erase, sizeof (erase));
175         ao_storage_flush();
176 }
177
178 static void
179 ao_log_read_erase(uint8_t pos)
180 {
181         ao_storage_read(ao_log_erase_pos(pos), &erase, sizeof (erase));
182 }
183
184
185 static void
186 ao_log_erase_mark(void)
187 {
188         uint8_t                         i;
189
190         for (i = 0; i < LOG_MAX_ERASE; i++) {
191                 ao_log_read_erase(i);
192                 if (erase.unused == 0 && erase.flight == ao_flight_number)
193                         return;
194                 if (erase.unused == 0xff) {
195                         ao_log_write_erase(i);
196                         return;
197                 }
198         }
199         ao_config_put();
200 }
201
202 static uint8_t
203 ao_log_slots()
204 {
205         return (uint8_t) (ao_storage_config / ao_config.flight_log_max);
206 }
207
208 static uint32_t
209 ao_log_pos(uint8_t slot)
210 {
211         return ((slot) * ao_config.flight_log_max);
212 }
213
214 static uint16_t
215 ao_log_flight(uint8_t slot)
216 {
217         if (!ao_storage_read(ao_log_pos(slot),
218                              &log,
219                              sizeof (struct ao_log_record)))
220                 return 0;
221
222         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
223                 return log.u.flight.flight;
224         return 0;
225 }
226
227 static uint16_t
228 ao_log_max_flight(void)
229 {
230         uint8_t         log_slot;
231         uint8_t         log_slots;
232         uint16_t        log_flight;
233         uint16_t        max_flight = 0;
234
235         /* Scan the log space looking for the biggest flight number */
236         log_slots = ao_log_slots();
237         for (log_slot = 0; log_slot < log_slots; log_slot++) {
238                 log_flight = ao_log_flight(log_slot);
239                 if (!log_flight)
240                         continue;
241                 if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0)
242                         max_flight = log_flight;
243         }
244         return max_flight;
245 }
246
247 static void
248 ao_log_scan(void) __reentrant
249 {
250         uint8_t         log_slot;
251         uint8_t         log_slots;
252         uint8_t         log_want;
253
254         ao_config_get();
255
256         ao_flight_number = ao_log_max_flight();
257         if (ao_flight_number)
258                 if (++ao_flight_number == 0)
259                         ao_flight_number = 1;
260
261         /* Now look through the log of flight numbers from erase operations and
262          * see if the last one is bigger than what we found above
263          */
264         for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) {
265                 ao_log_read_erase(log_slot);
266                 if (erase.unused == 0) {
267                         if (ao_flight_number == 0 ||
268                             (int16_t) (erase.flight - ao_flight_number) > 0)
269                                 ao_flight_number = erase.flight;
270                         break;
271                 }
272         }
273         if (ao_flight_number == 0)
274                 ao_flight_number = 1;
275
276         /* With a flight number in hand, find a place to write a new log,
277          * use the target flight number to index the available log slots so
278          * that we write logs to each spot about the same number of times.
279          */
280
281         /* Find a log slot for the next flight, if available */
282         ao_log_current_pos = ao_log_end_pos = 0;
283         log_slots = ao_log_slots();
284         log_want = (ao_flight_number - 1) % log_slots;
285         log_slot = log_want;
286         do {
287                 if (ao_log_flight(log_slot) == 0) {
288                         ao_log_current_pos = ao_log_pos(log_slot);
289                         ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
290                         break;
291                 }
292                 if (++log_slot >= log_slots)
293                         log_slot = 0;
294         } while (log_slot != log_want);
295
296         ao_wakeup(&ao_flight_number);
297 }
298
299 void
300 ao_log_start(void)
301 {
302         /* start logging */
303         ao_log_running = 1;
304         ao_wakeup(&ao_log_running);
305 }
306
307 void
308 ao_log_stop(void)
309 {
310         ao_log_running = 0;
311         ao_log_flush();
312 }
313
314 uint8_t
315 ao_log_present(void)
316 {
317         return ao_log_max_flight() != 0;
318 }
319
320 uint8_t
321 ao_log_full(void)
322 {
323         return ao_log_current_pos == ao_log_end_pos;
324 }
325
326 static __xdata struct ao_task ao_log_task;
327
328 void
329 ao_log_list(void) __reentrant
330 {
331         uint8_t slot;
332         uint8_t slots;
333         uint16_t flight;
334
335         slots = ao_log_slots();
336         for (slot = 0; slot < slots; slot++)
337         {
338                 flight = ao_log_flight(slot);
339                 if (flight)
340                         printf ("flight %d start %x end %x\n",
341                                 flight,
342                                 (uint16_t) (ao_log_pos(slot) >> 8),
343                                 (uint16_t) (ao_log_pos(slot+1) >> 8));
344         }
345         printf ("done\n");
346 }
347
348 void
349 ao_log_delete(void) __reentrant
350 {
351         uint8_t slot;
352         uint8_t slots;
353
354         ao_cmd_decimal();
355         if (ao_cmd_status != ao_cmd_success)
356                 return;
357
358         slots = ao_log_slots();
359         /* Look for the flight log matching the requested flight */
360         if (ao_cmd_lex_i) {
361                 for (slot = 0; slot < slots; slot++) {
362                         if (ao_log_flight(slot) == ao_cmd_lex_i) {
363                                 ao_log_erase_mark();
364                                 ao_log_current_pos = ao_log_pos(slot);
365                                 ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max;
366                                 while (ao_log_current_pos < ao_log_end_pos) {
367                                         /*
368                                          * Check to see if we've reached the end of
369                                          * the used memory to avoid re-erasing the same
370                                          * memory over and over again
371                                          */
372                                         if (ao_storage_read(ao_log_current_pos,
373                                                             &log,
374                                                             sizeof (struct ao_log_record))) {
375                                                 for (slot = 0; slot < sizeof (struct ao_log_record); slot++)
376                                                         if (((uint8_t *) &log)[slot] != 0xff)
377                                                                 break;
378                                                 if (slot == sizeof (struct ao_log_record))
379                                                         break;
380                                         }
381                                         ao_storage_erase(ao_log_current_pos);
382                                         ao_log_current_pos += ao_storage_block;
383                                 }
384                                 puts("Erased");
385                                 return;
386                         }
387                 }
388         }
389         printf("No such flight: %d\n", ao_cmd_lex_i);
390 }
391
392
393
394 __code struct ao_cmds ao_log_cmds[] = {
395         { ao_log_list,  "l\0List stored flight logs" },
396         { ao_log_delete,        "d <flight-number>\0Delete stored flight" },
397         { 0,    NULL },
398 };
399
400 void
401 ao_log_init(void)
402 {
403         ao_log_running = 0;
404
405         /* For now, just log the flight starting at the begining of eeprom */
406         ao_log_state = ao_flight_invalid;
407
408         ao_cmd_register(&ao_log_cmds[0]);
409
410         /* Create a task to log events to eeprom */
411         ao_add_task(&ao_log_task, ao_log, "log");
412 }