3 * Test reading from FT2232H in synchronous FIFO mode.
5 * The FT2232H must supply data due to an appropriate circuit
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
12 * After start, data will be read in streaming until the program is aborted
13 * Progess information wil be printed out
14 * If a filename is given on the command line, the data read will be
15 * written to that file
27 void check_outfile(char *);
29 static FILE *outputFile;
32 static int exitRequested = 0;
36 * SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
40 sigintHandler(int signum)
46 usage(const char *argv0)
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"
54 "If some filename is given, write data read to that file\n"
55 "Progess information is printed each second\n"
60 "Copyright (C) 2009 Micah Dowty <micah@navi.cx>\n"
61 "Adapted for use with libftdi (C) 2010 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>\n",
66 static uint32_t start = 0;
67 static uint32_t offset = 0;
68 static uint64_t blocks = 0;
69 static uint32_t skips = 0;
70 static uint32_t n_err = 0;
72 readCallback(uint8_t *buffer, int length, FTDIProgressInfo *progress, void *userdata)
80 for (i= offset; i<length-16; i+=16)
82 num = *(uint32_t*) (buffer+i);
83 if (start && (num != start +0x4000))
85 uint32_t delta = ((num-start)/0x4000)-1;
86 fprintf(stderr, "Skip %7d blocks from 0x%08x to 0x%08x at blocks %10ld \n",
87 delta, start -0x4000, num, blocks);
97 num = *(uint32_t*) (buffer+i);
98 if (start && (num != start +0x4000))
100 uint32_t delta = ((num-start)/0x4000)-1;
101 fprintf(stderr, "Skip %7d blocks from 0x%08x to 0x%08x at blocks %10ld \n",
102 delta, start -0x4000, num, blocks);
118 if (fwrite(buffer, length, 1, outputFile) != 1)
120 perror("Write error");
127 fprintf(stderr, "%10.02fs total time %9.3f MiB captured %7.1f kB/s curr rate %7.1f kB/s totalrate %d dropouts\n",
129 progress->current.totalBytes / (1024.0 * 1024.0),
130 progress->currentRate / 1024.0,
131 progress->totalRate / 1024.0,
134 return exitRequested ? 1 : 0;
137 int main(int argc, char **argv)
139 struct ftdi_context ftdic;
142 char const *outfile = 0;
145 char *descstring = NULL;
147 static struct option long_options[] = {{NULL},};
149 while ((c = getopt_long(argc, argv, "P:n", long_options, &option_index)) !=- 1)
164 if (optind == argc - 1)
166 // Exactly one extra argument- a dump file
167 outfile = argv[optind];
169 else if (optind < argc)
171 // Too many extra args
175 if (ftdi_init(&ftdic) < 0)
177 fprintf(stderr, "ftdi_init failed\n");
181 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
183 fprintf(stderr, "ftdi_set_interface failed\n");
187 if (ftdi_usb_open_desc(&ftdic, 0x0403, 0x6010, descstring, NULL) < 0)
189 fprintf(stderr,"Can't open ftdi device: %s\n",ftdi_get_error_string(&ftdic));
193 /* A timeout value of 1 results in may skipped blocks */
194 if(ftdi_set_latency_timer(&ftdic, 2))
196 fprintf(stderr,"Can't set latency, Error %s\n",ftdi_get_error_string(&ftdic));
200 /* if(ftdi_usb_purge_rx_buffer(&ftdic) < 0)
202 fprintf(stderr,"Can't rx purge\n",ftdi_get_error_string(&ftdic));
206 if ((of = fopen(outfile,"w+")) == 0)
207 fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno));
209 if (setvbuf(of, NULL, _IOFBF , 1<<16) == 0)
211 signal(SIGINT, sigintHandler);
213 err = ftdi_readstream(&ftdic, readCallback, NULL, 8, 256);
214 if (err < 0 && !exitRequested)
221 fprintf(stderr, "Capture ended.\n");
223 if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_RESET) < 0)
225 fprintf(stderr,"Can't set synchronous fifo mode, Error %s\n",ftdi_get_error_string(&ftdic));
228 ftdi_usb_close(&ftdic);
230 signal(SIGINT, SIG_DFL);
231 if (check && outfile)
233 if ((outputFile = fopen(outfile,"r")) == 0)
235 fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno));
238 check_outfile(descstring);
242 fprintf(stderr,"%d errors of %ld blocks (%Le), %d (%Le) blocks skipped\n",
243 n_err, blocks, (long double)n_err/(long double) blocks,
244 skips, (long double)skips/(long double) blocks);
248 void check_outfile(char *descstring)
250 if(strcmp(descstring,"FT2232HTEST") == 0)
256 unsigned int num_lines = 0, line_num = 1;
258 unsigned int num_start, num_end;
263 if(fgets(pa, 1023, outputFile) == NULL)
265 fprintf(stderr,"Empty output file\n");
268 while(fgets(pb, 1023, outputFile) != NULL)
271 unsigned int num_save = num_start;
272 if( sscanf(pa,"%6u%94s%6u",&num_start, bufr,&num_end) !=3)
274 fprintf(stdout,"Format doesn't match at line %8d \"%s",
277 line_num = num_save +2;
281 if ((num_start+1)%100000 != num_end)
284 fprintf(stdout,"Malformed line %d \"%s\"\n",
288 else if(num_start != line_num)
291 fprintf(stdout,"Skipping from %d to %d\n",
292 line_num, num_start);
303 fprintf(stdout,"\n%d errors of %d data sets %f\n", err_count, num_lines, (double) err_count/(double)num_lines);
305 fprintf(stdout,"No errors for %d lines\n",num_lines);
307 else if(strcmp(descstring,"LLBBC10") == 0)
311 uint32_t *pa = block0;
312 uint32_t *pb = block1;
313 uint32_t *pc = block0;
318 if (fread(pa, sizeof(uint32_t), 4,outputFile) < 4)
320 fprintf(stderr,"Empty result file\n");
323 while(fread(pb, sizeof(uint32_t), 4,outputFile) != 0)
327 if(start>0 && (nread != start))
331 fprintf(stderr, "Skip %7d blocks from 0x%08x to 0x%08x at blocks %10ld \n",
332 (nread-start)/0x4000, start -0x4000, nread, blocks);
339 start = nread + 0x4000;
345 fprintf(stderr, "%d blocks wrong from %ld blocks read\n",
348 fprintf(stderr, "%ld blocks all fine\n",blocks);