X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/usb-driver/blobdiff_plain/4af4753dc42ce756e91d803da1824478e11695f0..23d61681a4fd911467736fa854cc1c469b7de6f7:/jtagkey.c diff --git a/jtagkey.c b/jtagkey.c index 2be1954..e17afc8 100644 --- a/jtagkey.c +++ b/jtagkey.c @@ -1,14 +1,18 @@ #include #include +#include +#include #include "usb-driver.h" +#include "config.h" #include "jtagkey.h" -#define USBBUFSIZE 4096 +#define USBBUFSIZE 1048576 +#define SLOW_AND_SAFE 1 static struct ftdi_context ftdic; -static unsigned int usb_maxlen = 0; +static unsigned char bitbang_mode; -int jtagkey_init(unsigned short vid, unsigned short pid) { +static int jtagkey_init(unsigned short vid, unsigned short pid) { int ret = 0; unsigned char c; @@ -32,9 +36,13 @@ int jtagkey_init(unsigned short vid, unsigned short pid) { return ret; } + if ((ret = ftdi_write_data_set_chunksize(&ftdic, USBBUFSIZE)) != 0) { + fprintf(stderr, "unable to set write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + return ret; + } - if ((ret = ftdi_write_data_get_chunksize(&ftdic, &usb_maxlen)) != 0) { - fprintf(stderr, "unable to get write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + if ((ret = ftdi_read_data_set_chunksize(&ftdic, USBBUFSIZE)) != 0) { + fprintf(stderr, "unable to set read chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); return ret; } @@ -51,11 +59,13 @@ int jtagkey_init(unsigned short vid, unsigned short pid) { c = 0x00; ftdi_write_data(&ftdic, &c, 1); - if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_BITBANG)) != 0) { + if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB)) != 0) { fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); return ret; } + bitbang_mode = BITMODE_SYNCBB; + if ((ret = ftdi_usb_purge_buffers(&ftdic)) != 0) { fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); return ret; @@ -64,11 +74,51 @@ int jtagkey_init(unsigned short vid, unsigned short pid) { return ret; } -void jtagkey_close() { - ftdi_disable_bitbang(&ftdic); - ftdi_usb_close(&ftdic); - ftdi_deinit(&ftdic); +int jtagkey_open(int num) { + int ret; + + ret = jtagkey_init(config_usb_vid(num), config_usb_pid(num)); + + if (ret >= 0) + ret = 0xff; + + return ret; +} + +void jtagkey_close(int handle) { + if (handle == 0xff) { + ftdi_disable_bitbang(&ftdic); + ftdi_usb_close(&ftdic); + ftdi_deinit(&ftdic); + } +} + +#ifndef SLOW_AND_SAFE +static int jtagkey_set_bbmode(unsigned char mode) { + int ret = 0; + + if (bitbang_mode != mode) { + DPRINTF("switching bitbang-mode!\n"); + + /* Wait for the latency-timer to kick in */ + usleep(2); + if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, mode)) != 0) { + fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + return ret; + } + if ((ret = ftdi_usb_purge_buffers(&ftdic)) != 0) { + fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); + return ret; + } + /* Wait for the FTDI2232 to settle */ + usleep(2); + + bitbang_mode = mode; + } + + return ret; } +#endif void jtagkey_state(unsigned char data) { fprintf(stderr,"Pins high: "); @@ -91,6 +141,25 @@ void jtagkey_state(unsigned char data) { fprintf(stderr,"\n"); } +struct jtagkey_reader_arg { + int num; + unsigned char *buf; +}; + +static void *jtagkey_reader(void *thread_arg) { + struct jtagkey_reader_arg *arg = (struct jtagkey_reader_arg*)thread_arg; + + int i; + + i = 0; + DPRINTF("reader for %d bytes\n", arg->num); + while (i < arg->num) { + i += ftdi_read_data(&ftdic, arg->buf + i, arg->num - i); + } + + pthread_exit(NULL); +} + int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) { int ret = 0; int i; @@ -102,6 +171,8 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf; static unsigned char readbuf[USBBUFSIZE], *readpos; unsigned char data, prev_data; + struct jtagkey_reader_arg targ; + pthread_t reader_thread; /* Count reads */ for (i = 0; i < num; i++) @@ -112,20 +183,31 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) { unsigned char *pos = writebuf; int len; - DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num); - len = writepos-pos; + DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num); +#ifndef SLOW_AND_SAFE + jtagkey_set_bbmode(BITMODE_BITBANG); +#endif +#ifdef SLOW_AND_SAFE + targ.num = writepos-pos; + targ.buf = readbuf; + pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ); +#endif while (pos < writepos) { - if (len > usb_maxlen) - len = usb_maxlen; + len = writepos-pos; + + if (len > USBBUFSIZE) + len = USBBUFSIZE; DPRINTF("combined write of %d/%d\n",len,writepos-pos); ftdi_write_data(&ftdic, pos, len); pos += len; } +#ifdef SLOW_AND_SAFE + pthread_join(reader_thread, NULL); +#endif - DPRINTF("read %d/%d bytes\n", i, writepos-writebuf); writepos = writebuf; } @@ -200,17 +282,25 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, } } - if (nread || (*writepos != prev_data)) + if (nread || (*writepos != prev_data) || (i == num-1)) writepos++; } if (nread) { DPRINTF("writing %d bytes\n", writepos-writebuf); - for (i=0; i