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