Tidying low-level debug tool driver code:
- Use automatic objects for libusb/hidapi resources, where possible (to reduce manual resource management) - Removed unused/redundant code - Tidied HidInterface class - Tidied debug tool initialisation code - Other bits of tidying
This commit is contained in:
@@ -8,30 +8,32 @@ namespace Bloom::DebugToolDrivers
|
||||
using namespace Protocols::CmsisDap::Edbg::Avr;
|
||||
using namespace Bloom::Exceptions;
|
||||
|
||||
using Protocols::CmsisDap::Edbg::EdbgInterface;
|
||||
|
||||
MplabPickit4::MplabPickit4()
|
||||
: UsbDevice(MplabPickit4::USB_VENDOR_ID, MplabPickit4::USB_PRODUCT_ID)
|
||||
{}
|
||||
|
||||
void MplabPickit4::init() {
|
||||
UsbDevice::init();
|
||||
|
||||
// TODO: Move away from hard-coding the CMSIS-DAP/EDBG interface number
|
||||
auto& usbHidInterface = this->getEdbgInterface().getUsbHidInterface();
|
||||
usbHidInterface.setNumber(0);
|
||||
usbHidInterface.setLibUsbDevice(this->libUsbDevice);
|
||||
usbHidInterface.setLibUsbDeviceHandle(this->libUsbDeviceHandle);
|
||||
usbHidInterface.setVendorId(this->vendorId);
|
||||
usbHidInterface.setProductId(this->productId);
|
||||
auto usbHidInterface = Usb::HidInterface(0, this->vendorId, this->productId);
|
||||
|
||||
if (!usbHidInterface.isInitialised()) {
|
||||
usbHidInterface.init();
|
||||
}
|
||||
this->detachKernelDriverFromInterface(usbHidInterface.interfaceNumber);
|
||||
usbHidInterface.init();
|
||||
|
||||
this->getEdbgInterface().setMinimumCommandTimeGap(std::chrono::milliseconds(35));
|
||||
this->edbgInterface = std::make_unique<EdbgInterface>(std::move(usbHidInterface));
|
||||
|
||||
this->edbgInterface->setMinimumCommandTimeGap(std::chrono::milliseconds(35));
|
||||
|
||||
// We don't need to claim the CMSISDAP interface here as the HIDAPI will have already done so.
|
||||
if (!this->sessionStarted) {
|
||||
this->startSession();
|
||||
}
|
||||
|
||||
this->edbgAvr8Interface = std::make_unique<EdbgAvr8Interface>(this->edbgInterface);
|
||||
this->edbgAvrIspInterface = std::make_unique<EdbgAvrIspInterface>(this->edbgInterface);
|
||||
this->edbgAvr8Interface = std::make_unique<EdbgAvr8Interface>(this->edbgInterface.get());
|
||||
this->edbgAvrIspInterface = std::make_unique<EdbgAvrIspInterface>(this->edbgInterface.get());
|
||||
|
||||
this->setInitialised(true);
|
||||
}
|
||||
@@ -41,7 +43,7 @@ namespace Bloom::DebugToolDrivers
|
||||
this->endSession();
|
||||
}
|
||||
|
||||
this->getEdbgInterface().getUsbHidInterface().close();
|
||||
this->edbgInterface->getUsbHidInterface().close();
|
||||
UsbDevice::close();
|
||||
}
|
||||
|
||||
@@ -49,7 +51,7 @@ namespace Bloom::DebugToolDrivers
|
||||
using namespace CommandFrames::Discovery;
|
||||
using ResponseFrames::Discovery::ResponseId;
|
||||
|
||||
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
auto response = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
Query(QueryContext::SERIAL_NUMBER)
|
||||
);
|
||||
|
||||
@@ -59,7 +61,7 @@ namespace Bloom::DebugToolDrivers
|
||||
);
|
||||
}
|
||||
|
||||
auto data = response.getPayloadData();
|
||||
const auto data = response.getPayloadData();
|
||||
return std::string(data.begin(), data.end());
|
||||
}
|
||||
|
||||
@@ -67,7 +69,7 @@ namespace Bloom::DebugToolDrivers
|
||||
using namespace CommandFrames::HouseKeeping;
|
||||
using ResponseFrames::HouseKeeping::ResponseId;
|
||||
|
||||
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
auto response = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
StartSession()
|
||||
);
|
||||
|
||||
@@ -83,7 +85,7 @@ namespace Bloom::DebugToolDrivers
|
||||
using namespace CommandFrames::HouseKeeping;
|
||||
using ResponseFrames::HouseKeeping::ResponseId;
|
||||
|
||||
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
auto response = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
EndSession()
|
||||
);
|
||||
|
||||
|
||||
@@ -33,16 +33,12 @@ namespace Bloom::DebugToolDrivers
|
||||
static const std::uint16_t USB_VENDOR_ID = 1003;
|
||||
static const std::uint16_t USB_PRODUCT_ID = 8567;
|
||||
|
||||
MplabPickit4(): UsbDevice(MplabPickit4::USB_VENDOR_ID, MplabPickit4::USB_PRODUCT_ID) {}
|
||||
MplabPickit4();
|
||||
|
||||
void init() override;
|
||||
|
||||
void close() override;
|
||||
|
||||
Protocols::CmsisDap::Edbg::EdbgInterface& getEdbgInterface() {
|
||||
return this->edbgInterface;
|
||||
}
|
||||
|
||||
TargetInterfaces::Microchip::Avr::Avr8::Avr8DebugInterface* getAvr8DebugInterface() override {
|
||||
return this->edbgAvr8Interface.get();
|
||||
}
|
||||
@@ -53,7 +49,7 @@ namespace Bloom::DebugToolDrivers
|
||||
|
||||
std::string getName() override {
|
||||
return "MPLAB PICkit 4";
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the device serial number via the Discovery Protocol.
|
||||
@@ -73,7 +69,7 @@ namespace Bloom::DebugToolDrivers
|
||||
void endSession();
|
||||
|
||||
private:
|
||||
Protocols::CmsisDap::Edbg::EdbgInterface edbgInterface = Protocols::CmsisDap::Edbg::EdbgInterface();
|
||||
std::unique_ptr<Protocols::CmsisDap::Edbg::EdbgInterface> edbgInterface = nullptr;
|
||||
std::unique_ptr<Protocols::CmsisDap::Edbg::Avr::EdbgAvr8Interface> edbgAvr8Interface = nullptr;
|
||||
std::unique_ptr<Protocols::CmsisDap::Edbg::Avr::EdbgAvrIspInterface> edbgAvrIspInterface = nullptr;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user