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()); this->libusbDevice.swap(devices.front());
::libusb_device_handle* deviceHandle = nullptr; ::libusb_device_handle* deviceHandle = nullptr;
@@ -120,6 +119,10 @@ namespace Usb
return device; 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) { bool UsbDevice::waitForDevice(std::uint16_t vendorId, std::uint16_t productId, std::chrono::milliseconds timeout) {
static constexpr auto DELAY = std::chrono::milliseconds{50}; static constexpr auto DELAY = std::chrono::milliseconds{50};
for (auto i = 0; (i * DELAY) <= timeout; ++i){ for (auto i = 0; (i * DELAY) <= timeout; ++i){

View File

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

View File

@@ -4,7 +4,6 @@
#include "Protocols/WchLink/WchLinkInterface.hpp" #include "Protocols/WchLink/WchLinkInterface.hpp"
#include "src/TargetController/Exceptions/DeviceNotFound.hpp"
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp" #include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
#include "src/Logger/Logger.hpp" #include "src/Logger/Logger.hpp"
@@ -31,25 +30,10 @@ namespace DebugToolDrivers::Wch
{} {}
void WchLinkBase::init() { void WchLinkBase::init() {
using Exceptions::DeviceNotFound; if (this->toolConfig.exitIapMode && !UsbDevice::devicePresent(this->vendorId, this->productId)) {
try {
UsbDevice::init();
} catch (const DeviceNotFound& exception) {
auto iapDevice = Usb::UsbDevice::tryDevice(this->iapVendorId, this->iapProductId); auto iapDevice = Usb::UsbDevice::tryDevice(this->iapVendorId, this->iapProductId);
if (!iapDevice.has_value()) { if (iapDevice.has_value()) {
throw exception;
}
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"); Logger::warning("Found device in IAP mode - attempting exit operation");
this->exitIapMode(*iapDevice); this->exitIapMode(*iapDevice);
@@ -59,8 +43,10 @@ namespace DebugToolDrivers::Wch
} }
Logger::info("Re-enumerated device found - IAP exit operation was successful"); Logger::info("Re-enumerated device found - IAP exit operation was successful");
UsbDevice::init();
} }
}
UsbDevice::init();
this->detachKernelDriverFromInterface(this->wchLinkUsbInterfaceNumber); this->detachKernelDriverFromInterface(this->wchLinkUsbInterfaceNumber);