Skip to content

Commit 23796ef

Browse files
committed
drivers/cmsis_dap: use blocking flag instead of wait timeout
CMSIS-DAP bulk backend read op used two timeouts: transfer timeout used in libusb_fill_bulk_transfer() and wait timeout used optionally in libusb_handle_events_timeout_completed(). The real usage is limited to two cases only: 1) blocking read: the same timeout is used for both transfer and wait 2) non-blocking read: transfer timeout is used in libusb_fill_bulk_transfer(), libusb_handle_events_timeout_completed() is called with zero timeout. Use blocking flag as read op parameter to distinguish between these two cases. See also [1] Link: [1] 8596: jtag: cmsis_dap: include helper/time_support.h | https://review.openocd.org/c/openocd/+/8596 Signed-off-by: Tomas Vanek <[email protected]> Change-Id: Ia755f17dc72bb9ce8e02065fee6a064f8eec6661 Reviewed-on: https://review.openocd.org/c/openocd/+/8639 Tested-by: jenkins Reviewed-by: Paul Fertser <[email protected]>
1 parent 250ab10 commit 23796ef

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

src/jtag/drivers/cmsis_dap.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,6 @@ struct pending_scan_result {
225225
unsigned int buffer_offset;
226226
};
227227

228-
/* Read mode */
229-
enum cmsis_dap_blocking {
230-
CMSIS_DAP_NON_BLOCKING,
231-
CMSIS_DAP_BLOCKING
232-
};
233-
234228
/* Each block in FIFO can contain up to pending_queue_len transfers */
235229
static unsigned int pending_queue_len;
236230
static unsigned int tfer_max_command_size;
@@ -321,7 +315,7 @@ static void cmsis_dap_flush_read(struct cmsis_dap *dap)
321315
* USB close/open so we need to flush up to 64 old packets
322316
* to be sure all buffers are empty */
323317
for (i = 0; i < 64; i++) {
324-
int retval = dap->backend->read(dap, 10, NULL);
318+
int retval = dap->backend->read(dap, 10, CMSIS_DAP_BLOCKING);
325319
if (retval == ERROR_TIMEOUT_REACHED)
326320
break;
327321
}
@@ -338,7 +332,7 @@ static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
338332
if (dap->pending_fifo_block_count) {
339333
LOG_ERROR("pending %u blocks, flushing", dap->pending_fifo_block_count);
340334
while (dap->pending_fifo_block_count) {
341-
dap->backend->read(dap, 10, NULL);
335+
dap->backend->read(dap, 10, CMSIS_DAP_BLOCKING);
342336
dap->pending_fifo_block_count--;
343337
}
344338
dap->pending_fifo_put_idx = 0;
@@ -351,7 +345,7 @@ static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
351345
return retval;
352346

353347
/* get reply */
354-
retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, NULL);
348+
retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, CMSIS_DAP_BLOCKING);
355349
if (retval < 0)
356350
return retval;
357351

@@ -885,7 +879,7 @@ static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, enum cmsis_dap_blo
885879

886880
if (queued_retval != ERROR_OK) {
887881
/* keep reading blocks until the pipeline is empty */
888-
retval = dap->backend->read(dap, 10, NULL);
882+
retval = dap->backend->read(dap, 10, CMSIS_DAP_BLOCKING);
889883
if (retval == ERROR_TIMEOUT_REACHED || retval == 0) {
890884
/* timeout means that we flushed the pipeline,
891885
* we can safely discard remaining pending requests */
@@ -896,11 +890,7 @@ static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, enum cmsis_dap_blo
896890
}
897891

898892
/* get reply */
899-
struct timeval tv = {
900-
.tv_sec = 0,
901-
.tv_usec = 0
902-
};
903-
retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, blocking ? NULL : &tv);
893+
retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, blocking);
904894
bool timeout = (retval == ERROR_TIMEOUT_REACHED || retval == 0);
905895
if (timeout && blocking == CMSIS_DAP_NON_BLOCKING)
906896
return;

