Skip to content

Instantly share code, notes, and snippets.

@td2sk
Created January 6, 2015 16:06
Show Gist options
  • Save td2sk/2d8d0f8cbdb0c8bf9131 to your computer and use it in GitHub Desktop.
Save td2sk/2d8d0f8cbdb0c8bf9131 to your computer and use it in GitHub Desktop.
diff --git a/avrdude/Makefile.am b/avrdude/Makefile.am
index 1d915b0..be277c8 100644
--- a/avrdude/Makefile.am
+++ b/avrdude/Makefile.am
@@ -66,7 +66,7 @@ avrdude_CFLAGS = @ENABLE_WARNINGS@
libavrdude_a_CFLAGS = @ENABLE_WARNINGS@
libavrdude_la_CFLAGS = $(libavrdude_a_CFLAGS)
-avrdude_LDADD = $(top_builddir)/$(noinst_LIBRARIES) @LIBUSB_1_0@ @LIBUSB@ @LIBFTDI1@ @LIBFTDI@ @LIBHID@ @LIBELF@ @LIBPTHREAD@ -lm
+avrdude_LDADD = $(top_builddir)/$(noinst_LIBRARIES) @LIBUSB_1_0@ @LIBUSB@ @LIBFTDI1@ @LIBFTDI@ @LIBFTD2XX@ @LIBHID@ @LIBELF@ @LIBPTHREAD@ -lm
bin_PROGRAMS = avrdude
diff --git a/avrdude/configure.ac b/avrdude/configure.ac
index 7586bd6..4dfab27 100644
--- a/avrdude/configure.ac
+++ b/avrdude/configure.ac
@@ -156,8 +156,11 @@ AH_TEMPLATE([HAVE_LIBFTDI],
[Define if FTDI support is enabled via libftdi])
AH_TEMPLATE([HAVE_LIBFTDI_TYPE_232H],
[Define if libftdi supports FT232H, libftdi version >= 0.20])
+AH_TEMPLATE([HAVE_LIBFTD2XX],
+ [Define if FTDI support is enabled via libftd2xx])
AC_CHECK_LIB([ftdi1], [ftdi_new], [have_libftdi1=yes], [], [-lusb-1.0])
AC_CHECK_LIB([ftdi], [ftdi_usb_get_strings], [have_libftdi=yes], [], [-lusb])
+AC_CHECK_LIB([ftd2xx], [FT_Open], [have_libftd2xx=yes], [], [])
if test x$have_libftdi1 = xyes; then
LIBFTDI1="-lftdi1"
AC_DEFINE([HAVE_LIBFTDI1])
@@ -171,6 +174,13 @@ else
if test x$have_libftdi_FT232H = xyes; then
AC_DEFINE([HAVE_LIBFTDI_TYPE_232H])
fi
+ else
+ if test x$have_libftd2xx = xyes; then
+ LIBFTD2XX="-lftd2xx"
+ AC_DEFINE([HAVE_LIBFTD2XX])
+ AC_SUBST(LIBFTD2XX, $LIBFTD2XX)
+ AC_DEFINE([HAVE_LIBFTDI_TYPE_232H])
+ fi
fi
fi
AC_CHECK_HEADERS([pthread.h])
@@ -515,6 +525,16 @@ else
echo "DON'T HAVE libftdi"
fi
+if test x$have_libftd2xx = xyes; then
+ if test x$have_libftdi = xyes || test x$have_libftdi1 = xyes; then
+ echo "DO HAVE libftd2xx (but prefer to use libftdiX)"
+ else
+ echo "DO HAVE libftd2xx"
+ fi
+else
+ echo "DON'T HAVE libftd2xx"
+fi
+
if test x$have_libhid = xyes; then
echo "DO HAVE libhid"
else
diff --git a/avrdude/ft245r.c b/avrdude/ft245r.c
index cc1061d..250ce12 100644
--- a/avrdude/ft245r.c
+++ b/avrdude/ft245r.c
@@ -81,6 +81,8 @@
#elif defined(HAVE_LIBFTDI) && defined(HAVE_USB_H)
/* ftdi.h includes usb.h */
#include <ftdi.h>
+#elif defined(HAVE_LIBFTD2XX)
+#include <ftd2xx.h>
#else
#warning No libftdi or libusb support. Install libftdi1/libusb-1.0 or libftdi/libusb and run configure/make again.
#define DO_NOT_BUILD_FT245R
@@ -142,6 +144,60 @@ typedef dispatch_semaphore_t sem_t;
#define FT245R_DEBUG 0
+#if defined(HAVE_LIBFTDI) || defined(HAVE_LIBFTDI1)
+#elif defined(HAVE_LIBFTD2XX)
+// fallback libftdi to libftd2xx
+struct ftdi_context {
+ FT_HANDLE p;
+};
+
+static const int BITMODE_RESET = FT_BITMODE_RESET;
+static const int BITMODE_SYNCBB = FT_BITMODE_SYNC_BITBANG;
+
+int ftdi_init(struct ftdi_context *context){
+ return 0;
+}
+
+int ftdi_deinit(struct ftdi_context *context){
+ return 0;
+}
+
+int ftdi_read_data(struct ftdi_context *context, unsigned char *buf, int size){
+ DWORD readBytes;
+ // TODO detect error
+ FT_Read(context->p, buf, size, &readBytes);
+ return readBytes;
+}
+
+int ftdi_write_data(struct ftdi_context *context, const unsigned char *buf, int size){
+ DWORD writeBytes;
+ // TODO detect error
+ FT_Write(context->p, (unsigned char*)buf, size, &writeBytes);
+ return writeBytes;
+}
+
+int ftdi_set_bitmode(struct ftdi_context *context, unsigned char bitmask, unsigned char mode){
+ return FT_SetBitMode(context->p, bitmask, mode);
+}
+
+int ftdi_set_baudrate(struct ftdi_context *context, int baudrate){
+ return FT_SetBaudRate(context->p, baudrate);
+}
+
+int ftdi_usb_open_desc_index(struct ftdi_context *context, int vendor, int product, const char *description, const char *serial, unsigned int index){
+ // TODO use other information
+ return FT_Open(index, &context->p);
+}
+
+int ftdi_usb_close (struct ftdi_context *context){
+ return FT_Close(context->p);
+}
+
+char* ftdi_get_error_string (struct ftdi_context *context){
+ return "ftd2xx library do not support extra messages.";
+}
+#endif
+
static struct ftdi_context *handle;
static unsigned char ft245r_ddr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment