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_storage.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 #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
39 #ifndef USE_INTERNAL_FLASH
40 #error Please define USE_INTERNAL_FLASH
43 #ifndef AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX
44 #if USE_INTERNAL_FLASH
45 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ao_storage_config
47 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ((uint32_t) 192 * (uint32_t) 1024)
50 #define AO_CONFIG_DEFAULT_RADIO_POWER 0x60
51 #define AO_CONFIG_DEFAULT_RADIO_AMP 0
58 ao_storage_erase(ao_storage_config);
59 ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
61 ao_log_write_erase(0);
69 ao_mutex_get(&ao_config_mutex);
71 ao_mutex_put(&ao_config_mutex);
77 ao_config_set_radio(void)
79 ao_config.radio_setting = ao_freq_to_set(ao_config.frequency, ao_config.radio_cal);
81 #endif /* HAS_RADIO */
91 /* Yes, I know ao_storage_read calls ao_storage_setup,
92 * but ao_storage_setup *also* sets ao_storage_config, which we
93 * need before calling ao_storage_read here
96 ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
98 if (ao_config.major != AO_CONFIG_MAJOR) {
99 ao_config.major = AO_CONFIG_MAJOR;
102 /* Version 0 stuff */
103 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
104 ao_xmemset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
105 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
106 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
107 ao_config._legacy_radio_channel = 0;
109 minor = ao_config.minor;
110 if (minor != AO_CONFIG_MINOR) {
111 /* Fixups for minor version 1 */
113 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
114 /* Fixups for minor version 2 */
116 ao_config.accel_plus_g = 0;
117 ao_config.accel_minus_g = 0;
119 /* Fixups for minor version 3 */
122 ao_config.radio_cal = ao_radio_cal;
124 /* Fixups for minor version 4 */
126 ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
127 /* Fixupes for minor version 5 */
129 ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
131 ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
133 ao_config.radio_enable = AO_RADIO_ENABLE_CORE;
135 ao_xmemset(&ao_config.aes_key, '\0', AO_AES_LEN);
137 ao_config.frequency = 434550 + ao_config._legacy_radio_channel * 100;
139 ao_config.apogee_lockout = 0;
142 memset(&ao_config.pyro, '\0', sizeof (ao_config.pyro));
145 ao_config.aprs_interval = 0;
146 ao_config.aprs_width = AO_APRS_WIDTH_MEDIUM;
150 ao_config.radio_power = AO_CONFIG_DEFAULT_RADIO_POWER;
154 ao_config.radio_amp = AO_CONFIG_DEFAULT_RADIO_AMP;
156 ao_config.minor = AO_CONFIG_MINOR;
162 ao_config.frequency = 434550;
163 ao_config.radio_cal = ao_radio_cal;
164 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
165 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
168 ao_config_set_radio();
170 ao_config_loaded = 1;
174 _ao_config_edit_start(void)
176 ao_mutex_get(&ao_config_mutex);
181 _ao_config_edit_finish(void)
184 ao_mutex_put(&ao_config_mutex);
190 _ao_config_edit_start();
191 ao_mutex_put(&ao_config_mutex);
195 ao_config_callsign_show(void)
197 printf ("Callsign: \"%s\"\n", ao_config.callsign);
201 ao_config_callsign_set(void) __reentrant
204 static __xdata char callsign[AO_MAX_CALLSIGN + 1];
206 ao_xmemset(callsign, '\0', sizeof callsign);
209 while (ao_cmd_lex_c != '\n') {
210 if (c < AO_MAX_CALLSIGN)
211 callsign[c++] = ao_cmd_lex_c;
213 ao_cmd_status = ao_cmd_lex_error;
216 if (ao_cmd_status != ao_cmd_success)
218 _ao_config_edit_start();
219 ao_xmemcpy(&ao_config.callsign, &callsign,
220 AO_MAX_CALLSIGN + 1);
221 _ao_config_edit_finish();
227 ao_config_frequency_show(void) __reentrant
229 printf("Frequency: %ld\n",
230 ao_config.frequency);
234 ao_config_frequency_set(void) __reentrant
237 if (ao_cmd_status != ao_cmd_success)
239 _ao_config_edit_start();
240 ao_config.frequency = ao_cmd_lex_u32;
241 ao_config_set_radio();
242 _ao_config_edit_finish();
244 ao_radio_recv_abort();
252 ao_config_main_deploy_show(void) __reentrant
254 printf("Main deploy: %d meters\n",
255 ao_config.main_deploy);
259 ao_config_main_deploy_set(void) __reentrant
262 if (ao_cmd_status != ao_cmd_success)
264 _ao_config_edit_start();
265 ao_config.main_deploy = ao_cmd_lex_i;
266 _ao_config_edit_finish();
271 ao_config_accel_calibrate_show(void) __reentrant
273 printf("Accel cal +1g: %d -1g: %d\n",
274 ao_config.accel_plus_g, ao_config.accel_minus_g);
277 #define ACCEL_CALIBRATE_SAMPLES 1024
278 #define ACCEL_CALIBRATE_SHIFT 10
281 ao_config_accel_calibrate_auto(char *orientation) __reentrant
285 uint8_t cal_data_ring;
287 printf("Orient antenna %s and press a key...", orientation);
290 puts("\r\n"); flush();
291 puts("Calibrating..."); flush();
292 i = ACCEL_CALIBRATE_SAMPLES;
294 cal_data_ring = ao_sample_data;
296 ao_sleep(DATA_TO_XDATA(&ao_sample_data));
297 while (i && cal_data_ring != ao_sample_data) {
298 accel_total += (int32_t) ao_data_accel(&ao_data_ring[cal_data_ring]);
299 cal_data_ring = ao_data_ring_next(cal_data_ring);
303 return accel_total >> ACCEL_CALIBRATE_SHIFT;
307 ao_config_accel_calibrate_set(void) __reentrant
311 if (ao_cmd_status != ao_cmd_success)
313 if (ao_cmd_lex_i == 0) {
314 up = ao_config_accel_calibrate_auto("up");
315 down = ao_config_accel_calibrate_auto("down");
319 if (ao_cmd_status != ao_cmd_success)
324 printf("Invalid accel: up (%d) down (%d)\n",
328 _ao_config_edit_start();
329 ao_config.accel_plus_g = up;
330 ao_config.accel_minus_g = down;
331 _ao_config_edit_finish();
333 #endif /* HAS_ACCEL */
336 ao_config_apogee_delay_show(void) __reentrant
338 printf("Apogee delay: %d seconds\n",
339 ao_config.apogee_delay);
343 ao_config_apogee_delay_set(void) __reentrant
346 if (ao_cmd_status != ao_cmd_success)
348 _ao_config_edit_start();
349 ao_config.apogee_delay = ao_cmd_lex_i;
350 _ao_config_edit_finish();
354 ao_config_apogee_lockout_show(void) __reentrant
356 printf ("Apogee lockout: %d seconds\n",
357 ao_config.apogee_lockout);
361 ao_config_apogee_lockout_set(void) __reentrant
364 if (ao_cmd_status != ao_cmd_success)
366 _ao_config_edit_start();
367 ao_config.apogee_lockout = ao_cmd_lex_i;
368 _ao_config_edit_finish();
371 #endif /* HAS_FLIGHT */
375 ao_config_radio_cal_show(void) __reentrant
377 printf("Radio cal: %ld\n", ao_config.radio_cal);
381 ao_config_radio_cal_set(void) __reentrant
384 if (ao_cmd_status != ao_cmd_success)
386 _ao_config_edit_start();
387 ao_config.radio_cal = ao_cmd_lex_u32;
388 ao_config_set_radio();
389 _ao_config_edit_finish();
395 ao_config_log_show(void) __reentrant
397 printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
401 ao_config_log_set(void) __reentrant
403 uint16_t block = (uint16_t) (ao_storage_block >> 10);
404 uint16_t config = (uint16_t) (ao_storage_config >> 10);
407 if (ao_cmd_status != ao_cmd_success)
409 if (ao_log_present())
410 printf("Storage must be empty before changing log size\n");
411 else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
412 printf("Flight log size must be multiple of %d kB\n", block);
413 else if (ao_cmd_lex_i > config)
414 printf("Flight log max %d kB\n", config);
416 _ao_config_edit_start();
417 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
418 _ao_config_edit_finish();
425 ao_config_ignite_mode_show(void) __reentrant
427 printf("Ignite mode: %d\n", ao_config.ignite_mode);
431 ao_config_ignite_mode_set(void) __reentrant
434 if (ao_cmd_status != ao_cmd_success)
436 _ao_config_edit_start();
437 ao_config.ignite_mode = ao_cmd_lex_i;
438 _ao_config_edit_finish();
444 ao_config_pad_orientation_show(void) __reentrant
446 printf("Pad orientation: %d\n", ao_config.pad_orientation);
450 ao_config_pad_orientation_set(void) __reentrant
453 if (ao_cmd_status != ao_cmd_success)
455 _ao_config_edit_start();
457 if (ao_config.pad_orientation != ao_cmd_lex_i) {
459 t = ao_config.accel_plus_g;
460 ao_config.accel_plus_g = 0x7fff - ao_config.accel_minus_g;
461 ao_config.accel_minus_g = 0x7fff - t;
463 ao_config.pad_orientation = ao_cmd_lex_i;
464 _ao_config_edit_finish();
470 ao_config_radio_enable_show(void) __reentrant
472 printf("Radio enable: %d\n", ao_config.radio_enable);
476 ao_config_radio_enable_set(void) __reentrant
479 if (ao_cmd_status != ao_cmd_success)
481 _ao_config_edit_start();
482 ao_config.radio_enable = ao_cmd_lex_i;
483 _ao_config_edit_finish();
485 #endif /* HAS_RADIO */
489 __xdata uint8_t ao_config_aes_seq = 1;
492 ao_config_key_show(void) __reentrant
496 for (i = 0; i < AO_AES_LEN; i++)
497 printf ("%02x", ao_config.aes_key[i]);
502 ao_config_key_set(void) __reentrant
506 _ao_config_edit_start();
507 for (i = 0; i < AO_AES_LEN; i++) {
509 if (ao_cmd_status != ao_cmd_success)
511 ao_config.aes_key[i] = ao_cmd_lex_i;
514 _ao_config_edit_finish();
521 ao_config_aprs_show(void)
523 printf ("APRS interval: %d width %d\n", ao_config.aprs_interval, ao_config.aprs_width);
527 ao_config_aprs_set(void)
530 if (ao_cmd_status != ao_cmd_success)
532 _ao_config_edit_start();
533 ao_config.aprs_interval = ao_cmd_lex_i;
535 if (ao_cmd_status == ao_cmd_success &&
536 AO_APRS_WIDTH_NARROW <= ao_cmd_lex_i &&
537 ao_cmd_lex_i <= AO_APRS_WIDTH_WIDE)
539 ao_config.aprs_width = ao_cmd_lex_i;
541 ao_config.aprs_width = AO_APRS_WIDTH_MEDIUM;
542 _ao_config_edit_finish();
545 #endif /* HAS_APRS */
550 ao_config_radio_amp_show(void)
552 printf ("Radio amp setting: %d\n", ao_config.radio_amp);
556 ao_config_radio_amp_set(void)
559 if (ao_cmd_status != ao_cmd_success)
561 _ao_config_edit_start();
562 ao_config.radio_amp = ao_cmd_lex_i;
563 _ao_config_edit_finish();
571 ao_config_radio_power_show(void)
573 printf ("Radio power setting: %d\n", ao_config.radio_power);
577 ao_config_radio_power_set(void)
580 if (ao_cmd_status != ao_cmd_success)
582 _ao_config_edit_start();
583 ao_config.radio_power = ao_cmd_lex_i;
584 _ao_config_edit_finish();
589 struct ao_config_var {
591 void (*set)(void) __reentrant;
592 void (*show)(void) __reentrant;
596 ao_config_help(void) __reentrant;
599 ao_config_show(void) __reentrant;
602 ao_config_write(void) __reentrant;
604 __code struct ao_config_var ao_config_vars[] = {
606 { "m <meters>\0Main deploy (m)",
607 ao_config_main_deploy_set, ao_config_main_deploy_show, },
608 { "d <delay>\0Apogee delay (s)",
609 ao_config_apogee_delay_set, ao_config_apogee_delay_show },
610 { "L <seconds>\0Apogee detect lockout (s)",
611 ao_config_apogee_lockout_set, ao_config_apogee_lockout_show, },
612 #endif /* HAS_FLIGHT */
614 { "F <freq>\0Frequency (kHz)",
615 ao_config_frequency_set, ao_config_frequency_show },
616 { "c <call>\0Callsign (8 char max)",
617 ao_config_callsign_set, ao_config_callsign_show },
618 { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
619 ao_config_radio_enable_set, ao_config_radio_enable_show },
620 { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
621 ao_config_radio_cal_set, ao_config_radio_cal_show },
623 { "p <setting>\0Radio power setting (0-255)",
624 ao_config_radio_power_set, ao_config_radio_power_show },
627 { "d <setting>\0Radio amplifier setting (0-3)",
628 ao_config_radio_amp_set, ao_config_radio_amp_show },
630 #endif /* HAS_RADIO */
632 { "a <+g> <-g>\0Accel calib (0 for auto)",
633 ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
634 { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
635 ao_config_pad_orientation_set,ao_config_pad_orientation_show },
636 #endif /* HAS_ACCEL */
638 { "l <size>\0Flight log size (kB)",
639 ao_config_log_set, ao_config_log_show },
642 { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
643 ao_config_ignite_mode_set, ao_config_ignite_mode_show },
646 { "k <32 hex digits>\0Set AES encryption key",
647 ao_config_key_set, ao_config_key_show },
650 { "P <n,?>\0Configure pyro channels",
651 ao_pyro_set, ao_pyro_show },
654 { "A <secs>\0APRS packet interval (0 disable)",
655 ao_config_aprs_set, ao_config_aprs_show },
660 { "w\0Write to eeprom",
661 ao_config_write, 0 },
677 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
678 if (ao_config_vars[cmd].str[0] == c) {
679 (*ao_config_vars[cmd].set)();
682 ao_cmd_status = ao_cmd_syntax_error;
686 ao_config_help(void) __reentrant
689 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
691 ao_config_vars[cmd].str,
692 ao_config_vars[cmd].str+1+
693 strlen(ao_config_vars[cmd].str));
697 ao_config_show(void) __reentrant
701 printf("Config version: %d.%d\n",
702 ao_config.major, ao_config.minor);
703 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
704 if (ao_config_vars[cmd].show)
705 (*ao_config_vars[cmd].show)();
710 ao_config_write(void) __reentrant
713 ao_mutex_get(&ao_config_mutex);
714 if (ao_config_dirty) {
719 ao_mutex_put(&ao_config_mutex);
723 puts("Nothing to save");
727 __code struct ao_cmds ao_config_cmds[] = {
728 { ao_config_set, "c <var> <value>\0Set config (? for help, s to show)" },
735 ao_cmd_register(&ao_config_cmds[0]);