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