From 9ee8e84fbaf9f3f9b3121b9a99d3ff4547e6880c Mon Sep 17 00:00:00 2001 From: phdussud Date: Fri, 26 Feb 2021 10:01:15 -0800 Subject: [PATCH 1/8] Support for new DirtyJtag2 protocol. Merged with head --- src/dirtyJtag.cpp | 145 +++++++++++++++++++++++++++++++--------- src/dirtyJtag.hpp | 3 + src/ftdiJtagBitbang.cpp | 3 +- 3 files changed, 118 insertions(+), 33 deletions(-) diff --git a/src/dirtyJtag.cpp b/src/dirtyJtag.cpp index d7512f4..5e280a2 100644 --- a/src/dirtyJtag.cpp +++ b/src/dirtyJtag.cpp @@ -23,17 +23,33 @@ #include #include #include +#include #include "dirtyJtag.hpp" #include "display.hpp" using namespace std; +//#define DIRTY_JTAG_TEENSY_4 + +#ifdef DIRTY_JTAG_TEENSY_4 + +#define DIRTYJTAG_VID 0x16C0 +#define DIRTYJTAG_PID 0x1485 + +#define DIRTYJTAG_INTF 0 +#define DIRTYJTAG_WRITE_EP 0x2 +#define DIRTYJTAG_READ_EP 0x82 + +#else #define DIRTYJTAG_VID 0x1209 #define DIRTYJTAG_PID 0xC0CA +#define DIRTYJTAG_INTF 0 #define DIRTYJTAG_WRITE_EP 0x01 #define DIRTYJTAG_READ_EP 0x82 +#endif + enum dirtyJtagCmd { CMD_STOP = 0x00, @@ -45,6 +61,11 @@ enum dirtyJtagCmd { CMD_CLK = 0x06 }; +enum CommandModifier { + EXTEND_LENGTH = 0x40, + NO_READ = 0x80 +}; + enum dirtyJtagSig { SIG_TCK = (1 << 1), SIG_TDI = (1 << 2), @@ -71,14 +92,17 @@ DirtyJtag::DirtyJtag(uint32_t clkHZ, bool verbose): throw std::exception(); } - ret = libusb_claim_interface(dev_handle, 0); + ret = libusb_claim_interface(dev_handle, DIRTYJTAG_INTF); if (ret) { - cerr << "libusb error while claiming DirtyJTAG interface #0" << endl; + cerr << "libusb error while claiming DirtyJTAG interface" << endl; libusb_close(dev_handle); libusb_exit(usb_ctx); throw std::exception(); } + _version = 0; + getVersion(); + if (setClkFreq(clkHZ) < 0) { cerr << "Fail to set frequency" << endl; throw std::exception(); @@ -93,14 +117,50 @@ DirtyJtag::~DirtyJtag() libusb_exit(usb_ctx); } +void DirtyJtag::getVersion() +{ + int actual_length; + int ret; + uint8_t buf[] = {CMD_INFO, + CMD_STOP}; + uint8_t rx_buf[64]; + ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, 2, &actual_length, 1000); + if (ret < 0) { + cerr << "getVersion: usb bulk write failed " << ret << endl; + return; + } + do { + ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, + rx_buf, 64, &actual_length, 1000); + if (ret < 0) { + cerr << "getVersion: read: usb bulk read failed " << ret << endl; + return; + } + } while (actual_length == 0); + if (!strncmp("DJTAG1\n", (char*)rx_buf, 7)) { + _version = 1; + } else if (!strncmp("DJTAG2\n", (char*)rx_buf, 7)) { + _version = 2; + } else if (!strncmp("DJTAG3\n", (char*)rx_buf, 7)) { + _version = 3; + } else { + cerr << "dirtyJtag version unknown" << endl; + _version = 0; + } + +} + + + int DirtyJtag::setClkFreq(uint32_t clkHZ) { int actual_length; int ret, req_freq = clkHZ; - if (clkHZ > 600000) { - printWarn("DirtyJTAG probe limited to 600kHz"); - clkHZ = 600000; + if (clkHZ > 16000000) { + printWarn("DirtyJTAG probe limited to 16000kHz"); + clkHZ = 16000000; } printInfo("Jtag frequency : requested " + std::to_string(req_freq) + @@ -142,7 +202,7 @@ int DirtyJtag::toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len) 0, CMD_STOP}; while (clk_len > 0) { - buf[2] = (clk_len > 100) ? 100 : (uint8_t)clk_len; + buf[2] = (clk_len > 64) ? 64 : (uint8_t)clk_len; int ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, buf, 4, &actual_length, 1000); @@ -168,7 +228,7 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) uint32_t real_byte_len = (len + 7) / 8; uint8_t tx_cpy[real_byte_len]; - uint8_t tx_buf[33], rx_buf[32]; + uint8_t tx_buf[512], rx_buf[512]; uint8_t *tx_ptr = tx, *rx_ptr = rx; if (tx) @@ -177,31 +237,51 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) memset(tx_cpy, 0, real_byte_len); tx_ptr = tx_cpy; - /* first send 30 x 8 bits */ - tx_buf[0] = CMD_XFER; + tx_buf[0] = CMD_XFER | ((rx || (_version <= 1)) ? 0 : NO_READ); + uint16_t max_bit_transfer_length = (uint16_t[]){240, 496, 4000}[_version -1]; + assert(max_bit_transfer_length %8 == 0);//need to cut the bits on byte size. while (real_bit_len != 0) { - uint8_t bit_to_send = (real_bit_len > 240) ? 240 : real_bit_len; - uint8_t byte_to_send = (bit_to_send + 7) / 8; - tx_buf[1] = bit_to_send; - memset(tx_buf + 2, 0, 30); + uint16_t bit_to_send = (real_bit_len > max_bit_transfer_length) ? max_bit_transfer_length : real_bit_len; + size_t byte_to_send = (bit_to_send + 7) / 8; + size_t header_offset = 0; + if (_version == 3) { + tx_buf[1] = (bit_to_send >> 8) & 0xFF; + tx_buf[2] = bit_to_send & 0xFF; + header_offset = 3; + } else if (bit_to_send > 255) { + tx_buf[0] |= EXTEND_LENGTH; + tx_buf[1] = bit_to_send - 256; + header_offset = 2; + }else { + tx_buf[0] &= ~EXTEND_LENGTH; + tx_buf[1] = bit_to_send; + header_offset = 2; + } + memset(tx_buf + header_offset, 0, byte_to_send); for (int i = 0; i < bit_to_send; i++) if (tx_ptr[i >> 3] & (1 << (i & 0x07))) - tx_buf[2 + (i >> 3)] |= (0x80 >> (i & 0x07)); - - tx_buf[2 + byte_to_send] = CMD_STOP; - + tx_buf[header_offset + (i >> 3)] |= (0x80 >> (i & 0x07)); + + actual_length = 0; int ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - (unsigned char *)tx_buf, 33, &actual_length, 1000); - if (ret < 0) { - cerr << "writeTDI: fill: usb bulk write failed " << ret << endl; + (unsigned char *)tx_buf, (byte_to_send + header_offset), &actual_length, 1000); + if ((ret < 0) || (actual_length != (int)(byte_to_send + header_offset))) { + cerr << "writeTDI: fill: usb bulk write failed " << actual_length << endl; return EXIT_FAILURE; } + //cerr << actual_length << ", " << bit_to_send << endl; - ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, - rx_buf, 32, &actual_length, 1000); - if (ret < 0) { - cerr << "writeTDI: read: usb bulk write failed " << ret << endl; - return EXIT_FAILURE; + if (rx || (_version <= 1)) { + int transfer_length = (bit_to_send > 255) ? byte_to_send :32; + do { + ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, + rx_buf, transfer_length, &actual_length, 1000); + if (ret < 0) { + cerr << "writeTDI: read: usb bulk read failed " << ret << endl; + return EXIT_FAILURE; + } + } while (actual_length == 0); + assert((size_t)actual_length >= byte_to_send); } if (rx) { @@ -223,7 +303,7 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) (tx_cpy[pos >> 3] & (1 << (pos & 0x07))) ? SIG_TDI: 0; if (sendBitBang(SIG_TMS | SIG_TDI, - SIG_TMS | (last_bit), &sig, true) != 0) { + SIG_TMS | (last_bit), (rx ? &sig : 0), true) != 0) { cerr << "writeTDI: last bit error" << endl; return -EXIT_FAILURE; } @@ -235,6 +315,7 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) } } } + return EXIT_SUCCESS; } @@ -264,11 +345,13 @@ int DirtyJtag::sendBitBang(uint8_t mask, uint8_t val, uint8_t *read, bool last) return -EXIT_FAILURE; } - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, - read, 1, &actual_length, 1000) < 0) { - cerr << "sendBitBang: usb bulk write failed 4" << endl; - return -EXIT_FAILURE; - } + do { + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, + read, 1, &actual_length, 1000) < 0) { + cerr << "sendBitBang: usb bulk read failed 4" << endl; + return -EXIT_FAILURE; + } + } while (actual_length == 0); } if (last) { diff --git a/src/dirtyJtag.hpp b/src/dirtyJtag.hpp index 515958f..249d822 100644 --- a/src/dirtyJtag.hpp +++ b/src/dirtyJtag.hpp @@ -58,10 +58,13 @@ class DirtyJtag : public JtagInterface { bool _verbose; int sendBitBang(uint8_t mask, uint8_t val, uint8_t *read, bool last); + void getVersion(); libusb_device_handle *dev_handle; libusb_context *usb_ctx; uint8_t _tdi; uint8_t _tms; + uint8_t _version; + }; #endif // SRC_DIRTYJTAG_HPP_ diff --git a/src/ftdiJtagBitbang.cpp b/src/ftdiJtagBitbang.cpp index ee33c56..d4cd946 100644 --- a/src/ftdiJtagBitbang.cpp +++ b/src/ftdiJtagBitbang.cpp @@ -107,8 +107,7 @@ int FtdiJtagBitBang::setBitmode(uint8_t mode) _bitmode = mode; int ret = ftdi_set_bitmode(_ftdi, _tck_pin | _tms_pin | _tdi_pin, _bitmode); - ftdi_usb_purge_rx_buffer(_ftdi); - ftdi_usb_purge_tx_buffer(_ftdi); + ftdi_tcioflush(_ftdi); return ret; } From 94ef653682c02fb59e66d9ec1bac6cbaab7f37b7 Mon Sep 17 00:00:00 2001 From: phdussud Date: Sat, 27 Feb 2021 10:37:07 -0800 Subject: [PATCH 2/8] Resolved bunch of comments from owner --- src/dirtyJtag.cpp | 11 +++++------ src/dirtyJtag.hpp | 1 - 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/dirtyJtag.cpp b/src/dirtyJtag.cpp index 5e280a2..e9e8767 100644 --- a/src/dirtyJtag.cpp +++ b/src/dirtyJtag.cpp @@ -61,6 +61,7 @@ enum dirtyJtagCmd { CMD_CLK = 0x06 }; +//Modifiers applicable only to DirtyJTAG2 enum CommandModifier { EXTEND_LENGTH = 0x40, NO_READ = 0x80 @@ -134,8 +135,8 @@ void DirtyJtag::getVersion() ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, rx_buf, 64, &actual_length, 1000); if (ret < 0) { - cerr << "getVersion: read: usb bulk read failed " << ret << endl; - return; + cerr << "getVersion: read: usb bulk read failed " << ret << endl; + return; } } while (actual_length == 0); if (!strncmp("DJTAG1\n", (char*)rx_buf, 7)) { @@ -148,7 +149,6 @@ void DirtyJtag::getVersion() cerr << "dirtyJtag version unknown" << endl; _version = 0; } - } @@ -261,12 +261,12 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) for (int i = 0; i < bit_to_send; i++) if (tx_ptr[i >> 3] & (1 << (i & 0x07))) tx_buf[header_offset + (i >> 3)] |= (0x80 >> (i & 0x07)); - + actual_length = 0; int ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, (unsigned char *)tx_buf, (byte_to_send + header_offset), &actual_length, 1000); if ((ret < 0) || (actual_length != (int)(byte_to_send + header_offset))) { - cerr << "writeTDI: fill: usb bulk write failed " << actual_length << endl; + cerr << "writeTDI: fill: usb bulk write failed " << ret << "actual length: " << actual_length << endl; return EXIT_FAILURE; } //cerr << actual_length << ", " << bit_to_send << endl; @@ -315,7 +315,6 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) } } } - return EXIT_SUCCESS; } diff --git a/src/dirtyJtag.hpp b/src/dirtyJtag.hpp index 249d822..3d5067c 100644 --- a/src/dirtyJtag.hpp +++ b/src/dirtyJtag.hpp @@ -65,6 +65,5 @@ class DirtyJtag : public JtagInterface { uint8_t _tdi; uint8_t _tms; uint8_t _version; - }; #endif // SRC_DIRTYJTAG_HPP_ From fdd1037d463f8e0b0c2b94f660242b2e4fd7c843 Mon Sep 17 00:00:00 2001 From: phdussud Date: Sat, 27 Feb 2021 11:35:45 -0800 Subject: [PATCH 3/8] Remove temporary support for a hypotetical version based on Teensy 4 --- src/dirtyJtag.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/dirtyJtag.cpp b/src/dirtyJtag.cpp index e9e8767..1b475e3 100644 --- a/src/dirtyJtag.cpp +++ b/src/dirtyJtag.cpp @@ -30,25 +30,12 @@ using namespace std; -//#define DIRTY_JTAG_TEENSY_4 - -#ifdef DIRTY_JTAG_TEENSY_4 - -#define DIRTYJTAG_VID 0x16C0 -#define DIRTYJTAG_PID 0x1485 - -#define DIRTYJTAG_INTF 0 -#define DIRTYJTAG_WRITE_EP 0x2 -#define DIRTYJTAG_READ_EP 0x82 - -#else #define DIRTYJTAG_VID 0x1209 #define DIRTYJTAG_PID 0xC0CA #define DIRTYJTAG_INTF 0 #define DIRTYJTAG_WRITE_EP 0x01 #define DIRTYJTAG_READ_EP 0x82 -#endif enum dirtyJtagCmd { From 5d8ec9a162b12372a539c0fe5ce2d9170605b9e8 Mon Sep 17 00:00:00 2001 From: phdussud Date: Sat, 27 Feb 2021 12:45:12 -0800 Subject: [PATCH 4/8] Attempt to satisfy a comment from the owner --- src/dirtyJtag.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/dirtyJtag.cpp b/src/dirtyJtag.cpp index 1b475e3..b1afa31 100644 --- a/src/dirtyJtag.cpp +++ b/src/dirtyJtag.cpp @@ -54,6 +54,15 @@ enum CommandModifier { NO_READ = 0x80 }; +struct version_specific +{ + uint8_t no_read; //command modifer for xfer no read + uint16_t max_bits; //max bit count that can be transferred +}; + +static version_specific v_options[4] ={{0, 240}, {0, 240}, {NO_READ, 496}, {NO_READ, 4000}}; + + enum dirtyJtagSig { SIG_TCK = (1 << 1), SIG_TDI = (1 << 2), @@ -224,8 +233,8 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) memset(tx_cpy, 0, real_byte_len); tx_ptr = tx_cpy; - tx_buf[0] = CMD_XFER | ((rx || (_version <= 1)) ? 0 : NO_READ); - uint16_t max_bit_transfer_length = (uint16_t[]){240, 496, 4000}[_version -1]; + tx_buf[0] = CMD_XFER | (rx ? 0 : v_options[_version].no_read ); + uint16_t max_bit_transfer_length = v_options[_version].max_bits; assert(max_bit_transfer_length %8 == 0);//need to cut the bits on byte size. while (real_bit_len != 0) { uint16_t bit_to_send = (real_bit_len > max_bit_transfer_length) ? max_bit_transfer_length : real_bit_len; From 6e96d8f6d0f8c03a20168f11a45d42ebeca47b2e Mon Sep 17 00:00:00 2001 From: phdussud Date: Sat, 27 Feb 2021 13:12:21 -0800 Subject: [PATCH 5/8] Conditionalization of ftdi_tcioflush --- src/ftdiJtagBitbang.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ftdiJtagBitbang.cpp b/src/ftdiJtagBitbang.cpp index d4cd946..fd937d6 100644 --- a/src/ftdiJtagBitbang.cpp +++ b/src/ftdiJtagBitbang.cpp @@ -107,7 +107,12 @@ int FtdiJtagBitBang::setBitmode(uint8_t mode) _bitmode = mode; int ret = ftdi_set_bitmode(_ftdi, _tck_pin | _tms_pin | _tdi_pin, _bitmode); +#if (FTDI_VERSION < 105) + ftdi_usb_purge_rx_buffer(_ftdi); + ftdi_usb_purge_tx_buffer(_ftdi); +#else ftdi_tcioflush(_ftdi); +#endif return ret; } From e9b1a2e61038f8a4584f80215fe0f7207ab683c4 Mon Sep 17 00:00:00 2001 From: phdussud Date: Sat, 27 Feb 2021 13:19:25 -0800 Subject: [PATCH 6/8] fix for space --- notes.txt | 10 ++ src/dirtyJtag-teensy.cpp | 367 +++++++++++++++++++++++++++++++++++++++ src/dirtyJtag.cpp | 2 +- 3 files changed, 378 insertions(+), 1 deletion(-) create mode 100644 notes.txt create mode 100644 src/dirtyJtag-teensy.cpp diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..dafe096 --- /dev/null +++ b/notes.txt @@ -0,0 +1,10 @@ +to build with mingw: +PATH=$PATH:/p/fpga/fpgalink/install/bin +mkdir build +cd build +cmake -G "MinGW Makefiles" .. -DENABLE_FPGALINK=ON //-DCMAKE_BUILD_TYPE=Debug +cmake --build . + +running in cmd window: +set path=%path%;p:\fpga\fpgalink\install\bin;P:\msys2-64\mingw32\bin +openFPGALoader.exe -c fx2 --pins D1:A6:D3:D2 --detect \ No newline at end of file diff --git a/src/dirtyJtag-teensy.cpp b/src/dirtyJtag-teensy.cpp new file mode 100644 index 0000000..e9e8767 --- /dev/null +++ b/src/dirtyJtag-teensy.cpp @@ -0,0 +1,367 @@ +/* + * Copyright (C) 2020 Gwenhael Goavec-Merou + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "dirtyJtag.hpp" +#include "display.hpp" + +using namespace std; + +//#define DIRTY_JTAG_TEENSY_4 + +#ifdef DIRTY_JTAG_TEENSY_4 + +#define DIRTYJTAG_VID 0x16C0 +#define DIRTYJTAG_PID 0x1485 + +#define DIRTYJTAG_INTF 0 +#define DIRTYJTAG_WRITE_EP 0x2 +#define DIRTYJTAG_READ_EP 0x82 + +#else +#define DIRTYJTAG_VID 0x1209 +#define DIRTYJTAG_PID 0xC0CA + +#define DIRTYJTAG_INTF 0 +#define DIRTYJTAG_WRITE_EP 0x01 +#define DIRTYJTAG_READ_EP 0x82 +#endif + + +enum dirtyJtagCmd { + CMD_STOP = 0x00, + CMD_INFO = 0x01, + CMD_FREQ = 0x02, + CMD_XFER = 0x03, + CMD_SETSIG = 0x04, + CMD_GETSIG = 0x05, + CMD_CLK = 0x06 +}; + +//Modifiers applicable only to DirtyJTAG2 +enum CommandModifier { + EXTEND_LENGTH = 0x40, + NO_READ = 0x80 +}; + +enum dirtyJtagSig { + SIG_TCK = (1 << 1), + SIG_TDI = (1 << 2), + SIG_TDO = (1 << 3), + SIG_TMS = (1 << 4) +}; + +DirtyJtag::DirtyJtag(uint32_t clkHZ, bool verbose): + _verbose(verbose), + dev_handle(NULL), usb_ctx(NULL), _tdi(0), _tms(0) +{ + int ret; + + if (libusb_init(&usb_ctx) < 0) { + cerr << "libusb init failed" << endl; + throw std::exception(); + } + + dev_handle = libusb_open_device_with_vid_pid(usb_ctx, + DIRTYJTAG_VID, DIRTYJTAG_PID); + if (!dev_handle) { + cerr << "fails to open device" << endl; + libusb_exit(usb_ctx); + throw std::exception(); + } + + ret = libusb_claim_interface(dev_handle, DIRTYJTAG_INTF); + if (ret) { + cerr << "libusb error while claiming DirtyJTAG interface" << endl; + libusb_close(dev_handle); + libusb_exit(usb_ctx); + throw std::exception(); + } + + _version = 0; + getVersion(); + + if (setClkFreq(clkHZ) < 0) { + cerr << "Fail to set frequency" << endl; + throw std::exception(); + } +} + +DirtyJtag::~DirtyJtag() +{ + if (dev_handle) + libusb_close(dev_handle); + if (usb_ctx) + libusb_exit(usb_ctx); +} + +void DirtyJtag::getVersion() +{ + int actual_length; + int ret; + uint8_t buf[] = {CMD_INFO, + CMD_STOP}; + uint8_t rx_buf[64]; + ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, 2, &actual_length, 1000); + if (ret < 0) { + cerr << "getVersion: usb bulk write failed " << ret << endl; + return; + } + do { + ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, + rx_buf, 64, &actual_length, 1000); + if (ret < 0) { + cerr << "getVersion: read: usb bulk read failed " << ret << endl; + return; + } + } while (actual_length == 0); + if (!strncmp("DJTAG1\n", (char*)rx_buf, 7)) { + _version = 1; + } else if (!strncmp("DJTAG2\n", (char*)rx_buf, 7)) { + _version = 2; + } else if (!strncmp("DJTAG3\n", (char*)rx_buf, 7)) { + _version = 3; + } else { + cerr << "dirtyJtag version unknown" << endl; + _version = 0; + } +} + + + +int DirtyJtag::setClkFreq(uint32_t clkHZ) +{ + int actual_length; + int ret, req_freq = clkHZ; + + if (clkHZ > 16000000) { + printWarn("DirtyJTAG probe limited to 16000kHz"); + clkHZ = 16000000; + } + + printInfo("Jtag frequency : requested " + std::to_string(req_freq) + + "Hz -> real " + std::to_string(clkHZ) + "Hz"); + + uint8_t buf[] = {CMD_FREQ, + static_cast(0xff & ((clkHZ / 1000) >> 8)), + static_cast(0xff & ((clkHZ / 1000) )), + CMD_STOP}; + ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, 4, &actual_length, 1000); + if (ret < 0) { + cerr << "setClkFreq: usb bulk write failed " << ret << endl; + return -EXIT_FAILURE; + } + + return clkHZ; +} + +int DirtyJtag::writeTMS(uint8_t *tms, int len, bool flush_buffer) +{ + (void) flush_buffer; + + if (len == 0) + return 0; + + for (int i = 0; i < len; i++) { + bool val = (tms[i >> 3] & (1 << (i & 0x07))); + sendBitBang(SIG_TMS, ((val)?SIG_TMS : 0), NULL, (i == len-1)); + } + return len; +} + +int DirtyJtag::toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len) +{ + int actual_length; + uint8_t buf[] = {CMD_CLK, + static_cast(((tms) ? SIG_TMS : 0) | ((tdi) ? SIG_TDI : 0)), + 0, + CMD_STOP}; + while (clk_len > 0) { + buf[2] = (clk_len > 64) ? 64 : (uint8_t)clk_len; + + int ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, 4, &actual_length, 1000); + if (ret < 0) { + cerr << "toggleClk: usb bulk write failed " << ret << endl; + return -EXIT_FAILURE; + } + clk_len -= buf[2]; + } + + return EXIT_SUCCESS; +} + +int DirtyJtag::flush() +{ + return 0; +} + +int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) +{ + int actual_length; + uint32_t real_bit_len = len - (end ? 1 : 0); + uint32_t real_byte_len = (len + 7) / 8; + + uint8_t tx_cpy[real_byte_len]; + uint8_t tx_buf[512], rx_buf[512]; + uint8_t *tx_ptr = tx, *rx_ptr = rx; + + if (tx) + memcpy(tx_cpy, tx, real_byte_len); + else + memset(tx_cpy, 0, real_byte_len); + tx_ptr = tx_cpy; + + tx_buf[0] = CMD_XFER | ((rx || (_version <= 1)) ? 0 : NO_READ); + uint16_t max_bit_transfer_length = (uint16_t[]){240, 496, 4000}[_version -1]; + assert(max_bit_transfer_length %8 == 0);//need to cut the bits on byte size. + while (real_bit_len != 0) { + uint16_t bit_to_send = (real_bit_len > max_bit_transfer_length) ? max_bit_transfer_length : real_bit_len; + size_t byte_to_send = (bit_to_send + 7) / 8; + size_t header_offset = 0; + if (_version == 3) { + tx_buf[1] = (bit_to_send >> 8) & 0xFF; + tx_buf[2] = bit_to_send & 0xFF; + header_offset = 3; + } else if (bit_to_send > 255) { + tx_buf[0] |= EXTEND_LENGTH; + tx_buf[1] = bit_to_send - 256; + header_offset = 2; + }else { + tx_buf[0] &= ~EXTEND_LENGTH; + tx_buf[1] = bit_to_send; + header_offset = 2; + } + memset(tx_buf + header_offset, 0, byte_to_send); + for (int i = 0; i < bit_to_send; i++) + if (tx_ptr[i >> 3] & (1 << (i & 0x07))) + tx_buf[header_offset + (i >> 3)] |= (0x80 >> (i & 0x07)); + + actual_length = 0; + int ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + (unsigned char *)tx_buf, (byte_to_send + header_offset), &actual_length, 1000); + if ((ret < 0) || (actual_length != (int)(byte_to_send + header_offset))) { + cerr << "writeTDI: fill: usb bulk write failed " << ret << "actual length: " << actual_length << endl; + return EXIT_FAILURE; + } + //cerr << actual_length << ", " << bit_to_send << endl; + + if (rx || (_version <= 1)) { + int transfer_length = (bit_to_send > 255) ? byte_to_send :32; + do { + ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, + rx_buf, transfer_length, &actual_length, 1000); + if (ret < 0) { + cerr << "writeTDI: read: usb bulk read failed " << ret << endl; + return EXIT_FAILURE; + } + } while (actual_length == 0); + assert((size_t)actual_length >= byte_to_send); + } + + if (rx) { + for (int i = 0; i < bit_to_send; i++) + rx_ptr[i >> 3] = (rx_ptr[i >> 3] >> 1) | + (((rx_buf[i >> 3] << (i&0x07)) & 0x80)); + rx_ptr += byte_to_send; + } + + real_bit_len -= bit_to_send; + tx_ptr += byte_to_send; + } + + /* this step exist only with [D|I]R_SHIFT */ + if (end) { + int pos = len-1; + uint8_t sig; + unsigned char last_bit = + (tx_cpy[pos >> 3] & (1 << (pos & 0x07))) ? SIG_TDI: 0; + + if (sendBitBang(SIG_TMS | SIG_TDI, + SIG_TMS | (last_bit), (rx ? &sig : 0), true) != 0) { + cerr << "writeTDI: last bit error" << endl; + return -EXIT_FAILURE; + } + + if (rx) { + rx[pos >> 3] >>= 1; + if (sig & SIG_TDO) { + rx[pos >> 3] |= (1 << (pos & 0x07));; + } + } + } + return EXIT_SUCCESS; +} + +int DirtyJtag::sendBitBang(uint8_t mask, uint8_t val, uint8_t *read, bool last) +{ + int actual_length; + mask |= SIG_TCK; + uint8_t buf[] = { CMD_SETSIG, + static_cast(mask), + static_cast(val), + CMD_SETSIG, + static_cast(mask), + static_cast(val | SIG_TCK), + CMD_STOP}; + + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, 7, &actual_length, 1000) < 0) { + cerr << "sendBitBang: usb bulk write failed 1" << endl; + return -EXIT_FAILURE; + } + + if (read) { + uint8_t rd_buf[] = {CMD_GETSIG, CMD_STOP}; + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + rd_buf, 2, &actual_length, 1000) < 0) { + cerr << "sendBitBang: usb bulk write failed 3" << endl; + return -EXIT_FAILURE; + } + + do { + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, + read, 1, &actual_length, 1000) < 0) { + cerr << "sendBitBang: usb bulk read failed 4" << endl; + return -EXIT_FAILURE; + } + } while (actual_length == 0); + } + + if (last) { + buf[2] &= ~SIG_TCK; + buf[3] = CMD_STOP; + if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, + buf, 4, &actual_length, 1000) < 0) { + cerr << "sendBitBang usb bulk write failed" << endl; + return -EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} diff --git a/src/dirtyJtag.cpp b/src/dirtyJtag.cpp index b1afa31..b0a2e18 100644 --- a/src/dirtyJtag.cpp +++ b/src/dirtyJtag.cpp @@ -275,7 +275,7 @@ int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) if (ret < 0) { cerr << "writeTDI: read: usb bulk read failed " << ret << endl; return EXIT_FAILURE; - } + } } while (actual_length == 0); assert((size_t)actual_length >= byte_to_send); } From ea141fdcfeb19f5a65ee7b2a2dd216d39e91807f Mon Sep 17 00:00:00 2001 From: phdussud Date: Sat, 27 Feb 2021 13:25:22 -0800 Subject: [PATCH 7/8] Remove spurious files in previous commit --- notes.txt | 10 -- src/dirtyJtag-teensy.cpp | 367 --------------------------------------- 2 files changed, 377 deletions(-) delete mode 100644 notes.txt delete mode 100644 src/dirtyJtag-teensy.cpp diff --git a/notes.txt b/notes.txt deleted file mode 100644 index dafe096..0000000 --- a/notes.txt +++ /dev/null @@ -1,10 +0,0 @@ -to build with mingw: -PATH=$PATH:/p/fpga/fpgalink/install/bin -mkdir build -cd build -cmake -G "MinGW Makefiles" .. -DENABLE_FPGALINK=ON //-DCMAKE_BUILD_TYPE=Debug -cmake --build . - -running in cmd window: -set path=%path%;p:\fpga\fpgalink\install\bin;P:\msys2-64\mingw32\bin -openFPGALoader.exe -c fx2 --pins D1:A6:D3:D2 --detect \ No newline at end of file diff --git a/src/dirtyJtag-teensy.cpp b/src/dirtyJtag-teensy.cpp deleted file mode 100644 index e9e8767..0000000 --- a/src/dirtyJtag-teensy.cpp +++ /dev/null @@ -1,367 +0,0 @@ -/* - * Copyright (C) 2020 Gwenhael Goavec-Merou - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "dirtyJtag.hpp" -#include "display.hpp" - -using namespace std; - -//#define DIRTY_JTAG_TEENSY_4 - -#ifdef DIRTY_JTAG_TEENSY_4 - -#define DIRTYJTAG_VID 0x16C0 -#define DIRTYJTAG_PID 0x1485 - -#define DIRTYJTAG_INTF 0 -#define DIRTYJTAG_WRITE_EP 0x2 -#define DIRTYJTAG_READ_EP 0x82 - -#else -#define DIRTYJTAG_VID 0x1209 -#define DIRTYJTAG_PID 0xC0CA - -#define DIRTYJTAG_INTF 0 -#define DIRTYJTAG_WRITE_EP 0x01 -#define DIRTYJTAG_READ_EP 0x82 -#endif - - -enum dirtyJtagCmd { - CMD_STOP = 0x00, - CMD_INFO = 0x01, - CMD_FREQ = 0x02, - CMD_XFER = 0x03, - CMD_SETSIG = 0x04, - CMD_GETSIG = 0x05, - CMD_CLK = 0x06 -}; - -//Modifiers applicable only to DirtyJTAG2 -enum CommandModifier { - EXTEND_LENGTH = 0x40, - NO_READ = 0x80 -}; - -enum dirtyJtagSig { - SIG_TCK = (1 << 1), - SIG_TDI = (1 << 2), - SIG_TDO = (1 << 3), - SIG_TMS = (1 << 4) -}; - -DirtyJtag::DirtyJtag(uint32_t clkHZ, bool verbose): - _verbose(verbose), - dev_handle(NULL), usb_ctx(NULL), _tdi(0), _tms(0) -{ - int ret; - - if (libusb_init(&usb_ctx) < 0) { - cerr << "libusb init failed" << endl; - throw std::exception(); - } - - dev_handle = libusb_open_device_with_vid_pid(usb_ctx, - DIRTYJTAG_VID, DIRTYJTAG_PID); - if (!dev_handle) { - cerr << "fails to open device" << endl; - libusb_exit(usb_ctx); - throw std::exception(); - } - - ret = libusb_claim_interface(dev_handle, DIRTYJTAG_INTF); - if (ret) { - cerr << "libusb error while claiming DirtyJTAG interface" << endl; - libusb_close(dev_handle); - libusb_exit(usb_ctx); - throw std::exception(); - } - - _version = 0; - getVersion(); - - if (setClkFreq(clkHZ) < 0) { - cerr << "Fail to set frequency" << endl; - throw std::exception(); - } -} - -DirtyJtag::~DirtyJtag() -{ - if (dev_handle) - libusb_close(dev_handle); - if (usb_ctx) - libusb_exit(usb_ctx); -} - -void DirtyJtag::getVersion() -{ - int actual_length; - int ret; - uint8_t buf[] = {CMD_INFO, - CMD_STOP}; - uint8_t rx_buf[64]; - ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - buf, 2, &actual_length, 1000); - if (ret < 0) { - cerr << "getVersion: usb bulk write failed " << ret << endl; - return; - } - do { - ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, - rx_buf, 64, &actual_length, 1000); - if (ret < 0) { - cerr << "getVersion: read: usb bulk read failed " << ret << endl; - return; - } - } while (actual_length == 0); - if (!strncmp("DJTAG1\n", (char*)rx_buf, 7)) { - _version = 1; - } else if (!strncmp("DJTAG2\n", (char*)rx_buf, 7)) { - _version = 2; - } else if (!strncmp("DJTAG3\n", (char*)rx_buf, 7)) { - _version = 3; - } else { - cerr << "dirtyJtag version unknown" << endl; - _version = 0; - } -} - - - -int DirtyJtag::setClkFreq(uint32_t clkHZ) -{ - int actual_length; - int ret, req_freq = clkHZ; - - if (clkHZ > 16000000) { - printWarn("DirtyJTAG probe limited to 16000kHz"); - clkHZ = 16000000; - } - - printInfo("Jtag frequency : requested " + std::to_string(req_freq) + - "Hz -> real " + std::to_string(clkHZ) + "Hz"); - - uint8_t buf[] = {CMD_FREQ, - static_cast(0xff & ((clkHZ / 1000) >> 8)), - static_cast(0xff & ((clkHZ / 1000) )), - CMD_STOP}; - ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - buf, 4, &actual_length, 1000); - if (ret < 0) { - cerr << "setClkFreq: usb bulk write failed " << ret << endl; - return -EXIT_FAILURE; - } - - return clkHZ; -} - -int DirtyJtag::writeTMS(uint8_t *tms, int len, bool flush_buffer) -{ - (void) flush_buffer; - - if (len == 0) - return 0; - - for (int i = 0; i < len; i++) { - bool val = (tms[i >> 3] & (1 << (i & 0x07))); - sendBitBang(SIG_TMS, ((val)?SIG_TMS : 0), NULL, (i == len-1)); - } - return len; -} - -int DirtyJtag::toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len) -{ - int actual_length; - uint8_t buf[] = {CMD_CLK, - static_cast(((tms) ? SIG_TMS : 0) | ((tdi) ? SIG_TDI : 0)), - 0, - CMD_STOP}; - while (clk_len > 0) { - buf[2] = (clk_len > 64) ? 64 : (uint8_t)clk_len; - - int ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - buf, 4, &actual_length, 1000); - if (ret < 0) { - cerr << "toggleClk: usb bulk write failed " << ret << endl; - return -EXIT_FAILURE; - } - clk_len -= buf[2]; - } - - return EXIT_SUCCESS; -} - -int DirtyJtag::flush() -{ - return 0; -} - -int DirtyJtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) -{ - int actual_length; - uint32_t real_bit_len = len - (end ? 1 : 0); - uint32_t real_byte_len = (len + 7) / 8; - - uint8_t tx_cpy[real_byte_len]; - uint8_t tx_buf[512], rx_buf[512]; - uint8_t *tx_ptr = tx, *rx_ptr = rx; - - if (tx) - memcpy(tx_cpy, tx, real_byte_len); - else - memset(tx_cpy, 0, real_byte_len); - tx_ptr = tx_cpy; - - tx_buf[0] = CMD_XFER | ((rx || (_version <= 1)) ? 0 : NO_READ); - uint16_t max_bit_transfer_length = (uint16_t[]){240, 496, 4000}[_version -1]; - assert(max_bit_transfer_length %8 == 0);//need to cut the bits on byte size. - while (real_bit_len != 0) { - uint16_t bit_to_send = (real_bit_len > max_bit_transfer_length) ? max_bit_transfer_length : real_bit_len; - size_t byte_to_send = (bit_to_send + 7) / 8; - size_t header_offset = 0; - if (_version == 3) { - tx_buf[1] = (bit_to_send >> 8) & 0xFF; - tx_buf[2] = bit_to_send & 0xFF; - header_offset = 3; - } else if (bit_to_send > 255) { - tx_buf[0] |= EXTEND_LENGTH; - tx_buf[1] = bit_to_send - 256; - header_offset = 2; - }else { - tx_buf[0] &= ~EXTEND_LENGTH; - tx_buf[1] = bit_to_send; - header_offset = 2; - } - memset(tx_buf + header_offset, 0, byte_to_send); - for (int i = 0; i < bit_to_send; i++) - if (tx_ptr[i >> 3] & (1 << (i & 0x07))) - tx_buf[header_offset + (i >> 3)] |= (0x80 >> (i & 0x07)); - - actual_length = 0; - int ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - (unsigned char *)tx_buf, (byte_to_send + header_offset), &actual_length, 1000); - if ((ret < 0) || (actual_length != (int)(byte_to_send + header_offset))) { - cerr << "writeTDI: fill: usb bulk write failed " << ret << "actual length: " << actual_length << endl; - return EXIT_FAILURE; - } - //cerr << actual_length << ", " << bit_to_send << endl; - - if (rx || (_version <= 1)) { - int transfer_length = (bit_to_send > 255) ? byte_to_send :32; - do { - ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, - rx_buf, transfer_length, &actual_length, 1000); - if (ret < 0) { - cerr << "writeTDI: read: usb bulk read failed " << ret << endl; - return EXIT_FAILURE; - } - } while (actual_length == 0); - assert((size_t)actual_length >= byte_to_send); - } - - if (rx) { - for (int i = 0; i < bit_to_send; i++) - rx_ptr[i >> 3] = (rx_ptr[i >> 3] >> 1) | - (((rx_buf[i >> 3] << (i&0x07)) & 0x80)); - rx_ptr += byte_to_send; - } - - real_bit_len -= bit_to_send; - tx_ptr += byte_to_send; - } - - /* this step exist only with [D|I]R_SHIFT */ - if (end) { - int pos = len-1; - uint8_t sig; - unsigned char last_bit = - (tx_cpy[pos >> 3] & (1 << (pos & 0x07))) ? SIG_TDI: 0; - - if (sendBitBang(SIG_TMS | SIG_TDI, - SIG_TMS | (last_bit), (rx ? &sig : 0), true) != 0) { - cerr << "writeTDI: last bit error" << endl; - return -EXIT_FAILURE; - } - - if (rx) { - rx[pos >> 3] >>= 1; - if (sig & SIG_TDO) { - rx[pos >> 3] |= (1 << (pos & 0x07));; - } - } - } - return EXIT_SUCCESS; -} - -int DirtyJtag::sendBitBang(uint8_t mask, uint8_t val, uint8_t *read, bool last) -{ - int actual_length; - mask |= SIG_TCK; - uint8_t buf[] = { CMD_SETSIG, - static_cast(mask), - static_cast(val), - CMD_SETSIG, - static_cast(mask), - static_cast(val | SIG_TCK), - CMD_STOP}; - - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - buf, 7, &actual_length, 1000) < 0) { - cerr << "sendBitBang: usb bulk write failed 1" << endl; - return -EXIT_FAILURE; - } - - if (read) { - uint8_t rd_buf[] = {CMD_GETSIG, CMD_STOP}; - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - rd_buf, 2, &actual_length, 1000) < 0) { - cerr << "sendBitBang: usb bulk write failed 3" << endl; - return -EXIT_FAILURE; - } - - do { - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP, - read, 1, &actual_length, 1000) < 0) { - cerr << "sendBitBang: usb bulk read failed 4" << endl; - return -EXIT_FAILURE; - } - } while (actual_length == 0); - } - - if (last) { - buf[2] &= ~SIG_TCK; - buf[3] = CMD_STOP; - if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, - buf, 4, &actual_length, 1000) < 0) { - cerr << "sendBitBang usb bulk write failed" << endl; - return -EXIT_FAILURE; - } - } - - return EXIT_SUCCESS; -} From c5753af4744e97c14ba7aed46b4a2f5f1abd4a66 Mon Sep 17 00:00:00 2001 From: phdussud Date: Mon, 1 Mar 2021 11:01:26 -0800 Subject: [PATCH 8/8] Add a note about DirtyJtag compatibility --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f187c92..dbaddf4 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ __Supported cables:__ * anlogic JTAG adapter * [digilent_hs2](https://store.digilentinc.com/jtag-hs2-programming-cable/): jtag programmer cable from digilent * [DirtyJTAG](https://github.com/jeanthom/DirtyJTAG): JTAG probe firmware for STM32F1 + (Best to use release (1.4 or newer) or limit the --freq to 600000 with older releases. New version https://github.com/jeanthom/DirtyJTAG/tree/dirtyjtag2 is also supported) * Intel USB Blaster: jtag programmer cable from intel/altera * JTAG-HS3: jtag programmer cable from digilent * FT2232: generic programmer cable based on Ftdi FT2232