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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 __data uint32_t ao_log_current_pos;
22 __data uint32_t ao_log_start_pos;
23 __xdata uint8_t ao_log_running;
24 __xdata uint8_t ao_log_mutex;
27 ao_log_csum(uint8_t *b)
32 for (i = 0; i < sizeof (struct ao_log_record); i++)
38 ao_log_data(struct ao_log_record *log)
42 log->csum = ao_log_csum((uint8_t *) log);
43 ao_mutex_get(&ao_log_mutex); {
45 ao_ee_write(ao_log_current_pos,
47 sizeof (struct ao_log_record));
48 ao_log_current_pos += sizeof (struct ao_log_record);
49 if (ao_log_current_pos >= AO_EE_DATA_SIZE)
50 ao_log_current_pos = 0;
51 if (ao_log_current_pos == ao_log_start_pos)
54 } ao_mutex_put(&ao_log_mutex);
63 __xdata struct ao_log_record ao_log_dump;
64 static __data uint16_t ao_log_dump_flight;
65 static __data uint32_t ao_log_dump_pos;
68 ao_log_dump_check_data(void)
70 if (ao_log_csum((uint8_t *) &ao_log_dump) != 0)
76 ao_log_dump_scan(void)
78 if (!ao_ee_read(0, (uint8_t *) &ao_log_dump, sizeof (struct ao_log_record)))
79 ao_panic(AO_PANIC_LOG);
80 if (ao_log_dump_check_data() && ao_log_dump.type == AO_LOG_FLIGHT) {
81 ao_log_dump_flight = ao_log_dump.u.flight.flight;
84 ao_log_dump_flight = 0;
90 ao_log_dump_first(void)
93 if (!ao_log_dump_scan())
99 ao_log_dump_next(void)
101 ao_log_dump_pos += sizeof (struct ao_log_record);
102 if (ao_log_dump_pos >= AO_EE_DEVICE_SIZE)
104 if (!ao_ee_read(ao_log_dump_pos, (uint8_t *) &ao_log_dump,
105 sizeof (struct ao_log_record)))
107 return ao_log_dump_check_data();
110 uint8_t ao_log_adc_pos;
111 enum flight_state ao_log_state;
116 static __xdata struct ao_log_record log;
120 while (!ao_log_running)
121 ao_sleep(&ao_log_running);
123 log.type = AO_LOG_FLIGHT;
124 log.tick = ao_flight_state_tick;
125 log.u.flight.serial = 0;
126 log.u.flight.flight = ao_log_dump_flight + 1;
129 /* Write state change to EEPROM */
130 if (ao_flight_state != ao_log_state) {
131 ao_log_state = ao_flight_state;
132 log.type = AO_LOG_STATE;
133 log.tick = ao_flight_state_tick;
134 log.u.state.state = ao_log_state;
135 log.u.state.reason = 0;
138 /* Write samples to EEPROM */
139 while (ao_log_adc_pos != ao_adc_head) {
140 log.type = AO_LOG_SENSOR;
141 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
142 log.u.sensor.accel = ao_adc_ring[ao_log_adc_pos].accel;
143 log.u.sensor.pres = ao_adc_ring[ao_log_adc_pos].pres;
145 if (ao_log_adc_pos == 0) {
146 log.type = AO_LOG_TEMP_VOLT;
147 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
148 log.u.temp_volt.temp = ao_adc_ring[ao_log_adc_pos].temp;
149 log.u.temp_volt.v_batt = ao_adc_ring[ao_log_adc_pos].v_batt;
151 log.type = AO_LOG_DEPLOY;
152 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
153 log.u.deploy.drogue = ao_adc_ring[ao_log_adc_pos].sense_d;
154 log.u.deploy.main = ao_adc_ring[ao_log_adc_pos].sense_m;
158 if (ao_log_adc_pos == AO_ADC_RING)
162 /* Wait for a while */
163 ao_delay(AO_MS_TO_TICKS(100));
172 ao_wakeup(&ao_log_running);
175 static __xdata struct ao_task ao_log_task;
182 /* For now, just log the flight starting at the begining of eeprom */
183 ao_log_start_pos = 0;
184 ao_log_current_pos = ao_log_start_pos;
185 ao_log_state = ao_flight_invalid;
187 /* Create a task to log events to eeprom */
188 ao_add_task(&ao_log_task, ao_log);