altos: Move common storage code to ao_storage.c. Add M25P80 driver
[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 __xdata uint8_t ao_config_loaded;
22 __xdata 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
31 #if HAS_EEPROM
32 static void
33 _ao_config_put(void)
34 {
35         ao_storage_setup();
36         ao_storage_erase(ao_storage_config);
37         ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
38         ao_log_write_erase(0);
39         ao_storage_flush();
40 }
41
42 void
43 ao_config_put(void)
44 {
45         ao_mutex_get(&ao_config_mutex);
46         _ao_config_put();
47         ao_mutex_put(&ao_config_mutex);
48 }
49 #endif
50
51 static void
52 _ao_config_get(void)
53 {
54         if (ao_config_loaded)
55                 return;
56 #if HAS_EEPROM
57         ao_storage_setup();
58         ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
59 #endif
60         if (ao_config.major != AO_CONFIG_MAJOR) {
61                 ao_config.major = AO_CONFIG_MAJOR;
62                 ao_config.minor = AO_CONFIG_MINOR;
63                 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
64                 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
65                 ao_config.accel_plus_g = 0;
66                 ao_config.accel_minus_g = 0;
67                 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
68                 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
69                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
70                 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
71                 ao_config.radio_cal = ao_radio_cal;
72                 ao_config_dirty = 1;
73         }
74         if (ao_config.minor < AO_CONFIG_MINOR) {
75                 /* Fixups for minor version 1 */
76                 if (ao_config.minor < 1)
77                         ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
78                 /* Fixups for minor version 2 */
79                 if (ao_config.minor < 2) {
80                         ao_config.accel_plus_g = 0;
81                         ao_config.accel_minus_g = 0;
82                 }
83                 /* Fixups for minor version 3 */
84                 if (ao_config.minor < 3)
85                         ao_config.radio_cal = ao_radio_cal;
86                 ao_config.minor = AO_CONFIG_MINOR;
87                 ao_config_dirty = 1;
88         }
89         ao_config_loaded = 1;
90 }
91
92 void
93 ao_config_get(void)
94 {
95         ao_mutex_get(&ao_config_mutex);
96         _ao_config_get();
97         ao_mutex_put(&ao_config_mutex);
98 }
99
100 void
101 ao_config_callsign_show(void)
102 {
103         printf ("Callsign: \"%s\"\n", ao_config.callsign);
104 }
105
106 void
107 ao_config_callsign_set(void) __reentrant
108 {
109         uint8_t c;
110         char callsign[AO_MAX_CALLSIGN + 1];
111
112         ao_cmd_white();
113         c = 0;
114         while (ao_cmd_lex_c != '\n') {
115                 if (c < AO_MAX_CALLSIGN)
116                         callsign[c++] = ao_cmd_lex_c;
117                 else
118                         ao_cmd_status = ao_cmd_lex_error;
119                 ao_cmd_lex();
120         }
121         if (ao_cmd_status != ao_cmd_success)
122                 return;
123         ao_mutex_get(&ao_config_mutex);
124         _ao_config_get();
125         while (c < AO_MAX_CALLSIGN + 1)
126                 callsign[c++] = '\0';
127         memcpy(&ao_config.callsign, &callsign,
128                AO_MAX_CALLSIGN + 1);
129         ao_config_dirty = 1;
130         ao_mutex_put(&ao_config_mutex);
131         ao_config_callsign_show();
132 }
133
134 void
135 ao_config_radio_channel_show(void) __reentrant
136 {
137         uint32_t        freq = 434550L + ao_config.radio_channel * 100L;
138         uint16_t        mhz = freq / 1000L;
139         uint16_t        khz = freq % 1000L;
140
141         printf("Radio channel: %d (%d.%03dMHz)\n",
142                ao_config.radio_channel, mhz, khz);
143 }
144
145 void
146 ao_config_radio_channel_set(void) __reentrant
147 {
148         ao_cmd_decimal();
149         if (ao_cmd_status != ao_cmd_success)
150                 return;
151         ao_mutex_get(&ao_config_mutex);
152         _ao_config_get();
153         ao_config.radio_channel = ao_cmd_lex_i;
154         ao_config_dirty = 1;
155         ao_mutex_put(&ao_config_mutex);
156         ao_config_radio_channel_show();
157         ao_radio_recv_abort();
158 }
159
160 #if HAS_ADC
161
162 void
163 ao_config_main_deploy_show(void) __reentrant
164 {
165         printf("Main deploy: %d meters (%d feet)\n",
166                ao_config.main_deploy,
167                (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
168 }
169
170 void
171 ao_config_main_deploy_set(void) __reentrant
172 {
173         ao_cmd_decimal();
174         if (ao_cmd_status != ao_cmd_success)
175                 return;
176         ao_mutex_get(&ao_config_mutex);
177         _ao_config_get();
178         ao_config.main_deploy = ao_cmd_lex_i;
179         ao_config_dirty = 1;
180         ao_mutex_put(&ao_config_mutex);
181         ao_config_main_deploy_show();
182 }
183
184 void
185 ao_config_accel_calibrate_show(void) __reentrant
186 {
187         printf("Accel cal +1g: %d -1g: %d\n",
188                ao_config.accel_plus_g, ao_config.accel_minus_g);
189 }
190
191 #define ACCEL_CALIBRATE_SAMPLES 1024
192 #define ACCEL_CALIBRATE_SHIFT   10
193
194 static int16_t
195 ao_config_accel_calibrate_auto(char *orientation) __reentrant
196 {
197         uint16_t        i;
198         int32_t         accel_total;
199         uint8_t         cal_adc_ring;
200
201         printf("Orient %s and press a key...", orientation);
202         flush();
203         (void) getchar();
204         puts("\r\n"); flush();
205         puts("Calibrating..."); flush();
206         i = ACCEL_CALIBRATE_SAMPLES;
207         accel_total = 0;
208         cal_adc_ring = ao_adc_head;
209         while (i) {
210                 ao_sleep(&ao_adc_ring);
211                 while (i && cal_adc_ring != ao_adc_head) {
212                         accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
213                         cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
214                         i--;
215                 }
216         }
217         return accel_total >> ACCEL_CALIBRATE_SHIFT;
218 }
219
220 void
221 ao_config_accel_calibrate_set(void) __reentrant
222 {
223         int16_t up, down;
224         ao_cmd_decimal();
225         if (ao_cmd_status != ao_cmd_success)
226                 return;
227         if (ao_cmd_lex_i == 0) {
228                 up = ao_config_accel_calibrate_auto("antenna up");
229                 down = ao_config_accel_calibrate_auto("antenna down");
230         } else {
231                 up = ao_cmd_lex_i;
232                 ao_cmd_decimal();
233                 if (ao_cmd_status != ao_cmd_success)
234                         return;
235                 down = ao_cmd_lex_i;
236         }
237         if (up >= down) {
238                 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
239                        up, down);
240                 return;
241         }
242         ao_mutex_get(&ao_config_mutex);
243         _ao_config_get();
244         ao_config.accel_plus_g = up;
245         ao_config.accel_minus_g = down;
246         ao_config_dirty = 1;
247         ao_mutex_put(&ao_config_mutex);
248         ao_config_accel_calibrate_show();
249 }
250
251 void
252 ao_config_apogee_delay_show(void) __reentrant
253 {
254         printf("Apogee delay: %d seconds\n",
255                ao_config.apogee_delay);
256 }
257
258 void
259 ao_config_apogee_delay_set(void) __reentrant
260 {
261         ao_cmd_decimal();
262         if (ao_cmd_status != ao_cmd_success)
263                 return;
264         ao_mutex_get(&ao_config_mutex);
265         _ao_config_get();
266         ao_config.apogee_delay = ao_cmd_lex_i;
267         ao_config_dirty = 1;
268         ao_mutex_put(&ao_config_mutex);
269         ao_config_apogee_delay_show();
270 }
271
272 #endif /* HAS_ADC */
273
274 void
275 ao_config_radio_cal_show(void) __reentrant
276 {
277         printf("Radio cal: %ld\n", ao_config.radio_cal);
278 }
279
280 void
281 ao_config_radio_cal_set(void) __reentrant
282 {
283         ao_cmd_decimal();
284         if (ao_cmd_status != ao_cmd_success)
285                 return;
286         ao_mutex_get(&ao_config_mutex);
287         _ao_config_get();
288         ao_config.radio_cal = ao_cmd_lex_u32;
289         ao_config_dirty = 1;
290         ao_mutex_put(&ao_config_mutex);
291         ao_config_radio_cal_show();
292 }
293
294 struct ao_config_var {
295         char            cmd;
296         void            (*set)(void) __reentrant;
297         void            (*show)(void) __reentrant;
298         const char      *help;
299 };
300
301 static void
302 ao_config_help(void) __reentrant;
303
304 static void
305 ao_config_show(void) __reentrant;
306
307 static void
308 ao_config_write(void) __reentrant;
309
310 __code struct ao_config_var ao_config_vars[] = {
311 #if HAS_ADC
312         { 'm',  ao_config_main_deploy_set,      ao_config_main_deploy_show,
313                 "m <meters>  Set height above launch for main deploy (in meters)" },
314         { 'd',  ao_config_apogee_delay_set,     ao_config_apogee_delay_show,
315                 "d <delay>   Set apogee igniter delay (in seconds)" },
316 #endif /* HAS_ADC */
317         { 'r',  ao_config_radio_channel_set,    ao_config_radio_channel_show,
318                 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
319         { 'c',  ao_config_callsign_set,         ao_config_callsign_show,
320                 "c <call>    Set callsign broadcast in each packet (8 char max)" },
321 #if HAS_ADC
322         { 'a',  ao_config_accel_calibrate_set,  ao_config_accel_calibrate_show,
323                 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
324 #endif /* HAS_ADC */
325         { 'f',  ao_config_radio_cal_set,        ao_config_radio_cal_show,
326                 "f <cal>     Set radio calibration value (cal = rf/(xtal/2^16))" },
327         { 's',  ao_config_show,                 ao_config_show,
328                 "s           Show current config values" },
329 #if HAS_EEPROM
330         { 'w',  ao_config_write,                ao_config_write,
331                 "w           Write current values to eeprom" },
332 #endif
333         { '?',  ao_config_help,                 ao_config_help,
334                 "?           Show available config variables" },
335         { 0,    ao_config_help, ao_config_help,
336                 NULL },
337 };
338
339 void
340 ao_config_set(void)
341 {
342         char    c;
343         uint8_t cmd;
344         void (*__xdata func)(void) __reentrant;
345
346         ao_cmd_white();
347         c = ao_cmd_lex_c;
348         ao_cmd_lex();
349         func = 0;
350         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
351                 if (ao_config_vars[cmd].cmd == c) {
352                         func = ao_config_vars[cmd].set;
353                         break;
354                 }
355         if (func)
356                 (*func)();
357         else
358                 ao_cmd_status = ao_cmd_syntax_error;
359 }
360
361 static void
362 ao_config_help(void) __reentrant
363 {
364         uint8_t cmd;
365         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
366                 puts (ao_config_vars[cmd].help);
367 }
368
369 static void
370 ao_config_show(void) __reentrant
371 {
372         uint8_t cmd;
373         printf("Config version: %d.%d\n",
374                ao_config.major, ao_config.minor);
375         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
376                 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
377                         (*ao_config_vars[cmd].show)();
378 }
379
380 #if HAS_EEPROM
381 static void
382 ao_config_write(void) __reentrant
383 {
384         uint8_t saved = 0;
385         ao_mutex_get(&ao_config_mutex);
386         if (ao_config_dirty) {
387                 _ao_config_put();
388                 ao_config_dirty = 0;
389                 saved = 1;
390         }
391         ao_mutex_put(&ao_config_mutex);
392         if (saved)
393                 puts("Saved");
394         else
395                 puts("Nothing to save");
396 }
397 #endif
398
399 __code struct ao_cmds ao_config_cmds[] = {
400         { 'c',  ao_config_set,  "c <var> <value>                    Set config variable (? for help, s to show)" },
401         { '\0', ao_config_set, NULL },
402 };
403
404 void
405 ao_config_init(void)
406 {
407         ao_cmd_register(&ao_config_cmds[0]);
408 }