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