Skip to content

Commit c7c0d94

Browse files
committed
dirtyJtag: improves code / nitpick
1 parent 097e236 commit c7c0d94

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

src/dirtyJtag.cpp

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#include <stdio.h>
88
#include <string.h>
99

10+
#include <cassert>
1011
#include <iostream>
1112
#include <map>
12-
#include <vector>
1313
#include <string>
14-
#include <cassert>
14+
#include <vector>
1515

1616
#include "dirtyJtag.hpp"
1717
#include "display.hpp"
@@ -64,7 +64,7 @@ enum dirtyJtagSig {
6464

6565
DirtyJtag::DirtyJtag(uint32_t clkHZ, int8_t verbose, uint16_t vid, uint16_t pid):
6666
_verbose(verbose),
67-
dev_handle(NULL), usb_ctx(NULL), _tdi(0), _tms(0)
67+
dev_handle(NULL), usb_ctx(NULL), _tdi(0), _tms(0), _version(0)
6868
{
6969
int ret;
7070

@@ -88,7 +88,6 @@ DirtyJtag::DirtyJtag(uint32_t clkHZ, int8_t verbose, uint16_t vid, uint16_t pid)
8888
throw std::exception();
8989
}
9090

91-
_version = 0;
9291
if (!getVersion())
9392
throw std::runtime_error("Fail to get version");
9493

@@ -141,8 +140,6 @@ bool DirtyJtag::getVersion()
141140
return true;
142141
}
143142

