introduce DirtyJTAG cable

This commit is contained in:
Gwenhael Goavec-Merou 2020-06-14 15:41:00 +02:00
parent ace902cb98
commit 27b0c8a664
6 changed files with 367 additions and 3 deletions

View File

@ -45,6 +45,7 @@ check_cxx_source_compiles("
argp_parse(NULL, argc, argv, 0, 0, NULL);}" HAVE_ARGP)
set(OPENFPGALOADER_SOURCE
src/dirtyJtag.cpp
src/spiFlash.cpp
src/epcq.cpp
src/svf_jtag.cpp
@ -71,6 +72,7 @@ set(OPENFPGALOADER_SOURCE
set(OPENFPGALOADER_HEADERS
src/altera.hpp
src/dirtyJtag.hpp
src/progressBar.hpp
src/bitparser.hpp
src/ftdiJtagBitbang.hpp

View File

@ -29,6 +29,7 @@ __Supported (tested) FPGA:__
__Supported cables:__
* [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
* JTAG-HS3: jtag programmer cable from digilent
* FT2232: generic programmer cable based on Ftdi FT2232
* Tang Nano USB-JTAG interface: FT2232C clone based on CH552 microcontroler

View File

@ -11,7 +11,8 @@
*/
enum {
MODE_FTDI_BITBANG = 0, /*! used with ft232RL/ft231x */
MODE_FTDI_SERIAL = 1 /*! ft2232, ft232H */
MODE_FTDI_SERIAL = 1, /*! ft2232, ft232H */
MODE_DIRTYJTAG = 2 /*! JTAG probe firmware for STM32F1 */
} communication_type_t;
typedef struct {
@ -27,6 +28,7 @@ static std::map <std::string, cable_t> cable_list = {
{"digilent", {MODE_FTDI_SERIAL, {0x0403, 0x6010, INTERFACE_A, 0xe8, 0xeb, 0x00, 0x60}}},
{"digilent_hs2", {MODE_FTDI_SERIAL, {0x0403, 0x6014, INTERFACE_A, 0xe8, 0xeb, 0x00, 0x60}}},
{"digilent_hs3", {MODE_FTDI_SERIAL, {0x0403, 0x6014, INTERFACE_A, 0x88, 0x8B, 0x20, 0x30}}},
{"dirtyJtag", {MODE_DIRTYJTAG, {}}},
{"ft2232", {MODE_FTDI_SERIAL, {0x0403, 0x6010, INTERFACE_A, 0x08, 0x0B, 0x08, 0x0B}}},
{"ft231X", {MODE_FTDI_BITBANG, {0x0403, 0x6015, INTERFACE_A, 0x00, 0x00, 0x00, 0x00}}},
{"ft232", {MODE_FTDI_SERIAL, {0x0403, 0x6014, INTERFACE_A, 0x08, 0x0B, 0x08, 0x0B}}},

280
src/dirtyJtag.cpp Normal file
View File

@ -0,0 +1,280 @@
/*
* Copyright (C) 2020 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include <libusb.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include "dirtyJtag.hpp"
#include "display.hpp"
using namespace std;
#define DIRTYJTAG_VID 0x1209
#define DIRTYJTAG_PID 0xC0CA
#define DIRTYJTAG_WRITE_EP 0x01
#define DIRTYJTAG_READ_EP 0x82
enum dirtyJtagCmd {
CMD_STOP = 0x00,
CMD_INFO = 0x01,
CMD_FREQ = 0x02,
CMD_XFER = 0x03,
CMD_SETSIG = 0x04,
CMD_GETSIG = 0x05,
CMD_CLK = 0x06
};
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, 0);
if (ret) {
cerr << "libusb error while claiming DirtyJTAG interface #0" << endl;
libusb_close(dev_handle);
libusb_exit(usb_ctx);
throw std::exception();
}
if (clkHZ > 600000) {
printInfo("DirtyJTAG probe limited to 600kHz");
clkHZ = 600000;
}
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);
}
int DirtyJtag::setClkFreq(uint32_t clkHZ)
{
int actual_length;
int ret;
uint8_t buf[] = {CMD_FREQ,
static_cast<uint8_t>(0xff & ((clkHZ / 1000) >> 8)),
static_cast<uint8_t>(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<uint8_t>(((tms) ? SIG_TMS : 0) | ((tdi) ? SIG_TDI : 0)),
0,
CMD_STOP};
while (clk_len > 0) {
buf[2] = (clk_len > 100) ? 100 : (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[33], rx_buf[32];
uint8_t *tx_ptr = tx, *rx_ptr = rx;
if (tx)
memcpy(tx_cpy, tx, real_byte_len);
else
bzero(tx_cpy, real_byte_len);
tx_ptr = tx_cpy;
/* first send 30 x 8 bits */
tx_buf[0] = CMD_XFER;
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;
bzero(tx_buf + 2, 30);
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;
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;
return EXIT_FAILURE;
}
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) {
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_buf[pos >> 3] & (1 << pos & 0x07)) ? SIG_TDI: 0;
if (sendBitBang(SIG_TMS | SIG_TDI,
SIG_TMS | (last_bit), &sig, 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<uint8_t>(mask),
static_cast<uint8_t>(val),
CMD_SETSIG,
static_cast<uint8_t>(mask),
static_cast<uint8_t>(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;
}
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;
}
}
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;
}

69
src/dirtyJtag.hpp Normal file
View File

@ -0,0 +1,69 @@
/*
* Copyright (C) 2020 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef SRC_DIRTYJTAG_HPP_
#define SRC_DIRTYJTAG_HPP_
#include <libusb.h>
#include "jtagInterface.hpp"
/*!
* \file DirtyJtag.hpp
* \class DirtyJtag
* \brief concrete class between jtag implementation and FTDI capable bitbang mode
* \author Gwenhael Goavec-Merou
*/
class DirtyJtag : public JtagInterface {
public:
DirtyJtag(uint32_t clkHZ, bool verbose);
virtual ~DirtyJtag();
int setClkFreq(uint32_t clkHZ) override;
int setClkFreq(uint32_t clkHZ, char use_divide_by_5) override {
(void)use_divide_by_5; return setClkFreq(clkHZ);}
/* TMS */
int writeTMS(uint8_t *tms, int len, bool flush_buffer) override;
/* TDI */
int writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override;
/* clk */
int toggleClk(uint8_t tms, uint8_t tdo, uint32_t clk_len) override;
/*!
* \brief return internal buffer size (in byte).
* \return _buffer_size divided by 2 (two byte for clk) and divided by 8 (one
* state == one byte)
*/
int get_buffer_size() override { return 0;}
bool isFull() override { return false;}
int flush() override;
private:
bool _verbose;
int sendBitBang(uint8_t mask, uint8_t val, uint8_t *read, bool last);
libusb_device_handle *dev_handle;
libusb_context *usb_ctx;
uint8_t _tdi;
uint8_t _tms;
};
#endif // SRC_DIRTYJTAG_HPP_

View File

@ -29,6 +29,7 @@
#include "ftdipp_mpsse.hpp"
#include "ftdiJtagBitbang.hpp"
#include "ftdiJtagMPSSE.hpp"
#include "dirtyJtag.hpp"
using namespace std;
@ -92,12 +93,21 @@ Jtag::~Jtag()
void Jtag::init_internal(cable_t &cable, const string &dev,
const jtag_pins_conf_t *pin_conf, uint32_t clkHZ)
{
if (cable.type == MODE_FTDI_BITBANG) {
switch (cable.type) {
case MODE_FTDI_BITBANG:
if (pin_conf == NULL)
throw std::exception();
_jtag = new FtdiJtagBitBang(cable.config, pin_conf, dev, clkHZ, _verbose);
} else {
break;
case MODE_FTDI_SERIAL:
_jtag = new FtdiJtagMPSSE(cable.config, dev, clkHZ, _verbose);
break;
case MODE_DIRTYJTAG:
_jtag = new DirtyJtag(clkHZ, _verbose);
break;
default:
std::cerr << "Jtag: unknown cable type" << std::endl;
throw std::exception();
}
_tms_buffer = (unsigned char *)malloc(sizeof(unsigned char) * _tms_buffer_size);