ao-tools: Create ao-cal-freq
[fw/altos] / ao-tools / ao-cal-freq / ao-cal-freq.c
1 /*
2  * Copyright © 2014 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 <err.h>
19 #include <fcntl.h>
20 #include <gelf.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <sysexits.h>
25 #include <unistd.h>
26 #include <getopt.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <termios.h>
30 #include <math.h>
31 #include "ao-elf.h"
32 #include "ccdbg.h"
33 #include "cc-usb.h"
34 #include "cc.h"
35 #include "ao-verbose.h"
36
37 static const struct option options[] = {
38         { .name = "tty", .has_arg = 1, .val = 'T' },
39         { .name = "device", .has_arg = 1, .val = 'D' },
40         { .name = "raw", .has_arg = 0, .val = 'r' },
41         { .name = "verbose", .has_arg = 1, .val = 'v' },
42         { 0, 0, 0, 0},
43 };
44
45 static void usage(char *program)
46 {
47         fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>]\n", program);
48         exit(1);
49 }
50
51 void
52 done(struct cc_usb *cc, int code)
53 {
54         cc_usb_close(cc);
55         exit (code);
56 }
57
58 static int
59 ends_with(char *whole, char *suffix)
60 {
61         int whole_len = strlen(whole);
62         int suffix_len = strlen(suffix);
63
64         if (suffix_len > whole_len)
65                 return 0;
66         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
67 }
68
69 static int
70 starts_with(char *whole, char *prefix)
71 {
72         int whole_len = strlen(whole);
73         int prefix_len = strlen(prefix);
74
75         if (prefix_len > whole_len)
76                 return 0;
77         return strncmp(whole, prefix, prefix_len) == 0;
78 }
79
80 static char **
81 tok(char *line) {
82         char    **strs = malloc (sizeof (char *)), *str;
83         int     n = 0;
84
85         while ((str = strtok(line, " \t"))) {
86                 line = NULL;
87                 strs = realloc(strs, (n + 2) * sizeof (char *));
88                 strs[n] = strdup(str);
89                 n++;
90         }
91         strs[n] = '\0';
92         return strs;
93 }
94
95 static void
96 free_strs(char **strs) {
97         char    *str;
98         int     i;
99
100         for (i = 0; (str = strs[i]) != NULL; i++)
101                 free(str);
102         free(strs);
103 }
104
105 struct flash {
106         struct flash    *next;
107         char            line[512];
108         char            **strs;
109 };
110
111 static struct flash *
112 flash(struct cc_usb *usb)
113 {
114         struct flash    *head = NULL, **tail = &head;
115         cc_usb_printf(usb, "c s\nv\n");
116         for (;;) {
117                 char    line[512];
118                 struct flash    *b;
119
120                 cc_usb_getline(usb, line, sizeof (line));
121                 b = malloc (sizeof (struct flash));
122                 strcpy(b->line, line);
123                 b->strs = tok(line);
124                 b->next = NULL;
125                 *tail = b;
126                 tail = &b->next;
127                 if (strstr(line, "software-version"))
128                         break;
129         }
130         return head;
131 }
132
133 static void
134 free_flash(struct flash *b) {
135         struct flash *n;
136
137         while (b) {
138                 n = b->next;
139                 free_strs(b->strs);
140                 free(b);
141                 b = n;
142         }
143 }
144
145 char **
146 find_flash(struct flash *b, char *word0) {
147         int i;
148         for (;b; b = b->next) {
149                 if (strstr(b->line, word0))
150                         return b->strs;
151         }
152         return NULL;
153 }
154
155 void
156 await_key(void)
157 {
158         struct termios  termios, termios_save;
159         char    buf[512];
160
161         tcgetattr(0, &termios);
162         termios_save = termios;
163         cfmakeraw(&termios);
164         tcsetattr(0, TCSAFLUSH, &termios);
165         read(0, buf, sizeof (buf));
166         tcsetattr(0, TCSAFLUSH, &termios_save);
167 }
168
169 int
170 do_cal(struct cc_usb *usb) {
171         struct flash    *b;
172         char    line[1024];
173         double  measured_freq;
174         char    **cur_freq_words;
175         char    **cur_cal_words;
176         char    *line_end;
177         int     cur_freq;
178         int     cur_cal;
179         int     new_cal;
180
181         cc_usb_printf(usb, "E 0\n");
182
183         for(;;) {
184                 cc_usb_printf(usb, "C 1\n");
185                 cc_usb_sync(usb);
186
187                 printf("Generating RF carrier. Please enter measured frequency [enter for done]: ");
188                 fflush(stdout);
189                 fgets(line, sizeof (line) - 1, stdin);
190                 cc_usb_printf(usb, "C 0\n");
191                 cc_usb_sync(usb);
192
193                 measured_freq = strtod(line, &line_end);
194                 if (line_end == line)
195                         break;
196
197                 b = flash(usb);
198
199                 cur_cal_words = find_flash(b, "Radio cal:");
200                 cur_freq_words = find_flash(b, "Frequency:");
201
202                 if (!cur_cal_words || !cur_freq_words) {
203                         printf("no response\n");
204                         return 0;
205                 }
206
207                 cur_cal = atoi(cur_cal_words[2]);
208                 cur_freq = atoi(cur_freq_words[1]);
209
210                 printf ("Current radio calibration %d\n", cur_cal);
211                 printf ("Current radio frequency: %d\n", cur_freq);
212
213
214                 new_cal = floor ((((double) cur_freq / 1000.0) / measured_freq) * cur_cal + 0.5);
215
216                 printf ("Programming flash with cal value %d\n", new_cal);
217
218                 cc_usb_printf (usb, "c f %d\nc w\n", new_cal);
219                 cc_usb_sync(usb);
220         }
221         return 1;
222 }
223
224 int
225 main (int argc, char **argv)
226 {
227         char                    *device = NULL;
228         char                    *filename;
229         Elf                     *e;
230         unsigned int            s;
231         int                     i;
232         int                     c;
233         int                     tries;
234         struct cc_usb           *cc = NULL;
235         char                    *tty = NULL;
236         int                     success;
237         int                     verbose = 0;
238         int                     ret = 0;
239         int                     expected_size;
240
241         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
242                 switch (c) {
243                 case 'T':
244                         tty = optarg;
245                         break;
246                 case 'D':
247                         device = optarg;
248                         break;
249                 case 'v':
250                         verbose++;
251                         break;
252                 default:
253                         usage(argv[0]);
254                         break;
255                 }
256         }
257
258         ao_verbose = verbose;
259
260         if (verbose > 1)
261                 ccdbg_add_debug(CC_DEBUG_BITBANG);
262
263         if (!tty)
264                 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
265         if (!tty)
266                 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
267         if (!tty)
268                 tty = getenv("ALTOS_TTY");
269         if (!tty)
270                 tty="/dev/ttyACM0";
271
272         cc = cc_usb_open(tty);
273
274         if (!cc)
275                 exit(1);
276
277         if (!do_cal(cc))
278                 ret = 1;
279         done(cc, ret);
280 }