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