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