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