altos: Integrate telescience support
[fw/altos] / src / core / 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         /* 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)
117         {
118                 if (!ao_log_telescience_read(ao_log_current_pos))
119                         break;
120         }
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))
125                 {
126                         if (!ao_log_telescience_read(ao_log_current_pos))
127                                 break;
128                 }
129         }
130 }
131
132 void
133 ao_log_telescience(void)
134 {
135         ao_storage_setup();
136
137         /* This can take a while, so let the rest
138          * of the system finish booting before we start
139          */
140         ao_delay(AO_SEC_TO_TICKS(10));
141
142         ao_log_restart();
143         for (;;) {
144                 while (!ao_log_running)
145                         ao_sleep(&ao_log_running);
146
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
154                  * when starting up.
155                  */
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);
166                         }
167                         /* Wait for more ADC data to arrive */
168                         ao_sleep((void *) &ao_adc_head);
169                 }
170                 memset(&ao_log_store.adc, '\0', sizeof (ao_log_store.adc));
171         }
172 }
173
174 void
175 ao_log_set(void)
176 {
177         printf("Logging currently %s\n", ao_log_running ? "on" : "off");
178         ao_cmd_hex();
179         if (ao_cmd_status == ao_cmd_success) {
180                 if (ao_cmd_lex_i) {
181                         printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
182                         ao_log_start();
183                 } else {
184                         printf ("Log stopped at %ld\n", ao_log_current_pos);
185                         ao_log_stop();
186                 }
187         }
188         ao_cmd_status = ao_cmd_success;
189 }
190
191 void
192 ao_log_list(void)
193 {
194         uint32_t        pos;
195         uint32_t        start = 0;
196         uint8_t         flight = 0;
197
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)
202                 {
203                         if (pos != start) {
204                                 printf("flight %d start %x end %x\n",
205                                        flight,
206                                        (uint16_t) (start >> 8),
207                                        (uint16_t) ((pos + 0xff) >> 8)); flush();
208                         }
209                         if (ao_log_fetch.type != AO_LOG_TELESCIENCE_START)
210                                 break;
211                         start = pos;
212                         flight++;
213                 }
214         }
215         printf ("done\n");
216 }
217
218 void
219 ao_log_delete(void)
220 {
221         uint32_t        pos;
222
223         ao_cmd_hex();
224         if (ao_cmd_status != ao_cmd_success)
225                 return;
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);
229                 return;
230         }
231         ao_log_stop();
232         for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
233                 if (!ao_log_telescience_read(pos))
234                         break;
235                 ao_storage_erase(pos);
236         }
237         ao_log_current_pos = ao_log_start_pos = 0;
238         if (pos == 0)
239                 printf("No such flight: %d\n", ao_cmd_lex_i);
240         else
241                 printf ("Erased\n");
242 }
243
244 static void
245 ao_log_query(void)
246 {
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);
256 }
257
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" },
263         { 0,    NULL },
264 };
265
266 void
267 ao_log_init(void)
268 {
269         ao_log_running = 0;
270
271         ao_cmd_register(&ao_log_cmds[0]);
272
273         ao_add_task(&ao_log_task, ao_log_telescience, "log");
274 }