e0eae78e46cf6c5631efaa5b041b3d7a5ef8a879
[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 __pdata uint8_t ao_config_loaded;
22 __pdata 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 #define AO_CONFIG_DEFAULT_IGNITE_MODE   AO_IGNITE_MODE_DUAL
31 #define AO_CONFIG_DEFAULT_PAD_ORIENTATION       AO_PAD_ORIENTATION_ANTENNA_UP
32 #if HAS_EEPROM
33 #ifndef USE_INTERNAL_FLASH
34 #error Please define USE_INTERNAL_FLASH
35 #endif
36 #endif
37 #if USE_INTERNAL_FLASH
38 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ao_storage_config
39 #else
40 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ((uint32_t) 192 * (uint32_t) 1024)
41 #endif
42
43 #if HAS_EEPROM
44 static void
45 _ao_config_put(void)
46 {
47         ao_storage_setup();
48         ao_storage_erase(ao_storage_config);
49         ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
50         ao_log_write_erase(0);
51         ao_storage_flush();
52 }
53
54 void
55 ao_config_put(void)
56 {
57         ao_mutex_get(&ao_config_mutex);
58         _ao_config_put();
59         ao_mutex_put(&ao_config_mutex);
60 }
61 #endif
62
63 static void
64 _ao_config_get(void)
65 {
66         if (ao_config_loaded)
67                 return;
68 #if HAS_EEPROM
69         ao_storage_setup();
70         ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
71 #endif
72         if (ao_config.major != AO_CONFIG_MAJOR) {
73                 ao_config.major = AO_CONFIG_MAJOR;
74                 ao_config.minor = 0;
75                 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
76                 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
77                 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
78                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
79         }
80         if (ao_config.minor < AO_CONFIG_MINOR) {
81                 /* Fixups for minor version 1 */
82                 if (ao_config.minor < 1)
83                         ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
84                 /* Fixups for minor version 2 */
85                 if (ao_config.minor < 2) {
86                         ao_config.accel_plus_g = 0;
87                         ao_config.accel_minus_g = 0;
88                 }
89                 /* Fixups for minor version 3 */
90                 if (ao_config.minor < 3)
91                         ao_config.radio_cal = ao_radio_cal;
92                 /* Fixups for minor version 4 */
93                 if (ao_config.minor < 4)
94                         ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
95                 /* Fixupes for minor version 5 */
96                 if (ao_config.minor < 5)
97                         ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
98                 if (ao_config.minor < 6)
99                         ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
100                 if (ao_config.minor < 7)
101                         ao_config.radio_setting = ao_config.radio_cal;
102                 ao_config.minor = AO_CONFIG_MINOR;
103                 ao_config_dirty = 1;
104         }
105         ao_config_loaded = 1;
106 }
107
108 static void
109 _ao_config_edit_start(void)
110 {
111         ao_mutex_get(&ao_config_mutex);
112         _ao_config_get();
113 }
114
115 static void
116 _ao_config_edit_finish(void)
117 {
118         ao_config_dirty = 1;
119         ao_mutex_put(&ao_config_mutex);
120 }
121
122 void
123 ao_config_get(void)
124 {
125         _ao_config_edit_start();
126         ao_mutex_put(&ao_config_mutex);
127 }
128
129 void
130 ao_config_callsign_show(void)
131 {
132         printf ("Callsign: \"%s\"\n", ao_config.callsign);
133 }
134
135 void
136 ao_config_callsign_set(void) __reentrant
137 {
138         uint8_t c;
139         static __xdata char callsign[AO_MAX_CALLSIGN + 1];
140
141         memset(callsign, '\0', sizeof callsign);
142         ao_cmd_white();
143         c = 0;
144         while (ao_cmd_lex_c != '\n') {
145                 if (c < AO_MAX_CALLSIGN)
146                         callsign[c++] = ao_cmd_lex_c;
147                 else
148                         ao_cmd_status = ao_cmd_lex_error;
149                 ao_cmd_lex();
150         }
151         if (ao_cmd_status != ao_cmd_success)
152                 return;
153         _ao_config_edit_start();
154         memcpy(&ao_config.callsign, &callsign,
155                AO_MAX_CALLSIGN + 1);
156         _ao_config_edit_finish();
157 }
158
159 void
160 ao_config_radio_channel_show(void) __reentrant
161 {
162         printf("Radio channel: %d\n",
163                ao_config.radio_channel);
164 }
165
166 void
167 ao_config_radio_channel_set(void) __reentrant
168 {
169         ao_cmd_decimal();
170         if (ao_cmd_status != ao_cmd_success)
171                 return;
172         _ao_config_edit_start();
173         ao_config.radio_channel = ao_cmd_lex_i;
174         _ao_config_edit_finish();
175         ao_radio_recv_abort();
176 }
177
178 #if HAS_ADC
179
180 void
181 ao_config_main_deploy_show(void) __reentrant
182 {
183         printf("Main deploy: %d meters\n",
184                ao_config.main_deploy);
185 }
186
187 void
188 ao_config_main_deploy_set(void) __reentrant
189 {
190         ao_cmd_decimal();
191         if (ao_cmd_status != ao_cmd_success)
192                 return;
193         _ao_config_edit_start();
194         ao_config.main_deploy = ao_cmd_lex_i;
195         _ao_config_edit_finish();
196 }
197
198 #if HAS_ACCEL
199 void
200 ao_config_accel_calibrate_show(void) __reentrant
201 {
202         printf("Accel cal +1g: %d -1g: %d\n",
203                ao_config.accel_plus_g, ao_config.accel_minus_g);
204 }
205
206 #define ACCEL_CALIBRATE_SAMPLES 1024
207 #define ACCEL_CALIBRATE_SHIFT   10
208
209 static int16_t
210 ao_config_accel_calibrate_auto(char *orientation) __reentrant
211 {
212         uint16_t        i;
213         int32_t         accel_total;
214         uint8_t         cal_adc_ring;
215
216         printf("Orient antenna %s and press a key...", orientation);
217         flush();
218         (void) getchar();
219         puts("\r\n"); flush();
220         puts("Calibrating..."); flush();
221         i = ACCEL_CALIBRATE_SAMPLES;
222         accel_total = 0;
223         cal_adc_ring = ao_sample_adc;
224         while (i) {
225                 ao_sleep(DATA_TO_XDATA(&ao_sample_adc));
226                 while (i && cal_adc_ring != ao_sample_adc) {
227                         accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
228                         cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
229                         i--;
230                 }
231         }
232         return accel_total >> ACCEL_CALIBRATE_SHIFT;
233 }
234
235 void
236 ao_config_accel_calibrate_set(void) __reentrant
237 {
238         int16_t up, down;
239         ao_cmd_decimal();
240         if (ao_cmd_status != ao_cmd_success)
241                 return;
242         if (ao_cmd_lex_i == 0) {
243                 up = ao_config_accel_calibrate_auto("up");
244                 down = ao_config_accel_calibrate_auto("down");
245         } else {
246                 up = ao_cmd_lex_i;
247                 ao_cmd_decimal();
248                 if (ao_cmd_status != ao_cmd_success)
249                         return;
250                 down = ao_cmd_lex_i;
251         }
252         if (up >= down) {
253                 printf("Invalid accel: up (%d) down (%d)\n",
254                        up, down);
255                 return;
256         }
257         _ao_config_edit_start();
258         ao_config.accel_plus_g = up;
259         ao_config.accel_minus_g = down;
260         _ao_config_edit_finish();
261 }
262 #endif /* HAS_ACCEL */
263
264 void
265 ao_config_apogee_delay_show(void) __reentrant
266 {
267         printf("Apogee delay: %d seconds\n",
268                ao_config.apogee_delay);
269 }
270
271 void
272 ao_config_apogee_delay_set(void) __reentrant
273 {
274         ao_cmd_decimal();
275         if (ao_cmd_status != ao_cmd_success)
276                 return;
277         _ao_config_edit_start();
278         ao_config.apogee_delay = ao_cmd_lex_i;
279         _ao_config_edit_finish();
280 }
281
282 #endif /* HAS_ADC */
283
284 void
285 ao_config_radio_cal_show(void) __reentrant
286 {
287         printf("Radio cal: %ld\n", ao_config.radio_cal);
288 }
289
290 void
291 ao_config_radio_cal_set(void) __reentrant
292 {
293         ao_cmd_decimal();
294         if (ao_cmd_status != ao_cmd_success)
295                 return;
296         _ao_config_edit_start();
297         ao_config.radio_setting = ao_config.radio_cal = ao_cmd_lex_u32;
298         _ao_config_edit_finish();
299 }
300
301 #if HAS_EEPROM
302 void
303 ao_config_log_show(void) __reentrant
304 {
305         printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
306 }
307
308 void
309 ao_config_log_set(void) __reentrant
310 {
311         uint16_t        block = (uint16_t) (ao_storage_block >> 10);
312         uint16_t        config = (uint16_t) (ao_storage_config >> 10);
313
314         ao_cmd_decimal();
315         if (ao_cmd_status != ao_cmd_success)
316                 return;
317         if (ao_log_present())
318                 printf("Storage must be empty before changing log size\n");
319         else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
320                 printf("Flight log size must be multiple of %d kB\n", block);
321         else if (ao_cmd_lex_i > config)
322                 printf("Flight log max %d kB\n", config);
323         else {
324                 _ao_config_edit_start();
325                 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
326                 _ao_config_edit_finish();
327         }
328 }
329 #endif /* HAS_EEPROM */
330
331 #if HAS_IGNITE
332 void
333 ao_config_ignite_mode_show(void) __reentrant
334 {
335         printf("Ignite mode: %d\n", ao_config.ignite_mode);
336 }
337
338 void
339 ao_config_ignite_mode_set(void) __reentrant
340 {
341         ao_cmd_decimal();
342         if (ao_cmd_status != ao_cmd_success)
343                 return;
344         _ao_config_edit_start();
345         ao_config.ignite_mode = ao_cmd_lex_i;
346         _ao_config_edit_finish();
347 }
348 #endif
349
350 #if HAS_ACCEL
351 void
352 ao_config_pad_orientation_show(void) __reentrant
353 {
354         printf("Pad orientation: %d\n", ao_config.pad_orientation);
355 }
356
357 void
358 ao_config_pad_orientation_set(void) __reentrant
359 {
360         ao_cmd_decimal();
361         if (ao_cmd_status != ao_cmd_success)
362                 return;
363         _ao_config_edit_start();
364         ao_cmd_lex_i &= 1;
365         if (ao_config.pad_orientation != ao_cmd_lex_i) {
366                 uint16_t t;
367                 t = ao_config.accel_plus_g;
368                 ao_config.accel_plus_g = 0x7fff - ao_config.accel_minus_g;
369                 ao_config.accel_minus_g = 0x7fff - t;
370         }
371         ao_config.pad_orientation = ao_cmd_lex_i;
372         _ao_config_edit_finish();
373 }
374 #endif
375
376 void
377 ao_config_radio_setting_show(void) __reentrant
378 {
379         printf("Radio setting: %ld\n", ao_config.radio_setting);
380 }
381
382 void
383 ao_config_radio_setting_set(void) __reentrant
384 {
385         ao_cmd_decimal();
386         if (ao_cmd_status != ao_cmd_success)
387                 return;
388         _ao_config_edit_start();
389         ao_config.radio_setting = ao_cmd_lex_u32;
390         ao_config.radio_channel = 0;
391         _ao_config_edit_finish();
392         ao_radio_recv_abort();
393 }
394
395 struct ao_config_var {
396         __code char     *str;
397         void            (*set)(void) __reentrant;
398         void            (*show)(void) __reentrant;
399 };
400
401 static void
402 ao_config_help(void) __reentrant;
403
404 static void
405 ao_config_show(void) __reentrant;
406
407 static void
408 ao_config_write(void) __reentrant;
409
410 __code struct ao_config_var ao_config_vars[] = {
411 #if HAS_ADC
412         { "m <meters>\0Main deploy (in meters)",
413           ao_config_main_deploy_set,    ao_config_main_deploy_show, },
414         { "d <delay>\0Apogee delay (in seconds)",
415           ao_config_apogee_delay_set,   ao_config_apogee_delay_show },
416 #endif /* HAS_ADC */
417         { "r <channel>\0Radio channel (freq = 434.550 + chan * .1)",
418           ao_config_radio_channel_set,  ao_config_radio_channel_show },
419         { "c <call>\0Callsign (8 char max)",
420           ao_config_callsign_set,       ao_config_callsign_show },
421 #if HAS_ACCEL
422         { "a <+g> <-g>\0Accel calib (0 for auto)",
423           ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
424 #endif /* HAS_ACCEL */
425         { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
426           ao_config_radio_cal_set,      ao_config_radio_cal_show },
427 #if HAS_EEPROM
428         { "l <size>\0Flight log size in kB",
429           ao_config_log_set,            ao_config_log_show },
430 #endif
431 #if HAS_IGNITE
432         { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
433           ao_config_ignite_mode_set,    ao_config_ignite_mode_show },
434 #endif
435 #if HAS_ACCEL
436         { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
437           ao_config_pad_orientation_set,ao_config_pad_orientation_show },
438 #endif
439         { "R <setting>\0Radio freq control (freq = 434.550 * setting/cal)",
440           ao_config_radio_setting_set,  ao_config_radio_setting_show },
441         { "s\0Show",
442           ao_config_show,               0 },
443 #if HAS_EEPROM
444         { "w\0Write to eeprom",
445           ao_config_write,              0 },
446 #endif
447         { "?\0Help",
448           ao_config_help,               0 },
449         { 0, 0, 0 }
450 };
451
452 void
453 ao_config_set(void)
454 {
455         char    c;
456         uint8_t cmd;
457         void (*__xdata func)(void) __reentrant;
458
459         ao_cmd_white();
460         c = ao_cmd_lex_c;
461         ao_cmd_lex();
462         func = 0;
463         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
464                 if (ao_config_vars[cmd].str[0] == c) {
465                         (*ao_config_vars[cmd].set)();
466                         return;
467                 }
468         ao_cmd_status = ao_cmd_syntax_error;
469 }
470
471 static void
472 ao_config_help(void) __reentrant
473 {
474         uint8_t cmd;
475         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
476                 printf("%-20s %s\n",
477                        ao_config_vars[cmd].str,
478                        ao_config_vars[cmd].str+1+strlen(ao_config_vars[cmd].str));
479 }
480
481 static void
482 ao_config_show(void) __reentrant
483 {
484         uint8_t cmd;
485         printf("Config version: %d.%d\n",
486                ao_config.major, ao_config.minor);
487         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
488                 if (ao_config_vars[cmd].show)
489                         (*ao_config_vars[cmd].show)();
490 }
491
492 #if HAS_EEPROM
493 static void
494 ao_config_write(void) __reentrant
495 {
496         uint8_t saved = 0;
497         ao_mutex_get(&ao_config_mutex);
498         if (ao_config_dirty) {
499                 _ao_config_put();
500                 ao_config_dirty = 0;
501                 saved = 1;
502         }
503         ao_mutex_put(&ao_config_mutex);
504         if (saved)
505                 puts("Saved");
506         else
507                 puts("Nothing to save");
508 }
509 #endif
510
511 __code struct ao_cmds ao_config_cmds[] = {
512         { ao_config_set,        "c <var> <value>\0Set config variable (? for help, s to show)" },
513         { 0, NULL },
514 };
515
516 void
517 ao_config_init(void)
518 {
519         ao_cmd_register(&ao_config_cmds[0]);
520 }