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