sync repos

This commit is contained in:
dhewg 2010-07-11 12:13:18 +02:00
parent 706560a68c
commit 80811442b9
4 changed files with 71 additions and 12 deletions

1
client/.gitignore vendored
View File

@ -1,3 +1,2 @@
bootmii bootmii
*~

View File

@ -1,9 +1,17 @@
#USE_LIBFTDI = 1
CFLAGS = -Wall -W -Os -g CFLAGS = -Wall -W -Os -g
TARGET = bootmii TARGET = bootmii
OBJS = gecko.o main.o OBJS = gecko.o main.o
LIBS =
NOMAPFILE = 1 NOMAPFILE = 1
ifeq ($(USE_LIBFTDI),1)
CFLAGS += -DUSE_LIBFTDI
LIBS += -lftdi
endif
include ../common.mk include ../common.mk
install: all install: all

View File

@ -25,24 +25,49 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#ifdef USE_LIBFTDI
#include "ftdi.h"
#else
#ifndef __WIN32__ #ifndef __WIN32__
#include <termios.h> #include <termios.h>
#else #else
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#endif #endif
#endif
#include "gecko.h" #include "gecko.h"
#define FTDI_PACKET_SIZE 3968 #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__ #ifndef __WIN32__
static int fd_gecko = -1; static int fd_gecko = -1;
#else #else
static HANDLE handle_gecko = NULL; static HANDLE handle_gecko = NULL;
#endif #endif
#endif
int gecko_open(const char *dev) { 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__ #ifndef __WIN32__
struct termios newtio; struct termios newtio;
@ -94,6 +119,7 @@ int gecko_open(const char *dev) {
fprintf(stderr, "error setting communications event mask\n"); fprintf(stderr, "error setting communications event mask\n");
return 1; return 1;
} }
#endif
#endif #endif
gecko_flush(); gecko_flush();
@ -102,28 +128,35 @@ int gecko_open(const char *dev) {
} }
void gecko_close() { void gecko_close() {
#ifdef USE_LIBFTDI
ftdi_usb_close(&ftdi_ctx);
#else
#ifndef __WIN32__ #ifndef __WIN32__
if (fd_gecko > 0) if (fd_gecko > 0)
close(fd_gecko); close(fd_gecko);
#else #else
CloseHandle(handle_gecko); CloseHandle(handle_gecko);
#endif #endif
#endif
} }
void gecko_flush() { void gecko_flush() {
#ifdef USE_LIBFTDI
ftdi_usb_purge_buffers(&ftdi_ctx);
#else
#ifndef __WIN32__ #ifndef __WIN32__
tcflush(fd_gecko, TCIOFLUSH); tcflush(fd_gecko, TCIOFLUSH);
#else #else
PurgeComm(handle_gecko, PURGE_RXCLEAR | PURGE_TXCLEAR | PurgeComm(handle_gecko, PURGE_RXCLEAR | PURGE_TXCLEAR |
PURGE_TXABORT | PURGE_RXABORT); PURGE_TXABORT | PURGE_RXABORT);
#endif
#endif #endif
} }
int gecko_read(void *buf, size_t count) { int gecko_read(void *buf, size_t count) {
size_t left, chunk; size_t left, chunk;
#ifndef __WIN32__ #ifndef __WIN32__
size_t res; ssize_t res;
#else #else
DWORD res; DWORD res;
#endif #endif
@ -133,7 +166,14 @@ int gecko_read(void *buf, size_t count) {
chunk = left; chunk = left;
if (chunk > FTDI_PACKET_SIZE) if (chunk > FTDI_PACKET_SIZE)
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__ #ifndef __WIN32__
res = read(fd_gecko, buf, chunk); res = read(fd_gecko, buf, chunk);
if (res < 1) { if (res < 1) {
@ -145,6 +185,7 @@ int gecko_read(void *buf, size_t count) {
fprintf(stderr, "gecko_read\n"); fprintf(stderr, "gecko_read\n");
return 1; return 1;
} }
#endif
#endif #endif
left -= res; left -= res;
@ -157,7 +198,7 @@ int gecko_read(void *buf, size_t count) {
int gecko_write(const void *buf, size_t count) { int gecko_write(const void *buf, size_t count) {
size_t left, chunk; size_t left, chunk;
#ifndef __WIN32__ #ifndef __WIN32__
size_t res; ssize_t res;
#else #else
DWORD res; DWORD res;
#endif #endif
@ -169,6 +210,14 @@ int gecko_write(const void *buf, size_t count) {
if (chunk > FTDI_PACKET_SIZE) if (chunk > FTDI_PACKET_SIZE)
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__ #ifndef __WIN32__
res = write(fd_gecko, buf, count); res = write(fd_gecko, buf, count);
if (res < 1) { if (res < 1) {
@ -180,12 +229,13 @@ int gecko_write(const void *buf, size_t count) {
fprintf (stderr, "gecko_write\n"); fprintf (stderr, "gecko_write\n");
return 1; return 1;
} }
#endif
#endif #endif
left -= res; left -= res;
buf += res; buf += res;
#ifndef __WIN32__ #if !defined(__WIN32__) && !defined(USE_LIBFTDI)
// does this work with ftdi-sio? // does this work with ftdi-sio?
if (tcdrain(fd_gecko)) { if (tcdrain(fd_gecko)) {
perror ("gecko_drain"); perror ("gecko_drain");

View File

@ -63,7 +63,7 @@ void usage(const char *appname) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
int cmd, fd; int cmd, fd;
struct stat st; struct stat st;
char *tty; char *tty = NULL;
unsigned char buf4[4]; unsigned char buf4[4];
unsigned char *buf, *p; unsigned char *buf, *p;
off_t fsize, block; off_t fsize, block;
@ -82,7 +82,8 @@ int main(int argc, char **argv) {
if (cmd == CMD_NONE) if (cmd == CMD_NONE)
usage(argv[0]); usage(argv[0]);
#ifndef USE_LIBFTDI
tty = getenv(envvar); tty = getenv(envvar);
if (!tty) if (!tty)
tty = default_tty; tty = default_tty;
@ -92,17 +93,18 @@ int main(int argc, char **argv) {
if (!tty) { if (!tty) {
fprintf(stderr, "please set the environment variable %s to " fprintf(stderr, "please set the environment variable %s to "
"your usbgecko " "your usbgecko "
#ifndef __WIN32__ #ifndef __WIN32__
"tty device (eg \"/dev/ttyUSB0\")" "tty device (eg \"/dev/ttyUSB0\")"
#else #else
"COM port (eg \"COM3\")" "COM port (eg \"COM3\")"
#endif #endif
"\n", envvar); "\n", envvar);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("using %s\n", tty); printf("using %s\n", tty);
#endif
fd = open(argv[2], O_RDONLY | O_BINARY); fd = open(argv[2], O_RDONLY | O_BINARY);
if (fd < 0) { if (fd < 0) {