Debian Codegraph @

examples/baud_test.c

raw symbols SCIP
1 /* baud_test.c
2 *
3 * test setting the baudrate and compare it with the expected runtime
4 *
5 * options:
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
16 * -c <chunksize>
17 *
18 * (C) 2009 by Gerd v. Egidy <[email protected]>
19 *
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.
24
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.
29 *
30 */
31
32 #include <sys/time.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <ftdi.h>cxx . . $ `<file>/src/ftdi.h`/
37
38 double get_prec_timecxx . . $ get_prec_time(b7b5bf912346b3df).()
39 {
40 struct timevalcxx . . $ timeval# tv;
41 double res;
42
43 gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&tv,NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!);
44
45 res=tv.tv_seccxx . . $ timeval#tv_sec.;
46 res+=((double)tv.tv_useccxx . . $ timeval#tv_usec./1000000);
47
48 return res;
49 }
50
51 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv)
52 {
53 struct ftdi_contextcxx . . $ ftdi_context# *ftdi;
54 int i, t;
55 unsigned char *txbuf;
56 unsigned char *rxbuf;
57 double start, duration, plan;
58 int retval= 0;
59
60 // default values
61 int baud=9600;
62 int set_baud;
63 int datasize=100000;
64
65 char default_devicedesc[] = "i:0x0403:0x6001";
66 char *devicedesc=default_devicedesc;
67 int txchunksize=256;
68 enum ftdi_mpsse_modecxx . . $ ftdi_mpsse_mode# test_mode=BITMODE_BITBANGcxx . . $ BITMODE_BITBANG.;
69
70 while ((t = getoptcxx . . $ getopt(2483c31ff9bab884). (argc, argv, "b:d:p:m:c:")) != -1)
71 {
72 switch (t)
73 {
74 case 'd':
75 datasize = atoicxx . . $ atoi(5c4306d8553b3696). (optargcxx . . $ optarg.);
76 break;
77 case 'm':
78 switch (*optargcxx . . $ optarg.)
79 {
80 case 'r':
81 // serial
82 test_mode=BITMODE_RESETcxx . . $ BITMODE_RESET.;
83 break;
84 case 'a':
85 // async
86 test_mode=BITMODE_BITBANGcxx . . $ BITMODE_BITBANG.;
87 break;
88 case 's':
89 // sync
90 test_mode=BITMODE_SYNCBBcxx . . $ BITMODE_SYNCBB.;
91 break;
92 }
93 break;
94 case 'b':
95 baud = atoicxx . . $ atoi(5c4306d8553b3696). (optargcxx . . $ optarg.);
96 break;
97 case 'p':
98 devicedesc=optargcxx . . $ optarg.;
99 break;
100 case 'c':
101 txchunksize = atoicxx . . $ atoi(5c4306d8553b3696). (optargcxx . . $ optarg.);
102 break;
103 }
104 }
105
106 txbuf=malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(txchunksize);
107 rxbuf=malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(txchunksize);
108 if (txbuf == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`! || rxbuf == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
109 {
110 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "can't malloc\n");
111 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
112 }
113
114 if ((ftdi = ftdi_newcxx . . $ ftdi_new(207ff50971a1b30).()) == 0)
115 {
116 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ftdi_new failed\n");
117 retval = EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
118 goto done;
119 }
120
121 if (ftdi_usb_open_stringcxx . . $ ftdi_usb_open_string(4cc73c67245670e5).(ftdi, devicedesc) < 0)
122 {
123 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't open ftdi device: %s\n",ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
124 retval = EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
125 goto do_deinit;
126 }
127
128 set_baud=baud;
129 if (test_mode!=BITMODE_RESETcxx . . $ BITMODE_RESET.)
130 {
131 // we do bitbang, so real baudrate / 16
132 set_baud=baud/16;
133 }
134
135 ftdi_set_baudratecxx . . $ ftdi_set_baudrate(b8d6b357325709d2).(ftdi,set_baud);
136 printfcxx . . $ printf(581fddd72a8db8fc).("real baudrate used: %d\n",(test_mode==BITMODE_RESETcxx . . $ BITMODE_RESET.) ? ftdi->baudratecxx . . $ ftdi_context#baudrate. : ftdi->baudratecxx . . $ ftdi_context#baudrate.*16);
137
138 if (ftdi_set_bitmodecxx . . $ ftdi_set_bitmode(fd17119b3c08153d).(ftdi, 0xFF,test_mode) < 0)
139 {
140 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't set mode: %s\n",ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
141 retval = EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
142 goto do_close;
143 }
144
145 if (test_mode==BITMODE_RESETcxx . . $ BITMODE_RESET.)
146 {
147 // serial 8N1: 8 data bits, 1 startbit, 1 stopbit
148 plan=((double)(datasize*10))/baud;
149 }
150 else
151 {
152 // bitbang means 8 bits at once
153 plan=((double)datasize)/baud;
154 }
155
156 printfcxx . . $ printf(581fddd72a8db8fc).("this test should take %.2f seconds\n",plan);
157
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++)
161 {
162 if (test_mode==BITMODE_RESETcxx . . $ BITMODE_RESET.)
163 txbuf[i]=0xAA;
164 else
165 txbuf[i]=(i%2) ? 0xff : 0;
166 }
167
168 if (ftdi_write_data_set_chunksizecxx . . $ ftdi_write_data_set_chunksize(73cd79891ac8c192).(ftdi, txchunksize) < 0 ||
169 ftdi_read_data_set_chunksizecxx . . $ ftdi_read_data_set_chunksize(73cd79891ac8c192).(ftdi, txchunksize) < 0)
170 {
171 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't set chunksize: %s\n",ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
172 retval = EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
173 goto do_close;
174 }
175
176 if (test_mode==BITMODE_SYNCBBcxx . . $ BITMODE_SYNCBB.)
177 {
178 // completely clear the receive buffer before beginning
179 while (ftdi_read_datacxx . . $ ftdi_read_data(d0399952b505fffc).(ftdi, rxbuf, txchunksize)>0);
180 }
181
182 start=get_prec_timecxx . . $ get_prec_time(b7b5bf912346b3df).();
183
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_timeoutcxx . . $ ftdi_context#usb_read_timeout.=1;
187
188 i=0;
189 while (i < datasize)
190 {
191 int sendsize=txchunksize;
192 if (i+sendsize > datasize)
193 sendsize=datasize-i;
194
195 if ((sendsize=ftdi_write_datacxx . . $ ftdi_write_data(8bb9860f18916ed8).(ftdi, txbuf, sendsize)) < 0)
196 {
197 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"write failed at %d: %s\n",
198 i, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
199 retval = EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
200 goto do_close;
201 }
202
203 i+=sendsize;
204
205 if (test_mode==BITMODE_SYNCBBcxx . . $ BITMODE_SYNCBB.)
206 {
207 // read the same amount of data as sent
208 ftdi_read_datacxx . . $ ftdi_read_data(d0399952b505fffc).(ftdi, rxbuf, sendsize);
209 }
210 }
211
212 duration=get_prec_timecxx . . $ get_prec_time(b7b5bf912346b3df).()-start;
213 printfcxx . . $ printf(581fddd72a8db8fc).("and took %.4f seconds, this is %.0f baud or factor %.3f\n",duration,(plan*baud)/duration,plan/duration);
214 do_close:
215 ftdi_usb_closecxx . . $ ftdi_usb_close(e7991dd2c21df3a2).(ftdi);
216 do_deinit:
217 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdi);
218 done:
219 if(rxbuf)
220 freecxx . . $ free(5d22bdacc48458e8).(rxbuf);
221 if(txbuf)
222 freecxx . . $ free(5d22bdacc48458e8).(txbuf);
223 exitcxx . . $ exit(3ca292c338df9bb3). (retval);
224 }