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 ao_log_store;
34 struct ao_log_telescience ao_log_fetch;
36 static uint8_t ao_log_adc_pos;
38 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELESCIENCE;
41 ao_log_csum(__xdata uint8_t *b) __reentrant
46 for (i = 0; i < sizeof (struct ao_log_telescience); i++)
52 ao_log_telescience_write(void)
56 ao_log_store.csum = 0;
57 ao_log_store.csum = ao_log_csum((__xdata uint8_t *) &ao_log_store);
58 ao_mutex_get(&ao_log_mutex); {
59 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
63 ao_storage_write(ao_log_current_pos,
64 (__xdata uint8_t *) &ao_log_store,
65 sizeof (struct ao_log_telescience));
66 ao_log_current_pos += sizeof (struct ao_log_telescience);
68 } ao_mutex_put(&ao_log_mutex);
73 ao_log_valid(struct ao_log_telescience *log)
78 for (i = 0; i < sizeof (struct ao_log_telescience); i++)
85 ao_log_telescience_read(uint32_t pos)
87 if (!ao_storage_read(pos, &ao_log_fetch, sizeof (struct ao_log_telescience)))
89 return ao_log_valid(&ao_log_fetch);
95 if (!ao_log_running) {
97 ao_wakeup(&ao_log_running);
104 if (ao_log_running) {
112 /* Find end of data */
113 ao_log_end_pos = ao_storage_config;
114 for (ao_log_current_pos = 0;
115 ao_log_current_pos < ao_storage_config;
116 ao_log_current_pos += ao_storage_block)
118 if (!ao_log_telescience_read(ao_log_current_pos))
121 if (ao_log_current_pos > 0) {
122 ao_log_current_pos -= ao_storage_block;
123 for (; ao_log_current_pos < ao_storage_config;
124 ao_log_current_pos += sizeof (struct ao_log_telescience))
126 if (!ao_log_telescience_read(ao_log_current_pos))
133 ao_log_telescience(void)
137 /* This can take a while, so let the rest
138 * of the system finish booting before we start
140 ao_delay(AO_SEC_TO_TICKS(10));
144 while (!ao_log_running)
145 ao_sleep(&ao_log_running);
147 ao_log_start_pos = ao_log_current_pos;
148 ao_log_store.type = AO_LOG_TELESCIENCE_START;
149 ao_log_store.tick = ao_time();
150 ao_log_store.adc[0] = ao_companion_command.serial;
151 ao_log_store.adc[1] = ao_companion_command.flight;
152 ao_log_telescience_write();
153 /* Write the whole contents of the ring to the log
156 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
157 ao_log_store.type = AO_LOG_TELESCIENCE_DATA;
158 while (ao_log_running) {
159 /* Write samples to EEPROM */
160 while (ao_log_adc_pos != ao_adc_head) {
161 ao_log_store.tick = ao_adc_ring[ao_log_adc_pos].tick;
162 memcpy(&ao_log_store.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
163 AO_LOG_TELESCIENCE_NUM_ADC * sizeof (uint16_t));
164 ao_log_telescience_write();
165 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
167 /* Wait for more ADC data to arrive */
168 ao_sleep((void *) &ao_adc_head);
170 memset(&ao_log_store.adc, '\0', sizeof (ao_log_store.adc));
177 printf("Logging currently %s\n", ao_log_running ? "on" : "off");
179 if (ao_cmd_status == ao_cmd_success) {
181 printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
184 printf ("Log stopped at %ld\n", ao_log_current_pos);
188 ao_cmd_status = ao_cmd_success;
198 for (pos = 0; ; pos += sizeof (struct ao_log_telescience)) {
199 if (pos >= ao_storage_config ||
200 !ao_log_telescience_read(pos) ||
201 ao_log_fetch.type == AO_LOG_TELESCIENCE_START)
204 printf("flight %d start %x end %x\n",
206 (uint16_t) (start >> 8),
207 (uint16_t) ((pos + 0xff) >> 8)); flush();
209 if (ao_log_fetch.type != AO_LOG_TELESCIENCE_START)
224 if (ao_cmd_status != ao_cmd_success)
226 if (ao_cmd_lex_i != 1) {
227 ao_cmd_status = ao_cmd_syntax_error;
228 printf("No such flight: %d\n", ao_cmd_lex_i);
232 for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
233 if (!ao_log_telescience_read(pos))
235 ao_storage_erase(pos);
237 ao_log_current_pos = ao_log_start_pos = 0;
239 printf("No such flight: %d\n", ao_cmd_lex_i);
247 printf("Logging enabled: %d\n", ao_log_running);
248 printf("Log start: %ld\n", ao_log_start_pos);
249 printf("Log cur: %ld\n", ao_log_current_pos);
250 printf("Log end: %ld\n", ao_log_end_pos);
251 printf("log data tick: %04x\n", ao_log_store.tick);
252 printf("TM data tick: %04x\n", ao_log_store.tm_tick);
253 printf("TM state: %d\n", ao_log_store.tm_state);
254 printf("TM serial: %d\n", ao_companion_command.serial);
255 printf("TM flight: %d\n", ao_companion_command.flight);
258 const struct ao_cmds ao_log_cmds[] = {
259 { ao_log_set, "L <0 off, 1 on>\0Set logging mode" },
260 { ao_log_list, "l\0List stored flight logs" },
261 { ao_log_delete, "d 1\0Delete all stored flights" },
262 { ao_log_query, "q\0Query log status" },
271 ao_cmd_register(&ao_log_cmds[0]);
273 ao_add_task(&ao_log_task, ao_log_telescience, "log");