921a248065a25d130e27c10799e2dc3dc9c3c41e
[fw/altos] / src / core / 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 #ifndef AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX
38 #if USE_INTERNAL_FLASH
39 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ao_storage_config
40 #else
41 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ((uint32_t) 192 * (uint32_t) 1024)
42 #endif
43 #endif
44
45 #if HAS_EEPROM
46 static void
47 _ao_config_put(void)
48 {
49         ao_storage_setup();
50         ao_storage_erase(ao_storage_config);
51         ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
52 #if HAS_FLIGHT
53         ao_log_write_erase(0);
54 #endif
55         ao_storage_flush();
56 }
57
58 void
59 ao_config_put(void)
60 {
61         ao_mutex_get(&ao_config_mutex);
62         _ao_config_put();
63         ao_mutex_put(&ao_config_mutex);
64 }
65 #endif
66
67 static void
68 _ao_config_get(void)
69 {
70         if (ao_config_loaded)
71                 return;
72 #if HAS_EEPROM
73         ao_storage_setup();
74         ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
75 #endif
76         if (ao_config.major != AO_CONFIG_MAJOR) {
77                 ao_config.major = AO_CONFIG_MAJOR;
78                 ao_config.minor = 0;
79
80                 /* Version 0 stuff */
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);
86                 ao_config_dirty = 1;
87         }
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;
96                 }
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;
115                 ao_config_dirty = 1;
116         }
117         ao_config_loaded = 1;
118 }
119
120 static void
121 _ao_config_edit_start(void)
122 {
123         ao_mutex_get(&ao_config_mutex);
124         _ao_config_get();
125 }
126
127 static void
128 _ao_config_edit_finish(void)
129 {
130         ao_config_dirty = 1;
131         ao_mutex_put(&ao_config_mutex);
132 }
133
134 void
135 ao_config_get(void)
136 {
137         _ao_config_edit_start();
138         ao_mutex_put(&ao_config_mutex);
139 }
140
141 void
142 ao_config_callsign_show(void)
143 {
144         printf ("Callsign: \"%s\"\n", ao_config.callsign);
145 }
146
147 void
148 ao_config_callsign_set(void) __reentrant
149 {
150         uint8_t c;
151         static __xdata char callsign[AO_MAX_CALLSIGN + 1];
152
153         ao_xmemset(callsign, '\0', sizeof callsign);
154         ao_cmd_white();
155         c = 0;
156         while (ao_cmd_lex_c != '\n') {
157                 if (c < AO_MAX_CALLSIGN)
158                         callsign[c++] = ao_cmd_lex_c;
159                 else
160                         ao_cmd_status = ao_cmd_lex_error;
161                 ao_cmd_lex();
162         }
163         if (ao_cmd_status != ao_cmd_success)
164                 return;
165         _ao_config_edit_start();
166         ao_xmemcpy(&ao_config.callsign, &callsign,
167                AO_MAX_CALLSIGN + 1);
168         _ao_config_edit_finish();
169 }
170
171 void
172 ao_config_radio_channel_show(void) __reentrant
173 {
174         printf("Radio channel: %d\n",
175                ao_config.radio_channel);
176 }
177
178 void
179 ao_config_radio_channel_set(void) __reentrant
180 {
181         ao_cmd_decimal();
182         if (ao_cmd_status != ao_cmd_success)
183                 return;
184         _ao_config_edit_start();
185         ao_config.radio_channel = ao_cmd_lex_i;
186         _ao_config_edit_finish();
187         ao_radio_recv_abort();
188 }
189
190 #if HAS_ADC
191
192 void
193 ao_config_main_deploy_show(void) __reentrant
194 {
195         printf("Main deploy: %d meters\n",
196                ao_config.main_deploy);
197 }
198
199 void
200 ao_config_main_deploy_set(void) __reentrant
201 {
202         ao_cmd_decimal();
203         if (ao_cmd_status != ao_cmd_success)
204                 return;
205         _ao_config_edit_start();
206         ao_config.main_deploy = ao_cmd_lex_i;
207         _ao_config_edit_finish();
208 }
209
210 #if HAS_ACCEL
211 void
212 ao_config_accel_calibrate_show(void) __reentrant
213 {
214         printf("Accel cal +1g: %d -1g: %d\n",
215                ao_config.accel_plus_g, ao_config.accel_minus_g);
216 }
217
218 #define ACCEL_CALIBRATE_SAMPLES 1024
219 #define ACCEL_CALIBRATE_SHIFT   10
220
221 static int16_t
222 ao_config_accel_calibrate_auto(char *orientation) __reentrant
223 {
224         uint16_t        i;
225         int32_t         accel_total;
226         uint8_t         cal_adc_ring;
227
228         printf("Orient antenna %s and press a key...", orientation);
229         flush();
230         (void) getchar();
231         puts("\r\n"); flush();
232         puts("Calibrating..."); flush();
233         i = ACCEL_CALIBRATE_SAMPLES;
234         accel_total = 0;
235         cal_adc_ring = ao_sample_adc;
236         while (i) {
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);
241                         i--;
242                 }
243         }
244         return accel_total >> ACCEL_CALIBRATE_SHIFT;
245 }
246
247 void
248 ao_config_accel_calibrate_set(void) __reentrant
249 {
250         int16_t up, down;
251         ao_cmd_decimal();
252         if (ao_cmd_status != ao_cmd_success)
253                 return;
254         if (ao_cmd_lex_i == 0) {
255                 up = ao_config_accel_calibrate_auto("up");
256                 down = ao_config_accel_calibrate_auto("down");
257         } else {
258                 up = ao_cmd_lex_i;
259                 ao_cmd_decimal();
260                 if (ao_cmd_status != ao_cmd_success)
261                         return;
262                 down = ao_cmd_lex_i;
263         }
264         if (up >= down) {
265                 printf("Invalid accel: up (%d) down (%d)\n",
266                        up, down);
267                 return;
268         }
269         _ao_config_edit_start();
270         ao_config.accel_plus_g = up;
271         ao_config.accel_minus_g = down;
272         _ao_config_edit_finish();
273 }
274 #endif /* HAS_ACCEL */
275
276 void
277 ao_config_apogee_delay_show(void) __reentrant
278 {
279         printf("Apogee delay: %d seconds\n",
280                ao_config.apogee_delay);
281 }
282
283 void
284 ao_config_apogee_delay_set(void) __reentrant
285 {
286         ao_cmd_decimal();
287         if (ao_cmd_status != ao_cmd_success)
288                 return;
289         _ao_config_edit_start();
290         ao_config.apogee_delay = ao_cmd_lex_i;
291         _ao_config_edit_finish();
292 }
293
294 #endif /* HAS_ADC */
295
296 void
297 ao_config_radio_cal_show(void) __reentrant
298 {
299         printf("Radio cal: %ld\n", ao_config.radio_cal);
300 }
301
302 void
303 ao_config_radio_cal_set(void) __reentrant
304 {
305         ao_cmd_decimal();
306         if (ao_cmd_status != ao_cmd_success)
307                 return;
308         _ao_config_edit_start();
309         ao_config.radio_setting = ao_config.radio_cal = ao_cmd_lex_u32;
310         _ao_config_edit_finish();
311 }
312
313 #if HAS_LOG
314 void
315 ao_config_log_show(void) __reentrant
316 {
317         printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
318 }
319
320 void
321 ao_config_log_set(void) __reentrant
322 {
323         uint16_t        block = (uint16_t) (ao_storage_block >> 10);
324         uint16_t        config = (uint16_t) (ao_storage_config >> 10);
325
326         ao_cmd_decimal();
327         if (ao_cmd_status != ao_cmd_success)
328                 return;
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);
335         else {
336                 _ao_config_edit_start();
337                 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
338                 _ao_config_edit_finish();
339         }
340 }
341 #endif /* HAS_LOG */
342
343 #if HAS_IGNITE
344 void
345 ao_config_ignite_mode_show(void) __reentrant
346 {
347         printf("Ignite mode: %d\n", ao_config.ignite_mode);
348 }
349
350 void
351 ao_config_ignite_mode_set(void) __reentrant
352 {
353         ao_cmd_decimal();
354         if (ao_cmd_status != ao_cmd_success)
355                 return;
356         _ao_config_edit_start();
357         ao_config.ignite_mode = ao_cmd_lex_i;
358         _ao_config_edit_finish();
359 }
360 #endif
361
362 #if HAS_ACCEL
363 void
364 ao_config_pad_orientation_show(void) __reentrant
365 {
366         printf("Pad orientation: %d\n", ao_config.pad_orientation);
367 }
368
369 void
370 ao_config_pad_orientation_set(void) __reentrant
371 {
372         ao_cmd_decimal();
373         if (ao_cmd_status != ao_cmd_success)
374                 return;
375         _ao_config_edit_start();
376         ao_cmd_lex_i &= 1;
377         if (ao_config.pad_orientation != ao_cmd_lex_i) {
378                 uint16_t t;
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;
382         }
383         ao_config.pad_orientation = ao_cmd_lex_i;
384         _ao_config_edit_finish();
385 }
386 #endif
387
388 void
389 ao_config_radio_setting_show(void) __reentrant
390 {
391         printf("Radio setting: %ld\n", ao_config.radio_setting);
392 }
393
394 void
395 ao_config_radio_setting_set(void) __reentrant
396 {
397         ao_cmd_decimal();
398         if (ao_cmd_status != ao_cmd_success)
399                 return;
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();
405 }
406
407 void
408 ao_config_radio_enable_show(void) __reentrant
409 {
410         printf("Radio enable: %d\n", ao_config.radio_enable);
411 }
412
413 void
414 ao_config_radio_enable_set(void) __reentrant
415 {
416         ao_cmd_decimal();
417         if (ao_cmd_status != ao_cmd_success)
418                 return;
419         _ao_config_edit_start();
420         ao_config.radio_enable = ao_cmd_lex_i;
421         _ao_config_edit_finish();
422 }
423         
424 #if HAS_AES
425 void
426 ao_config_key_show(void) __reentrant
427 {
428         uint8_t i;
429         printf("AES key: ");
430         for (i = 0; i < AO_AES_LEN; i++)
431                 printf ("%02x", ao_config.aes_key[i]);
432         printf("\n");
433 }
434
435 void
436 ao_config_key_set(void) __reentrant
437 {
438         uint8_t i;
439
440         _ao_config_edit_start();
441         for (i = 0; i < AO_AES_LEN; i++) {
442                 ao_cmd_hexbyte();
443                 if (ao_cmd_status != ao_cmd_success)
444                         break;
445                 ao_config.aes_key[i] = ao_cmd_lex_i;
446         }
447         _ao_config_edit_finish();
448 }
449 #endif
450
451 struct ao_config_var {
452         __code char     *str;
453         void            (*set)(void) __reentrant;
454         void            (*show)(void) __reentrant;
455 };
456
457 static void
458 ao_config_help(void) __reentrant;
459
460 static void
461 ao_config_show(void) __reentrant;
462
463 static void
464 ao_config_write(void) __reentrant;
465
466 __code struct ao_config_var ao_config_vars[] = {
467 #if HAS_ADC
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 },
472 #endif /* HAS_ADC */
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 },
481 #if HAS_ACCEL
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 },
487 #if HAS_LOG
488         { "l <size>\0Flight log size in kB",
489           ao_config_log_set,            ao_config_log_show },
490 #endif
491 #if HAS_IGNITE
492         { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
493           ao_config_ignite_mode_set,    ao_config_ignite_mode_show },
494 #endif
495 #if HAS_ACCEL
496         { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
497           ao_config_pad_orientation_set,ao_config_pad_orientation_show },
498 #endif
499 #if HAS_AES
500         { "k <32 hex digits>\0Set AES encryption key",
501           ao_config_key_set, ao_config_key_show },
502 #endif
503         { "s\0Show",
504           ao_config_show,               0 },
505 #if HAS_EEPROM
506         { "w\0Write to eeprom",
507           ao_config_write,              0 },
508 #endif
509         { "?\0Help",
510           ao_config_help,               0 },
511         { 0, 0, 0 }
512 };
513
514 void
515 ao_config_set(void)
516 {
517         char    c;
518         uint8_t cmd;
519         void (*__xdata func)(void) __reentrant;
520
521         ao_cmd_white();
522         c = ao_cmd_lex_c;
523         ao_cmd_lex();
524         func = 0;
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)();
528                         return;
529                 }
530         ao_cmd_status = ao_cmd_syntax_error;
531 }
532
533 static void
534 ao_config_help(void) __reentrant
535 {
536         uint8_t cmd;
537         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
538                 printf("%-20s %s\n",
539                        ao_config_vars[cmd].str,
540                        ao_config_vars[cmd].str+1+
541                        strlen(ao_config_vars[cmd].str));
542 }
543
544 static void
545 ao_config_show(void) __reentrant
546 {
547         uint8_t cmd;
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)();
553 }
554
555 #if HAS_EEPROM
556 static void
557 ao_config_write(void) __reentrant
558 {
559         uint8_t saved = 0;
560         ao_mutex_get(&ao_config_mutex);
561         if (ao_config_dirty) {
562                 _ao_config_put();
563                 ao_config_dirty = 0;
564                 saved = 1;
565         }
566         ao_mutex_put(&ao_config_mutex);
567         if (saved)
568                 puts("Saved");
569         else
570                 puts("Nothing to save");
571 }
572 #endif
573
574 __code struct ao_cmds ao_config_cmds[] = {
575         { ao_config_set,        "c <var> <value>\0Set config variable (? for help, s to show)" },
576         { 0, NULL },
577 };
578
579 void
580 ao_config_init(void)
581 {
582         ao_cmd_register(&ao_config_cmds[0]);
583 }