jtag/drivers/cmsis_dap: improve USB packets filling
[openocd.git] / src / jtag / drivers / cmsis_dap.c
index 9bd4cb73a4c8ed1f439757e400b566be9334966c..10e6638627def00d9f9050fe9a8ca56c9bd30bf1 100644 (file)
@@ -1,3 +1,5 @@
+// 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
@@ -40,6 +29,7 @@
 
 #include <transport/transport.h>
 #include "helper/replacements.h"
+#include <jtag/adapter.h>
 #include <jtag/swd.h>
 #include <jtag/interface.h>
 #include <jtag/commands.h>
@@ -47,6 +37,7 @@
 #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
@@ -75,12 +66,9 @@ static const struct cmsis_dap_backend *const cmsis_dap_backends[] = {
 /* 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
@@ -210,7 +198,7 @@ static bool swd_mode;
  * None as yet... */
 
 static const char * const info_caps_str[INFO_CAPS__NUM_CAPS] = {
-       "SWD  supported",
+       "SWD supported",
        "JTAG supported",
        "SWO-UART supported",
        "SWO-MANCHESTER supported",
@@ -221,38 +209,21 @@ static const char * const info_caps_str[INFO_CAPS__NUM_CAPS] = {
        "UART via USB COM port 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;
-};
-
 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
@@ -260,7 +231,7 @@ static int pending_scan_result_count;
 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;
@@ -288,13 +259,13 @@ static int cmsis_dap_open(void)
        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;
@@ -321,16 +292,15 @@ static void cmsis_dap_close(struct cmsis_dap *dap)
                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)
@@ -351,27 +321,27 @@ 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;
@@ -435,7 +405,7 @@ static int cmsis_dap_cmd_dap_swj_sequence(uint8_t s_len, const uint8_t *sequence
 
 #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");
@@ -587,6 +557,8 @@ static int cmsis_dap_metacmd_targetsel(uint32_t instance_id)
        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 */
@@ -672,7 +644,7 @@ static int cmsis_dap_cmd_dap_swo_baudrate(
        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);
@@ -772,12 +744,20 @@ static int cmsis_dap_cmd_dap_swo_data(
        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];
 
-       LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %d", block->transfer_count, 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",
+                         block->transfer_count, dap->pending_fifo_put_idx);
 
        if (queued_retval != ERROR_OK) {
                LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
@@ -792,12 +772,12 @@ static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
        command[2] = block->transfer_count;
        size_t idx = 3;
 
-       for (int i = 0; i < block->transfer_count; i++) {
+       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);
@@ -828,7 +808,7 @@ static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
                }
        }
 
-       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;
@@ -836,10 +816,10 @@ static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
                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;
 
@@ -849,14 +829,14 @@ skip:
 
 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) {
@@ -873,7 +853,7 @@ static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
                goto skip;
        }
 
-       uint8_t transfer_count = resp[1];
+       unsigned int 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);
@@ -892,10 +872,10 @@ static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
                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);
+       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) {
                        static uint32_t last_read;
@@ -903,7 +883,7 @@ static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
                        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) ||
@@ -919,22 +899,22 @@ static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
 
 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;
@@ -942,22 +922,56 @@ static int cmsis_dap_swd_run_queue(void)
        return retval;
 }
 
+static unsigned int cmsis_dap_tfer_cmd_size(unsigned int write_count,
+                                                                                       unsigned int read_count)
+{
+       unsigned int size = 3;                  /* 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)
+{
+       unsigned int size = 3;                  /* 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;
+       if (cmd & SWD_CMD_RNW)
+               read_count++;
+       else
+               write_count++;
+
+       unsigned int cmd_size = cmsis_dap_tfer_cmd_size(write_count, read_count);
+       unsigned int resp_size = cmsis_dap_tfer_resp_size(write_count, read_count);
+
+       /* 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;
 
@@ -966,13 +980,16 @@ static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
                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 (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++;
 }
@@ -1034,7 +1051,7 @@ static int cmsis_dap_get_caps_info(void)
 
                cmsis_dap_handle->caps = caps;
 
-               for (int i = 0; i < INFO_CAPS__NUM_CAPS; ++i) {
+               for (unsigned int i = 0; i < INFO_CAPS__NUM_CAPS; ++i) {
                        if (caps & BIT(i))
                                LOG_INFO("CMSIS-DAP: %s", info_caps_str[i]);
                }
@@ -1133,6 +1150,11 @@ static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq)
                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;
@@ -1144,7 +1166,7 @@ static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq)
 
        /* 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)
@@ -1207,7 +1229,6 @@ static int cmsis_dap_init(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);
@@ -1217,12 +1238,6 @@ static int cmsis_dap_init(void)
        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)
@@ -1232,23 +1247,34 @@ static int cmsis_dap_init(void)
                }
        }
 
+       /* 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;
@@ -1261,7 +1287,7 @@ static int cmsis_dap_init(void)
 
        /* 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;
 
@@ -1374,7 +1400,7 @@ static void cmsis_dap_end_state(tap_state_t state)
 }
 
 #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;
@@ -1385,7 +1411,7 @@ static void sprint_binary(char *s, const uint8_t *buf, int offset, int len)
        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(' ');
@@ -1403,18 +1429,18 @@ static void debug_parse_cmsis_buf(const uint8_t *cmd, int cmdlen)
        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)
@@ -1499,10 +1525,11 @@ static void cmsis_dap_flush(void)
  * 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);
 
@@ -1511,11 +1538,11 @@ static void cmsis_dap_add_jtag_sequence(int s_len, const uint8_t *sequence, int
 
        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,
@@ -1529,7 +1556,7 @@ static void cmsis_dap_add_jtag_sequence(int s_len, const uint8_t *sequence, int
                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();
@@ -2050,16 +2077,6 @@ COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
        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) {
@@ -2116,13 +2133,6 @@ static const struct command_registration cmsis_dap_command_handlers[] = {
                .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,

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)