ftdi: use "adapter usb location" instead of ftdi_location command
[openocd.git] / src / jtag / drivers / ftdi.c
index f427a6bdc35608d10346e1da7d5795c74e7d5e70..b709d28daaa678ffb1fd1c153ef49e0367e98579 100644 (file)
@@ -13,9 +13,7 @@
 *   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, write to the                         *
-*   Free Software Foundation, Inc.,                                       *
-*   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
+*   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
 
 /**
@@ -71,7 +69,9 @@
 #endif
 
 /* project specific includes */
+#include <jtag/drivers/jtag_usb_common.h>
 #include <jtag/interface.h>
+#include <jtag/swd.h>
 #include <transport/transport.h>
 #include <helper/time_support.h>
 
 #include "mpsse.h"
 
 #define JTAG_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
+#define JTAG_MODE_ALT (LSB_FIRST | NEG_EDGE_IN | NEG_EDGE_OUT)
+#define SWD_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
 
 static char *ftdi_device_desc;
 static char *ftdi_serial;
 static uint8_t ftdi_channel;
+static uint8_t ftdi_jtag_mode = JTAG_MODE;
+
+static bool swd_mode;
 
 #define MAX_USB_IDS 8
 /* vid = pid = 0 marks the end of the list */
@@ -100,16 +105,33 @@ static struct mpsse_ctx *mpsse_ctx;
 struct signal {
        const char *name;
        uint16_t data_mask;
+       uint16_t input_mask;
        uint16_t oe_mask;
        bool invert_data;
+       bool invert_input;
        bool invert_oe;
        struct signal *next;
 };
 
 static struct signal *signals;
 
+/* FIXME: Where to store per-instance data? We need an SWD context. */
+static struct swd_cmd_queue_entry {
+       uint8_t cmd;
+       uint32_t *dst;
+       uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
+} *swd_cmd_queue;
+static size_t swd_cmd_queue_length;
+static size_t swd_cmd_queue_alloced;
+static int queued_retval;
+static int freq;
+
 static uint16_t output;
 static uint16_t direction;
+static uint16_t jtag_output_init;
+static uint16_t jtag_direction_init;
+
+static int ftdi_swd_switch_seq(enum swd_special_seq seq);
 
 static struct signal *find_signal_by_name(const char *name)
 {
@@ -191,6 +213,32 @@ static int ftdi_set_signal(const struct signal *s, char value)
        return ERROR_OK;
 }
 
+static int ftdi_get_signal(const struct signal *s, uint16_t * value_out)
+{
+       uint8_t data_low = 0;
+       uint8_t data_high = 0;
+
+       if (s->input_mask == 0) {
+               LOG_ERROR("interface doesn't provide signal '%s'", s->name);
+               return ERROR_FAIL;
+       }
+
+       if (s->input_mask & 0xff)
+               mpsse_read_data_bits_low_byte(mpsse_ctx, &data_low);
+       if (s->input_mask >> 8)
+               mpsse_read_data_bits_high_byte(mpsse_ctx, &data_high);
+
+       mpsse_flush(mpsse_ctx);
+
+       *value_out = (((uint16_t)data_high) << 8) | data_low;
+
+       if (s->invert_input)
+               *value_out = ~(*value_out);
+
+       *value_out &= s->input_mask;
+
+       return ERROR_OK;
+}
 
 /**
  * Function move_to_state
@@ -225,7 +273,7 @@ static void move_to_state(tap_state_t goal_state)
                0,
                tms_count,
                false,
-               JTAG_MODE);
+               ftdi_jtag_mode);
 }
 
 static int ftdi_speed(int speed)
@@ -238,6 +286,9 @@ static int ftdi_speed(int speed)
                return retval;
        }
 
+       if (!swd_mode && speed >= 10000000 && ftdi_jtag_mode != JTAG_MODE_ALT)
+               LOG_INFO("ftdi: if you experience problems at higher adapter clocks, try "
+                        "the command \"ftdi_tdo_sample_edge falling\"");
        return ERROR_OK;
 }
 
@@ -285,7 +336,7 @@ static void ftdi_execute_runtest(struct jtag_command *cmd)
        while (i > 0) {
                /* there are no state transitions in this code, so omit state tracking */
                unsigned this_len = i > 7 ? 7 : i;
