387eb6eacf2f0331b0564987d0df758bf18170e4
[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 static uint8_t
39 ao_log_csum(__xdata uint8_t *b) __reentrant
40 {
41         uint8_t sum = 0x5a;
42         uint8_t i;
43
44         for (i = 0; i < sizeof (struct ao_log_telescience); i++)
45                 sum += *b++;
46         return -sum;
47 }
48
49 static uint8_t
50 ao_log_telescience_write(void)
51 {
52         uint8_t wrote = 0;
53
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)
58                         ao_log_stop();
59                 if (ao_log_running) {
60                         wrote = 1;
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);
65                 }
66         } ao_mutex_put(&ao_log_mutex);
67         return wrote;
68 }
69
70 static uint8_t
71 ao_log_valid(struct ao_log_telescience *log)
72 {
73         uint8_t *d;
74         uint8_t i;
75         d = (uint8_t *) log;
76         for (i = 0; i < sizeof (struct ao_log_telescience); i++)
77                 if (d[i] != 0xff)
78                         return 1;
79         return 0;
80 }
81
82 static uint8_t
83 ao_log_telescience_read(uint32_t pos)
84 {
85         if (!ao_storage_read(pos, &ao_log_fetch, sizeof (struct ao_log_telescience)))
86                 return 0;
87         return ao_log_valid(&ao_log_fetch);
88 }
89
90 void
91 ao_log_start(void)
92 {
93         if (!ao_log_running) {
94                 ao_log_running = 1;
95                 ao_wakeup(&ao_log_running);
96         }
97 }
98
99 void
100 ao_log_stop(void)
101 {
102         if (ao_log_running) {
103                 ao_log_running = 0;
104         }
105 }
106
107 void
108 ao_log_restart(void)
109 {
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)
116         {
117                 printf("reading %ld\n", ao_log_current_pos); flush();
118                 if (!ao_log_telescience_read(ao_log_current_pos))
119                         break;
120         }
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))
126                 {
127                         if (!ao_log_telescience_read(ao_log_current_pos))
128                                 break;
129                 }
130         }
131         printf("Logging will start at %ld\n", ao_log_current_pos); flush();
132 }
133
134 void
135 ao_log_telescience(void)
136 {
137         ao_storage_setup();
138
139         /* This can take a while, so let the rest
140          * of the system finish booting before we start
141          */
142         ao_delay(AO_SEC_TO_TICKS(10));
143
144         ao_log_restart();
145         for (;;) {
146                 while (!ao_log_running)
147                         ao_sleep(&ao_log_running);
148
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
156                  * when starting up.
157                  */
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);
168                         }
169                         /* Wait for more ADC data to arrive */
170                         ao_sleep((void *) &ao_adc_head);
171                 }
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));
175         }
176 }
177
178 void
179 ao_log_set(void)
180 {
181         printf("Logging currently %s\n", ao_log_running ? "on" : "off");
182         ao_cmd_hex();
183         if (ao_cmd_status == ao_cmd_success) {
184                 if (ao_cmd_lex_i) {
185                         printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
186                         ao_log_start();
187                 } else {
188                         printf ("Log stopped at %ld\n", ao_log_current_pos);
189                         ao_log_stop();
190                 }
191         }
192         ao_cmd_status = ao_cmd_success;
193 }
194
195 void
196 ao_log_list(void)
197 {
198         uint32_t        pos;
199         uint32_t        start = 0;
200         uint8_t         flight = 0;
201
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)
206                 {
207                         if (pos != start) {
208                                 printf("flight %d start %x end %x\n",
209                                        flight,
210                                        (uint16_t) (start >> 8),
211                                        (uint16_t) ((pos + 0xff) >> 8)); flush();
212                         }
213                         if (ao_log_fetch.type != AO_LOG_TELESCIENCE_START)
214                                 break;
215                         start = pos;
216                         flight++;
217                 }
218         }
219         printf ("done\n");
220 }
221
222 void
223 ao_log_delete(void)
224 {
225         uint32_t        pos;
226
227         ao_cmd_hex();
228         if (ao_cmd_status != ao_cmd_success)
229                 return;
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);
233                 return;
234         }
235         ao_log_stop();
236         for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
237                 if (!ao_log_telescience_read(pos))
238                         break;
239                 ao_storage_erase(pos);
240         }
241         ao_log_current_pos = ao_log_start_pos = 0;
242         if (pos == 0)
243                 printf("No such flight: %d\n", ao_cmd_lex_i);
244         else
245                 printf ("Erased\n");
246 }
247
248 static void
249 ao_log_query(void)
250 {
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);
258 }
259
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" },
265         { 0,    NULL },
266 };
267
268 void
269 ao_log_init(void)
270 {
271         ao_log_running = 0;
272
273         ao_cmd_register(&ao_log_cmds[0]);
274
275         ao_add_task(&ao_log_task, ao_log_telescience, "log");
276 }