drivers/cmsis-dap: tidy up buffer access

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>
This commit is contained in:
Tomas Vanek 2021-03-23 09:12:26 +01:00
parent fed42ccfd3
commit 01030fb893
4 changed files with 123 additions and 120 deletions

View 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) static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_SWJ_PINS;
buffer[1] = CMD_DAP_SWJ_PINS; command[1] = pins;
buffer[2] = pins; command[2] = mask;
buffer[3] = mask; h_u32_to_le(&command[3], delay);
buffer[4] = delay & 0xff;
buffer[5] = (delay >> 8) & 0xff; retval = cmsis_dap_xfer(cmsis_dap_handle, 7);
buffer[6] = (delay >> 16) & 0xff;
buffer[7] = (delay >> 24) & 0xff;
retval = cmsis_dap_xfer(cmsis_dap_handle, 8);
if (retval != ERROR_OK) { if (retval != ERROR_OK) {
LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed."); 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) if (input)
*input = buffer[1]; *input = cmsis_dap_handle->response[1];
return ERROR_OK; 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) static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
/* set clock in Hz */ /* set clock in Hz */
swj_clock *= 1000; 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."); LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
return ERROR_JTAG_DEVICE_ERROR; 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) static int cmsis_dap_cmd_DAP_SWJ_Sequence(uint8_t s_len, const uint8_t *sequence)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
#ifdef CMSIS_DAP_JTAG_DEBUG #ifdef CMSIS_DAP_JTAG_DEBUG
LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len); 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"); printf("\n");
#endif #endif
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_SWJ_SEQ;
buffer[1] = CMD_DAP_SWJ_SEQ; command[1] = s_len;
buffer[2] = s_len; bit_copy(&command[2], 0, sequence, 0, s_len);
bit_copy(&buffer[3], 0, sequence, 0, s_len);
retval = cmsis_dap_xfer(cmsis_dap_handle, DIV_ROUND_UP(s_len, 8) + 3); 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)
if (retval != ERROR_OK || buffer[1] != DAP_OK)
return ERROR_FAIL; return ERROR_FAIL;
return ERROR_OK; 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) static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_INFO;
buffer[1] = CMD_DAP_INFO; command[1] = info;
buffer[2] = info;
retval = cmsis_dap_xfer(cmsis_dap_handle, 3); retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
if (retval != ERROR_OK) { if (retval != ERROR_OK) {
LOG_ERROR("CMSIS-DAP command CMD_INFO failed."); LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
return ERROR_JTAG_DEVICE_ERROR; return ERROR_JTAG_DEVICE_ERROR;
} }
*data = &(buffer[1]); *data = &cmsis_dap_handle->response[1];
return ERROR_OK; 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) static int cmsis_dap_cmd_DAP_LED(uint8_t led, uint8_t state)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_LED;
buffer[1] = CMD_DAP_LED; command[1] = led;
buffer[2] = led; command[2] = state;
buffer[3] = state;
retval = cmsis_dap_xfer(cmsis_dap_handle, 4);
if (retval != ERROR_OK || buffer[1] != 0x00) { retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_LED failed."); LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
return ERROR_JTAG_DEVICE_ERROR; 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) static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_CONNECT;
buffer[1] = CMD_DAP_CONNECT; command[1] = mode;
buffer[2] = mode;
retval = cmsis_dap_xfer(cmsis_dap_handle, 3); retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
if (retval != ERROR_OK) { if (retval != ERROR_OK) {
LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed."); LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
return ERROR_JTAG_DEVICE_ERROR; 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); LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
return ERROR_JTAG_DEVICE_ERROR; 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) static int cmsis_dap_cmd_DAP_Disconnect(void)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_DISCONNECT;
buffer[1] = CMD_DAP_DISCONNECT;
retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
if (retval != ERROR_OK || buffer[1] != DAP_OK) { retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed."); LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
return ERROR_JTAG_DEVICE_ERROR; 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) static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_TFER_CONFIGURE;
buffer[1] = CMD_DAP_TFER_CONFIGURE; command[1] = idle;
buffer[2] = idle; h_u16_to_le(&command[2], retry_count);
buffer[3] = retry_count & 0xff; h_u16_to_le(&command[4], match_retry);
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);
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."); LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
return ERROR_JTAG_DEVICE_ERROR; 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) static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_SWD_CONFIGURE;
buffer[1] = CMD_DAP_SWD_CONFIGURE; command[1] = cfg;
buffer[2] = cfg;
retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
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."); LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
return ERROR_JTAG_DEVICE_ERROR; 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) static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
{ {
int retval; int retval;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_DELAY;
buffer[1] = CMD_DAP_DELAY; h_u16_to_le(&command[1], delay_us);
buffer[2] = delay_us & 0xff;
buffer[3] = (delay_us >> 8) & 0xff;
retval = cmsis_dap_xfer(cmsis_dap_handle, 4);
if (retval != ERROR_OK || buffer[1] != DAP_OK) { retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
LOG_ERROR("CMSIS-DAP command CMD_Delay failed."); LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
return ERROR_JTAG_DEVICE_ERROR; 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) 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]; 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); 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) if (block->transfer_count == 0)
goto skip; goto skip;
size_t idx = 0; command[0] = CMD_DAP_TFER;
buffer[idx++] = 0; /* report number */ command[1] = 0x00; /* DAP Index */
buffer[idx++] = CMD_DAP_TFER; command[2] = block->transfer_count;
buffer[idx++] = 0x00; /* DAP Index */ size_t idx = 3;
buffer[idx++] = block->transfer_count;
for (int i = 0; i < block->transfer_count; i++) { for (int i = 0; i < block->transfer_count; i++) {
struct pending_transfer_result *transfer = &(block->transfers[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; data &= ~CORUNDETECT;
} }
buffer[idx++] = (cmd >> 1) & 0x0f; command[idx++] = (cmd >> 1) & 0x0f;
if (!(cmd & SWD_CMD_RnW)) { if (!(cmd & SWD_CMD_RnW)) {
buffer[idx++] = (data) & 0xff; h_u32_to_le(&command[idx], data);
buffer[idx++] = (data >> 8) & 0xff; idx += 4;
buffer[idx++] = (data >> 16) & 0xff;
buffer[idx++] = (data >> 24) & 0xff;
} }
} }
@ -601,7 +588,6 @@ skip:
static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms) 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]; struct pending_request_block *block = &pending_fifo[pending_fifo_get_idx];
if (pending_fifo_block_count == 0) 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; goto skip;
} }
if (buffer[2] & 0x08) { uint8_t *resp = dap->response;
LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", buffer[1]); 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; queued_retval = ERROR_FAIL;
goto skip; goto skip;
} }
uint8_t ack = buffer[2] & 0x07;
if (ack != SWD_ACK_OK) { 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"); ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL; queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
goto skip; 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", 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; 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]); struct pending_transfer_result *transfer = &(block->transfers[i]);
if (transfer->cmd & SWD_CMD_RnW) { if (transfer->cmd & SWD_CMD_RnW) {
static uint32_t last_read; 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; uint32_t tmp = data;
idx += 4; 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", 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); queued_seq_count, queued_seq_buf_end, pending_scan_result_count);
/* prep CMSIS-DAP packet */ /* prepare CMSIS-DAP packet */
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */ command[0] = CMD_DAP_JTAG_SEQ;
buffer[1] = CMD_DAP_JTAG_SEQ; command[1] = queued_seq_count;
buffer[2] = queued_seq_count; memcpy(&command[2], queued_seq_buf, queued_seq_buf_end);
memcpy(buffer + 3, queued_seq_buf, queued_seq_buf_end);
#ifdef CMSIS_DAP_JTAG_DEBUG #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 #endif
/* send command to USB device */ /* send command to USB device */
int retval = cmsis_dap_xfer(cmsis_dap_handle, queued_seq_buf_end + 3); int retval = cmsis_dap_xfer(cmsis_dap_handle, queued_seq_buf_end + 2);
if (retval != ERROR_OK || buffer[1] != DAP_OK) {
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."); LOG_ERROR("CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
exit(-1); exit(-1);
} }
@ -1166,7 +1156,7 @@ static void cmsis_dap_flush(void)
#ifdef CMSIS_DAP_JTAG_DEBUG #ifdef CMSIS_DAP_JTAG_DEBUG
LOG_DEBUG_IO("USB response buf:"); LOG_DEBUG_IO("USB response buf:");
for (int c = 0; c < queued_seq_buf_end + 3; ++c) for (int c = 0; c < queued_seq_buf_end + 3; ++c)
printf("%02X ", buffer[c]); printf("%02X ", resp[c]);
printf("\n"); printf("\n");
#endif #endif
@ -1177,10 +1167,10 @@ static void cmsis_dap_flush(void)
i, pending_scan_result_count, scan->length, scan->first + 2, scan->buffer_offset); i, pending_scan_result_count, scan->length, scan->first + 2, scan->buffer_offset);
#ifdef CMSIS_DAP_JTAG_DEBUG #ifdef CMSIS_DAP_JTAG_DEBUG
for (uint32_t b = 0; b < DIV_ROUND_UP(scan->length, 8); ++b) 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"); printf("\n");
#endif #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 */ /* reset */
@ -1554,22 +1544,21 @@ COMMAND_HANDLER(cmsis_dap_handle_cmd_command)
{ {
int retval; int retval;
unsigned i; unsigned i;
uint8_t *buffer = cmsis_dap_handle->packet_buffer; uint8_t *command = cmsis_dap_handle->command;
buffer[0] = 0; /* report number */
for (i = 0; i < CMD_ARGC; i++) 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) { if (retval != ERROR_OK) {
LOG_ERROR("CMSIS-DAP command failed."); LOG_ERROR("CMSIS-DAP command failed.");
return ERROR_JTAG_DEVICE_ERROR; return ERROR_JTAG_DEVICE_ERROR;
} }
uint8_t *resp = cmsis_dap_handle->response;
LOG_INFO("Returned data %02" PRIx8 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8, 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; return ERROR_OK;
} }

