altos: Make telescience reliably log only when told to
[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
20 static struct ao_task ao_log_task;
21
22 uint8_t         ao_log_running;
23 uint8_t         ao_log_mutex;
24 uint32_t        ao_log_start_pos;
25 uint32_t        ao_log_end_pos;
26 uint32_t        ao_log_current_pos;
27
28 #define AO_LOG_TELESCIENCE_START        ((uint8_t) 's')
29 #define AO_LOG_TELESCIENCE_DATA         ((uint8_t) 'd')
30
31 struct ao_log_telescience {
32         uint8_t         type;
33         uint8_t         csum;
34         uint16_t        tick;
35         union {
36                 uint8_t         bytes[28];
37                 uint16_t        adc[NUM_ADC];
38         } u;
39 };
40
41 static struct ao_log_telescience log;
42 static uint8_t  ao_log_adc_pos;
43
44 static uint8_t
45 ao_log_csum(__xdata uint8_t *b) __reentrant
46 {
47         uint8_t sum = 0x5a;
48         uint8_t i;
49
50         for (i = 0; i < sizeof (struct ao_log_telescience); i++)
51                 sum += *b++;
52         return -sum;
53 }
54
55 uint8_t
56 ao_log_telescience_data(struct ao_log_telescience *log)
57 {
58         uint8_t wrote = 0;
59
60         log->csum = 0;
61         log->csum = ao_log_csum((__xdata uint8_t *) log);
62         ao_mutex_get(&ao_log_mutex); {
63                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
64                         ao_log_stop();
65                 if (ao_log_running) {
66                         wrote = 1;
67                         ao_storage_write(ao_log_current_pos,
68                                          log,
69                                          sizeof (struct ao_log_telescience));
70                         ao_log_current_pos += sizeof (struct ao_log_telescience);
71                 }
72         } ao_mutex_put(&ao_log_mutex);
73         return wrote;
74 }
75
76 static uint8_t
77 ao_log_valid(struct ao_log_telescience *log)
78 {
79         uint8_t *d;
80         uint8_t i;
81         d = (uint8_t *) log;
82         for (i = 0; i < sizeof (struct ao_log_telescience); i++)
83                 if (d[i] != 0xff)
84                         return 1;
85         return 0;
86 }
87
88 void
89 ao_log_start(void)
90 {
91         ao_log_running = 1;
92         ao_wakeup(&ao_log_running);
93 }
94
95 void
96 ao_log_stop(void)
97 {
98         ao_log_running = 0;
99         ao_wakeup((void *) &ao_adc_head);
100 }
101
102 void
103 ao_log_check_pin(void)
104 {
105         if (PINB & (1 << PINB0))
106                 ao_log_stop();
107         else
108                 ao_log_start();
109 }
110
111 void
112 ao_log_telescience(void)
113 {
114         ao_storage_setup();
115
116         /* Find end of data */
117         while (ao_log_start_pos < ao_log_end_pos) {
118                 if (!(ao_storage_read(ao_log_start_pos, &log, sizeof (struct ao_log_telescience))))
119                         break;
120                 if (!ao_log_valid(&log))
121                         break;
122         }
123
124         /*
125          * Wait for the other side to settle down
126          */
127         ao_delay(AO_SEC_TO_TICKS(5));
128
129         ao_log_check_pin();
130
131         ao_log_current_pos = ao_log_start_pos;
132         ao_log_end_pos = ao_storage_config;
133         for (;;) {
134                 while (!ao_log_running)
135                         ao_sleep(&ao_log_running);
136
137                 flush();
138                 memset(&log, '\0', sizeof (struct ao_log_telescience));
139                 log.type = AO_LOG_TELESCIENCE_START;
140                 log.tick = ao_time();
141                 ao_log_telescience_data(&log);
142                 /* Write the whole contents of the ring to the log
143                  * when starting up.
144                  */
145                 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
146                 log.type = AO_LOG_TELESCIENCE_DATA;
147                 while (ao_log_running) {
148                         /* Write samples to EEPROM */
149                         while (ao_log_adc_pos != ao_adc_head) {
150                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
151                                 memcpy(&log.u.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
152                                        NUM_ADC * sizeof (uint16_t));
153                                 ao_log_telescience_data(&log);
154                                 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
155                         }
156                         /* Wait for more ADC data to arrive */
157                         ao_sleep((void *) &ao_adc_head);
158                 }
159                 flush();
160         }
161 }
162
163 void
164 ao_log_set(void)
165 {
166         printf("Logging currently %s\n", ao_log_running ? "on" : "off");
167         ao_cmd_hex();
168         if (ao_cmd_status == ao_cmd_success) {
169                 if (ao_cmd_lex_i) {
170                         printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
171                         ao_log_start();
172                 } else {
173                         printf ("Log stopped at %ld\n", ao_log_current_pos);
174                         ao_log_stop();
175                 }
176         }
177         ao_cmd_status = ao_cmd_success;
178 }
179
180 void
181 ao_log_list(void)
182 {
183         uint32_t        pos;
184         uint32_t        start = 0;
185         uint8_t         flight = 0;
186
187         for (pos = 0; ; pos += sizeof (struct ao_log_telescience)) {
188                 if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
189                         break;
190                 if (!ao_log_valid(&log) || log.type == AO_LOG_TELESCIENCE_START) {
191                         if (pos != start) {
192                                 printf("flight %d start %x end %x\n",
193                                        flight,
194                                        (uint16_t) (start >> 8),
195                                        (uint16_t) ((pos + 0xff) >> 8));
196                         }
197                         if (!ao_log_valid(&log))
198                                 break;
199                         start = pos;
200                         flight++;
201                 }
202         }
203         printf ("done\n");
204 }
205
206 void
207 ao_log_delete(void)
208 {
209         uint32_t        pos;
210
211         ao_cmd_hex();
212         if (ao_cmd_status != ao_cmd_success)
213                 return;
214         if (ao_cmd_lex_i != 1) {
215                 ao_cmd_status = ao_cmd_syntax_error;
216                 printf("No such flight: %d\n", ao_cmd_lex_i);
217                 return;
218         }
219         ao_log_stop();
220         for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
221                 if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
222                         break;
223                 if (!ao_log_valid(&log))
224                         break;
225                 ao_storage_erase(pos);
226         }
227         ao_log_current_pos = ao_log_start_pos = 0;
228         if (pos == 0)
229                 printf("No such flight: %d\n", ao_cmd_lex_i);
230         else
231                 printf ("Erased\n");
232 }
233
234 const struct ao_cmds ao_log_cmds[] = {
235         { ao_log_set,   "L <0 off, 1 on>\0Set logging mode" },
236         { ao_log_list,  "l\0List stored flight logs" },
237         { ao_log_delete, "d 1\0Delete all stored flights" },
238         { 0,    NULL },
239 };
240
241 ISR(PCINT0_vect)
242 {
243         ao_log_check_pin();
244 }
245
246 void
247 ao_log_init(void)
248 {
249         ao_log_running = 0;
250
251         DDRB &= ~(1 << DDB0);
252
253         PORTB |= (1 << PORTB0);         /* Pull input up; require input to log */
254
255         PCMSK0 |= (1 << PCINT0);        /* Enable PCINT0 pin change */
256
257         PCICR |= (1 << PCIE0);          /* Enable pin change interrupt */
258
259         ao_cmd_register(&ao_log_cmds[0]);
260
261         ao_add_task(&ao_log_task, ao_log_telescience, "log");
262 }