3 * test setting the baudrate and compare it with the expected runtime
6 * -p <usb-product-id> (vendor is fixed to ftdi / 0x0403)
7 * -d <datasize to send in bytes>
8 * -b <baudrate> (divides by 16 if bitbang as taken from the ftdi datasheets)
9 * -m <mode to use> r: serial a: async bitbang s:sync bitbang
12 * (C) 2009 by Gerd v. Egidy <gerd.von.egidy@intra2net.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
31 double get_prec_time()
36 gettimeofday(&tv,NULL);
39 res+=((double)tv.tv_usec/1000000);
44 int main(int argc, char **argv)
46 struct ftdi_context ftdic;
50 double start, duration, plan;
56 int product_id=0x6001;
58 enum ftdi_mpsse_mode test_mode=BITMODE_BITBANG;
60 while ((t = getopt (argc, argv, "b:d:p:m:c:")) != -1)
65 datasize = atoi (optarg);
72 test_mode=BITMODE_RESET;
76 test_mode=BITMODE_BITBANG;
80 test_mode=BITMODE_SYNCBB;
88 sscanf(optarg,"0x%x",&product_id);
91 txchunksize = atoi (optarg);
96 txbuf=malloc(txchunksize);
97 rxbuf=malloc(txchunksize);
98 if (txbuf == NULL || rxbuf == NULL)
100 fprintf(stderr, "can't malloc\n");
104 if (ftdi_init(&ftdic) < 0)
106 fprintf(stderr, "ftdi_init failed\n");
110 if (ftdi_usb_open(&ftdic, 0x0403, product_id) < 0)
112 fprintf(stderr,"Can't open ftdi device: %s\n",ftdi_get_error_string(&ftdic));
117 if (test_mode!=BITMODE_RESET)
119 // we do bitbang, so real baudrate / 16
123 ftdi_set_baudrate(&ftdic,set_baud);
124 printf("real baudrate used: %d\n",(test_mode==BITMODE_RESET) ? ftdic.baudrate : ftdic.baudrate*16);
126 if (ftdi_set_bitmode(&ftdic, 0xFF,test_mode) < 0)
128 fprintf(stderr,"Can't set mode: %s\n",ftdi_get_error_string(&ftdic));
132 if (test_mode==BITMODE_RESET)
134 // serial 8N1: 8 data bits, 1 startbit, 1 stopbit
135 plan=((double)(datasize*10))/baud;
139 // bitbang means 8 bits at once
140 plan=((double)datasize)/baud;
143 printf("this test should take %.2f seconds\n",plan);
145 // prepare data to send: 0 and 1 bits alternating (except for serial start/stopbit):
146 // maybe someone wants to look at this with a scope or logic analyzer
147 for (i=0; i<txchunksize; i++)
149 if (test_mode==BITMODE_RESET)
152 txbuf[i]=(i%2) ? 0xff : 0;
155 if (ftdi_write_data_set_chunksize(&ftdic, txchunksize) < 0 ||
156 ftdi_read_data_set_chunksize(&ftdic, txchunksize) < 0)
158 fprintf(stderr,"Can't set chunksize: %s\n",ftdi_get_error_string(&ftdic));
162 if(test_mode==BITMODE_SYNCBB)
164 // completely clear the receive buffer before beginning
165 while(ftdi_read_data(&ftdic, rxbuf, txchunksize)>0);
168 start=get_prec_time();
170 // don't wait for more data to arrive, take what we get and keep on sending
171 // yes, we really would like to have libusb 1.0+ with async read/write...
172 ftdic.usb_read_timeout=1;
177 int sendsize=txchunksize;
178 if (i+sendsize > datasize)
181 if ((sendsize=ftdi_write_data(&ftdic, txbuf, sendsize)) < 0)
183 fprintf(stderr,"write failed at %d: %s\n",
184 i, ftdi_get_error_string(&ftdic));
190 if(test_mode==BITMODE_SYNCBB)
192 // read the same amount of data as sent
193 ftdi_read_data(&ftdic, rxbuf, sendsize);
197 duration=get_prec_time()-start;
198 printf("and took %.4f seconds, this is %.0f baud or factor %.3f\n",duration,(plan*baud)/duration,plan/duration);
200 ftdi_usb_close(&ftdic);