Tidying WCH-LinkE IAP exit code

This commit is contained in:
Nav
2024-12-29 04:41:36 +00:00
parent ab49d10bef
commit a0631c21e6
3 changed files with 18 additions and 28 deletions

View File

@@ -49,7 +49,6 @@ namespace Usb
};
}
// For now, just use the first device found.
this->libusbDevice.swap(devices.front());
::libusb_device_handle* deviceHandle = nullptr;
@@ -120,6 +119,10 @@ namespace Usb
return device;
}
bool UsbDevice::devicePresent(std::uint16_t vendorId, std::uint16_t productId) {
return !UsbDevice::findMatchingDevices(vendorId, productId).empty();
}
bool UsbDevice::waitForDevice(std::uint16_t vendorId, std::uint16_t productId, std::chrono::milliseconds timeout) {
static constexpr auto DELAY = std::chrono::milliseconds{50};
for (auto i = 0; (i * DELAY) <= timeout; ++i){

View File

@@ -69,6 +69,7 @@ namespace Usb
virtual void setConfiguration(std::uint8_t configurationIndex);
static std::optional<UsbDevice> tryDevice(std::uint16_t vendorId, std::uint16_t productId);
static bool devicePresent(std::uint16_t vendorId, std::uint16_t productId);
static bool waitForDevice(std::uint16_t vendorId, std::uint16_t productId, std::chrono::milliseconds timeout);
protected:

View File

@@ -4,7 +4,6 @@
#include "Protocols/WchLink/WchLinkInterface.hpp"
#include "src/TargetController/Exceptions/DeviceNotFound.hpp"
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
#include "src/Logger/Logger.hpp"
@@ -31,37 +30,24 @@ namespace DebugToolDrivers::Wch
{}
void WchLinkBase::init() {
using Exceptions::DeviceNotFound;
try {
UsbDevice::init();
} catch (const DeviceNotFound& exception) {
if (this->toolConfig.exitIapMode && !UsbDevice::devicePresent(this->vendorId, this->productId)) {
auto iapDevice = Usb::UsbDevice::tryDevice(this->iapVendorId, this->iapProductId);
if (!iapDevice.has_value()) {
throw exception;
if (iapDevice.has_value()) {
Logger::warning("Found device in IAP mode - attempting exit operation");
this->exitIapMode(*iapDevice);
Logger::info("Waiting for device to re-enumerate...");
if (!Usb::UsbDevice::waitForDevice(this->vendorId, this->productId, std::chrono::seconds{8})) {
throw DeviceInitializationFailure{"Timeout exceeded whilst waiting for device to re-enumerate"};
}
Logger::info("Re-enumerated device found - IAP exit operation was successful");
}
if (!this->toolConfig.exitIapMode) {
throw DeviceInitializationFailure{
"Device found in IAP mode - Bloom can have the device exit this mode - see the 'exit_iap_mode' "
"tool config parameter, for more"
};
}
Logger::warning("Found device in IAP mode - attempting exit operation");
this->exitIapMode(*iapDevice);
Logger::info("Waiting for device to re-enumerate...");
if (!Usb::UsbDevice::waitForDevice(this->vendorId, this->productId, std::chrono::seconds{8})) {
throw DeviceInitializationFailure{"Timeout exceeded whilst waiting for device to re-enumerate"};
}
Logger::info("Re-enumerated device found - IAP exit operation was successful");
UsbDevice::init();
}
UsbDevice::init();
this->detachKernelDriverFromInterface(this->wchLinkUsbInterfaceNumber);
this->wchLinkUsbInterface = std::make_unique<Usb::UsbInterface>(