144-
145-
146143
int DirtyJtag::setClkFreq(uint32_t clkHZ)
147144
{
148145
int actual_length;
@@ -181,13 +178,14 @@ int DirtyJtag::writeTMS(const uint8_t *tms, uint32_t len,
181178
if (len == 0)
182179
return 0;
183180

184-
uint8_t mask = SIG_TCK | SIG_TMS | SIG_TDI;
181+
_tdi = (tdi) ? SIG_TDI : 0;
182+
183+
const uint8_t mask = SIG_TCK | SIG_TMS | SIG_TDI;
185184
uint8_t buf[64];
186-
u_int buffer_idx = 0;
187-
for (uint32_t i = 0; i < len; i++)
188-
{
189-
uint8_t val = (tms[i >> 3] & (1 << (i & 0x07))) ? SIG_TMS : 0;
190-
val |= tdi ? SIG_TDI : 0;
185+
uint32_t buffer_idx = 0;
186+
for (uint32_t i = 0; i < len; i++) {
187+
_tms = ((tms[i >> 3] & (1 << (i & 0x07))) ? SIG_TMS : 0);
188+
const uint8_t val = _tdi | _tms;
191189
buf[buffer_idx++] = CMD_SETSIG;
192190
buf[buffer_idx++] = mask;
193191
buf[buffer_idx++] = val;
@@ -216,13 +214,15 @@ int DirtyJtag::writeTMS(const uint8_t *tms, uint32_t len,
216214
return len;
217215
}
218216

219-
int DirtyJtag::toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len)
217+
int DirtyJtag::toggleClk(uint8_t tms, __attribute__((unused)) uint8_t tdi,
218+
uint32_t clk_len)
220219
{
221220
int actual_length;
221+
_tms = tms ? SIG_TMS : 0;
222222
uint8_t buf[] = {CMD_CLK,
223-
static_cast<uint8_t>(((tms) ? SIG_TMS : 0) | ((tdi) ? SIG_TDI : 0)),
224-
0,
225-
CMD_STOP};
223+
static_cast<uint8_t>(_tms | _tdi),
224+
0,
225+
CMD_STOP};
226226
while (clk_len > 0) {
227227
buf[2] = (clk_len > 64) ? 64 : (uint8_t)clk_len;
228228

@@ -247,7 +247,7 @@ int DirtyJtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
247247
{
248248
int actual_length;
249249
uint32_t real_bit_len = len - (end ? 1 : 0);
250-
uint32_t kRealByteLen = (len + 7) / 8;
250+
const uint32_t kRealByteLen = (len + 7) / 8;
251251

252252
uint8_t tx_cpy[kRealByteLen];
253253
uint8_t tx_buf[512], rx_buf[512];
@@ -263,10 +263,11 @@ int DirtyJtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
263263
uint16_t max_bit_transfer_length = v_options[_version].max_bits;
264264
// need to cut the bits on byte size.
265265
assert(max_bit_transfer_length % 8 == 0);
266+
uint32_t rx_cnt = 0;
266267
while (real_bit_len != 0) {
267-
uint16_t bit_to_send = (real_bit_len > max_bit_transfer_length) ?
268+
const uint16_t bit_to_send = (real_bit_len > max_bit_transfer_length) ?
268269
max_bit_transfer_length : real_bit_len;
269-
size_t byte_to_send = (bit_to_send + 7) / 8;
270+
const size_t byte_to_send = (bit_to_send + 7) / 8;
270271
size_t header_offset = 0;
271272
if (_version == 3) {
272273
tx_buf[1] = (bit_to_send >> 8) & 0xFF;
@@ -276,29 +277,32 @@ int DirtyJtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
276277
tx_buf[0] |= EXTEND_LENGTH;
277278
tx_buf[1] = bit_to_send - 256;
278279
header_offset = 2;
279-
}else {
280+
} else {
280281
tx_buf[0] &= ~EXTEND_LENGTH;
281282
tx_buf[1] = bit_to_send;
282283
header_offset = 2;
283284
}
284285
memset(tx_buf + header_offset, 0, byte_to_send);
285-
for (int i = 0; i < bit_to_send; i++)
286-
if (tx_ptr[i >> 3] & (1 << (i & 0x07)))
286+
for (int i = 0; i < bit_to_send; i++) {
287+
_tdi = (tx_ptr[i >> 3] & (1 << (i & 0x07))) ? SIG_TDI : 0;
288+
if (_tdi == SIG_TDI)
287289
tx_buf[header_offset + (i >> 3)] |= (0x80 >> (i & 0x07));
290+
}
288291

289292
actual_length = 0;
293+
const int xfer_len = byte_to_send + header_offset;
290294
int ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP,
291-
(unsigned char *)tx_buf, (byte_to_send + header_offset),
295+
(unsigned char *)tx_buf, xfer_len,
292296
&actual_length, DIRTYJTAG_TIMEOUT);
293-
if ((ret < 0) || (actual_length != (int)(byte_to_send + header_offset))) {
297+
if ((ret < 0) || (actual_length != xfer_len)) {
294298
cerr << "writeTDI: fill: usb bulk write failed " << ret <<
295299
"actual length: " << actual_length << endl;
296300
return EXIT_FAILURE;
297301
}
298302
// cerr << actual_length << ", " << bit_to_send << endl;
299303

300304
if (rx || (_version <= 1)) {
301-
int transfer_length = (bit_to_send > 255) ? byte_to_send :32;
305+
const int transfer_length = (bit_to_send > 255) ? byte_to_send : 32;
302306
do {
303307
ret = libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP,
304308
rx_buf, transfer_length, &actual_length, DIRTYJTAG_TIMEOUT);
@@ -307,14 +311,14 @@ int DirtyJtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
307311
return EXIT_FAILURE;
308312
}
309313
} while (actual_length == 0);
310-
assert((size_t)actual_length >= byte_to_send);
314+
assert(static_cast<size_t>(actual_length) >= byte_to_send);
311315
}
312316

313317
if (rx) {
314-
for (int i = 0; i < bit_to_send; i++)
315-
rx_ptr[i >> 3] = (rx_ptr[i >> 3] >> 1) |
318+
for (int i = 0; i < bit_to_send; i++, rx_cnt++) {
319+
rx_ptr[rx_cnt >> 3] = (rx_ptr[rx_cnt >> 3] >> 1) |
316320
(((rx_buf[i >> 3] << (i&0x07)) & 0x80));
317-
rx_ptr += byte_to_send;
321+
}
318322
}
319323

320324
real_bit_len -= bit_to_send;
@@ -325,15 +329,13 @@ int DirtyJtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
325329
if (end) {
326330
int pos = len-1;
327331
uint8_t sig;
328-
unsigned char last_bit =
329-
(tx_cpy[pos >> 3] & (1 << (pos & 0x07))) ? SIG_TDI: 0;
332+
_tdi = (tx_cpy[pos >> 3] & (1 << (pos & 0x07))) ? SIG_TDI: 0;
333+
_tms = SIG_TMS;
330334

331-
uint8_t mask = SIG_TMS | SIG_TDI;
332-
uint8_t val = SIG_TMS | (last_bit);
335+
if (rx) {
336+
const uint8_t mask = SIG_TMS | SIG_TDI | SIG_TCK;
337+
const uint8_t val = _tms | _tdi;
333338

334-
if (rx)
335-
{
336-
mask |= SIG_TCK;
337339
uint8_t buf[] = {
338340
CMD_SETSIG,
339341
static_cast<uint8_t>(mask),
@@ -345,39 +347,36 @@ int DirtyJtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
345347
CMD_STOP,
346348
};
347349
if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP,
348-
buf, sizeof(buf), &actual_length,
349-
DIRTYJTAG_TIMEOUT) < 0)
350-
{
350+
buf, 8, &actual_length,
351+
DIRTYJTAG_TIMEOUT) < 0) {
351352
cerr << "writeTDI: last bit error: usb bulk write failed 1" << endl;
352353
return -EXIT_FAILURE;
353354
}
354-
do
355-
{
355+
356+
do {
356357
if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_READ_EP,
357358
&sig, 1, &actual_length,
358-
DIRTYJTAG_TIMEOUT) < 0)
359-
{
359+
DIRTYJTAG_TIMEOUT) < 0) {
360360
cerr << "writeTDI: last bit error: usb bulk read failed" << endl;
361361
return -EXIT_FAILURE;
362362
}
363363
} while (actual_length == 0);
364-
rx[pos >> 3] >>= 1;
364+
365+
rx[rx_cnt >> 3] >>= 1;
365366
if (sig & SIG_TDO)
366-
{
367-
rx[pos >> 3] |= (1 << (pos & 0x07));
368-
}
367+
rx[rx_cnt >> 3] |= (1 << (rx_cnt & 0x07));
368+
369369
buf[2] &= ~SIG_TCK;
370370
buf[3] = CMD_STOP;
371371
if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP,
372372
buf, 4, &actual_length,
373-
DIRTYJTAG_TIMEOUT) < 0)
374-
{
373+
DIRTYJTAG_TIMEOUT) < 0) {
375374
cerr << "writeTDI: last bit error: usb bulk write failed 2" << endl;
376375
return -EXIT_FAILURE;
377376
}
378377

379378
} else {
380-
if (toggleClk(SIG_TMS, last_bit, 1)) {
379+
if (toggleClk(_tms, _tdi, 1)) {
381380
cerr << "writeTDI: last bit error" << endl;
382381
return -EXIT_FAILURE;
383382
}
@@ -415,8 +414,8 @@ bool DirtyJtag::_set_gpio_level(uint8_t gpio, uint8_t val)
415414
int actual_length;
416415
uint8_t buf[] = {
417416
CMD_SETSIG,
418-
static_cast<uint8_t>(gpio), // mask
419-
static_cast<uint8_t>(val), // bit set
417+
static_cast<uint8_t>(gpio), // mask
418+
static_cast<uint8_t>(val), // bit set
420419
CMD_STOP,
421420
};
422421
if (libusb_bulk_transfer(dev_handle, DIRTYJTAG_WRITE_EP, buf, 4,

0 commit comments

Comments
 (0)