static int cmsis_dap_usb_interface = -1;
+static void cmsis_dap_usb_close(struct cmsis_dap *dap);
+static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz);
+
static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], char *serial)
{
int err;
return ERROR_FAIL;
}
- dap->packet_size = packet_size + 1; /* "+ 1" for compatibility with the HID backend */
+ dap->packet_size = packet_size;
+ dap->packet_buffer_size = packet_size;
dap->bdata->usb_ctx = ctx;
dap->bdata->dev_handle = dev_handle;
dap->bdata->ep_out = ep_out;
dap->bdata->ep_in = ep_in;
dap->bdata->interface = interface_num;
+
+ dap->packet_buffer = malloc(dap->packet_buffer_size);
+ if (dap->packet_buffer == NULL) {
+ LOG_ERROR("unable to allocate memory");
+ cmsis_dap_usb_close(dap);
+ return ERROR_FAIL;
+ }
+
+ dap->command = dap->packet_buffer;
+ dap->response = dap->packet_buffer;
+
return ERROR_OK;
}
libusb_exit(dap->bdata->usb_ctx);
free(dap->bdata);
dap->bdata = NULL;
+ free(dap->packet_buffer);
+ dap->packet_buffer = NULL;
}
static int cmsis_dap_usb_read(struct cmsis_dap *dap, int timeout_ms)
}
}
- memset(&dap->packet_buffer[transferred], 0, dap->packet_size - transferred);
+ memset(&dap->packet_buffer[transferred], 0, dap->packet_buffer_size - transferred);
return transferred;
}
/* skip the first byte that is only used by the HID backend */
err = libusb_bulk_transfer(dap->bdata->dev_handle, dap->bdata->ep_out,
- dap->packet_buffer + 1, txlen - 1, &transferred, timeout_ms);
+ dap->packet_buffer, txlen, &transferred, timeout_ms);
if (err) {
if (err == LIBUSB_ERROR_TIMEOUT) {
return ERROR_TIMEOUT_REACHED;
return transferred;
}
+static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
+{
+ uint8_t *buf = malloc(pkt_sz);
+ if (buf == NULL) {
+ LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
+ return ERROR_FAIL;
+ }
+
+ dap->packet_buffer = buf;
+ dap->packet_size = pkt_sz;
+ dap->packet_buffer_size = pkt_sz;
+
+ dap->command = dap->packet_buffer;
+ dap->response = dap->packet_buffer;
+
+ return ERROR_OK;
+}
+
COMMAND_HANDLER(cmsis_dap_handle_usb_interface_command)
{
if (CMD_ARGC == 1)
.close = cmsis_dap_usb_close,
.read = cmsis_dap_usb_read,
.write = cmsis_dap_usb_write,
+ .packet_buffer_alloc = cmsis_dap_usb_alloc,
};