altos: Report log format in the version command
[fw/altos] / src-avr / ao_log_telescience.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19 #include "ao_product.h"
20
21 static struct ao_task ao_log_task;
22 //static struct ao_task ao_spi_task;
23
24 uint8_t         ao_log_running;
25 uint8_t         ao_log_mutex;
26 uint32_t        ao_log_start_pos;
27 uint32_t        ao_log_end_pos;
28 uint32_t        ao_log_current_pos;
29
30 #define AO_LOG_TELESCIENCE_START        ((uint8_t) 's')
31 #define AO_LOG_TELESCIENCE_DATA         ((uint8_t) 'd')
32
33 struct ao_log_telescience ao_log_store;
34 struct ao_log_telescience ao_log_fetch;
35
36 static uint8_t  ao_log_adc_pos;
37
38 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELESCIENCE;
39
40 static uint8_t
41 ao_log_csum(__xdata uint8_t *b) __reentrant
42 {
43         uint8_t sum = 0x5a;
44         uint8_t i;
45
46         for (i = 0; i < sizeof (struct ao_log_telescience); i++)
47                 sum += *b++;
48         return -sum;
49 }
50
51 static uint8_t
52 ao_log_telescience_write(void)
53 {
54         uint8_t wrote = 0;
55
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)
60                         ao_log_stop();
61                 if (ao_log_running) {
62                         wrote = 1;
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);
67                 }
68         } ao_mutex_put(&ao_log_mutex);
69         return wrote;
70 }
71
72 static uint8_t
73 ao_log_valid(struct ao_log_telescience *log)
74 {
75         uint8_t *d;
76         uint8_t i;
77         d = (uint8_t *) log;
78         for (i = 0; i < sizeof (struct ao_log_telescience); i++)
79                 if (d[i] != 0xff)
80                         return 1;
81         return 0;
82 }
83
84 static uint8_t
85 ao_log_telescience_read(uint32_t pos)
86 {
87         if (!ao_storage_read(pos, &ao_log_fetch, sizeof (struct ao_log_telescience)))
88                 return 0;
89         return ao_log_valid(&ao_log_fetch);
90 }
91
92 void
93 ao_log_start(void)
94 {
95         if (!ao_log_running) {
96                 ao_log_running = 1;
97                 ao_wakeup(&ao_log_running);
98         }
99 }
100
101 void
102 ao_log_stop(void)
103 {
104         if (ao_log_running) {
105                 ao_log_running = 0;
106         }
107 }
108
109 void
110 ao_log_restart(void)
111 {
112         printf("Finding end of current data...\n"); flush();
113         /* Find end of data */
114         ao_log_end_pos = ao_storage_config;
115         for (ao_log_current_pos = 0;
116              ao_log_current_pos < ao_storage_config;
117              ao_log_current_pos += ao_storage_block)
118         {
119                 printf("reading %ld\n", ao_log_current_pos); flush();
120                 if (!ao_log_telescience_read(ao_log_current_pos))
121                         break;
122         }
123         printf("last block is at %ld\n", ao_log_current_pos); flush();
124         if (ao_log_current_pos > 0) {
125                 ao_log_current_pos -= ao_storage_block;
126                 for (; ao_log_current_pos < ao_storage_config;
127                      ao_log_current_pos += sizeof (struct ao_log_telescience))
128                 {
129                         if (!ao_log_telescience_read(ao_log_current_pos))
130                                 break;
131                 }
132         }
133         printf("Logging will start at %ld\n", ao_log_current_pos); flush();
134 }
135
136 void
137 ao_log_telescience(void)
138 {
139         ao_storage_setup();
140
141         /* This can take a while, so let the rest
142          * of the system finish booting before we start
143          */
144         ao_delay(AO_SEC_TO_TICKS(10));
145
146         ao_log_restart();
147         for (;;) {
148                 while (!ao_log_running)
149                         ao_sleep(&ao_log_running);
150
151                 ao_log_start_pos = ao_log_current_pos;
152                 printf("Start logging at %ld state %d\n",
153                        ao_log_current_pos, ao_log_store.tm_state); flush();
154                 ao_log_store.type = AO_LOG_TELESCIENCE_START;
155                 ao_log_store.tick = ao_time();
156                 ao_log_telescience_write();
157                 /* Write the whole contents of the ring to the log
158                  * when starting up.
159                  */
160                 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
161                 ao_log_store.type = AO_LOG_TELESCIENCE_DATA;
162                 while (ao_log_running) {
163                         /* Write samples to EEPROM */
164                         while (ao_log_adc_pos != ao_adc_head) {
165                                 ao_log_store.tick = ao_adc_ring[ao_log_adc_pos].tick;
166                                 memcpy(&ao_log_store.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
167                                        NUM_ADC * sizeof (uint16_t));
168                                 ao_log_telescience_write();
169                                 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
170                         }
171                         /* Wait for more ADC data to arrive */
172                         ao_sleep((void *) &ao_adc_head);
173                 }
174                 printf("Stop logging at %ld state %d\n",
175                        ao_log_current_pos, ao_log_store.tm_state); flush();
176                 memset(&ao_log_store.adc, '\0', sizeof (ao_log_store.adc));
177         }
178 }
179
180 void
181 ao_log_set(void)
182 {
183         printf("Logging currently %s\n", ao_log_running ? "on" : "off");
184         ao_cmd_hex();
185         if (ao_cmd_status == ao_cmd_success) {
186                 if (ao_cmd_lex_i) {
187                         printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
188                         ao_log_start();
189                 } else {
190                         printf ("Log stopped at %ld\n", ao_log_current_pos);
191                         ao_log_stop();
192                 }
193         }
194         ao_cmd_status = ao_cmd_success;
195 }
196
197 void
198 ao_log_list(void)
199 {
200         uint32_t        pos;
201         uint32_t        start = 0;
202         uint8_t         flight = 0;
203
204         for (pos = 0; ; pos += sizeof (struct ao_log_telescience)) {
205                 if (pos >= ao_storage_config ||
206                     !ao_log_telescience_read(pos) ||
207                     ao_log_fetch.type == AO_LOG_TELESCIENCE_START)
208                 {
209                         if (pos != start) {
210                                 printf("flight %d start %x end %x\n",
211                                        flight,
212                                        (uint16_t) (start >> 8),
213                                        (uint16_t) ((pos + 0xff) >> 8)); flush();
214                         }
215                         if (ao_log_fetch.type != AO_LOG_TELESCIENCE_START)
216                                 break;
217                         start = pos;
218                         flight++;
219                 }
220         }
221         printf ("done\n");
222 }
223
224 void
225 ao_log_delete(void)
226 {
227         uint32_t        pos;
228
229         ao_cmd_hex();
230         if (ao_cmd_status != ao_cmd_success)
231                 return;
232         if (ao_cmd_lex_i != 1) {
233                 ao_cmd_status = ao_cmd_syntax_error;
234                 printf("No such flight: %d\n", ao_cmd_lex_i);
235                 return;
236         }
237         ao_log_stop();
238         for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
239                 if (!ao_log_telescience_read(pos))
240                         break;
241                 ao_storage_erase(pos);
242         }
243         ao_log_current_pos = ao_log_start_pos = 0;
244         if (pos == 0)
245                 printf("No such flight: %d\n", ao_cmd_lex_i);
246         else
247                 printf ("Erased\n");
248 }
249
250 static void
251 ao_log_query(void)
252 {
253         printf("Logging enabled: %d\n", ao_log_running);
254         printf("Log start: %ld\n", ao_log_start_pos);
255         printf("Log cur: %ld\n", ao_log_current_pos);
256         printf("Log end: %ld\n", ao_log_end_pos);
257         printf("log data tick: %04x\n", ao_log_store.tick);
258         printf("TM data tick: %04x\n", ao_log_store.tm_tick);
259         printf("TM state: %d\n", ao_log_store.tm_state);
260 }
261
262 const struct ao_cmds ao_log_cmds[] = {
263         { ao_log_set,   "L <0 off, 1 on>\0Set logging mode" },
264         { ao_log_list,  "l\0List stored flight logs" },
265         { ao_log_delete, "d 1\0Delete all stored flights" },
266         { ao_log_query, "q\0Query log status" },
267         { 0,    NULL },
268 };
269
270 void
271 ao_log_init(void)
272 {
273         ao_log_running = 0;
274
275         ao_cmd_register(&ao_log_cmds[0]);
276
277         ao_add_task(&ao_log_task, ao_log_telescience, "log");
278 }