+// SPDX-License-Identifier: GPL-2.0-or-later
+
/***************************************************************************
* Copyright (C) 2021 by Adrian Negreanu *
* groleo@gmail.com *
* *
* Copyright (C) 2013 by Spencer Oliver *
* spen@spen-soft.co.uk *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <transport/transport.h>
#include "helper/replacements.h"
+#include <jtag/adapter.h>
#include <jtag/swd.h>
#include <jtag/interface.h>
#include <jtag/commands.h>
#include <target/cortex_m.h>
#include "cmsis_dap.h"
+#include "libusb_helper.h"
static const struct cmsis_dap_backend *const cmsis_dap_backends[] = {
#if BUILD_CMSIS_DAP_USB == 1
/* vid = pid = 0 marks the end of the list */
static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
-static char *cmsis_dap_serial;
static int cmsis_dap_backend = -1;
static bool swd_mode;
-#define USB_TIMEOUT 1000
-
/* CMSIS-DAP General Commands */
#define CMD_DAP_INFO 0x00
#define CMD_DAP_LED 0x01
#define INFO_ID_PKT_SZ 0xff /* short */
#define INFO_ID_SWO_BUF_SZ 0xfd /* word */
-#define INFO_CAPS_SWD BIT(0)
-#define INFO_CAPS_JTAG BIT(1)
-#define INFO_CAPS_SWO_UART BIT(2)
-#define INFO_CAPS_SWO_MANCHESTER BIT(3)
+#define INFO_CAPS_SWD BIT(0)
+#define INFO_CAPS_JTAG BIT(1)
+#define INFO_CAPS_SWO_UART BIT(2)
+#define INFO_CAPS_SWO_MANCHESTER BIT(3)
+#define INFO_CAPS_ATOMIC_CMDS BIT(4)
+#define INFO_CAPS_TEST_DOMAIN_TIMER BIT(5)
+#define INFO_CAPS_SWO_STREAMING_TRACE BIT(6)
+#define INFO_CAPS_UART_PORT BIT(7)
+#define INFO_CAPS_USB_COM_PORT BIT(8)
+#define INFO_CAPS__NUM_CAPS 9
/* CMD_LED */
#define LED_ID_CONNECT 0x00
#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
/* CMSIS-DAP Vendor Commands
* None as yet... */
-static const char * const info_caps_str[] = {
- "SWD Supported",
- "JTAG Supported",
- "SWO-UART Supported",
- "SWO-MANCHESTER Supported"
-};
-
-struct pending_transfer_result {
- uint8_t cmd;
- uint32_t data;
- void *buffer;
-};
-
-struct pending_request_block {
- struct pending_transfer_result *transfers;
- int transfer_count;
+static const char * const info_caps_str[INFO_CAPS__NUM_CAPS] = {
+ "SWD supported",
+ "JTAG supported",
+ "SWO-UART supported",
+ "SWO-MANCHESTER supported",
+ "Atomic commands supported",
+ "Test domain timer supported",
+ "SWO streaming trace supported",
+ "UART communication port supported",
+ "UART via USB COM port supported",
};
struct pending_scan_result {
/** Offset in bytes in the CMD_DAP_JTAG_SEQ response buffer. */
- unsigned first;
+ unsigned int first;
/** Number of bits to read. */
- unsigned length;
+ unsigned int length;
/** Location to store the result */
uint8_t *buffer;
/** Offset in the destination buffer */
- unsigned buffer_offset;
+ unsigned int buffer_offset;
};
-/* Up to MIN(packet_count, MAX_PENDING_REQUESTS) requests may be issued
- * until the first response arrives */
-#define MAX_PENDING_REQUESTS 3
-
-/* Pending requests are organized as a FIFO - circular buffer */
/* Each block in FIFO can contain up to pending_queue_len transfers */
-static int pending_queue_len;
-static struct pending_request_block pending_fifo[MAX_PENDING_REQUESTS];
-static int pending_fifo_put_idx, pending_fifo_get_idx;
-static int pending_fifo_block_count;
+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;
if (cmsis_dap_backend >= 0) {
/* Use forced backend */
backend = cmsis_dap_backends[cmsis_dap_backend];
- if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, cmsis_dap_serial) != ERROR_OK)
+ if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial()) != ERROR_OK)
backend = NULL;
} else {
/* Try all backends */
for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
backend = cmsis_dap_backends[i];
- if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, cmsis_dap_serial) == ERROR_OK)
+ if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial()) == ERROR_OK)
break;
else
backend = NULL;
dap->backend = NULL;
}
- free(cmsis_dap_handle->packet_buffer);
- free(cmsis_dap_handle);
- cmsis_dap_handle = NULL;
- free(cmsis_dap_serial);
- cmsis_dap_serial = NULL;
+ free(dap->packet_buffer);
- for (int i = 0; i < MAX_PENDING_REQUESTS; i++) {
- free(pending_fifo[i].transfers);
- pending_fifo[i].transfers = NULL;
+ for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
+ free(dap->pending_fifo[i].transfers);
+ dap->pending_fifo[i].transfers = NULL;
}
+
+ free(cmsis_dap_handle);
+ cmsis_dap_handle = NULL;
}
static void cmsis_dap_flush_read(struct cmsis_dap *dap)
/* Send a message and receive the reply */
static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
{
- if (pending_fifo_block_count) {
- LOG_ERROR("pending %d blocks, flushing", pending_fifo_block_count);
- while (pending_fifo_block_count) {
+ 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);
- pending_fifo_block_count--;
+ dap->pending_fifo_block_count--;
}
- pending_fifo_put_idx = 0;
- pending_fifo_get_idx = 0;
+ dap->pending_fifo_put_idx = 0;
+ dap->pending_fifo_get_idx = 0;
}
- uint8_t current_cmd = cmsis_dap_handle->command[0];
- int retval = dap->backend->write(dap, txlen, USB_TIMEOUT);
+ uint8_t current_cmd = dap->command[0];
+ int retval = dap->backend->write(dap, txlen, LIBUSB_TIMEOUT_MS);
if (retval < 0)
return retval;
/* get reply */
- retval = dap->backend->read(dap, USB_TIMEOUT);
+ retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS);
if (retval < 0)
return retval;
- uint8_t *resp = cmsis_dap_handle->response;
+ uint8_t *resp = dap->response;
if (resp[0] == DAP_ERROR) {
LOG_ERROR("CMSIS-DAP command 0x%" PRIx8 " not implemented", current_cmd);
return ERROR_NOT_IMPLEMENTED;
#ifdef CMSIS_DAP_JTAG_DEBUG
LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len);
- for (int i = 0; i < DIV_ROUND_UP(s_len, 8); ++i)
+ for (unsigned int i = 0; i < DIV_ROUND_UP(s_len, 8); ++i)
printf("%02X ", sequence[i]);
printf("\n");
The purpose of this operation is to select the target
corresponding to the instance_id that is written */
+ LOG_DEBUG_IO("DP write reg TARGETSEL %" PRIx32, instance_id);
+
size_t idx = 0;
command[idx++] = CMD_DAP_SWD_SEQUENCE;
command[idx++] = 3; /* sequence count */
command[0] = CMD_DAP_SWO_BAUDRATE;
h_u32_to_le(&command[1], in_baudrate);
- int retval = cmsis_dap_xfer(cmsis_dap_handle, 4);
+ int retval = cmsis_dap_xfer(cmsis_dap_handle, 5);
uint32_t rvbr = le_to_h_u32(&cmsis_dap_handle->response[1]);
if (retval != ERROR_OK || rvbr == 0) {
LOG_ERROR("CMSIS-DAP: command CMD_SWO_Baudrate(%u) -> %u failed.", in_baudrate, rvbr);
return ERROR_OK;
}
+
static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
{
- uint8_t *command = cmsis_dap_handle->command;
- struct pending_request_block *block = &pending_fifo[pending_fifo_put_idx];
+ uint8_t *command = dap->command;
+ struct pending_request_block *block = &dap->pending_fifo[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 %d", block->transfer_count, pending_fifo_put_idx);
+ 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;
- for (int i = 0; i < block->transfer_count; i++) {
+ 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]);
uint8_t cmd = transfer->cmd;
uint32_t data = transfer->data;
- LOG_DEBUG_IO("%s %s reg %x %"PRIx32,
+ LOG_DEBUG_IO("%s %s reg %x %" PRIx32,
cmd & SWD_CMD_APNDP ? "AP" : "DP",
cmd & SWD_CMD_RNW ? "read" : "write",
(cmd & SWD_CMD_A32) >> 1, data);
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;
}
}
- int retval = dap->backend->write(dap, idx, USB_TIMEOUT);
+ int retval = dap->backend->write(dap, idx, LIBUSB_TIMEOUT_MS);
if (retval < 0) {
queued_retval = retval;
goto skip;
queued_retval = ERROR_OK;
}
- pending_fifo_put_idx = (pending_fifo_put_idx + 1) % dap->packet_count;
- pending_fifo_block_count++;
- if (pending_fifo_block_count > dap->packet_count)
- LOG_ERROR("too much pending writes %d", pending_fifo_block_count);
+ dap->pending_fifo_put_idx = (dap->pending_fifo_put_idx + 1) % dap->packet_count;
+ dap->pending_fifo_block_count++;
+ if (dap->pending_fifo_block_count > dap->packet_count)
+ LOG_ERROR("too much pending writes %u", dap->pending_fifo_block_count);
return;
static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
{
- struct pending_request_block *block = &pending_fifo[pending_fifo_get_idx];
+ struct pending_request_block *block = &dap->pending_fifo[dap->pending_fifo_get_idx];
- if (pending_fifo_block_count == 0)
+ if (dap->pending_fifo_block_count == 0)
LOG_ERROR("no pending write");
/* get reply */
int retval = dap->backend->read(dap, timeout_ms);
- if (retval == ERROR_TIMEOUT_REACHED && timeout_ms < USB_TIMEOUT)
+ if (retval == ERROR_TIMEOUT_REACHED && timeout_ms < LIBUSB_TIMEOUT_MS)
return;
if (retval <= 0) {
}
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;
}
- uint8_t 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_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
block->transfer_count, transfer_count);
- 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 < transfer_count; i++) {
+ LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %u timeout %i",
+ transfer_count, dap->pending_fifo_get_idx, timeout_ms);
+
+ for (unsigned 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 tmp = data;
idx += 4;
- LOG_DEBUG_IO("Read result: %"PRIx32, data);
+ LOG_DEBUG_IO("Read result: %" PRIx32, data);
/* Imitate posted AP reads */
if ((transfer->cmd & SWD_CMD_APNDP) ||
skip:
block->transfer_count = 0;
- pending_fifo_get_idx = (pending_fifo_get_idx + 1) % dap->packet_count;
- pending_fifo_block_count--;
+ dap->pending_fifo_get_idx = (dap->pending_fifo_get_idx + 1) % dap->packet_count;
+ dap->pending_fifo_block_count--;
}
static int cmsis_dap_swd_run_queue(void)
{
- if (pending_fifo_block_count)
+ if (cmsis_dap_handle->pending_fifo_block_count)
cmsis_dap_swd_read_process(cmsis_dap_handle, 0);
cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
- while (pending_fifo_block_count)
- cmsis_dap_swd_read_process(cmsis_dap_handle, USB_TIMEOUT);
+ while (cmsis_dap_handle->pending_fifo_block_count)
+ cmsis_dap_swd_read_process(cmsis_dap_handle, LIBUSB_TIMEOUT_MS);
- pending_fifo_put_idx = 0;
- pending_fifo_get_idx = 0;
+ cmsis_dap_handle->pending_fifo_put_idx = 0;
+ cmsis_dap_handle->pending_fifo_get_idx = 0;
int retval = queued_retval;
queued_retval = ERROR_OK;
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 (pending_fifo[pending_fifo_put_idx].transfer_count == pending_queue_len
- || targetsel_cmd) {
- if (pending_fifo_block_count)
+ 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);
+
+ /* 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) {
+ if (cmsis_dap_handle->pending_fifo_block_count)
cmsis_dap_swd_read_process(cmsis_dap_handle, 0);
/* Not enough room in the queue. Run the queue. */
cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
- if (pending_fifo_block_count >= cmsis_dap_handle->packet_count)
- cmsis_dap_swd_read_process(cmsis_dap_handle, USB_TIMEOUT);
+ if (cmsis_dap_handle->pending_fifo_block_count >= cmsis_dap_handle->packet_count)
+ 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;
return;
}
- struct pending_request_block *block = &pending_fifo[pending_fifo_put_idx];
+ struct pending_request_block *block = &cmsis_dap_handle->pending_fifo[cmsis_dap_handle->pending_fifo_put_idx];
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++;
}
if (retval != ERROR_OK)
return retval;
- if (data[0] == 1) {
- uint8_t caps = data[1];
+ if (data[0] == 1 || data[0] == 2) {
+ uint16_t caps = data[1];
+ if (data[0] == 2)
+ caps |= (uint16_t)data[2] << 8;
cmsis_dap_handle->caps = caps;
- if (caps & INFO_CAPS_SWD)
- LOG_INFO("CMSIS-DAP: %s", info_caps_str[0]);
- if (caps & INFO_CAPS_JTAG)
- LOG_INFO("CMSIS-DAP: %s", info_caps_str[1]);
- if (caps & INFO_CAPS_SWO_UART)
- LOG_INFO("CMSIS-DAP: %s", info_caps_str[2]);
- if (caps & INFO_CAPS_SWO_MANCHESTER)
- LOG_INFO("CMSIS-DAP: %s", info_caps_str[3]);
+ for (unsigned int i = 0; i < INFO_CAPS__NUM_CAPS; ++i) {
+ if (caps & BIT(i))
+ LOG_INFO("CMSIS-DAP: %s", info_caps_str[i]);
+ }
}
return ERROR_OK;
s = swd_seq_dormant_to_swd;
s_len = swd_seq_dormant_to_swd_len;
break;
+ case DORMANT_TO_JTAG:
+ LOG_DEBUG("DORMANT-to-JTAG");
+ s = swd_seq_dormant_to_jtag;
+ s_len = swd_seq_dormant_to_jtag_len;
+ break;
default:
LOG_ERROR("Sequence %d not supported", seq);
return ERROR_FAIL;
/* Atmel EDBG needs renew clock setting after SWJ_Sequence
* otherwise default frequency is used */
- return cmsis_dap_cmd_dap_swj_clock(jtag_get_speed_khz());
+ return cmsis_dap_cmd_dap_swj_clock(adapter_get_speed_khz());
}
static int cmsis_dap_swd_open(void)
/* 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)
goto init_err;
if (data[0] == 1) { /* byte */
- int pkt_cnt = data[1];
+ unsigned int pkt_cnt = data[1];
if (pkt_cnt > 1)
cmsis_dap_handle->packet_count = MIN(MAX_PENDING_REQUESTS, pkt_cnt);
- LOG_DEBUG("CMSIS-DAP: Packet Count = %d", pkt_cnt);
+ LOG_DEBUG("CMSIS-DAP: Packet Count = %u", pkt_cnt);
}
- LOG_DEBUG("Allocating FIFO for %d pending packets", cmsis_dap_handle->packet_count);
- for (int i = 0; i < cmsis_dap_handle->packet_count; i++) {
- pending_fifo[i].transfers = malloc(pending_queue_len * sizeof(struct pending_transfer_result));
- if (!pending_fifo[i].transfers) {
+ LOG_DEBUG("Allocating FIFO for %u pending packets", cmsis_dap_handle->packet_count);
+ for (unsigned int i = 0; i < cmsis_dap_handle->packet_count; i++) {
+ cmsis_dap_handle->pending_fifo[i].transfers = malloc(pending_queue_len
+ * sizeof(struct pending_transfer_result));
+ if (!cmsis_dap_handle->pending_fifo[i].transfers) {
LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
retval = ERROR_FAIL;
goto init_err;
/* Now try to connect to the target
* TODO: This is all SWD only @ present */
- retval = cmsis_dap_cmd_dap_swj_clock(jtag_get_speed_khz());
+ retval = cmsis_dap_cmd_dap_swj_clock(adapter_get_speed_khz());
if (retval != ERROR_OK)
goto init_err;
}
#ifdef SPRINT_BINARY
-static void sprint_binary(char *s, const uint8_t *buf, int offset, int len)
+static void sprint_binary(char *s, const uint8_t *buf, unsigned int offset, unsigned int len)
{
if (!len)
return;
buf = { 0xc0 0x18 } offset=3 len=10 should result in: 11000 11000
i=3 there means i/8 = 0 so c = 0xFF, and
*/
- for (int i = offset; i < offset + len; ++i) {
+ for (unsigned int i = offset; i < offset + len; ++i) {
uint8_t c = buf[i / 8], mask = 1 << (i % 8);
if ((i != offset) && !(i % 8))
putchar(' ');
for (int i = 0; i < cmdlen; ++i)
printf(" %02x", cmd[i]);
printf("\n");
- switch (cmd[1]) {
+ switch (cmd[0]) {
case CMD_DAP_JTAG_SEQ: {
- printf("cmsis-dap jtag sequence command %02x (n=%d)\n", cmd[1], cmd[2]);
+ printf("cmsis-dap jtag sequence command %02x (n=%d)\n", cmd[0], cmd[1]);
/*
- * #2 = number of sequences
- * #3 = sequence info 1
- * #4...4+n_bytes-1 = sequence 1
+ * #1 = number of sequences
+ * #2 = sequence info 1
+ * #3...4+n_bytes-1 = sequence 1
* #4+n_bytes = sequence info 2
* #5+n_bytes = sequence 2 (single bit)
*/
- int pos = 3;
- for (int seq = 0; seq < cmd[2]; ++seq) {
+ int pos = 2;
+ for (int seq = 0; seq < cmd[1]; ++seq) {
uint8_t info = cmd[pos++];
int len = info & DAP_JTAG_SEQ_TCK;
if (len == 0)
* sequence=NULL means clock out zeros on TDI
* tdo_buffer=NULL means don't capture TDO
*/
-static void cmsis_dap_add_jtag_sequence(int s_len, const uint8_t *sequence, int s_offset,
- bool tms, uint8_t *tdo_buffer, int tdo_buffer_offset)
+static void cmsis_dap_add_jtag_sequence(unsigned int s_len, const uint8_t *sequence,
+ unsigned int s_offset, bool tms,
+ uint8_t *tdo_buffer, unsigned int tdo_buffer_offset)
{
- LOG_DEBUG_IO("[at %d] %d bits, tms %s, seq offset %d, tdo buf %p, tdo offset %d",
+ LOG_DEBUG_IO("[at %d] %u bits, tms %s, seq offset %u, tdo buf %p, tdo offset %u",
queued_seq_buf_end,
s_len, tms ? "HIGH" : "LOW", s_offset, tdo_buffer, tdo_buffer_offset);
if (s_len > 64) {
LOG_DEBUG_IO("START JTAG SEQ SPLIT");
- for (int offset = 0; offset < s_len; offset += 64) {
- int len = s_len - offset;
+ for (unsigned int offset = 0; offset < s_len; offset += 64) {
+ unsigned int len = s_len - offset;
if (len > 64)
len = 64;
- LOG_DEBUG_IO("Splitting long jtag sequence: %d-bit chunk starting at offset %d", len, offset);
+ LOG_DEBUG_IO("Splitting long jtag sequence: %u-bit chunk starting at offset %u", len, offset);
cmsis_dap_add_jtag_sequence(
len,
sequence,
return;
}
- int cmd_len = 1 + DIV_ROUND_UP(s_len, 8);
+ unsigned int cmd_len = 1 + DIV_ROUND_UP(s_len, 8);
if (queued_seq_count >= 255 || queued_seq_buf_end + cmd_len > QUEUED_SEQ_BUF_LEN)
/* empty out the buffer */
cmsis_dap_flush();
uint8_t *command = cmsis_dap_handle->command;
for (unsigned i = 0; i < CMD_ARGC; i++)
- command[i] = strtoul(CMD_ARGV[i], NULL, 16);
+ COMMAND_PARSE_NUMBER(u8, CMD_ARGV[i], command[i]);
int retval = cmsis_dap_xfer(cmsis_dap_handle, CMD_ARGC);
return ERROR_OK;
}
-COMMAND_HANDLER(cmsis_dap_handle_serial_command)
-{
- if (CMD_ARGC == 1)
- cmsis_dap_serial = strdup(CMD_ARGV[0]);
- else
- LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
-
- return ERROR_OK;
-}
-
COMMAND_HANDLER(cmsis_dap_handle_backend_command)
{
if (CMD_ARGC == 1) {
.help = "the vendor ID and product ID of the CMSIS-DAP device",
.usage = "(vid pid)*",
},
- {
- .name = "cmsis_dap_serial",
- .handler = &cmsis_dap_handle_serial_command,
- .mode = COMMAND_CONFIG,
- .help = "set the serial number of the adapter",
- .usage = "serial_string",
- },
{
.name = "cmsis_dap_backend",
.handler = &cmsis_dap_handle_backend_command,