drivers/cmsis-dap: tidy up buffer access
authorTomas Vanek <vanekt@fbl.cz>
Tue, 23 Mar 2021 08:12:26 +0000 (09:12 +0100)
committerTomas Vanek <vanekt@fbl.cz>
Sat, 10 Apr 2021 19:54:43 +0000 (20:54 +0100)
Each one of CMSIS-DAP command handlers was responsible for setting
HID report number, which in case of USB bulk transport was not used
at all. The command had to be filled with 1 byte offset whereas
the response was read without an offset.

Introduce 'command' and 'response' pointers into struct cmsis_dap.
Use them for filling the command and read the response respectively.
CMSIS-DAP command parameter are now at positions as documented in
https://arm-software.github.io/CMSIS_5/DAP/html/group__DAP__Commands__gr.html
Adjust buffer allocation for HID and USB bulk transports.

While on it, use h_u32_to_le() and h_u16_to_le() instead of per-byte
writes.

Change-Id: Ib0808d6826ba0e254c1007ace8b743405536332a
Signed-off-by: Tomas Vanek <vanekt@fbl.cz>
Reviewed-on: http://openocd.zylin.com/6120
Reviewed-by: Adrian M Negreanu <adrian.negreanu@nxp.com>
Tested-by: jenkins
Reviewed-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
src/jtag/drivers/cmsis_dap.c
src/jtag/drivers/cmsis_dap.h
src/jtag/drivers/cmsis_dap_usb_bulk.c
src/jtag/drivers/cmsis_dap_usb_hid.c

index 7a5acc3140c2bb1dab7996a7d0430214a4024833..eb973642ca230d87e3c7db7d242a6bc677e06320 100644 (file)
@@ -307,17 +307,14 @@ static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
-
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_SWJ_PINS;
-       buffer[2] = pins;
-       buffer[3] = mask;
-       buffer[4] = delay & 0xff;
-       buffer[5] = (delay >> 8) & 0xff;
-       buffer[6] = (delay >> 16) & 0xff;
-       buffer[7] = (delay >> 24) & 0xff;
-       retval = cmsis_dap_xfer(cmsis_dap_handle, 8);
+       uint8_t *command = cmsis_dap_handle->command;
+
+       command[0] = CMD_DAP_SWJ_PINS;
+       command[1] = pins;
+       command[2] = mask;
+       h_u32_to_le(&command[3], delay);
+
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 7);
 
        if (retval != ERROR_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
@@ -325,7 +322,7 @@ static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay
        }
 
        if (input)
-               *input = buffer[1];
+               *input = cmsis_dap_handle->response[1];
 
        return ERROR_OK;
 }
@@ -333,19 +330,17 @@ static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay
 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
+       uint8_t *command = cmsis_dap_handle->command;
 
        /* set clock in Hz */
        swj_clock *= 1000;
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_SWJ_CLOCK;
-       buffer[2] = swj_clock & 0xff;
-       buffer[3] = (swj_clock >> 8) & 0xff;
-       buffer[4] = (swj_clock >> 16) & 0xff;
-       buffer[5] = (swj_clock >> 24) & 0xff;
-       retval = cmsis_dap_xfer(cmsis_dap_handle, 6);
 
-       if (retval != ERROR_OK || buffer[1] != DAP_OK) {
+       command[0] = CMD_DAP_SWJ_CLOCK;
+       h_u32_to_le(&command[1], swj_clock);
+
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 5);
+
+       if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -357,7 +352,7 @@ static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
 static int cmsis_dap_cmd_DAP_SWJ_Sequence(uint8_t s_len, const uint8_t *sequence)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
+       uint8_t *command = cmsis_dap_handle->command;
 
 #ifdef CMSIS_DAP_JTAG_DEBUG
        LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len);
