2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 #include <ao_config.h>
22 #include <ao_sample.h>
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;
31 #ifndef AO_CONFIG_DEFAULT_APRS_INTERVAL
32 #define AO_CONFIG_DEFAULT_APRS_INTERVAL 0
34 #define AO_CONFIG_DEFAULT_MAIN_DEPLOY 250
35 #define AO_CONFIG_DEFAULT_RADIO_CHANNEL 0
36 #define AO_CONFIG_DEFAULT_CALLSIGN "N0CALL"
37 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G 16000
38 #define AO_CONFIG_DEFAULT_APOGEE_DELAY 0
39 #define AO_CONFIG_DEFAULT_IGNITE_MODE AO_IGNITE_MODE_DUAL
40 #define AO_CONFIG_DEFAULT_PAD_ORIENTATION AO_PAD_ORIENTATION_ANTENNA_UP
42 #ifndef USE_INTERNAL_FLASH
43 #error Please define USE_INTERNAL_FLASH
46 #ifndef AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX
47 #if USE_INTERNAL_FLASH
48 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ao_storage_config
50 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ((uint32_t) 192 * (uint32_t) 1024)
53 #ifndef AO_CONFIG_DEFAULT_RADIO_POWER
54 #define AO_CONFIG_DEFAULT_RADIO_POWER 0x60
56 #define AO_CONFIG_DEFAULT_RADIO_AMP 0
63 ao_config_write(&ao_config, sizeof (ao_config));
65 ao_log_write_erase(0);
73 ao_mutex_get(&ao_config_mutex);
75 ao_mutex_put(&ao_config_mutex);
81 ao_config_set_radio(void)
83 ao_config.radio_setting = ao_freq_to_set(ao_config.frequency, ao_config.radio_cal);
85 #endif /* HAS_RADIO */
95 /* Yes, I know ao_storage_read calls ao_storage_setup,
96 * but ao_storage_setup *also* sets ao_storage_config, which we
97 * need before calling ao_storage_read here
100 ao_config_read(&ao_config, sizeof (ao_config));
102 if (ao_config.major != AO_CONFIG_MAJOR) {
103 ao_config.major = AO_CONFIG_MAJOR;
106 /* Version 0 stuff */
107 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
108 ao_xmemset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
109 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
110 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
111 ao_config._legacy_radio_channel = 0;
113 minor = ao_config.minor;
114 if (minor != AO_CONFIG_MINOR) {
115 /* Fixups for minor version 1 */
117 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
118 /* Fixups for minor version 2 */
120 ao_config.accel_plus_g = 0;
121 ao_config.accel_minus_g = 0;
123 /* Fixups for minor version 3 */
126 ao_config.radio_cal = ao_radio_cal;
128 /* Fixups for minor version 4 */
131 ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
133 /* Fixupes for minor version 5 */
135 ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
137 ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
139 ao_config.radio_enable = AO_RADIO_ENABLE_CORE;
141 ao_xmemset(&ao_config.aes_key, '\0', AO_AES_LEN);
143 ao_config.frequency = 434550 + ao_config._legacy_radio_channel * 100;
145 ao_config.apogee_lockout = 0;
148 memset(&ao_config.pyro, '\0', sizeof (ao_config.pyro));
151 ao_config.aprs_interval = AO_CONFIG_DEFAULT_APRS_INTERVAL;
154 ao_config.radio_power = AO_CONFIG_DEFAULT_RADIO_POWER;
158 ao_config.radio_amp = AO_CONFIG_DEFAULT_RADIO_AMP;
162 ao_config.accel_zero_along = 0;
163 ao_config.accel_zero_across = 0;
164 ao_config.accel_zero_through = 0;
166 /* Reset the main accel offsets to force
169 ao_config.accel_plus_g = 0;
170 ao_config.accel_minus_g = 0;
173 ao_config.minor = AO_CONFIG_MINOR;
179 ao_config.frequency = 434550;
180 ao_config.radio_cal = ao_radio_cal;
181 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
182 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
185 ao_config_set_radio();
187 ao_config_loaded = 1;
191 _ao_config_edit_start(void)
193 ao_mutex_get(&ao_config_mutex);
198 _ao_config_edit_finish(void)
201 ao_mutex_put(&ao_config_mutex);
207 _ao_config_edit_start();
208 ao_mutex_put(&ao_config_mutex);
212 ao_config_callsign_show(void)
214 printf ("Callsign: \"%s\"\n", ao_config.callsign);
218 ao_config_callsign_set(void) __reentrant
221 static __xdata char callsign[AO_MAX_CALLSIGN + 1];
223 ao_xmemset(callsign, '\0', sizeof callsign);
226 while (ao_cmd_lex_c != '\n') {
227 if (c < AO_MAX_CALLSIGN)
228 callsign[c++] = ao_cmd_lex_c;
230 ao_cmd_status = ao_cmd_lex_error;
233 if (ao_cmd_status != ao_cmd_success)
235 _ao_config_edit_start();
236 ao_xmemcpy(&ao_config.callsign, &callsign,
237 AO_MAX_CALLSIGN + 1);
238 _ao_config_edit_finish();
244 ao_config_frequency_show(void) __reentrant
246 printf("Frequency: %ld\n",
247 ao_config.frequency);
251 ao_config_frequency_set(void) __reentrant
254 if (ao_cmd_status != ao_cmd_success)
256 _ao_config_edit_start();
257 ao_config.frequency = ao_cmd_lex_u32;
258 ao_config_set_radio();
259 _ao_config_edit_finish();
261 ao_radio_recv_abort();
269 ao_config_main_deploy_show(void) __reentrant
271 printf("Main deploy: %d meters\n",
272 ao_config.main_deploy);
276 ao_config_main_deploy_set(void) __reentrant
279 if (ao_cmd_status != ao_cmd_success)
281 _ao_config_edit_start();
282 ao_config.main_deploy = ao_cmd_lex_i;
283 _ao_config_edit_finish();
288 ao_config_accel_calibrate_show(void) __reentrant
290 printf("Accel cal +1g: %d -1g: %d\n",
291 ao_config.accel_plus_g, ao_config.accel_minus_g);
293 printf ("IMU cal along %d across %d through %d\n",
294 ao_config.accel_zero_along,
295 ao_config.accel_zero_across,
296 ao_config.accel_zero_through);
300 #define ACCEL_CALIBRATE_SAMPLES 1024
301 #define ACCEL_CALIBRATE_SHIFT 10
304 static int16_t accel_cal_along;
305 static int16_t accel_cal_across;
306 static int16_t accel_cal_through;
310 ao_config_accel_calibrate_auto(char *orientation) __reentrant
314 uint8_t cal_data_ring;
316 int32_t accel_along_total = 0;
317 int32_t accel_across_total = 0;
318 int32_t accel_through_total = 0;
321 printf("Orient antenna %s and press a key...", orientation);
324 puts("\r\n"); flush();
325 puts("Calibrating..."); flush();
326 i = ACCEL_CALIBRATE_SAMPLES;
328 cal_data_ring = ao_sample_data;
330 ao_sleep(DATA_TO_XDATA(&ao_sample_data));
331 while (i && cal_data_ring != ao_sample_data) {
332 accel_total += (int32_t) ao_data_accel(&ao_data_ring[cal_data_ring]);
334 accel_along_total += (int32_t) ao_data_along(&ao_data_ring[cal_data_ring]);
335 accel_across_total += (int32_t) ao_data_across(&ao_data_ring[cal_data_ring]);
336 accel_through_total += (int32_t) ao_data_through(&ao_data_ring[cal_data_ring]);
338 cal_data_ring = ao_data_ring_next(cal_data_ring);
343 accel_cal_along = accel_along_total >> ACCEL_CALIBRATE_SHIFT;
344 accel_cal_across = accel_across_total >> ACCEL_CALIBRATE_SHIFT;
345 accel_cal_through = accel_through_total >> ACCEL_CALIBRATE_SHIFT;
347 return accel_total >> ACCEL_CALIBRATE_SHIFT;
351 ao_config_accel_calibrate_set(void) __reentrant
355 int16_t accel_along_up, accel_along_down;
356 int16_t accel_across_up, accel_across_down;
357 int16_t accel_through_up, accel_through_down;
361 if (ao_cmd_status != ao_cmd_success)
363 if (ao_cmd_lex_i == 0) {
364 up = ao_config_accel_calibrate_auto("up");
366 accel_along_up = accel_cal_along;
367 accel_across_up = accel_cal_across;
368 accel_through_up = accel_cal_through;
370 down = ao_config_accel_calibrate_auto("down");
372 accel_along_down = accel_cal_along;
373 accel_across_down = accel_cal_across;
374 accel_through_down = accel_cal_through;
379 if (ao_cmd_status != ao_cmd_success)
384 printf("Invalid accel: up (%d) down (%d)\n",
388 _ao_config_edit_start();
389 ao_config.accel_plus_g = up;
390 ao_config.accel_minus_g = down;
392 ao_config.accel_zero_along = (accel_along_up + accel_along_down) / 2;
393 ao_config.accel_zero_across = (accel_across_up + accel_across_down) / 2;
394 ao_config.accel_zero_through = (accel_through_up + accel_through_down) / 2;
396 _ao_config_edit_finish();
398 #endif /* HAS_ACCEL */
401 ao_config_apogee_delay_show(void) __reentrant
403 printf("Apogee delay: %d seconds\n",
404 ao_config.apogee_delay);
408 ao_config_apogee_delay_set(void) __reentrant
411 if (ao_cmd_status != ao_cmd_success)
413 _ao_config_edit_start();
414 ao_config.apogee_delay = ao_cmd_lex_i;
415 _ao_config_edit_finish();
419 ao_config_apogee_lockout_show(void) __reentrant
421 printf ("Apogee lockout: %d seconds\n",
422 ao_config.apogee_lockout);
426 ao_config_apogee_lockout_set(void) __reentrant
429 if (ao_cmd_status != ao_cmd_success)
431 _ao_config_edit_start();
432 ao_config.apogee_lockout = ao_cmd_lex_i;
433 _ao_config_edit_finish();
436 #endif /* HAS_FLIGHT */
440 ao_config_radio_cal_show(void) __reentrant
442 printf("Radio cal: %ld\n", ao_config.radio_cal);
446 ao_config_radio_cal_set(void) __reentrant
449 if (ao_cmd_status != ao_cmd_success)
451 _ao_config_edit_start();
452 ao_config.radio_cal = ao_cmd_lex_u32;
453 ao_config_set_radio();
454 _ao_config_edit_finish();
460 ao_config_log_show(void) __reentrant
462 printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
466 ao_config_log_set(void) __reentrant
468 uint16_t block = (uint16_t) (ao_storage_block >> 10);
469 uint16_t config = (uint16_t) (ao_storage_config >> 10);
472 if (ao_cmd_status != ao_cmd_success)
474 if (ao_log_present())
475 printf("Storage must be empty before changing log size\n");
476 else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
477 printf("Flight log size must be multiple of %d kB\n", block);
478 else if (ao_cmd_lex_i > config)
479 printf("Flight log max %d kB\n", config);
481 _ao_config_edit_start();
482 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
483 _ao_config_edit_finish();
490 ao_config_ignite_mode_show(void) __reentrant
492 printf("Ignite mode: %d\n", ao_config.ignite_mode);
496 ao_config_ignite_mode_set(void) __reentrant
499 if (ao_cmd_status != ao_cmd_success)
501 _ao_config_edit_start();
502 ao_config.ignite_mode = ao_cmd_lex_i;
503 _ao_config_edit_finish();
509 ao_config_pad_orientation_show(void) __reentrant
511 printf("Pad orientation: %d\n", ao_config.pad_orientation);
515 ao_config_pad_orientation_set(void) __reentrant
518 if (ao_cmd_status != ao_cmd_success)
520 _ao_config_edit_start();
522 if (ao_config.pad_orientation != ao_cmd_lex_i) {
524 t = ao_config.accel_plus_g;
525 ao_config.accel_plus_g = 0x7fff - ao_config.accel_minus_g;
526 ao_config.accel_minus_g = 0x7fff - t;
528 ao_config.pad_orientation = ao_cmd_lex_i;
529 _ao_config_edit_finish();
535 ao_config_radio_enable_show(void) __reentrant
537 printf("Radio enable: %d\n", ao_config.radio_enable);
541 ao_config_radio_enable_set(void) __reentrant
544 if (ao_cmd_status != ao_cmd_success)
546 _ao_config_edit_start();
547 ao_config.radio_enable = ao_cmd_lex_i;
548 _ao_config_edit_finish();
550 #endif /* HAS_RADIO */
554 __xdata uint8_t ao_config_aes_seq = 1;
557 ao_config_key_show(void) __reentrant
561 for (i = 0; i < AO_AES_LEN; i++)
562 printf ("%02x", ao_config.aes_key[i]);
567 ao_config_key_set(void) __reentrant
571 _ao_config_edit_start();
572 for (i = 0; i < AO_AES_LEN; i++) {
574 if (ao_cmd_status != ao_cmd_success)
576 ao_config.aes_key[i] = ao_cmd_lex_i;
579 _ao_config_edit_finish();
586 ao_config_aprs_show(void)
588 printf ("APRS interval: %d\n", ao_config.aprs_interval);
592 ao_config_aprs_set(void)
595 if (ao_cmd_status != ao_cmd_success)
597 _ao_config_edit_start();
598 ao_config.aprs_interval = ao_cmd_lex_i;
599 _ao_config_edit_finish();
602 #endif /* HAS_APRS */
607 ao_config_radio_amp_show(void)
609 printf ("Radio amp setting: %d\n", ao_config.radio_amp);
613 ao_config_radio_amp_set(void)
616 if (ao_cmd_status != ao_cmd_success)
618 _ao_config_edit_start();
619 ao_config.radio_amp = ao_cmd_lex_i;
620 _ao_config_edit_finish();
628 ao_config_radio_power_show(void)
630 printf ("Radio power setting: %d\n", ao_config.radio_power);
634 ao_config_radio_power_set(void)
637 if (ao_cmd_status != ao_cmd_success)
639 _ao_config_edit_start();
640 ao_config.radio_power = ao_cmd_lex_i;
641 _ao_config_edit_finish();
646 struct ao_config_var {
648 void (*set)(void) __reentrant;
649 void (*show)(void) __reentrant;
653 ao_config_help(void) __reentrant;
656 ao_config_show(void) __reentrant;
659 ao_config_save(void) __reentrant;
661 __code struct ao_config_var ao_config_vars[] = {
663 { "m <meters>\0Main deploy (m)",
664 ao_config_main_deploy_set, ao_config_main_deploy_show, },
665 { "d <delay>\0Apogee delay (s)",
666 ao_config_apogee_delay_set, ao_config_apogee_delay_show },
667 { "L <seconds>\0Apogee detect lockout (s)",
668 ao_config_apogee_lockout_set, ao_config_apogee_lockout_show, },
669 #endif /* HAS_FLIGHT */
671 { "F <freq>\0Frequency (kHz)",
672 ao_config_frequency_set, ao_config_frequency_show },
673 { "c <call>\0Callsign (8 char max)",
674 ao_config_callsign_set, ao_config_callsign_show },
675 { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
676 ao_config_radio_enable_set, ao_config_radio_enable_show },
677 { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
678 ao_config_radio_cal_set, ao_config_radio_cal_show },
680 { "p <setting>\0Radio power setting (0-255)",
681 ao_config_radio_power_set, ao_config_radio_power_show },
684 { "d <setting>\0Radio amplifier setting (0-3)",
685 ao_config_radio_amp_set, ao_config_radio_amp_show },
687 #endif /* HAS_RADIO */
689 { "a <+g> <-g>\0Accel calib (0 for auto)",
690 ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
691 { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
692 ao_config_pad_orientation_set,ao_config_pad_orientation_show },
693 #endif /* HAS_ACCEL */
695 { "l <size>\0Flight log size (kB)",
696 ao_config_log_set, ao_config_log_show },
699 { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
700 ao_config_ignite_mode_set, ao_config_ignite_mode_show },
703 { "k <32 hex digits>\0Set AES encryption key",
704 ao_config_key_set, ao_config_key_show },
707 { "P <n,?>\0Configure pyro channels",
708 ao_pyro_set, ao_pyro_show },
711 { "A <secs>\0APRS packet interval (0 disable)",
712 ao_config_aprs_set, ao_config_aprs_show },
717 { "w\0Write to eeprom",
734 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
735 if (ao_config_vars[cmd].str[0] == c) {
736 (*ao_config_vars[cmd].set)();
739 ao_cmd_status = ao_cmd_syntax_error;
743 ao_config_help(void) __reentrant
746 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
748 ao_config_vars[cmd].str,
749 ao_config_vars[cmd].str+1+
750 strlen(ao_config_vars[cmd].str));
754 ao_config_show(void) __reentrant
758 printf("Config version: %d.%d\n",
759 ao_config.major, ao_config.minor);
760 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
761 if (ao_config_vars[cmd].show)
762 (*ao_config_vars[cmd].show)();
770 ao_config_save(void) __reentrant
773 ao_mutex_get(&ao_config_mutex);
774 if (ao_config_dirty) {
779 ao_mutex_put(&ao_config_mutex);
783 puts("Nothing to save");
787 __code struct ao_cmds ao_config_cmds[] = {
788 { ao_config_set, "c <var> <value>\0Set config (? for help, s to show)" },
795 ao_cmd_register(&ao_config_cmds[0]);