#define CMD_DAP_TFER_BLOCK 0x06
#define CMD_DAP_TFER_ABORT 0x07
+/* DAP_TransferBlock increases the sum of command/response sizes
+ * (due to 16-bit Transfer Count) if used in a small packet.
+ * Prevent using it until we have at least r/w operations. */
+#define CMD_DAP_TFER_BLOCK_MIN_OPS 4
+
/* DAP Status Code */
#define DAP_OK 0
#define DAP_ERROR 0xFF
/* 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;
+static unsigned int tfer_max_response_size;
/* pointers to buffers that will receive jtag scan results on the next flush */
#define MAX_PENDING_SCAN_RESULTS 256
static struct pending_scan_result pending_scan_results[MAX_PENDING_SCAN_RESULTS];
/* queued JTAG sequences that will be executed on the next flush */
-#define QUEUED_SEQ_BUF_LEN (cmsis_dap_handle->packet_size - 3)
+#define QUEUED_SEQ_BUF_LEN (cmsis_dap_handle->packet_usable_size - 3)
static int queued_seq_count;
static int queued_seq_buf_end;
static int queued_seq_tdo_ptr;
return ERROR_OK;
}
+
static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
{
uint8_t *command = dap->command;
struct pending_request_block *block = &dap->pending_fifo[dap->pending_fifo_put_idx];
- LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %u",
- block->transfer_count, dap->pending_fifo_put_idx);
+ assert(dap->write_count + dap->read_count == block->transfer_count);
+
+ /* Reset packet size check counters for the next packet */
+ dap->write_count = 0;
+ dap->read_count = 0;
+
+ LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %u%s",
+ block->transfer_count, dap->pending_fifo_put_idx,
+ cmsis_dap_handle->swd_cmds_differ ? "" : ", same swd ops");
if (queued_retval != ERROR_OK) {
LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
if (block->transfer_count == 0)
goto skip;
- command[0] = CMD_DAP_TFER;
+ bool block_cmd = !cmsis_dap_handle->swd_cmds_differ
+ && block->transfer_count >= CMD_DAP_TFER_BLOCK_MIN_OPS;
+ block->command = block_cmd ? CMD_DAP_TFER_BLOCK : CMD_DAP_TFER;
+
+ command[0] = block->command;
command[1] = 0x00; /* DAP Index */
- command[2] = block->transfer_count;
- size_t idx = 3;
+
+ unsigned int idx;
+ if (block_cmd) {
+ h_u16_to_le(&command[2], block->transfer_count);
+ idx = 4; /* The first transfer will store the common DAP register */
+ } else {
+ command[2] = block->transfer_count;
+ idx = 3;
+ }
for (unsigned int i = 0; i < block->transfer_count; i++) {
struct pending_transfer_result *transfer = &(block->transfers[i]);
data &= ~CORUNDETECT;
}
- command[idx++] = (cmd >> 1) & 0x0f;
+ if (!block_cmd || i == 0)
+ command[idx++] = (cmd >> 1) & 0x0f;
+
if (!(cmd & SWD_CMD_RNW)) {
h_u32_to_le(&command[idx], data);
idx += 4;
}
uint8_t *resp = dap->response;
- if (resp[0] != CMD_DAP_TFER) {
+ if (resp[0] != block->command) {
LOG_ERROR("CMSIS-DAP command mismatch. Expected 0x%x received 0x%" PRIx8,
- CMD_DAP_TFER, resp[0]);
+ block->command, resp[0]);
queued_retval = ERROR_FAIL;
goto skip;
}
- unsigned int transfer_count = resp[1];
- uint8_t ack = resp[2] & 0x07;
- if (resp[2] & 0x08) {
+ unsigned int transfer_count;
+ unsigned int idx;
+ if (block->command == CMD_DAP_TFER_BLOCK) {
+ transfer_count = le_to_h_u16(&resp[1]);
+ idx = 3;
+ } else {
+ transfer_count = resp[1];
+ idx = 2;
+ }
+ if (resp[idx] & 0x08) {
LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", transfer_count);
queued_retval = ERROR_FAIL;
goto skip;
}
+ uint8_t ack = resp[idx++] & 0x07;
if (ack != SWD_ACK_OK) {
LOG_DEBUG("SWD ack not OK @ %d %s", transfer_count,
ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %u timeout %i",
transfer_count, dap->pending_fifo_get_idx, timeout_ms);
- unsigned int idx = 3;
+
for (unsigned int i = 0; i < transfer_count; i++) {
struct pending_transfer_result *transfer = &(block->transfers[i]);
if (transfer->cmd & SWD_CMD_RNW) {
return retval;
}
+static unsigned int cmsis_dap_tfer_cmd_size(unsigned int write_count,
+ unsigned int read_count, bool block_tfer)
+{
+ unsigned int size;
+ if (block_tfer) {
+ size = 5; /* DAP_TransferBlock header */
+ size += write_count * 4; /* data */
+ } else {
+ size = 3; /* DAP_Transfer header */
+ size += write_count * (1 + 4); /* DAP register + data */
+ size += read_count; /* DAP register */
+ }
+ return size;
+}
+
+static unsigned int cmsis_dap_tfer_resp_size(unsigned int write_count,
+ unsigned int read_count, bool block_tfer)
+{
+ unsigned int size;
+ if (block_tfer)
+ size = 4; /* DAP_TransferBlock response header */
+ else
+ size = 3; /* DAP_Transfer response header */
+
+ size += read_count * 4; /* data */
+ return size;
+}
+
static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
{
+ /* Compute sizes of the DAP Transfer command and the expected response
+ * for all queued and this operation */
bool targetsel_cmd = swd_cmd(false, false, DP_TARGETSEL) == cmd;
- if (cmsis_dap_handle->pending_fifo[cmsis_dap_handle->pending_fifo_put_idx].transfer_count == pending_queue_len
- || targetsel_cmd) {
+ unsigned int write_count = cmsis_dap_handle->write_count;
+ unsigned int read_count = cmsis_dap_handle->read_count;
+ bool block_cmd;
+ if (write_count + read_count < CMD_DAP_TFER_BLOCK_MIN_OPS)
+ block_cmd = false;
+ else
+ block_cmd = !cmsis_dap_handle->swd_cmds_differ
+ && cmd == cmsis_dap_handle->common_swd_cmd;
+
+ if (cmd & SWD_CMD_RNW)
+ read_count++;
+ else
+ write_count++;
+
+ unsigned int cmd_size = cmsis_dap_tfer_cmd_size(write_count, read_count,
+ block_cmd);
+ unsigned int resp_size = cmsis_dap_tfer_resp_size(write_count, read_count,
+ block_cmd);
+ unsigned int max_transfer_count = block_cmd ? 65535 : 255;
+
+ /* Does the DAP Transfer command and the expected response fit into one packet?
+ * Run the queue also before a targetsel - it cannot be queued */
+ if (cmd_size > tfer_max_command_size
+ || resp_size > tfer_max_response_size
+ || targetsel_cmd
+ || write_count + read_count > max_transfer_count) {
if (cmsis_dap_handle->pending_fifo_block_count)
cmsis_dap_swd_read_process(cmsis_dap_handle, 0);
cmsis_dap_swd_read_process(cmsis_dap_handle, LIBUSB_TIMEOUT_MS);
}
+ assert(cmsis_dap_handle->pending_fifo[cmsis_dap_handle->pending_fifo_put_idx].transfer_count < pending_queue_len);
+
if (queued_retval != ERROR_OK)
return;
struct pending_transfer_result *transfer = &(block->transfers[block->transfer_count]);
transfer->data = data;
transfer->cmd = cmd;
+ if (block->transfer_count == 0) {
+ cmsis_dap_handle->swd_cmds_differ = false;
+ cmsis_dap_handle->common_swd_cmd = cmd;
+ } else if (cmd != cmsis_dap_handle->common_swd_cmd) {
+ cmsis_dap_handle->swd_cmds_differ = true;
+ }
+
if (cmd & SWD_CMD_RNW) {
/* Queue a read transaction */
transfer->buffer = dst;
+ cmsis_dap_handle->read_count++;
+ } else {
+ cmsis_dap_handle->write_count++;
}
block->transfer_count++;
}
unsigned int s_len;
int retval;
- if ((output_pins & (SWJ_PIN_SRST | SWJ_PIN_TRST)) == (SWJ_PIN_SRST | SWJ_PIN_TRST)) {
+ if (seq != LINE_RESET &&
+ (output_pins & (SWJ_PIN_SRST | SWJ_PIN_TRST))
+ == (SWJ_PIN_SRST | SWJ_PIN_TRST)) {
/* Following workaround deasserts reset on most adapters.
* Do not reconnect if a reset line is active!
* Reconnecting would break connecting under reset. */
/* Be conservative and suppress submitting multiple HID requests
* until we get packet count info from the adaptor */
cmsis_dap_handle->packet_count = 1;
- pending_queue_len = 12;
/* INFO_ID_PKT_SZ - short */
retval = cmsis_dap_cmd_dap_info(INFO_ID_PKT_SZ, &data);
if (data[0] == 2) { /* short */
uint16_t pkt_sz = data[1] + (data[2] << 8);
if (pkt_sz != cmsis_dap_handle->packet_size) {
-
- /* 4 bytes of command header + 5 bytes per register
- * write. For bulk read sequences just 4 bytes are
- * needed per transfer, so this is suboptimal. */
- pending_queue_len = (pkt_sz - 4) / 5;
-
free(cmsis_dap_handle->packet_buffer);
retval = cmsis_dap_handle->backend->packet_buffer_alloc(cmsis_dap_handle, pkt_sz);
if (retval != ERROR_OK)
}
}
+ /* Maximal number of transfers which fit to one packet:
+ * Limited by response size: 3 bytes of response header + 4 per read
+ * Plus writes to full command size: 3 bytes cmd header + 1 per read + 5 per write */
+ tfer_max_command_size = cmsis_dap_handle->packet_usable_size;
+ tfer_max_response_size = cmsis_dap_handle->packet_usable_size;
+ unsigned int max_reads = tfer_max_response_size / 4;
+ pending_queue_len = max_reads + (tfer_max_command_size - max_reads) / 5;
+ cmsis_dap_handle->write_count = 0;
+ cmsis_dap_handle->read_count = 0;
+
/* INFO_ID_PKT_CNT - byte */
retval = cmsis_dap_cmd_dap_info(INFO_ID_PKT_CNT, &data);
if (retval != ERROR_OK)
COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
{
if (CMD_ARGC > MAX_USB_IDS * 2) {
- LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
+ LOG_WARNING("ignoring extra IDs in cmsis-dap vid_pid "
"(maximum is %d pairs)", MAX_USB_IDS);
CMD_ARGC = MAX_USB_IDS * 2;
}
if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
- LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
+ LOG_WARNING("incomplete cmsis-dap vid_pid configuration directive");
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
/* remove the incomplete trailing id */
}
}
- LOG_ERROR("invalid backend argument to cmsis_dap_backend <backend>");
+ LOG_ERROR("invalid backend argument to cmsis-dap backend <backend>");
}
} else {
- LOG_ERROR("expected exactly one argument to cmsis_dap_backend <backend>");
+ LOG_ERROR("expected exactly one argument to cmsis-dap backend <backend>");
}
return ERROR_OK;
.usage = "",
.help = "issue cmsis-dap command",
},
- COMMAND_REGISTRATION_DONE
-};
-
-
-static const struct command_registration cmsis_dap_command_handlers[] = {
- {
- .name = "cmsis-dap",
- .mode = COMMAND_ANY,
- .help = "perform CMSIS-DAP management",
- .usage = "<cmd>",
- .chain = cmsis_dap_subcommand_handlers,
- },
{
- .name = "cmsis_dap_vid_pid",
+ .name = "vid_pid",
.handler = &cmsis_dap_handle_vid_pid_command,
.mode = COMMAND_CONFIG,
.help = "the vendor ID and product ID of the CMSIS-DAP device",
.usage = "(vid pid)*",
},
{
- .name = "cmsis_dap_backend",
+ .name = "backend",
.handler = &cmsis_dap_handle_backend_command,
.mode = COMMAND_CONFIG,
.help = "set the communication backend to use (USB bulk or HID).",
},
#if BUILD_CMSIS_DAP_USB
{
- .name = "cmsis_dap_usb",
+ .name = "usb",
.chain = cmsis_dap_usb_subcommand_handlers,
.mode = COMMAND_ANY,
.help = "USB bulk backend-specific commands",
COMMAND_REGISTRATION_DONE
};
+
+static const struct command_registration cmsis_dap_command_handlers[] = {
+ {
+ .name = "cmsis-dap",
+ .mode = COMMAND_ANY,
+ .help = "perform CMSIS-DAP management",
+ .usage = "<cmd>",
+ .chain = cmsis_dap_subcommand_handlers,
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
static const struct swd_driver cmsis_dap_swd_driver = {
.init = cmsis_dap_swd_init,
.switch_seq = cmsis_dap_swd_switch_seq,