Skip to content

Commit 9bc259f

Browse files
committed
esp_usb_jtag: fixed verbosity level/be more quiet
1 parent 4b1d261 commit 9bc259f

1 file changed

Lines changed: 64 additions & 30 deletions

File tree

src/esp_usb_jtag.cpp

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static uint16_t esp_usb_target_chip_id = 0; /* not applicable for FPGA, they hav
243243

244244

245245
esp_usb_jtag::esp_usb_jtag(uint32_t clkHZ, int8_t verbose, int vid = ESPUSBJTAG_VID, int pid = ESPUSBJTAG_PID):
246-
_verbose(verbose),
246+
_verbose(verbose > 1),
247247
dev_handle(NULL), usb_ctx(NULL), _tdi(0), _tms(0)
248248
{
249249
int ret;
@@ -277,6 +277,18 @@ esp_usb_jtag::esp_usb_jtag(uint32_t clkHZ, int8_t verbose, int vid = ESPUSBJTAG_
277277
cerr << "Fail to set frequency" << endl;
278278
throw std::exception();
279279
}
280+
281+
//drain_in(); // FIXME: why buffer is not empty here, and why some read fails ?
282+
283+
if (1 == 0) { // debug functions
284+
uint8_t mess[256];
285+
mess[0] = 0x55;
286+
mess[1] = 0xAA;
287+
mess[2] = 0xBB;
288+
while(1)
289+
writeTDI(mess, 0, 8 * 3, false);
290+
//toggleClk(0,1, 10000);
291+
}
280292
}
281293

282294
esp_usb_jtag::~esp_usb_jtag()
@@ -424,8 +436,10 @@ int esp_usb_jtag::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer,
424436
{
425437
uint8_t buf[OUT_BUF_SZ];
426438
char mess[256];
427-
snprintf(mess, 256, "writeTMS %d %d", len, flush_buffer);
428-
printSuccess(mess);
439+
if (_verbose) {
440+
snprintf(mess, 256, "writeTMS %d %d", len, flush_buffer);
441+
printSuccess(mess);
442+
}
429443

430444
if(flush_buffer)
431445
flush();
@@ -474,7 +488,8 @@ int esp_usb_jtag::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer,
474488
printError(mess);
475489
return -EXIT_FAILURE;
476490
}
477-
cerr << "tms" << endl;
491+
if (_verbose)
492+
cerr << "tms" << endl;
478493
}
479494

480495
return len;
@@ -493,8 +508,10 @@ int esp_usb_jtag::toggleClk(uint8_t tms, uint8_t tdi, uint32_t len)
493508
{
494509
uint8_t buf[OUT_BUF_SZ];
495510
char mess[256];
496-
snprintf(mess, 256, "toggleClk %d", len);
497-
printSuccess(mess);
511+
if (_verbose) {
512+
snprintf(mess, 256, "toggleClk %d", len);
513+
printSuccess(mess);
514+
}
498515

499516
if (len == 0)
500517
return 0;
@@ -559,7 +576,8 @@ int esp_usb_jtag::setio(int srst, int tms, int tdi, int tck)
559576
int esp_usb_jtag::flush()
560577
{
561578
const uint8_t buf = (CMD_FLUSH << 4) | CMD_FLUSH;
562-
printInfo("flush");
579+
if (_verbose)
580+
printInfo("flush");
563581

564582
if (xfer(&buf, NULL, 1) < 0) {
565583
printError("ESP USB Jtag: flush failed");
@@ -586,11 +604,13 @@ int esp_usb_jtag::xfer(const uint8_t *tx, uint8_t *rx, const uint16_t length)
586604
{
587605
char mess[128];
588606
const bool is_read = (rx != NULL), is_write = (tx != NULL);
589-
snprintf(mess, 128, "xfer: rx: %d tx: %d length %d", is_read, is_write, length);
590-
printInfo(mess);
607+
if (_verbose) {
608+
snprintf(mess, 128, "xfer: rx: %d tx: %d length %d", is_read, is_write, length);
609+
printInfo(mess);
610+
}
591611
const unsigned char endpoint = (is_write) ? ESPUSBJTAG_WRITE_EP : ESPUSBJTAG_READ_EP;
592612
uint8_t *data = (is_write) ? (uint8_t *)tx : rx;
593-
if (is_write) {
613+
if (is_write && _verbose) {
594614
printf("xfer: write: ");
595615
for (int i = 0; i < length; i++)
596616
printf("%02x ", data[i]);
@@ -606,7 +626,7 @@ int esp_usb_jtag::xfer(const uint8_t *tx, uint8_t *rx, const uint16_t length)
606626
return ret;
607627
}
608628

609-
if (is_read) {
629+
if (is_read && _verbose) {
610630
printf("xfer: read: ");
611631
for (int i = 0; i < length; i++)
612632
printf("%02x ", data[i]);
@@ -624,8 +644,10 @@ int esp_usb_jtag::xfer(const uint8_t *tx, uint8_t *rx, const uint16_t length)
624644
int esp_usb_jtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
625645
{
626646
char mess[256];
627-
snprintf(mess, 256, "writeTDI: start len: %d end: %d", len, end);
628-
printSuccess(mess);
647+
if (_verbose) {
648+
snprintf(mess, 256, "writeTDI: start len: %d end: %d", len, end);
649+
printSuccess(mess);
650+
}
629651
int ret;
630652
uint8_t tx_buf[OUT_EP_SZ];
631653
const uint8_t tdo = !(rx == NULL); // only set cap/tdo when something to read
@@ -645,8 +667,10 @@ int esp_usb_jtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool en
645667
uint32_t real_bit_len = len - (end ? 1 : 0); // substractor 1 when the
646668
// last bit is sent with
647669
// TMS transition
648-
snprintf(mess, 256, "real_bit_len=0x%08x\n", real_bit_len);
649-
printInfo(mess);
670+
if (_verbose) {
671+
snprintf(mess, 256, "real_bit_len=0x%08x\n", real_bit_len);
672+
printInfo(mess);
673+
}
650674

651675
// drain_in();
652676
uint32_t tx_buffer_idx = 0; // reset
@@ -658,11 +682,14 @@ int esp_usb_jtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool en
658682
// 2nd (low nibble) is data
659683
// last byte in buf will have data in both nibbles, no flush
660684
// exec order: high-nibble-first, low-nibble-second
661-
cerr << "is high nibble=" << (int)is_high_nibble << endl;
662-
for(uint32_t i = 0; i < (real_bit_len + 7) >> 3; i++)
663-
cerr << " " << std::hex << (int)tx[i];
664-
cerr << endl;
665-
cerr << "tdi_bits ";
685+
if (_verbose) {
686+
cerr << "is high nibble=" << (int)is_high_nibble << endl;
687+
//int bits_in_tx_buf = 0;
688+
for(uint32_t i = 0; i < (real_bit_len + 7) >> 3; i++)
689+
cerr << " " << std::hex << (int)tx[i];
690+
cerr << endl;
691+
cerr << "tdi_bits ";
692+
}
666693

667694
for (uint32_t pos = 0; pos < real_bit_len; pos += xfer_len) {
668695
// Compute number of bits to write
@@ -686,7 +713,8 @@ int esp_usb_jtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool en
686713
for (uint32_t i = 0; i < xfer_len; i++) {
687714
uint32_t curr_pos = pos + i;
688715
_tdi = (tx[curr_pos >> 3] >> (curr_pos & 7)) & 1; // get i'th bit from rx
689-
cerr << (int)_tdi;
716+
if (_verbose)
717+
cerr << (int)_tdi;
690718
const uint8_t cmd = CMD_CLK(tdo, _tdi, _tms); // with TDO capture
691719
if(is_high_nibble) { // 1st (high nibble) = data
692720
tx_buf[tx_buffer_idx] = prev_high_nibble = cmd << 4;
@@ -698,18 +726,22 @@ int esp_usb_jtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool en
698726
}
699727

700728
/* Flush current buffer */
701-
printf("\nwriteTDI: write_ep len bytes=0x%04x\n", tx_buffer_idx);
702-
for(uint32_t j = 0; j < tx_buffer_idx; j++)
703-
printf(" %02x", tx_buf[j]);
704-
printf("\n");
705-
printf("AA\n");
729+
if (_verbose) {
730+
printf("\nwriteTDI: write_ep len bytes=0x%04x\n", tx_buffer_idx);
731+
for(uint32_t j = 0; j < tx_buffer_idx; j++)
732+
printf(" %02x", tx_buf[j]);
733+
printf("\n");
734+
printf("AA\n");
735+
}
706736
ret = xfer(tx_buf, NULL, tx_buffer_idx);
707-
printf("BB\n");
737+
if (_verbose)
738+
printf("BB\n");
708739
if (ret < 0) {
709740
printError("writeTDI: usb bulk write failed " + std::to_string(ret));
710741
return -EXIT_FAILURE;
711742
}
712-
cerr << "writeTDI write 0x" << tx_buffer_idx << " bytes" << endl;
743+
if (_verbose)
744+
cerr << "writeTDI write 0x" << tx_buffer_idx << " bytes" << endl;
713745
if (rx) {
714746
flush(); // must flush before reading
715747
// TODO support odd len for TDO
@@ -728,7 +760,8 @@ int esp_usb_jtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool en
728760
}
729761
nb_try++;
730762
} while (nb_try < 3 && ret == 0);
731-
cerr << "writeTDI read " << std::to_string(ret) << endl;
763+
if (_verbose)
764+
cerr << "writeTDI read " << std::to_string(ret) << endl;
732765
if (read_byte_len != ret) {
733766
snprintf(mess, 256, "writeTDI: usb bulk read expected=%d received=%d", read_byte_len, ret);
734767
printError(mess);
@@ -739,7 +772,8 @@ int esp_usb_jtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool en
739772
}
740773
}
741774

742-
printSuccess("WriteTDI: end");
775+
if (_verbose)
776+
printSuccess("WriteTDI: end");
743777

744778
#if 1
745779
if (end) {

0 commit comments

Comments
 (0)