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