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.
19 #include "ao_product.h"
21 static struct ao_task ao_log_task;
22 static struct ao_task ao_spi_task;
24 uint8_t ao_log_running;
26 uint32_t ao_log_start_pos;
27 uint32_t ao_log_end_pos;
28 uint32_t ao_log_current_pos;
30 #define AO_LOG_TELESCIENCE_START ((uint8_t) 's')
31 #define AO_LOG_TELESCIENCE_DATA ((uint8_t) 'd')
33 struct ao_log_telescience {
39 uint16_t adc[NUM_ADC];
43 static struct ao_log_telescience log;
44 static uint8_t ao_log_adc_pos;
47 ao_log_csum(__xdata uint8_t *b) __reentrant
52 for (i = 0; i < sizeof (struct ao_log_telescience); i++)
58 ao_log_telescience_data(struct ao_log_telescience *log)
63 log->csum = ao_log_csum((__xdata uint8_t *) log);
64 ao_mutex_get(&ao_log_mutex); {
65 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
69 ao_storage_write(ao_log_current_pos,
71 sizeof (struct ao_log_telescience));
72 ao_log_current_pos += sizeof (struct ao_log_telescience);
74 } ao_mutex_put(&ao_log_mutex);
79 ao_log_valid(struct ao_log_telescience *log)
84 for (i = 0; i < sizeof (struct ao_log_telescience); i++)
94 ao_wakeup(&ao_log_running);
101 ao_wakeup((void *) &ao_adc_head);
105 ao_log_check_pin(void)
107 if (PINB & (1 << PINB0))
114 ao_log_telescience(void)
118 /* Find end of data */
119 while (ao_log_start_pos < ao_log_end_pos) {
120 if (!(ao_storage_read(ao_log_start_pos, &log, sizeof (struct ao_log_telescience))))
122 if (!ao_log_valid(&log))
127 * Wait for the other side to settle down
129 ao_delay(AO_SEC_TO_TICKS(5));
133 ao_log_current_pos = ao_log_start_pos;
134 ao_log_end_pos = ao_storage_config;
136 while (!ao_log_running)
137 ao_sleep(&ao_log_running);
140 memset(&log, '\0', sizeof (struct ao_log_telescience));
141 log.type = AO_LOG_TELESCIENCE_START;
142 log.tick = ao_time();
143 ao_log_telescience_data(&log);
144 /* Write the whole contents of the ring to the log
147 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
148 log.type = AO_LOG_TELESCIENCE_DATA;
149 while (ao_log_running) {
150 /* Write samples to EEPROM */
151 while (ao_log_adc_pos != ao_adc_head) {
152 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
153 memcpy(&log.u.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
154 NUM_ADC * sizeof (uint16_t));
155 ao_log_telescience_data(&log);
156 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
158 /* Wait for more ADC data to arrive */
159 ao_sleep((void *) &ao_adc_head);
168 printf("Logging currently %s\n", ao_log_running ? "on" : "off");
170 if (ao_cmd_status == ao_cmd_success) {
172 printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
175 printf ("Log stopped at %ld\n", ao_log_current_pos);
179 ao_cmd_status = ao_cmd_success;
189 for (pos = 0; ; pos += sizeof (struct ao_log_telescience)) {
190 if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
192 if (!ao_log_valid(&log) || log.type == AO_LOG_TELESCIENCE_START) {
194 printf("flight %d start %x end %x\n",
196 (uint16_t) (start >> 8),
197 (uint16_t) ((pos + 0xff) >> 8));
199 if (!ao_log_valid(&log))
214 if (ao_cmd_status != ao_cmd_success)
216 if (ao_cmd_lex_i != 1) {
217 ao_cmd_status = ao_cmd_syntax_error;
218 printf("No such flight: %d\n", ao_cmd_lex_i);
222 for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
223 if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
225 if (!ao_log_valid(&log))
227 ao_storage_erase(pos);
229 ao_log_current_pos = ao_log_start_pos = 0;
231 printf("No such flight: %d\n", ao_cmd_lex_i);
237 ao_spi_telescience(void)
240 struct ao_companion_command command;
241 struct ao_companion_setup setup;
246 ao_spi_slave_read((uint8_t *) &u.command, sizeof (struct ao_companion_command));
247 switch (u.command.command) {
248 case AO_COMPANION_SETUP:
249 u.setup.board_id = AO_idProduct_NUMBER;
250 u.setup.board_id_inverse = ~AO_idProduct_NUMBER;
251 u.setup.update_period = 10;
252 u.setup.channels = NUM_ADC;
253 ao_spi_slave_write((uint8_t *) &u.setup,
254 sizeof (struct ao_companion_setup));
256 case AO_COMPANION_FETCH:
258 ao_spi_slave_write((uint8_t *) &u.data.adc,
259 NUM_ADC * sizeof (uint16_t));
260 if (u.command.flight_state >= ao_flight_boost &&
261 u.command.flight_state < ao_flight_landed) {
273 const struct ao_cmds ao_log_cmds[] = {
274 { ao_log_set, "L <0 off, 1 on>\0Set logging mode" },
275 { ao_log_list, "l\0List stored flight logs" },
276 { ao_log_delete, "d 1\0Delete all stored flights" },
285 ao_cmd_register(&ao_log_cmds[0]);
287 ao_add_task(&ao_log_task, ao_log_telescience, "log");
288 ao_add_task(&ao_spi_task, ao_spi_telescience, "spi");