altos: Simplify storage API
[fw/altos] / src / ao_log.c
1 /*
2  * Copyright © 2009 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 #include "ao.h"
19
20 static __pdata uint32_t ao_log_current_pos;
21 static __pdata uint32_t ao_log_end_pos;
22 static __pdata uint32_t ao_log_start_pos;
23 static __xdata uint8_t  ao_log_running;
24 static __xdata uint8_t  ao_log_mutex;
25
26 static uint8_t
27 ao_log_csum(__xdata uint8_t *b) __reentrant
28 {
29         uint8_t sum = 0x5a;
30         uint8_t i;
31
32         for (i = 0; i < sizeof (struct ao_log_record); i++)
33                 sum += *b++;
34         return -sum;
35 }
36
37 uint8_t
38 ao_log_data(__xdata struct ao_log_record *log) __reentrant
39 {
40         uint8_t wrote = 0;
41         /* set checksum */
42         log->csum = 0;
43         log->csum = ao_log_csum((__xdata uint8_t *) log);
44         ao_mutex_get(&ao_log_mutex); {
45                 if (ao_log_current_pos >= ao_log_end_pos)
46                         ao_log_running = 0;
47                 if (ao_log_running) {
48                         wrote = 1;
49                         ao_storage_write(ao_log_current_pos,
50                                          log,
51                                          sizeof (struct ao_log_record));
52                         ao_log_current_pos += sizeof (struct ao_log_record);
53                 }
54         } ao_mutex_put(&ao_log_mutex);
55         return wrote;
56 }
57
58 void
59 ao_log_flush(void)
60 {
61         ao_storage_flush();
62 }
63
64 __xdata struct ao_log_record log;
65 __xdata uint16_t ao_flight_number;
66
67 static uint8_t
68 ao_log_dump_check_data(void)
69 {
70         if (ao_log_csum((uint8_t *) &log) != 0)
71                 return 0;
72         return 1;
73 }
74
75 static void
76 ao_log_scan(void)
77 {
78         if (!ao_storage_read(0, &log, sizeof (struct ao_log_record)))
79                 ao_panic(AO_PANIC_LOG);
80         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT) {
81                 ao_flight_number = log.u.flight.flight + 1;
82                 if (ao_flight_number == 0)
83                         ao_flight_number = 1;
84         } else {
85                 ao_flight_number = 1;
86         }
87         ao_wakeup(&ao_flight_number);
88 }
89
90 __xdata uint8_t ao_log_adc_pos;
91 __xdata enum flight_state ao_log_state;
92
93 /* a hack to make sure that ao_log_records fill the eeprom block in even units */
94 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_record))] ;
95
96 void
97 ao_log(void)
98 {
99         ao_storage_setup();
100
101         /* For now, use all of the available space */
102         ao_log_current_pos = 0;
103         ao_log_end_pos = ao_storage_config;
104
105         ao_log_scan();
106
107         while (!ao_log_running)
108                 ao_sleep(&ao_log_running);
109
110         log.type = AO_LOG_FLIGHT;
111         log.tick = ao_flight_tick;
112         log.u.flight.ground_accel = ao_ground_accel;
113         log.u.flight.flight = ao_flight_number;
114         ao_log_data(&log);
115
116         /* Write the whole contents of the ring to the log
117          * when starting up.
118          */
119         ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
120         for (;;) {
121                 /* Write samples to EEPROM */
122                 while (ao_log_adc_pos != ao_adc_head) {
123                         log.type = AO_LOG_SENSOR;
124                         log.tick = ao_adc_ring[ao_log_adc_pos].tick;
125                         log.u.sensor.accel = ao_adc_ring[ao_log_adc_pos].accel;
126                         log.u.sensor.pres = ao_adc_ring[ao_log_adc_pos].pres;
127                         ao_log_data(&log);
128                         if ((ao_log_adc_pos & 0x1f) == 0) {
129                                 log.type = AO_LOG_TEMP_VOLT;
130                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
131                                 log.u.temp_volt.temp = ao_adc_ring[ao_log_adc_pos].temp;
132                                 log.u.temp_volt.v_batt = ao_adc_ring[ao_log_adc_pos].v_batt;
133                                 ao_log_data(&log);
134                                 log.type = AO_LOG_DEPLOY;
135                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
136                                 log.u.deploy.drogue = ao_adc_ring[ao_log_adc_pos].sense_d;
137                                 log.u.deploy.main = ao_adc_ring[ao_log_adc_pos].sense_m;
138                                 ao_log_data(&log);
139                         }
140                         ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
141                 }
142                 /* Write state change to EEPROM */
143                 if (ao_flight_state != ao_log_state) {
144                         ao_log_state = ao_flight_state;
145                         log.type = AO_LOG_STATE;
146                         log.tick = ao_flight_tick;
147                         log.u.state.state = ao_log_state;
148                         log.u.state.reason = 0;
149                         ao_log_data(&log);
150
151                         if (ao_log_state == ao_flight_landed)
152                                 ao_log_stop();
153                 }
154
155                 /* Wait for a while */
156                 ao_delay(AO_MS_TO_TICKS(100));
157
158                 /* Stop logging when told to */
159                 while (!ao_log_running)
160                         ao_sleep(&ao_log_running);
161         }
162 }
163
164 void
165 ao_log_start(void)
166 {
167         /* start logging */
168         ao_log_running = 1;
169         ao_wakeup(&ao_log_running);
170 }
171
172 void
173 ao_log_stop(void)
174 {
175         ao_log_running = 0;
176         ao_log_flush();
177 }
178
179 static __xdata struct ao_task ao_log_task;
180
181 void
182 ao_log_init(void)
183 {
184         ao_log_running = 0;
185
186         /* For now, just log the flight starting at the begining of eeprom */
187         ao_log_state = ao_flight_invalid;
188
189         /* Create a task to log events to eeprom */
190         ao_add_task(&ao_log_task, ao_log, "log");
191 }