]> cvs.zerfleddert.de Git - usb-driver/blobdiff - jtagkey.c
remove old debug message
[usb-driver] / jtagkey.c
index 2be195451f13f2cbfa1aaa9c10ebd5666c420f8a..2490b31e3970e6b8805b23ef7d796cd9d07ab9fb 100644 (file)
--- a/jtagkey.c
+++ b/jtagkey.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <ftdi.h>
 #include <stdio.h>
 #include <ftdi.h>
+#include <unistd.h>
 #include "usb-driver.h"
 #include "jtagkey.h"
 
 #include "usb-driver.h"
 #include "jtagkey.h"
 
@@ -7,6 +8,7 @@
 
 static struct ftdi_context ftdic;
 static unsigned int usb_maxlen = 0;
 
 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) {
        int ret = 0;
 
 int jtagkey_init(unsigned short vid, unsigned short pid) {
        int ret = 0;
@@ -56,6 +58,8 @@ int jtagkey_init(unsigned short vid, unsigned short pid) {
                return ret;
        }
 
                return ret;
        }
 
+       bitbang_mode = BITMODE_BITBANG;
+
        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;
@@ -70,6 +74,31 @@ void jtagkey_close() {
        ftdi_deinit(&ftdic);
 }
 
        ftdi_deinit(&ftdic);
 }
 
+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;
+}
+
 void jtagkey_state(unsigned char data) {
        fprintf(stderr,"Pins high: ");
 
 void jtagkey_state(unsigned char data) {
        fprintf(stderr,"Pins high: ");
 
@@ -114,9 +143,10 @@ 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);
 
-               len = writepos-pos;
-
+               jtagkey_set_bbmode(BITMODE_BITBANG);
                while (pos < writepos) {
                while (pos < writepos) {
+                       len = writepos-pos;
+
                        if (len > usb_maxlen)
                                len = usb_maxlen;
 
                        if (len > usb_maxlen)
                                len = usb_maxlen;
 
@@ -125,7 +155,6 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
                        pos += len;
                }
 
                        pos += len;
                }
 
-               DPRINTF("read %d/%d bytes\n", i, writepos-writebuf);
                writepos = writebuf;
        }
 
                writepos = writebuf;
        }
 
@@ -207,9 +236,16 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
        if (nread)
        {
                DPRINTF("writing %d bytes\n", writepos-writebuf);
        if (nread)
        {
                DPRINTF("writing %d bytes\n", writepos-writebuf);
-               for (i=0; i<writepos-writebuf; i++) {
-                       ftdi_write_data(&ftdic, writebuf+i, 1);
-                       ftdi_read_pins(&ftdic, readbuf+i);
+
+               *writepos = last_data;
+               writepos++;
+
+               jtagkey_set_bbmode(BITMODE_SYNCBB);
+               ftdi_write_data(&ftdic, writebuf, writepos-writebuf);
+
+               i = 0;
+               while (i < writepos-writebuf) {
+                       i += ftdi_read_data(&ftdic, readbuf, sizeof(readbuf));
                }
 
 #ifdef DEBUG
                }
 
 #ifdef DEBUG
@@ -225,7 +261,6 @@ int jtagkey_transfer(WD_TRANSFER *tr, int fd, unsigned int request, int ppbase,
        }
 
        readpos = readbuf;
        }
 
        readpos = readbuf;
-       readpos += 0;
 
        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",
@@ -245,6 +280,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);
Impressum, Datenschutz