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