diff --git a/src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.cpp b/src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.cpp index 24ca00ff..e57e97d8 100644 --- a/src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.cpp +++ b/src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.cpp @@ -1,5 +1,8 @@ #include "MplabPickit4.hpp" +#include "src/TargetController/Exceptions/DeviceNotFound.hpp" +#include "src/Services/PathService.hpp" + namespace Bloom::DebugToolDrivers { MplabPickit4::MplabPickit4() @@ -9,4 +12,31 @@ namespace Bloom::DebugToolDrivers MplabPickit4::CMSIS_HID_INTERFACE_NUMBER ) {} + + void MplabPickit4::init() { + using Exceptions::DeviceNotFound; + + try { + EdbgDevice::init(); + + } catch (const DeviceNotFound& exception) { + /* + * The MPLAB PICkit 4 could be connected but not in AVR mode - if this is the case, inform the user and + * direct them to the AVR mode article. + */ + const auto nonEdbgDevices = this->findMatchingDevices( + MplabPickit4::NON_EDBG_USB_VENDOR_ID, + MplabPickit4::NON_EDBG_USB_PRODUCT_ID + ); + + if (!nonEdbgDevices.empty()) { + throw DeviceNotFound( + "The connected MPLAB PICkit 4 device is not in \"AVR mode\". Please follow the instructions at " + + Services::PathService::homeDomainName() + "/docs/avr-mode" + ); + } + + throw exception; + } + } } diff --git a/src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.hpp b/src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.hpp index 63ca1f8a..a8ca45f4 100644 --- a/src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.hpp +++ b/src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.hpp @@ -26,10 +26,15 @@ namespace Bloom::DebugToolDrivers static const inline std::uint16_t USB_PRODUCT_ID = 0x2177; static const inline std::uint8_t CMSIS_HID_INTERFACE_NUMBER = 0; + static const inline std::uint16_t NON_EDBG_USB_VENDOR_ID = 0x04d8; + static const inline std::uint16_t NON_EDBG_USB_PRODUCT_ID = 0x9012; + MplabPickit4(); std::string getName() override { return "MPLAB PICkit 4"; } + + void init() override; }; } diff --git a/src/DebugToolDrivers/Microchip/MplabSnap/MplabSnap.cpp b/src/DebugToolDrivers/Microchip/MplabSnap/MplabSnap.cpp index b513c847..3a0e747f 100644 --- a/src/DebugToolDrivers/Microchip/MplabSnap/MplabSnap.cpp +++ b/src/DebugToolDrivers/Microchip/MplabSnap/MplabSnap.cpp @@ -1,5 +1,8 @@ #include "MplabSnap.hpp" +#include "src/TargetController/Exceptions/DeviceNotFound.hpp" +#include "src/Services/PathService.hpp" + namespace Bloom::DebugToolDrivers { MplabSnap::MplabSnap() @@ -9,4 +12,39 @@ namespace Bloom::DebugToolDrivers MplabSnap::CMSIS_HID_INTERFACE_NUMBER ) {} + + void MplabSnap::init() { + using Exceptions::DeviceNotFound; + + try { + EdbgDevice::init(); + + } catch (const DeviceNotFound& exception) { + /* + * The MPLAB Snap could be connected but not in AVR mode - if this is the case, inform the user and direct + * them to the AVR mode article. + */ + auto nonEdbgDevices = this->findMatchingDevices( + MplabSnap::NON_EDBG_USB_VENDOR_ID, + MplabSnap::NON_EDBG_USB_PRODUCT_ID + ); + + if (nonEdbgDevices.empty()) { + // The MPLAB Snap sometimes uses another product ID when not in AVR mode. + nonEdbgDevices = this->findMatchingDevices( + MplabSnap::NON_EDBG_USB_VENDOR_ID, + MplabSnap::NON_EDBG_USB_PRODUCT_ID_ALTERNATIVE + ); + } + + if (!nonEdbgDevices.empty()) { + throw DeviceNotFound( + "The connected MPLAB Snap device is not in \"AVR mode\". Please follow the instructions at " + + Services::PathService::homeDomainName() + "/docs/avr-mode" + ); + } + + throw exception; + } + } } diff --git a/src/DebugToolDrivers/Microchip/MplabSnap/MplabSnap.hpp b/src/DebugToolDrivers/Microchip/MplabSnap/MplabSnap.hpp index 4518cdc0..e541829c 100644 --- a/src/DebugToolDrivers/Microchip/MplabSnap/MplabSnap.hpp +++ b/src/DebugToolDrivers/Microchip/MplabSnap/MplabSnap.hpp @@ -28,10 +28,16 @@ namespace Bloom::DebugToolDrivers static const inline std::uint16_t USB_PRODUCT_ID = 0x2180; static const inline std::uint8_t CMSIS_HID_INTERFACE_NUMBER = 0; + static const inline std::uint16_t NON_EDBG_USB_VENDOR_ID = 0x04d8; + static const inline std::uint16_t NON_EDBG_USB_PRODUCT_ID = 0x9018; + static const inline std::uint16_t NON_EDBG_USB_PRODUCT_ID_ALTERNATIVE = 0x9017; + MplabSnap(); std::string getName() override { return "MPLAB Snap"; } + + void init() override; }; } diff --git a/src/DebugToolDrivers/USB/UsbDevice.cpp b/src/DebugToolDrivers/USB/UsbDevice.cpp index 26f2c4c6..7ab0419b 100644 --- a/src/DebugToolDrivers/USB/UsbDevice.cpp +++ b/src/DebugToolDrivers/USB/UsbDevice.cpp @@ -4,6 +4,7 @@ #include "src/Logger/Logger.hpp" #include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp" +#include "src/TargetController/Exceptions/DeviceNotFound.hpp" namespace Bloom::Usb { @@ -25,7 +26,7 @@ namespace Bloom::Usb auto devices = this->findMatchingDevices(this->vendorId, this->productId); if (devices.empty()) { - throw DeviceInitializationFailure( + throw DeviceNotFound( "Failed to find USB device with matching vendor and product ID. Please examine the debug tool's USB " "connection, as well as the selected environment's debug tool configuration, in bloom.yaml" ); diff --git a/src/TargetController/Exceptions/DeviceNotFound.hpp b/src/TargetController/Exceptions/DeviceNotFound.hpp new file mode 100644 index 00000000..90aaf0d1 --- /dev/null +++ b/src/TargetController/Exceptions/DeviceNotFound.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "src/Exceptions/Exception.hpp" + +namespace Bloom::Exceptions +{ + class DeviceNotFound: public Exception + { + public: + explicit DeviceNotFound(const std::string& message): Exception(message) { + this->message = message; + } + + explicit DeviceNotFound(const char* message): Exception(message) { + this->message = std::string(message); + } + }; +} diff --git a/src/TargetController/TargetControllerComponent.cpp b/src/TargetController/TargetControllerComponent.cpp index 4a1b24c6..305e34fb 100644 --- a/src/TargetController/TargetControllerComponent.cpp +++ b/src/TargetController/TargetControllerComponent.cpp @@ -9,9 +9,6 @@ #include "src/Services/ProcessService.hpp" #include "src/Logger/Logger.hpp" -#include "src/TargetController/Exceptions/DeviceFailure.hpp" -#include "src/TargetController/Exceptions/TargetOperationFailure.hpp" -#include "src/Exceptions/TargetControllerStartupFailure.hpp" #include "src/Exceptions/InvalidConfig.hpp" namespace Bloom::TargetController @@ -97,14 +94,6 @@ namespace Bloom::TargetController } } - } catch (const TargetControllerStartupFailure& exception) { - Logger::error("TargetController failed to start up. See below for errors:"); - Logger::error(exception.getMessage()); - - } catch (const Exception& exception) { - Logger::error("The TargetController encountered a fatal error. See below for errors:"); - Logger::error(exception.getMessage()); - } catch (const std::exception& exception) { Logger::error("The TargetController encountered a fatal error. See below for errors:"); Logger::error(std::string(exception.what()));