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