unsigned int buffer_offset;
};
-/* Read mode */
-enum cmsis_dap_blocking {
- CMSIS_DAP_NON_BLOCKING,
- CMSIS_DAP_BLOCKING
-};
-
/* Each block in FIFO can contain up to pending_queue_len transfers */
static unsigned int pending_queue_len;
static unsigned int tfer_max_command_size;
* USB close/open so we need to flush up to 64 old packets
* to be sure all buffers are empty */
for (i = 0; i < 64; i++) {
- int retval = dap->backend->read(dap, 10, NULL);
+ int retval = dap->backend->read(dap, 10, CMSIS_DAP_BLOCKING);
if (retval == ERROR_TIMEOUT_REACHED)
break;
}
if (dap->pending_fifo_block_count) {
LOG_ERROR("pending %u blocks, flushing", dap->pending_fifo_block_count);
while (dap->pending_fifo_block_count) {
- dap->backend->read(dap, 10, NULL);
+ dap->backend->read(dap, 10, CMSIS_DAP_BLOCKING);
dap->pending_fifo_block_count--;
}
dap->pending_fifo_put_idx = 0;
return retval;
/* get reply */
- retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, NULL);
+ retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, CMSIS_DAP_BLOCKING);
if (retval < 0)
return retval;
if (queued_retval != ERROR_OK) {
/* keep reading blocks until the pipeline is empty */
- retval = dap->backend->read(dap, 10, NULL);
+ retval = dap->backend->read(dap, 10, CMSIS_DAP_BLOCKING);
if (retval == ERROR_TIMEOUT_REACHED || retval == 0) {
/* timeout means that we flushed the pipeline,
* we can safely discard remaining pending requests */
}
/* get reply */
- struct timeval tv = {
- .tv_sec = 0,
- .tv_usec = 0
- };
- retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, blocking ? NULL : &tv);
+ retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS, blocking);
bool timeout = (retval == ERROR_TIMEOUT_REACHED || retval == 0);
if (timeout && blocking == CMSIS_DAP_NON_BLOCKING)
return;
bool trace_enabled;
};
+/* Read mode */
+enum cmsis_dap_blocking {
+ CMSIS_DAP_NON_BLOCKING,
+ CMSIS_DAP_BLOCKING
+};
+
struct cmsis_dap_backend {
const char *name;
int (*open)(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], const char *serial);
void (*close)(struct cmsis_dap *dap);
int (*read)(struct cmsis_dap *dap, int transfer_timeout_ms,
- struct timeval *wait_timeout);
+ enum cmsis_dap_blocking blocking);
int (*write)(struct cmsis_dap *dap, int len, int timeout_ms);
int (*packet_buffer_alloc)(struct cmsis_dap *dap, unsigned int pkt_sz);
void (*packet_buffer_free)(struct cmsis_dap *dap);
}
static int cmsis_dap_usb_read(struct cmsis_dap *dap, int transfer_timeout_ms,
- struct timeval *wait_timeout)
+ enum cmsis_dap_blocking blocking)
{
int transferred = 0;
int err;
}
}
- struct timeval tv = {
- .tv_sec = transfer_timeout_ms / 1000,
- .tv_usec = transfer_timeout_ms % 1000 * 1000
- };
+ struct timeval tv;
+ if (blocking == CMSIS_DAP_NON_BLOCKING) {
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ } else {
+ tv.tv_sec = transfer_timeout_ms / 1000;
+ tv.tv_usec = transfer_timeout_ms % 1000 * 1000;
+ }
while (tr->status == CMSIS_DAP_TRANSFER_PENDING) {
- err = libusb_handle_events_timeout_completed(dap->bdata->usb_ctx,
- wait_timeout ? wait_timeout : &tv,
+ err = libusb_handle_events_timeout_completed(dap->bdata->usb_ctx, &tv,
&tr->status);
if (err) {
LOG_ERROR("error handling USB events: %s", libusb_strerror(err));
return ERROR_FAIL;
}
- if (wait_timeout)
+ if (tv.tv_sec == 0 && tv.tv_usec == 0)
break;
}
}
static int cmsis_dap_hid_read(struct cmsis_dap *dap, int transfer_timeout_ms,
- struct timeval *wait_timeout)
+ enum cmsis_dap_blocking blocking)
{
- int timeout_ms;
- if (wait_timeout)
- timeout_ms = wait_timeout->tv_usec / 1000 + wait_timeout->tv_sec * 1000;
- else
- timeout_ms = transfer_timeout_ms;
+ int wait_ms = (blocking == CMSIS_DAP_NON_BLOCKING) ? 0 : transfer_timeout_ms;
int retval = hid_read_timeout(dap->bdata->dev_handle,
dap->packet_buffer, dap->packet_buffer_size,
- timeout_ms);
+ wait_ms);
if (retval == 0) {
return ERROR_TIMEOUT_REACHED;
} else if (retval == -1) {