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