]> cvs.zerfleddert.de Git - usb-driver/blobdiff - jtagkey.c
programming is now abaout as fast as a parallel cable again
[usb-driver] / jtagkey.c
index 42669591ecfd76a9a06098e34380367ce4df8aa6..ffae2fa1a99898f997114ce1e3da666500c43a93 100644 (file)
--- a/jtagkey.c
+++ b/jtagkey.c
@@ -1,13 +1,36 @@
 #include <stdio.h>
 #include <ftdi.h>
 #include <stdio.h>
 #include <ftdi.h>
+#include <unistd.h>
+#include <pthread.h>
 #include "usb-driver.h"
 #include "usb-driver.h"
+#include "config.h"
 #include "jtagkey.h"
 
 #include "jtagkey.h"
 
-#define USBBUFSIZE 384
+#define USBBUFSIZE 1048576
+#define JTAG_SPEED 100000
+#define BULK_LATENCY 2
+#define OTHER_LATENCY 1
 
 static struct ftdi_context ftdic;
 
 
 static struct ftdi_context ftdic;
 
-int jtagkey_init(unsigned short vid, unsigned short pid) {
+static int jtagkey_latency(int latency) {
+       static int current = 0;
+       int ret;
+
+       if (current != latency) {
+               DPRINTF("switching latency\n");
+               if ((ret = ftdi_set_latency_timer(&ftdic, latency))  != 0) {
+                       fprintf(stderr, "unable to set latency timer: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
+                       return ret;
+               }
+               
+               current = latency;
+       }
+
+       return ret;
+}
+
+static int jtagkey_init(unsigned short vid, unsigned short pid) {
        int ret = 0;
        unsigned char c;
 
        int ret = 0;
        unsigned char c;
 
@@ -31,22 +54,18 @@ int jtagkey_init(unsigned short vid, unsigned short pid) {
                return ret;
        }
 
                return ret;
        }
 
-#if 0
-       if ((ret = ftdi_write_data_set_chunksize(&ftdic, 1))  != 0) {
+       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;
        }
                fprintf(stderr, "unable to set write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
                return ret;
        }
-#endif
 
 
-       if ((ret = ftdi_set_latency_timer(&ftdic, 1))  != 0) {
-               fprintf(stderr, "unable to set latency timer: %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;
        }
 
                return ret;
        }
 
-       if ((ret = ftdi_set_baudrate(&ftdic, 500000))  != 0) {
-               fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
+       if ((ret = jtagkey_latency(OTHER_LATENCY)) != 0)
                return ret;
                return ret;
-       }
 
        c = 0x00;
        ftdi_write_data(&ftdic, &c, 1);
 
        c = 0x00;
        ftdi_write_data(&ftdic, &c, 1);
@@ -56,6 +75,11 @@ int jtagkey_init(unsigned short vid, unsigned short pid) {
                return ret;
        }
 
                return ret;
        }
 
+       if ((ret = ftdi_set_baudrate(&ftdic, JTAG_SPEED))  != 0) {
+               fprintf(stderr, "unable to set baudrate: %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;
        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,10 +88,23 @@ int jtagkey_init(unsigned short vid, unsigned short pid) {
        return ret;
 }
 
        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);
+       }
 }
 
 void jtagkey_state(unsigned char data) {
 }
 
 void jtagkey_state(unsigned char data) {
@@ -91,6 +128,25 @@ void jtagkey_state(unsigned char data) {
        fprintf(stderr,"\n");
 }
 
        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);
+}
+
+/* TODO: Interpret JTAG commands and transfer in MPSSE mode */
 int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) {
        int ret = 0;
        int i;
 int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase, int ecpbase, int num) {
        int ret = 0;
        int i;
@@ -98,30 +154,47 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
        unsigned long port;
        unsigned char val;
        static unsigned char last_data = 0;
        unsigned long port;
        unsigned char val;
        static unsigned char last_data = 0;
+       static unsigned char last_write = 0x00;
        static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
        static unsigned char readbuf[USBBUFSIZE], *readpos;
        static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
        static unsigned char readbuf[USBBUFSIZE], *readpos;
-       unsigned char data;
-       static unsigned char last_write = 0x00;
+       unsigned char data, prev_data, last_cyc_write;
+       struct jtagkey_reader_arg targ;
+       pthread_t reader_thread;
 
 
-       /* Count writes */
+       /* Count reads */
        for (i = 0; i < num; i++)
        for (i = 0; i < num; i++)
-               if (tr[i].cmdTrans == 10)
+               if (tr[i].cmdTrans == PP_READ)
                        nread++;
 
        /* Write combining */
        if ((writepos-writebuf > sizeof(writebuf)-num) || (nread && writepos-writebuf)) {
                        nread++;
 
        /* Write combining */
        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);
                DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
+               jtagkey_latency(BULK_LATENCY);
 
 
-               ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
+               targ.num = writepos-pos;
+               targ.buf = readbuf;
+               pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
+
+               while (pos < writepos) {
+                       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;
+               }
+               pthread_join(reader_thread, NULL);
 
 
-               i = 0;
-               while (i < writepos-writebuf) {
-                       i += ftdi_read_data(&ftdic, readbuf, sizeof(readbuf));
-               };
-               DPRINTF("read %d/%d bytes\n", i, writepos-writebuf);
                writepos = writebuf;
        }
 
                writepos = writebuf;
        }
 
