From: Thomas Jarosch Date: Mon, 4 Oct 2010 15:49:14 +0000 (+0200) Subject: Reformat example code to match libftdi style: X-Git-Tag: v1.0rc1~133^2~27 X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=commitdiff_plain;h=05c2e40a3c720c63a75d36a33d0d5de0677f8f57 Reformat example code to match libftdi style: /usr/bin/astyle --indent=spaces=4 --indent-switches --brackets=break \ --convert-tabs --one-line=keep-statements --one-line=keep-blocks \ $* --- diff --git a/examples/baud_test.c b/examples/baud_test.c index b53031b..d8d4886 100644 --- a/examples/baud_test.c +++ b/examples/baud_test.c @@ -12,7 +12,7 @@ * s::: first device with given vendor id, product id and serial string * -d * -b (divides by 16 if bitbang as taken from the ftdi datasheets) - * -m r: serial a: async bitbang s:sync bitbang + * -m r: serial a: async bitbang s:sync bitbang * -c * * (C) 2009 by Gerd v. Egidy @@ -39,7 +39,7 @@ double get_prec_time() { struct timeval tv; double res; - + gettimeofday(&tv,NULL); res=tv.tv_sec; @@ -65,7 +65,7 @@ int main(int argc, char **argv) char *devicedesc=default_devicedesc; int txchunksize=256; enum ftdi_mpsse_mode test_mode=BITMODE_BITBANG; - + while ((t = getopt (argc, argv, "b:d:p:m:c:")) != -1) { switch (t) @@ -74,7 +74,7 @@ int main(int argc, char **argv) datasize = atoi (optarg); break; case 'm': - switch(*optarg) + switch (*optarg) { case 'r': // serial @@ -162,16 +162,16 @@ int main(int argc, char **argv) } if (ftdi_write_data_set_chunksize(&ftdic, txchunksize) < 0 || - ftdi_read_data_set_chunksize(&ftdic, txchunksize) < 0) + ftdi_read_data_set_chunksize(&ftdic, txchunksize) < 0) { fprintf(stderr,"Can't set chunksize: %s\n",ftdi_get_error_string(&ftdic)); return EXIT_FAILURE; } - if(test_mode==BITMODE_SYNCBB) + if (test_mode==BITMODE_SYNCBB) { // completely clear the receive buffer before beginning - while(ftdi_read_data(&ftdic, rxbuf, txchunksize)>0); + while (ftdi_read_data(&ftdic, rxbuf, txchunksize)>0); } start=get_prec_time(); @@ -181,7 +181,7 @@ int main(int argc, char **argv) ftdic.usb_read_timeout=1; i=0; - while(i < datasize) + while (i < datasize) { int sendsize=txchunksize; if (i+sendsize > datasize) @@ -196,7 +196,7 @@ int main(int argc, char **argv) i+=sendsize; - if(test_mode==BITMODE_SYNCBB) + if (test_mode==BITMODE_SYNCBB) { // read the same amount of data as sent ftdi_read_data(&ftdic, rxbuf, sendsize); diff --git a/examples/eeprom.c b/examples/eeprom.c index 3350fa3..e2c8052 100644 --- a/examples/eeprom.c +++ b/examples/eeprom.c @@ -29,8 +29,8 @@ int main(int argc, char **argv) if ((ftdi = ftdi_new()) == 0) { - fprintf(stderr, "Failed to allocate ftdi structure :%s \n", - ftdi_get_error_string(ftdi)); + fprintf(stderr, "Failed to allocate ftdi structure :%s \n", + ftdi_get_error_string(ftdi)); return EXIT_FAILURE; } @@ -38,43 +38,43 @@ int main(int argc, char **argv) { switch (i) { - case 'd': - use_defaults = 1; - if(optarg) - large_chip = 0x66; - break; - case 'e': - erase = 1; - break; - case 'v': - vid = strtoul(optarg, NULL, 0); - break; - case 'p': - pid = strtoul(optarg, NULL, 0); - break; - case 'P': + case 'd': + use_defaults = 1; + if (optarg) + large_chip = 0x66; + break; + case 'e': + erase = 1; + break; + case 'v': + vid = strtoul(optarg, NULL, 0); + break; + case 'p': + pid = strtoul(optarg, NULL, 0); + break; + case 'P': desc = optarg; - break; - case 'S': - serial = optarg; - break; - case 'w': - do_write = 1; - break; - default: - fprintf(stderr, "usage: %s [options]\n", *argv); - fprintf(stderr, "\t-d[num] Work with default valuesfor 128 Byte " + break; + case 'S': + serial = optarg; + break; + case 'w': + do_write = 1; + break; + default: + fprintf(stderr, "usage: %s [options]\n", *argv); + fprintf(stderr, "\t-d[num] Work with default valuesfor 128 Byte " "EEPROM or for 256 Byte EEPROM if some [num] is given\n"); - fprintf(stderr, "\t-w write\n"); - fprintf(stderr, "\t-e erase\n"); - fprintf(stderr, "\t-v verbose decoding\n"); - fprintf(stderr, "\t-p Search for device with PID == number\n"); - fprintf(stderr, "\t-v Search for device with VID == number\n"); - fprintf(stderr, "\t-P Search for device with PID == number\n"); + fprintf(stderr, "\t-v Search for device with VID == number\n"); + fprintf(stderr, "\t-P = 0) { - fprintf(stderr, "read %d bytes\n", f); - fwrite(buf, f, 1, stdout); - fflush(stderr); - fflush(stdout); + while ((f = ftdi_read_data(&ftdic, buf, sizeof(buf))) >= 0) + { + fprintf(stderr, "read %d bytes\n", f); + fwrite(buf, f, 1, stdout); + fflush(stderr); + fflush(stdout); } ftdi_usb_close(&ftdic); diff --git a/examples/stream_test.c b/examples/stream_test.c index f8d024e..47f2f39 100644 --- a/examples/stream_test.c +++ b/examples/stream_test.c @@ -4,7 +4,7 @@ * * The FT2232H must supply data due to an appropriate circuit * - * To check for skipped block with appended code, + * To check for skipped block with appended code, * a structure as follows is assumed * 1* uint32_t num (incremented in 0x4000 steps) * 3* uint32_t dont_care @@ -39,28 +39,28 @@ static int exitRequested = 0; static void sigintHandler(int signum) { - exitRequested = 1; + exitRequested = 1; } static void usage(const char *argv0) { - fprintf(stderr, - "Usage: %s [options...] \n" - "Test streaming read from FT2232H\n" - "[-P string] only look for product with given string\n" - "[-n] don't check for special block structure\n" - "\n" - "If some filename is given, write data read to that file\n" - "Progess information is printed each second\n" - "Abort with ^C\n" - "\n" - "Options:\n" - "\n" - "Copyright (C) 2009 Micah Dowty \n" - "Adapted for use with libftdi (C) 2010 Uwe Bonnes \n", - argv0); - exit(1); + fprintf(stderr, + "Usage: %s [options...] \n" + "Test streaming read from FT2232H\n" + "[-P string] only look for product with given string\n" + "[-n] don't check for special block structure\n" + "\n" + "If some filename is given, write data read to that file\n" + "Progess information is printed each second\n" + "Abort with ^C\n" + "\n" + "Options:\n" + "\n" + "Copyright (C) 2009 Micah Dowty \n" + "Adapted for use with libftdi (C) 2010 Uwe Bonnes \n", + argv0); + exit(1); } static uint32_t start = 0; @@ -71,241 +71,242 @@ static uint32_t n_err = 0; static int readCallback(uint8_t *buffer, int length, FTDIProgressInfo *progress, void *userdata) { - if (length) - { - if (check) - { - int i,rem; - uint32_t num; - for (i= offset; i3) - { - num = *(uint32_t*) (buffer+i); - if (start && (num != start +0x4000)) - { - uint32_t delta = ((num-start)/0x4000)-1; - fprintf(stderr, "Skip %7d blocks from 0x%08x to 0x%08x at blocks %10llu\n", - delta, start -0x4000, num, (unsigned long long) blocks); - n_err++; - skips += delta; - } - start = num; - } - else if (rem) - start += 0x4000; - if (rem != 0); - { - blocks ++; - offset = 16-rem; - } - } - if (outputFile) - { - if (fwrite(buffer, length, 1, outputFile) != 1) - { - perror("Write error"); - return 1; - } - } - } - if (progress) - { - fprintf(stderr, "%10.02fs total time %9.3f MiB captured %7.1f kB/s curr rate %7.1f kB/s totalrate %d dropouts\n", - progress->totalTime, - progress->current.totalBytes / (1024.0 * 1024.0), - progress->currentRate / 1024.0, - progress->totalRate / 1024.0, - n_err); - } - return exitRequested ? 1 : 0; + if (length) + { + if (check) + { + int i,rem; + uint32_t num; + for (i= offset; i3) + { + num = *(uint32_t*) (buffer+i); + if (start && (num != start +0x4000)) + { + uint32_t delta = ((num-start)/0x4000)-1; + fprintf(stderr, "Skip %7d blocks from 0x%08x to 0x%08x at blocks %10llu\n", + delta, start -0x4000, num, (unsigned long long) blocks); + n_err++; + skips += delta; + } + start = num; + } + else if (rem) + start += 0x4000; + if (rem != 0); + { + blocks ++; + offset = 16-rem; + } + } + if (outputFile) + { + if (fwrite(buffer, length, 1, outputFile) != 1) + { + perror("Write error"); + return 1; + } + } + } + if (progress) + { + fprintf(stderr, "%10.02fs total time %9.3f MiB captured %7.1f kB/s curr rate %7.1f kB/s totalrate %d dropouts\n", + progress->totalTime, + progress->current.totalBytes / (1024.0 * 1024.0), + progress->currentRate / 1024.0, + progress->totalRate / 1024.0, + n_err); + } + return exitRequested ? 1 : 0; } int main(int argc, char **argv) { - struct ftdi_context ftdic; - int err, c; - FILE *of = NULL; - char const *outfile = 0; - outputFile =0; - exitRequested = 0; - char *descstring = NULL; - int option_index; - static struct option long_options[] = {{NULL},}; + struct ftdi_context ftdic; + int err, c; + FILE *of = NULL; + char const *outfile = 0; + outputFile =0; + exitRequested = 0; + char *descstring = NULL; + int option_index; + static struct option long_options[] = {{NULL},}; - while ((c = getopt_long(argc, argv, "P:n", long_options, &option_index)) !=- 1) - switch (c) - { - case -1: - break; - case 'P': - descstring = optarg; - break; - case 'n': - check = 0; - break; - default: - usage(argv[0]); - } - - if (optind == argc - 1) - { - // Exactly one extra argument- a dump file - outfile = argv[optind]; - } - else if (optind < argc) - { - // Too many extra args - usage(argv[0]); - } - - if (ftdi_init(&ftdic) < 0) - { - fprintf(stderr, "ftdi_init failed\n"); - return EXIT_FAILURE; - } - - if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0) - { - fprintf(stderr, "ftdi_set_interface failed\n"); - return EXIT_FAILURE; - } - - if (ftdi_usb_open_desc(&ftdic, 0x0403, 0x6010, descstring, NULL) < 0) - { - fprintf(stderr,"Can't open ftdi device: %s\n",ftdi_get_error_string(&ftdic)); - return EXIT_FAILURE; - } - - /* A timeout value of 1 results in may skipped blocks */ - if(ftdi_set_latency_timer(&ftdic, 2)) - { - fprintf(stderr,"Can't set latency, Error %s\n",ftdi_get_error_string(&ftdic)); - return EXIT_FAILURE; - } - -/* if(ftdi_usb_purge_rx_buffer(&ftdic) < 0) - { - fprintf(stderr,"Can't rx purge\n",ftdi_get_error_string(&ftdic)); - return EXIT_FAILURE; - }*/ - if (outfile) - if ((of = fopen(outfile,"w+")) == 0) - fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno)); - if (of) - if (setvbuf(of, NULL, _IOFBF , 1<<16) == 0) - outputFile = of; - signal(SIGINT, sigintHandler); - - err = ftdi_readstream(&ftdic, readCallback, NULL, 8, 256); - if (err < 0 && !exitRequested) - exit(1); - - if (outputFile) { - fclose(outputFile); - outputFile = NULL; - } - fprintf(stderr, "Capture ended.\n"); - - if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_RESET) < 0) - { - fprintf(stderr,"Can't set synchronous fifo mode, Error %s\n",ftdi_get_error_string(&ftdic)); - return EXIT_FAILURE; - } - ftdi_usb_close(&ftdic); - ftdi_deinit(&ftdic); - signal(SIGINT, SIG_DFL); - if (check && outfile) - { - if ((outputFile = fopen(outfile,"r")) == 0) + while ((c = getopt_long(argc, argv, "P:n", long_options, &option_index)) !=- 1) + switch (c) + { + case -1: + break; + case 'P': + descstring = optarg; + break; + case 'n': + check = 0; + break; + default: + usage(argv[0]); + } + + if (optind == argc - 1) + { + // Exactly one extra argument- a dump file + outfile = argv[optind]; + } + else if (optind < argc) + { + // Too many extra args + usage(argv[0]); + } + + if (ftdi_init(&ftdic) < 0) + { + fprintf(stderr, "ftdi_init failed\n"); + return EXIT_FAILURE; + } + + if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0) + { + fprintf(stderr, "ftdi_set_interface failed\n"); + return EXIT_FAILURE; + } + + if (ftdi_usb_open_desc(&ftdic, 0x0403, 0x6010, descstring, NULL) < 0) + { + fprintf(stderr,"Can't open ftdi device: %s\n",ftdi_get_error_string(&ftdic)); + return EXIT_FAILURE; + } + + /* A timeout value of 1 results in may skipped blocks */ + if (ftdi_set_latency_timer(&ftdic, 2)) + { + fprintf(stderr,"Can't set latency, Error %s\n",ftdi_get_error_string(&ftdic)); + return EXIT_FAILURE; + } + + /* if(ftdi_usb_purge_rx_buffer(&ftdic) < 0) { - fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno)); + fprintf(stderr,"Can't rx purge\n",ftdi_get_error_string(&ftdic)); return EXIT_FAILURE; - } - check_outfile(descstring); - fclose(outputFile); - } - else if (check) - fprintf(stderr,"%d errors of %llu blocks (%Le), %d (%Le) blocks skipped\n", - n_err, (unsigned long long) blocks, (long double)n_err/(long double) blocks, - skips, (long double)skips/(long double) blocks); - exit (0); + }*/ + if (outfile) + if ((of = fopen(outfile,"w+")) == 0) + fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno)); + if (of) + if (setvbuf(of, NULL, _IOFBF , 1<<16) == 0) + outputFile = of; + signal(SIGINT, sigintHandler); + + err = ftdi_readstream(&ftdic, readCallback, NULL, 8, 256); + if (err < 0 && !exitRequested) + exit(1); + + if (outputFile) + { + fclose(outputFile); + outputFile = NULL; + } + fprintf(stderr, "Capture ended.\n"); + + if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_RESET) < 0) + { + fprintf(stderr,"Can't set synchronous fifo mode, Error %s\n",ftdi_get_error_string(&ftdic)); + return EXIT_FAILURE; + } + ftdi_usb_close(&ftdic); + ftdi_deinit(&ftdic); + signal(SIGINT, SIG_DFL); + if (check && outfile) + { + if ((outputFile = fopen(outfile,"r")) == 0) + { + fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno)); + return EXIT_FAILURE; + } + check_outfile(descstring); + fclose(outputFile); + } + else if (check) + fprintf(stderr,"%d errors of %llu blocks (%Le), %d (%Le) blocks skipped\n", + n_err, (unsigned long long) blocks, (long double)n_err/(long double) blocks, + skips, (long double)skips/(long double) blocks); + exit (0); } void check_outfile(char *descstring) { - if(strcmp(descstring,"FT2232HTEST") == 0) + if (strcmp(descstring,"FT2232HTEST") == 0) { - char buf0[1024]; - char buf1[1024]; - char bufr[1024]; - char *pa, *pb, *pc; - unsigned int num_lines = 0, line_num = 1; - int err_count = 0; - unsigned int num_start, num_end; + char buf0[1024]; + char buf1[1024]; + char bufr[1024]; + char *pa, *pb, *pc; + unsigned int num_lines = 0, line_num = 1; + int err_count = 0; + unsigned int num_start, num_end; - pa = buf0; - pb = buf1; - pc = buf0; - if(fgets(pa, 1023, outputFile) == NULL) - { - fprintf(stderr,"Empty output file\n"); - return; - } - while(fgets(pb, 1023, outputFile) != NULL) - { - num_lines++; - unsigned int num_save = num_start; - if( sscanf(pa,"%6u%94s%6u",&num_start, bufr,&num_end) !=3) - { - fprintf(stdout,"Format doesn't match at line %8d \"%s", - num_lines, pa); - err_count++; - line_num = num_save +2; - } - else - { - if ((num_start+1)%100000 != num_end) - { - if (err_count < 20) - fprintf(stdout,"Malformed line %d \"%s\"\n", - num_lines, pa); - err_count++; - } - else if(num_start != line_num) - { - if (err_count < 20) - fprintf(stdout,"Skipping from %d to %d\n", - line_num, num_start); - err_count++; - - } - line_num = num_end; - } - pa = pb; - pb = pc; - pc = pa; - } - if(err_count) - fprintf(stdout,"\n%d errors of %d data sets %f\n", err_count, num_lines, (double) err_count/(double)num_lines); - else - fprintf(stdout,"No errors for %d lines\n",num_lines); - } - else if(strcmp(descstring,"LLBBC10") == 0) - { + pa = buf0; + pb = buf1; + pc = buf0; + if (fgets(pa, 1023, outputFile) == NULL) + { + fprintf(stderr,"Empty output file\n"); + return; + } + while (fgets(pb, 1023, outputFile) != NULL) + { + num_lines++; + unsigned int num_save = num_start; + if ( sscanf(pa,"%6u%94s%6u",&num_start, bufr,&num_end) !=3) + { + fprintf(stdout,"Format doesn't match at line %8d \"%s", + num_lines, pa); + err_count++; + line_num = num_save +2; + } + else + { + if ((num_start+1)%100000 != num_end) + { + if (err_count < 20) + fprintf(stdout,"Malformed line %d \"%s\"\n", + num_lines, pa); + err_count++; + } + else if (num_start != line_num) + { + if (err_count < 20) + fprintf(stdout,"Skipping from %d to %d\n", + line_num, num_start); + err_count++; + + } + line_num = num_end; + } + pa = pb; + pb = pc; + pc = pa; + } + if (err_count) + fprintf(stdout,"\n%d errors of %d data sets %f\n", err_count, num_lines, (double) err_count/(double)num_lines); + else + fprintf(stdout,"No errors for %d lines\n",num_lines); + } + else if (strcmp(descstring,"LLBBC10") == 0) + { uint32_t block0[4]; uint32_t block1[4]; uint32_t *pa = block0; @@ -320,13 +321,13 @@ void check_outfile(char *descstring) fprintf(stderr,"Empty result file\n"); return; } - while(fread(pb, sizeof(uint32_t), 4,outputFile) != 0) + while (fread(pb, sizeof(uint32_t), 4,outputFile) != 0) { blocks++; nread = pa[0]; - if(start>0 && (nread != start)) + if (start>0 && (nread != start)) { - if(n_shown < 30) + if (n_shown < 30) { fprintf(stderr, "Skip %7d blocks from 0x%08x to 0x%08x at blocks %10llu \n", (nread-start)/0x4000, start -0x4000, nread, (unsigned long long) blocks); @@ -334,14 +335,14 @@ void check_outfile(char *descstring) } n_errors++; } - else if (n_shown >0) - n_shown--; + else if (n_shown >0) + n_shown--; start = nread + 0x4000; pa = pb; pb = pc; pc = pa; } - if(n_errors) + if (n_errors) fprintf(stderr, "%d blocks wrong from %llu blocks read\n", n_errors, (unsigned long long) blocks); else