From 80811442b9e8f98c7fcd1f95dcbaf1469bed89d9 Mon Sep 17 00:00:00 2001 From: dhewg Date: Sun, 11 Jul 2010 12:13:18 +0200 Subject: [PATCH] sync repos --- client/.gitignore | 1 - client/Makefile | 8 +++++++ client/gecko.c | 60 +++++++++++++++++++++++++++++++++++++++++++---- client/main.c | 14 ++++++----- 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/client/.gitignore b/client/.gitignore index 186d0fe..d16e4ba 100644 --- a/client/.gitignore +++ b/client/.gitignore @@ -1,3 +1,2 @@ bootmii -*~ diff --git a/client/Makefile b/client/Makefile index d3f9f16..242c7d0 100644 --- a/client/Makefile +++ b/client/Makefile @@ -1,9 +1,17 @@ +#USE_LIBFTDI = 1 + CFLAGS = -Wall -W -Os -g TARGET = bootmii OBJS = gecko.o main.o +LIBS = NOMAPFILE = 1 +ifeq ($(USE_LIBFTDI),1) +CFLAGS += -DUSE_LIBFTDI +LIBS += -lftdi +endif + include ../common.mk install: all diff --git a/client/gecko.c b/client/gecko.c index 9c3ab18..57537e9 100644 --- a/client/gecko.c +++ b/client/gecko.c @@ -25,24 +25,49 @@ #include #include +#ifdef USE_LIBFTDI +#include "ftdi.h" +#else #ifndef __WIN32__ #include #else #define WIN32_LEAN_AND_MEAN #include #endif +#endif #include "gecko.h" #define FTDI_PACKET_SIZE 3968 +#ifdef USE_LIBFTDI +#define DEFAULT_FTDI_VID 0x0403 +#define DEFAULT_FTDI_PID 0x6001 + +struct ftdi_context ftdi_ctx; +#else #ifndef __WIN32__ static int fd_gecko = -1; #else static HANDLE handle_gecko = NULL; #endif +#endif int gecko_open(const char *dev) { +#ifdef USE_LIBFTDI + (void) dev; + + int err; + + ftdi_init(&ftdi_ctx); + + err = ftdi_usb_open(&ftdi_ctx, DEFAULT_FTDI_VID, DEFAULT_FTDI_PID); + if (err < 0) { + fprintf(stderr, "ftdi_usb_open_desc failed: %d (%s)\n", + err, ftdi_get_error_string(&ftdi_ctx)); + return 1; + } +#else #ifndef __WIN32__ struct termios newtio; @@ -94,6 +119,7 @@ int gecko_open(const char *dev) { fprintf(stderr, "error setting communications event mask\n"); return 1; } +#endif #endif gecko_flush(); @@ -102,28 +128,35 @@ int gecko_open(const char *dev) { } void gecko_close() { +#ifdef USE_LIBFTDI + ftdi_usb_close(&ftdi_ctx); +#else #ifndef __WIN32__ if (fd_gecko > 0) close(fd_gecko); #else CloseHandle(handle_gecko); #endif +#endif } void gecko_flush() { +#ifdef USE_LIBFTDI + ftdi_usb_purge_buffers(&ftdi_ctx); +#else #ifndef __WIN32__ tcflush(fd_gecko, TCIOFLUSH); #else PurgeComm(handle_gecko, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_TXABORT | PURGE_RXABORT); - +#endif #endif } int gecko_read(void *buf, size_t count) { size_t left, chunk; #ifndef __WIN32__ - size_t res; + ssize_t res; #else DWORD res; #endif @@ -133,7 +166,14 @@ int gecko_read(void *buf, size_t count) { chunk = left; if (chunk > FTDI_PACKET_SIZE) chunk = FTDI_PACKET_SIZE; - +#ifdef USE_LIBFTDI + res = ftdi_read_data(&ftdi_ctx, buf, chunk); + if (res < 1) { + fprintf(stderr, "ftdi_read_data failed: %d (%s)\n", + (int) res, ftdi_get_error_string(&ftdi_ctx)); + return 1; + } +#else #ifndef __WIN32__ res = read(fd_gecko, buf, chunk); if (res < 1) { @@ -145,6 +185,7 @@ int gecko_read(void *buf, size_t count) { fprintf(stderr, "gecko_read\n"); return 1; } +#endif #endif left -= res; @@ -157,7 +198,7 @@ int gecko_read(void *buf, size_t count) { int gecko_write(const void *buf, size_t count) { size_t left, chunk; #ifndef __WIN32__ - size_t res; + ssize_t res; #else DWORD res; #endif @@ -169,6 +210,14 @@ int gecko_write(const void *buf, size_t count) { if (chunk > FTDI_PACKET_SIZE) chunk = FTDI_PACKET_SIZE; +#ifdef USE_LIBFTDI + res = ftdi_write_data(&ftdi_ctx, (unsigned char *)buf, chunk); + if (res < 1) { + fprintf(stderr, "ftdi_write_data failed: %d (%s)\n", + (int) res, ftdi_get_error_string(&ftdi_ctx)); + return 1; + } +#else #ifndef __WIN32__ res = write(fd_gecko, buf, count); if (res < 1) { @@ -180,12 +229,13 @@ int gecko_write(const void *buf, size_t count) { fprintf (stderr, "gecko_write\n"); return 1; } +#endif #endif left -= res; buf += res; -#ifndef __WIN32__ +#if !defined(__WIN32__) && !defined(USE_LIBFTDI) // does this work with ftdi-sio? if (tcdrain(fd_gecko)) { perror ("gecko_drain"); diff --git a/client/main.c b/client/main.c index 6932539..e18786d 100644 --- a/client/main.c +++ b/client/main.c @@ -63,7 +63,7 @@ void usage(const char *appname) { int main(int argc, char **argv) { int cmd, fd; struct stat st; - char *tty; + char *tty = NULL; unsigned char buf4[4]; unsigned char *buf, *p; off_t fsize, block; @@ -82,7 +82,8 @@ int main(int argc, char **argv) { if (cmd == CMD_NONE) usage(argv[0]); - + +#ifndef USE_LIBFTDI tty = getenv(envvar); if (!tty) tty = default_tty; @@ -92,17 +93,18 @@ int main(int argc, char **argv) { if (!tty) { fprintf(stderr, "please set the environment variable %s to " - "your usbgecko " + "your usbgecko " #ifndef __WIN32__ - "tty device (eg \"/dev/ttyUSB0\")" + "tty device (eg \"/dev/ttyUSB0\")" #else - "COM port (eg \"COM3\")" + "COM port (eg \"COM3\")" #endif - "\n", envvar); + "\n", envvar); exit(EXIT_FAILURE); } printf("using %s\n", tty); +#endif fd = open(argv[2], O_RDONLY | O_BINARY); if (fd < 0) {