-               mpsse_clock_tms_cs_out(mpsse_ctx, &zero, 0, this_len, false, JTAG_MODE);
+               mpsse_clock_tms_cs_out(mpsse_ctx, &zero, 0, this_len, false, ftdi_jtag_mode);
                i -= this_len;
        }
 
@@ -325,7 +376,7 @@ static void ftdi_execute_tms(struct jtag_command *cmd)
                0,
                cmd->cmd.tms->num_bits,
                false,
-               JTAG_MODE);
+               ftdi_jtag_mode);
 }
 
 static void ftdi_execute_pathmove(struct jtag_command *cmd)
@@ -372,7 +423,7 @@ static void ftdi_execute_pathmove(struct jtag_command *cmd)
                                        0,
                                        bit_count,
                                        false,
-                                       JTAG_MODE);
+                                       ftdi_jtag_mode);
                        bit_count = 0;
                }
        }
@@ -388,11 +439,11 @@ static void ftdi_execute_scan(struct jtag_command *cmd)
        while (cmd->cmd.scan->num_fields > 0
                        && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
                cmd->cmd.scan->num_fields--;
-               LOG_DEBUG("discarding trailing empty field");
+               DEBUG_JTAG_IO("discarding trailing empty field");
        }
 
        if (cmd->cmd.scan->num_fields == 0) {
-               LOG_DEBUG("empty scan, doing nothing");
+               DEBUG_JTAG_IO("empty scan, doing nothing");
                return;
        }
 
@@ -427,7 +478,7 @@ static void ftdi_execute_scan(struct jtag_command *cmd)
                                field->in_value,
                                0,
                                field->num_bits - 1,
-                               JTAG_MODE);
+                               ftdi_jtag_mode);
                        uint8_t last_bit = 0;
                        if (field->out_value)
                                bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
@@ -439,14 +490,14 @@ static void ftdi_execute_scan(struct jtag_command *cmd)
                                        field->num_bits - 1,
                                        1,
                                        last_bit,
-                                       JTAG_MODE);
+                                       ftdi_jtag_mode);
                        tap_set_state(tap_state_transition(tap_get_state(), 1));
                        mpsse_clock_tms_cs_out(mpsse_ctx,
                                        &tms_bits,
                                        1,
                                        1,
                                        last_bit,
-                                       JTAG_MODE);
+                                       ftdi_jtag_mode);
                        tap_set_state(tap_state_transition(tap_get_state(), 0));
                } else
                        mpsse_clock_data(mpsse_ctx,
@@ -455,7 +506,7 @@ static void ftdi_execute_scan(struct jtag_command *cmd)
                                field->in_value,
                                0,
                                field->num_bits,
-                               JTAG_MODE);
+                               ftdi_jtag_mode);
        }
 
        if (tap_get_state() != tap_get_end_state())
@@ -482,7 +533,8 @@ static void ftdi_execute_reset(struct jtag_command *cmd)
                        ftdi_set_signal(trst, '0');
                else
                        LOG_ERROR("Can't assert TRST: nTRST signal is not defined");
-       } else if (trst && cmd->cmd.reset->trst == 0) {
+       } else if (trst && jtag_get_reset_config() & RESET_HAS_TRST &&
+                       cmd->cmd.reset->trst == 0) {
                if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN)
                        ftdi_set_signal(trst, 'z');
                else
@@ -495,7 +547,8 @@ static void ftdi_execute_reset(struct jtag_command *cmd)
                        ftdi_set_signal(srst, '0');
                else
                        LOG_ERROR("Can't assert SRST: nSRST signal is not defined");
