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 __xdata uint8_t ao_config_loaded;
22 __xdata 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
31 * For 434.550MHz, the frequency value is:
33 * 434.550e6 / (24e6 / 2**16) = 1186611.2
35 * This value is stored in a const variable so that
36 * ao-load can change it during programming for
37 * devices that have no eeprom for config data.
39 const uint32_t ao_radio_cal = 1186611;
45 ao_ee_write_config((uint8_t *) &ao_config, sizeof (ao_config));
55 ao_ee_read_config((uint8_t *) &ao_config, sizeof (ao_config));
57 if (ao_config.major != AO_CONFIG_MAJOR) {
58 ao_config.major = AO_CONFIG_MAJOR;
59 ao_config.minor = AO_CONFIG_MINOR;
60 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
61 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
62 ao_config.accel_plus_g = 0;
63 ao_config.accel_minus_g = 0;
64 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
65 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
66 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
67 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
68 ao_config.radio_cal = ao_radio_cal;
71 if (ao_config.minor < AO_CONFIG_MINOR) {
72 /* Fixups for minor version 1 */
73 if (ao_config.minor < 1)
74 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
75 /* Fixups for minor version 2 */
76 if (ao_config.minor < 2) {
77 ao_config.accel_plus_g = 0;
78 ao_config.accel_minus_g = 0;
80 /* Fixups for minor version 3 */
81 if (ao_config.minor < 3)
82 ao_config.radio_cal = ao_radio_cal;
83 ao_config.minor = AO_CONFIG_MINOR;
92 ao_mutex_get(&ao_config_mutex);
94 ao_mutex_put(&ao_config_mutex);
98 ao_config_callsign_show(void)
100 printf ("Callsign: \"%s\"\n", ao_config.callsign);
104 ao_config_callsign_set(void) __reentrant
107 char callsign[AO_MAX_CALLSIGN + 1];
111 while (ao_cmd_lex_c != '\n') {
112 if (c < AO_MAX_CALLSIGN)
113 callsign[c++] = ao_cmd_lex_c;
115 ao_cmd_status = ao_cmd_lex_error;
118 if (ao_cmd_status != ao_cmd_success)
120 ao_mutex_get(&ao_config_mutex);
122 while (c < AO_MAX_CALLSIGN + 1)
123 callsign[c++] = '\0';
124 memcpy(&ao_config.callsign, &callsign,
125 AO_MAX_CALLSIGN + 1);
127 ao_mutex_put(&ao_config_mutex);
128 ao_config_callsign_show();
132 ao_config_radio_channel_show(void) __reentrant
134 uint32_t freq = 434550L + ao_config.radio_channel * 100L;
135 uint16_t mhz = freq / 1000L;
136 uint16_t khz = freq % 1000L;
138 printf("Radio channel: %d (%d.%03dMHz)\n",
139 ao_config.radio_channel, mhz, khz);
143 ao_config_radio_channel_set(void) __reentrant
146 if (ao_cmd_status != ao_cmd_success)
148 ao_mutex_get(&ao_config_mutex);
150 ao_config.radio_channel = ao_cmd_lex_i;
152 ao_mutex_put(&ao_config_mutex);
153 ao_config_radio_channel_show();
159 ao_config_main_deploy_show(void) __reentrant
161 printf("Main deploy: %d meters (%d feet)\n",
162 ao_config.main_deploy,
163 (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
167 ao_config_main_deploy_set(void) __reentrant
170 if (ao_cmd_status != ao_cmd_success)
172 ao_mutex_get(&ao_config_mutex);
174 ao_config.main_deploy = ao_cmd_lex_i;
176 ao_mutex_put(&ao_config_mutex);
177 ao_config_main_deploy_show();
181 ao_config_accel_calibrate_show(void) __reentrant
183 printf("Accel cal +1g: %d -1g: %d\n",
184 ao_config.accel_plus_g, ao_config.accel_minus_g);
187 #define ACCEL_CALIBRATE_SAMPLES 1024
188 #define ACCEL_CALIBRATE_SHIFT 10
191 ao_config_accel_calibrate_auto(char *orientation) __reentrant
195 uint8_t cal_adc_ring;
197 printf("Orient %s and press a key...", orientation);
200 puts("\r\n"); flush();
201 puts("Calibrating..."); flush();
202 i = ACCEL_CALIBRATE_SAMPLES;
204 cal_adc_ring = ao_adc_head;
206 ao_sleep(&ao_adc_ring);
207 while (i && cal_adc_ring != ao_adc_head) {
208 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
209 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
213 return accel_total >> ACCEL_CALIBRATE_SHIFT;
217 ao_config_accel_calibrate_set(void) __reentrant
221 if (ao_cmd_status != ao_cmd_success)
223 if (ao_cmd_lex_i == 0) {
224 up = ao_config_accel_calibrate_auto("antenna up");
225 down = ao_config_accel_calibrate_auto("antenna down");
229 if (ao_cmd_status != ao_cmd_success)
234 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
238 ao_mutex_get(&ao_config_mutex);
240 ao_config.accel_plus_g = up;
241 ao_config.accel_minus_g = down;
243 ao_mutex_put(&ao_config_mutex);
244 ao_config_accel_calibrate_show();
248 ao_config_apogee_delay_show(void) __reentrant
250 printf("Apogee delay: %d seconds\n",
251 ao_config.apogee_delay);
255 ao_config_apogee_delay_set(void) __reentrant
258 if (ao_cmd_status != ao_cmd_success)
260 ao_mutex_get(&ao_config_mutex);
262 ao_config.apogee_delay = ao_cmd_lex_i;
264 ao_mutex_put(&ao_config_mutex);
265 ao_config_apogee_delay_show();
271 ao_config_radio_cal_show(void) __reentrant
273 printf("Radio cal: %ld\n", ao_config.radio_cal);
277 ao_config_radio_cal_set(void) __reentrant
280 if (ao_cmd_status != ao_cmd_success)
282 ao_mutex_get(&ao_config_mutex);
284 ao_config.radio_cal = ao_cmd_lex_u32;
286 ao_mutex_put(&ao_config_mutex);
287 ao_config_radio_cal_show();
290 struct ao_config_var {
292 void (*set)(void) __reentrant;
293 void (*show)(void) __reentrant;
298 ao_config_help(void) __reentrant;
301 ao_config_show(void) __reentrant;
304 ao_config_write(void) __reentrant;
306 __code struct ao_config_var ao_config_vars[] = {
308 { 'm', ao_config_main_deploy_set, ao_config_main_deploy_show,
309 "m <meters> Set height above launch for main deploy (in meters)" },
310 { 'd', ao_config_apogee_delay_set, ao_config_apogee_delay_show,
311 "d <delay> Set apogee igniter delay (in seconds)" },
313 { 'r', ao_config_radio_channel_set, ao_config_radio_channel_show,
314 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
315 { 'c', ao_config_callsign_set, ao_config_callsign_show,
316 "c <call> Set callsign broadcast in each packet (8 char max)" },
318 { 'a', ao_config_accel_calibrate_set, ao_config_accel_calibrate_show,
319 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
321 { 'f', ao_config_radio_cal_set, ao_config_radio_cal_show,
322 "f <cal> Set radio calibration value (cal = rf/(xtal/2^16))" },
323 { 's', ao_config_show, ao_config_show,
324 "s Show current config values" },
326 { 'w', ao_config_write, ao_config_write,
327 "w Write current values to eeprom" },
329 { '?', ao_config_help, ao_config_help,
330 "? Show available config variables" },
331 { 0, ao_config_help, ao_config_help,
340 void (*__xdata func)(void) __reentrant;
346 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
347 if (ao_config_vars[cmd].cmd == c) {
348 func = ao_config_vars[cmd].set;
354 ao_cmd_status = ao_cmd_syntax_error;
358 ao_config_help(void) __reentrant
361 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
362 puts (ao_config_vars[cmd].help);
366 ao_config_show(void) __reentrant
369 printf("Config version: %d.%d\n",
370 ao_config.major, ao_config.minor);
371 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
372 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
373 (*ao_config_vars[cmd].show)();
378 ao_config_write(void) __reentrant
380 ao_mutex_get(&ao_config_mutex);
381 if (ao_config_dirty) {
386 ao_mutex_put(&ao_config_mutex);
390 __code struct ao_cmds ao_config_cmds[] = {
391 { 'c', ao_config_set, "c <var> <value> Set config variable (? for help, s to show)" },
392 { '\0', ao_config_set, NULL },
398 ao_cmd_register(&ao_config_cmds[0]);