From ad397a4bb9a5c258c8655e30f6856c0103d49a9e Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Mon, 9 Oct 2006 15:31:40 +0000 Subject: [PATCH] libftdi: (tomj) polished documentation, started "examples" dir --- Makefile.am | 2 +- configure.in | 4 +- doc/Doxyfile.in | 4 +- doc/Makefile.am | 2 +- examples/Makefile.am | 13 +++++++ examples/bitbang.c | 69 +++++++++++++++++++++++++++++++++++ examples/bitbang2.c | 79 ++++++++++++++++++++++++++++++++++++++++ examples/bitbang_ft2232.c | 87 +++++++++++++++++++++++++++++++++++++++++++++ examples/simple.c | 19 ++++++++++ src/ftdi.c | 8 +++-- 10 files changed, 278 insertions(+), 9 deletions(-) create mode 100644 examples/Makefile.am create mode 100644 examples/bitbang.c create mode 100644 examples/bitbang2.c create mode 100644 examples/bitbang_ft2232.c create mode 100644 examples/simple.c diff --git a/Makefile.am b/Makefile.am index 2687bf5..a3847e8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ # have all needed files, that a GNU package needs AUTOMAKE_OPTIONS = foreign 1.4 -SUBDIRS = src doc +SUBDIRS = src examples doc EXTRA_DIST = libftdi.spec COPYING.LIB README ChangeLog libftdi-config.in diff --git a/configure.in b/configure.in index 70acef7..27e9f4d 100644 --- a/configure.in +++ b/configure.in @@ -1,7 +1,7 @@ AC_INIT(configure.in) AM_CONFIG_HEADER(config.h) -AM_INIT_AUTOMAKE(libftdi, 0.7) +AM_INIT_AUTOMAKE(libftdi, 0.8) AC_LANG_C AC_PROG_CC @@ -37,4 +37,4 @@ AC_PATH_PROG(DOXYGEN, doxygen, $PATH) AM_CONDITIONAL(HAVE_DOXYGEN, test -n $DOXYGEN); AC_OUTPUT([libftdi-config],[chmod a+x libftdi-config]) -AC_OUTPUT(Makefile src/Makefile doc/Doxyfile doc/Makefile libftdi.pc) +AC_OUTPUT(Makefile src/Makefile examples/Makefile doc/Doxyfile doc/Makefile libftdi.pc) diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index 29d7544..f21766e 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -319,7 +319,7 @@ INLINE_INFO = YES # alphabetically by member name. If set to NO the members will appear in # declaration order. -SORT_MEMBER_DOCS = YES +SORT_MEMBER_DOCS = NO # If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the # brief documentation of file, namespace and class members alphabetically @@ -500,7 +500,7 @@ EXCLUDE_PATTERNS = # directories that contain example code fragments that are included (see # the \include command). -EXAMPLE_PATH = +EXAMPLE_PATH = ../examples # If the value of the EXAMPLE_PATH tag contains directories, you can use the # EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp diff --git a/doc/Makefile.am b/doc/Makefile.am index d482b04..f0f6850 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -8,5 +8,5 @@ endif all: $(MANUALS) -html/index.html: ../src/*.c ../src/*.h +html/index.html: ../src/*.c ../src/*.h ../examples/*.c $(DOXYGEN) diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..a11640a --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,13 @@ +METASOURCES = AUTO +INCLUDES = -I$(top_srcdir)/src +LDADD = $(top_builddir)/src/libftdi.la + +bin_PROGRAMS = simple \ + bitbang \ + bitbang2 \ + bitbang_ft2232 + +simple_SOURCES = simple.c +bitbang_SOURCES = bitbang.c +bitbang2_SOURCES = bitbang2.c +bitbang_ft2232_SOURCES = bitbang_ft2232.c diff --git a/examples/bitbang.c b/examples/bitbang.c new file mode 100644 index 0000000..1234852 --- /dev/null +++ b/examples/bitbang.c @@ -0,0 +1,69 @@ +/* This program is distributed under the GPL, version 2 */ + +#include +#include +#include + +int main(int argc, char **argv) +{ + struct ftdi_context ftdic; + int f,i; + + ftdi_init(&ftdic); + + f = ftdi_usb_open(&ftdic, 0x0403, 0x6001); + + if(f < 0 && f != -5) { + fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic)); + exit(-1); + } + + printf("ftdi open succeeded: %d\n",f); + + printf("enabling bitbang mode\n"); + ftdi_enable_bitbang(&ftdic, 0xFF); + + char buf[1]; + + sleep(3); + + buf[0] = 0x0; + printf("turning everything on\n"); + f = ftdi_write_data(&ftdic, buf, 1); + if(f < 0) { + fprintf(stderr,"write failed for 0x%x, error %d (%s)\n",buf[0],f, ftdi_get_error_string(&ftdic)); + } + + sleep(3); + + buf[0] = 0xFF; + printf("turning everything off\n"); + f = ftdi_write_data(&ftdic, buf, 1); + if(f < 0) { + fprintf(stderr,"write failed for 0x%x, error %d (%s)\n",buf[0],f, ftdi_get_error_string(&ftdic)); + } + + sleep(3); + + for(i = 0; i < 32; i++) { + buf[0] = 0 | (0xFF ^ 1 << (i % 8)); + if( i > 0 && (i % 8) == 0) { + printf("\n"); + } + printf("%02hhx ",buf[0]); + fflush(stdout); + f = ftdi_write_data(&ftdic, buf, 1); + if(f < 0) { + fprintf(stderr,"write failed for 0x%x, error %d (%s)\n",buf[0],f, ftdi_get_error_string(&ftdic)); + } + sleep(1); + } + + printf("\n"); + + printf("disabling bitbang mode\n"); + ftdi_disable_bitbang(&ftdic); + + ftdi_usb_close(&ftdic); + ftdi_deinit(&ftdic); +} diff --git a/examples/bitbang2.c b/examples/bitbang2.c new file mode 100644 index 0000000..64fa04a --- /dev/null +++ b/examples/bitbang2.c @@ -0,0 +1,79 @@ +/* ftdi_out.c + * + * Output a (stream of) byte(s) in bitbang mode to the + * ftdi245 chip that is (hopefully) attached. + * + * We have a little board that has a FT245BM chip and + * the 8 outputs are connected to several different + * things that we can turn on and off with this program. + * + * If you have an idea about hardware that can easily + * interface onto an FTDI chip, I'd like to collect + * ideas. If I find it worthwhile to make, I'll consider + * making it, I'll even send you a prototype (against + * cost-of-material) if you want. + * + * At "harddisk-recovery.nl" they have a little board that + * controls the power to two harddrives and two fans. + * + * -- REW R.E.Wolff@BitWizard.nl + * + * + * + * This program was based on libftdi_example_bitbang2232.c + * which doesn't carry an author or attribution header. + * + * + * This program is distributed under the GPL, version 2. + * Millions copies of the GPL float around the internet. + */ + + +#include +#include +#include + +void ftdi_fatal (struct ftdi_context *ftdi, char *str) +{ + fprintf (stderr, "%s: %s\n", + str, ftdi_get_error_string (ftdi)); + exit (1); +} + +int main(int argc, char **argv) +{ + struct ftdi_context ftdic; + int i, t; + unsigned char data; + int delay = 100000; /* 100 thousand microseconds: 1 tenth of a second */ + + while ((t = getopt (argc, argv, "d:")) != -1) { + switch (t) { + case 'd': + delay = atoi (optarg); + break; + } + } + + ftdi_init(&ftdic); + + if (ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0) + ftdi_fatal (&ftdic, "Can't open ftdi device"); + + if (ftdi_enable_bitbang(&ftdic, 0xFF) < 0) + ftdi_fatal (&ftdic, "Can't enable bitbang"); + + for (i=optind; i < argc ; i++) { + sscanf (argv[i], "%x", &t); + data = t; + if(ftdi_write_data(&ftdic, &data, 1) < 0) { + fprintf(stderr,"write failed for 0x%x: %s\n", + data, ftdi_get_error_string(&ftdic)); + } + usleep(delay); + } + + ftdi_usb_close(&ftdic); + ftdi_deinit(&ftdic); + exit (0); +} diff --git a/examples/bitbang_ft2232.c b/examples/bitbang_ft2232.c new file mode 100644 index 0000000..17b6472 --- /dev/null +++ b/examples/bitbang_ft2232.c @@ -0,0 +1,87 @@ +/* This program is distributed under the GPL, version 2 */ + +#include +#include +#include + +int main(int argc, char **argv) +{ + struct ftdi_context ftdic,ftdic2; + int f,i; + + ftdi_init(&ftdic); + + f = ftdi_usb_open(&ftdic, 0x0403, 0x6001); + + if(f < 0 && f != -5) { + fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic)); + exit(-1); + } + + printf("ftdi open succeeded: %d\n",f); + + printf("enabling bitbang mode\n"); + ftdi_enable_bitbang(&ftdic, 0xFF); + + ftdic2.interface=1; + ftdic2.index=1; + ftdic2.out_ep=0x83; + ftdic2.in_ep=0x4; + + f = ftdi_usb_open(&ftdic2, 0x0403, 0x6001); + + if(f < 0 && f != -5) { + fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic)); + exit(-1); + } + + printf("ftdi open succeeded: %d\n",f); + + printf("enabling bitbang mode\n"); + ftdi_enable_bitbang(&ftdic2, 0xFF); + + + char buf[1]; + + sleep(3); + + buf[0] = 0x0; + printf("turning everything on\n"); + f = ftdi_write_data(&ftdic, buf, 1); + if(f < 0) { + fprintf(stderr,"write failed for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); + } + + sleep(3); + + buf[0] = 0xFF; + printf("turning everything off\n"); + f = ftdi_write_data(&ftdic, buf, 1); + if(f < 0) { + fprintf(stderr,"write failed for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); + } + + sleep(3); + + for(i = 0; i < 32; i++) { + buf[0] = 0 | (0xFF ^ 1 << (i % 8)); + if( i > 0 && (i % 8) == 0) { + printf("\n"); + } + printf("%02hhx ",buf[0]); + fflush(stdout); + f = ftdi_write_data(&ftdic, buf, 1); + if(f < 0) { + fprintf(stderr,"write failed for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); + } + sleep(1); + } + + printf("\n"); + + printf("disabling bitbang mode\n"); + ftdi_disable_bitbang(&ftdic); + + ftdi_usb_close(&ftdic); + ftdi_deinit(&ftdic); +} diff --git a/examples/simple.c b/examples/simple.c new file mode 100644 index 0000000..5568712 --- /dev/null +++ b/examples/simple.c @@ -0,0 +1,19 @@ +#include +#include + +int main(int argc, char **argv) +{ + int ret; + struct ftdi_context ftdic; + ftdi_init(&ftdic); + + if((ret = ftdi_usb_open(&ftdic, 0x0403, 0x6001)) < 0) { + fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + return EXIT_FAILURE; + } + + ftdi_usb_close(&ftdic); + ftdi_deinit(&ftdic); + + return EXIT_SUCCESS; +} diff --git a/src/ftdi.c b/src/ftdi.c index df7daaf..8399030 100644 --- a/src/ftdi.c +++ b/src/ftdi.c @@ -17,11 +17,13 @@ /** \mainpage libftdi API documentation - Library to talk to FTDI chips. See http://www.ftdichip.com - - The latest versions of libftdi is available at + Library to talk to FTDI chips. You find the latest versions of libftdi at http://www.intra2net.com/de/produkte/opensource/ftdi/ + The library is easy to use. Have a look at this short example: + \include simple.c + + More examples can be found in the "examples" directory. */ /** \addtogroup libftdi */ /* @{ */ -- 1.7.1