|
33 | 33 | @brief Implementation of GlasgowSWDInterface
|
34 | 34 | */
|
35 | 35 |
|
| 36 | +#include <functional> |
36 | 37 | #include "jtaghal.h"
|
37 | 38 |
|
38 | 39 | #if HAVE_LIBUSB
|
|
42 | 43 |
|
43 | 44 | using namespace std;
|
44 | 45 |
|
| 46 | +static void ForEachGlasgowDevice(std::function<bool(libusb_device_descriptor *, libusb_device_handle *)> fn) |
| 47 | +{ |
| 48 | + int ret; |
| 49 | + |
| 50 | + libusb_context *context; |
| 51 | + if((ret = libusb_init(&context)) != 0) { |
| 52 | + throw JtagExceptionWrapper( |
| 53 | + "libusb_init failed", |
| 54 | + libusb_error_name(ret)); |
| 55 | + } |
| 56 | + |
| 57 | + ssize_t device_count; |
| 58 | + libusb_device **device_list; |
| 59 | + if((device_count = libusb_get_device_list(context, &device_list)) < 0) { |
| 60 | + libusb_exit(context); |
| 61 | + |
| 62 | + throw JtagExceptionWrapper( |
| 63 | + "libusb_get_device_list failed", |
| 64 | + libusb_error_name(device_count)); |
| 65 | + } |
| 66 | + |
| 67 | + for(int i = 0; i < device_count; i++) { |
| 68 | + libusb_device *device = device_list[i]; |
| 69 | + libusb_device_descriptor device_desc; |
| 70 | + if((ret = libusb_get_device_descriptor(device, &device_desc)) != 0) { |
| 71 | + LogError("Cannot get USB device descriptor for device %03d:%03d\n", |
| 72 | + libusb_get_bus_number(device), |
| 73 | + libusb_get_port_number(device)); |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if(device_desc.idVendor != VID_QIHW || device_desc.idProduct != PID_GLASGOW) |
| 78 | + continue; |
| 79 | + |
| 80 | + libusb_device_handle *device_handle; |
| 81 | + if((ret = libusb_open(device, &device_handle)) != 0) { |
| 82 | + LogError("Cannot open Glasgow device %03d:%03d\n", |
| 83 | + libusb_get_bus_number(device), |
| 84 | + libusb_get_port_number(device)); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + bool done = fn(&device_desc, device_handle); |
| 89 | + |
| 90 | + libusb_close(device_handle); |
| 91 | + |
| 92 | + if(done) break; |
| 93 | + } |
| 94 | + |
| 95 | + libusb_free_device_list(device_list, /*unref_devices=*/true); |
| 96 | + |
| 97 | + libusb_exit(context); |
| 98 | +} |
| 99 | + |
| 100 | +int GlasgowSWDInterface::GetInterfaceCount() |
| 101 | +{ |
| 102 | + int count = 0; |
| 103 | + |
| 104 | + ForEachGlasgowDevice([&](libusb_device_descriptor *, libusb_device_handle *) { |
| 105 | + count++; |
| 106 | + return false; |
| 107 | + }); |
| 108 | + |
| 109 | + return count; |
| 110 | +} |
| 111 | + |
| 112 | +string GlasgowSWDInterface::GetAPIVersion() |
| 113 | +{ |
| 114 | + return ""; |
| 115 | +} |
| 116 | + |
| 117 | +string GlasgowSWDInterface::GetSerialNumber(int index) |
| 118 | +{ |
| 119 | + int ret; |
| 120 | + int curr_index = 0; |
| 121 | + char device_serial[64] = {}; |
| 122 | + |
| 123 | + ForEachGlasgowDevice([&](libusb_device_descriptor *desc, libusb_device_handle *handle) { |
| 124 | + if(curr_index == index) { |
| 125 | + if((ret = libusb_get_string_descriptor_ascii(handle, desc->iSerialNumber, |
| 126 | + (uint8_t *)device_serial, sizeof(device_serial))) != 0) { |
| 127 | + strncpy(device_serial, "(error)", sizeof(device_serial)); |
| 128 | + } |
| 129 | + |
| 130 | + return true; |
| 131 | + } else { |
| 132 | + index++; |
| 133 | + return false; |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + return device_serial; |
| 138 | +} |
| 139 | + |
| 140 | +string GlasgowSWDInterface::GetDescription(int index) |
| 141 | +{ |
| 142 | + int curr_index = 0; |
| 143 | + uint16_t revision = 0; |
| 144 | + |
| 145 | + ForEachGlasgowDevice([&](libusb_device_descriptor *desc, libusb_device_handle *) { |
| 146 | + if(curr_index == index) { |
| 147 | + revision = desc->bcdDevice; |
| 148 | + |
| 149 | + return true; |
| 150 | + } else { |
| 151 | + index++; |
| 152 | + return false; |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + string description = "Glasgow rev"; |
| 157 | + description += 'A' + (revision & 0xff) - 1; |
| 158 | + if((revision >> 8) == 0xa0 || (revision >> 8) == 0x00) { |
| 159 | + description += " (no firmware)"; |
| 160 | + } |
| 161 | + return description; |
| 162 | +} |
| 163 | + |
45 | 164 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
46 | 165 | // Construction / destruction
|
47 | 166 |
|
|
0 commit comments