Allow radio calibration to be set from ao-load
[fw/altos] / src / ao_config.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
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;
24
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 /*
31  * For 434.550MHz, the frequency value is:
32  *
33  * 434.550e6 / (24e6 / 2**16) = 1186611.2
34  *
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.
38  */
39 const uint32_t ao_radio_cal = 1186611;
40
41 static void
42 _ao_config_put(void)
43 {
44         ao_ee_write_config((uint8_t *) &ao_config, sizeof (ao_config));
45 }
46
47 static void
48 _ao_config_get(void)
49 {
50         if (ao_config_loaded)
51                 return;
52         ao_ee_read_config((uint8_t *) &ao_config, sizeof (ao_config));
53         if (ao_config.major != AO_CONFIG_MAJOR) {
54                 ao_config.major = AO_CONFIG_MAJOR;
55                 ao_config.minor = AO_CONFIG_MINOR;
56                 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
57                 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
58                 ao_config.accel_plus_g = 0;
59                 ao_config.accel_minus_g = 0;
60                 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
61                 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
62                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
63                 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
64                 ao_config.radio_cal = ao_radio_cal;
65                 ao_config_dirty = 1;
66         }
67         if (ao_config.minor < AO_CONFIG_MINOR) {
68                 /* Fixups for minor version 1 */
69                 if (ao_config.minor < 1)
70                         ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
71                 /* Fixups for minor version 2 */
72                 if (ao_config.minor < 2) {
73                         ao_config.accel_plus_g = 0;
74                         ao_config.accel_minus_g = 0;
75                 }
76                 /* Fixups for minor version 3 */
77                 if (ao_config.minor < 3)
78                         ao_config.radio_cal = ao_radio_cal;
79                 ao_config.minor = AO_CONFIG_MINOR;
80                 ao_config_dirty = 1;
81         }
82         ao_config_loaded = 1;
83 }
84
85 void
86 ao_config_get(void)
87 {
88         ao_mutex_get(&ao_config_mutex);
89         _ao_config_get();
90         ao_mutex_put(&ao_config_mutex);
91 }
92
93 void
94 ao_config_callsign_show(void)
95 {
96         printf ("Callsign: \"%s\"\n", ao_config.callsign);
97 }
98
99 void
100 ao_config_callsign_set(void) __reentrant
101 {
102         uint8_t c;
103         char callsign[AO_MAX_CALLSIGN + 1];
104
105         ao_cmd_white();
106         c = 0;
107         while (ao_cmd_lex_c != '\n') {
108                 if (c < AO_MAX_CALLSIGN)
109                         callsign[c++] = ao_cmd_lex_c;
110                 else
111                         ao_cmd_status = ao_cmd_lex_error;
112                 ao_cmd_lex();
113         }
114         if (ao_cmd_status != ao_cmd_success)
115                 return;
116         ao_mutex_get(&ao_config_mutex);
117         _ao_config_get();
118         while (c < AO_MAX_CALLSIGN + 1)
119                 callsign[c++] = '\0';
120         memcpy(&ao_config.callsign, &callsign,
121                AO_MAX_CALLSIGN + 1);
122         ao_config_dirty = 1;
123         ao_mutex_put(&ao_config_mutex);
124         ao_config_callsign_show();
125 }
126
127 void
128 ao_config_radio_channel_show(void) __reentrant
129 {
130         uint32_t        freq = 434550L + ao_config.radio_channel * 100L;
131         uint16_t        mhz = freq / 1000L;
132         uint16_t        khz = freq % 1000L;
133
134         printf("Radio channel: %d (%d.%03dMHz)\n",
135                ao_config.radio_channel, mhz, khz);
136 }
137
138 void
139 ao_config_radio_channel_set(void) __reentrant
140 {
141         ao_cmd_decimal();
142         if (ao_cmd_status != ao_cmd_success)
143                 return;
144         ao_mutex_get(&ao_config_mutex);
145         _ao_config_get();
146         ao_config.radio_channel = ao_cmd_lex_i;
147         ao_config_dirty = 1;
148         ao_mutex_put(&ao_config_mutex);
149         ao_config_radio_channel_show();
150 }
151
152 void
153 ao_config_main_deploy_show(void) __reentrant
154 {
155         printf("Main deploy: %d meters (%d feet)\n",
156                ao_config.main_deploy,
157                (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
158 }
159
160 void
161 ao_config_main_deploy_set(void) __reentrant
162 {
163         ao_cmd_decimal();
164         if (ao_cmd_status != ao_cmd_success)
165                 return;
166         ao_mutex_get(&ao_config_mutex);
167         _ao_config_get();
168         ao_config.main_deploy = ao_cmd_lex_i;
169         ao_config_dirty = 1;
170         ao_mutex_put(&ao_config_mutex);
171         ao_config_main_deploy_show();
172 }
173
174 void
175 ao_config_accel_calibrate_show(void) __reentrant
176 {
177         printf("Accel cal +1g: %d -1g: %d\n",
178                ao_config.accel_plus_g, ao_config.accel_minus_g);
179 }
180
181 #define ACCEL_CALIBRATE_SAMPLES 1024
182 #define ACCEL_CALIBRATE_SHIFT   10
183
184 static int16_t
185 ao_config_accel_calibrate_auto(char *orientation) __reentrant
186 {
187         uint16_t        i;
188         int32_t         accel_total;
189         uint8_t         cal_adc_ring;
190
191         printf("Orient %s and press a key...", orientation);
192         flush();
193         (void) getchar();
194         puts("\r\n"); flush();
195         puts("Calibrating..."); flush();
196         i = ACCEL_CALIBRATE_SAMPLES;
197         accel_total = 0;
198         cal_adc_ring = ao_adc_head;
199         while (i) {
200                 ao_sleep(&ao_adc_ring);
201                 while (i && cal_adc_ring != ao_adc_head) {
202                         accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
203                         cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
204                         i--;
205                 }
206         }
207         return accel_total >> ACCEL_CALIBRATE_SHIFT;
208 }
209
210 void
211 ao_config_accel_calibrate_set(void) __reentrant
212 {
213         int16_t up, down;
214         ao_cmd_decimal();
215         if (ao_cmd_status != ao_cmd_success)
216                 return;
217         if (ao_cmd_lex_i == 0) {
218                 up = ao_config_accel_calibrate_auto("antenna up");
219                 down = ao_config_accel_calibrate_auto("antenna down");
220         } else {
221                 up = ao_cmd_lex_i;
222                 ao_cmd_decimal();
223                 if (ao_cmd_status != ao_cmd_success)
224                         return;
225                 down = ao_cmd_lex_i;
226         }
227         if (up >= down) {
228                 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
229                        up, down);
230                 return;
231         }
232         ao_mutex_get(&ao_config_mutex);
233         _ao_config_get();
234         ao_config.accel_plus_g = up;
235         ao_config.accel_minus_g = down;
236         ao_config_dirty = 1;
237         ao_mutex_put(&ao_config_mutex);
238         ao_config_accel_calibrate_show();
239 }
240
241 void
242 ao_config_apogee_delay_show(void) __reentrant
243 {
244         printf("Apogee delay: %d seconds\n",
245                ao_config.apogee_delay);
246 }
247
248 void
249 ao_config_apogee_delay_set(void) __reentrant
250 {
251         ao_cmd_decimal();
252         if (ao_cmd_status != ao_cmd_success)
253                 return;
254         ao_mutex_get(&ao_config_mutex);
255         _ao_config_get();
256         ao_config.apogee_delay = ao_cmd_lex_i;
257         ao_config_dirty = 1;
258         ao_mutex_put(&ao_config_mutex);
259         ao_config_apogee_delay_show();
260 }
261
262 void
263 ao_config_radio_cal_show(void) __reentrant
264 {
265         printf("Radio cal: %ld\n", ao_config.radio_cal);
266 }
267
268 void
269 ao_config_radio_cal_set(void) __reentrant
270 {
271         ao_cmd_decimal();
272         if (ao_cmd_status != ao_cmd_success)
273                 return;
274         ao_mutex_get(&ao_config_mutex);
275         _ao_config_get();
276         ao_config.radio_cal = ao_cmd_lex_u32;
277         ao_config_dirty = 1;
278         ao_mutex_put(&ao_config_mutex);
279         ao_config_radio_cal_show();
280 }
281
282 struct ao_config_var {
283         char            cmd;
284         void            (*set)(void) __reentrant;
285         void            (*show)(void) __reentrant;
286         const char      *help;
287 };
288
289 void
290 ao_config_help(void) __reentrant;
291
292 void
293 ao_config_show(void) __reentrant;
294
295 void
296 ao_config_write(void) __reentrant;
297
298 __code struct ao_config_var ao_config_vars[] = {
299         { 'm',  ao_config_main_deploy_set,      ao_config_main_deploy_show,
300                 "m <meters>  Set height above launch for main deploy (in meters)" },
301         { 'd',  ao_config_apogee_delay_set,     ao_config_apogee_delay_show,
302                 "d <delay>   Set apogee igniter delay (in seconds)" },
303         { 'r',  ao_config_radio_channel_set,    ao_config_radio_channel_show,
304                 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
305         { 'c',  ao_config_callsign_set,         ao_config_callsign_show,
306                 "c <call>    Set callsign broadcast in each packet (8 char max)" },
307         { 'a',  ao_config_accel_calibrate_set,  ao_config_accel_calibrate_show,
308                 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
309         { 'f',  ao_config_radio_cal_set,        ao_config_radio_cal_show,
310                 "f <cal>     Set radio calibration value (cal = rf/(xtal/2^16))" },
311         { 's',  ao_config_show,                 ao_config_show,
312                 "s           Show current config values" },
313         { 'w',  ao_config_write,                ao_config_write,
314                 "w           Write current values to eeprom" },
315         { '?',  ao_config_help,                 ao_config_help,
316                 "?           Show available config variables" },
317         { 0,    ao_config_main_deploy_set,      ao_config_main_deploy_show,
318                 NULL },
319 };
320
321 void
322 ao_config_set(void)
323 {
324         char    c;
325         uint8_t cmd;
326         void (*__xdata func)(void) __reentrant;
327
328         ao_cmd_white();
329         c = ao_cmd_lex_c;
330         ao_cmd_lex();
331         func = 0;
332         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
333                 if (ao_config_vars[cmd].cmd == c) {
334                         func = ao_config_vars[cmd].set;
335                         break;
336                 }
337         if (func)
338                 (*func)();
339         else
340                 ao_cmd_status = ao_cmd_syntax_error;
341 }
342
343 void
344 ao_config_help(void) __reentrant
345 {
346         uint8_t cmd;
347         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
348                 puts (ao_config_vars[cmd].help);
349 }
350
351 void
352 ao_config_show(void) __reentrant
353 {
354         uint8_t cmd;
355         printf("Config version: %d.%d\n",
356                ao_config.major, ao_config.minor);
357         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
358                 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
359                         (*ao_config_vars[cmd].show)();
360 }
361
362 void
363 ao_config_write(void) __reentrant
364 {
365         ao_mutex_get(&ao_config_mutex);
366         if (ao_config_dirty) {
367                 _ao_config_put();
368                 ao_config_dirty = 0;
369                 printf("Saved\n");
370         }
371         ao_mutex_put(&ao_config_mutex);
372 }
373
374 __code struct ao_cmds ao_config_cmds[] = {
375         { 'c',  ao_config_set,  "c <var> <value>                    Set config variable (? for help, s to show)" },
376         { '\0', ao_config_set, NULL },
377 };
378
379 void
380 ao_config_init(void)
381 {
382         ao_cmd_register(&ao_config_cmds[0]);
383 }