33cda3eb3560303f2a3d9030ebc0f76ed7fc89b9
[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                         int16_t         altitude;
180                         uint16_t        unused;
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                         int16_t         altitude;       /* 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                 } gps;  /* 30 */
265                 /* AO_LOG_GPS_SAT */
266                 struct {
267                         uint16_t        channels;       /* 4 */
268                         struct {
269                                 uint8_t svid;
270                                 uint8_t c_n;
271                         } sats[12];                     /* 6 */
272                 } gps_sat;                              /* 30 */
273         } u;
274 };
275
276 struct ao_log_metrum {
277         char                    type;                   /* 0 */
278         uint8_t                 csum;                   /* 1 */
279         uint16_t                tick;                   /* 2 */
280         union {                                         /* 4 */
281                 /* AO_LOG_FLIGHT */
282                 struct {
283                         uint16_t        flight;         /* 4 */
284                         int16_t         ground_accel;   /* 6 */
285                         uint32_t        ground_pres;    /* 8 */
286                         uint32_t        ground_temp;    /* 12 */
287                 } flight;       /* 16 */
288                 /* AO_LOG_STATE */
289                 struct {
290                         uint16_t        state;          /* 4 */
291                         uint16_t        reason;         /* 6 */
292                 } state;        /* 8 */
293                 /* AO_LOG_SENSOR */
294                 struct {
295                         uint32_t        pres;           /* 4 */
296                         uint32_t        temp;           /* 8 */
297                         int16_t         accel;          /* 12 */
298                 } sensor;       /* 14 */
299                 /* AO_LOG_TEMP_VOLT */
300                 struct {
301                         int16_t         v_batt;         /* 4 */
302                         int16_t         sense_a;        /* 6 */
303                         int16_t         sense_m;        /* 8 */
304                 } volt;         /* 10 */
305                 /* AO_LOG_GPS_POS */
306                 struct {
307                         int32_t         latitude;       /* 4 */
308                         int32_t         longitude;      /* 8 */
309                         int16_t         altitude;       /* 12 */
310                 } gps;          /* 14 */
311                 /* AO_LOG_GPS_TIME */
312                 struct {
313                         uint8_t         hour;           /* 4 */
314                         uint8_t         minute;         /* 5 */
315                         uint8_t         second;         /* 6 */
316                         uint8_t         flags;          /* 7 */
317                         uint8_t         year;           /* 8 */
318                         uint8_t         month;          /* 9 */
319                         uint8_t         day;            /* 10 */
320                         uint8_t         pad;            /* 11 */
321                 } gps_time;     /* 12 */
322                 /* AO_LOG_GPS_SAT (up to three packets) */
323                 struct {
324                         uint8_t channels;               /* 4 */
325                         uint8_t more;                   /* 5 */
326                         struct {
327                                 uint8_t svid;
328                                 uint8_t c_n;
329                         } sats[4];                      /* 6 */
330                 } gps_sat;                              /* 14 */
331                 uint8_t         raw[12];                /* 4 */
332         } u;    /* 16 */
333 };
334
335 struct ao_log_mini {
336         char            type;                           /* 0 */
337         uint8_t         csum;                           /* 1 */
338         uint16_t        tick;                           /* 2 */
339         union {                                         /* 4 */
340                 /* AO_LOG_FLIGHT */
341                 struct {
342                         uint16_t        flight;         /* 4 */
343                         uint16_t        r6;
344                         uint32_t        ground_pres;    /* 8 */
345                 } flight;
346                 /* AO_LOG_STATE */
347                 struct {
348                         uint16_t        state;          /* 4 */
349                         uint16_t        reason;         /* 6 */
350                 } state;
351                 /* AO_LOG_SENSOR */
352                 struct {
353                         uint8_t         pres[3];        /* 4 */
354                         uint8_t         temp[3];        /* 7 */
355                         int16_t         sense_a;        /* 10 */
356                         int16_t         sense_m;        /* 12 */
357                         int16_t         v_batt;         /* 14 */
358                 } sensor;                               /* 16 */
359         } u;                                            /* 16 */
360 };                                                      /* 16 */
361
362 #define ao_log_pack24(dst,value) do {           \
363                 (dst)[0] = (value);             \
364                 (dst)[1] = (value) >> 8;        \
365                 (dst)[2] = (value) >> 16;       \
366         } while (0)
367
368 struct ao_log_gps {
369         char                    type;                   /* 0 */
370         uint8_t                 csum;                   /* 1 */
371         uint16_t                tick;                   /* 2 */
372         union {                                         /* 4 */
373                 /* AO_LOG_FLIGHT */
374                 struct {
375                         uint16_t        flight;                 /* 4 */
376                         int16_t         start_altitude;         /* 6 */
377                         int32_t         start_latitude;         /* 8 */
378                         int32_t         start_longitude;        /* 12 */
379                 } flight;                                       /* 16 */
380                 /* AO_LOG_GPS_TIME */
381                 struct {
382                         int32_t         latitude;       /* 4 */
383                         int32_t         longitude;      /* 8 */
384                         int16_t         altitude;       /* 12 */
385                         uint8_t         hour;           /* 14 */
386                         uint8_t         minute;         /* 15 */
387                         uint8_t         second;         /* 16 */
388                         uint8_t         flags;          /* 17 */
389                         uint8_t         year;           /* 18 */
390                         uint8_t         month;          /* 19 */
391                         uint8_t         day;            /* 20 */
392                         uint8_t         course;         /* 21 */
393                         uint16_t        ground_speed;   /* 22 */
394                         int16_t         climb_rate;     /* 24 */
395                         uint8_t         pdop;           /* 26 */
396                         uint8_t         hdop;           /* 27 */
397                         uint8_t         vdop;           /* 28 */
398                         uint8_t         mode;           /* 29 */
399                         uint8_t         state;          /* 30 */
400                 } gps;  /* 31 */
401                 /* AO_LOG_GPS_SAT */
402                 struct {
403                         uint16_t        channels;       /* 4 */
404                         struct {
405                                 uint8_t svid;
406                                 uint8_t c_n;
407                         } sats[12];                     /* 6 */
408                 } gps_sat;                              /* 30 */
409         } u;
410 };
411
412 /* Write a record to the eeprom log */
413 uint8_t
414 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
415
416 uint8_t
417 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant;
418
419 uint8_t
420 ao_log_metrum(__xdata struct ao_log_metrum *log) __reentrant;
421
422 uint8_t
423 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant;
424
425 uint8_t
426 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant;
427
428 void
429 ao_log_flush(void);
430
431 void
432 ao_gps_report_metrum_init(void);
433
434 #endif /* _AO_LOG_H_ */