ULINK driver: Implement variable TCK frequency in OpenULINK firmware
[openocd.git] / src / jtag / drivers / buspirate.c
index 85b76edfafad34e2b4c0d5f550115756d5504193..a360d2304287bbff5fdf7786fbbe061c2bba3f1c 100644 (file)
@@ -45,7 +45,7 @@ static void buspirate_scan(bool ir_scan, enum scan_type type,
        uint8_t *buffer, int scan_size, struct scan_command *command);
 
 
-#define CMD_UNKOWN        0x00
+#define CMD_UNKNOWN       0x00
 #define CMD_PORT_MODE     0x01
 #define CMD_FEATURE       0x02
 #define CMD_READ_ADCS     0x03
@@ -55,6 +55,11 @@ static void buspirate_scan(bool ir_scan, enum scan_type type,
 #define CMD_UART_SPEED    0x07
 #define CMD_JTAG_SPEED    0x08
 
+/* Not all OSes have this speed defined */
+#if !defined(B1000000)
+#define  B1000000 0010010
+#endif
+
 enum {
        MODE_HIZ = 0,
        MODE_JTAG = 1,          /* push-pull outputs */
@@ -108,7 +113,8 @@ static void buspirate_jtag_set_feature(int, char, char);
 static void buspirate_jtag_get_adcs(int);
 
 /* low level HW communication interface */
-static int buspirate_serial_setspeed(int fd, speed_t speed);
+static int buspirate_serial_open(char *port);
+static int buspirate_serial_setspeed(int fd, char speed);
 static int buspirate_serial_write(int fd, char *buf, int size);
 static int buspirate_serial_read(int fd, char *buf, int size);
 static void buspirate_serial_close(int fd);
@@ -215,13 +221,13 @@ static int buspirate_init(void)
                return ERROR_JTAG_INIT_FAILED;
        }
 
-       buspirate_fd = open(buspirate_port, O_RDWR | O_NOCTTY);
+       buspirate_fd = buspirate_serial_open(buspirate_port);
        if (buspirate_fd == -1) {
                LOG_ERROR("Could not open serial port.");
                return ERROR_JTAG_INIT_FAILED;
        }
 
-       buspirate_serial_setspeed(buspirate_fd, B115200);
+       buspirate_serial_setspeed(buspirate_fd, SERIAL_NORMAL);
 
        buspirate_jtag_enable(buspirate_fd);
 
@@ -261,11 +267,6 @@ static int buspirate_quit(void)
 /* openocd command interface */
 COMMAND_HANDLER(buspirate_handle_adc_command)
 {
-       if (CMD_ARGC != 0) {
-               LOG_ERROR("usage: buspirate_adc");
-               return ERROR_OK;
-       }
-
        if (buspirate_fd == -1)
                return ERROR_OK;
 
@@ -278,15 +279,17 @@ COMMAND_HANDLER(buspirate_handle_adc_command)
 
 COMMAND_HANDLER(buspirate_handle_vreg_command)
 {
-       if (CMD_ARGC != 1) {
+       if (CMD_ARGC < 1) {
                LOG_ERROR("usage: buspirate_vreg <1|0>");
                return ERROR_OK;
        }
 
        if (atoi(CMD_ARGV[0]) == 1)
                buspirate_vreg = 1;
-       else
+       else if (atoi(CMD_ARGV[0]) == 0)
                buspirate_vreg = 0;
+       else
+               LOG_ERROR("usage: buspirate_vreg <1|0>");
 
        return ERROR_OK;
 
@@ -294,15 +297,17 @@ COMMAND_HANDLER(buspirate_handle_vreg_command)
 
 COMMAND_HANDLER(buspirate_handle_pullup_command)
 {
-       if (CMD_ARGC != 1) {
+       if (CMD_ARGC < 1) {
                LOG_ERROR("usage: buspirate_pullup <1|0>");
                return ERROR_OK;
        }
 
        if (atoi(CMD_ARGV[0]) == 1)
                buspirate_pullup = 1;
-       else
+       else if (atoi(CMD_ARGV[0]) == 0)
                buspirate_pullup = 0;
+       else
+               LOG_ERROR("usage: buspirate_pullup <1|0>");
 
        return ERROR_OK;
 
@@ -310,7 +315,7 @@ COMMAND_HANDLER(buspirate_handle_pullup_command)
 
 COMMAND_HANDLER(buspirate_handle_led_command)
 {
-       if (CMD_ARGC != 1) {
+       if (CMD_ARGC < 1) {
                LOG_ERROR("usage: buspirate_led <1|0>");
                return ERROR_OK;
        }
@@ -319,10 +324,12 @@ COMMAND_HANDLER(buspirate_handle_led_command)
                /* enable led */
                buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
                                ACTION_ENABLE);
-       } else {
+       } else if (atoi(CMD_ARGV[0]) == 0) {
                /* disable led */
                buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
                                ACTION_DISABLE);
+       } else {
+               LOG_ERROR("usage: buspirate_led <1|0>");
        }
 
        return ERROR_OK;
@@ -331,7 +338,7 @@ COMMAND_HANDLER(buspirate_handle_led_command)
 
 COMMAND_HANDLER(buspirate_handle_mode_command)
 {
-       if (CMD_ARGC != 1) {
+       if (CMD_ARGC < 1) {
                LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
                return ERROR_OK;
        }
@@ -349,7 +356,7 @@ COMMAND_HANDLER(buspirate_handle_mode_command)
 
 COMMAND_HANDLER(buspirate_handle_speed_command)
 {
-       if (CMD_ARGC != 1) {
+       if (CMD_ARGC < 1) {
                LOG_ERROR("usage: buspirate_speed <normal|fast>");
                return ERROR_OK;
        }
@@ -367,12 +374,12 @@ COMMAND_HANDLER(buspirate_handle_speed_command)
 
 COMMAND_HANDLER(buspirate_handle_port_command)
 {
-       if (CMD_ARGC != 1) {
+       if (CMD_ARGC < 1) {
                LOG_ERROR("usage: buspirate_port /dev/ttyUSB0");
                return ERROR_OK;
        }
 
-       if (buspirate_port == 0)
+       if (buspirate_port == NULL)
                buspirate_port = strdup(CMD_ARGV[0]);
 
        return ERROR_OK;
@@ -717,16 +724,15 @@ static void buspirate_jtag_enable(int fd)
        while (!done) {
                ret = buspirate_serial_read(fd, tmp, 4);
                if (ret != 4) {
-                       LOG_ERROR("Buspirate did not respond :"
-                               "( restart everything");
+                       LOG_ERROR("Buspirate error. Is is binary/"
+                               "/OpenOCD support enabled?");
                        exit(-1);
                }
-               LOG_DEBUG("TUI");
                if (strncmp(tmp, "BBIO", 4) == 0) {
                        ret = buspirate_serial_read(fd, tmp, 1);
                        if (ret != 1) {
-                               LOG_ERROR("Buspirate did not respond well :"
-                                       "( restart everything");
+                               LOG_ERROR("Buspirate did not correctly! "
+                                       "Do you have correct firmware?");
                                exit(-1);
                        }
                        if (tmp[0] != '1') {
@@ -741,8 +747,8 @@ static void buspirate_jtag_enable(int fd)
                } else if (strncmp(tmp, "OCD1", 4) == 0)
                        done = 1;
                else {
-                       LOG_ERROR("Buspirate did not respond :"
-                               "( restart everything");
+                       LOG_ERROR("Buspirate did not correctly! "
+                               "Do you have correct firmware?");
                        exit(-1);
                }
        }
@@ -762,7 +768,7 @@ static void buspirate_jtag_reset(int fd)
                tmp[0] = 0x0F; /*  reset BP */
                buspirate_serial_write(fd, tmp, 1);
        } else
-               LOG_ERROR("Bad reply :( Please restart manually");
+               LOG_ERROR("Unable to restart buspirate!");
 }
 
 static void buspirate_jtag_set_speed(int fd, char speed)
@@ -770,7 +776,6 @@ static void buspirate_jtag_set_speed(int fd, char speed)
        int ret;
        char tmp[2];
        char ack[2];
-       speed_t baudrate = B115200;
 
        ack[0] = 0xAA;
        ack[1] = 0x55;
@@ -780,21 +785,16 @@ static void buspirate_jtag_set_speed(int fd, char speed)
        buspirate_jtag_command(fd, tmp, 2);
 
        /* here the adapter changes speed, we need follow */
-       if (speed == SERIAL_FAST)
-               baudrate = B1000000;
-
-       buspirate_serial_setspeed(fd, baudrate);
+       buspirate_serial_setspeed(fd, speed);
 
        buspirate_serial_write(fd, ack, 2);
        ret = buspirate_serial_read(fd, tmp, 2);
        if (ret != 2) {
-               LOG_ERROR("Buspirate did not respond :"
-                       "( restart everything");
+               LOG_ERROR("Buspirate did not ack speed change");
                exit(-1);
        }
        if ((tmp[0] != CMD_UART_SPEED) || (tmp[1] != speed)) {
-               LOG_ERROR("Buspirate didn't reply as expected :"
-                       "( restart everything");
+               LOG_ERROR("Buspirate didn't reply as expected");
                exit(-1);
        }
        LOG_INFO("Buspirate switched to %s mode",
@@ -873,15 +873,23 @@ static unsigned char buspirate_jtag_command(int fd,
 
 /* low level serial port */
 /* TODO add support for WIN32 and others ! */
-static int buspirate_serial_setspeed(int fd, speed_t speed)
+static int buspirate_serial_open(char *port)
+{
+       int fd;
+       fd = open(buspirate_port, O_RDWR | O_NOCTTY | O_NDELAY);
+       return fd;
+}
+
+static int buspirate_serial_setspeed(int fd, char speed)
 {
        struct termios t_opt;
+       speed_t baud = (speed == SERIAL_FAST) ? B1000000 : B115200;
 
        /* set the serial port parameters */
        fcntl(fd, F_SETFL, 0);
        tcgetattr(fd, &t_opt);
-       cfsetispeed(&t_opt, speed);
-       cfsetospeed(&t_opt, speed);
+       cfsetispeed(&t_opt, baud);
+       cfsetospeed(&t_opt, baud);
        t_opt.c_cflag |= (CLOCAL | CREAD);
        t_opt.c_cflag &= ~PARENB;
        t_opt.c_cflag &= ~CSTOPB;
@@ -940,7 +948,7 @@ static int buspirate_serial_read(int fd, char *buf, int size)
        buspirate_print_buffer(buf, len);
 
        if (len != size)
-               LOG_ERROR("Error sending data");
+               LOG_ERROR("Error reading data");
 
        return len;
 }

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)