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 __xdata struct ao_config ao_config;
21 __pdata uint8_t ao_config_loaded;
22 __pdata uint8_t ao_config_dirty;
23 __xdata uint8_t ao_config_mutex;
25 #define AO_CONFIG_DEFAULT_MAIN_DEPLOY 250
26 #define AO_CONFIG_DEFAULT_RADIO_CHANNEL 0
27 #define AO_CONFIG_DEFAULT_CALLSIGN "N0CALL"
28 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G 16000
29 #define AO_CONFIG_DEFAULT_APOGEE_DELAY 0
30 #define AO_CONFIG_DEFAULT_IGNITE_MODE AO_IGNITE_MODE_DUAL
31 #define AO_CONFIG_DEFAULT_PAD_ORIENTATION AO_PAD_ORIENTATION_ANTENNA_UP
33 #ifndef USE_INTERNAL_FLASH
34 #error Please define USE_INTERNAL_FLASH
37 #ifndef AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX
38 #if USE_INTERNAL_FLASH
39 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ao_storage_config
41 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ((uint32_t) 192 * (uint32_t) 1024)
50 ao_storage_erase(ao_storage_config);
51 ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
53 ao_log_write_erase(0);
61 ao_mutex_get(&ao_config_mutex);
63 ao_mutex_put(&ao_config_mutex);
74 ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
76 if (ao_config.major != AO_CONFIG_MAJOR) {
77 ao_config.major = AO_CONFIG_MAJOR;
81 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
82 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
83 ao_xmemset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
84 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
85 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
88 if (ao_config.minor < AO_CONFIG_MINOR) {
89 /* Fixups for minor version 1 */
90 if (ao_config.minor < 1)
91 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
92 /* Fixups for minor version 2 */
93 if (ao_config.minor < 2) {
94 ao_config.accel_plus_g = 0;
95 ao_config.accel_minus_g = 0;
97 /* Fixups for minor version 3 */
98 if (ao_config.minor < 3)
99 ao_config.radio_cal = ao_radio_cal;
100 /* Fixups for minor version 4 */
101 if (ao_config.minor < 4)
102 ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
103 /* Fixupes for minor version 5 */
104 if (ao_config.minor < 5)
105 ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
106 if (ao_config.minor < 6)
107 ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
108 if (ao_config.minor < 7)
109 ao_config.radio_setting = ao_config.radio_cal;
110 if (ao_config.minor < 8)
111 ao_config.radio_enable = TRUE;
112 if (ao_config.minor < 9)
113 memset(&ao_config.aes_key, 0, AO_AES_LEN);
114 ao_config.minor = AO_CONFIG_MINOR;
117 ao_config_loaded = 1;
121 _ao_config_edit_start(void)
123 ao_mutex_get(&ao_config_mutex);
128 _ao_config_edit_finish(void)
131 ao_mutex_put(&ao_config_mutex);
137 _ao_config_edit_start();
138 ao_mutex_put(&ao_config_mutex);
142 ao_config_callsign_show(void)
144 printf ("Callsign: \"%s\"\n", ao_config.callsign);
148 ao_config_callsign_set(void) __reentrant
151 static __xdata char callsign[AO_MAX_CALLSIGN + 1];
153 ao_xmemset(callsign, '\0', sizeof callsign);
156 while (ao_cmd_lex_c != '\n') {
157 if (c < AO_MAX_CALLSIGN)
158 callsign[c++] = ao_cmd_lex_c;
160 ao_cmd_status = ao_cmd_lex_error;
163 if (ao_cmd_status != ao_cmd_success)
165 _ao_config_edit_start();
166 ao_xmemcpy(&ao_config.callsign, &callsign,
167 AO_MAX_CALLSIGN + 1);
168 _ao_config_edit_finish();
172 ao_config_radio_channel_show(void) __reentrant
174 printf("Radio channel: %d\n",
175 ao_config.radio_channel);
179 ao_config_radio_channel_set(void) __reentrant
182 if (ao_cmd_status != ao_cmd_success)
184 _ao_config_edit_start();
185 ao_config.radio_channel = ao_cmd_lex_i;
186 _ao_config_edit_finish();
187 ao_radio_recv_abort();
193 ao_config_main_deploy_show(void) __reentrant
195 printf("Main deploy: %d meters\n",
196 ao_config.main_deploy);
200 ao_config_main_deploy_set(void) __reentrant
203 if (ao_cmd_status != ao_cmd_success)
205 _ao_config_edit_start();
206 ao_config.main_deploy = ao_cmd_lex_i;
207 _ao_config_edit_finish();
212 ao_config_accel_calibrate_show(void) __reentrant
214 printf("Accel cal +1g: %d -1g: %d\n",
215 ao_config.accel_plus_g, ao_config.accel_minus_g);
218 #define ACCEL_CALIBRATE_SAMPLES 1024
219 #define ACCEL_CALIBRATE_SHIFT 10
222 ao_config_accel_calibrate_auto(char *orientation) __reentrant
226 uint8_t cal_adc_ring;
228 printf("Orient antenna %s and press a key...", orientation);
231 puts("\r\n"); flush();
232 puts("Calibrating..."); flush();
233 i = ACCEL_CALIBRATE_SAMPLES;
235 cal_adc_ring = ao_sample_adc;
237 ao_sleep(DATA_TO_XDATA(&ao_sample_adc));
238 while (i && cal_adc_ring != ao_sample_adc) {
239 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
240 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
244 return accel_total >> ACCEL_CALIBRATE_SHIFT;
248 ao_config_accel_calibrate_set(void) __reentrant
252 if (ao_cmd_status != ao_cmd_success)
254 if (ao_cmd_lex_i == 0) {
255 up = ao_config_accel_calibrate_auto("up");
256 down = ao_config_accel_calibrate_auto("down");
260 if (ao_cmd_status != ao_cmd_success)
265 printf("Invalid accel: up (%d) down (%d)\n",
269 _ao_config_edit_start();
270 ao_config.accel_plus_g = up;
271 ao_config.accel_minus_g = down;
272 _ao_config_edit_finish();
274 #endif /* HAS_ACCEL */
277 ao_config_apogee_delay_show(void) __reentrant
279 printf("Apogee delay: %d seconds\n",
280 ao_config.apogee_delay);
284 ao_config_apogee_delay_set(void) __reentrant
287 if (ao_cmd_status != ao_cmd_success)
289 _ao_config_edit_start();
290 ao_config.apogee_delay = ao_cmd_lex_i;
291 _ao_config_edit_finish();
297 ao_config_radio_cal_show(void) __reentrant
299 printf("Radio cal: %ld\n", ao_config.radio_cal);
303 ao_config_radio_cal_set(void) __reentrant
306 if (ao_cmd_status != ao_cmd_success)
308 _ao_config_edit_start();
309 ao_config.radio_setting = ao_config.radio_cal = ao_cmd_lex_u32;
310 _ao_config_edit_finish();
315 ao_config_log_show(void) __reentrant
317 printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
321 ao_config_log_set(void) __reentrant
323 uint16_t block = (uint16_t) (ao_storage_block >> 10);
324 uint16_t config = (uint16_t) (ao_storage_config >> 10);
327 if (ao_cmd_status != ao_cmd_success)
329 if (ao_log_present())
330 printf("Storage must be empty before changing log size\n");
331 else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
332 printf("Flight log size must be multiple of %d kB\n", block);
333 else if (ao_cmd_lex_i > config)
334 printf("Flight log max %d kB\n", config);
336 _ao_config_edit_start();
337 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
338 _ao_config_edit_finish();
345 ao_config_ignite_mode_show(void) __reentrant
347 printf("Ignite mode: %d\n", ao_config.ignite_mode);
351 ao_config_ignite_mode_set(void) __reentrant
354 if (ao_cmd_status != ao_cmd_success)
356 _ao_config_edit_start();
357 ao_config.ignite_mode = ao_cmd_lex_i;
358 _ao_config_edit_finish();
364 ao_config_pad_orientation_show(void) __reentrant
366 printf("Pad orientation: %d\n", ao_config.pad_orientation);
370 ao_config_pad_orientation_set(void) __reentrant
373 if (ao_cmd_status != ao_cmd_success)
375 _ao_config_edit_start();
377 if (ao_config.pad_orientation != ao_cmd_lex_i) {
379 t = ao_config.accel_plus_g;
380 ao_config.accel_plus_g = 0x7fff - ao_config.accel_minus_g;
381 ao_config.accel_minus_g = 0x7fff - t;
383 ao_config.pad_orientation = ao_cmd_lex_i;
384 _ao_config_edit_finish();
389 ao_config_radio_setting_show(void) __reentrant
391 printf("Radio setting: %ld\n", ao_config.radio_setting);
395 ao_config_radio_setting_set(void) __reentrant
398 if (ao_cmd_status != ao_cmd_success)
400 _ao_config_edit_start();
401 ao_config.radio_setting = ao_cmd_lex_u32;
402 ao_config.radio_channel = 0;
403 _ao_config_edit_finish();
404 ao_radio_recv_abort();
408 ao_config_radio_enable_show(void) __reentrant
410 printf("Radio enable: %d\n", ao_config.radio_enable);
414 ao_config_radio_enable_set(void) __reentrant
417 if (ao_cmd_status != ao_cmd_success)
419 _ao_config_edit_start();
420 ao_config.radio_enable = ao_cmd_lex_i;
421 _ao_config_edit_finish();
426 ao_config_key_show(void) __reentrant
430 for (i = 0; i < AO_AES_LEN; i++)
431 printf ("%02x", ao_config.aes_key[i]);
436 ao_config_key_set(void) __reentrant
440 _ao_config_edit_start();
441 for (i = 0; i < AO_AES_LEN; i++) {
443 if (ao_cmd_status != ao_cmd_success)
445 ao_config.aes_key[i] = ao_cmd_lex_i;
447 _ao_config_edit_finish();
451 struct ao_config_var {
453 void (*set)(void) __reentrant;
454 void (*show)(void) __reentrant;
458 ao_config_help(void) __reentrant;
461 ao_config_show(void) __reentrant;
464 ao_config_write(void) __reentrant;
466 __code struct ao_config_var ao_config_vars[] = {
468 { "m <meters>\0Main deploy (in meters)",
469 ao_config_main_deploy_set, ao_config_main_deploy_show, },
470 { "d <delay>\0Apogee delay (in seconds)",
471 ao_config_apogee_delay_set, ao_config_apogee_delay_show },
473 { "r <channel>\0Radio channel (freq = 434.550 + chan * .1)",
474 ao_config_radio_channel_set, ao_config_radio_channel_show },
475 { "c <call>\0Callsign (8 char max)",
476 ao_config_callsign_set, ao_config_callsign_show },
477 { "R <setting>\0Radio freq control (freq = 434.550 * setting/cal)",
478 ao_config_radio_setting_set, ao_config_radio_setting_show },
479 { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
480 ao_config_radio_enable_set, ao_config_radio_enable_show },
482 { "a <+g> <-g>\0Accel calib (0 for auto)",
483 ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
484 #endif /* HAS_ACCEL */
485 { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
486 ao_config_radio_cal_set, ao_config_radio_cal_show },
488 { "l <size>\0Flight log size in kB",
489 ao_config_log_set, ao_config_log_show },
492 { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
493 ao_config_ignite_mode_set, ao_config_ignite_mode_show },
496 { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
497 ao_config_pad_orientation_set,ao_config_pad_orientation_show },
500 { "k <32 hex digits>\0Set AES encryption key",
501 ao_config_key_set, ao_config_key_show },
506 { "w\0Write to eeprom",
507 ao_config_write, 0 },
519 void (*__xdata func)(void) __reentrant;
525 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
526 if (ao_config_vars[cmd].str[0] == c) {
527 (*ao_config_vars[cmd].set)();
530 ao_cmd_status = ao_cmd_syntax_error;
534 ao_config_help(void) __reentrant
537 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
539 ao_config_vars[cmd].str,
540 ao_config_vars[cmd].str+1+
541 strlen(ao_config_vars[cmd].str));
545 ao_config_show(void) __reentrant
548 printf("Config version: %d.%d\n",
549 ao_config.major, ao_config.minor);
550 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
551 if (ao_config_vars[cmd].show)
552 (*ao_config_vars[cmd].show)();
557 ao_config_write(void) __reentrant
560 ao_mutex_get(&ao_config_mutex);
561 if (ao_config_dirty) {
566 ao_mutex_put(&ao_config_mutex);
570 puts("Nothing to save");
574 __code struct ao_cmds ao_config_cmds[] = {
575 { ao_config_set, "c <var> <value>\0Set config variable (? for help, s to show)" },
582 ao_cmd_register(&ao_config_cmds[0]);