0c10e60894e57569d18a07d870610717da95c1cb
[fw/altos] / src / ao_config.c
1 /*
2  * Copyright © 2009 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 __xdata struct ao_config ao_config;
21 __pdata uint8_t ao_config_loaded;
22 __pdata uint8_t ao_config_dirty;
23 __xdata uint8_t ao_config_mutex;
24
25 #define AO_CONFIG_DEFAULT_MAIN_DEPLOY   250
26 #define AO_CONFIG_DEFAULT_RADIO_CHANNEL 0
27 #define AO_CONFIG_DEFAULT_CALLSIGN      "N0CALL"
28 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G  16000
29 #define AO_CONFIG_DEFAULT_APOGEE_DELAY  0
30 #define AO_CONFIG_DEFAULT_IGNITE_MODE   AO_IGNITE_MODE_DUAL
31 #define AO_CONFIG_DEFAULT_PAD_ORIENTATION       AO_PAD_ORIENTATION_ANTENNA_UP
32 #if HAS_EEPROM
33 #ifndef USE_INTERNAL_FLASH
34 #error Please define USE_INTERNAL_FLASH
35 #endif
36 #endif
37 #if USE_INTERNAL_FLASH
38 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ao_storage_config
39 #else
40 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ((uint32_t) 192 * (uint32_t) 1024)
41 #endif
42
43 #if HAS_EEPROM
44 static void
45 _ao_config_put(void)
46 {
47         ao_storage_setup();
48         ao_storage_erase(ao_storage_config);
49         ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
50         ao_log_write_erase(0);
51         ao_storage_flush();
52 }
53
54 void
55 ao_config_put(void)
56 {
57         ao_mutex_get(&ao_config_mutex);
58         _ao_config_put();
59         ao_mutex_put(&ao_config_mutex);
60 }
61 #endif
62
63 static void
64 _ao_config_get(void)
65 {
66         if (ao_config_loaded)
67                 return;
68 #if HAS_EEPROM
69         ao_storage_setup();
70         ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
71 #endif
72         if (ao_config.major != AO_CONFIG_MAJOR) {
73                 ao_config.major = AO_CONFIG_MAJOR;
74                 ao_config.minor = 0;
75                 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
76                 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
77                 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
78                 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
79                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
80         }
81         if (ao_config.minor < AO_CONFIG_MINOR) {
82                 /* Fixups for minor version 1 */
83                 if (ao_config.minor < 1)
84                         ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
85                 /* Fixups for minor version 2 */
86                 if (ao_config.minor < 2) {
87                         ao_config.accel_plus_g = 0;
88                         ao_config.accel_minus_g = 0;
89                 }
90                 /* Fixups for minor version 3 */
91                 if (ao_config.minor < 3)
92                         ao_config.radio_cal = ao_radio_cal;
93                 /* Fixups for minor version 4 */
94                 if (ao_config.minor < 4)
95                         ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
96                 /* Fixupes for minor version 5 */
97                 if (ao_config.minor < 5)
98                         ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
99                 if (ao_config.minor < 6)
100                         ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
101                 if (ao_config.minor < 7)
102                         ao_config.radio_setting = ao_config.radio_cal;
103                 if (ao_config.minor < 8)
104                         ao_config.radio_enable = TRUE;
105                 ao_config.minor = AO_CONFIG_MINOR;
106                 ao_config_dirty = 1;
107         }
108         ao_config_loaded = 1;
109 }
110
111 static void
112 _ao_config_edit_start(void)
113 {
114         ao_mutex_get(&ao_config_mutex);
115         _ao_config_get();
116 }
117
118 static void
119 _ao_config_edit_finish(void)
120 {
121         ao_config_dirty = 1;
122         ao_mutex_put(&ao_config_mutex);
123 }
124
125 void
126 ao_config_get(void)
127 {
128         _ao_config_edit_start();
129         ao_mutex_put(&ao_config_mutex);
130 }
131
132 void
133 ao_config_callsign_show(void)
134 {
135         printf ("Callsign: \"%s\"\n", ao_config.callsign);
136 }
137
138 void
139 ao_config_callsign_set(void) __reentrant
140 {
141         uint8_t c;
142         static __xdata char callsign[AO_MAX_CALLSIGN + 1];
143
144         memset(callsign, '\0', sizeof callsign);
145         ao_cmd_white();
146         c = 0;
147         while (ao_cmd_lex_c != '\n') {
148                 if (c < AO_MAX_CALLSIGN)
149                         callsign[c++] = ao_cmd_lex_c;
150                 else
151                         ao_cmd_status = ao_cmd_lex_error;
152                 ao_cmd_lex();
153         }
154         if (ao_cmd_status != ao_cmd_success)
155                 return;
156         _ao_config_edit_start();
157         memcpy(&ao_config.callsign, &callsign,
158                AO_MAX_CALLSIGN + 1);
159         _ao_config_edit_finish();
160 }
161
162 void
163 ao_config_radio_channel_show(void) __reentrant
164 {
165         printf("Radio channel: %d\n",
166                ao_config.radio_channel);
167 }
168
169 void
170 ao_config_radio_channel_set(void) __reentrant
171 {
172         ao_cmd_decimal();
173         if (ao_cmd_status != ao_cmd_success)
174                 return;
175         _ao_config_edit_start();
176         ao_config.radio_channel = ao_cmd_lex_i;
177         _ao_config_edit_finish();
178         ao_radio_recv_abort();
179 }
180
181 #if HAS_ADC
182
183 void
184 ao_config_main_deploy_show(void) __reentrant
185 {
186         printf("Main deploy: %d meters\n",
187                ao_config.main_deploy);
188 }
189
190 void
191 ao_config_main_deploy_set(void) __reentrant
192 {
193         ao_cmd_decimal();
194         if (ao_cmd_status != ao_cmd_success)
195                 return;
196         _ao_config_edit_start();
197         ao_config.main_deploy = ao_cmd_lex_i;
198         _ao_config_edit_finish();
199 }
200
201 #if HAS_ACCEL
202 void
203 ao_config_accel_calibrate_show(void) __reentrant
204 {
205         printf("Accel cal +1g: %d -1g: %d\n",
206                ao_config.accel_plus_g, ao_config.accel_minus_g);
207 }
208
209 #define ACCEL_CALIBRATE_SAMPLES 1024
210 #define ACCEL_CALIBRATE_SHIFT   10
211
212 static int16_t
213 ao_config_accel_calibrate_auto(char *orientation) __reentrant
214 {
215         uint16_t        i;
216         int32_t         accel_total;
217         uint8_t         cal_adc_ring;
218
219         printf("Orient antenna %s and press a key...", orientation);
220         flush();
221         (void) getchar();
222         puts("\r\n"); flush();
223         puts("Calibrating..."); flush();
224         i = ACCEL_CALIBRATE_SAMPLES;
225         accel_total = 0;
226         cal_adc_ring = ao_sample_adc;
227         while (i) {
228                 ao_sleep(DATA_TO_XDATA(&ao_sample_adc));
229                 while (i && cal_adc_ring != ao_sample_adc) {
230                         accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
231                         cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
232                         i--;
233                 }
234         }
235         return accel_total >> ACCEL_CALIBRATE_SHIFT;
236 }
237
238 void
239 ao_config_accel_calibrate_set(void) __reentrant
240 {
241         int16_t up, down;
242         ao_cmd_decimal();
243         if (ao_cmd_status != ao_cmd_success)
244                 return;
245         if (ao_cmd_lex_i == 0) {
246                 up = ao_config_accel_calibrate_auto("up");
247                 down = ao_config_accel_calibrate_auto("down");
248         } else {
249                 up = ao_cmd_lex_i;
250                 ao_cmd_decimal();
251                 if (ao_cmd_status != ao_cmd_success)
252                         return;
253                 down = ao_cmd_lex_i;
254         }
255         if (up >= down) {
256                 printf("Invalid accel: up (%d) down (%d)\n",
257                        up, down);
258                 return;
259         }
260         _ao_config_edit_start();
261         ao_config.accel_plus_g = up;
262         ao_config.accel_minus_g = down;
263         _ao_config_edit_finish();
264 }
265 #endif /* HAS_ACCEL */
266
267 void
268 ao_config_apogee_delay_show(void) __reentrant
269 {
270         printf("Apogee delay: %d seconds\n",
271                ao_config.apogee_delay);
272 }
273
274 void
275 ao_config_apogee_delay_set(void) __reentrant
276 {
277         ao_cmd_decimal();
278         if (ao_cmd_status != ao_cmd_success)
279                 return;
280         _ao_config_edit_start();
281         ao_config.apogee_delay = ao_cmd_lex_i;
282         _ao_config_edit_finish();
283 }
284
285 #endif /* HAS_ADC */
286
287 void
288 ao_config_radio_cal_show(void) __reentrant
289 {
290         printf("Radio cal: %ld\n", ao_config.radio_cal);
291 }
292
293 void
294 ao_config_radio_cal_set(void) __reentrant
295 {
296         ao_cmd_decimal();
297         if (ao_cmd_status != ao_cmd_success)
298                 return;
299         _ao_config_edit_start();
300         ao_config.radio_setting = ao_config.radio_cal = ao_cmd_lex_u32;
301         _ao_config_edit_finish();
302 }
303
304 #if HAS_EEPROM
305 void
306 ao_config_log_show(void) __reentrant
307 {
308         printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
309 }
310
311 void
312 ao_config_log_set(void) __reentrant
313 {
314         uint16_t        block = (uint16_t) (ao_storage_block >> 10);
315         uint16_t        config = (uint16_t) (ao_storage_config >> 10);
316
317         ao_cmd_decimal();
318         if (ao_cmd_status != ao_cmd_success)
319                 return;
320         if (ao_log_present())
321                 printf("Storage must be empty before changing log size\n");
322         else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
323                 printf("Flight log size must be multiple of %d kB\n", block);
324         else if (ao_cmd_lex_i > config)
325                 printf("Flight log max %d kB\n", config);
326         else {
327                 _ao_config_edit_start();
328                 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
329                 _ao_config_edit_finish();
330         }
331 }
332 #endif /* HAS_EEPROM */
333
334 #if HAS_IGNITE
335 void
336 ao_config_ignite_mode_show(void) __reentrant
337 {
338         printf("Ignite mode: %d\n", ao_config.ignite_mode);
339 }
340
341 void
342 ao_config_ignite_mode_set(void) __reentrant
343 {
344         ao_cmd_decimal();
345         if (ao_cmd_status != ao_cmd_success)
346                 return;
347         _ao_config_edit_start();
348         ao_config.ignite_mode = ao_cmd_lex_i;
349         _ao_config_edit_finish();
350 }
351 #endif
352
353 #if HAS_ACCEL
354 void
355 ao_config_pad_orientation_show(void) __reentrant
356 {
357         printf("Pad orientation: %d\n", ao_config.pad_orientation);
358 }
359
360 void
361 ao_config_pad_orientation_set(void) __reentrant
362 {
363         ao_cmd_decimal();
364         if (ao_cmd_status != ao_cmd_success)
365                 return;
366         _ao_config_edit_start();
367         ao_cmd_lex_i &= 1;
368         if (ao_config.pad_orientation != ao_cmd_lex_i) {
369                 uint16_t t;
370                 t = ao_config.accel_plus_g;
371                 ao_config.accel_plus_g = 0x7fff - ao_config.accel_minus_g;
372                 ao_config.accel_minus_g = 0x7fff - t;
373         }
374         ao_config.pad_orientation = ao_cmd_lex_i;
375         _ao_config_edit_finish();
376 }
377 #endif
378
379 void
380 ao_config_radio_setting_show(void) __reentrant
381 {
382         printf("Radio setting: %ld\n", ao_config.radio_setting);
383 }
384
385 void
386 ao_config_radio_setting_set(void) __reentrant
387 {
388         ao_cmd_decimal();
389         if (ao_cmd_status != ao_cmd_success)
390                 return;
391         _ao_config_edit_start();
392         ao_config.radio_setting = ao_cmd_lex_u32;
393         ao_config.radio_channel = 0;
394         _ao_config_edit_finish();
395         ao_radio_recv_abort();
396 }
397
398 void
399 ao_config_radio_enable_show(void) __reentrant
400 {
401         printf("Radio enable: %d\n", ao_config.radio_enable);
402 }
403
404 void
405 ao_config_radio_enable_set(void) __reentrant
406 {
407         ao_cmd_decimal();
408         if (ao_cmd_status != ao_cmd_success)
409                 return;
410         _ao_config_edit_start();
411         ao_config.radio_enable = ao_cmd_lex_i;
412         _ao_config_edit_finish();
413 }
414         
415 struct ao_config_var {
416         __code char     *str;
417         void            (*set)(void) __reentrant;
418         void            (*show)(void) __reentrant;
419 };
420
421 static void
422 ao_config_help(void) __reentrant;
423
424 static void
425 ao_config_show(void) __reentrant;
426
427 static void
428 ao_config_write(void) __reentrant;
429
430 __code struct ao_config_var ao_config_vars[] = {
431 #if HAS_ADC
432         { "m <meters>\0Main deploy (in meters)",
433           ao_config_main_deploy_set,    ao_config_main_deploy_show, },
434         { "d <delay>\0Apogee delay (in seconds)",
435           ao_config_apogee_delay_set,   ao_config_apogee_delay_show },
436 #endif /* HAS_ADC */
437         { "r <channel>\0Radio channel (freq = 434.550 + chan * .1)",
438           ao_config_radio_channel_set,  ao_config_radio_channel_show },
439         { "c <call>\0Callsign (8 char max)",
440           ao_config_callsign_set,       ao_config_callsign_show },
441         { "R <setting>\0Radio freq control (freq = 434.550 * setting/cal)",
442           ao_config_radio_setting_set,  ao_config_radio_setting_show },
443         { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
444           ao_config_radio_enable_set, ao_config_radio_enable_show },
445 #if HAS_ACCEL
446         { "a <+g> <-g>\0Accel calib (0 for auto)",
447           ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
448 #endif /* HAS_ACCEL */
449         { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
450           ao_config_radio_cal_set,      ao_config_radio_cal_show },
451 #if HAS_EEPROM
452         { "l <size>\0Flight log size in kB",
453           ao_config_log_set,            ao_config_log_show },
454 #endif
455 #if HAS_IGNITE
456         { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
457           ao_config_ignite_mode_set,    ao_config_ignite_mode_show },
458 #endif
459 #if HAS_ACCEL
460         { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
461           ao_config_pad_orientation_set,ao_config_pad_orientation_show },
462 #endif
463         { "s\0Show",
464           ao_config_show,               0 },
465 #if HAS_EEPROM
466         { "w\0Write to eeprom",
467           ao_config_write,              0 },
468 #endif
469         { "?\0Help",
470           ao_config_help,               0 },
471         { 0, 0, 0 }
472 };
473
474 void
475 ao_config_set(void)
476 {
477         char    c;
478         uint8_t cmd;
479         void (*__xdata func)(void) __reentrant;
480
481         ao_cmd_white();
482         c = ao_cmd_lex_c;
483         ao_cmd_lex();
484         func = 0;
485         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
486                 if (ao_config_vars[cmd].str[0] == c) {
487                         (*ao_config_vars[cmd].set)();
488                         return;
489                 }
490         ao_cmd_status = ao_cmd_syntax_error;
491 }
492
493 static void
494 ao_config_help(void) __reentrant
495 {
496         uint8_t cmd;
497         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
498                 printf("%-20s %s\n",
499                        ao_config_vars[cmd].str,
500                        ao_config_vars[cmd].str+1+strlen(ao_config_vars[cmd].str));
501 }
502
503 static void
504 ao_config_show(void) __reentrant
505 {
506         uint8_t cmd;
507         printf("Config version: %d.%d\n",
508                ao_config.major, ao_config.minor);
509         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
510                 if (ao_config_vars[cmd].show)
511                         (*ao_config_vars[cmd].show)();
512 }
513
514 #if HAS_EEPROM
515 static void
516 ao_config_write(void) __reentrant
517 {
518         uint8_t saved = 0;
519         ao_mutex_get(&ao_config_mutex);
520         if (ao_config_dirty) {
521                 _ao_config_put();
522                 ao_config_dirty = 0;
523                 saved = 1;
524         }
525         ao_mutex_put(&ao_config_mutex);
526         if (saved)
527                 puts("Saved");
528         else
529                 puts("Nothing to save");
530 }
531 #endif
532
533 __code struct ao_cmds ao_config_cmds[] = {
534         { ao_config_set,        "c <var> <value>\0Set config variable (? for help, s to show)" },
535         { 0, NULL },
536 };
537
538 void
539 ao_config_init(void)
540 {
541         ao_cmd_register(&ao_config_cmds[0]);
542 }