Debian Codegraph @

examples/stream_test.c

raw symbols SCIP
1 /* stream_test.c
2 *
3 * Test reading from FT2232H in synchronous FIFO mode.
4 *
5 * The FT2232H must supply data due to an appropriate circuit
6 *
7 * To check for skipped block with appended code,
8 * a structure as follows is assumed
9 * 1* uint32_t num (incremented in 0x4000 steps)
10 * 3* uint32_t dont_care
11 *
12 * After start, data will be read in streaming until the program is aborted
13 * Progress information will be printed out
14 * If a filename is given on the command line, the data read will be
15 * written to that file
16 *
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <signal.h>
25 #include <errno.h>
26 #include <ftdi.h>cxx . . $ `<file>/src/ftdi.h`/
27 void check_outfilecxx . . $ check_outfile(fda57c8427c60377).(char *);
28
29 static FILEcxx . . $ FILE# *outputFilecxx . . $ outputFile.;
30
31 static int checkcxx . . $ check. = 1;
32 static int exitRequestedcxx . . $ exitRequested. = 0;
33 /*
34 * sigintHandler --
35 *
36 * SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
37 */
38
39 static void
40 sigintHandlercxx . . $ sigintHandler(d4f767463ce0a6b3).(int signum)
41 {
42 exitRequestedcxx . . $ exitRequested. = 1;
43 }
44
45 static void
46 usagecxx . . $ usage(85c52e162fed56f9).(const char *argv0)
47 {
48 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,
49 "Usage: %s [options...] \n"
50 "Test streaming read from FT2232H\n"
51 "[-P string] only look for product with given string\n"
52 "[-n] don't check for special block structure\n"
53 "\n"
54 "If some filename is given, write data read to that file\n"
55 "Progress information is printed each second\n"
56 "Abort with ^C\n"
57 "\n"
58 "Options:\n"
59 "\n"
60 "Copyright (C) 2009 Micah Dowty <[email protected]>\n"
61 "Adapted for use with libftdi (C) 2010 Uwe Bonnes <[email protected]>\n",
62 argv0);
63 exitcxx . . $ exit(3ca292c338df9bb3).(1);
64 }
65
66 static uint32_tcxx . . $ uint32_t# startcxx . . $ start. = 0;
67 static uint32_tcxx . . $ uint32_t# offsetcxx . . $ offset. = 0;
68 static uint64_tcxx . . $ uint64_t# blockscxx . . $ blocks. = 0;
69 static uint32_tcxx . . $ uint32_t# skipscxx . . $ skips. = 0;
70 static uint32_tcxx . . $ uint32_t# n_errcxx . . $ n_err. = 0;
71 static int
72 readCallbackcxx . . $ readCallback(93c707ed2ca71e6c).(uint8_tcxx . . $ uint8_t# *buffer, int length, FTDIProgressInfocxx . . $ FTDIProgressInfo# *progress, void *userdata)
73 {
74 if (length)
75 {
76 if (checkcxx . . $ check.)
77 {
78 int i,rem;
79 uint32_tcxx . . $ uint32_t# num;
80 for (i= offsetcxx . . $ offset.; i<length-16; i+=16)
81 {
82 num = *(uint32_tcxx . . $ uint32_t#*) (buffer+i);
83 if (startcxx . . $ start. && (num != startcxx . . $ start. +0x4000))
84 {
85 uint32_tcxx . . $ uint32_t# delta = ((num-startcxx . . $ start.)/0x4000)-1;
86 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Skip %7d blocks from 0x%08x to 0x%08x at blocks %10llu\n",
87 delta, startcxx . . $ start. -0x4000, num, (unsigned long long)blockscxx . . $ blocks.);
88 n_errcxx . . $ n_err.++;
89 skipscxx . . $ skips. += delta;
90 }
91 blockscxx . . $ blocks. ++;
92 startcxx . . $ start. = num;
93 }
94 rem = length -i;
95 if (rem >3)
96 {
97 num = *(uint32_tcxx . . $ uint32_t#*) (buffer+i);
98 if (startcxx . . $ start. && (num != startcxx . . $ start. +0x4000))
99 {
100 uint32_tcxx . . $ uint32_t# delta = ((num-startcxx . . $ start.)/0x4000)-1;
101 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Skip %7d blocks from 0x%08x to 0x%08x at blocks %10llu\n",
102 delta, startcxx . . $ start. -0x4000, num, (unsigned long long) blockscxx . . $ blocks.);
103 n_errcxx . . $ n_err.++;
104 skipscxx . . $ skips. += delta;
105 }
106 startcxx . . $ start. = num;
107 }
108 else if (rem)
109 startcxx . . $ start. += 0x4000;
110 if (rem != 0)
111 {
112 blockscxx . . $ blocks. ++;
113 offsetcxx . . $ offset. = 16-rem;
114 }
115 }
116 if (outputFilecxx . . $ outputFile.)
117 {
118 if (fwritecxx . . $ fwrite(ab0609fa07c47d6e).(buffer, length, 1, outputFilecxx . . $ outputFile.) != 1)
119 {
120 perrorcxx . . $ perror(85c52e162fed56f9).("Write error");
121 return 1;
122 }
123 }
124 }
125 if (progress)
126 {
127 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%10.02fs total time %9.3f MiB captured %7.1f kB/s curr rate %7.1f kB/s totalrate %d dropouts\n",
128 progress->totalTimecxx . . $ $anonymous_type_5ca5afed695a70e1_0#totalTime.,
129 progress->currentcxx . . $ $anonymous_type_5ca5afed695a70e1_0#current..totalBytescxx . . $ size_and_time#totalBytes. / (1024.0 * 1024.0),
130 progress->currentRatecxx . . $ $anonymous_type_5ca5afed695a70e1_0#currentRate. / 1024.0,
131 progress->totalRatecxx . . $ $anonymous_type_5ca5afed695a70e1_0#totalRate. / 1024.0,
132 n_errcxx . . $ n_err.);
133 }
134 return exitRequestedcxx . . $ exitRequested. ? 1 : 0;
135 }
136
137 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv)
138 {
139 struct ftdi_contextcxx . . $ ftdi_context# *ftdi;
140 int err, c;
141 FILEcxx . . $ FILE# *of = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
142 char const *outfile = 0;
143 outputFilecxx . . $ outputFile. =0;
144 exitRequestedcxx . . $ exitRequested. = 0;
145 char *descstring = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
146 int option_index;
147 static struct optioncxx . . $ option# long_options[] = {{NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!},};
148
149 while ((c = getopt_longcxx . . $ getopt_long(41f665c090816971).(argc, argv, "P:n", long_options, &option_index)) !=- 1)
150 switch (c)
151 {
152 case -1:
153 break;
154 case 'P':
155 descstring = optargcxx . . $ optarg.;
156 break;
157 case 'n':
158 checkcxx . . $ check. = 0;
159 break;
160 default:
161 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
162 }
163
164 if (optindcxx . . $ optind. == argc - 1)
165 {
166 // Exactly one extra argument- a dump file
167 outfile = argv[optindcxx . . $ optind.];
168 }
169 else if (optindcxx . . $ optind. < argc)
170 {
171 // Too many extra args
172 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
173 }
174
175 if ((ftdi = ftdi_newcxx . . $ ftdi_new(207ff50971a1b30).()) == 0)
176 {
177 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ftdi_new failed\n");
178 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
179 }
180
181 if (ftdi_set_interfacecxx . . $ ftdi_set_interface(73d9956508f3a98f).(ftdi, INTERFACE_Acxx . . $ INTERFACE_A.) < 0)
182 {
183 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ftdi_set_interface failed\n");
184 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdi);
185 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
186 }
187
188 if (ftdi_usb_open_desccxx . . $ ftdi_usb_open_desc(667eff1aa87ee6b2).(ftdi, 0x0403, 0x6010, descstring, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) < 0)
189 {
190 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));
191 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdi);
192 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
193 }
194
195 /* A timeout value of 1 results in may skipped blocks */
196 if(ftdi_set_latency_timercxx . . $ ftdi_set_latency_timer(2eccf738de101a49).(ftdi, 2))
197 {
198 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't set latency, Error %s\n",ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
199 ftdi_usb_closecxx . . $ ftdi_usb_close(e7991dd2c21df3a2).(ftdi);
200 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdi);
201 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
202 }
203
204 /* if(ftdi_usb_purge_rx_buffer(ftdi) < 0)
205 {
206 fprintf(stderr,"Can't rx purge\n",ftdi_get_error_string(ftdi));
207 return EXIT_FAILURE;
208 }*/
209 if (outfile)
210 if ((of = fopencxx . . $ fopen(82ec0787cd37e0a3).(outfile,"w+")) == 0)
211 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't open logfile %s, Error %s\n", outfile, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
212 if (of)
213 if (setvbufcxx . . $ setvbuf(4da3ba8c983f31bc).(of, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, _IOFBFcxx . . $ `<external>/232824719582829767/stdio.h:98:9`! , 1<<16) == 0)
214 outputFilecxx . . $ outputFile. = of;
215 signalcxx . . $ signal(c2d516773f91faf8).(SIGINTcxx . . $ `<external>/11773118241360591791/signum-generic.h:48:9`!, sigintHandlercxx . . $ sigintHandler(d4f767463ce0a6b3).);
216
217 err = ftdi_readstreamcxx . . $ ftdi_readstream(6585c5a6b0d5e143).(ftdi, readCallbackcxx . . $ readCallback(93c707ed2ca71e6c)., NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, 8, 256);
218 if (err < 0 && !exitRequestedcxx . . $ exitRequested.)
219 exitcxx . . $ exit(3ca292c338df9bb3).(1);
220
221 if (outputFilecxx . . $ outputFile.) {
222 fclosecxx . . $ fclose(89bc1efcdac0ca22).(outputFilecxx . . $ outputFile.);
223 outputFilecxx . . $ outputFile. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
224 }
225 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Capture ended.\n");
226
227 if (ftdi_set_bitmodecxx . . $ ftdi_set_bitmode(fd17119b3c08153d).(ftdi, 0xff, BITMODE_RESETcxx . . $ BITMODE_RESET.) < 0)
228 {
229 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't set synchronous fifo mode, Error %s\n",ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
230 ftdi_usb_closecxx . . $ ftdi_usb_close(e7991dd2c21df3a2).(ftdi);
231 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdi);
232 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
233 }
234 ftdi_usb_closecxx . . $ ftdi_usb_close(e7991dd2c21df3a2).(ftdi);
235 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdi);
236 signalcxx . . $ signal(c2d516773f91faf8).(SIGINTcxx . . $ `<external>/11773118241360591791/signum-generic.h:48:9`!, SIG_DFLcxx . . $ `<external>/11773118241360591791/signum-generic.h:29:9`!);
237 if (checkcxx . . $ check. && outfile)
238 {
239 if ((outputFilecxx . . $ outputFile. = fopencxx . . $ fopen(82ec0787cd37e0a3).(outfile,"r")) == 0)
240 {
241 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't open logfile %s, Error %s\n", outfile, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
242 ftdi_usb_closecxx . . $ ftdi_usb_close(e7991dd2c21df3a2).(ftdi);
243 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdi);
244 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
245 }
246 check_outfilecxx . . $ check_outfile(fda57c8427c60377).(descstring);
247 fclosecxx . . $ fclose(89bc1efcdac0ca22).(outputFilecxx . . $ outputFile.);
248 }
249 else if (checkcxx . . $ check.)
250 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"%d errors of %llu blocks (%Le), %d (%Le) blocks skipped\n",
251 n_errcxx . . $ n_err., (unsigned long long) blockscxx . . $ blocks., (long double)n_errcxx . . $ n_err./(long double) blockscxx . . $ blocks.,
252 skipscxx . . $ skips., (long double)skipscxx . . $ skips./(long double) blockscxx . . $ blocks.);
253 exitcxx . . $ exit(3ca292c338df9bb3). (0);
254 }
255
256 void check_outfilecxx . . $ check_outfile(fda57c8427c60377).(char *descstring)
257 {
258 if(strcmpcxx . . $ strcmp(5ae29d726ae57f88).(descstring,"FT2232HTEST") == 0)
259 {
260 char buf0[1024];
261 char buf1[1024];
262 char bufr[1024];
263 char *pa, *pb, *pc;
264 unsigned int num_lines = 0, line_num = 1;
265 int err_count = 0;
266 unsigned int num_start, num_end;
267
268 pa = buf0;
269 pb = buf1;
270 pc = buf0;
271 if(fgetscxx . . $ fgets(efb0aaba7ac866ae).(pa, 1023, outputFilecxx . . $ outputFile.) == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
272 {
273 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Empty output file\n");
274 return;
275 }
276 while(fgetscxx . . $ fgets(efb0aaba7ac866ae).(pb, 1023, outputFilecxx . . $ outputFile.) != NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
277 {
278 num_lines++;
279 unsigned int num_save = num_start;
280 if( sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(pa,"%6u%94s%6u",&num_start, bufr,&num_end) !=3)
281 {
282 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!,"Format doesn't match at line %8d \"%s",
283 num_lines, pa);
284 err_count++;
285 line_num = num_save +2;
286 }
287 else
288 {
289 if ((num_start+1)%100000 != num_end)
290 {
291 if (err_count < 20)
292 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!,"Malformed line %d \"%s\"\n",
293 num_lines, pa);
294 err_count++;
295 }
296 else if(num_start != line_num)
297 {
298 if (err_count < 20)
299 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!,"Skipping from %d to %d\n",
300 line_num, num_start);
301 err_count++;
302
303 }
304 line_num = num_end;
305 }
306 pa = pb;
307 pb = pc;
308 pc = pa;
309 }
310 if(err_count)
311 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!,"\n%d errors of %d data sets %f\n", err_count, num_lines, (double) err_count/(double)num_lines);
312 else
313 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!,"No errors for %d lines\n",num_lines);
314 }
315 else if(strcmpcxx . . $ strcmp(5ae29d726ae57f88).(descstring,"LLBBC10") == 0)
316 {
317 uint32_tcxx . . $ uint32_t# block0[4];
318 uint32_tcxx . . $ uint32_t# block1[4];
319 uint32_tcxx . . $ uint32_t# *pa = block0;
320 uint32_tcxx . . $ uint32_t# *pb = block1;
321 uint32_tcxx . . $ uint32_t# *pc = block0;
322 uint32_tcxx . . $ uint32_t# start= 0;
323 uint32_tcxx . . $ uint32_t# nread = 0;
324 int n_shown = 0;
325 int n_errors = 0;
326 if (freadcxx . . $ fread(738fc8ac1d711e58).(pa, sizeof(uint32_tcxx . . $ uint32_t#), 4,outputFilecxx . . $ outputFile.) < 4)
327 {
328 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Empty result file\n");
329 return;
330 }
331 while(freadcxx . . $ fread(738fc8ac1d711e58).(pb, sizeof(uint32_tcxx . . $ uint32_t#), 4,outputFilecxx . . $ outputFile.) != 0)
332 {
333 blockscxx . . $ blocks.++;
334 nread = pa[0];
335 if(start>0 && (nread != start))
336 {
337 if(n_shown < 30)
338 {
339 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Skip %7d blocks from 0x%08x to 0x%08x at blocks %10llu \n",
340 (nread-start)/0x4000, start -0x4000, nread, (unsigned long long) blockscxx . . $ blocks.);
341 n_shown ++;
342 }
343 n_errors++;
344 }
345 else if (n_shown >0)
346 n_shown--;
347 start = nread + 0x4000;
348 pa = pb;
349 pb = pc;
350 pc = pa;
351 }
352 if(n_errors)
353 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%d blocks wrong from %llu blocks read\n",
354 n_errors, (unsigned long long) blockscxx . . $ blocks.);
355 else
356 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%llu blocks all fine\n", (unsigned long long) blockscxx . . $ blocks.);
357 }
358 }