ao-dump-up: Add --wait option to make testing µPusb easier
[fw/altos] / ao-tools / ao-dump-up / ao-dump-up.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; 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 <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <string.h>
24 #include "cc-usb.h"
25 #include "cc.h"
26
27 #define NUM_BLOCK       512
28
29 static const struct option options[] = {
30         { .name = "tty", .has_arg = 1, .val = 'T' },
31         { .name = "device", .has_arg = 1, .val = 'D' },
32         { .name = "wait", .has_arg = 0, .val = 'w' },
33         { 0, 0, 0, 0},
34 };
35
36 static void usage(char *program)
37 {
38         fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--wait]\n", program);
39         exit(1);
40 }
41
42 static uint8_t
43 log_checksum(int d[8])
44 {
45         uint8_t sum = 0x5a;
46         int     i;
47
48         for (i = 0; i < 8; i++)
49                 sum += (uint8_t) d[i];
50         return -sum;
51 }
52
53 static int get_nonwhite(struct cc_usb *cc, int timeout)
54 {
55         int     c;
56
57         for (;;) {
58                 c = cc_usb_getchar_timeout(cc, timeout);
59                 putchar(c);
60                 if (!isspace(c))
61                         return c;
62         }
63 }
64
65 static uint8_t
66 get_hexc(struct cc_usb *cc)
67 {
68         int     c = get_nonwhite(cc, 1000);
69
70         if ('0' <= c && c <= '9')
71                 return c - '0';
72         if ('a' <= c && c <= 'f')
73                 return c - 'a' + 10;
74         if ('A' <= c && c <= 'F')
75                 return c - 'A' + 10;
76         fprintf(stderr, "Non-hex char '%c'\n", c);
77         exit(1);
78 }
79
80 static int file_crc;
81
82 static const int POLY = 0x8408;
83
84 static int
85 log_crc(int crc, int b)
86 {
87         int     i;
88
89         for (i = 0; i < 8; i++) {
90                 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
91                         crc = (crc >> 1) ^ POLY;
92                 else
93                         crc = crc >> 1;
94                 b >>= 1;
95         }
96         return crc & 0xffff;
97 }
98
99 static uint8_t
100 get_hex(struct cc_usb *cc)
101 {
102         int     a = get_hexc(cc);
103         int     b = get_hexc(cc);
104         int     h = (a << 4) + b;
105
106         file_crc = log_crc(file_crc, h);
107         return h;
108 }
109
110 static int get_32(struct cc_usb *cc)
111 {
112         int     v = 0;
113         int     i;
114         for (i = 0; i < 4; i++) {
115                 v += get_hex(cc) << (i * 8);
116         }
117         return v;
118 }
119
120 static int get_16(struct cc_usb *cc)
121 {
122         int     v = 0;
123         int     i;
124         for (i = 0; i < 2; i++) {
125                 v += get_hex(cc) << (i * 8);
126         }
127         return v;
128 }
129
130 static int swap16(int i)
131 {
132         return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
133 }
134
135 static int find_header(struct cc_usb *cc)
136 {
137         for (;;) {
138                 if (get_nonwhite(cc, -1) == 'M' && get_nonwhite(cc, 1000) == 'P')
139                         return 1;
140         }
141 }
142
143 static const char *state_names[] = {
144         "startup",
145         "idle",
146         "pad",
147         "boost",
148         "fast",
149         "coast",
150         "drogue",
151         "main",
152         "landed",
153         "invalid"
154 };
155
156
157 int
158 main (int argc, char **argv)
159 {
160         struct cc_usb   *cc;
161         char            *tty = NULL;
162         char            *device = NULL;
163         int             c;
164         char            line[8192];
165         int             nsamples;
166         int             i;
167         int             crc;
168         int             current_crc;
169         int             wait = 0;
170
171         while ((c = getopt_long(argc, argv, "wT:D:", options, NULL)) != -1) {
172                 switch (c) {
173                 case 'w':
174                         wait = 1;
175                         break;
176                 case 'T':
177                         tty = optarg;
178                         break;
179                 case 'D':
180                         device = optarg;
181                         break;
182                 default:
183                         usage(argv[0]);
184                         break;
185                 }
186         }
187         if (!tty) {
188                 for (;;) {
189                         tty = cc_usbdevs_find_by_arg(device, "FT230X Basic UART");
190                         if (tty) {
191                                 if (wait) {
192                                         printf("tty is %s\n", tty);
193                                         sleep(1);
194                                 }
195                                 break;
196                         }
197                         if (!wait)
198                                 break;
199                         sleep(1);
200                 }
201         }
202         if (!tty)
203                 tty = getenv("ALTOS_TTY");
204         if (!tty)
205                 tty="/dev/ttyUSB0";
206         cc = cc_usb_open(tty);
207         if (!cc)
208                 exit(1);
209         find_header(cc);
210         file_crc = 0xffff;
211         get_32(cc);     /* ground pressure */
212         get_32(cc);     /* min pressure */
213         nsamples = get_16(cc);  /* nsamples */
214         for (i = 0; i < nsamples; i++)
215                 get_16(cc);     /* sample i */
216         current_crc = swap16(~file_crc & 0xffff);
217         crc = get_16(cc);       /* crc */
218         putchar ('\n');
219         if (crc == current_crc)
220                 printf("CRC valid\n");
221         else
222                 printf("CRC invalid\n");
223         cc_usb_close(cc);
224         exit (0);
225 }