hosted/cmsis_dap: Broken up the dap_init function into multiple parts so they're easier to understand

This commit is contained in:
dragonmux 2022-07-11 16:32:16 -04:00 committed by Piotr Esden-Tempski
parent ec5ac64bdc
commit 0d2fb6ab79
1 changed files with 55 additions and 40 deletions

View File

@ -80,25 +80,21 @@ static size_t mbslen(const char *str)
return result; return result;
} }
/* LPC845 Breakout Board Rev. 0 report invalid response with > 65 bytes */ static bool dap_init_hid(const bmp_info_t *const info)
int dap_init(bmp_info_t *info)
{ {
type = (info->in_ep && info->out_ep) ? CMSIS_TYPE_BULK : CMSIS_TYPE_HID;
if (type == CMSIS_TYPE_HID) {
DEBUG_INFO("Using hid transfer\n"); DEBUG_INFO("Using hid transfer\n");
if (hid_init()) if (hid_init())
return -1; return false;
const size_t size = mbslen(info->serial); const size_t size = mbslen(info->serial);
if (size > 64) { if (size > 64) {
PRINT_INFO("Serial number invalid, aborting\n"); PRINT_INFO("Serial number invalid, aborting\n");
return -1; return false;
} }
wchar_t serial[65] = {0}; wchar_t serial[65] = {0};
if (mbstowcs(serial, info->serial, size) != size) { if (mbstowcs(serial, info->serial, size) != size) {
PRINT_INFO("Serial number conversion failed, aborting\n"); PRINT_INFO("Serial number conversion failed, aborting\n");
return -1; return false;
} }
serial[size] = 0; serial[size] = 0;
/* Blacklist devices that do not work with 513 byte report length /* Blacklist devices that do not work with 513 byte report length
@ -111,29 +107,48 @@ int dap_init(bmp_info_t *info)
handle = hid_open(info->vid, info->pid, (serial[0]) ? serial : NULL); handle = hid_open(info->vid, info->pid, (serial[0]) ? serial : NULL);
if (!handle) { if (!handle) {
PRINT_INFO("hid_open failed: %ls\n", hid_error(NULL)); PRINT_INFO("hid_open failed: %ls\n", hid_error(NULL));
return -1; return false;
} }
} else if (type == CMSIS_TYPE_BULK) { return true;
}
static bool dap_init_bulk(const bmp_info_t *const info)
{
DEBUG_INFO("Using bulk transfer\n"); DEBUG_INFO("Using bulk transfer\n");
usb_handle = libusb_open_device_with_vid_pid(info->libusb_ctx, info->vid, info->pid); usb_handle = libusb_open_device_with_vid_pid(info->libusb_ctx, info->vid, info->pid);
if (!usb_handle) { if (!usb_handle) {
DEBUG_WARN("WARN: libusb_open_device_with_vid_pid() failed\n"); DEBUG_WARN("WARN: libusb_open_device_with_vid_pid() failed\n");
return -1; return false;
} }
if (libusb_claim_interface(usb_handle, info->interface_num) < 0) { if (libusb_claim_interface(usb_handle, info->interface_num) < 0) {
DEBUG_WARN("WARN: libusb_claim_interface() failed\n"); DEBUG_WARN("WARN: libusb_claim_interface() failed\n");
return -1; return false;
} }
in_ep = info->in_ep; in_ep = info->in_ep;
out_ep = info->out_ep; out_ep = info->out_ep;
return true;
}
/* LPC845 Breakout Board Rev. 0 report invalid response with > 65 bytes */
int dap_init(bmp_info_t *info)
{
type = (info->in_ep && info->out_ep) ? CMSIS_TYPE_BULK : CMSIS_TYPE_HID;
if (type == CMSIS_TYPE_HID) {
if (!dap_init_hid(info))
return -1;
} else if (type == CMSIS_TYPE_BULK) {
if (!dap_init_bulk(info))
return -1;
} }
dap_disconnect(); dap_disconnect();
size_t size = dap_info(DAP_INFO_FW_VER, buffer, sizeof(buffer)); size_t size = dap_info(DAP_INFO_FW_VER, buffer, sizeof(buffer));
if (size) { if (size) {
DEBUG_INFO("Ver %s, ", buffer); DEBUG_INFO("Ver %s, ", buffer);
int major = -1, minor = -1, sub = -1; int major = -1;
if (sscanf((const char *)buffer, "%d.%d.%d", int minor = -1;
&major, &minor, &sub)) { int sub = -1;
if (sscanf((const char *)buffer, "%d.%d.%d", &major, &minor, &sub)) {
if (sub == -1) { if (sub == -1) {
if (minor >= 10) { if (minor >= 10) {
minor /= 10; minor /= 10;