-       } else if (srst && cmd->cmd.reset->srst == 0) {
+       } else if (srst && jtag_get_reset_config() & RESET_HAS_SRST &&
+                       cmd->cmd.reset->srst == 0) {
                if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL)
                        ftdi_set_signal(srst, '1');
                else
@@ -532,7 +585,7 @@ static void ftdi_execute_stableclocks(struct jtag_command *cmd)
        while (num_cycles > 0) {
                /* there are no state transitions in this code, so omit state tracking */
                unsigned this_len = num_cycles > 7 ? 7 : num_cycles;
-               mpsse_clock_tms_cs_out(mpsse_ctx, &tms, 0, this_len, false, JTAG_MODE);
+               mpsse_clock_tms_cs_out(mpsse_ctx, &tms, 0, this_len, false, ftdi_jtag_mode);
                num_cycles -= this_len;
        }
 
@@ -605,7 +658,7 @@ static int ftdi_initialize(void)
 
        for (int i = 0; ftdi_vid[i] || ftdi_pid[i]; i++) {
                mpsse_ctx = mpsse_open(&ftdi_vid[i], &ftdi_pid[i], ftdi_device_desc,
-                               ftdi_serial, ftdi_channel);
+                               ftdi_serial, jtag_usb_get_location(), ftdi_channel);
                if (mpsse_ctx)
                        break;
        }
@@ -613,11 +666,27 @@ static int ftdi_initialize(void)
        if (!mpsse_ctx)
                return ERROR_JTAG_INIT_FAILED;
 
+       output = jtag_output_init;
+       direction = jtag_direction_init;
+
+       if (swd_mode) {
+               struct signal *sig = find_signal_by_name("SWD_EN");
+               if (!sig) {
+                       LOG_ERROR("SWD mode is active but SWD_EN signal is not defined");
+                       return ERROR_JTAG_INIT_FAILED;
+               }
+               /* A dummy SWD_EN would have zero mask */
+               if (sig->data_mask)
+                       ftdi_set_signal(sig, '1');
+       }
+
        mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff);
        mpsse_set_data_bits_high_byte(mpsse_ctx, output >> 8, direction >> 8);
 
        mpsse_loopback_config(mpsse_ctx, false);
 
+       freq = mpsse_set_frequency(mpsse_ctx, jtag_get_speed_khz() * 1000);
+
        return mpsse_flush(mpsse_ctx);
 }
 
@@ -625,6 +694,19 @@ static int ftdi_quit(void)
 {
        mpsse_close(mpsse_ctx);
 
+       struct signal *sig = signals;
+       while (sig) {
+               struct signal *next = sig->next;
+               free((void *)sig->name);
+               free(sig);
+               sig = next;
+       }
+
+       free(ftdi_device_desc);
+       free(ftdi_serial);
+
+       free(swd_cmd_queue);
+
        return ERROR_OK;
 }
 
@@ -669,8 +751,8 @@ COMMAND_HANDLER(ftdi_handle_layout_init_command)
        if (CMD_ARGC != 2)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], output);
-       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], direction);
+       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], jtag_output_init);
+       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], jtag_direction_init);
 
        return ERROR_OK;
 }
@@ -682,6 +764,8 @@ COMMAND_HANDLER(ftdi_handle_layout_signal_command)
 
        bool invert_data = false;
        uint16_t data_mask = 0;
