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_sample.h>
23 __xdata struct ao_config ao_config;
24 __pdata uint8_t ao_config_loaded;
25 __pdata uint8_t ao_config_dirty;
26 __xdata uint8_t ao_config_mutex;
28 #define AO_CONFIG_DEFAULT_MAIN_DEPLOY 250
29 #define AO_CONFIG_DEFAULT_RADIO_CHANNEL 0
30 #define AO_CONFIG_DEFAULT_CALLSIGN "N0CALL"
31 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G 16000
32 #define AO_CONFIG_DEFAULT_APOGEE_DELAY 0
33 #define AO_CONFIG_DEFAULT_IGNITE_MODE AO_IGNITE_MODE_DUAL
34 #define AO_CONFIG_DEFAULT_PAD_ORIENTATION AO_PAD_ORIENTATION_ANTENNA_UP
36 #ifndef USE_INTERNAL_FLASH
37 #error Please define USE_INTERNAL_FLASH
40 #ifndef AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX
41 #if USE_INTERNAL_FLASH
42 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ao_storage_config
44 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ((uint32_t) 192 * (uint32_t) 1024)
53 ao_storage_erase(ao_storage_config);
54 ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
56 ao_log_write_erase(0);
64 ao_mutex_get(&ao_config_mutex);
66 ao_mutex_put(&ao_config_mutex);
72 ao_config_set_radio(void)
74 ao_config.radio_setting = ao_freq_to_set(ao_config.frequency, ao_config.radio_cal);
76 #endif /* HAS_RADIO */
85 ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
87 if (ao_config.major != AO_CONFIG_MAJOR) {
88 ao_config.major = AO_CONFIG_MAJOR;
92 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
93 ao_xmemset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
94 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
95 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
98 if (ao_config.minor != AO_CONFIG_MINOR) {
99 /* Fixups for minor version 1 */
100 if (ao_config.minor < 1)
101 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
102 /* Fixups for minor version 2 */
103 if (ao_config.minor < 2) {
104 ao_config.accel_plus_g = 0;
105 ao_config.accel_minus_g = 0;
107 /* Fixups for minor version 3 */
109 if (ao_config.minor < 3)
110 ao_config.radio_cal = ao_radio_cal;
112 /* Fixups for minor version 4 */
113 if (ao_config.minor < 4)
114 ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
115 /* Fixupes for minor version 5 */
116 if (ao_config.minor < 5)
117 ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
118 if (ao_config.minor < 6)
119 ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
120 if (ao_config.minor < 8)
121 ao_config.radio_enable = TRUE;
122 if (ao_config.minor < 9)
123 ao_xmemset(&ao_config.aes_key, '\0', AO_AES_LEN);
124 if (ao_config.minor < 10)
125 ao_config.frequency = 434550;
126 if (ao_config.minor < 11)
127 ao_config.apogee_lockout = 0;
128 ao_config.minor = AO_CONFIG_MINOR;
132 ao_config_set_radio();
134 ao_config_loaded = 1;
138 _ao_config_edit_start(void)
140 ao_mutex_get(&ao_config_mutex);
145 _ao_config_edit_finish(void)
148 ao_mutex_put(&ao_config_mutex);
154 _ao_config_edit_start();
155 ao_mutex_put(&ao_config_mutex);
159 ao_config_callsign_show(void)
161 printf ("Callsign: \"%s\"\n", ao_config.callsign);
165 ao_config_callsign_set(void) __reentrant
168 static __xdata char callsign[AO_MAX_CALLSIGN + 1];
170 ao_xmemset(callsign, '\0', sizeof callsign);
173 while (ao_cmd_lex_c != '\n') {
174 if (c < AO_MAX_CALLSIGN)
175 callsign[c++] = ao_cmd_lex_c;
177 ao_cmd_status = ao_cmd_lex_error;
180 if (ao_cmd_status != ao_cmd_success)
182 _ao_config_edit_start();
183 ao_xmemcpy(&ao_config.callsign, &callsign,
184 AO_MAX_CALLSIGN + 1);
185 _ao_config_edit_finish();
190 ao_config_frequency_show(void) __reentrant
192 printf("Frequency: %ld\n",
193 ao_config.frequency);
197 ao_config_frequency_set(void) __reentrant
200 if (ao_cmd_status != ao_cmd_success)
202 _ao_config_edit_start();
203 ao_config.frequency = ao_cmd_lex_u32;
204 ao_config_set_radio();
205 _ao_config_edit_finish();
206 ao_radio_recv_abort();
213 ao_config_main_deploy_show(void) __reentrant
215 printf("Main deploy: %d meters\n",
216 ao_config.main_deploy);
220 ao_config_main_deploy_set(void) __reentrant
223 if (ao_cmd_status != ao_cmd_success)
225 _ao_config_edit_start();
226 ao_config.main_deploy = ao_cmd_lex_i;
227 _ao_config_edit_finish();
232 ao_config_accel_calibrate_show(void) __reentrant
234 printf("Accel cal +1g: %d -1g: %d\n",
235 ao_config.accel_plus_g, ao_config.accel_minus_g);
238 #define ACCEL_CALIBRATE_SAMPLES 1024
239 #define ACCEL_CALIBRATE_SHIFT 10
242 ao_config_accel_calibrate_auto(char *orientation) __reentrant
246 uint8_t cal_data_ring;
248 printf("Orient antenna %s and press a key...", orientation);
251 puts("\r\n"); flush();
252 puts("Calibrating..."); flush();
253 i = ACCEL_CALIBRATE_SAMPLES;
255 cal_data_ring = ao_sample_data;
257 ao_sleep(DATA_TO_XDATA(&ao_sample_data));
258 while (i && cal_data_ring != ao_sample_data) {
259 accel_total += (int32_t) ao_data_accel(&ao_data_ring[cal_data_ring]);
260 cal_data_ring = ao_data_ring_next(cal_data_ring);
264 return accel_total >> ACCEL_CALIBRATE_SHIFT;
268 ao_config_accel_calibrate_set(void) __reentrant
272 if (ao_cmd_status != ao_cmd_success)
274 if (ao_cmd_lex_i == 0) {
275 up = ao_config_accel_calibrate_auto("up");
276 down = ao_config_accel_calibrate_auto("down");
280 if (ao_cmd_status != ao_cmd_success)
285 printf("Invalid accel: up (%d) down (%d)\n",
289 _ao_config_edit_start();
290 ao_config.accel_plus_g = up;
291 ao_config.accel_minus_g = down;
292 _ao_config_edit_finish();
294 #endif /* HAS_ACCEL */
297 ao_config_apogee_delay_show(void) __reentrant
299 printf("Apogee delay: %d seconds\n",
300 ao_config.apogee_delay);
304 ao_config_apogee_delay_set(void) __reentrant
307 if (ao_cmd_status != ao_cmd_success)
309 _ao_config_edit_start();
310 ao_config.apogee_delay = ao_cmd_lex_i;
311 _ao_config_edit_finish();
315 ao_config_apogee_lockout_show(void) __reentrant
317 printf ("Apogee lockout: %d seconds\n",
318 ao_config.apogee_lockout);
322 ao_config_apogee_lockout_set(void) __reentrant
325 if (ao_cmd_status != ao_cmd_success)
327 _ao_config_edit_start();
328 ao_config.apogee_lockout = ao_cmd_lex_i;
329 _ao_config_edit_finish();
332 #endif /* HAS_FLIGHT */
336 ao_config_radio_cal_show(void) __reentrant
338 printf("Radio cal: %ld\n", ao_config.radio_cal);
342 ao_config_radio_cal_set(void) __reentrant
345 if (ao_cmd_status != ao_cmd_success)
347 _ao_config_edit_start();
348 ao_config.radio_cal = ao_cmd_lex_u32;
349 ao_config_set_radio();
350 _ao_config_edit_finish();
356 ao_config_log_show(void) __reentrant
358 printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
362 ao_config_log_set(void) __reentrant
364 uint16_t block = (uint16_t) (ao_storage_block >> 10);
365 uint16_t config = (uint16_t) (ao_storage_config >> 10);
368 if (ao_cmd_status != ao_cmd_success)
370 if (ao_log_present())
371 printf("Storage must be empty before changing log size\n");
372 else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
373 printf("Flight log size must be multiple of %d kB\n", block);
374 else if (ao_cmd_lex_i > config)
375 printf("Flight log max %d kB\n", config);
377 _ao_config_edit_start();
378 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
379 _ao_config_edit_finish();
386 ao_config_ignite_mode_show(void) __reentrant
388 printf("Ignite mode: %d\n", ao_config.ignite_mode);
392 ao_config_ignite_mode_set(void) __reentrant
395 if (ao_cmd_status != ao_cmd_success)
397 _ao_config_edit_start();
398 ao_config.ignite_mode = ao_cmd_lex_i;
399 _ao_config_edit_finish();
405 ao_config_pad_orientation_show(void) __reentrant
407 printf("Pad orientation: %d\n", ao_config.pad_orientation);
411 ao_config_pad_orientation_set(void) __reentrant
414 if (ao_cmd_status != ao_cmd_success)
416 _ao_config_edit_start();
418 if (ao_config.pad_orientation != ao_cmd_lex_i) {
420 t = ao_config.accel_plus_g;
421 ao_config.accel_plus_g = 0x7fff - ao_config.accel_minus_g;
422 ao_config.accel_minus_g = 0x7fff - t;
424 ao_config.pad_orientation = ao_cmd_lex_i;
425 _ao_config_edit_finish();
431 ao_config_radio_enable_show(void) __reentrant
433 printf("Radio enable: %d\n", ao_config.radio_enable);
437 ao_config_radio_enable_set(void) __reentrant
440 if (ao_cmd_status != ao_cmd_success)
442 _ao_config_edit_start();
443 ao_config.radio_enable = ao_cmd_lex_i;
444 _ao_config_edit_finish();
446 #endif /* HAS_RADIO */
450 ao_config_key_show(void) __reentrant
454 for (i = 0; i < AO_AES_LEN; i++)
455 printf ("%02x", ao_config.aes_key[i]);
460 ao_config_key_set(void) __reentrant
464 _ao_config_edit_start();
465 for (i = 0; i < AO_AES_LEN; i++) {
467 if (ao_cmd_status != ao_cmd_success)
469 ao_config.aes_key[i] = ao_cmd_lex_i;
471 _ao_config_edit_finish();
475 struct ao_config_var {
477 void (*set)(void) __reentrant;
478 void (*show)(void) __reentrant;
482 ao_config_help(void) __reentrant;
485 ao_config_show(void) __reentrant;
488 ao_config_write(void) __reentrant;
490 __code struct ao_config_var ao_config_vars[] = {
492 { "m <meters>\0Main deploy (m)",
493 ao_config_main_deploy_set, ao_config_main_deploy_show, },
494 { "d <delay>\0Apogee delay (s)",
495 ao_config_apogee_delay_set, ao_config_apogee_delay_show },
496 { "L <seconds>\0Apogee detect lockout (s)",
497 ao_config_apogee_lockout_set, ao_config_apogee_lockout_show, },
498 #endif /* HAS_FLIGHT */
500 { "F <freq>\0Frequency (kHz)",
501 ao_config_frequency_set, ao_config_frequency_show },
502 { "c <call>\0Callsign (8 char max)",
503 ao_config_callsign_set, ao_config_callsign_show },
504 { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
505 ao_config_radio_enable_set, ao_config_radio_enable_show },
506 #endif /* HAS_RADIO */
508 { "a <+g> <-g>\0Accel calib (0 for auto)",
509 ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
510 #endif /* HAS_ACCEL */
512 { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
513 ao_config_radio_cal_set, ao_config_radio_cal_show },
514 #endif /* HAS_RADIO */
516 { "l <size>\0Flight log size (kB)",
517 ao_config_log_set, ao_config_log_show },
520 { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
521 ao_config_ignite_mode_set, ao_config_ignite_mode_show },
524 { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
525 ao_config_pad_orientation_set,ao_config_pad_orientation_show },
528 { "k <32 hex digits>\0Set AES encryption key",
529 ao_config_key_set, ao_config_key_show },
534 { "w\0Write to eeprom",
535 ao_config_write, 0 },
547 void (*__xdata func)(void) __reentrant;
553 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
554 if (ao_config_vars[cmd].str[0] == c) {
555 (*ao_config_vars[cmd].set)();
558 ao_cmd_status = ao_cmd_syntax_error;
562 ao_config_help(void) __reentrant
565 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
567 ao_config_vars[cmd].str,
568 ao_config_vars[cmd].str+1+
569 strlen(ao_config_vars[cmd].str));
573 ao_config_show(void) __reentrant
577 printf("Config version: %d.%d\n",
578 ao_config.major, ao_config.minor);
579 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
580 if (ao_config_vars[cmd].show)
581 (*ao_config_vars[cmd].show)();
586 ao_config_write(void) __reentrant
589 ao_mutex_get(&ao_config_mutex);
590 if (ao_config_dirty) {
595 ao_mutex_put(&ao_config_mutex);
599 puts("Nothing to save");
603 __code struct ao_cmds ao_config_cmds[] = {
604 { ao_config_set, "c <var> <value>\0Set config (? for help, s to show)" },
611 ao_cmd_register(&ao_config_cmds[0]);