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' },
35 static void usage(char *program)
37 fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>]\n", program);
42 log_checksum(int d[8])
47 for (i = 0; i < 8; i++)
48 sum += (uint8_t) d[i];
52 static int get_nonwhite(struct cc_usb *cc, int timeout)
57 c = cc_usb_getchar_timeout(cc, timeout);
65 get_hexc(struct cc_usb *cc)
67 int c = get_nonwhite(cc, 1000);
69 if ('0' <= c && c <= '9')
71 if ('a' <= c && c <= 'f')
73 if ('A' <= c && c <= 'F')
75 fprintf(stderr, "Non-hex char '%c'\n", c);
81 static const int POLY = 0x8408;
84 log_crc(int crc, int b)
88 for (i = 0; i < 8; i++) {
89 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
90 crc = (crc >> 1) ^ POLY;
99 get_hex(struct cc_usb *cc)
101 int a = get_hexc(cc);
102 int b = get_hexc(cc);
103 int h = (a << 4) + b;
105 file_crc = log_crc(file_crc, h);
109 static int get_32(struct cc_usb *cc)
113 for (i = 0; i < 4; i++) {
114 v += get_hex(cc) << (i * 8);
119 static int get_16(struct cc_usb *cc)
123 for (i = 0; i < 2; i++) {
124 v += get_hex(cc) << (i * 8);
129 static int swap16(int i)
131 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
134 static int find_header(struct cc_usb *cc)
137 if (get_nonwhite(cc, 0) == 'M' && get_nonwhite(cc, 1000) == 'P')
142 static const char *state_names[] = {
157 main (int argc, char **argv)
169 while ((c = getopt_long(argc, argv, "T:D:", options, NULL)) != -1) {
183 tty = cc_usbdevs_find_by_arg(device, "FT230X Basic UART");
185 tty = getenv("ALTOS_TTY");
188 cc = cc_usb_open(tty);
193 get_32(cc); /* ground pressure */
194 get_32(cc); /* min pressure */
195 nsamples = get_16(cc); /* nsamples */
196 for (i = 0; i < nsamples; i++)
197 get_16(cc); /* sample i */
198 current_crc = swap16(~file_crc & 0xffff);
199 crc = get_16(cc); /* crc */
201 if (crc == current_crc)
202 printf("CRC valid\n");
204 printf("CRC invalid\n");