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.
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' },
36 static void usage(char *program)
38 fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--wait]\n", program);
43 log_checksum(int d[8])
48 for (i = 0; i < 8; i++)
49 sum += (uint8_t) d[i];
53 static int get_nonwhite(struct cc_usb *cc, int timeout)
58 c = cc_usb_getchar_timeout(cc, timeout);
66 get_hexc(struct cc_usb *cc)
68 int c = get_nonwhite(cc, 1000);
70 if ('0' <= c && c <= '9')
72 if ('a' <= c && c <= 'f')
74 if ('A' <= c && c <= 'F')
76 fprintf(stderr, "Non-hex char '%c'\n", c);
82 static const int POLY = 0x8408;
85 log_crc(int crc, int b)
89 for (i = 0; i < 8; i++) {
90 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
91 crc = (crc >> 1) ^ POLY;
100 get_hex(struct cc_usb *cc)
102 int a = get_hexc(cc);
103 int b = get_hexc(cc);
104 int h = (a << 4) + b;
106 file_crc = log_crc(file_crc, h);
110 static int get_32(struct cc_usb *cc)
114 for (i = 0; i < 4; i++) {
115 v += get_hex(cc) << (i * 8);
120 static int get_16(struct cc_usb *cc)
124 for (i = 0; i < 2; i++) {
125 v += get_hex(cc) << (i * 8);
130 static int swap16(int i)
132 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
135 static int find_header(struct cc_usb *cc)
138 if (get_nonwhite(cc, -1) == 'M' && get_nonwhite(cc, 1000) == 'P')
144 main (int argc, char **argv)
157 while ((c = getopt_long(argc, argv, "wT:D:", options, NULL)) != -1) {
175 tty = cc_usbdevs_find_by_arg(device, "FT230X Basic UART");
178 printf("tty is %s\n", tty);
189 tty = getenv("ALTOS_TTY");
192 cc = cc_usb_open(tty);
197 get_32(cc); /* ground pressure */
198 get_32(cc); /* min pressure */
199 nsamples = get_16(cc); /* nsamples */
200 for (i = 0; i < nsamples; i++)
201 get_16(cc); /* sample i */
202 current_crc = swap16(~file_crc & 0xffff);
203 crc = get_16(cc); /* crc */
205 if (crc == current_crc)
206 printf("CRC valid\n");
208 printf("CRC invalid\n");