altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / ao-tools / lib / ao-eeprom-read.h
1 /*
2  * Copyright © 2017 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
15 #ifndef _AO_EEPROM_READ_H_
16 #define _AO_EEPROM_READ_H_
17
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <ao-ms5607.h>
21
22 #define AO_MAX_CALLSIGN 8
23 #define AO_AES_LEN      16
24 #define AO_PYRO_NUM     8
25
26 /* required functions from the underlying log system */
27
28 #define AO_LOG_FORMAT_UNKNOWN           0       /* unknown; altosui will have to guess */
29 #define AO_LOG_FORMAT_FULL              1       /* 8 byte typed log records */
30 #define AO_LOG_FORMAT_TINY              2       /* two byte state/baro records */
31 #define AO_LOG_FORMAT_TELEMETRY         3       /* 32 byte ao_telemetry records */
32 #define AO_LOG_FORMAT_TELESCIENCE       4       /* 32 byte typed telescience records */
33 #define AO_LOG_FORMAT_TELEMEGA_OLD      5       /* 32 byte typed telemega records */
34 #define AO_LOG_FORMAT_EASYMINI1         6       /* 16-byte MS5607 baro only, 3.0V supply */
35 #define AO_LOG_FORMAT_TELEMETRUM        7       /* 16-byte typed telemetrum records */
36 #define AO_LOG_FORMAT_TELEMINI2         8       /* 16-byte MS5607 baro only, 3.3V supply, cc1111 SoC */
37 #define AO_LOG_FORMAT_TELEGPS           9       /* 32 byte telegps records */
38 #define AO_LOG_FORMAT_TELEMEGA          10      /* 32 byte typed telemega records with 32 bit gyro cal */
39 #define AO_LOG_FORMAT_DETHERM           11      /* 16-byte MS5607 baro only, no ADC */
40 #define AO_LOG_FORMAT_TELEMINI3         12      /* 16-byte MS5607 baro only, 3.3V supply, stm32f042 SoC */
41 #define AO_LOG_FORMAT_TELEFIRETWO       13      /* 32-byte test stand data */
42 #define AO_LOG_FORMAT_EASYMINI2         14      /* 16-byte MS5607 baro only, 3.3V supply, stm32f042 SoC */
43 #define AO_LOG_FORMAT_TELEMEGA_3        15      /* 32 byte typed telemega records with 32 bit gyro cal and mpu9250 */
44 #define AO_LOG_FORMAT_EASYMEGA_2        16      /* 32 byte typed telemega records with 32 bit gyro cal, mpu9250 rotated 90° and adxl375 */
45 #define AO_LOG_FORMAT_TELESTATIC        17      /* 32 byte typed telestatic records */
46 #define AO_LOG_FORMAT_MICROPEAK2        18      /* 2-byte baro values with header */
47 #define AO_LOG_FORMAT_TELEMEGA_4        19      /* 32 byte typed telemega records with 32 bit gyro cal and Bmx160 */
48 #define AO_LOG_FORMAT_NONE              127     /* No log at all */
49
50 enum ao_pyro_flag {
51         ao_pyro_none                    = 0x00000000,
52
53         ao_pyro_accel_less              = 0x00000001,
54         ao_pyro_accel_greater           = 0x00000002,
55
56         ao_pyro_speed_less              = 0x00000004,
57         ao_pyro_speed_greater           = 0x00000008,
58
59         ao_pyro_height_less             = 0x00000010,
60         ao_pyro_height_greater          = 0x00000020,
61
62         ao_pyro_orient_less             = 0x00000040,
63         ao_pyro_orient_greater          = 0x00000080,
64
65         ao_pyro_time_less               = 0x00000100,
66         ao_pyro_time_greater            = 0x00000200,
67
68         ao_pyro_ascending               = 0x00000400,
69         ao_pyro_descending              = 0x00000800,
70
71         ao_pyro_after_motor             = 0x00001000,
72
73         ao_pyro_delay                   = 0x00002000,
74
75         ao_pyro_state_less              = 0x00004000,
76         ao_pyro_state_greater_or_equal  = 0x00008000,
77 };
78
79 struct ao_pyro {
80         enum ao_pyro_flag       flags;
81         int16_t                 accel_less, accel_greater;
82         int16_t                 speed_less, speed_greater;
83         int16_t                 height_less, height_greater;
84         int16_t                 orient_less, orient_greater;
85         int16_t                 time_less, time_greater;
86         int16_t                 delay;
87         uint8_t                 state_less, state_greater_or_equal;
88         int16_t                 motor;
89         uint16_t                delay_done;
90         uint8_t                 _unused;        /* was 'fired' */
91 };
92
93 struct ao_config {
94         uint8_t         major;
95         uint8_t         minor;
96         uint16_t        main_deploy;
97         int16_t         accel_plus_g;           /* changed for minor version 2 */
98         uint8_t         _legacy_radio_channel;
99         char            callsign[AO_MAX_CALLSIGN + 1];
100         uint8_t         apogee_delay;           /* minor version 1 */
101         int16_t         accel_minus_g;          /* minor version 2 */
102         uint32_t        radio_cal;              /* minor version 3 */
103         uint32_t        flight_log_max;         /* minor version 4 */
104         uint8_t         ignite_mode;            /* minor version 5 */
105         uint8_t         pad_orientation;        /* minor version 6 */
106         uint32_t        radio_setting;          /* minor version 7 */
107         uint8_t         radio_enable;           /* minor version 8 */
108         uint8_t         aes_key[AO_AES_LEN];    /* minor version 9 */
109         uint32_t        frequency;              /* minor version 10 */
110         uint16_t        apogee_lockout;         /* minor version 11 */
111         struct ao_pyro  pyro[AO_PYRO_NUM];      /* minor version 12 */
112         uint16_t        aprs_interval;          /* minor version 13 */
113         uint8_t         radio_power;            /* minor version 14 */
114         uint8_t         radio_amp;              /* minor version 14 */
115         int16_t         accel_zero_along;       /* minor version 15 */
116         int16_t         accel_zero_across;      /* minor version 15 */
117         int16_t         accel_zero_through;     /* minor version 15 */
118         uint8_t         mid_beep;               /* minor version 16 */
119         uint16_t        tracker_motion;         /* minor version 17 */
120         uint8_t         tracker_interval;       /* minor version 17 */
121         uint16_t        pyro_time;              /* minor version 18 */
122         uint8_t         aprs_ssid;              /* minor version 19 */
123         uint8_t         radio_rate;             /* minor version 20 */
124         uint32_t        send_frequency;         /* minor version 21 */
125         uint8_t         aprs_format;            /* minor version 22 */
126         uint8_t         pad_box;                /* minor version 22 */
127         uint8_t         pad_idle;               /* minor version 23 */
128 };
129
130 /*
131  * ao_log_big.c
132  */
133
134 /*
135  * The data log is recorded in the eeprom as a sequence
136  * of data packets.
137  *
138  * Each packet starts with a 4-byte header that has the
139  * packet type, the packet checksum and the tick count. Then
140  * they all contain 2 16 bit values which hold packet-specific
141  * data.
142  *
143  * For each flight, the first packet
144  * is FLIGHT packet, indicating the serial number of the
145  * device and a unique number marking the number of flights
146  * recorded by this device.
147  *
148  * During flight, data from the accelerometer and barometer
149  * are recorded in SENSOR packets, using the raw 16-bit values
150  * read from the A/D converter.
151  *
152  * Also during flight, but at a lower rate, the deployment
153  * sensors are recorded in DEPLOY packets. The goal here is to
154  * detect failure in the deployment circuits.
155  *
156  * STATE packets hold state transitions as the flight computer
157  * transitions through different stages of the flight.
158  */
159 #define AO_LOG_FLIGHT           'F'
160 #define AO_LOG_SENSOR           'A'
161 #define AO_LOG_TEMP_VOLT        'T'
162 #define AO_LOG_DEPLOY           'D'
163 #define AO_LOG_STATE            'S'
164 #define AO_LOG_GPS_TIME         'G'
165 #define AO_LOG_GPS_LAT          'N'
166 #define AO_LOG_GPS_LON          'W'
167 #define AO_LOG_GPS_ALT          'H'
168 #define AO_LOG_GPS_SAT          'V'
169 #define AO_LOG_GPS_DATE         'Y'
170 #define AO_LOG_GPS_POS          'P'
171
172 #define AO_LOG_POS_NONE         (~0UL)
173
174 /* Common header in all log formats */
175 struct ao_log_header {
176         char                    type;                           /* 0 */
177         uint8_t                 csum;                           /* 1 */
178         uint16_t                tick;                           /* 2 */
179 };
180
181 struct ao_log_record {
182         char                    type;                           /* 0 */
183         uint8_t                 csum;                           /* 1 */
184         uint16_t                tick;                           /* 2 */
185         union {
186                 struct {
187                         int16_t         ground_accel;           /* 4 */
188                         uint16_t        flight;                 /* 6 */
189                 } flight;
190                 struct {
191                         int16_t         accel;                  /* 4 */
192                         int16_t         pres;                   /* 6 */
193                 } sensor;
194                 struct {
195                         int16_t         temp;
196                         int16_t         v_batt;
197                 } temp_volt;
198                 struct {
199                         int16_t         drogue;
200                         int16_t         main;
201                 } deploy;
202                 struct {
203                         uint16_t        state;
204                         uint16_t        reason;
205                 } state;
206                 struct {
207                         uint8_t         hour;
208                         uint8_t         minute;
209                         uint8_t         second;
210                         uint8_t         flags;
211                 } gps_time;
212                 int32_t         gps_latitude;
213                 int32_t         gps_longitude;
214                 struct {
215                         uint16_t        altitude_low;
216                         int16_t         altitude_high;
217                 } gps_altitude;
218                 struct {
219                         uint16_t        svid;
220                         uint8_t         unused;
221                         uint8_t         c_n;
222                 } gps_sat;
223                 struct {
224                         uint8_t         year;
225                         uint8_t         month;
226                         uint8_t         day;
227                         uint8_t         extra;
228                 } gps_date;
229                 struct {
230                         uint16_t        d0;
231                         uint16_t        d1;
232                 } anon;
233         } u;
234 };
235
236 struct ao_log_mega {
237         char                    type;                   /* 0 */
238         uint8_t                 csum;                   /* 1 */
239         uint16_t                tick;                   /* 2 */
240         union {                                         /* 4 */
241                 /* AO_LOG_FLIGHT */
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         pad_18;                 /* 18 */
250                         int32_t         ground_roll;            /* 20 */
251                         int32_t         ground_pitch;           /* 24 */
252                         int32_t         ground_yaw;             /* 28 */
253                 } flight;                                       /* 32 */
254                 struct {
255                         uint16_t        flight;                 /* 4 */
256                         int16_t         ground_accel;           /* 6 */
257                         uint32_t        ground_pres;            /* 8 */
258                         int16_t         ground_accel_along;     /* 12 */
259                         int16_t         ground_accel_across;    /* 14 */
260                         int16_t         ground_accel_through;   /* 16 */
261                         int16_t         ground_roll;            /* 18 */
262                         int16_t         ground_pitch;           /* 20 */
263                         int16_t         ground_yaw;             /* 22 */
264                 } flight_old;                                   /* 24 */
265                 /* AO_LOG_STATE */
266                 struct {
267                         uint16_t        state;
268                         uint16_t        reason;
269                 } state;
270                 /* AO_LOG_SENSOR */
271                 struct {
272                         uint32_t        pres;           /* 4 */
273                         uint32_t        temp;           /* 8 */
274                         int16_t         accel_x;        /* 12 */
275                         int16_t         accel_y;        /* 14 */
276                         int16_t         accel_z;        /* 16 */
277                         int16_t         gyro_x;         /* 18 */
278                         int16_t         gyro_y;         /* 20 */
279                         int16_t         gyro_z;         /* 22 */
280                         int16_t         mag_x;          /* 24 */
281                         int16_t         mag_z;          /* 26 */
282                         int16_t         mag_y;          /* 28 */
283                         int16_t         accel;          /* 30 */
284                 } sensor;       /* 32 */
285                 /* AO_LOG_TEMP_VOLT */
286                 struct {
287                         int16_t         v_batt;         /* 4 */
288                         int16_t         v_pbatt;        /* 6 */
289                         int16_t         n_sense;        /* 8 */
290                         int16_t         sense[10];      /* 10 */
291                         uint16_t        pyro;           /* 30 */
292                 } volt;                                 /* 32 */
293                 /* AO_LOG_GPS_TIME */
294                 struct {
295                         int32_t         latitude;       /* 4 */
296                         int32_t         longitude;      /* 8 */
297                         uint16_t        altitude_low;   /* 12 */
298                         uint8_t         hour;           /* 14 */
299                         uint8_t         minute;         /* 15 */
300                         uint8_t         second;         /* 16 */
301                         uint8_t         flags;          /* 17 */
302                         uint8_t         year;           /* 18 */
303                         uint8_t         month;          /* 19 */
304                         uint8_t         day;            /* 20 */
305                         uint8_t         course;         /* 21 */
306                         uint16_t        ground_speed;   /* 22 */
307                         int16_t         climb_rate;     /* 24 */
308                         uint8_t         pdop;           /* 26 */
309                         uint8_t         hdop;           /* 27 */
310                         uint8_t         vdop;           /* 28 */
311                         uint8_t         mode;           /* 29 */
312                         int16_t         altitude_high;  /* 30 */
313                 } gps;  /* 32 */
314                 /* AO_LOG_GPS_SAT */
315                 struct {
316                         uint16_t        channels;       /* 4 */
317                         struct {
318                                 uint8_t svid;
319                                 uint8_t c_n;
320                         } sats[12];                     /* 6 */
321                 } gps_sat;                              /* 30 */
322         } u;
323 };
324
325 #define AO_LOG_MEGA_GPS_ALTITUDE(l)     ((int32_t) ((l)->u.gps.altitude_high << 16) | ((l)->u.gps.altitude_low))
326 #define AO_LOG_MEGA_SET_GPS_ALTITUDE(l,a)       (((l)->u.gps.mode |= AO_GPS_MODE_ALTITUDE_24), \
327                                                  ((l)->u.gps.altitude_high = (a) >> 16), \
328                                                  (l)->u.gps.altitude_low = (a))
329
330 struct ao_log_firetwo {
331         char                    type;                   /* 0 */
332         uint8_t                 csum;                   /* 1 */
333         uint16_t                tick;                   /* 2 */
334         union {                                         /* 4 */
335                 /* AO_LOG_FLIGHT */
336                 struct {
337                         uint16_t        flight;         /* 4 */
338                 } flight;       /* 6 */
339                 /* AO_LOG_STATE */
340                 struct {
341                         uint16_t        state;          /* 4 */
342                         uint16_t        reason;         /* 6 */
343                 } state;        /* 8 */
344                 /* AO_LOG_SENSOR */
345                 struct {
346                         uint16_t        pressure;       /* 4 */
347                         uint16_t        thrust;         /* 6 */
348                         uint16_t        thermistor[4];  /* 8 */
349                 } sensor;       /* 24 */
350                 uint8_t         align[28];              /* 4 */
351         } u;    /* 32 */
352 };
353
354 struct ao_log_telestatic {
355         char                    type;                   /* 0 */
356         uint8_t                 csum;                   /* 1 */
357         uint16_t                tick;                   /* 2 */
358         union {                                         /* 4 */
359                 /* AO_LOG_FLIGHT */
360                 struct {
361                         uint16_t        flight;         /* 4 */
362                 } flight;       /* 6 */
363                 /* AO_LOG_STATE */
364                 struct {
365                         uint16_t        state;          /* 4 */
366                         uint16_t        reason;         /* 6 */
367                 } state;        /* 8 */
368                 /* AO_LOG_SENSOR */
369                 struct {
370                         uint32_t        pressure;       /* 4 */
371                         uint32_t        pressure2;      /* 8 */
372                         uint32_t        thrust;         /* 12 */
373                         uint32_t        mass;           /* 16 */
374                         uint16_t        t_low;          /* 20 */
375                         uint16_t        t_high[4];      /* 22 */
376                 } sensor;       /* 30 */
377                 uint8_t         align[28];              /* 4 */
378         } u;    /* 32 */
379 };
380
381 struct ao_log_metrum {
382         char                    type;                   /* 0 */
383         uint8_t                 csum;                   /* 1 */
384         uint16_t                tick;                   /* 2 */
385         union {                                         /* 4 */
386                 /* AO_LOG_FLIGHT */
387                 struct {
388                         uint16_t        flight;         /* 4 */
389                         int16_t         ground_accel;   /* 6 */
390                         uint32_t        ground_pres;    /* 8 */
391                         uint32_t        ground_temp;    /* 12 */
392                 } flight;       /* 16 */
393                 /* AO_LOG_STATE */
394                 struct {
395                         uint16_t        state;          /* 4 */
396                         uint16_t        reason;         /* 6 */
397                 } state;        /* 8 */
398                 /* AO_LOG_SENSOR */
399                 struct {
400                         uint32_t        pres;           /* 4 */
401                         uint32_t        temp;           /* 8 */
402                         int16_t         accel;          /* 12 */
403                 } sensor;       /* 14 */
404                 /* AO_LOG_TEMP_VOLT */
405                 struct {
406                         int16_t         v_batt;         /* 4 */
407                         int16_t         sense_a;        /* 6 */
408                         int16_t         sense_m;        /* 8 */
409                 } volt;         /* 10 */
410                 /* AO_LOG_GPS_POS */
411                 struct {
412                         int32_t         latitude;       /* 4 */
413                         int32_t         longitude;      /* 8 */
414                         uint16_t        altitude_low;   /* 12 */
415                         int16_t         altitude_high;  /* 14 */
416                 } gps;          /* 16 */
417                 /* AO_LOG_GPS_TIME */
418                 struct {
419                         uint8_t         hour;           /* 4 */
420                         uint8_t         minute;         /* 5 */
421                         uint8_t         second;         /* 6 */
422                         uint8_t         flags;          /* 7 */
423                         uint8_t         year;           /* 8 */
424                         uint8_t         month;          /* 9 */
425                         uint8_t         day;            /* 10 */
426                         uint8_t         pdop;           /* 11 */
427                 } gps_time;     /* 12 */
428                 /* AO_LOG_GPS_SAT (up to three packets) */
429                 struct {
430                         uint8_t channels;               /* 4 */
431                         uint8_t more;                   /* 5 */
432                         struct {
433                                 uint8_t svid;
434                                 uint8_t c_n;
435                         } sats[4];                      /* 6 */
436                 } gps_sat;                              /* 14 */
437                 uint8_t         raw[12];                /* 4 */
438         } u;    /* 16 */
439 };
440
441 struct ao_log_mini {
442         char            type;                           /* 0 */
443         uint8_t         csum;                           /* 1 */
444         uint16_t        tick;                           /* 2 */
445         union {                                         /* 4 */
446                 /* AO_LOG_FLIGHT */
447                 struct {
448                         uint16_t        flight;         /* 4 */
449                         uint16_t        r6;
450                         uint32_t        ground_pres;    /* 8 */
451                 } flight;
452                 /* AO_LOG_STATE */
453                 struct {
454                         uint16_t        state;          /* 4 */
455                         uint16_t        reason;         /* 6 */
456                 } state;
457                 /* AO_LOG_SENSOR */
458                 struct {
459                         uint8_t         pres[3];        /* 4 */
460                         uint8_t         temp[3];        /* 7 */
461                         int16_t         sense_a;        /* 10 */
462                         int16_t         sense_m;        /* 12 */
463                         int16_t         v_batt;         /* 14 */
464                 } sensor;                               /* 16 */
465         } u;                                            /* 16 */
466 };                                                      /* 16 */
467
468 #define ao_log_pack24(dst,value) do {           \
469                 (dst)[0] = (value);             \
470                 (dst)[1] = (value) >> 8;        \
471                 (dst)[2] = (value) >> 16;       \
472         } while (0)
473
474 struct ao_log_gps {
475         char                    type;                   /* 0 */
476         uint8_t                 csum;                   /* 1 */
477         uint16_t                tick;                   /* 2 */
478         union {                                         /* 4 */
479                 /* AO_LOG_FLIGHT */
480                 struct {
481                         uint16_t        flight;                 /* 4 */
482                         int16_t         start_altitude;         /* 6 */
483                         int32_t         start_latitude;         /* 8 */
484                         int32_t         start_longitude;        /* 12 */
485                 } flight;                                       /* 16 */
486                 /* AO_LOG_GPS_TIME */
487                 struct {
488                         int32_t         latitude;       /* 4 */
489                         int32_t         longitude;      /* 8 */
490                         uint16_t        altitude_low;   /* 12 */
491                         uint8_t         hour;           /* 14 */
492                         uint8_t         minute;         /* 15 */
493                         uint8_t         second;         /* 16 */
494                         uint8_t         flags;          /* 17 */
495                         uint8_t         year;           /* 18 */
496                         uint8_t         month;          /* 19 */
497                         uint8_t         day;            /* 20 */
498                         uint8_t         course;         /* 21 */
499                         uint16_t        ground_speed;   /* 22 */
500                         int16_t         climb_rate;     /* 24 */
501                         uint8_t         pdop;           /* 26 */
502                         uint8_t         hdop;           /* 27 */
503                         uint8_t         vdop;           /* 28 */
504                         uint8_t         mode;           /* 29 */
505                         int16_t         altitude_high;  /* 30 */
506                 } gps;  /* 31 */
507                 /* AO_LOG_GPS_SAT */
508                 struct {
509                         uint16_t        channels;       /* 4 */
510                         struct {
511                                 uint8_t svid;
512                                 uint8_t c_n;
513                         } sats[12];                     /* 6 */
514                 } gps_sat;                              /* 30 */
515         } u;
516 };
517
518 struct ao_eeprom {
519         struct ao_config        config;
520         struct ao_ms5607_prom   ms5607_prom;
521         int                     log_format;
522         uint16_t                serial_number;
523         uint8_t                 *data;
524         uint32_t                len;
525 };
526
527 struct ao_eeprom *ao_eeprom_read(FILE *file);
528
529 struct ao_eeprom *ao_eeprom_read_old(FILE *file);
530
531 void ao_eeprom_free_data(struct ao_eeprom *ao_eeprom);
532
533 #endif /* _AO_EEPROM_READ_H_ */