f092027a6db88bcc78424ea13ab1e677025856b4
[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_telescience(void)
90 {
91         ao_storage_setup();
92
93         /* Find end of data */
94         while (ao_log_start_pos < ao_log_end_pos) {
95                 if (!(ao_storage_read(ao_log_start_pos, &log, sizeof (struct ao_log_telescience))))
96                         break;
97                 if (!ao_log_valid(&log))
98                         break;
99         }
100         ao_log_current_pos = ao_log_start_pos;
101         ao_log_end_pos = ao_storage_config;
102         for (;;) {
103                 while (!ao_log_running)
104                         ao_sleep(&ao_log_running);
105
106                 memset(&log, '\0', sizeof (struct ao_log_telescience));
107                 log.type = AO_LOG_TELESCIENCE_START;
108                 log.tick = ao_time();
109                 ao_log_telescience_data(&log);
110                 /* Write the whole contents of the ring to the log
111                  * when starting up.
112                  */
113                 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
114                 log.type = AO_LOG_TELESCIENCE_DATA;
115                 while (ao_log_running) {
116                         /* Write samples to EEPROM */
117                         while (ao_log_adc_pos != ao_adc_head) {
118                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
119                                 memcpy(&log.u.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
120                                        NUM_ADC * sizeof (uint16_t));
121                                 ao_log_telescience_data(&log);
122                                 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
123                         }
124                         /* Wait for more ADC data to arrive */
125                         ao_sleep((void *) &ao_adc_head);
126                 }
127         }
128 }
129
130 void
131 ao_log_start(void)
132 {
133         printf("Log goes from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
134         ao_log_running = 1;
135         ao_wakeup(&ao_log_running);
136 }
137
138 void
139 ao_log_stop(void)
140 {
141         printf ("Log stopped at %ld\n", ao_log_current_pos);
142         ao_log_running = 0;
143         ao_wakeup((void *) &ao_adc_head);
144 }
145
146 void
147 ao_log_set(void)
148 {
149         ao_cmd_hex();
150         if (ao_cmd_status == ao_cmd_success) {
151                 if (ao_cmd_lex_i)
152                         ao_log_start();
153                 else
154                         ao_log_stop();
155         }
156 }
157
158 void
159 ao_log_list(void)
160 {
161         uint32_t        pos;
162         uint32_t        start = 0;
163         uint8_t         flight = 0;
164
165         for (pos = 0; pos < ao_storage_config; pos += sizeof (struct ao_log_telescience)) {
166                 if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
167                         break;
168                 if (!ao_log_valid(&log) || log.type == AO_LOG_TELESCIENCE_START) {
169                         if (pos != start) {
170                                 printf("flight %d start %x end %x\n",
171                                        flight,
172                                        (uint16_t) (start >> 8),
173                                        (uint16_t) ((pos + 0xff) >> 8));
174                         }
175                         if (!ao_log_valid(&log))
176                                 break;
177                         start = pos;
178                         flight++;
179                 }
180         }
181         printf ("done\n");
182 }
183
184 void
185 ao_log_delete(void)
186 {
187         uint32_t        pos;
188
189         ao_cmd_hex();
190         if (ao_cmd_status != ao_cmd_success)
191                 return;
192         if (ao_cmd_lex_i != 1) {
193                 ao_cmd_status = ao_cmd_syntax_error;
194                 printf("No such flight: %d\n", ao_cmd_lex_i);
195                 return;
196         }
197         for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
198                 if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
199                         break;
200                 if (!ao_log_valid(&log))
201                         break;
202                 ao_storage_erase(pos);
203         }
204         if (pos == 0)
205                 printf("No such flight: %d\n", ao_cmd_lex_i);
206         else
207                 printf ("Erased\n");
208 }
209
210 const struct ao_cmds ao_log_cmds[] = {
211         { ao_log_set,   "L <0 off, 1 on>\0Set logging mode" },
212         { ao_log_list,  "l\0List stored flight logs" },
213         { ao_log_delete, "d 1\0Delete all stored flights" },
214         { 0,    NULL },
215 };
216
217 void
218 ao_log_init(void)
219 {
220         ao_log_running = 0;
221
222         ao_cmd_register(&ao_log_cmds[0]);
223
224         ao_add_task(&ao_log_task, ao_log_telescience, "log");
225 }