jtag: linuxgpiod: drop extra parenthesis
[openocd.git] / src / jtag / drivers / buspirate.c
index 14c719d4ce84b919a0286f4e975596d0241e9de1..3b03337c942d59a091f6295d06ba481eba8dc9c1 100644 (file)
@@ -1,20 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2010 by Michal Demin                                    *
  *   based on usbprog.c and arm-jtag-ew.c                                  *
  *   Several fixes by R. Diez in 2013.                                     *
- *                                                                         *
- *   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
 
 #undef DEBUG_SERIAL
 /*#define DEBUG_SERIAL */
-static int buspirate_execute_queue(void);
+static int buspirate_execute_queue(struct jtag_command *cmd_queue);
 static int buspirate_init(void);
 static int buspirate_quit(void);
+static int buspirate_reset(int trst, int srst);
 
 static void buspirate_end_state(tap_state_t state);
 static void buspirate_state_move(void);
@@ -57,6 +47,8 @@ static void buspirate_stableclocks(int num_cycles);
 #define CMD_RAW_SPEED     0x60
 #define CMD_RAW_MODE      0x80
 
+#define CMD_TAP_SHIFT_HEADER_LEN 3
+
 /* raw-wire mode configuration */
 #define CMD_RAW_CONFIG_HIZ 0x00
 #define CMD_RAW_CONFIG_3V3 0x08
@@ -70,6 +62,9 @@ static void buspirate_stableclocks(int num_cycles);
 #define  B1000000 0010010
 #endif
 
