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