View File

@ -14,6 +14,8 @@ struct cmsis_dap {
int packet_count; int packet_count;
uint8_t *packet_buffer; uint8_t *packet_buffer;
uint16_t packet_buffer_size; uint16_t packet_buffer_size;
uint8_t *command;
uint8_t *response;
uint8_t caps; uint8_t caps;
uint8_t mode; uint8_t mode;
}; };

View 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_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->usb_ctx = ctx;
dap->bdata->dev_handle = dev_handle; dap->bdata->dev_handle = dev_handle;
dap->bdata->ep_out = ep_out; 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; return ERROR_FAIL;
} }
dap->command = dap->packet_buffer;
dap->response = dap->packet_buffer;
return ERROR_OK; 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 */ /* skip the first byte that is only used by the HID backend */
err = libusb_bulk_transfer(dap->bdata->dev_handle, dap->bdata->ep_out, 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) {
if (err == LIBUSB_ERROR_TIMEOUT) { if (err == LIBUSB_ERROR_TIMEOUT) {
return ERROR_TIMEOUT_REACHED; 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) 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(pkt_sz);
uint8_t *buf = malloc(packet_buffer_size);
if (buf == NULL) { if (buf == NULL) {
LOG_ERROR("unable to allocate CMSIS-DAP packet buffer"); LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
return ERROR_FAIL; 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_buffer = buf;
dap->packet_size = pkt_sz; 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; return ERROR_OK;
} }

View File

@ -164,6 +164,8 @@ static int cmsis_dap_hid_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
return ERROR_FAIL; return ERROR_FAIL;
} }
dap->command = dap->packet_buffer + REPORT_ID_SIZE;
dap->response = dap->packet_buffer;
return ERROR_OK; 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; (void) timeout_ms;
dap->packet_buffer[0] = 0; /* HID report number */
/* Pad the rest of the TX buffer with 0's */ /* 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 */ /* write data to device */
int retval = hid_write(dap->bdata->dev_handle, dap->packet_buffer, dap->packet_buffer_size); 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_size = pkt_sz;
dap->packet_buffer_size = packet_buffer_size; dap->packet_buffer_size = packet_buffer_size;
dap->command = dap->packet_buffer + REPORT_ID_SIZE;
dap->response = dap->packet_buffer;
return ERROR_OK; return ERROR_OK;
} }