+#define SHORT_TIMEOUT  1  /* Must be at least 1. */
+#define NORMAL_TIMEOUT 10
+
 enum {
        MODE_HIZ = 0,
        MODE_JTAG = 1,          /* push-pull outputs */
@@ -106,9 +101,6 @@ static bool swd_mode;
 static int  queued_retval;
 static char swd_features;
 
-static const cc_t SHORT_TIMEOUT  = 1; /* Must be at least 1. */
-static const cc_t NORMAL_TIMEOUT = 10;
-
 static int buspirate_fd = -1;
 static int buspirate_pinmode = MODE_JTAG_OD;
 static int buspirate_baudrate = SERIAL_NORMAL;
@@ -133,7 +125,6 @@ static void buspirate_tap_append_scan(int length, uint8_t *buffer,
                struct scan_command *command);
 static void buspirate_tap_make_space(int scan, int bits);
 
-static void buspirate_reset(int trst, int srst);
 static void buspirate_set_feature(int, char, char);
 static void buspirate_set_mode(int, char);
 static void buspirate_set_speed(int, char);
@@ -160,10 +151,10 @@ static int buspirate_serial_read(int fd, uint8_t *buf, int size);
 static void buspirate_serial_close(int fd);
 static void buspirate_print_buffer(uint8_t *buf, int size);
 
-static int buspirate_execute_queue(void)
+static int buspirate_execute_queue(struct jtag_command *cmd_queue)
 {
        /* currently processed command */
-       struct jtag_command *cmd = jtag_command_queue;
+       struct jtag_command *cmd = cmd_queue;
        int scan_size;
        enum scan_type type;
        uint8_t *buffer;
@@ -171,7 +162,7 @@ static int buspirate_execute_queue(void)
        while (cmd) {
                switch (cmd->type) {
                case JTAG_RUNTEST:
-                       DEBUG_JTAG_IO("runtest %i cycles, end in %s",
+                       LOG_DEBUG_IO("runtest %i cycles, end in %s",
                                cmd->cmd.runtest->num_cycles,
                                tap_state_name(cmd->cmd.runtest
                                        ->end_state));
@@ -181,7 +172,7 @@ static int buspirate_execute_queue(void)
                                        ->num_cycles);
                        break;
                case JTAG_TLR_RESET:
-                       DEBUG_JTAG_IO("statemove end in %s",
+                       LOG_DEBUG_IO("statemove end in %s",
                                tap_state_name(cmd->cmd.statemove
                                                ->end_state));
                        buspirate_end_state(cmd->cmd.statemove
@@ -189,7 +180,7 @@ static int buspirate_execute_queue(void)
                        buspirate_state_move();
                        break;
                case JTAG_PATHMOVE:
-                       DEBUG_JTAG_IO("pathmove: %i states, end in %s",
+                       LOG_DEBUG_IO("pathmove: %i states, end in %s",
                                cmd->cmd.pathmove->num_states,
                                tap_state_name(cmd->cmd.pathmove
                                        ->path[cmd->cmd.pathmove
@@ -199,7 +190,7 @@ static int buspirate_execute_queue(void)
                                        cmd->cmd.pathmove->path);
                        break;
                case JTAG_SCAN:
-                       DEBUG_JTAG_IO("scan end in %s",
+                       LOG_DEBUG_IO("scan end in %s",
                                tap_state_name(cmd->cmd.scan
                                        ->end_state));
 
@@ -213,25 +204,13 @@ static int buspirate_execute_queue(void)
                                buffer, scan_size, cmd->cmd.scan);
 
                        break;
-               case JTAG_RESET:
-                       DEBUG_JTAG_IO("reset trst: %i srst %i",
-                               cmd->cmd.reset->trst, cmd->cmd.reset->srst);
-
-                       /* flush buffers, so we can reset */
-                       buspirate_tap_execute();
-
-                       if (cmd->cmd.reset->trst == 1)
-                               tap_set_state(TAP_RESET);
-                       buspirate_reset(cmd->cmd.reset->trst,
-                                       cmd->cmd.reset->srst);
-                       break;
                case JTAG_SLEEP:
-                       DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
+                       LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
                        buspirate_tap_execute();
                        jtag_sleep(cmd->cmd.sleep->us);
                                break;
                case JTAG_STABLECLOCKS:
-                       DEBUG_JTAG_IO("stable clock %i cycles", cmd->cmd.stableclocks->num_cycles);
+                       LOG_DEBUG_IO("stable clock %i cycles", cmd->cmd.stableclocks->num_cycles);
                        buspirate_stableclocks(cmd->cmd.stableclocks->num_cycles);
                                break;
                default:
@@ -295,7 +274,7 @@ static bool read_and_discard_all_data(const int fd)
 
 static int buspirate_init(void)
 {
-       if (buspirate_port == NULL) {
+       if (!buspirate_port) {
                LOG_ERROR("You need to specify the serial port!");
                return ERROR_JTAG_INIT_FAILED;
        }
@@ -364,10 +343,8 @@ static int buspirate_quit(void)
 
        buspirate_serial_close(buspirate_fd);
 
-       if (buspirate_port) {
-               free(buspirate_port);
-               buspirate_port = NULL;
-       }
+       free(buspirate_port);
+       buspirate_port = NULL;
        return ERROR_OK;
 }
 
@@ -478,58 +455,58 @@ COMMAND_HANDLER(buspirate_handle_port_command)
        if (CMD_ARGC < 1)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       if (buspirate_port == NULL)
+       if (!buspirate_port)
                buspirate_port = strdup(CMD_ARGV[0]);
 
        return ERROR_OK;
 
 }
 
-static const struct command_registration buspirate_command_handlers[] = {
+static const struct command_registration buspirate_subcommand_handlers[] = {
        {
-               .name = "buspirate_adc",
+               .name = "adc",
                .handler = &buspirate_handle_adc_command,
                .mode = COMMAND_EXEC,
                .help = "reads voltages on adc pins",
                .usage = "",
        },
        {
-               .name = "buspirate_vreg",
+               .name = "vreg",
                .usage = "<1|0>",
                .handler = &buspirate_handle_vreg_command,
                .mode = COMMAND_CONFIG,
                .help = "changes the state of voltage regulators",
        },
        {
-               .name = "buspirate_pullup",
+               .name = "pullup",
                .usage = "<1|0>",
                .handler = &buspirate_handle_pullup_command,
                .mode = COMMAND_CONFIG,
                .help = "changes the state of pullup",
        },
        {
-               .name = "buspirate_led",
+               .name = "led",
                .usage = "<1|0>",
                .handler = &buspirate_handle_led_command,
                .mode = COMMAND_EXEC,
                .help = "changes the state of led",
        },
        {
-               .name = "buspirate_speed",
+               .name = "speed",
                .usage = "<normal|fast>",
                .handler = &buspirate_handle_speed_command,
                .mode = COMMAND_CONFIG,
                .help = "speed of the interface",
        },
        {
-               .name = "buspirate_mode",
+               .name = "mode",
                .usage = "<normal|open-drain>",
                .handler = &buspirate_handle_mode_command,
                .mode = COMMAND_CONFIG,
                .help = "pin mode of the interface",
        },
        {
-               .name = "buspirate_port",
+               .name = "port",
                .usage = "/dev/ttyUSB0",
                .handler = &buspirate_handle_port_command,
                .mode = COMMAND_CONFIG,
@@ -538,6 +515,17 @@ static const struct command_registration buspirate_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
+static const struct command_registration buspirate_command_handlers[] = {
+       {
+               .name = "buspirate",
+               .mode = COMMAND_ANY,
+               .help = "perform buspirate management",
+               .chain = buspirate_subcommand_handlers,
+               .usage = "",
+       },
+       COMMAND_REGISTRATION_DONE
+};
+
 static const struct swd_driver buspirate_swd = {
        .init = buspirate_swd_init,
        .switch_seq = buspirate_swd_switch_seq,
@@ -548,14 +536,21 @@ static const struct swd_driver buspirate_swd = {
 
 static const char * const buspirate_transports[] = { "jtag", "swd", NULL };
 
-struct jtag_interface buspirate_interface = {
-       .name = "buspirate",
+static struct jtag_interface buspirate_interface = {
        .execute_queue = buspirate_execute_queue,
-       .commands = buspirate_command_handlers,
+};
+
+struct adapter_driver buspirate_adapter_driver = {
+       .name = "buspirate",
        .transports = buspirate_transports,
-       .swd = &buspirate_swd,
+       .commands = buspirate_command_handlers,
+
        .init = buspirate_init,
-       .quit = buspirate_quit
+       .quit = buspirate_quit,
+       .reset = buspirate_reset,
+
+       .jtag_ops = &buspirate_interface,
+       .swd_ops = &buspirate_swd,
 };
 
 /*************** jtag execute commands **********************/
@@ -624,7 +619,7 @@ static void buspirate_runtest(int num_cycles)
        for (i = 0; i < num_cycles; i++)
                buspirate_tap_append(0, 0);
 
-       DEBUG_JTAG_IO("runtest: cur_state %s end_state %s",
+       LOG_DEBUG_IO("runtest: cur_state %s end_state %s",
                        tap_state_name(tap_get_state()),
                        tap_state_name(tap_get_end_state()));
 
@@ -715,8 +710,6 @@ static void buspirate_tap_init(void)
 
 static int buspirate_tap_execute(void)
 {
-       static const int CMD_TAP_SHIFT_HEADER_LEN = 3;
-
        uint8_t tmp[4096];
        uint8_t *in_buf;
        int i;
@@ -807,7 +800,7 @@ static void buspirate_tap_append(int tms, int tdi)
                int bit_index = tap_chain_index % 8;
                uint8_t bit = 1 << bit_index;
 
-               if (0 == bit_index) {
+               if (bit_index == 0) {
                        /* Let's say that the TAP shift operation wants to shift 9 bits,
                           so we will be sending to the Bus Pirate a bit count of 9 but still
                           full 16 bits (2 bytes) of shift data.
@@ -860,7 +853,7 @@ static void buspirate_tap_append_scan(int length, uint8_t *buffer,
 /*************** wrapper functions *********************/
 
 /* (1) assert or (0) deassert reset lines */
-static void buspirate_reset(int trst, int srst)
+static int buspirate_reset(int trst, int srst)
 {
        LOG_DEBUG("trst: %i, srst: %i", trst, srst);
 
@@ -873,6 +866,8 @@ static void buspirate_reset(int trst, int srst)
                buspirate_set_feature(buspirate_fd, FEATURE_SRST, ACTION_DISABLE);
        else
                buspirate_set_feature(buspirate_fd, FEATURE_SRST, ACTION_ENABLE);
+
+       return ERROR_OK;
 }
 
 static void buspirate_set_feature(int fd, char feat, char action)
@@ -1052,7 +1047,7 @@ static void buspirate_jtag_reset(int fd)
        tmp[0] = 0x00; /* exit OCD1 mode */
        buspirate_serial_write(fd, tmp, 1);
        usleep(10000);
-       /* We ignore the return value here purposly, nothing we can do */
+       /* We ignore the return value here on purpose, nothing we can do */
        buspirate_serial_read(fd, tmp, 5);
        if (strncmp((char *)tmp, "BBIO1", 5) == 0) {
                tmp[0] = 0x0F; /*  reset BP */
@@ -1183,13 +1178,13 @@ static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout)
 
        /* set the serial port parameters */
        fcntl(fd, F_SETFL, 0);
-       if (0 != tcgetattr(fd, &t_opt))
+       if (tcgetattr(fd, &t_opt) != 0)
                return -1;
 
-       if (0 != cfsetispeed(&t_opt, baud))
+       if (cfsetispeed(&t_opt, baud) != 0)
                return -1;
 
-       if (0 != cfsetospeed(&t_opt, baud))
+       if (cfsetospeed(&t_opt, baud) != 0)
                return -1;
 
        t_opt.c_cflag |= (CLOCAL | CREAD);
@@ -1211,7 +1206,7 @@ static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout)
        /* Note that, in the past, TCSANOW was used below instead of TCSADRAIN,
           and CMD_UART_SPEED did not work properly then, at least with
           the Bus Pirate v3.5 (USB). */
-       if (0 != tcsetattr(fd, TCSADRAIN, &t_opt)) {
+       if (tcsetattr(fd, TCSADRAIN, &t_opt) != 0) {
                /* According to the Linux documentation, this is actually not enough
                   to detect errors, you need to call tcgetattr() and check that
                   all changes have been performed successfully. */
@@ -1312,7 +1307,7 @@ static int buspirate_swd_switch_seq(enum swd_special_seq seq)
 {
        const uint8_t *sequence;
        int sequence_len;
-       uint8_t tmp[64];
+       uint32_t no_bytes, sequence_offset;
 
        switch (seq) {
        case LINE_RESET:
@@ -1335,15 +1330,24 @@ static int buspirate_swd_switch_seq(enum swd_special_seq seq)
                return ERROR_FAIL;
        }
 
-       /* FIXME: all above sequences fit into one pirate command for now
-        *        but it may cause trouble later
-        */
+       no_bytes = sequence_len;
+       sequence_offset = 0;
+
+       while (no_bytes) {
+               uint8_t tmp[17];
+               uint32_t to_send;
+
+               to_send = no_bytes > 16 ? 16 : no_bytes;
 
-       tmp[0] = 0x10 + ((sequence_len - 1) & 0x0F);
-       memcpy(tmp + 1, sequence, sequence_len);
+               tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
+               memcpy(tmp + 1, &sequence[sequence_offset], to_send);
+
+               buspirate_serial_write(buspirate_fd, tmp, to_send + 1);
+               buspirate_serial_read(buspirate_fd, tmp, to_send + 1);
 
-       buspirate_serial_write(buspirate_fd, tmp, sequence_len + 1);
-       buspirate_serial_read(buspirate_fd, tmp, sequence_len + 1);
+               no_bytes -= to_send;
+               sequence_offset += to_send;
+       }
 
        return ERROR_OK;
 }
@@ -1361,7 +1365,7 @@ static uint8_t buspirate_swd_write_header(uint8_t cmd)
        tmp[5] = 0x07; /* write mode trn_1 */
        tmp[6] = 0x07; /* write mode trn_2 */
 
-       to_send = ((cmd & SWD_CMD_RnW) == 0) ? 7 : 5;
+       to_send = ((cmd & SWD_CMD_RNW) == 0) ? 7 : 5;
        buspirate_serial_write(buspirate_fd, tmp, to_send);
 
        /* read ack */
@@ -1407,7 +1411,7 @@ static void buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_del
        uint8_t tmp[16];
 
        LOG_DEBUG("buspirate_swd_read_reg");
-       assert(cmd & SWD_CMD_RnW);
+       assert(cmd & SWD_CMD_RNW);
 
        if (queued_retval != ERROR_OK) {
                LOG_DEBUG("Skip buspirate_swd_read_reg because queued_retval=%d", queued_retval);
@@ -1437,8 +1441,8 @@ static void buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_del
 
        LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
                        ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
-                       cmd & SWD_CMD_APnDP ? "AP" : "DP",
-                       cmd & SWD_CMD_RnW ? "read" : "write",
+                       cmd & SWD_CMD_APNDP ? "AP" : "DP",
+                       cmd & SWD_CMD_RNW ? "read" : "write",
                        (cmd & SWD_CMD_A32) >> 1,
                        data);
 
@@ -1451,7 +1455,7 @@ static void buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_del
                }
                if (value)
                        *value = data;
-               if (cmd & SWD_CMD_APnDP)
+               if (cmd & SWD_CMD_APNDP)
                        buspirate_swd_idle_clocks(ap_delay_clk);
                return;
         case SWD_ACK_WAIT:
@@ -1474,7 +1478,7 @@ static void buspirate_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_del
        uint8_t tmp[16];
 
        LOG_DEBUG("buspirate_swd_write_reg");
-       assert(!(cmd & SWD_CMD_RnW));
+       assert(!(cmd & SWD_CMD_RNW));
 
        if (queued_retval != ERROR_OK) {
                LOG_DEBUG("Skip buspirate_swd_write_reg because queued_retval=%d", queued_retval);
@@ -1495,14 +1499,14 @@ static void buspirate_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_del
 
        LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
                        ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
-                       cmd & SWD_CMD_APnDP ? "AP" : "DP",
-                       cmd & SWD_CMD_RnW ? "read" : "write",
+                       cmd & SWD_CMD_APNDP ? "AP" : "DP",
+                       cmd & SWD_CMD_RNW ? "read" : "write",
                        (cmd & SWD_CMD_A32) >> 1,
                        value);
 
        switch (ack) {
         case SWD_ACK_OK:
-               if (cmd & SWD_CMD_APnDP)
+               if (cmd & SWD_CMD_APNDP)
                        buspirate_swd_idle_clocks(ap_delay_clk);
                return;
         case SWD_ACK_WAIT:
@@ -1532,5 +1536,3 @@ static int buspirate_swd_run_queue(void)
        LOG_DEBUG("SWD queue return value: %02x", retval);
        return retval;
 }
-
-

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)