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