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
*~

View File

@ -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

View File

@ -25,24 +25,49 @@
#include <fcntl.h>
#include <errno.h>
#ifdef USE_LIBFTDI
#include "ftdi.h"
#else
#ifndef __WIN32__
#include <termios.h>
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#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");

View File

@ -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) {