+       bool invert_input = false;
+       uint16_t input_mask = 0;
        bool invert_oe = false;
        uint16_t oe_mask = 0;
        for (unsigned i = 1; i < CMD_ARGC; i += 2) {
@@ -691,12 +775,35 @@ COMMAND_HANDLER(ftdi_handle_layout_signal_command)
                } else if (strcmp("-ndata", CMD_ARGV[i]) == 0) {
                        invert_data = true;
                        COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
+               } else if (strcmp("-input", CMD_ARGV[i]) == 0) {
+                       invert_input = false;
+                       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
+               } else if (strcmp("-ninput", CMD_ARGV[i]) == 0) {
+                       invert_input = true;
+                       COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
                } else if (strcmp("-oe", CMD_ARGV[i]) == 0) {
                        invert_oe = false;
                        COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
                } else if (strcmp("-noe", CMD_ARGV[i]) == 0) {
                        invert_oe = true;
                        COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
+               } else if (!strcmp("-alias", CMD_ARGV[i]) ||
+                          !strcmp("-nalias", CMD_ARGV[i])) {
+                       if (!strcmp("-nalias", CMD_ARGV[i])) {
+                               invert_data = true;
+                               invert_input = true;
+                       }
+                       struct signal *sig = find_signal_by_name(CMD_ARGV[i + 1]);
+                       if (!sig) {
+                               LOG_ERROR("signal %s is not defined", CMD_ARGV[i + 1]);
+                               return ERROR_FAIL;
+                       }
+                       data_mask = sig->data_mask;
+                       input_mask = sig->input_mask;
+                       oe_mask = sig->oe_mask;
+                       invert_input ^= sig->invert_input;
+                       invert_oe = sig->invert_oe;
+                       invert_data ^= sig->invert_data;
                } else {
                        LOG_ERROR("unknown option '%s'", CMD_ARGV[i]);
                        return ERROR_COMMAND_SYNTAX_ERROR;
@@ -714,6 +821,8 @@ COMMAND_HANDLER(ftdi_handle_layout_signal_command)
 
        sig->invert_data = invert_data;
        sig->data_mask = data_mask;
+       sig->invert_input = invert_input;
+       sig->input_mask = input_mask;
        sig->invert_oe = invert_oe;
        sig->oe_mask = oe_mask;
 
@@ -742,6 +851,7 @@ COMMAND_HANDLER(ftdi_handle_set_signal_command)
                        ftdi_set_signal(sig, *CMD_ARGV[1]);
                        break;
                }
+               /* fallthrough */
        default:
                LOG_ERROR("unknown signal level '%s', use 0, 1 or z", CMD_ARGV[1]);
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -750,6 +860,28 @@ COMMAND_HANDLER(ftdi_handle_set_signal_command)
        return mpsse_flush(mpsse_ctx);
 }
 
