altos: add high-z accel and mag sensor to megametrum logging
[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_MEGAMETRUM        5       /* 32 byte typed megametrum records */
47 #define AO_LOG_FORMAT_NONE              127     /* No log at all */
48
49 extern __code uint8_t ao_log_format;
50
51 /* Return the flight number from the given log slot, 0 if none */
52 uint16_t
53 ao_log_flight(uint8_t slot);
54
55 /* Flush the log */
56 void
57 ao_log_flush(void);
58
59 /* Logging thread main routine */
60 void
61 ao_log(void);
62
63 /* functions provided in ao_log.c */
64
65 /* Figure out the current flight number */
66 void
67 ao_log_scan(void) __reentrant;
68
69 /* Return the position of the start of the given log slot */
70 uint32_t
71 ao_log_pos(uint8_t slot);
72
73 /* Start logging to eeprom */
74 void
75 ao_log_start(void);
76
77 /* Stop logging */
78 void
79 ao_log_stop(void);
80
81 /* Initialize the logging system */
82 void
83 ao_log_init(void);
84
85 /* Write out the current flight number to the erase log */
86 void
87 ao_log_write_erase(uint8_t pos);
88
89 /* Returns true if there are any logs stored in eeprom */
90 uint8_t
91 ao_log_present(void);
92
93 /* Returns true if there is no more storage space available */
94 uint8_t
95 ao_log_full(void);
96
97 /*
98  * ao_log_big.c
99  */
100
101 /*
102  * The data log is recorded in the eeprom as a sequence
103  * of data packets.
104  *
105  * Each packet starts with a 4-byte header that has the
106  * packet type, the packet checksum and the tick count. Then
107  * they all contain 2 16 bit values which hold packet-specific
108  * data.
109  *
110  * For each flight, the first packet
111  * is FLIGHT packet, indicating the serial number of the
112  * device and a unique number marking the number of flights
113  * recorded by this device.
114  *
115  * During flight, data from the accelerometer and barometer
116  * are recorded in SENSOR packets, using the raw 16-bit values
117  * read from the A/D converter.
118  *
119  * Also during flight, but at a lower rate, the deployment
120  * sensors are recorded in DEPLOY packets. The goal here is to
121  * detect failure in the deployment circuits.
122  *
123  * STATE packets hold state transitions as the flight computer
124  * transitions through different stages of the flight.
125  */
126 #define AO_LOG_FLIGHT           'F'
127 #define AO_LOG_SENSOR           'A'
128 #define AO_LOG_TEMP_VOLT        'T'
129 #define AO_LOG_DEPLOY           'D'
130 #define AO_LOG_STATE            'S'
131 #define AO_LOG_GPS_TIME         'G'
132 #define AO_LOG_GPS_LAT          'N'
133 #define AO_LOG_GPS_LON          'W'
134 #define AO_LOG_GPS_ALT          'H'
135 #define AO_LOG_GPS_SAT          'V'
136 #define AO_LOG_GPS_DATE         'Y'
137
138 #define AO_LOG_POS_NONE         (~0UL)
139
140 struct ao_log_record {
141         char                    type;
142         uint8_t                 csum;
143         uint16_t                tick;
144         union {
145                 struct {
146                         int16_t         ground_accel;
147                         uint16_t        flight;
148                 } flight;
149                 struct {
150                         int16_t         accel;
151                         int16_t         pres;
152                 } sensor;
153                 struct {
154                         int16_t         temp;
155                         int16_t         v_batt;
156                 } temp_volt;
157                 struct {
158                         int16_t         drogue;
159                         int16_t         main;
160                 } deploy;
161                 struct {
162                         uint16_t        state;
163                         uint16_t        reason;
164                 } state;
165                 struct {
166                         uint8_t         hour;
167                         uint8_t         minute;
168                         uint8_t         second;
169                         uint8_t         flags;
170                 } gps_time;
171                 int32_t         gps_latitude;
172                 int32_t         gps_longitude;
173                 struct {
174                         int16_t         altitude;
175                         uint16_t        unused;
176                 } gps_altitude;
177                 struct {
178                         uint16_t        svid;
179                         uint8_t         unused;
180                         uint8_t         c_n;
181                 } gps_sat;
182                 struct {
183                         uint8_t         year;
184                         uint8_t         month;
185                         uint8_t         day;
186                         uint8_t         extra;
187                 } gps_date;
188                 struct {
189                         uint16_t        d0;
190                         uint16_t        d1;
191                 } anon;
192         } u;
193 };
194
195 struct ao_log_mega {
196         char                    type;                   /* 0 */
197         uint8_t                 csum;                   /* 1 */
198         uint16_t                tick;                   /* 2 */
199         union {                                         /* 4 */
200                 struct {
201                         uint16_t        flight;         /* 4 */
202                         int16_t         ground_accel;   /* 6 */
203                         uint32_t        ground_pres;    /* 8 */
204                         uint32_t        ground_temp;    /* 12 */
205                 } flight;                               /* 16 */
206                 struct {
207                         uint16_t        state;
208                         uint16_t        reason;
209                 } state;
210                 struct {
211                         uint32_t        pres;           /* 4 */
212                         uint32_t        temp;           /* 8 */
213                         int16_t         accel_x;        /* 12 */
214                         int16_t         accel_y;        /* 14 */
215                         int16_t         accel_z;        /* 16 */
216                         int16_t         gyro_x;         /* 18 */
217                         int16_t         gyro_y;         /* 20 */
218                         int16_t         gyro_z;         /* 22 */
219                         int16_t         mag_x;          /* 24 */
220                         int16_t         mag_y;          /* 26 */
221                         int16_t         mag_z;          /* 28 */
222                         int16_t         accel;          /* 30 */
223                 } sensor;       /* 32 */
224                 struct {
225                         int16_t         v_batt;         /* 4 */
226                         int16_t         v_pbatt;        /* 6 */
227                         int16_t         n_sense;        /* 8 */
228                         int16_t         sense[10];      /* 10 */
229                 } volt;                                 /* 30 */
230                 struct {
231                         int32_t         latitude;       /* 4 */
232                         int32_t         longitude;      /* 8 */
233                         int16_t         altitude;       /* 12 */
234                         uint8_t         hour;           /* 14 */
235                         uint8_t         minute;         /* 15 */
236                         uint8_t         second;         /* 16 */
237                         uint8_t         flags;          /* 17 */
238                         uint8_t         year;           /* 18 */
239                         uint8_t         month;          /* 19 */
240                         uint8_t         day;            /* 20 */
241                         uint8_t         pad;            /* 21 */
242                 } gps;  /* 22 */
243                 struct {
244                         uint16_t        channels;       /* 4 */
245                         struct {
246                                 uint8_t svid;
247                                 uint8_t c_n;
248                         } sats[12];                     /* 6 */
249                 } gps_sat;                              /* 30 */
250         } u;
251 };
252
253 /* Write a record to the eeprom log */
254 uint8_t
255 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
256
257 uint8_t
258 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant;
259
260 #endif /* _AO_LOG_H_ */