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