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