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