2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 static __pdata uint32_t ao_log_current_pos;
21 static __pdata uint32_t ao_log_start_pos;
22 static __xdata uint8_t ao_log_running;
23 static __xdata uint8_t ao_log_mutex;
26 ao_log_csum(uint8_t *b)
31 for (i = 0; i < sizeof (struct ao_log_record); i++)
37 ao_log_data(struct ao_log_record *log)
41 log->csum = ao_log_csum((uint8_t *) log);
42 ao_mutex_get(&ao_log_mutex); {
44 ao_ee_write(ao_log_current_pos,
46 sizeof (struct ao_log_record));
47 ao_log_current_pos += sizeof (struct ao_log_record);
48 if (ao_log_current_pos >= AO_EE_DATA_SIZE)
49 ao_log_current_pos = 0;
50 if (ao_log_current_pos == ao_log_start_pos)
53 } ao_mutex_put(&ao_log_mutex);
62 __xdata struct ao_log_record ao_log_dump;
63 static __xdata uint16_t ao_log_dump_flight;
64 static __xdata uint32_t ao_log_dump_pos;
67 ao_log_dump_check_data(void)
69 if (ao_log_csum((uint8_t *) &ao_log_dump) != 0)
75 ao_log_dump_scan(void)
77 if (!ao_ee_read(0, (uint8_t *) &ao_log_dump, sizeof (struct ao_log_record)))
78 ao_panic(AO_PANIC_LOG);
79 if (ao_log_dump_check_data() && ao_log_dump.type == AO_LOG_FLIGHT) {
80 ao_log_dump_flight = ao_log_dump.u.flight.flight;
83 ao_log_dump_flight = 0;
89 ao_log_dump_first(void)
92 if (!ao_log_dump_scan())
98 ao_log_dump_next(void)
100 ao_log_dump_pos += sizeof (struct ao_log_record);
101 if (ao_log_dump_pos >= AO_EE_DEVICE_SIZE)
103 if (!ao_ee_read(ao_log_dump_pos, (uint8_t *) &ao_log_dump,
104 sizeof (struct ao_log_record)))
106 return ao_log_dump_check_data();
109 __xdata uint8_t ao_log_adc_pos;
110 __xdata enum flight_state ao_log_state;
112 /* a hack to make sure that ao_log_records fill the eeprom block in even units */
113 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_record))] ;
118 static __xdata struct ao_log_record log;
122 while (!ao_log_running)
123 ao_sleep(&ao_log_running);
125 log.type = AO_LOG_FLIGHT;
126 log.tick = ao_flight_tick;
127 log.u.flight.ground_accel = ao_ground_accel;
128 log.u.flight.flight = ao_log_dump_flight + 1;
131 /* Write the whole contents of the ring to the log
134 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
136 /* Write samples to EEPROM */
137 while (ao_log_adc_pos != ao_adc_head) {
138 log.type = AO_LOG_SENSOR;
139 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
140 log.u.sensor.accel = ao_adc_ring[ao_log_adc_pos].accel;
141 log.u.sensor.pres = ao_adc_ring[ao_log_adc_pos].pres;
143 if ((ao_log_adc_pos & 0x1f) == 0) {
144 log.type = AO_LOG_TEMP_VOLT;
145 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
146 log.u.temp_volt.temp = ao_adc_ring[ao_log_adc_pos].temp;
147 log.u.temp_volt.v_batt = ao_adc_ring[ao_log_adc_pos].v_batt;
149 log.type = AO_LOG_DEPLOY;
150 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
151 log.u.deploy.drogue = ao_adc_ring[ao_log_adc_pos].sense_d;
152 log.u.deploy.main = ao_adc_ring[ao_log_adc_pos].sense_m;
155 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
157 /* Write state change to EEPROM */
158 if (ao_flight_state != ao_log_state) {
159 ao_log_state = ao_flight_state;
160 log.type = AO_LOG_STATE;
161 log.tick = ao_flight_tick;
162 log.u.state.state = ao_log_state;
163 log.u.state.reason = 0;
166 if (ao_log_state == ao_flight_landed)
170 /* Wait for a while */
171 ao_delay(AO_MS_TO_TICKS(100));
173 /* Stop logging when told to */
174 while (!ao_log_running)
175 ao_sleep(&ao_log_running);
184 ao_wakeup(&ao_log_running);
199 for (more = ao_log_dump_first(); more; more = ao_log_dump_next()) {
200 printf("%c %4x %4x %4x\n",
203 ao_log_dump.u.anon.d0,
204 ao_log_dump.u.anon.d1);
205 if (ao_log_dump.type == AO_LOG_STATE &&
206 ao_log_dump.u.state.state == ao_flight_landed)
212 __code struct ao_cmds ao_log_cmds[] = {
213 { 'l', dump_log, "l Dump last flight log" },
214 { 0, dump_log, NULL },
217 static __xdata struct ao_task ao_log_task;
224 /* For now, just log the flight starting at the begining of eeprom */
225 ao_log_start_pos = 0;
226 ao_log_current_pos = ao_log_start_pos;
227 ao_log_state = ao_flight_invalid;
229 /* Create a task to log events to eeprom */
230 ao_add_task(&ao_log_task, ao_log, "log");
231 ao_cmd_register(&ao_log_cmds[0]);