+COMMAND_HANDLER(ftdi_handle_get_signal_command)
+{
+       if (CMD_ARGC < 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+
+       struct signal *sig;
+       uint16_t sig_data = 0;
+       sig = find_signal_by_name(CMD_ARGV[0]);
+       if (!sig) {
+               LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
+               return ERROR_FAIL;
+       }
+
+       int ret = ftdi_get_signal(sig, &sig_data);
+       if (ret != ERROR_OK)
+               return ret;
+
+       LOG_USER("Signal %s = %#06x", sig->name, sig_data);
+
+       return ERROR_OK;
+}
+
 COMMAND_HANDLER(ftdi_handle_vid_pid_command)
 {
        if (CMD_ARGC > MAX_USB_IDS * 2) {
@@ -780,6 +912,29 @@ COMMAND_HANDLER(ftdi_handle_vid_pid_command)
        return ERROR_OK;
 }
 
+COMMAND_HANDLER(ftdi_handle_tdo_sample_edge_command)
+{
+       Jim_Nvp *n;
+       static const Jim_Nvp nvp_ftdi_jtag_modes[] = {
+               { .name = "rising", .value = JTAG_MODE },
+               { .name = "falling", .value = JTAG_MODE_ALT },
+               { .name = NULL, .value = -1 },
+       };
+
+       if (CMD_ARGC > 0) {
+               n = Jim_Nvp_name2value_simple(nvp_ftdi_jtag_modes, CMD_ARGV[0]);
+               if (n->name == NULL)
+                       return ERROR_COMMAND_SYNTAX_ERROR;
+               ftdi_jtag_mode = n->value;
+
+       }
+
+       n = Jim_Nvp_value2name_simple(nvp_ftdi_jtag_modes, ftdi_jtag_mode);
+       command_print(CMD_CTX, "ftdi samples TDO on %s edge of TCK", n->name);
+
+       return ERROR_OK;
+}
+
 static const struct command_registration ftdi_command_handlers[] = {
        {
                .name = "ftdi_device_desc",
@@ -816,7 +971,7 @@ static const struct command_registration ftdi_command_handlers[] = {
                .mode = COMMAND_ANY,
                .help = "define a signal controlled by one or more FTDI GPIO as data "
                        "and/or output enable",
-               .usage = "name [-data mask|-ndata mask] [-oe mask|-noe mask]",
+               .usage = "name [-data mask|-ndata mask] [-oe mask|-noe mask] [-alias|-nalias name]",
        },
        {
                .name = "ftdi_set_signal",
@@ -825,6 +980,13 @@ static const struct command_registration ftdi_command_handlers[] = {
                .help = "control a layout-specific signal",
                .usage = "name (1|0|z)",
        },
+       {
+               .name = "ftdi_get_signal",
+               .handler = &ftdi_handle_get_signal_command,
+               .mode = COMMAND_EXEC,
+               .help = "read the value of a layout-specific signal",
+               .usage = "name",
+       },
        {
                .name = "ftdi_vid_pid",
                .handler = &ftdi_handle_vid_pid_command,
@@ -832,14 +994,266 @@ static const struct command_registration ftdi_command_handlers[] = {
                .help = "the vendor ID and product ID of the FTDI device",
                .usage = "(vid pid)* ",
        },
+       {
+               .name = "ftdi_tdo_sample_edge",
+               .handler = &ftdi_handle_tdo_sample_edge_command,
+               .mode = COMMAND_ANY,
+               .help = "set which TCK clock edge is used for sampling TDO "
+                       "- default is rising-edge (Setting to falling-edge may "
+                       "allow signalling speed increase)",
+               .usage = "(rising|falling)",
+       },
        COMMAND_REGISTRATION_DONE
 };
 
+static int create_default_signal(const char *name, uint16_t data_mask)
+{
+       struct signal *sig = create_signal(name);
+       if (!sig) {
+               LOG_ERROR("failed to create signal %s", name);
+               return ERROR_FAIL;
+       }
+       sig->invert_data = false;
+       sig->data_mask = data_mask;
+       sig->invert_oe = false;
+       sig->oe_mask = 0;
+
+       return ERROR_OK;
+}
+
+static int create_signals(void)
+{
+       if (create_default_signal("TCK", 0x01) != ERROR_OK)
+               return ERROR_FAIL;
+       if (create_default_signal("TDI", 0x02) != ERROR_OK)
+               return ERROR_FAIL;
+       if (create_default_signal("TDO", 0x04) != ERROR_OK)
+               return ERROR_FAIL;
+       if (create_default_signal("TMS", 0x08) != ERROR_OK)
+               return ERROR_FAIL;
+       return ERROR_OK;
+}
+
+static int ftdi_swd_init(void)
+{
+       LOG_INFO("FTDI SWD mode enabled");
+       swd_mode = true;
+
+       if (create_signals() != ERROR_OK)
+               return ERROR_FAIL;
+
+       swd_cmd_queue_alloced = 10;
+       swd_cmd_queue = malloc(swd_cmd_queue_alloced * sizeof(*swd_cmd_queue));
+
+       return swd_cmd_queue != NULL ? ERROR_OK : ERROR_FAIL;
+}
+
+static void ftdi_swd_swdio_en(bool enable)
+{
+       struct signal *oe = find_signal_by_name("SWDIO_OE");
+       if (oe) {
+               if (oe->data_mask)
+                       ftdi_set_signal(oe, enable ? '1' : '0');
+               else {
+                       /* Sets TDI/DO pin (pin 2) to input during rx when both pins are connected
+                          to SWDIO */
+                       if (enable)
+                               direction |= jtag_direction_init & 0x0002U;
+                       else
+                               direction &= ~0x0002U;
+                       mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff);
+               }
+       }
+}
+
+/**
+ * Flush the MPSSE queue and process the SWD transaction queue
+ * @param dap
+ * @return
+ */
+static int ftdi_swd_run_queue(void)
+{
+       LOG_DEBUG_IO("Executing %zu queued transactions", swd_cmd_queue_length);
+       int retval;
+       struct signal *led = find_signal_by_name("LED");
+
+       if (queued_retval != ERROR_OK) {
+               LOG_DEBUG_IO("Skipping due to previous errors: %d", queued_retval);
+               goto skip;
+       }
+
+       /* A transaction must be followed by another transaction or at least 8 idle cycles to
+        * ensure that data is clocked through the AP. */
+       mpsse_clock_data_out(mpsse_ctx, NULL, 0, 8, SWD_MODE);
+
+       /* Terminate the "blink", if the current layout has that feature */
+       if (led)
+               ftdi_set_signal(led, '0');
+
+       queued_retval = mpsse_flush(mpsse_ctx);
+       if (queued_retval != ERROR_OK) {
+               LOG_ERROR("MPSSE failed");
+               goto skip;
+       }
+
+       for (size_t i = 0; i < swd_cmd_queue_length; i++) {
+               int ack = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1, 3);
+
+               LOG_DEBUG_IO("%s %s %s reg %X = %08"PRIx32,
+                               ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
+                               swd_cmd_queue[i].cmd & SWD_CMD_APnDP ? "AP" : "DP",
+                               swd_cmd_queue[i].cmd & SWD_CMD_RnW ? "read" : "write",
+                               (swd_cmd_queue[i].cmd & SWD_CMD_A32) >> 1,
+                               buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn,
+                                               1 + 3 + (swd_cmd_queue[i].cmd & SWD_CMD_RnW ? 0 : 1), 32));
+
+               if (ack != SWD_ACK_OK) {
+                       queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
+                       goto skip;
+
+               } else if (swd_cmd_queue[i].cmd & SWD_CMD_RnW) {
+                       uint32_t data = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3, 32);
+                       int parity = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 32, 1);
+
+                       if (parity != parity_u32(data)) {
+                               LOG_ERROR("SWD Read data parity mismatch");
+                               queued_retval = ERROR_FAIL;
+                               goto skip;
+                       }
+
+                       if (swd_cmd_queue[i].dst != NULL)
+                               *swd_cmd_queue[i].dst = data;
+               }
+       }
+
+skip:
+       swd_cmd_queue_length = 0;
+       retval = queued_retval;
+       queued_retval = ERROR_OK;
+
+       /* Queue a new "blink" */
+       if (led && retval == ERROR_OK)
+               ftdi_set_signal(led, '1');
+
+       return retval;
+}
+
+static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
+{
+       if (swd_cmd_queue_length >= swd_cmd_queue_alloced) {
+               /* Not enough room in the queue. Run the queue and increase its size for next time.
+                * Note that it's not possible to avoid running the queue here, because mpsse contains
+                * pointers into the queue which may be invalid after the realloc. */
+               queued_retval = ftdi_swd_run_queue();
+               struct swd_cmd_queue_entry *q = realloc(swd_cmd_queue, swd_cmd_queue_alloced * 2 * sizeof(*swd_cmd_queue));
+               if (q != NULL) {
+                       swd_cmd_queue = q;
+                       swd_cmd_queue_alloced *= 2;
+                       LOG_DEBUG("Increased SWD command queue to %zu elements", swd_cmd_queue_alloced);
+               }
+       }
+
+       if (queued_retval != ERROR_OK)
+               return;
+
+       size_t i = swd_cmd_queue_length++;
+       swd_cmd_queue[i].cmd = cmd | SWD_CMD_START | SWD_CMD_PARK;
+
+       mpsse_clock_data_out(mpsse_ctx, &swd_cmd_queue[i].cmd, 0, 8, SWD_MODE);
+
+       if (swd_cmd_queue[i].cmd & SWD_CMD_RnW) {
+               /* Queue a read transaction */
+               swd_cmd_queue[i].dst = dst;
+
+               ftdi_swd_swdio_en(false);
+               mpsse_clock_data_in(mpsse_ctx, swd_cmd_queue[i].trn_ack_data_parity_trn,
+                               0, 1 + 3 + 32 + 1 + 1, SWD_MODE);
+               ftdi_swd_swdio_en(true);
+       } else {
+               /* Queue a write transaction */
+               ftdi_swd_swdio_en(false);
+
+               mpsse_clock_data_in(mpsse_ctx, swd_cmd_queue[i].trn_ack_data_parity_trn,
+                               0, 1 + 3 + 1, SWD_MODE);
+
+               ftdi_swd_swdio_en(true);
+
+               buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1, 32, data);
+               buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(data));
+
+               mpsse_clock_data_out(mpsse_ctx, swd_cmd_queue[i].trn_ack_data_parity_trn,
+                               1 + 3 + 1, 32 + 1, SWD_MODE);
+       }
+
+       /* Insert idle cycles after AP accesses to avoid WAIT */
+       if (cmd & SWD_CMD_APnDP)
+               mpsse_clock_data_out(mpsse_ctx, NULL, 0, ap_delay_clk, SWD_MODE);
+
+}
+
+static void ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
+{
+       assert(cmd & SWD_CMD_RnW);
+       ftdi_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
+}
+
+static void ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
+{
+       assert(!(cmd & SWD_CMD_RnW));
+       ftdi_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
+}
+
+static int_least32_t ftdi_swd_frequency(int_least32_t hz)
+{
+       if (hz > 0)
+               freq = mpsse_set_frequency(mpsse_ctx, hz);
+
+       return freq;
+}
+
+static int ftdi_swd_switch_seq(enum swd_special_seq seq)
+{
+       switch (seq) {
+       case LINE_RESET:
+               LOG_DEBUG("SWD line reset");
+               ftdi_swd_swdio_en(true);
+               mpsse_clock_data_out(mpsse_ctx, swd_seq_line_reset, 0, swd_seq_line_reset_len, SWD_MODE);
+               break;
+       case JTAG_TO_SWD:
+               LOG_DEBUG("JTAG-to-SWD");
+               ftdi_swd_swdio_en(true);
+               mpsse_clock_data_out(mpsse_ctx, swd_seq_jtag_to_swd, 0, swd_seq_jtag_to_swd_len, SWD_MODE);
+               break;
+       case SWD_TO_JTAG:
+               LOG_DEBUG("SWD-to-JTAG");
+               ftdi_swd_swdio_en(true);
+               mpsse_clock_data_out(mpsse_ctx, swd_seq_swd_to_jtag, 0, swd_seq_swd_to_jtag_len, SWD_MODE);
+               break;
+       default:
+               LOG_ERROR("Sequence %d not supported", seq);
+               return ERROR_FAIL;
+       }
+
+       return ERROR_OK;
+}
+
+static const struct swd_driver ftdi_swd = {
+       .init = ftdi_swd_init,
+       .frequency = ftdi_swd_frequency,
+       .switch_seq = ftdi_swd_switch_seq,
+       .read_reg = ftdi_swd_read_reg,
+       .write_reg = ftdi_swd_write_reg,
+       .run = ftdi_swd_run_queue,
+};
+
+static const char * const ftdi_transports[] = { "jtag", "swd", NULL };
+
 struct jtag_interface ftdi_interface = {
        .name = "ftdi",
        .supported = DEBUG_CAP_TMS_SEQ,
        .commands = ftdi_command_handlers,
-       .transports = jtag_only,
+       .transports = ftdi_transports,
+       .swd = &ftdi_swd,
 
        .init = ftdi_initialize,
        .quit = ftdi_quit,

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)