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
64 ao_config_write(0, &ao_config, sizeof (ao_config));
66 ao_log_write_erase(0);
74 ao_mutex_get(&ao_config_mutex);
76 ao_mutex_put(&ao_config_mutex);
82 ao_config_set_radio(void)
84 ao_config.radio_setting = ao_freq_to_set(ao_config.frequency, ao_config.radio_cal);
86 #endif /* HAS_RADIO */
96 /* Yes, I know ao_storage_read calls ao_storage_setup,
97 * but ao_storage_setup *also* sets ao_storage_config, which we
98 * need before calling ao_storage_read here
101 ao_config_read(0, &ao_config, sizeof (ao_config));
103 if (ao_config.major != AO_CONFIG_MAJOR) {
104 ao_config.major = AO_CONFIG_MAJOR;
107 /* Version 0 stuff */
108 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
109 ao_xmemset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
110 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
111 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
112 ao_config._legacy_radio_channel = 0;
114 minor = ao_config.minor;
115 if (minor != AO_CONFIG_MINOR) {
116 /* Fixups for minor version 1 */
118 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
119 /* Fixups for minor version 2 */
121 ao_config.accel_plus_g = 0;
122 ao_config.accel_minus_g = 0;
124 /* Fixups for minor version 3 */
127 ao_config.radio_cal = ao_radio_cal;
129 /* Fixups for minor version 4 */
132 ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
134 /* Fixupes for minor version 5 */
136 ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
138 ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
140 ao_config.radio_enable = AO_RADIO_ENABLE_CORE;
142 ao_xmemset(&ao_config.aes_key, '\0', AO_AES_LEN);
144 ao_config.frequency = 434550 + ao_config._legacy_radio_channel * 100;
146 ao_config.apogee_lockout = 0;
149 memset(&ao_config.pyro, '\0', sizeof (ao_config.pyro));
152 ao_config.aprs_interval = AO_CONFIG_DEFAULT_APRS_INTERVAL;
155 ao_config.radio_power = AO_CONFIG_DEFAULT_RADIO_POWER;
159 ao_config.radio_amp = AO_CONFIG_DEFAULT_RADIO_AMP;
163 ao_config.accel_zero_along = 0;
164 ao_config.accel_zero_across = 0;
165 ao_config.accel_zero_through = 0;
167 /* Reset the main accel offsets to force
170 ao_config.accel_plus_g = 0;
171 ao_config.accel_minus_g = 0;
174 ao_config.minor = AO_CONFIG_MINOR;
180 ao_config.frequency = 434550;
181 ao_config.radio_cal = ao_radio_cal;
182 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
183 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
186 ao_config_set_radio();
188 ao_config_loaded = 1;
192 _ao_config_edit_start(void)
194 ao_mutex_get(&ao_config_mutex);
199 _ao_config_edit_finish(void)
202 ao_mutex_put(&ao_config_mutex);
208 _ao_config_edit_start();
209 ao_mutex_put(&ao_config_mutex);
213 ao_config_callsign_show(void)
215 printf ("Callsign: \"%s\"\n", ao_config.callsign);
219 ao_config_callsign_set(void) __reentrant
222 static __xdata char callsign[AO_MAX_CALLSIGN + 1];
224 ao_xmemset(callsign, '\0', sizeof callsign);
227 while (ao_cmd_lex_c != '\n') {
228 if (c < AO_MAX_CALLSIGN)
229 callsign[c++] = ao_cmd_lex_c;
231 ao_cmd_status = ao_cmd_lex_error;
234 if (ao_cmd_status != ao_cmd_success)
236 _ao_config_edit_start();
237 ao_xmemcpy(&ao_config.callsign, &callsign,
238 AO_MAX_CALLSIGN + 1);
239 _ao_config_edit_finish();
245 ao_config_frequency_show(void) __reentrant
247 printf("Frequency: %ld\n",
248 ao_config.frequency);
252 ao_config_frequency_set(void) __reentrant
255 if (ao_cmd_status != ao_cmd_success)
257 _ao_config_edit_start();
258 ao_config.frequency = ao_cmd_lex_u32;
259 ao_config_set_radio();
260 _ao_config_edit_finish();
262 ao_radio_recv_abort();
270 ao_config_main_deploy_show(void) __reentrant
272 printf("Main deploy: %d meters\n",
273 ao_config.main_deploy);
277 ao_config_main_deploy_set(void) __reentrant
280 if (ao_cmd_status != ao_cmd_success)
282 _ao_config_edit_start();
283 ao_config.main_deploy = ao_cmd_lex_i;
284 _ao_config_edit_finish();
289 ao_config_accel_calibrate_show(void) __reentrant
291 printf("Accel cal +1g: %d -1g: %d\n",
292 ao_config.accel_plus_g, ao_config.accel_minus_g);
294 printf ("IMU cal along %d across %d through %d\n",
295 ao_config.accel_zero_along,
296 ao_config.accel_zero_across,
297 ao_config.accel_zero_through);
301 #define ACCEL_CALIBRATE_SAMPLES 1024
302 #define ACCEL_CALIBRATE_SHIFT 10
305 static int16_t accel_cal_along;
306 static int16_t accel_cal_across;
307 static int16_t accel_cal_through;
311 ao_config_accel_calibrate_auto(char *orientation) __reentrant
315 uint8_t cal_data_ring;
317 int32_t accel_along_total = 0;
318 int32_t accel_across_total = 0;
319 int32_t accel_through_total = 0;
322 printf("Orient antenna %s and press a key...", orientation);
325 puts("\r\n"); flush();
326 puts("Calibrating..."); flush();
327 i = ACCEL_CALIBRATE_SAMPLES;
329 cal_data_ring = ao_sample_data;
331 ao_sleep(DATA_TO_XDATA(&ao_sample_data));
332 while (i && cal_data_ring != ao_sample_data) {
333 accel_total += (int32_t) ao_data_accel(&ao_data_ring[cal_data_ring]);
335 accel_along_total += (int32_t) ao_data_along(&ao_data_ring[cal_data_ring]);
336 accel_across_total += (int32_t) ao_data_across(&ao_data_ring[cal_data_ring]);
337 accel_through_total += (int32_t) ao_data_through(&ao_data_ring[cal_data_ring]);
339 cal_data_ring = ao_data_ring_next(cal_data_ring);
344 accel_cal_along = accel_along_total >> ACCEL_CALIBRATE_SHIFT;
345 accel_cal_across = accel_across_total >> ACCEL_CALIBRATE_SHIFT;
346 accel_cal_through = accel_through_total >> ACCEL_CALIBRATE_SHIFT;
348 return accel_total >> ACCEL_CALIBRATE_SHIFT;
352 ao_config_accel_calibrate_set(void) __reentrant
356 int16_t accel_along_up, accel_along_down;
357 int16_t accel_across_up, accel_across_down;
358 int16_t accel_through_up, accel_through_down;
362 if (ao_cmd_status != ao_cmd_success)
364 if (ao_cmd_lex_i == 0) {
365 up = ao_config_accel_calibrate_auto("up");
367 accel_along_up = accel_cal_along;
368 accel_across_up = accel_cal_across;
369 accel_through_up = accel_cal_through;
371 down = ao_config_accel_calibrate_auto("down");
373 accel_along_down = accel_cal_along;
374 accel_across_down = accel_cal_across;
375 accel_through_down = accel_cal_through;
380 if (ao_cmd_status != ao_cmd_success)
385 printf("Invalid accel: up (%d) down (%d)\n",
389 _ao_config_edit_start();
390 ao_config.accel_plus_g = up;
391 ao_config.accel_minus_g = down;
393 ao_config.accel_zero_along = (accel_along_up + accel_along_down) / 2;
394 ao_config.accel_zero_across = (accel_across_up + accel_across_down) / 2;
395 ao_config.accel_zero_through = (accel_through_up + accel_through_down) / 2;
397 _ao_config_edit_finish();
399 #endif /* HAS_ACCEL */
402 ao_config_apogee_delay_show(void) __reentrant
404 printf("Apogee delay: %d seconds\n",
405 ao_config.apogee_delay);
409 ao_config_apogee_delay_set(void) __reentrant
412 if (ao_cmd_status != ao_cmd_success)
414 _ao_config_edit_start();
415 ao_config.apogee_delay = ao_cmd_lex_i;
416 _ao_config_edit_finish();
420 ao_config_apogee_lockout_show(void) __reentrant
422 printf ("Apogee lockout: %d seconds\n",
423 ao_config.apogee_lockout);
427 ao_config_apogee_lockout_set(void) __reentrant
430 if (ao_cmd_status != ao_cmd_success)
432 _ao_config_edit_start();
433 ao_config.apogee_lockout = ao_cmd_lex_i;
434 _ao_config_edit_finish();
437 #endif /* HAS_FLIGHT */
441 ao_config_radio_cal_show(void) __reentrant
443 printf("Radio cal: %ld\n", ao_config.radio_cal);
447 ao_config_radio_cal_set(void) __reentrant
450 if (ao_cmd_status != ao_cmd_success)
452 _ao_config_edit_start();
453 ao_config.radio_cal = ao_cmd_lex_u32;
454 ao_config_set_radio();
455 _ao_config_edit_finish();
461 ao_config_log_show(void) __reentrant
463 printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
467 ao_config_log_set(void) __reentrant
469 uint16_t block = (uint16_t) (ao_storage_block >> 10);
470 uint16_t log_max = (uint16_t) (ao_storage_log_max >> 10);
473 if (ao_cmd_status != ao_cmd_success)
475 if (ao_log_present())
476 printf("Storage must be empty before changing log size\n");
477 else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
478 printf("Flight log size must be multiple of %d kB\n", block);
479 else if (ao_cmd_lex_i > log_max)
480 printf("Flight log max %d kB\n", log_max);
482 _ao_config_edit_start();
483 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
484 _ao_config_edit_finish();
491 ao_config_ignite_mode_show(void) __reentrant
493 printf("Ignite mode: %d\n", ao_config.ignite_mode);
497 ao_config_ignite_mode_set(void) __reentrant
500 if (ao_cmd_status != ao_cmd_success)
502 _ao_config_edit_start();
503 ao_config.ignite_mode = ao_cmd_lex_i;
504 _ao_config_edit_finish();
510 ao_config_pad_orientation_show(void) __reentrant
512 printf("Pad orientation: %d\n", ao_config.pad_orientation);
516 ao_config_pad_orientation_set(void) __reentrant
519 if (ao_cmd_status != ao_cmd_success)
521 _ao_config_edit_start();
523 if (ao_config.pad_orientation != ao_cmd_lex_i) {
525 t = ao_config.accel_plus_g;
526 ao_config.accel_plus_g = 0x7fff - ao_config.accel_minus_g;
527 ao_config.accel_minus_g = 0x7fff - t;
529 ao_config.pad_orientation = ao_cmd_lex_i;
530 _ao_config_edit_finish();
536 ao_config_radio_enable_show(void) __reentrant
538 printf("Radio enable: %d\n", ao_config.radio_enable);
542 ao_config_radio_enable_set(void) __reentrant
545 if (ao_cmd_status != ao_cmd_success)
547 _ao_config_edit_start();
548 ao_config.radio_enable = ao_cmd_lex_i;
549 _ao_config_edit_finish();
551 #endif /* HAS_RADIO */
555 __xdata uint8_t ao_config_aes_seq = 1;
558 ao_config_key_show(void) __reentrant
562 for (i = 0; i < AO_AES_LEN; i++)
563 printf ("%02x", ao_config.aes_key[i]);
568 ao_config_key_set(void) __reentrant
572 _ao_config_edit_start();
573 for (i = 0; i < AO_AES_LEN; i++) {
575 if (ao_cmd_status != ao_cmd_success)
577 ao_config.aes_key[i] = ao_cmd_lex_i;
580 _ao_config_edit_finish();
587 ao_config_aprs_show(void)
589 printf ("APRS interval: %d\n", ao_config.aprs_interval);
593 ao_config_aprs_set(void)
596 if (ao_cmd_status != ao_cmd_success)
598 _ao_config_edit_start();
599 ao_config.aprs_interval = ao_cmd_lex_i;
600 _ao_config_edit_finish();
603 #endif /* HAS_APRS */
608 ao_config_radio_amp_show(void)
610 printf ("Radio amp setting: %d\n", ao_config.radio_amp);
614 ao_config_radio_amp_set(void)
617 if (ao_cmd_status != ao_cmd_success)
619 _ao_config_edit_start();
620 ao_config.radio_amp = ao_cmd_lex_i;
621 _ao_config_edit_finish();
629 ao_config_radio_power_show(void)
631 printf ("Radio power setting: %d\n", ao_config.radio_power);
635 ao_config_radio_power_set(void)
638 if (ao_cmd_status != ao_cmd_success)
640 _ao_config_edit_start();
641 ao_config.radio_power = ao_cmd_lex_i;
642 _ao_config_edit_finish();
647 struct ao_config_var {
649 void (*set)(void) __reentrant;
650 void (*show)(void) __reentrant;
654 ao_config_help(void) __reentrant;
657 ao_config_show(void) __reentrant;
660 ao_config_save(void) __reentrant;
662 __code struct ao_config_var ao_config_vars[] = {
664 { "m <meters>\0Main deploy (m)",
665 ao_config_main_deploy_set, ao_config_main_deploy_show, },
666 { "d <delay>\0Apogee delay (s)",
667 ao_config_apogee_delay_set, ao_config_apogee_delay_show },
668 { "L <seconds>\0Apogee detect lockout (s)",
669 ao_config_apogee_lockout_set, ao_config_apogee_lockout_show, },
670 #endif /* HAS_FLIGHT */
672 { "F <freq>\0Frequency (kHz)",
673 ao_config_frequency_set, ao_config_frequency_show },
674 { "c <call>\0Callsign (8 char max)",
675 ao_config_callsign_set, ao_config_callsign_show },
676 { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
677 ao_config_radio_enable_set, ao_config_radio_enable_show },
678 { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
679 ao_config_radio_cal_set, ao_config_radio_cal_show },
681 { "p <setting>\0Radio power setting (0-255)",
682 ao_config_radio_power_set, ao_config_radio_power_show },
685 { "d <setting>\0Radio amplifier setting (0-3)",
686 ao_config_radio_amp_set, ao_config_radio_amp_show },
688 #endif /* HAS_RADIO */
690 { "a <+g> <-g>\0Accel calib (0 for auto)",
691 ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
692 { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
693 ao_config_pad_orientation_set,ao_config_pad_orientation_show },
694 #endif /* HAS_ACCEL */
696 { "l <size>\0Flight log size (kB)",
697 ao_config_log_set, ao_config_log_show },
700 { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
701 ao_config_ignite_mode_set, ao_config_ignite_mode_show },
704 { "k <32 hex digits>\0Set AES encryption key",
705 ao_config_key_set, ao_config_key_show },
708 { "P <n,?>\0Configure pyro channels",
709 ao_pyro_set, ao_pyro_show },
712 { "A <secs>\0APRS packet interval (0 disable)",
713 ao_config_aprs_set, ao_config_aprs_show },
718 { "w\0Write to eeprom",
735 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
736 if (ao_config_vars[cmd].str[0] == c) {
737 (*ao_config_vars[cmd].set)();
740 ao_cmd_status = ao_cmd_syntax_error;
744 ao_config_help(void) __reentrant
747 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
749 ao_config_vars[cmd].str,
750 ao_config_vars[cmd].str+1+
751 strlen(ao_config_vars[cmd].str));
755 ao_config_show(void) __reentrant
759 printf("Config version: %d.%d\n",
760 ao_config.major, ao_config.minor);
761 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
762 if (ao_config_vars[cmd].show)
763 (*ao_config_vars[cmd].show)();
771 ao_config_save(void) __reentrant
774 ao_mutex_get(&ao_config_mutex);
775 if (ao_config_dirty) {
780 ao_mutex_put(&ao_config_mutex);
784 puts("Nothing to save");
788 __code struct ao_cmds ao_config_cmds[] = {
789 { ao_config_set, "c <var> <value>\0Set config (? for help, s to show)" },
796 ao_cmd_register(&ao_config_cmds[0]);