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 2020-02-22 21:17:41 +02:00
parent 7f9ca71ed8
commit 46df381a46
6 changed files with 1214 additions and 0 deletions

View File

@ -197,6 +197,7 @@
<ClCompile Include="msvc\getopt.c" />
<ClCompile Include="msvc\gettimeofday.c" />
<ClCompile Include="msvc\unistd.cpp" />
<ClCompile Include="msvc\usb_com_helper.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="arduino.h" />
@ -249,6 +250,8 @@
<ClInclude Include="msvc\msvc_compat.h" />
<ClInclude Include="msvc\sys\time.h" />
<ClInclude Include="msvc\unistd.h" />
<ClInclude Include="msvc\usb_com_helper.h" />
<ClInclude Include="msvc\usb_com_locator.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="external\libelf\libelf.vcxproj">

View File

@ -176,6 +176,9 @@
<ClCompile Include="msvc\unistd.cpp">
<Filter>4 msvc</Filter>
</ClCompile>
<ClCompile Include="msvc\usb_com_helper.cpp">
<Filter>4 msvc</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="arduino.h">
@ -328,5 +331,11 @@
<ClInclude Include="msvc\msvc_compat.h">
<Filter>4 msvc</Filter>
</ClInclude>
<ClInclude Include="msvc\usb_com_helper.h">
<Filter>4 msvc</Filter>
</ClInclude>
<ClInclude Include="msvc\usb_com_locator.h">
<Filter>4 msvc</Filter>
</ClInclude>
</ItemGroup>
</Project>

82
msvc/usb_com_helper.cpp Normal file
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
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
msvc/usb_com_locator.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -40,6 +40,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
@ -238,6 +242,13 @@ static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
#endif
}
#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