3 * test setting the baudrate and compare it with the expected runtime
6 * -p <devicestring> defaults to "i:0x0403:0x6001" (this is the first FT232R with default id)
7 * d:<devicenode> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
8 * i:<vendor>:<product> first device with given vendor and product id,
9 * ids can be decimal, octal (preceded by "0") or hex (preceded by "0x")
10 * i:<vendor>:<product>:<index> as above with index being the number of the device (starting with 0)
11 * if there are more than one
12 * s:<vendor>:<product>:<serial> first device with given vendor id, product id and serial string
13 * -d <datasize to send in bytes>
14 * -b <baudrate> (divides by 16 if bitbang as taken from the ftdi datasheets)
15 * -m <mode to use> r: serial a: async bitbang s:sync bitbang
18 * (C) 2009 by Gerd v. Egidy <gerd.von.egidy@intra2net.com>
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
38 double get_prec_time()
43 gettimeofday(&tv,NULL);
46 res+=((double)tv.tv_usec/1000000);
51 int main(int argc, char **argv)
53 struct ftdi_context *ftdi;
57 double start, duration, plan;
65 char default_devicedesc[] = "i:0x0403:0x6001";
66 char *devicedesc=default_devicedesc;
68 enum ftdi_mpsse_mode test_mode=BITMODE_BITBANG;
70 while ((t = getopt (argc, argv, "b:d:p:m:c:")) != -1)
75 datasize = atoi (optarg);
82 test_mode=BITMODE_RESET;
86 test_mode=BITMODE_BITBANG;
90 test_mode=BITMODE_SYNCBB;
101 txchunksize = atoi (optarg);
106 txbuf=malloc(txchunksize);
107 rxbuf=malloc(txchunksize);
108 if (txbuf == NULL || rxbuf == NULL)
110 fprintf(stderr, "can't malloc\n");
114 if ((ftdi = ftdi_new()) == 0)
116 fprintf(stderr, "ftdi_new failed\n");
117 retval = EXIT_FAILURE;
121 if (ftdi_usb_open_string(ftdi, devicedesc) < 0)
123 fprintf(stderr,"Can't open ftdi device: %s\n",ftdi_get_error_string(ftdi));
124 retval = EXIT_FAILURE;
129 if (test_mode!=BITMODE_RESET)
131 // we do bitbang, so real baudrate / 16
135 ftdi_set_baudrate(ftdi,set_baud);
136 printf("real baudrate used: %d\n",(test_mode==BITMODE_RESET) ? ftdi->baudrate : ftdi->baudrate*16);
138 if (ftdi_set_bitmode(ftdi, 0xFF,test_mode) < 0)
140 fprintf(stderr,"Can't set mode: %s\n",ftdi_get_error_string(ftdi));
141 retval = EXIT_FAILURE;
145 if (test_mode==BITMODE_RESET)
147 // serial 8N1: 8 data bits, 1 startbit, 1 stopbit
148 plan=((double)(datasize*10))/baud;
152 // bitbang means 8 bits at once
153 plan=((double)datasize)/baud;
156 printf("this test should take %.2f seconds\n",plan);
158 // prepare data to send: 0 and 1 bits alternating (except for serial start/stopbit):
159 // maybe someone wants to look at this with a scope or logic analyzer
160 for (i=0; i<txchunksize; i++)
162 if (test_mode==BITMODE_RESET)
165 txbuf[i]=(i%2) ? 0xff : 0;
168 if (ftdi_write_data_set_chunksize(ftdi, txchunksize) < 0 ||
169 ftdi_read_data_set_chunksize(ftdi, txchunksize) < 0)
171 fprintf(stderr,"Can't set chunksize: %s\n",ftdi_get_error_string(ftdi));
172 retval = EXIT_FAILURE;
176 if (test_mode==BITMODE_SYNCBB)
178 // completely clear the receive buffer before beginning
179 while (ftdi_read_data(ftdi, rxbuf, txchunksize)>0);
182 start=get_prec_time();
184 // don't wait for more data to arrive, take what we get and keep on sending
185 // yes, we really would like to have libusb 1.0+ with async read/write...
186 ftdi->usb_read_timeout=1;
191 int sendsize=txchunksize;
192 if (i+sendsize > datasize)
195 if ((sendsize=ftdi_write_data(ftdi, txbuf, sendsize)) < 0)
197 fprintf(stderr,"write failed at %d: %s\n",
198 i, ftdi_get_error_string(ftdi));
199 retval = EXIT_FAILURE;
205 if (test_mode==BITMODE_SYNCBB)
207 // read the same amount of data as sent
208 ftdi_read_data(ftdi, rxbuf, sendsize);
212 duration=get_prec_time()-start;
213 printf("and took %.4f seconds, this is %.0f baud or factor %.3f\n",duration,(plan*baud)/duration,plan/duration);
215 ftdi_usb_close(ftdi);