src/jtag/drivers/cmsis_dap.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,18 @@ struct cmsis_dap {
5858
bool trace_enabled;
5959
};
6060

61+
/* Read mode */
62+
enum cmsis_dap_blocking {
63+
CMSIS_DAP_NON_BLOCKING,
64+
CMSIS_DAP_BLOCKING
65+
};
66+
6167
struct cmsis_dap_backend {
6268
const char *name;
6369
int (*open)(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], const char *serial);
6470
void (*close)(struct cmsis_dap *dap);
6571
int (*read)(struct cmsis_dap *dap, int transfer_timeout_ms,
66-
struct timeval *wait_timeout);
72+
enum cmsis_dap_blocking blocking);
6773
int (*write)(struct cmsis_dap *dap, int len, int timeout_ms);
6874
int (*packet_buffer_alloc)(struct cmsis_dap *dap, unsigned int pkt_sz);
6975
void (*packet_buffer_free)(struct cmsis_dap *dap);

src/jtag/drivers/cmsis_dap_usb_bulk.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ static void LIBUSB_CALL cmsis_dap_usb_callback(struct libusb_transfer *transfer)
441441
}
442442

443443
static int cmsis_dap_usb_read(struct cmsis_dap *dap, int transfer_timeout_ms,
444-
struct timeval *wait_timeout)
444+
enum cmsis_dap_blocking blocking)
445445
{
446446
int transferred = 0;
447447
int err;
@@ -464,20 +464,23 @@ static int cmsis_dap_usb_read(struct cmsis_dap *dap, int transfer_timeout_ms,
464464
}
465465
}
466466

467-
struct timeval tv = {
468-
.tv_sec = transfer_timeout_ms / 1000,
469-
.tv_usec = transfer_timeout_ms % 1000 * 1000
470-
};
467+
struct timeval tv;
468+
if (blocking == CMSIS_DAP_NON_BLOCKING) {
469+
tv.tv_sec = 0;
470+
tv.tv_usec = 0;
471+
} else {
472+
tv.tv_sec = transfer_timeout_ms / 1000;
473+
tv.tv_usec = transfer_timeout_ms % 1000 * 1000;
474+
}
471475

472476
while (tr->status == CMSIS_DAP_TRANSFER_PENDING) {
473-
err = libusb_handle_events_timeout_completed(dap->bdata->usb_ctx,
474-
wait_timeout ? wait_timeout : &tv,
477+
err = libusb_handle_events_timeout_completed(dap->bdata->usb_ctx, &tv,
475478
&tr->status);
476479
if (err) {
477480
LOG_ERROR("error handling USB events: %s", libusb_strerror(err));
478481
return ERROR_FAIL;
479482
}
480-
if (wait_timeout)
483+
if (tv.tv_sec == 0 && tv.tv_usec == 0)
481484
break;
482485
}
483486

src/jtag/drivers/cmsis_dap_usb_hid.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,13 @@ static void cmsis_dap_hid_close(struct cmsis_dap *dap)
206206
}
207207

208208
static int cmsis_dap_hid_read(struct cmsis_dap *dap, int transfer_timeout_ms,
209-
struct timeval *wait_timeout)
209+
enum cmsis_dap_blocking blocking)
210210
{
211-
int timeout_ms;
212-
if (wait_timeout)
213-
timeout_ms = wait_timeout->tv_usec / 1000 + wait_timeout->tv_sec * 1000;
214-
else
215-
timeout_ms = transfer_timeout_ms;
211+
int wait_ms = (blocking == CMSIS_DAP_NON_BLOCKING) ? 0 : transfer_timeout_ms;
216212

217213
int retval = hid_read_timeout(dap->bdata->dev_handle,
218214
dap->packet_buffer, dap->packet_buffer_size,
219-
timeout_ms);
215+
wait_ms);
220216
if (retval == 0) {
221217
return ERROR_TIMEOUT_REACHED;
222218
} else if (retval == -1) {

0 commit comments

Comments
 (0)