altos: Share common logging code. Deal with corrupt initial flight records
[fw/altos] / src / kernel / ao_log.h
1 /*
2  * Copyright © 2012 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _AO_LOG_H_
20 #define _AO_LOG_H_
21
22 #include <ao_flight.h>
23
24 /*
25  * ao_log.c
26  */
27
28 /* We record flight numbers in the first record of
29  * the log. Tasks may wait for this to be initialized
30  * by sleeping on this variable.
31  */
32 extern __xdata int16_t ao_flight_number;
33 extern __xdata uint8_t  ao_log_mutex;
34 extern __pdata uint32_t ao_log_current_pos;
35 extern __pdata uint32_t ao_log_end_pos;
36 extern __pdata uint32_t ao_log_start_pos;
37 extern __xdata uint8_t  ao_log_running;
38 extern __pdata enum ao_flight_state ao_log_state;
39
40 /* required functions from the underlying log system */
41
42 #define AO_LOG_FORMAT_UNKNOWN           0       /* unknown; altosui will have to guess */
43 #define AO_LOG_FORMAT_FULL              1       /* 8 byte typed log records */
44 #define AO_LOG_FORMAT_TINY              2       /* two byte state/baro records */
45 #define AO_LOG_FORMAT_TELEMETRY         3       /* 32 byte ao_telemetry records */
46 #define AO_LOG_FORMAT_TELESCIENCE       4       /* 32 byte typed telescience records */
47 #define AO_LOG_FORMAT_TELEMEGA_OLD      5       /* 32 byte typed telemega records */
48 #define AO_LOG_FORMAT_EASYMINI1         6       /* 16-byte MS5607 baro only, 3.0V supply */
49 #define AO_LOG_FORMAT_TELEMETRUM        7       /* 16-byte typed telemetrum records */
50 #define AO_LOG_FORMAT_TELEMINI2         8       /* 16-byte MS5607 baro only, 3.3V supply, cc1111 SoC */
51 #define AO_LOG_FORMAT_TELEGPS           9       /* 32 byte telegps records */
52 #define AO_LOG_FORMAT_TELEMEGA          10      /* 32 byte typed telemega records with 32 bit gyro cal */
53 #define AO_LOG_FORMAT_DETHERM           11      /* 16-byte MS5607 baro only, no ADC */
54 #define AO_LOG_FORMAT_TELEMINI3         12      /* 16-byte MS5607 baro only, 3.3V supply, stm32f042 SoC */
55 #define AO_LOG_FORMAT_TELEFIRETWO       13      /* 32-byte test stand data */
56 #define AO_LOG_FORMAT_EASYMINI2         14      /* 16-byte MS5607 baro only, 3.3V supply, stm32f042 SoC */
57 #define AO_LOG_FORMAT_NONE              127     /* No log at all */
58
59 /* Return the flight number from the given log slot, 0 if none, -slot on failure */
60
61 int16_t
62 ao_log_flight(uint8_t slot);
63
64 /* Checksum the loaded log record */
65 uint8_t
66 ao_log_check_data(void);
67
68 /* Check to see if the loaded log record is empty */
69 uint8_t
70 ao_log_check_clear(void);
71
72 /* Check if there is valid log data at the specified location */
73 #define AO_LOG_VALID    1
74 #define AO_LOG_EMPTY    0
75 #define AO_LOG_INVALID  -1
76
77 int8_t
78 ao_log_check(uint32_t pos);
79
80 /* Flush the log */
81 void
82 ao_log_flush(void);
83
84 /* Logging thread main routine */
85 void
86 ao_log(void);
87
88 /* functions provided in ao_log.c */
89
90 /* Figure out the current flight number */
91 uint8_t
92 ao_log_scan(void) __reentrant;
93
94 /* Return the position of the start of the given log slot */
95 uint32_t
96 ao_log_pos(uint8_t slot);
97
98 /* Start logging to eeprom */
99 void
100 ao_log_start(void);
101
102 /* Stop logging */
103 void
104 ao_log_stop(void);
105
106 /* Initialize the logging system */
107 void
108 ao_log_init(void);
109
110 /* Write out the current flight number to the erase log */
111 void
112 ao_log_write_erase(uint8_t pos);
113
114 /* Returns true if there are any logs stored in eeprom */
115 uint8_t
116 ao_log_present(void);
117
118 /* Returns true if there is no more storage space available */
119 uint8_t
120 ao_log_full(void);
121
122 /*
123  * ao_log_big.c
124  */
125
126 /*
127  * The data log is recorded in the eeprom as a sequence
128  * of data packets.
129  *
130  * Each packet starts with a 4-byte header that has the
131  * packet type, the packet checksum and the tick count. Then
132  * they all contain 2 16 bit values which hold packet-specific
133  * data.
134  *
135  * For each flight, the first packet
136  * is FLIGHT packet, indicating the serial number of the
137  * device and a unique number marking the number of flights
138  * recorded by this device.
139  *
140  * During flight, data from the accelerometer and barometer
141  * are recorded in SENSOR packets, using the raw 16-bit values
142  * read from the A/D converter.
143  *
144  * Also during flight, but at a lower rate, the deployment
145  * sensors are recorded in DEPLOY packets. The goal here is to
146  * detect failure in the deployment circuits.
147  *
148  * STATE packets hold state transitions as the flight computer
149  * transitions through different stages of the flight.
150  */
151 #define AO_LOG_FLIGHT           'F'
152 #define AO_LOG_SENSOR           'A'
153 #define AO_LOG_TEMP_VOLT        'T'
154 #define AO_LOG_DEPLOY           'D'
155 #define AO_LOG_STATE            'S'
156 #define AO_LOG_GPS_TIME         'G'
157 #define AO_LOG_GPS_LAT          'N'
158 #define AO_LOG_GPS_LON          'W'
159 #define AO_LOG_GPS_ALT          'H'
160 #define AO_LOG_GPS_SAT          'V'
161 #define AO_LOG_GPS_DATE         'Y'
162 #define AO_LOG_GPS_POS          'P'
163
164 #define AO_LOG_POS_NONE         (~0UL)
165
166 struct ao_log_record {
167         char                    type;                           /* 0 */
168         uint8_t                 csum;                           /* 1 */
169         uint16_t                tick;                           /* 2 */
170         union {
171                 struct {
172                         int16_t         ground_accel;           /* 4 */
173                         uint16_t        flight;                 /* 6 */
174                 } flight;
175                 struct {
176                         int16_t         accel;                  /* 4 */
177                         int16_t         pres;                   /* 6 */
178                 } sensor;
179                 struct {
180                         int16_t         temp;
181                         int16_t         v_batt;
182                 } temp_volt;
183                 struct {
184                         int16_t         drogue;
185                         int16_t         main;
186                 } deploy;
187                 struct {
188                         uint16_t        state;
189                         uint16_t        reason;
190                 } state;
191                 struct {
192                         uint8_t         hour;
193                         uint8_t         minute;
194                         uint8_t         second;
195                         uint8_t         flags;
196                 } gps_time;
197                 int32_t         gps_latitude;
198                 int32_t         gps_longitude;
199                 struct {
200                         uint16_t        altitude_low;
201                         int16_t         altitude_high;
202                 } gps_altitude;
203                 struct {
204                         uint16_t        svid;
205                         uint8_t         unused;
206                         uint8_t         c_n;
207                 } gps_sat;
208                 struct {
209                         uint8_t         year;
210                         uint8_t         month;
211                         uint8_t         day;
212                         uint8_t         extra;
213                 } gps_date;
214                 struct {
215                         uint16_t        d0;
216                         uint16_t        d1;
217                 } anon;
218         } u;
219 };
220
221 struct ao_log_mega {
222         char                    type;                   /* 0 */
223         uint8_t                 csum;                   /* 1 */
224         uint16_t                tick;                   /* 2 */
225         union {                                         /* 4 */
226                 /* AO_LOG_FLIGHT */
227                 struct {
228                         uint16_t        flight;                 /* 4 */
229                         int16_t         ground_accel;           /* 6 */
230                         uint32_t        ground_pres;            /* 8 */
231                         int16_t         ground_accel_along;     /* 12 */
232                         int16_t         ground_accel_across;    /* 14 */
233                         int16_t         ground_accel_through;   /* 16 */
234                         int16_t         pad_18;                 /* 18 */
235                         int32_t         ground_roll;            /* 20 */
236                         int32_t         ground_pitch;           /* 24 */
237                         int32_t         ground_yaw;             /* 28 */
238                 } flight;                                       /* 32 */
239                 struct {
240                         uint16_t        flight;                 /* 4 */
241                         int16_t         ground_accel;           /* 6 */
242                         uint32_t        ground_pres;            /* 8 */
243                         int16_t         ground_accel_along;     /* 12 */
244                         int16_t         ground_accel_across;    /* 14 */
245                         int16_t         ground_accel_through;   /* 16 */
246                         int16_t         ground_roll;            /* 18 */
247                         int16_t         ground_pitch;           /* 20 */
248                         int16_t         ground_yaw;             /* 22 */
249                 } flight_old;                                   /* 24 */
250                 /* AO_LOG_STATE */
251                 struct {
252                         uint16_t        state;
253                         uint16_t        reason;
254                 } state;
255                 /* AO_LOG_SENSOR */
256                 struct {
257                         uint32_t        pres;           /* 4 */
258                         uint32_t        temp;           /* 8 */
259                         int16_t         accel_x;        /* 12 */
260                         int16_t         accel_y;        /* 14 */
261                         int16_t         accel_z;        /* 16 */
262                         int16_t         gyro_x;         /* 18 */
263                         int16_t         gyro_y;         /* 20 */
264                         int16_t         gyro_z;         /* 22 */
265                         int16_t         mag_x;          /* 24 */
266                         int16_t         mag_z;          /* 26 */
267                         int16_t         mag_y;          /* 28 */
268                         int16_t         accel;          /* 30 */
269                 } sensor;       /* 32 */
270                 /* AO_LOG_TEMP_VOLT */
271                 struct {
272                         int16_t         v_batt;         /* 4 */
273                         int16_t         v_pbatt;        /* 6 */
274                         int16_t         n_sense;        /* 8 */
275                         int16_t         sense[10];      /* 10 */
276                         uint16_t        pyro;           /* 30 */
277                 } volt;                                 /* 32 */
278                 /* AO_LOG_GPS_TIME */
279                 struct {
280                         int32_t         latitude;       /* 4 */
281                         int32_t         longitude;      /* 8 */
282                         uint16_t        altitude_low;   /* 12 */
283                         uint8_t         hour;           /* 14 */
284                         uint8_t         minute;         /* 15 */
285                         uint8_t         second;         /* 16 */
286                         uint8_t         flags;          /* 17 */
287                         uint8_t         year;           /* 18 */
288                         uint8_t         month;          /* 19 */
289                         uint8_t         day;            /* 20 */
290                         uint8_t         course;         /* 21 */
291                         uint16_t        ground_speed;   /* 22 */
292                         int16_t         climb_rate;     /* 24 */
293                         uint8_t         pdop;           /* 26 */
294                         uint8_t         hdop;           /* 27 */
295                         uint8_t         vdop;           /* 28 */
296                         uint8_t         mode;           /* 29 */
297                         int16_t         altitude_high;  /* 30 */
298                 } gps;  /* 32 */
299                 /* AO_LOG_GPS_SAT */
300                 struct {
301                         uint16_t        channels;       /* 4 */
302                         struct {
303                                 uint8_t svid;
304                                 uint8_t c_n;
305                         } sats[12];                     /* 6 */
306                 } gps_sat;                              /* 30 */
307         } u;
308 };
309
310 #define AO_LOG_MEGA_GPS_ALTITUDE(l)     ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
311 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a)       (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
312                                                  ((l)->u.gps.altitude_high = (a) >> 16), \
313                                                  (l)->u.gps.altitude_low = (a))
314
315 struct ao_log_firetwo {
316         char                    type;                   /* 0 */
317         uint8_t                 csum;                   /* 1 */
318         uint16_t                tick;                   /* 2 */
319         union {                                         /* 4 */
320                 /* AO_LOG_FLIGHT */
321                 struct {
322                         uint16_t        flight;         /* 4 */
323                 } flight;       /* 6 */
324                 /* AO_LOG_STATE */
325                 struct {
326                         uint16_t        state;          /* 4 */
327                         uint16_t        reason;         /* 6 */
328                 } state;        /* 8 */
329                 /* AO_LOG_SENSOR */
330                 struct {
331                         uint16_t        pressure;       /* 4 */
332                         uint16_t        thrust;         /* 6 */
333                         uint16_t        thermistor[4];  /* 8 */
334                 } sensor;       /* 24 */
335                 uint8_t         align[28];              /* 4 */
336         } u;    /* 32 */
337 };
338
339 struct ao_log_metrum {
340         char                    type;                   /* 0 */
341         uint8_t                 csum;                   /* 1 */
342         uint16_t                tick;                   /* 2 */
343         union {                                         /* 4 */
344                 /* AO_LOG_FLIGHT */
345                 struct {
346                         uint16_t        flight;         /* 4 */
347                         int16_t         ground_accel;   /* 6 */
348                         uint32_t        ground_pres;    /* 8 */
349                         uint32_t        ground_temp;    /* 12 */
350                 } flight;       /* 16 */
351                 /* AO_LOG_STATE */
352                 struct {
353                         uint16_t        state;          /* 4 */
354                         uint16_t        reason;         /* 6 */
355                 } state;        /* 8 */
356                 /* AO_LOG_SENSOR */
357                 struct {
358                         uint32_t        pres;           /* 4 */
359                         uint32_t        temp;           /* 8 */
360                         int16_t         accel;          /* 12 */
361                 } sensor;       /* 14 */
362                 /* AO_LOG_TEMP_VOLT */
363                 struct {
364                         int16_t         v_batt;         /* 4 */
365                         int16_t         sense_a;        /* 6 */
366                         int16_t         sense_m;        /* 8 */
367                 } volt;         /* 10 */
368                 /* AO_LOG_GPS_POS */
369                 struct {
370                         int32_t         latitude;       /* 4 */
371                         int32_t         longitude;      /* 8 */
372                         uint16_t        altitude_low;   /* 12 */
373                         int16_t         altitude_high;  /* 14 */
374                 } gps;          /* 16 */
375                 /* AO_LOG_GPS_TIME */
376                 struct {
377                         uint8_t         hour;           /* 4 */
378                         uint8_t         minute;         /* 5 */
379                         uint8_t         second;         /* 6 */
380                         uint8_t         flags;          /* 7 */
381                         uint8_t         year;           /* 8 */
382                         uint8_t         month;          /* 9 */
383                         uint8_t         day;            /* 10 */
384                         uint8_t         pdop;           /* 11 */
385                 } gps_time;     /* 12 */
386                 /* AO_LOG_GPS_SAT (up to three packets) */
387                 struct {
388                         uint8_t channels;               /* 4 */
389                         uint8_t more;                   /* 5 */
390                         struct {
391                                 uint8_t svid;
392                                 uint8_t c_n;
393                         } sats[4];                      /* 6 */
394                 } gps_sat;                              /* 14 */
395                 uint8_t         raw[12];                /* 4 */
396         } u;    /* 16 */
397 };
398
399 struct ao_log_mini {
400         char            type;                           /* 0 */
401         uint8_t         csum;                           /* 1 */
402         uint16_t        tick;                           /* 2 */
403         union {                                         /* 4 */
404                 /* AO_LOG_FLIGHT */
405                 struct {
406                         uint16_t        flight;         /* 4 */
407                         uint16_t        r6;
408                         uint32_t        ground_pres;    /* 8 */
409                 } flight;
410                 /* AO_LOG_STATE */
411                 struct {
412                         uint16_t        state;          /* 4 */
413                         uint16_t        reason;         /* 6 */
414                 } state;
415                 /* AO_LOG_SENSOR */
416                 struct {
417                         uint8_t         pres[3];        /* 4 */
418                         uint8_t         temp[3];        /* 7 */
419                         int16_t         sense_a;        /* 10 */
420                         int16_t         sense_m;        /* 12 */
421                         int16_t         v_batt;         /* 14 */
422                 } sensor;                               /* 16 */
423         } u;                                            /* 16 */
424 };                                                      /* 16 */
425
426 #define ao_log_pack24(dst,value) do {           \
427                 (dst)[0] = (value);             \
428                 (dst)[1] = (value) >> 8;        \
429                 (dst)[2] = (value) >> 16;       \
430         } while (0)
431
432 struct ao_log_gps {
433         char                    type;                   /* 0 */
434         uint8_t                 csum;                   /* 1 */
435         uint16_t                tick;                   /* 2 */
436         union {                                         /* 4 */
437                 /* AO_LOG_FLIGHT */
438                 struct {
439                         uint16_t        flight;                 /* 4 */
440                         int16_t         start_altitude;         /* 6 */
441                         int32_t         start_latitude;         /* 8 */
442                         int32_t         start_longitude;        /* 12 */
443                 } flight;                                       /* 16 */
444                 /* AO_LOG_GPS_TIME */
445                 struct {
446                         int32_t         latitude;       /* 4 */
447                         int32_t         longitude;      /* 8 */
448                         uint16_t        altitude_low;   /* 12 */
449                         uint8_t         hour;           /* 14 */
450                         uint8_t         minute;         /* 15 */
451                         uint8_t         second;         /* 16 */
452                         uint8_t         flags;          /* 17 */
453                         uint8_t         year;           /* 18 */
454                         uint8_t         month;          /* 19 */
455                         uint8_t         day;            /* 20 */
456                         uint8_t         course;         /* 21 */
457                         uint16_t        ground_speed;   /* 22 */
458                         int16_t         climb_rate;     /* 24 */
459                         uint8_t         pdop;           /* 26 */
460                         uint8_t         hdop;           /* 27 */
461                         uint8_t         vdop;           /* 28 */
462                         uint8_t         mode;           /* 29 */
463                         int16_t         altitude_high;  /* 30 */
464                 } gps;  /* 31 */
465                 /* AO_LOG_GPS_SAT */
466                 struct {
467                         uint16_t        channels;       /* 4 */
468                         struct {
469                                 uint8_t svid;
470                                 uint8_t c_n;
471                         } sats[12];                     /* 6 */
472                 } gps_sat;                              /* 30 */
473         } u;
474 };
475
476 #if AO_LOG_FORMAT == AO_LOG_FOMAT_TELEMEGA_OLD || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA
477 typedef struct ao_log_mega ao_log_type;
478 #endif
479
480 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMETRUM
481 typedef struct ao_log_metrum ao_log_type;
482 #endif
483
484 #if AO_LOG_FORMAT == AO_LOG_FORMAT_EASYMINI1 || AO_LOG_FORMAT == AO_LOG_FORMAT_EASYMINI2 || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMINI2 || AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMINI3
485 typedef struct ao_log_mini ao_log_type;
486 #endif
487
488 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEGPS
489 typedef struct ao_log_gps ao_log_type;
490 #endif
491
492 #if AO_LOG_FORMAT == AO_LOG_FORMAT_FULL
493 typedef struct ao_log_record ao_log_type;
494 #endif
495
496 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TINY
497 #define AO_LOG_UNCOMMON 1
498 #endif
499
500 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMETRY
501 #define AO_LOG_UNCOMMON 1
502 #endif
503
504 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELESCIENCE
505 #define AO_LOG_UNCOMMON 1
506 #endif
507
508 #ifndef AO_LOG_UNCOMMON
509 extern __xdata ao_log_type log;
510
511 #define AO_LOG_SIZE sizeof(ao_log_type)
512
513 /* Write a record to the eeprom log */
514
515 uint8_t
516 ao_log_write(__xdata ao_log_type *log) __reentrant;
517 #endif
518
519 void
520 ao_log_flush(void);
521
522 void
523 ao_gps_report_metrum_init(void);
524
525 #endif /* _AO_LOG_H_ */