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