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