2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
30 static const struct option options[] = {
31 { .name = "tty", .has_arg = 1, .val = 'T' },
32 { .name = "device", .has_arg = 1, .val = 'D' },
33 { .name = "wait", .has_arg = 0, .val = 'w' },
37 static void usage(char *program)
39 fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--wait]\n", program);
43 static int get_nonwhite(struct cc_usb *cc, int timeout)
48 c = cc_usb_getchar_timeout(cc, timeout);
56 get_hexc(struct cc_usb *cc)
58 int c = get_nonwhite(cc, 1000);
60 if ('0' <= c && c <= '9')
62 if ('a' <= c && c <= 'f')
64 if ('A' <= c && c <= 'F')
66 fprintf(stderr, "Non-hex char '%c'\n", c);
72 static const int POLY = 0x8408;
75 log_crc(int crc, int b)
79 for (i = 0; i < 8; i++) {
80 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
81 crc = (crc >> 1) ^ POLY;
90 get_hex(struct cc_usb *cc)
96 file_crc = log_crc(file_crc, h);
100 static int get_32(struct cc_usb *cc)
104 for (i = 0; i < 4; i++) {
105 v += get_hex(cc) << (i * 8);
110 static int get_16(struct cc_usb *cc)
114 for (i = 0; i < 2; i++) {
115 v += get_hex(cc) << (i * 8);
120 static int swap16(int i)
122 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
125 static int find_header(struct cc_usb *cc)
128 if (get_nonwhite(cc, -1) == 'M' && get_nonwhite(cc, 1000) == 'P')
134 main (int argc, char **argv)
146 while ((c = getopt_long(argc, argv, "wT:D:", options, NULL)) != -1) {
164 tty = cc_usbdevs_find_by_arg(device, "FT230X Basic UART");
167 printf("tty is %s\n", tty);
178 tty = getenv("ALTOS_TTY");
181 cc = cc_usb_open(tty);
186 get_32(cc); /* ground pressure */
187 get_32(cc); /* min pressure */
188 nsamples = get_16(cc); /* nsamples */
189 for (i = 0; i < nsamples; i++)
190 get_16(cc); /* sample i */
191 current_crc = swap16(~file_crc & 0xffff);
192 crc = get_16(cc); /* crc */
194 if (crc == current_crc)
195 printf("CRC valid\n");
197 printf("CRC invalid\n");