611c00d526f367fcb20f26f72aae39c0104ab732
[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 /*
22  * ao_log.c
23  */
24
25 /* We record flight numbers in the first record of
26  * the log. Tasks may wait for this to be initialized
27  * by sleeping on this variable.
28  */
29 extern __xdata uint16_t ao_flight_number;
30
31 extern __pdata uint32_t ao_log_current_pos;
32 extern __pdata uint32_t ao_log_end_pos;
33 extern __pdata uint32_t ao_log_start_pos;
34 extern __xdata uint8_t  ao_log_running;
35 extern __pdata enum flight_state ao_log_state;
36
37 /* required functions from the underlying log system */
38
39 #define AO_LOG_FORMAT_UNKNOWN           0       /* unknown; altosui will have to guess */
40 #define AO_LOG_FORMAT_FULL              1       /* 8 byte typed log records */
41 #define AO_LOG_FORMAT_TINY              2       /* two byte state/baro records */
42 #define AO_LOG_FORMAT_TELEMETRY         3       /* 32 byte ao_telemetry records */
43 #define AO_LOG_FORMAT_TELESCIENCE       4       /* 32 byte typed telescience records */
44 #define AO_LOG_FORMAT_NONE              127     /* No log at all */
45
46 extern __code uint8_t ao_log_format;
47
48 /* Return the flight number from the given log slot, 0 if none */
49 uint16_t
50 ao_log_flight(uint8_t slot);
51
52 /* Flush the log */
53 void
54 ao_log_flush(void);
55
56 /* Logging thread main routine */
57 void
58 ao_log(void);
59
60 /* functions provided in ao_log.c */
61
62 /* Figure out the current flight number */
63 void
64 ao_log_scan(void) __reentrant;
65
66 /* Return the position of the start of the given log slot */
67 uint32_t
68 ao_log_pos(uint8_t slot);
69
70 /* Start logging to eeprom */
71 void
72 ao_log_start(void);
73
74 /* Stop logging */
75 void
76 ao_log_stop(void);
77
78 /* Initialize the logging system */
79 void
80 ao_log_init(void);
81
82 /* Write out the current flight number to the erase log */
83 void
84 ao_log_write_erase(uint8_t pos);
85
86 /* Returns true if there are any logs stored in eeprom */
87 uint8_t
88 ao_log_present(void);
89
90 /* Returns true if there is no more storage space available */
91 uint8_t
92 ao_log_full(void);
93
94 /*
95  * ao_log_big.c
96  */
97
98 /*
99  * The data log is recorded in the eeprom as a sequence
100  * of data packets.
101  *
102  * Each packet starts with a 4-byte header that has the
103  * packet type, the packet checksum and the tick count. Then
104  * they all contain 2 16 bit values which hold packet-specific
105  * data.
106  *
107  * For each flight, the first packet
108  * is FLIGHT packet, indicating the serial number of the
109  * device and a unique number marking the number of flights
110  * recorded by this device.
111  *
112  * During flight, data from the accelerometer and barometer
113  * are recorded in SENSOR packets, using the raw 16-bit values
114  * read from the A/D converter.
115  *
116  * Also during flight, but at a lower rate, the deployment
117  * sensors are recorded in DEPLOY packets. The goal here is to
118  * detect failure in the deployment circuits.
119  *
120  * STATE packets hold state transitions as the flight computer
121  * transitions through different stages of the flight.
122  */
123 #define AO_LOG_FLIGHT           'F'
124 #define AO_LOG_SENSOR           'A'
125 #define AO_LOG_TEMP_VOLT        'T'
126 #define AO_LOG_DEPLOY           'D'
127 #define AO_LOG_STATE            'S'
128 #define AO_LOG_GPS_TIME         'G'
129 #define AO_LOG_GPS_LAT          'N'
130 #define AO_LOG_GPS_LON          'W'
131 #define AO_LOG_GPS_ALT          'H'
132 #define AO_LOG_GPS_SAT          'V'
133 #define AO_LOG_GPS_DATE         'Y'
134
135 #define AO_LOG_POS_NONE         (~0UL)
136
137 struct ao_log_record {
138         char                    type;
139         uint8_t                 csum;
140         uint16_t                tick;
141         union {
142                 struct {
143                         int16_t         ground_accel;
144                         uint16_t        flight;
145                 } flight;
146                 struct {
147                         int16_t         accel;
148                         int16_t         pres;
149                 } sensor;
150                 struct {
151                         int16_t         temp;
152                         int16_t         v_batt;
153                 } temp_volt;
154                 struct {
155                         int16_t         drogue;
156                         int16_t         main;
157                 } deploy;
158                 struct {
159                         uint16_t        state;
160                         uint16_t        reason;
161                 } state;
162                 struct {
163                         uint8_t         hour;
164                         uint8_t         minute;
165                         uint8_t         second;
166                         uint8_t         flags;
167                 } gps_time;
168                 int32_t         gps_latitude;
169                 int32_t         gps_longitude;
170                 struct {
171                         int16_t         altitude;
172                         uint16_t        unused;
173                 } gps_altitude;
174                 struct {
175                         uint16_t        svid;
176                         uint8_t         unused;
177                         uint8_t         c_n;
178                 } gps_sat;
179                 struct {
180                         uint8_t         year;
181                         uint8_t         month;
182                         uint8_t         day;
183                         uint8_t         extra;
184                 } gps_date;
185                 struct {
186                         uint16_t        d0;
187                         uint16_t        d1;
188                 } anon;
189         } u;
190 };
191
192 /* Write a record to the eeprom log */
193 uint8_t
194 ao_log_data(__xdata struct ao_log_record *log) __reentrant;
195
196 #endif /* _AO_LOG_H_ */