Add support for COM port discovery via USB VID/PID Add support for Leonardo USB bootloader auto-reset

This commit is contained in:
Marius Greuel 2021-12-28 12:19:35 +01:00
parent fd040bc1f0
commit f0aaa7bdeb
5 changed files with 1203 additions and 0 deletions

View File

@ -317,6 +317,7 @@ if(MSVC)
"${PROJECT_SOURCE_DIR}/msvc/getopt.c"
"${PROJECT_SOURCE_DIR}/msvc/gettimeofday.c"
"${PROJECT_SOURCE_DIR}/msvc/usleep.cpp"
"${PROJECT_SOURCE_DIR}/msvc/usb_com_helper.cpp"
)
set(EXTRA_WINDOWS_INCLUDES ${EXTRA_WINDOWS_INCLUDES}
"${PROJECT_SOURCE_DIR}/msvc"

View File

@ -0,0 +1,82 @@
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2019 Marius Greuel
*
* 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/>.
*/
extern "C"
{
#include "avrdude.h"
}
#include "usb_com_helper.h"
#include "usb_com_locator.h"
class AvrdudeConsole : public UsbComLocator::IConsole
{
public:
void WriteLine(UsbComLocator::MessageLevel level, const std::string& message) override
{
switch (level)
{
case UsbComLocator::MessageLevel::Verbose:
avrdude_message(MSG_NOTICE, "%s: %s\n", progname, message.c_str());
break;
case UsbComLocator::MessageLevel::Debug:
avrdude_message(MSG_DEBUG, "%s: %s\n", progname, message.c_str());
break;
default:
avrdude_message(MSG_INFO, "%s: %s\n", progname, message.c_str());
break;
}
}
};
int win32_find_usb_com_port(const char* deviceId, char** port, bool wait_for_device, bool find_related_devices, bool auto_reset)
{
try
{
UsbComLocator::Locator::Options options;
options.WaitForDevice = wait_for_device;
options.FindRelatedDevices = find_related_devices;
options.AutoReset = auto_reset;
AvrdudeConsole console;
UsbComLocator::Locator locator(&console);
std::string filename = locator.FindPortForDevice(deviceId, &options);
if (filename.empty())
{
*port = nullptr;
return -1;
}
filename = "\\\\.\\" + filename;
size_t bufferSize = filename.size() + 1;
*port = static_cast<char*>(malloc(bufferSize));
if (*port == nullptr)
{
return -1;
}
strncpy(*port, filename.c_str(), bufferSize);
return 0;
}
catch (const std::exception&)
{
*port = nullptr;
return -1;
}
}

35
src/msvc/usb_com_helper.h Normal file
View File

@ -0,0 +1,35 @@
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2019 Marius Greuel
*
* 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/>.
*/
#ifndef USB_COM_HELPER_H
#define USB_COM_HELPER_H
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
int win32_find_usb_com_port(const char* deviceId, char** port, bool wait_for_device, bool find_related_devices, bool auto_reset);
#ifdef __cplusplus
}
#endif
#endif // USB_COM_HELPER_H

1074
src/msvc/usb_com_locator.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -36,6 +36,10 @@
#include "avrdude.h"
#include "libavrdude.h"
#ifdef _MSC_VER
#include "msvc/usb_com_helper.h"
#endif
long serial_recv_timeout = 5000; /* ms */
#define W32SERBUFSIZE 1024
@ -257,6 +261,13 @@ static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
return net_open(port + strlen("net:"), fdp);
}
#ifdef _MSC_VER
if (win32_find_usb_com_port(port, &newname, true, true, true) >= 0)
{
port = newname;
}
#endif
if (strncasecmp(port, "com", strlen("com")) == 0) {
// prepend "\\\\.\\" to name, required for port # >= 10