]> git.zerfleddert.de Git - usb-driver/blobdiff - jtagkey.c
adapt snapshot-link to new gitweb
[usb-driver] / jtagkey.c
index 129cfe1cc652d9d9f4214f74bfb1483d0036a4bd..d23c60d244a06e6e04e368b1e701b777875d725d 100644 (file)
--- a/jtagkey.c
+++ b/jtagkey.c
@@ -2,16 +2,35 @@
 #include <ftdi.h>
 #include <unistd.h>
 #include <pthread.h>
 #include <ftdi.h>
 #include <unistd.h>
 #include <pthread.h>
+#include <inttypes.h>
 #include "usb-driver.h"
 #include "config.h"
 #include "jtagkey.h"
 #include "usb-driver.h"
 #include "config.h"
 #include "jtagkey.h"
+#include "jtagmon.h"
 
 #define USBBUFSIZE 1048576
 #define JTAG_SPEED 100000
 
 #define USBBUFSIZE 1048576
 #define JTAG_SPEED 100000
-#define SLOW_AND_SAFE 1
+#define BULK_LATENCY 2
+#define OTHER_LATENCY 1
 
 static struct ftdi_context ftdic;
 
 static struct ftdi_context ftdic;
-static unsigned char bitbang_mode;
+
+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;
 
 static int jtagkey_init(unsigned short vid, unsigned short pid) {
        int ret = 0;
@@ -47,10 +66,8 @@ static int jtagkey_init(unsigned short vid, unsigned short pid) {
                return ret;
        }
 
                return ret;
        }
 
-       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 = 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);
@@ -65,8 +82,6 @@ static int jtagkey_init(unsigned short vid, unsigned short pid) {
                return ret;
        }
 
                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;
        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;
@@ -94,33 +109,6 @@ void jtagkey_close(int handle) {
        }
 }
 
        }
 }
 
-#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: ");
 
 void jtagkey_state(unsigned char data) {
        fprintf(stderr,"Pins high: ");
 
@@ -149,7 +137,6 @@ struct jtagkey_reader_arg {
 
 static void *jtagkey_reader(void *thread_arg) {
        struct jtagkey_reader_arg *arg = (struct jtagkey_reader_arg*)thread_arg;
 
 static void *jtagkey_reader(void *thread_arg) {
        struct jtagkey_reader_arg *arg = (struct jtagkey_reader_arg*)thread_arg;
-
        int i;
 
        i = 0;
        int i;
 
        i = 0;
@@ -161,6 +148,7 @@ static void *jtagkey_reader(void *thread_arg) {
        pthread_exit(NULL);
 }
 
        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;
@@ -171,7 +159,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
        static unsigned char last_write = 0x00;
        static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
        static unsigned char readbuf[USBBUFSIZE], *readpos;
        static unsigned char last_write = 0x00;
        static unsigned char writebuf[USBBUFSIZE], *writepos = writebuf;
        static unsigned char readbuf[USBBUFSIZE], *readpos;
-       unsigned char data, prev_data;
+       unsigned char data, prev_data, last_cyc_write;
        struct jtagkey_reader_arg targ;
        pthread_t reader_thread;
 
        struct jtagkey_reader_arg targ;
        pthread_t reader_thread;
 
@@ -186,15 +174,12 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                int len;
 
                DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
                int len;
 
                DPRINTF("writing %d bytes due to %d following reads in %d chunks or full buffer\n", writepos-writebuf, nread, num);
+               jtagkey_latency(BULK_LATENCY);
 
 
-#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);
                targ.num = writepos-pos;
                targ.buf = readbuf;
                pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
-#endif
+
                while (pos < writepos) {
                        len = writepos-pos;
 
                while (pos < writepos) {
                        len = writepos-pos;
 
@@ -205,13 +190,13 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                        ftdi_write_data(&ftdic, pos, len);
                        pos += len;
                }
                        ftdi_write_data(&ftdic, pos, len);
                        pos += len;
                }
-#ifdef SLOW_AND_SAFE
                pthread_join(reader_thread, NULL);
                pthread_join(reader_thread, NULL);
-#endif
 
                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,
@@ -223,6 +208,9 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
 #ifdef DEBUG
                if (tr[i].cmdTrans == 13)
                        DPRINTF("write byte: %d\n", val);
 #ifdef DEBUG
                if (tr[i].cmdTrans == 13)
                        DPRINTF("write byte: %d\n", val);
+
+               if (tr[i].cmdTrans == 13)
+                       jtagmon(val & PP_TCK, val & PP_TMS, val & PP_TDI);
 #endif
 
                /* Pad writebuf for read-commands in stream */
 #endif
 
                /* Pad writebuf for read-commands in stream */
@@ -283,7 +271,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                        }
                }
 
                        }
                }
 
-               if (nread || (*writepos != prev_data) || (i == num-1))
+               if ((tr[i].cmdTrans == PP_READ) || (*writepos != prev_data) || (i == num-1))
                        writepos++;
        }
 
                        writepos++;
        }
 
@@ -294,9 +282,8 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                *writepos = last_data;
                writepos++;
 
                *writepos = last_data;
                writepos++;
 
-#ifndef SLOW_AND_SAFE
-               jtagkey_set_bbmode(BITMODE_SYNCBB);
-#endif
+               jtagkey_latency(OTHER_LATENCY);
+
                targ.num = writepos-writebuf;
                targ.buf = readbuf;
                pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
                targ.num = writepos-writebuf;
                targ.buf = readbuf;
                pthread_create(&reader_thread, NULL, &jtagkey_reader, &targ);
@@ -316,6 +303,7 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
        }
 
        readpos = readbuf;
        }
 
        readpos = readbuf;
+       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",
@@ -324,6 +312,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) {
@@ -345,7 +337,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