+       last_cyc_write = last_write;
+
        for (i = 0; i < num; i++) {
                DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
                                (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
        for (i = 0; i < num; i++) {
                DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
                                (unsigned long)tr[i].dwPort, tr[i].cmdTrans, tr[i].dwBytes,
@@ -137,6 +210,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
 
                /* Pad writebuf for read-commands in stream */
                *writepos = last_data;
 
                /* Pad writebuf for read-commands in stream */
                *writepos = last_data;
+               prev_data = last_data;
 
                if (port == ppbase + PP_DATA) {
                        DPRINTF("data port\n");
 
                if (port == ppbase + PP_DATA) {
                        DPRINTF("data port\n");
@@ -191,20 +265,25 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                                        break;
                        }
                }
                                        break;
                        }
                }
-               writepos++;
+
+               if ((tr[i].cmdTrans == PP_READ) || (*writepos != prev_data) || (i == num-1))
+                       writepos++;
        }
 
        if (nread)
        {
                DPRINTF("writing %d bytes\n", writepos-writebuf);
        }
 
        if (nread)
        {
                DPRINTF("writing %d bytes\n", writepos-writebuf);
-               ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
-               DPRINTF("reading %d bytes\n", writepos-writebuf);
 
 
-               i = 0;
-               while (i < writepos-writebuf) {
-                       i += ftdi_read_data(&ftdic, readbuf, sizeof(readbuf));
-               }
-               DPRINTF("read %d bytes\n", i);
+               *writepos = last_data;
+               writepos++;
+
+               jtagkey_latency(OTHER_LATENCY);
+
+               targ.num = writepos-writebuf;
+               targ.buf = readbuf;
+               pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
+               ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
+               pthread_join(reader_thread, NULL);
 
 #ifdef DEBUG
                DPRINTF("write: ");
 
 #ifdef DEBUG
                DPRINTF("write: ");
@@ -219,7 +298,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
        }
 
        readpos = readbuf;
        }
 
        readpos = readbuf;
-       readpos += 0;
+       last_write = last_cyc_write;
 
        for (i = 0; i < num; i++) {
                DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
 
        for (i = 0; i < num; i++) {
                DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
@@ -228,6 +307,10 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
 
                port = (unsigned long)tr[i].dwPort;
                val = tr[i].Data.Byte;
 
                port = (unsigned long)tr[i].dwPort;
                val = tr[i].Data.Byte;
+
+               if ((tr[i].cmdTrans != PP_READ) && (val == last_write) && (i != num-1))
+                       continue;
+
                readpos++;
 
                if (port == ppbase + PP_DATA) {
                readpos++;
 
                if (port == ppbase + PP_DATA) {
@@ -239,6 +322,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                        switch(tr[i].cmdTrans) {
                                case PP_READ:
                                        data = *readpos;
                        switch(tr[i].cmdTrans) {
                                case PP_READ:
                                        data = *readpos;
+
 #ifdef DEBUG
                                        DPRINTF("READ: 0x%x\n", data);
                                        jtagkey_state(data);
 #ifdef DEBUG
                                        DPRINTF("READ: 0x%x\n", data);
                                        jtagkey_state(data);
@@ -248,7 +332,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                                        if ((data & JTAGKEY_TDO) && (last_write & PP_PROG))
                                                val |= PP_TDO;
 
                                        if ((data & JTAGKEY_TDO) && (last_write & PP_PROG))
                                                val |= PP_TDO;
 
-                                       if (!(last_write & PP_PROG))
+                                       if (~last_write & PP_PROG)
                                                val |= 0x08;
 
                                        if (last_write & 0x40)
                                                val |= 0x08;
 
                                        if (last_write & 0x40)
Impressum, Datenschutz