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;
39 ao_log_csum(__xdata uint8_t *b) __reentrant
44 for (i = 0; i < sizeof (struct ao_log_telescience); i++)
50 ao_log_telescience_write(void)
54 ao_log_store.csum = 0;
55 ao_log_store.csum = ao_log_csum((__xdata uint8_t *) &ao_log_store);
56 ao_mutex_get(&ao_log_mutex); {
57 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
61 ao_storage_write(ao_log_current_pos,
62 (__xdata uint8_t *) &ao_log_store,
63 sizeof (struct ao_log_telescience));
64 ao_log_current_pos += sizeof (struct ao_log_telescience);
66 } ao_mutex_put(&ao_log_mutex);
71 ao_log_valid(struct ao_log_telescience *log)
76 for (i = 0; i < sizeof (struct ao_log_telescience); i++)
83 ao_log_telescience_read(uint32_t pos)
85 if (!ao_storage_read(pos, &ao_log_fetch, sizeof (struct ao_log_telescience)))
87 return ao_log_valid(&ao_log_fetch);
93 if (!ao_log_running) {
95 ao_wakeup(&ao_log_running);
102 if (ao_log_running) {
110 printf("Finding end of current data...\n"); flush();
111 /* Find end of data */
112 ao_log_end_pos = ao_storage_config;
113 for (ao_log_current_pos = 0;
114 ao_log_current_pos < ao_storage_config;
115 ao_log_current_pos += ao_storage_block)
117 printf("reading %ld\n", ao_log_current_pos); flush();
118 if (!ao_log_telescience_read(ao_log_current_pos))
121 printf("last block is at %ld\n", ao_log_current_pos); flush();
122 if (ao_log_current_pos > 0) {
123 ao_log_current_pos -= ao_storage_block;
124 for (; ao_log_current_pos < ao_storage_config;
125 ao_log_current_pos += sizeof (struct ao_log_telescience))
127 if (!ao_log_telescience_read(ao_log_current_pos))
131 printf("Logging will start at %ld\n", ao_log_current_pos); flush();
135 ao_log_telescience(void)
139 /* This can take a while, so let the rest
140 * of the system finish booting before we start
142 ao_delay(AO_SEC_TO_TICKS(10));
146 while (!ao_log_running)
147 ao_sleep(&ao_log_running);
149 ao_log_start_pos = ao_log_current_pos;
150 printf("Start logging at %ld state %d\n",
151 ao_log_current_pos, ao_log_store.tm_state); flush();
152 ao_log_store.type = AO_LOG_TELESCIENCE_START;
153 ao_log_store.tick = ao_time();
154 ao_log_telescience_write();
155 /* Write the whole contents of the ring to the log
158 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
159 ao_log_store.type = AO_LOG_TELESCIENCE_DATA;
160 while (ao_log_running) {
161 /* Write samples to EEPROM */
162 while (ao_log_adc_pos != ao_adc_head) {
163 ao_log_store.tick = ao_adc_ring[ao_log_adc_pos].tick;
164 memcpy(&ao_log_store.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
165 NUM_ADC * sizeof (uint16_t));
166 ao_log_telescience_write();
167 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
169 /* Wait for more ADC data to arrive */
170 ao_sleep((void *) &ao_adc_head);
172 printf("Stop logging at %ld state %d\n",
173 ao_log_current_pos, ao_log_store.tm_state); flush();
174 memset(&ao_log_store.adc, '\0', sizeof (ao_log_store.adc));
181 printf("Logging currently %s\n", ao_log_running ? "on" : "off");
183 if (ao_cmd_status == ao_cmd_success) {
185 printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
188 printf ("Log stopped at %ld\n", ao_log_current_pos);
192 ao_cmd_status = ao_cmd_success;
202 for (pos = 0; ; pos += sizeof (struct ao_log_telescience)) {
203 if (pos >= ao_storage_config ||
204 !ao_log_telescience_read(pos) ||
205 ao_log_fetch.type == AO_LOG_TELESCIENCE_START)
208 printf("flight %d start %x end %x\n",
210 (uint16_t) (start >> 8),
211 (uint16_t) ((pos + 0xff) >> 8)); flush();
213 if (ao_log_fetch.type != AO_LOG_TELESCIENCE_START)
228 if (ao_cmd_status != ao_cmd_success)
230 if (ao_cmd_lex_i != 1) {
231 ao_cmd_status = ao_cmd_syntax_error;
232 printf("No such flight: %d\n", ao_cmd_lex_i);
236 for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
237 if (!ao_log_telescience_read(pos))
239 ao_storage_erase(pos);
241 ao_log_current_pos = ao_log_start_pos = 0;
243 printf("No such flight: %d\n", ao_cmd_lex_i);
251 printf("Logging enabled: %d\n", ao_log_running);
252 printf("Log start: %ld\n", ao_log_start_pos);
253 printf("Log cur: %ld\n", ao_log_current_pos);
254 printf("Log end: %ld\n", ao_log_end_pos);
255 printf("log data tick: %04x\n", ao_log_store.tick);
256 printf("TM data tick: %04x\n", ao_log_store.tm_tick);
257 printf("TM state: %d\n", ao_log_store.tm_state);
260 const struct ao_cmds ao_log_cmds[] = {
261 { ao_log_set, "L <0 off, 1 on>\0Set logging mode" },
262 { ao_log_list, "l\0List stored flight logs" },
263 { ao_log_delete, "d 1\0Delete all stored flights" },
264 { ao_log_query, "q\0Query log status" },
273 ao_cmd_register(&ao_log_cmds[0]);
275 ao_add_task(&ao_log_task, ao_log_telescience, "log");