From 8ee65e7f9dd8077b0f0dd960fd4d2170a6c7ee9c Mon Sep 17 00:00:00 2001 From: Gwenhael Goavec-Merou Date: Wed, 4 Jan 2023 18:50:13 +0100 Subject: [PATCH] usbBlaster,ftdiJtagMPSSE: fix alignment for 1 bit len read transaction --- src/ftdiJtagMPSSE.cpp | 5 +++-- src/usbBlaster.cpp | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ftdiJtagMPSSE.cpp b/src/ftdiJtagMPSSE.cpp index 58c477b..213fe78 100644 --- a/src/ftdiJtagMPSSE.cpp +++ b/src/ftdiJtagMPSSE.cpp @@ -271,7 +271,8 @@ int FtdiJtagMPSSE::writeTDI(uint8_t *tdi, uint8_t *tdo, uint32_t len, bool last) } unsigned char last_bit = (tdi) ? *tx_ptr : 0; - bool double_write = true; + // never double write when nb_bit == 0 + bool double_write = (nb_bit != 0) ? true : false; if (nb_bit != 0) { display("%s read/write %d bit\n", __func__, nb_bit); @@ -333,7 +334,7 @@ int FtdiJtagMPSSE::writeTDI(uint8_t *tdi, uint8_t *tdo, uint32_t len, bool last) index++; } /* in this case for 1 one it's always bit 7 */ - *rx_ptr |= ((c[index] & 0x80) << (7 - nb_bit)); + *rx_ptr |= (((c[index]) & 0x80) >> (7 - nb_bit)); } else if (_ch552WA) { mpsse_write(); ftdi_read_data(_ftdi, c, 1); diff --git a/src/usbBlaster.cpp b/src/usbBlaster.cpp index 6c7fcfa..9441dfd 100644 --- a/src/usbBlaster.cpp +++ b/src/usbBlaster.cpp @@ -211,6 +211,8 @@ int UsbBlaster::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) int num_read = _nb_bit; if (writeBit((rx)? rx_ptr:NULL, num_read/2) < 0) return -EXIT_FAILURE; + if (rx) // realign bits + *rx_ptr >>= (8 - nb_bit); } /* set TMS high */ @@ -225,7 +227,7 @@ int UsbBlaster::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) if (writeBit((rx)? &tmp:NULL, 1) < 0) return -EXIT_FAILURE; if (rx) - *rx_ptr = (tmp & 0x80) | ((*rx_ptr) >> 1); + *rx_ptr |= (tmp & 0x80) >> (7 - nb_bit); _in_buf[_nb_bit++] = mask; if (writeBit(NULL, 0) < 0) return -EXIT_FAILURE;