Add SWD protocol support to sysfsgpio
[openocd.git] / src / jtag / drivers / cmsis_dap_usb.c
index 4073d215aedd8ffedff5f06cf8d233dc213ee794..d607b7f59ec3cff9f7955be6a717292857574849 100644 (file)
@@ -60,6 +60,7 @@
 /* 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 wchar_t *cmsis_dap_serial;
 static bool swd_mode;
 
 #define PACKET_SIZE       (64 + 1)     /* 64 bytes plus report id */
@@ -137,7 +138,7 @@ static bool swd_mode;
 /* CMSIS-DAP Vendor Commands
  * None as yet... */
 
-static char *info_caps_str[] = {
+static const char * const info_caps_str[] = {
        "SWD  Supported",
        "JTAG Supported"
 };
@@ -162,15 +163,19 @@ static int cmsis_dap_usb_open(void)
        int i;
        struct hid_device_info *devs, *cur_dev;
        unsigned short target_vid, target_pid;
+       wchar_t *target_serial = NULL;
+
+       bool found = false;
+       bool serial_found = false;
 
        target_vid = 0;
        target_pid = 0;
 
        /*
-       The CMSIS-DAP specification stipulates:
-       "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
-       debuggers to idenify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
-       */
+        * The CMSIS-DAP specification stipulates:
+        * "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
+        * debuggers to identify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
+        */
        devs = hid_enumerate(0x0, 0x0);
        cur_dev = devs;
        while (NULL != cur_dev) {
@@ -179,22 +184,33 @@ static int cmsis_dap_usb_open(void)
                                LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
                                          cur_dev->vendor_id, cur_dev->product_id);
                        } else {
-                               if (wcsstr(cur_dev->product_string, L"CMSIS-DAP"))
-                                       /*
-                                       if the user hasn't specified VID:PID *and*
-                                       product string contains "CMSIS-DAP", pick it
-                                       */
-                                       break;
+                               if (wcsstr(cur_dev->product_string, L"CMSIS-DAP")) {
+                                       /* if the user hasn't specified VID:PID *and*
+                                        * product string contains "CMSIS-DAP", pick it
+                                        */
+                                       found = true;
+                               }
                        }
                } else {
-                       /*
-                       otherwise, exhaustively compare against all VID:PID in list
-                       */
+                       /* otherwise, exhaustively compare against all VID:PID in list */
                        for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
                                if ((cmsis_dap_vid[i] == cur_dev->vendor_id) && (cmsis_dap_pid[i] == cur_dev->product_id))
-                                       break;
+                                       found = true;
                        }
+
                        if (cmsis_dap_vid[i] || cmsis_dap_pid[i])
+                               found = true;
+               }
+
+               if (found) {
+                       /* we have found an adapter, so exit further checks */
+                       /* check serial number matches if given */
+                       if (cmsis_dap_serial != NULL) {
+                               if (wcscmp(cmsis_dap_serial, cur_dev->serial_number) == 0) {
+                                       serial_found = true;
+                                       break;
+                               }
+                       } else
                                break;
                }
 
@@ -204,16 +220,23 @@ static int cmsis_dap_usb_open(void)
        if (NULL != cur_dev) {
                target_vid = cur_dev->vendor_id;
                target_pid = cur_dev->product_id;
+               if (serial_found)
+                       target_serial = cmsis_dap_serial;
        }
 
        hid_free_enumeration(devs);
 
+       if (target_vid == 0 && target_pid == 0) {
+               LOG_ERROR("unable to find CMSIS-DAP device");
+               return ERROR_FAIL;
+       }
+
        if (hid_init() != 0) {
                LOG_ERROR("unable to open HIDAPI");
                return ERROR_FAIL;
        }
 
-       dev = hid_open(target_vid, target_pid, NULL);
+       dev = hid_open(target_vid, target_pid, target_serial);
 
        if (dev == NULL) {
                LOG_ERROR("unable to open CMSIS-DAP device");
@@ -267,6 +290,11 @@ static void cmsis_dap_usb_close(struct cmsis_dap *dap)
                cmsis_dap_handle = NULL;
        }
 
+       if (cmsis_dap_serial) {
+               free(cmsis_dap_serial);
+               cmsis_dap_serial = NULL;
+       }
+
        return;
 }
 
@@ -995,6 +1023,27 @@ COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
        return ERROR_OK;
 }
 
+COMMAND_HANDLER(cmsis_dap_handle_serial_command)
+{
+       if (CMD_ARGC == 1) {
+               size_t len = mbstowcs(NULL, CMD_ARGV[0], 0);
+               cmsis_dap_serial = calloc(len + 1, sizeof(wchar_t));
+               if (cmsis_dap_serial == NULL) {
+                       LOG_ERROR("unable to allocate memory");
+                       return ERROR_OK;
+               }
+               if (mbstowcs(cmsis_dap_serial, CMD_ARGV[0], len + 1) == (size_t)-1) {
+                       free(cmsis_dap_serial);
+                       cmsis_dap_serial = NULL;
+                       LOG_ERROR("unable to convert serial");
+               }
+       } else {
+               LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
+       }
+
+       return ERROR_OK;
+}
+
 static const struct command_registration cmsis_dap_subcommand_handlers[] = {
        {
                .name = "info",
@@ -1021,6 +1070,13 @@ 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",
+       },
        COMMAND_REGISTRATION_DONE
 };
 

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)