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