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.
31 static const struct option options[] = {
32 { .name = "tty", .has_arg = 1, .val = 'T' },
33 { .name = "device", .has_arg = 1, .val = 'D' },
34 { .name = "wait", .has_arg = 0, .val = 'w' },
38 static void usage(char *program)
40 fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--wait]\n", program);
44 static int get_nonwhite(struct cc_usb *cc, int timeout)
49 c = cc_usb_getchar_timeout(cc, timeout);
56 static const uint8_t test_data[] = {
57 0xfc, 0xfd, 0xfe, 0xff, 0xf8, 0xf9, 0xfa, 0xfb, 0x40, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
58 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
59 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
60 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
61 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
62 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
63 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
64 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
65 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
69 static bool test_failed;
75 if (test_pos >= sizeof (test_data) || test_data[test_pos++] != b)
80 get_hexc(struct cc_usb *cc)
82 int c = get_nonwhite(cc, 1000);
84 if ('0' <= c && c <= '9')
86 if ('a' <= c && c <= 'f')
88 if ('A' <= c && c <= 'F')
90 fprintf(stderr, "Non-hex char '%c'\n", c);
96 static const int POLY = 0x8408;
99 log_crc(int crc, int b)
103 for (i = 0; i < 8; i++) {
104 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
105 crc = (crc >> 1) ^ POLY;
114 get_hex(struct cc_usb *cc)
116 int a = get_hexc(cc);
117 int b = get_hexc(cc);
118 int h = (a << 4) + b;
120 file_crc = log_crc(file_crc, h);
125 static int get_32(struct cc_usb *cc)
129 for (i = 0; i < 4; i++) {
130 v += get_hex(cc) << (i * 8);
135 static int get_16(struct cc_usb *cc)
139 for (i = 0; i < 2; i++) {
140 v += get_hex(cc) << (i * 8);
145 static int swap16(int i)
147 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
150 static int find_header(struct cc_usb *cc)
153 if (get_nonwhite(cc, -1) == 'M' && get_nonwhite(cc, 1000) == 'P')
159 main (int argc, char **argv)
171 while ((c = getopt_long(argc, argv, "wT:D:", options, NULL)) != -1) {
189 tty = cc_usbdevs_find_by_arg(device, "FT230X Basic UART");
192 printf("tty is %s\n", tty);
203 tty = getenv("ALTOS_TTY");
206 cc = cc_usb_open(tty);
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 */
219 if (crc == current_crc) {
221 printf("\033[32mValid MicroTest Data\033[39m\n");
223 printf("CRC valid\n");
226 printf("CRC invalid\n");