Moved most of the EDBG device code to an abstract base class, to reduce code duplication
This commit is contained in:
@@ -1,99 +1,12 @@
|
||||
#include "MplabPickit4.hpp"
|
||||
|
||||
#include "src/TargetController/Exceptions/DeviceFailure.hpp"
|
||||
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
|
||||
|
||||
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)
|
||||
: EdbgDevice(
|
||||
MplabPickit4::USB_VENDOR_ID,
|
||||
MplabPickit4::USB_PRODUCT_ID,
|
||||
MplabPickit4::CMSIS_HID_INTERFACE_NUMBER
|
||||
)
|
||||
{}
|
||||
|
||||
void MplabPickit4::init() {
|
||||
UsbDevice::init();
|
||||
|
||||
// TODO: Move away from hard-coding the CMSIS-DAP/EDBG interface number
|
||||
auto usbHidInterface = Usb::HidInterface(0, this->vendorId, this->productId);
|
||||
|
||||
this->detachKernelDriverFromInterface(usbHidInterface.interfaceNumber);
|
||||
usbHidInterface.init();
|
||||
|
||||
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.get());
|
||||
this->edbgAvrIspInterface = std::make_unique<EdbgAvrIspInterface>(this->edbgInterface.get());
|
||||
|
||||
this->setInitialised(true);
|
||||
}
|
||||
|
||||
void MplabPickit4::close() {
|
||||
if (this->sessionStarted) {
|
||||
this->endSession();
|
||||
}
|
||||
|
||||
this->edbgInterface->getUsbHidInterface().close();
|
||||
UsbDevice::close();
|
||||
}
|
||||
|
||||
std::string MplabPickit4::getSerialNumber() {
|
||||
using namespace CommandFrames::Discovery;
|
||||
using ResponseFrames::Discovery::ResponseId;
|
||||
|
||||
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
Query(QueryContext::SERIAL_NUMBER)
|
||||
);
|
||||
|
||||
if (responseFrame.id != ResponseId::OK) {
|
||||
throw DeviceInitializationFailure(
|
||||
"Failed to fetch serial number from device - invalid Discovery Protocol response ID."
|
||||
);
|
||||
}
|
||||
|
||||
const auto data = responseFrame.getPayloadData();
|
||||
return std::string(data.begin(), data.end());
|
||||
}
|
||||
|
||||
void MplabPickit4::startSession() {
|
||||
using namespace CommandFrames::HouseKeeping;
|
||||
using ResponseFrames::HouseKeeping::ResponseId;
|
||||
|
||||
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
StartSession()
|
||||
);
|
||||
|
||||
if (responseFrame.id == ResponseId::FAILED) {
|
||||
// Failed response returned!
|
||||
throw DeviceInitializationFailure("Failed to start session with MPLAB PICkit 4!");
|
||||
}
|
||||
|
||||
this->sessionStarted = true;
|
||||
}
|
||||
|
||||
void MplabPickit4::endSession() {
|
||||
using namespace CommandFrames::HouseKeeping;
|
||||
using ResponseFrames::HouseKeeping::ResponseId;
|
||||
|
||||
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
EndSession()
|
||||
);
|
||||
|
||||
if (responseFrame.id == ResponseId::FAILED) {
|
||||
// Failed response returned!
|
||||
throw DeviceFailure("Failed to end session with MPLAB PICkit 4!");
|
||||
}
|
||||
|
||||
this->sessionStarted = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user