From 59b56bcc95e845e498f34a91a1c24f2fb098f58b Mon Sep 17 00:00:00 2001 From: Gwenhael Goavec-Merou Date: Sun, 29 Oct 2023 06:41:39 +0100 Subject: [PATCH] all jtag cable: no more hardcoding tdi bit with writeTMS --- src/anlogicCable.cpp | 3 ++- src/anlogicCable.hpp | 2 +- src/ch347jtag.cpp | 3 ++- src/ch347jtag.hpp | 2 +- src/ch552_jtag.cpp | 3 ++- src/ch552_jtag.hpp | 2 +- src/cmsisDAP.cpp | 3 ++- src/cmsisDAP.hpp | 2 +- src/dirtyJtag.cpp | 5 +++-- src/dirtyJtag.hpp | 2 +- src/ftdiJtagBitbang.cpp | 3 ++- src/ftdiJtagBitbang.hpp | 2 +- src/ftdiJtagMPSSE.cpp | 16 ++++++++++------ src/ftdiJtagMPSSE.hpp | 2 +- src/jetsonNanoJtagBitbang.cpp | 3 ++- src/jetsonNanoJtagBitbang.hpp | 2 +- src/jlink.cpp | 3 ++- src/jlink.hpp | 2 +- src/jtag.cpp | 7 ++++--- src/jtag.hpp | 3 ++- src/jtagInterface.hpp | 2 +- src/libgpiodJtagBitbang.cpp | 3 ++- src/libgpiodJtagBitbang.hpp | 2 +- src/remoteBitbang_client.cpp | 2 +- src/remoteBitbang_client.hpp | 2 +- src/usbBlaster.cpp | 3 ++- src/usbBlaster.hpp | 2 +- src/xvc_client.cpp | 3 ++- src/xvc_client.hpp | 2 +- 29 files changed, 54 insertions(+), 37 deletions(-) diff --git a/src/anlogicCable.cpp b/src/anlogicCable.cpp index e37840fd38..d6f6ffb07f 100644 --- a/src/anlogicCable.cpp +++ b/src/anlogicCable.cpp @@ -136,7 +136,8 @@ int AnlogicCable::setClkFreq(uint32_t clkHZ) return clkHZ; } -int AnlogicCable::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int AnlogicCable::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, + __attribute((unused)) const uint8_t tdi) { (void) flush_buffer; diff --git a/src/anlogicCable.hpp b/src/anlogicCable.hpp index 3ec4c4f90d..9802e238c3 100644 --- a/src/anlogicCable.hpp +++ b/src/anlogicCable.hpp @@ -25,7 +25,7 @@ class AnlogicCable : public JtagInterface { int setClkFreq(uint32_t clkHZ) override; /* TMS */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /* TDI */ int writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override; /* clk */ diff --git a/src/ch347jtag.cpp b/src/ch347jtag.cpp index 15011f90cc..ca2ce53c2e 100644 --- a/src/ch347jtag.cpp +++ b/src/ch347jtag.cpp @@ -268,7 +268,8 @@ int CH347Jtag::_setClkFreq(uint32_t clkHZ) return _clkHZ; } -int CH347Jtag::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int CH347Jtag::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, + __attribute__((unused)) const uint8_t tdi) { if (get_obuf_length() < (int)(len * 2 + 4)) { // check if there is enough room left flush(); diff --git a/src/ch347jtag.hpp b/src/ch347jtag.hpp index 4abb4de0eb..db61d99f59 100644 --- a/src/ch347jtag.hpp +++ b/src/ch347jtag.hpp @@ -18,7 +18,7 @@ class CH347Jtag : public JtagInterface { int setClkFreq(uint32_t clkHZ) override { return _setClkFreq(clkHZ); }; int _setClkFreq(uint32_t clkHZ); /* TMS */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /* TDI */ int writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override; /* clk */ diff --git a/src/ch552_jtag.cpp b/src/ch552_jtag.cpp index 7462f786a9..cdd00dfcb8 100644 --- a/src/ch552_jtag.cpp +++ b/src/ch552_jtag.cpp @@ -88,7 +88,8 @@ int CH552_jtag::setClkFreq(uint32_t clkHZ) { return ret; } -int CH552_jtag::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int CH552_jtag::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, + __attribute__((unused)) const uint8_t tdi) { (void) flush_buffer; display("%s %d %d\n", __func__, len, (len/8)+1); diff --git a/src/ch552_jtag.hpp b/src/ch552_jtag.hpp index bd3fd5db86..99fce03b35 100644 --- a/src/ch552_jtag.hpp +++ b/src/ch552_jtag.hpp @@ -32,7 +32,7 @@ class CH552_jtag : public JtagInterface, private FTDIpp_MPSSE { uint32_t getClkFreq() override {return FTDIpp_MPSSE::getClkFreq();} /* TMS */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /* clock */ int toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len) override; /* TDI */ diff --git a/src/cmsisDAP.cpp b/src/cmsisDAP.cpp index 490da947fa..92a464c188 100644 --- a/src/cmsisDAP.cpp +++ b/src/cmsisDAP.cpp @@ -313,7 +313,8 @@ int CmsisDAP::setClkFreq(uint32_t clkHZ) * flush the buffer * tms states are written only if max or if flush_buffer set */ -int CmsisDAP::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int CmsisDAP::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, + __attribute__((unused)) const uint8_t tdi) { /* nothing to send * check if the buffer must be flushed diff --git a/src/cmsisDAP.hpp b/src/cmsisDAP.hpp index ef692dc65b..59933d7bcb 100644 --- a/src/cmsisDAP.hpp +++ b/src/cmsisDAP.hpp @@ -44,7 +44,7 @@ class CmsisDAP: public JtagInterface { * \param[in] flush_buffer: force buffer to be send or not * \return <= 0 if something wrong, len otherwise */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /*! * \brief write and read len bits with optional tms set to 1 if end diff --git a/src/dirtyJtag.cpp b/src/dirtyJtag.cpp index 41c380d943..5020699458 100644 --- a/src/dirtyJtag.cpp +++ b/src/dirtyJtag.cpp @@ -171,9 +171,10 @@ int DirtyJtag::setClkFreq(uint32_t clkHZ) return clkHZ; } -int DirtyJtag::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int DirtyJtag::writeTMS(const uint8_t *tms, uint32_t len, + __attribute__((unused)) bool flush_buffer, + __attribute__((unused)) const uint8_t tdi) { - (void) flush_buffer; int actual_length; if (len == 0) diff --git a/src/dirtyJtag.hpp b/src/dirtyJtag.hpp index 1c7fcaf40b..e52fa39ae3 100644 --- a/src/dirtyJtag.hpp +++ b/src/dirtyJtag.hpp @@ -25,7 +25,7 @@ class DirtyJtag : public JtagInterface { int setClkFreq(uint32_t clkHZ) override; /* TMS */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /* TDI */ int writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override; /* clk */ diff --git a/src/ftdiJtagBitbang.cpp b/src/ftdiJtagBitbang.cpp index 96e73155d3..3358b402e9 100644 --- a/src/ftdiJtagBitbang.cpp +++ b/src/ftdiJtagBitbang.cpp @@ -116,7 +116,8 @@ int FtdiJtagBitBang::setBitmode(uint8_t mode) return ret; } -int FtdiJtagBitBang::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int FtdiJtagBitBang::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, + __attribute__((unused)) const uint8_t tdi) { int ret; diff --git a/src/ftdiJtagBitbang.hpp b/src/ftdiJtagBitbang.hpp index 44dc88a78d..224ac48902 100644 --- a/src/ftdiJtagBitbang.hpp +++ b/src/ftdiJtagBitbang.hpp @@ -31,7 +31,7 @@ class FtdiJtagBitBang : public JtagInterface, private FTDIpp_MPSSE { int setClkFreq(uint32_t clkHZ) override; /* TMS */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /* TDI */ int writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override; diff --git a/src/ftdiJtagMPSSE.cpp b/src/ftdiJtagMPSSE.cpp index 3bf73ec45f..4af3172640 100644 --- a/src/ftdiJtagMPSSE.cpp +++ b/src/ftdiJtagMPSSE.cpp @@ -107,10 +107,11 @@ void FtdiJtagMPSSE::config_edge() } } -int FtdiJtagMPSSE::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int FtdiJtagMPSSE::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi) { (void) flush_buffer; display("%s %d %d\n", __func__, len, (len/8)+1); + uint8_t curr_tdi = (tdi << 7); if (len == 0) return 0; @@ -123,14 +124,17 @@ int FtdiJtagMPSSE::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) MPSSE_BITMODE | _write_mode), 0, 0}; while (xfer > 0) { + uint8_t curr_tms = 0; int bit_to_send = (xfer > 6) ? 6 : xfer; buf[1] = bit_to_send-1; - buf[2] = 0x80; + buf[2] = curr_tdi; for (int i = 0; i < bit_to_send; i++, offset++) { - buf[2] |= - (((tms[offset >> 3] & (1 << (offset & 0x07))) ? 1 : 0) << i); + curr_tms = ((tms[offset >> 3] & (1 << (offset & 0x07))) ? 1 : 0); + + buf[2] |= (curr_tms << i); } + buf[2] |= (curr_tms << bit_to_send); pos+=3; mpsse_store(buf, 3); @@ -230,8 +234,8 @@ int FtdiJtagMPSSE::writeTDI(const uint8_t *tdi, uint8_t *tdo, uint32_t len, bool static_cast((xfer - 1) & 0xff), // low static_cast((((xfer - 1) >> 8) & 0xff))}; // high - display("%s len : %d %d %d %d\n", __func__, len, real_len, nb_byte, - nb_bit); + display("%s len : %d %d %d %d last: %d\n", __func__, len, real_len, nb_byte, + nb_bit, last); if ((nb_byte + _num + 3) > _buffer_size) mpsse_write(); diff --git a/src/ftdiJtagMPSSE.hpp b/src/ftdiJtagMPSSE.hpp index 421cd6cbf1..323f647e11 100644 --- a/src/ftdiJtagMPSSE.hpp +++ b/src/ftdiJtagMPSSE.hpp @@ -58,7 +58,7 @@ class FtdiJtagMPSSE : public JtagInterface, public FTDIpp_MPSSE { } /* TMS */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /* clock */ int toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len) override; /* TDI */ diff --git a/src/jetsonNanoJtagBitbang.cpp b/src/jetsonNanoJtagBitbang.cpp index e21f9b1213..7ecf10584b 100644 --- a/src/jetsonNanoJtagBitbang.cpp +++ b/src/jetsonNanoJtagBitbang.cpp @@ -209,7 +209,8 @@ int JetsonNanoJtagBitbang::setClkFreq(__attribute__((unused)) uint32_t clkHZ) } int JetsonNanoJtagBitbang::writeTMS(uint8_t *tms_buf, uint32_t len, - __attribute__((unused)) bool flush_buffer) + __attribute__((unused)) bool flush_buffer, + __attribute__((unused)) const uint8_t tdi) { int tms; diff --git a/src/jetsonNanoJtagBitbang.hpp b/src/jetsonNanoJtagBitbang.hpp index e999d1b297..3df287a9bb 100644 --- a/src/jetsonNanoJtagBitbang.hpp +++ b/src/jetsonNanoJtagBitbang.hpp @@ -53,7 +53,7 @@ class JetsonNanoJtagBitbang : public JtagInterface { virtual ~JetsonNanoJtagBitbang(); int setClkFreq(uint32_t clkHZ) override; - int writeTMS(uint8_t *tms_buf, uint32_t len, bool flush_buffer) override; + int writeTMS(uint8_t *tms_buf, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; int writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override; int toggleClk(uint8_t tms, uint8_t tdo, uint32_t clk_len) override; diff --git a/src/jlink.cpp b/src/jlink.cpp index 55d46165c3..a03f04b2fb 100644 --- a/src/jlink.cpp +++ b/src/jlink.cpp @@ -89,7 +89,8 @@ Jlink::~Jlink() libusb_exit(jlink_ctx); } -int Jlink::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int Jlink::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, + __attribute__((unused)) const uint8_t tdi) { // empty buffer // if asked flush diff --git a/src/jlink.hpp b/src/jlink.hpp index fce6c12cab..681db9708d 100644 --- a/src/jlink.hpp +++ b/src/jlink.hpp @@ -44,7 +44,7 @@ class Jlink: public JtagInterface { * \param[in] flush_buffer: force buffer to be send or not * \return <= 0 if something wrong, len otherwise */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /*! * \brief write and read len bits with optional tms set to 1 if end diff --git a/src/jtag.cpp b/src/jtag.cpp index 49ee67416e..52ad6e0189 100644 --- a/src/jtag.cpp +++ b/src/jtag.cpp @@ -81,7 +81,7 @@ Jtag::Jtag(const cable_t &cable, const jtag_pins_conf_t *pin_conf, _verbose(verbose > 1), _state(RUN_TEST_IDLE), _tms_buffer_size(128), _num_tms(0), - _board_name("nope"), device_index(0) + _board_name("nope"), device_index(0), _curr_tdi(1) { switch (cable.type) { case MODE_ANLOGICCABLE: @@ -319,7 +319,7 @@ int Jtag::flushTMS(bool flush_buffer) if (_num_tms != 0) { display("%s: %d %x\n", __func__, _num_tms, _tms_buffer[0]); - ret = _jtag->writeTMS(_tms_buffer, _num_tms, flush_buffer); + ret = _jtag->writeTMS(_tms_buffer, _num_tms, flush_buffer, _curr_tdi); /* reset buffer and number of bits */ memset(_tms_buffer, 0, _tms_buffer_size); @@ -433,8 +433,9 @@ int Jtag::shiftIR(unsigned char *tdi, unsigned char *tdo, int irlen, tapState_t return 0; } -void Jtag::set_state(tapState_t newState) +void Jtag::set_state(tapState_t newState, const uint8_t tdi) { + _curr_tdi = tdi; unsigned char tms = 0; while (newState != _state) { display("_state : %16s(%02d) -> %s(%02d) ", diff --git a/src/jtag.hpp b/src/jtag.hpp index 240bdda447..9e8307e77f 100644 --- a/src/jtag.hpp +++ b/src/jtag.hpp @@ -119,7 +119,7 @@ class Jtag { void toggleClk(int nb); void go_test_logic_reset(); - void set_state(tapState_t newState); + void set_state(tapState_t newState, const uint8_t tdi = 1); int flushTMS(bool flush_buffer = false); void flush() {flushTMS(); _jtag->flush();} void setTMS(unsigned char tms); @@ -160,5 +160,6 @@ class Jtag { std::vector _devices_list; /*!< ordered list of devices idcode */ std::vector _irlength_list; /*!< ordered list of irlength */ + uint8_t _curr_tdi; }; #endif diff --git a/src/jtagInterface.hpp b/src/jtagInterface.hpp index b6b203657e..171cbf5b94 100644 --- a/src/jtagInterface.hpp +++ b/src/jtagInterface.hpp @@ -53,7 +53,7 @@ class JtagInterface { * \param len: number of bit to send * \return number of bit send/received */ - virtual int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) = 0; + virtual int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) = 0; /*! * \brief send TDI bits (mainly in shift DR/IR state) diff --git a/src/libgpiodJtagBitbang.cpp b/src/libgpiodJtagBitbang.cpp index 5b40b805c2..eee04b2eb1 100644 --- a/src/libgpiodJtagBitbang.cpp +++ b/src/libgpiodJtagBitbang.cpp @@ -292,7 +292,8 @@ int LibgpiodJtagBitbang::setClkFreq(__attribute__((unused)) uint32_t clkHZ) } int LibgpiodJtagBitbang::writeTMS(const uint8_t *tms_buf, uint32_t len, - __attribute__((unused)) bool flush_buffer) + __attribute__((unused)) bool flush_buffer, + __attribute__((unused)) uint8_t tdi) { int tms; diff --git a/src/libgpiodJtagBitbang.hpp b/src/libgpiodJtagBitbang.hpp index cf239d53f6..de64ad804f 100644 --- a/src/libgpiodJtagBitbang.hpp +++ b/src/libgpiodJtagBitbang.hpp @@ -33,7 +33,7 @@ class LibgpiodJtagBitbang : public JtagInterface { virtual ~LibgpiodJtagBitbang(); int setClkFreq(uint32_t clkHZ) override; - int writeTMS(const uint8_t *tms_buf, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms_buf, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; int writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override; int toggleClk(uint8_t tms, uint8_t tdo, uint32_t clk_len) override; diff --git a/src/remoteBitbang_client.cpp b/src/remoteBitbang_client.cpp index 8c73297b34..639981e22d 100644 --- a/src/remoteBitbang_client.cpp +++ b/src/remoteBitbang_client.cpp @@ -77,7 +77,7 @@ RemoteBitbang_client::~RemoteBitbang_client() } int RemoteBitbang_client::writeTMS(const uint8_t *tms, uint32_t len, - bool flush_buffer) + bool flush_buffer, __attribute__((unused)) const uint8_t tdi) { // empty buffer // if asked flush diff --git a/src/remoteBitbang_client.hpp b/src/remoteBitbang_client.hpp index 5405b4e87c..b4b5c70a33 100644 --- a/src/remoteBitbang_client.hpp +++ b/src/remoteBitbang_client.hpp @@ -43,7 +43,7 @@ class RemoteBitbang_client: public JtagInterface { * \param[in] flush_buffer: force buffer to be send or not * \return <= 0 if something wrong, len otherwise */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /*! * \brief write and read len bits with optional tms set to 1 if end diff --git a/src/usbBlaster.cpp b/src/usbBlaster.cpp index 9e2c294d7d..51f5ab1a4c 100644 --- a/src/usbBlaster.cpp +++ b/src/usbBlaster.cpp @@ -89,7 +89,8 @@ uint32_t UsbBlaster::getClkFreq() return ll_driver->getClkFreq(); } -int UsbBlaster::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int UsbBlaster::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, + __attribute__((unused)) const uint8_t tdi) { int ret; diff --git a/src/usbBlaster.hpp b/src/usbBlaster.hpp index 277119e1f3..b04a2c636e 100644 --- a/src/usbBlaster.hpp +++ b/src/usbBlaster.hpp @@ -54,7 +54,7 @@ class UsbBlaster : public JtagInterface { * \param flush_buffer force flushing the buffer * \return number of state written * */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /* TDI */ int writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override; diff --git a/src/xvc_client.cpp b/src/xvc_client.cpp index 2c0aa9c71b..d095049350 100644 --- a/src/xvc_client.cpp +++ b/src/xvc_client.cpp @@ -87,7 +87,8 @@ XVC_client::~XVC_client() close(_sock); } -int XVC_client::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) +int XVC_client::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, + __attribute__((unused)) const uint8_t tdi) { // empty buffer // if asked flush diff --git a/src/xvc_client.hpp b/src/xvc_client.hpp index 6f363baa4b..9bd7169147 100644 --- a/src/xvc_client.hpp +++ b/src/xvc_client.hpp @@ -43,7 +43,7 @@ class XVC_client: public JtagInterface { * \param[in] flush_buffer: force buffer to be send or not * \return <= 0 if something wrong, len otherwise */ - int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override; + int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer, const uint8_t tdi = 1) override; /*! * \brief write and read len bits with optional tms set to 1 if end