2 * Copyright © 2011 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 __xdata uint8_t ao_log_mutex;
21 static __xdata struct ao_log_record log;
24 ao_log_csum(__xdata uint8_t *b) __reentrant
29 for (i = 0; i < sizeof (struct ao_log_record); i++)
35 ao_log_data(__xdata struct ao_log_record *log) __reentrant
40 log->csum = ao_log_csum((__xdata uint8_t *) log);
41 ao_mutex_get(&ao_log_mutex); {
42 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
46 ao_storage_write(ao_log_current_pos,
48 sizeof (struct ao_log_record));
49 ao_log_current_pos += sizeof (struct ao_log_record);
51 } ao_mutex_put(&ao_log_mutex);
56 ao_log_dump_check_data(void)
58 if (ao_log_csum((uint8_t *) &log) != 0)
63 static __xdata uint8_t ao_log_adc_pos;
65 /* a hack to make sure that ao_log_records fill the eeprom block in even units */
66 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_record))] ;
75 while (!ao_log_running)
76 ao_sleep(&ao_log_running);
78 log.type = AO_LOG_FLIGHT;
79 log.tick = ao_flight_tick;
81 log.u.flight.ground_accel = ao_ground_accel;
83 log.u.flight.flight = ao_flight_number;
86 /* Write the whole contents of the ring to the log
89 ao_log_adc_pos = ao_adc_ring_next(ao_flight_adc);
91 /* Write samples to EEPROM */
92 while (ao_log_adc_pos != ao_flight_adc) {
93 log.type = AO_LOG_SENSOR;
94 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
95 log.u.sensor.accel = ao_adc_ring[ao_log_adc_pos].accel;
96 log.u.sensor.pres = ao_adc_ring[ao_log_adc_pos].pres;
98 if ((ao_log_adc_pos & 0x1f) == 0) {
99 log.type = AO_LOG_TEMP_VOLT;
100 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
101 log.u.temp_volt.temp = ao_adc_ring[ao_log_adc_pos].temp;
102 log.u.temp_volt.v_batt = ao_adc_ring[ao_log_adc_pos].v_batt;
104 log.type = AO_LOG_DEPLOY;
105 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
106 log.u.deploy.drogue = ao_adc_ring[ao_log_adc_pos].sense_d;
107 log.u.deploy.main = ao_adc_ring[ao_log_adc_pos].sense_m;
110 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
112 /* Write state change to EEPROM */
113 if (ao_flight_state != ao_log_state) {
114 ao_log_state = ao_flight_state;
115 log.type = AO_LOG_STATE;
116 log.tick = ao_flight_tick;
117 log.u.state.state = ao_log_state;
118 log.u.state.reason = 0;
121 if (ao_log_state == ao_flight_landed)
125 /* Wait for a while */
126 ao_delay(AO_MS_TO_TICKS(100));
128 /* Stop logging when told to */
129 while (!ao_log_running)
130 ao_sleep(&ao_log_running);
135 ao_log_flight(uint8_t slot)
137 if (!ao_storage_read(ao_log_pos(slot),
139 sizeof (struct ao_log_record)))
142 if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
143 return log.u.flight.flight;