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