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