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