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