altos: Force 434.550Mhz by connecting debug gnd and clk (trac #41)
[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 #include "ao_log.h"
20 #include <ao_sample.h>
21 #include <ao_data.h>
22
23 __xdata struct ao_config ao_config;
24 __pdata uint8_t ao_config_loaded;
25 __pdata uint8_t ao_config_dirty;
26 __xdata uint8_t ao_config_mutex;
27
28 #define AO_CONFIG_DEFAULT_MAIN_DEPLOY   250
29 #define AO_CONFIG_DEFAULT_RADIO_CHANNEL 0
30 #define AO_CONFIG_DEFAULT_CALLSIGN      "N0CALL"
31 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G  16000
32 #define AO_CONFIG_DEFAULT_APOGEE_DELAY  0
33 #define AO_CONFIG_DEFAULT_IGNITE_MODE   AO_IGNITE_MODE_DUAL
34 #define AO_CONFIG_DEFAULT_PAD_ORIENTATION       AO_PAD_ORIENTATION_ANTENNA_UP
35 #if HAS_EEPROM
36 #ifndef USE_INTERNAL_FLASH
37 #error Please define USE_INTERNAL_FLASH
38 #endif
39 #endif
40 #ifndef AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX
41 #if USE_INTERNAL_FLASH
42 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ao_storage_config
43 #else
44 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ((uint32_t) 192 * (uint32_t) 1024)
45 #endif
46 #endif
47
48 #if HAS_EEPROM
49 static void
50 _ao_config_put(void)
51 {
52         ao_storage_setup();
53         ao_storage_erase(ao_storage_config);
54         ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
55 #if HAS_FLIGHT
56         ao_log_write_erase(0);
57 #endif
58         ao_storage_flush();
59 }
60
61 void
62 ao_config_put(void)
63 {
64         ao_mutex_get(&ao_config_mutex);
65         _ao_config_put();
66         ao_mutex_put(&ao_config_mutex);
67 }
68 #endif
69
70 #if HAS_RADIO
71 void
72 ao_config_set_radio(void)
73 {
74         ao_config.radio_setting = ao_freq_to_set(ao_config.frequency, ao_config.radio_cal);
75 }
76 #endif /* HAS_RADIO */
77
78 static void
79 _ao_config_get(void)
80 {
81         uint8_t minor;
82
83         if (ao_config_loaded)
84                 return;
85 #if HAS_EEPROM
86         /* Yes, I know ao_storage_read calls ao_storage_setup,
87          * but ao_storage_setup *also* sets ao_storage_config, which we
88          * need before calling ao_storage_read here
89          */
90         ao_storage_setup();
91         ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
92 #endif
93         if (ao_config.major != AO_CONFIG_MAJOR) {
94                 ao_config.major = AO_CONFIG_MAJOR;
95                 ao_config.minor = 0;
96
97                 /* Version 0 stuff */
98                 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
99                 ao_xmemset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
100                 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
101                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
102         }
103         minor = ao_config.minor;
104         if (minor != AO_CONFIG_MINOR) {
105                 /* Fixups for minor version 1 */
106                 if (minor < 1)
107                         ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
108                 /* Fixups for minor version 2 */
109                 if (minor < 2) {
110                         ao_config.accel_plus_g = 0;
111                         ao_config.accel_minus_g = 0;
112                 }
113                 /* Fixups for minor version 3 */
114 #if HAS_RADIO
115                 if (minor < 3)
116                         ao_config.radio_cal = ao_radio_cal;
117 #endif
118                 /* Fixups for minor version 4 */
119                 if (minor < 4)
120                         ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
121                 /* Fixupes for minor version 5 */
122                 if (minor < 5)
123                         ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
124                 if (minor < 6)
125                         ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
126                 if (minor < 8)
127                         ao_config.radio_enable = TRUE;
128                 if (minor < 9)
129                         ao_xmemset(&ao_config.aes_key, '\0', AO_AES_LEN);
130                 if (minor < 10)
131                         ao_config.frequency = 434550;
132                 if (minor < 11)
133                         ao_config.apogee_lockout = 0;
134                 ao_config.minor = AO_CONFIG_MINOR;
135                 ao_config_dirty = 1;
136         }
137 #if HAS_RADIO
138 #if HAS_FORCE_FREQ
139         if (ao_force_freq)
140                 ao_config.frequency = 434550;
141 #endif
142         ao_config_set_radio();
143 #endif
144         ao_config_loaded = 1;
145 }
146
147 static void
148 _ao_config_edit_start(void)
149 {
150         ao_mutex_get(&ao_config_mutex);
151         _ao_config_get();
152 }
153
154 static void
155 _ao_config_edit_finish(void)
156 {
157         ao_config_dirty = 1;
158         ao_mutex_put(&ao_config_mutex);
159 }
160
161 void
162 ao_config_get(void)
163 {
164         _ao_config_edit_start();
165         ao_mutex_put(&ao_config_mutex);
166 }
167
168 void
169 ao_config_callsign_show(void)
170 {
171         printf ("Callsign: \"%s\"\n", ao_config.callsign);
172 }
173
174 void
175 ao_config_callsign_set(void) __reentrant
176 {
177         uint8_t c;
178         static __xdata char callsign[AO_MAX_CALLSIGN + 1];
179
180         ao_xmemset(callsign, '\0', sizeof callsign);
181         ao_cmd_white();
182         c = 0;
183         while (ao_cmd_lex_c != '\n') {
184                 if (c < AO_MAX_CALLSIGN)
185                         callsign[c++] = ao_cmd_lex_c;
186                 else
187                         ao_cmd_status = ao_cmd_lex_error;
188                 ao_cmd_lex();
189         }
190         if (ao_cmd_status != ao_cmd_success)
191                 return;
192         _ao_config_edit_start();
193         ao_xmemcpy(&ao_config.callsign, &callsign,
194                AO_MAX_CALLSIGN + 1);
195         _ao_config_edit_finish();
196 }
197
198 #if HAS_RADIO
199 void
200 ao_config_frequency_show(void) __reentrant
201 {
202         printf("Frequency: %ld\n",
203                ao_config.frequency);
204 }
205
206 void
207 ao_config_frequency_set(void) __reentrant
208 {
209         ao_cmd_decimal();
210         if (ao_cmd_status != ao_cmd_success)
211                 return;
212         _ao_config_edit_start();
213         ao_config.frequency = ao_cmd_lex_u32;
214         ao_config_set_radio();
215         _ao_config_edit_finish();
216         ao_radio_recv_abort();
217 }
218 #endif
219
220 #if HAS_FLIGHT
221
222 void
223 ao_config_main_deploy_show(void) __reentrant
224 {
225         printf("Main deploy: %d meters\n",
226                ao_config.main_deploy);
227 }
228
229 void
230 ao_config_main_deploy_set(void) __reentrant
231 {
232         ao_cmd_decimal();
233         if (ao_cmd_status != ao_cmd_success)
234                 return;
235         _ao_config_edit_start();
236         ao_config.main_deploy = ao_cmd_lex_i;
237         _ao_config_edit_finish();
238 }
239
240 #if HAS_ACCEL
241 void
242 ao_config_accel_calibrate_show(void) __reentrant
243 {
244         printf("Accel cal +1g: %d -1g: %d\n",
245                ao_config.accel_plus_g, ao_config.accel_minus_g);
246 }
247
248 #define ACCEL_CALIBRATE_SAMPLES 1024
249 #define ACCEL_CALIBRATE_SHIFT   10
250
251 static int16_t
252 ao_config_accel_calibrate_auto(char *orientation) __reentrant
253 {
254         uint16_t        i;
255         int32_t         accel_total;
256         uint8_t         cal_data_ring;
257
258         printf("Orient antenna %s and press a key...", orientation);
259         flush();
260         (void) getchar();
261         puts("\r\n"); flush();
262         puts("Calibrating..."); flush();
263         i = ACCEL_CALIBRATE_SAMPLES;
264         accel_total = 0;
265         cal_data_ring = ao_sample_data;
266         while (i) {
267                 ao_sleep(DATA_TO_XDATA(&ao_sample_data));
268                 while (i && cal_data_ring != ao_sample_data) {
269                         accel_total += (int32_t) ao_data_accel(&ao_data_ring[cal_data_ring]);
270                         cal_data_ring = ao_data_ring_next(cal_data_ring);
271                         i--;
272                 }
273         }
274         return accel_total >> ACCEL_CALIBRATE_SHIFT;
275 }
276
277 void
278 ao_config_accel_calibrate_set(void) __reentrant
279 {
280         int16_t up, down;
281         ao_cmd_decimal();
282         if (ao_cmd_status != ao_cmd_success)
283                 return;
284         if (ao_cmd_lex_i == 0) {
285                 up = ao_config_accel_calibrate_auto("up");
286                 down = ao_config_accel_calibrate_auto("down");
287         } else {
288                 up = ao_cmd_lex_i;
289                 ao_cmd_decimal();
290                 if (ao_cmd_status != ao_cmd_success)
291                         return;
292                 down = ao_cmd_lex_i;
293         }
294         if (up >= down) {
295                 printf("Invalid accel: up (%d) down (%d)\n",
296                        up, down);
297                 return;
298         }
299         _ao_config_edit_start();
300         ao_config.accel_plus_g = up;
301         ao_config.accel_minus_g = down;
302         _ao_config_edit_finish();
303 }
304 #endif /* HAS_ACCEL */
305
306 void
307 ao_config_apogee_delay_show(void) __reentrant
308 {
309         printf("Apogee delay: %d seconds\n",
310                ao_config.apogee_delay);
311 }
312
313 void
314 ao_config_apogee_delay_set(void) __reentrant
315 {
316         ao_cmd_decimal();
317         if (ao_cmd_status != ao_cmd_success)
318                 return;
319         _ao_config_edit_start();
320         ao_config.apogee_delay = ao_cmd_lex_i;
321         _ao_config_edit_finish();
322 }
323
324 void
325 ao_config_apogee_lockout_show(void) __reentrant
326 {
327         printf ("Apogee lockout: %d seconds\n",
328                 ao_config.apogee_lockout);
329 }
330
331 void
332 ao_config_apogee_lockout_set(void) __reentrant
333 {
334         ao_cmd_decimal();
335         if (ao_cmd_status != ao_cmd_success)
336                 return;
337         _ao_config_edit_start();
338         ao_config.apogee_lockout = ao_cmd_lex_i;
339         _ao_config_edit_finish();
340 }
341
342 #endif /* HAS_FLIGHT */
343
344 #if HAS_RADIO
345 void
346 ao_config_radio_cal_show(void) __reentrant
347 {
348         printf("Radio cal: %ld\n", ao_config.radio_cal);
349 }
350
351 void
352 ao_config_radio_cal_set(void) __reentrant
353 {
354         ao_cmd_decimal();
355         if (ao_cmd_status != ao_cmd_success)
356                 return;
357         _ao_config_edit_start();
358         ao_config.radio_cal = ao_cmd_lex_u32;
359         ao_config_set_radio();
360         _ao_config_edit_finish();
361 }
362 #endif
363
364 #if HAS_LOG
365 void
366 ao_config_log_show(void) __reentrant
367 {
368         printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
369 }
370
371 void
372 ao_config_log_set(void) __reentrant
373 {
374         uint16_t        block = (uint16_t) (ao_storage_block >> 10);
375         uint16_t        config = (uint16_t) (ao_storage_config >> 10);
376
377         ao_cmd_decimal();
378         if (ao_cmd_status != ao_cmd_success)
379                 return;
380         if (ao_log_present())
381                 printf("Storage must be empty before changing log size\n");
382         else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
383                 printf("Flight log size must be multiple of %d kB\n", block);
384         else if (ao_cmd_lex_i > config)
385                 printf("Flight log max %d kB\n", config);
386         else {
387                 _ao_config_edit_start();
388                 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
389                 _ao_config_edit_finish();
390         }
391 }
392 #endif /* HAS_LOG */
393
394 #if HAS_IGNITE
395 void
396 ao_config_ignite_mode_show(void) __reentrant
397 {
398         printf("Ignite mode: %d\n", ao_config.ignite_mode);
399 }
400
401 void
402 ao_config_ignite_mode_set(void) __reentrant
403 {
404         ao_cmd_decimal();
405         if (ao_cmd_status != ao_cmd_success)
406                 return;
407         _ao_config_edit_start();
408         ao_config.ignite_mode = ao_cmd_lex_i;
409         _ao_config_edit_finish();
410 }
411 #endif
412
413 #if HAS_ACCEL
414 void
415 ao_config_pad_orientation_show(void) __reentrant
416 {
417         printf("Pad orientation: %d\n", ao_config.pad_orientation);
418 }
419
420 void
421 ao_config_pad_orientation_set(void) __reentrant
422 {
423         ao_cmd_decimal();
424         if (ao_cmd_status != ao_cmd_success)
425                 return;
426         _ao_config_edit_start();
427         ao_cmd_lex_i &= 1;
428         if (ao_config.pad_orientation != ao_cmd_lex_i) {
429                 uint16_t t;
430                 t = ao_config.accel_plus_g;
431                 ao_config.accel_plus_g = 0x7fff - ao_config.accel_minus_g;
432                 ao_config.accel_minus_g = 0x7fff - t;
433         }
434         ao_config.pad_orientation = ao_cmd_lex_i;
435         _ao_config_edit_finish();
436 }
437 #endif
438
439 #if HAS_RADIO
440 void
441 ao_config_radio_enable_show(void) __reentrant
442 {
443         printf("Radio enable: %d\n", ao_config.radio_enable);
444 }
445
446 void
447 ao_config_radio_enable_set(void) __reentrant
448 {
449         ao_cmd_decimal();
450         if (ao_cmd_status != ao_cmd_success)
451                 return;
452         _ao_config_edit_start();
453         ao_config.radio_enable = ao_cmd_lex_i;
454         _ao_config_edit_finish();
455 }
456 #endif /* HAS_RADIO */
457         
458 #if HAS_AES
459 void
460 ao_config_key_show(void) __reentrant
461 {
462         uint8_t i;
463         printf("AES key: ");
464         for (i = 0; i < AO_AES_LEN; i++)
465                 printf ("%02x", ao_config.aes_key[i]);
466         printf("\n");
467 }
468
469 void
470 ao_config_key_set(void) __reentrant
471 {
472         uint8_t i;
473
474         _ao_config_edit_start();
475         for (i = 0; i < AO_AES_LEN; i++) {
476                 ao_cmd_hexbyte();
477                 if (ao_cmd_status != ao_cmd_success)
478                         break;
479                 ao_config.aes_key[i] = ao_cmd_lex_i;
480         }
481         _ao_config_edit_finish();
482 }
483 #endif
484
485 struct ao_config_var {
486         __code char     *str;
487         void            (*set)(void) __reentrant;
488         void            (*show)(void) __reentrant;
489 };
490
491 static void
492 ao_config_help(void) __reentrant;
493
494 static void
495 ao_config_show(void) __reentrant;
496
497 static void
498 ao_config_write(void) __reentrant;
499
500 __code struct ao_config_var ao_config_vars[] = {
501 #if HAS_FLIGHT
502         { "m <meters>\0Main deploy (m)",
503           ao_config_main_deploy_set,    ao_config_main_deploy_show, },
504         { "d <delay>\0Apogee delay (s)",
505           ao_config_apogee_delay_set,   ao_config_apogee_delay_show },
506         { "L <seconds>\0Apogee detect lockout (s)",
507           ao_config_apogee_lockout_set, ao_config_apogee_lockout_show, },
508 #endif /* HAS_FLIGHT */
509 #if HAS_RADIO
510         { "F <freq>\0Frequency (kHz)",
511           ao_config_frequency_set, ao_config_frequency_show },
512         { "c <call>\0Callsign (8 char max)",
513           ao_config_callsign_set,       ao_config_callsign_show },
514         { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
515           ao_config_radio_enable_set, ao_config_radio_enable_show },
516 #endif /* HAS_RADIO */
517 #if HAS_ACCEL
518         { "a <+g> <-g>\0Accel calib (0 for auto)",
519           ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
520 #endif /* HAS_ACCEL */
521 #if HAS_RADIO
522         { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
523           ao_config_radio_cal_set,      ao_config_radio_cal_show },
524 #endif /* HAS_RADIO */
525 #if HAS_LOG
526         { "l <size>\0Flight log size (kB)",
527           ao_config_log_set,            ao_config_log_show },
528 #endif
529 #if HAS_IGNITE
530         { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
531           ao_config_ignite_mode_set,    ao_config_ignite_mode_show },
532 #endif
533 #if HAS_ACCEL
534         { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
535           ao_config_pad_orientation_set,ao_config_pad_orientation_show },
536 #endif
537 #if HAS_AES
538         { "k <32 hex digits>\0Set AES encryption key",
539           ao_config_key_set, ao_config_key_show },
540 #endif
541         { "s\0Show",
542           ao_config_show,               0 },
543 #if HAS_EEPROM
544         { "w\0Write to eeprom",
545           ao_config_write,              0 },
546 #endif
547         { "?\0Help",
548           ao_config_help,               0 },
549         { 0, 0, 0 }
550 };
551
552 void
553 ao_config_set(void)
554 {
555         char    c;
556         uint8_t cmd;
557         void (*__xdata func)(void) __reentrant;
558
559         ao_cmd_white();
560         c = ao_cmd_lex_c;
561         ao_cmd_lex();
562         func = 0;
563         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
564                 if (ao_config_vars[cmd].str[0] == c) {
565                         (*ao_config_vars[cmd].set)();
566                         return;
567                 }
568         ao_cmd_status = ao_cmd_syntax_error;
569 }
570
571 static void
572 ao_config_help(void) __reentrant
573 {
574         uint8_t cmd;
575         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
576                 printf("%-20s %s\n",
577                        ao_config_vars[cmd].str,
578                        ao_config_vars[cmd].str+1+
579                        strlen(ao_config_vars[cmd].str));
580 }
581
582 static void
583 ao_config_show(void) __reentrant
584 {
585         uint8_t cmd;
586         ao_config_get();
587         printf("Config version: %d.%d\n",
588                ao_config.major, ao_config.minor);
589         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
590                 if (ao_config_vars[cmd].show)
591                         (*ao_config_vars[cmd].show)();
592 }
593
594 #if HAS_EEPROM
595 static void
596 ao_config_write(void) __reentrant
597 {
598         uint8_t saved = 0;
599         ao_mutex_get(&ao_config_mutex);
600         if (ao_config_dirty) {
601                 _ao_config_put();
602                 ao_config_dirty = 0;
603                 saved = 1;
604         }
605         ao_mutex_put(&ao_config_mutex);
606         if (saved)
607                 puts("Saved");
608         else
609                 puts("Nothing to save");
610 }
611 #endif
612
613 __code struct ao_cmds ao_config_cmds[] = {
614         { ao_config_set,        "c <var> <value>\0Set config (? for help, s to show)" },
615         { 0, NULL },
616 };
617
618 void
619 ao_config_init(void)
620 {
621         ao_cmd_register(&ao_config_cmds[0]);
622 }