@@ -367,14 +362,12 @@ static int cmsis_dap_cmd_DAP_SWJ_Sequence(uint8_t s_len, const uint8_t *sequence
        printf("\n");
 #endif
 
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_SWJ_SEQ;
-       buffer[2] = s_len;
-       bit_copy(&buffer[3], 0, sequence, 0, s_len);
+       command[0] = CMD_DAP_SWJ_SEQ;
+       command[1] = s_len;
+       bit_copy(&command[2], 0, sequence, 0, s_len);
 
-       retval = cmsis_dap_xfer(cmsis_dap_handle, DIV_ROUND_UP(s_len, 8) + 3);
-
-       if (retval != ERROR_OK || buffer[1] != DAP_OK)
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 2 + DIV_ROUND_UP(s_len, 8));
+       if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK)
                return ERROR_FAIL;
 
        return ERROR_OK;
@@ -383,19 +376,19 @@ static int cmsis_dap_cmd_DAP_SWJ_Sequence(uint8_t s_len, const uint8_t *sequence
 static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
+       uint8_t *command = cmsis_dap_handle->command;
 
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_INFO;
-       buffer[2] = info;
-       retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
+       command[0] = CMD_DAP_INFO;
+       command[1] = info;
+
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
 
        if (retval != ERROR_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
                return ERROR_JTAG_DEVICE_ERROR;
        }
 
-       *data = &(buffer[1]);
+       *data = &cmsis_dap_handle->response[1];
 
        return ERROR_OK;
 }
@@ -403,15 +396,15 @@ static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
 static int cmsis_dap_cmd_DAP_LED(uint8_t led, uint8_t state)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
+       uint8_t *command = cmsis_dap_handle->command;
 
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_LED;
-       buffer[2] = led;
-       buffer[3] = state;
-       retval = cmsis_dap_xfer(cmsis_dap_handle, 4);
+       command[0] = CMD_DAP_LED;
+       command[1] = led;
+       command[2] = state;
+
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
 
