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