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