-       if (retval != ERROR_OK || buffer[1] != 0x00) {
+       if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -422,19 +415,19 @@ static int cmsis_dap_cmd_DAP_LED(uint8_t led, uint8_t state)
 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
+       uint8_t *command = cmsis_dap_handle->command;
 
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_CONNECT;
-       buffer[2] = mode;
-       retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
+       command[0] = CMD_DAP_CONNECT;
+       command[1] = mode;
+
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
 
        if (retval != ERROR_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
                return ERROR_JTAG_DEVICE_ERROR;
        }
 
-       if (buffer[1] != mode) {
+       if (cmsis_dap_handle->response[1] != mode) {
                LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -445,13 +438,13 @@ static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
 static int cmsis_dap_cmd_DAP_Disconnect(void)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
+       uint8_t *command = cmsis_dap_handle->command;
 
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_DISCONNECT;
-       retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
+       command[0] = CMD_DAP_DISCONNECT;
+
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
 
-       if (retval != ERROR_OK || buffer[1] != DAP_OK) {
+       if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -462,18 +455,16 @@ static int cmsis_dap_cmd_DAP_Disconnect(void)
 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
-
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_TFER_CONFIGURE;
-       buffer[2] = idle;
-       buffer[3] = retry_count & 0xff;
-       buffer[4] = (retry_count >> 8) & 0xff;
-       buffer[5] = match_retry & 0xff;
-       buffer[6] = (match_retry >> 8) & 0xff;
-       retval = cmsis_dap_xfer(cmsis_dap_handle, 7);
+       uint8_t *command = cmsis_dap_handle->command;
+
+       command[0] = CMD_DAP_TFER_CONFIGURE;
+       command[1] = idle;
+       h_u16_to_le(&command[2], retry_count);
+       h_u16_to_le(&command[4], match_retry);
 
-       if (retval != ERROR_OK || buffer[1] != DAP_OK) {
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 6);
+
+       if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -484,14 +475,14 @@ static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t retry_count,
 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
+       uint8_t *command = cmsis_dap_handle->command;
 
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_SWD_CONFIGURE;
-       buffer[2] = cfg;
-       retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
+       command[0] = CMD_DAP_SWD_CONFIGURE;
+       command[1] = cfg;
 
-       if (retval != ERROR_OK || buffer[1] != DAP_OK) {
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
+
+       if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -503,15 +494,14 @@ static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
 {
        int retval;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
+       uint8_t *command = cmsis_dap_handle->command;
+
+       command[0] = CMD_DAP_DELAY;
+       h_u16_to_le(&command[1], delay_us);
 
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_DELAY;
-       buffer[2] = delay_us & 0xff;
-       buffer[3] = (delay_us >> 8) & 0xff;
-       retval = cmsis_dap_xfer(cmsis_dap_handle, 4);
+       retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
 
-       if (retval != ERROR_OK || buffer[1] != DAP_OK) {
+       if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -522,7 +512,7 @@ static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
 
 static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
 {
-       uint8_t *buffer = dap->packet_buffer;
+       uint8_t *command = cmsis_dap_handle->command;
        struct pending_request_block *block = &pending_fifo[pending_fifo_put_idx];
 
        LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %d", block->transfer_count, pending_fifo_put_idx);
@@ -535,11 +525,10 @@ static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
        if (block->transfer_count == 0)
                goto skip;
 
-       size_t idx = 0;
-       buffer[idx++] = 0;      /* report number */
-       buffer[idx++] = CMD_DAP_TFER;
-       buffer[idx++] = 0x00;   /* DAP Index */
-       buffer[idx++] = block->transfer_count;
+       command[0] = CMD_DAP_TFER;
+       command[1] = 0x00;      /* DAP Index */
+       command[2] = block->transfer_count;
+       size_t idx = 3;
 
        for (int i = 0; i < block->transfer_count; i++) {
                struct pending_transfer_result *transfer = &(block->transfers[i]);
@@ -570,12 +559,10 @@ static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
                        data &= ~CORUNDETECT;
                }
 
-               buffer[idx++] = (cmd >> 1) & 0x0f;
+               command[idx++] = (cmd >> 1) & 0x0f;
                if (!(cmd & SWD_CMD_RnW)) {
-                       buffer[idx++] = (data) & 0xff;
-                       buffer[idx++] = (data >> 8) & 0xff;
-                       buffer[idx++] = (data >> 16) & 0xff;
-                       buffer[idx++] = (data >> 24) & 0xff;
+                       h_u32_to_le(&command[idx], data);
+                       idx += 4;
                }
        }
 
@@ -601,7 +588,6 @@ skip:
 
 static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
 {
-       uint8_t *buffer = dap->packet_buffer;
        struct pending_request_block *block = &pending_fifo[pending_fifo_get_idx];
 
        if (pending_fifo_block_count == 0)
@@ -618,30 +604,33 @@ static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
                goto skip;
        }
 
-       if (buffer[2] & 0x08) {
-               LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", buffer[1]);
+       uint8_t *resp = dap->response;
+       uint8_t transfer_count = resp[1];
+       uint8_t ack = resp[2] & 0x07;
+       if (resp[2] & 0x08) {
+               LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", transfer_count);
                queued_retval = ERROR_FAIL;
                goto skip;
        }
-       uint8_t ack = buffer[2] & 0x07;
        if (ack != SWD_ACK_OK) {
-               LOG_DEBUG("SWD ack not OK @ %d %s", buffer[1],
+               LOG_DEBUG("SWD ack not OK @ %d %s", transfer_count,
                          ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
                queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
                goto skip;
        }
 
-       if (block->transfer_count != buffer[1])
+       if (block->transfer_count != transfer_count)
                LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
-                         block->transfer_count, buffer[1]);
+                         block->transfer_count, transfer_count);
 
-       LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %d", buffer[1], pending_fifo_get_idx);
+       LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %d",
+                transfer_count, pending_fifo_get_idx);
        size_t idx = 3;
-       for (int i = 0; i < buffer[1]; i++) {
+       for (int i = 0; i < transfer_count; i++) {
                struct pending_transfer_result *transfer = &(block->transfers[i]);
                if (transfer->cmd & SWD_CMD_RnW) {
                        static uint32_t last_read;
-                       uint32_t data = le_to_h_u32(&buffer[idx]);
+                       uint32_t data = le_to_h_u32(&resp[idx]);
                        uint32_t tmp = data;
                        idx += 4;
 
@@ -1145,20 +1134,21 @@ static void cmsis_dap_flush(void)
        LOG_DEBUG_IO("Flushing %d queued sequences (%d bytes) with %d pending scan results to capture",
                queued_seq_count, queued_seq_buf_end, pending_scan_result_count);
 
-       /* prep CMSIS-DAP packet */
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
-       buffer[0] = 0;  /* report number */
-       buffer[1] = CMD_DAP_JTAG_SEQ;
-       buffer[2] = queued_seq_count;
-       memcpy(buffer + 3, queued_seq_buf, queued_seq_buf_end);
+       /* prepare CMSIS-DAP packet */
+       uint8_t *command = cmsis_dap_handle->command;
+       command[0] = CMD_DAP_JTAG_SEQ;
+       command[1] = queued_seq_count;
+       memcpy(&command[2], queued_seq_buf, queued_seq_buf_end);
 
 #ifdef CMSIS_DAP_JTAG_DEBUG
-       debug_parse_cmsis_buf(buffer, queued_seq_buf_end + 3);
+       debug_parse_cmsis_buf(command, queued_seq_buf_end + 2);
 #endif
 
        /* send command to USB device */
-       int retval = cmsis_dap_xfer(cmsis_dap_handle, queued_seq_buf_end + 3);
-       if (retval != ERROR_OK || buffer[1] != DAP_OK) {
+       int retval = cmsis_dap_xfer(cmsis_dap_handle, queued_seq_buf_end + 2);
+
+       uint8_t *resp = cmsis_dap_handle->response;
+       if (retval != ERROR_OK || resp[1] != DAP_OK) {
                LOG_ERROR("CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
                exit(-1);
        }
@@ -1166,7 +1156,7 @@ static void cmsis_dap_flush(void)
 #ifdef CMSIS_DAP_JTAG_DEBUG
        LOG_DEBUG_IO("USB response buf:");
        for (int c = 0; c < queued_seq_buf_end + 3; ++c)
-               printf("%02X ", buffer[c]);
+               printf("%02X ", resp[c]);
        printf("\n");
 #endif
 
@@ -1177,10 +1167,10 @@ static void cmsis_dap_flush(void)
                        i, pending_scan_result_count, scan->length, scan->first + 2, scan->buffer_offset);
 #ifdef CMSIS_DAP_JTAG_DEBUG
                for (uint32_t b = 0; b < DIV_ROUND_UP(scan->length, 8); ++b)
-                       printf("%02X ", buffer[2+scan->first+b]);
+                       printf("%02X ", resp[2+scan->first+b]);
                printf("\n");
 #endif
-               bit_copy(scan->buffer, scan->buffer_offset, buffer + 2 + scan->first, 0, scan->length);
+               bit_copy(scan->buffer, scan->buffer_offset, &resp[2 + scan->first], 0, scan->length);
        }
 
        /* reset */
@@ -1554,22 +1544,21 @@ COMMAND_HANDLER(cmsis_dap_handle_cmd_command)
 {
        int retval;
        unsigned i;
-       uint8_t *buffer = cmsis_dap_handle->packet_buffer;
-
-       buffer[0] = 0;  /* report number */
+       uint8_t *command = cmsis_dap_handle->command;
 
        for (i = 0; i < CMD_ARGC; i++)
-               buffer[i + 1] = strtoul(CMD_ARGV[i], NULL, 16);
+               command[i] = strtoul(CMD_ARGV[i], NULL, 16);
 
-       retval = cmsis_dap_xfer(cmsis_dap_handle, CMD_ARGC + 1);
+       retval = cmsis_dap_xfer(cmsis_dap_handle, CMD_ARGC);
 
        if (retval != ERROR_OK) {
                LOG_ERROR("CMSIS-DAP command failed.");
                return ERROR_JTAG_DEVICE_ERROR;
        }
 
+       uint8_t *resp = cmsis_dap_handle->response;
        LOG_INFO("Returned data %02" PRIx8 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8,
-               buffer[1], buffer[2], buffer[3], buffer[4]);
+               resp[1], resp[2], resp[3], resp[4]);
 
        return ERROR_OK;
 }
index a096a95c0d4c6973e5d03e1faa2e156949b6b345..c9f24c8968dba74c129a1a5fd71c80be0b31f009 100644 (file)
@@ -14,6 +14,8 @@ struct cmsis_dap {
        int packet_count;
        uint8_t *packet_buffer;
        uint16_t packet_buffer_size;
+       uint8_t *command;
+       uint8_t *response;
        uint8_t caps;
        uint8_t mode;
 };
index 7efea5166689d26339de7fcc377afaddddc2eda4..c9ee53d2effe94085a1d2d53e23f3edd06875c0d 100644 (file)
@@ -360,7 +360,7 @@ static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
                        }
 
                        dap->packet_size = packet_size;
-                       dap->packet_buffer_size = packet_size + REPORT_ID_SIZE;
+                       dap->packet_buffer_size = packet_size;
                        dap->bdata->usb_ctx = ctx;
                        dap->bdata->dev_handle = dev_handle;
                        dap->bdata->ep_out = ep_out;
@@ -374,6 +374,9 @@ static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
                                return ERROR_FAIL;
                        }
 
+                       dap->command = dap->packet_buffer;
+                       dap->response = dap->packet_buffer;
+
                        return ERROR_OK;
                }
 
@@ -425,7 +428,7 @@ static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
 
        /* 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 + REPORT_ID_SIZE, txlen - REPORT_ID_SIZE, &transferred, timeout_ms);
+                                                       dap->packet_buffer, txlen, &transferred, timeout_ms);
        if (err) {
                if (err == LIBUSB_ERROR_TIMEOUT) {
                        return ERROR_TIMEOUT_REACHED;
@@ -440,8 +443,7 @@ static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
 
 static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
 {
-       unsigned int packet_buffer_size = pkt_sz + REPORT_ID_SIZE;
-       uint8_t *buf = malloc(packet_buffer_size);
+       uint8_t *buf = malloc(pkt_sz);
        if (buf == NULL) {
                LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
                return ERROR_FAIL;
@@ -449,7 +451,10 @@ static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
 
        dap->packet_buffer = buf;
        dap->packet_size = pkt_sz;
-       dap->packet_buffer_size = packet_buffer_size;
+       dap->packet_buffer_size = pkt_sz;
+
+       dap->command = dap->packet_buffer;
+       dap->response = dap->packet_buffer;
 
        return ERROR_OK;
 }
index b290d6f81883ea3c13507e19b0eb430ffae18ef7..e83ad1feb1193296fda056fc4344026bd9679fa4 100644 (file)
@@ -164,6 +164,8 @@ static int cmsis_dap_hid_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
                return ERROR_FAIL;
        }
 
+       dap->command = dap->packet_buffer + REPORT_ID_SIZE;
+       dap->response = dap->packet_buffer;
        return ERROR_OK;
 }
 
@@ -195,8 +197,10 @@ static int cmsis_dap_hid_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
 {
        (void) timeout_ms;
 
+       dap->packet_buffer[0] = 0; /* HID report number */
+
        /* Pad the rest of the TX buffer with 0's */
-       memset(dap->packet_buffer + txlen, 0, dap->packet_buffer_size - txlen);
+       memset(dap->command + txlen, 0, dap->packet_size - txlen);
 
        /* write data to device */
        int retval = hid_write(dap->bdata->dev_handle, dap->packet_buffer, dap->packet_buffer_size);
@@ -221,6 +225,9 @@ static int cmsis_dap_hid_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
        dap->packet_size = pkt_sz;
        dap->packet_buffer_size = packet_buffer_size;
 
+       dap->command = dap->packet_buffer + REPORT_ID_SIZE;
+       dap->response = dap->packet_buffer;
+
        return ERROR_OK;
 }