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> (multiplies by 16 if bitbang as written in the ftdi datasheets)
9 * -m <mode to use> r: serial a: async bitbang s:sync bitbang
11 * (C) 2009 by Gerd v. Egidy <gerd.von.egidy@intra2net.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
30 double get_prec_time()
35 gettimeofday(&tv,NULL);
38 res+=((double)tv.tv_usec/1000000);
43 int main(int argc, char **argv)
45 struct ftdi_context ftdic;
49 double start, duration, plan;
55 int product_id=0x6001;
56 enum ftdi_mpsse_mode test_mode=BITMODE_BITBANG;
58 while ((t = getopt (argc, argv, "b:d:p:m:")) != -1)
63 datasize = atoi (optarg);
70 test_mode=BITMODE_RESET;
74 test_mode=BITMODE_BITBANG;
78 test_mode=BITMODE_SYNCBB;
86 sscanf(optarg,"0x%x",&product_id);
91 if (ftdi_init(&ftdic) < 0)
93 fprintf(stderr, "ftdi_init failed\n");
97 if (ftdi_usb_open(&ftdic, 0x0403, product_id) < 0)
99 fprintf(stderr,"Can't open ftdi device: %s\n",ftdi_get_error_string(&ftdic));
104 if (test_mode!=BITMODE_RESET)
106 // we do bitbang, so real baudrate / 16
110 ftdi_set_baudrate(&ftdic,set_baud);
111 printf("real baudrate used: %d\n",(test_mode==BITMODE_RESET) ? ftdic.baudrate : ftdic.baudrate*16);
113 if (ftdi_set_bitmode(&ftdic, 0xFF,test_mode) < 0)
115 fprintf(stderr,"Can't set mode: %s\n",ftdi_get_error_string(&ftdic));
119 if (test_mode==BITMODE_RESET)
121 // serial 8N1: 8 data bits, 1 startbit, 1 stopbit
122 plan=((double)(datasize*10))/baud;
126 // bitbang means 8 bits at once
127 plan=((double)datasize)/baud;
130 printf("this test should take %.2f seconds\n",plan);
132 // prepare data to send: 0 and 1 bits alternating (except for serial start/stopbit):
133 // maybe someone wants to look at this with a scope or logic analyzer
134 for (i=0; i<sizeof(txbuf); i++)
136 if (test_mode==BITMODE_RESET)
139 txbuf[i]=(i%2) ? 0xff : 0;
142 if(test_mode==BITMODE_SYNCBB)
144 // clear the receive buffer before beginning
145 // will read only as much as available
146 ftdi_read_data(&ftdic, rxbuf, sizeof(rxbuf));
149 start=get_prec_time();
154 int sendsize=sizeof(txbuf);
155 if (i+sendsize > datasize)
158 if ((sendsize=ftdi_write_data(&ftdic, txbuf, sendsize)) < 0)
160 fprintf(stderr,"write failed at %d: %s\n",
161 i, ftdi_get_error_string(&ftdic));
167 if(test_mode==BITMODE_SYNCBB)
169 // read everything available
170 ftdi_read_data(&ftdic, rxbuf, sizeof(rxbuf));
174 duration=get_prec_time()-start;
175 printf("and took %.4f seconds, this is factor %.2f\n",duration,plan/duration);
177 ftdi_usb_close(&ftdic);