2021-11-28 22:41:41 +00:00
|
|
|
#include "MplabPickit4.hpp"
|
|
|
|
|
|
|
|
|
|
#include "src/TargetController/Exceptions/DeviceFailure.hpp"
|
|
|
|
|
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
namespace Bloom::DebugToolDrivers
|
|
|
|
|
{
|
|
|
|
|
using namespace Protocols::CmsisDap::Edbg::Avr;
|
|
|
|
|
using namespace Bloom::Exceptions;
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
using Protocols::CmsisDap::Edbg::EdbgInterface;
|
|
|
|
|
|
|
|
|
|
MplabPickit4::MplabPickit4()
|
|
|
|
|
: UsbDevice(MplabPickit4::USB_VENDOR_ID, MplabPickit4::USB_PRODUCT_ID)
|
|
|
|
|
{}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void MplabPickit4::init() {
|
|
|
|
|
UsbDevice::init();
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
// TODO: Move away from hard-coding the CMSIS-DAP/EDBG interface number
|
2022-10-01 16:50:57 +01:00
|
|
|
auto usbHidInterface = Usb::HidInterface(0, this->vendorId, this->productId);
|
|
|
|
|
|
|
|
|
|
this->detachKernelDriverFromInterface(usbHidInterface.interfaceNumber);
|
|
|
|
|
usbHidInterface.init();
|
|
|
|
|
|
|
|
|
|
this->edbgInterface = std::make_unique<EdbgInterface>(std::move(usbHidInterface));
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
this->edbgInterface->setMinimumCommandTimeGap(std::chrono::milliseconds(35));
|
2022-02-05 15:32:08 +00:00
|
|
|
|
|
|
|
|
// We don't need to claim the CMSISDAP interface here as the HIDAPI will have already done so.
|
|
|
|
|
if (!this->sessionStarted) {
|
|
|
|
|
this->startSession();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
this->edbgAvr8Interface = std::make_unique<EdbgAvr8Interface>(this->edbgInterface.get());
|
|
|
|
|
this->edbgAvrIspInterface = std::make_unique<EdbgAvrIspInterface>(this->edbgInterface.get());
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->setInitialised(true);
|
2021-11-28 22:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void MplabPickit4::close() {
|
|
|
|
|
if (this->sessionStarted) {
|
|
|
|
|
this->endSession();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
this->edbgInterface->getUsbHidInterface().close();
|
2022-02-05 15:32:08 +00:00
|
|
|
UsbDevice::close();
|
|
|
|
|
}
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
std::string MplabPickit4::getSerialNumber() {
|
|
|
|
|
using namespace CommandFrames::Discovery;
|
2022-03-01 20:18:45 +00:00
|
|
|
using ResponseFrames::Discovery::ResponseId;
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
2022-02-05 15:32:08 +00:00
|
|
|
Query(QueryContext::SERIAL_NUMBER)
|
2021-11-28 22:41:41 +00:00
|
|
|
);
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (responseFrame.id != ResponseId::OK) {
|
2022-02-05 15:32:08 +00:00
|
|
|
throw DeviceInitializationFailure(
|
|
|
|
|
"Failed to fetch serial number from device - invalid Discovery Protocol response ID."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
const auto data = responseFrame.getPayloadData();
|
2022-02-05 15:32:08 +00:00
|
|
|
return std::string(data.begin(), data.end());
|
2021-11-28 22:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void MplabPickit4::startSession() {
|
|
|
|
|
using namespace CommandFrames::HouseKeeping;
|
2022-03-01 20:25:16 +00:00
|
|
|
using ResponseFrames::HouseKeeping::ResponseId;
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
2022-02-05 15:32:08 +00:00
|
|
|
StartSession()
|
|
|
|
|
);
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (responseFrame.id == ResponseId::FAILED) {
|
2022-02-05 15:32:08 +00:00
|
|
|
// Failed response returned!
|
|
|
|
|
throw DeviceInitializationFailure("Failed to start session with MPLAB PICkit 4!");
|
|
|
|
|
}
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->sessionStarted = true;
|
2021-11-28 22:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void MplabPickit4::endSession() {
|
|
|
|
|
using namespace CommandFrames::HouseKeeping;
|
2022-03-01 20:25:16 +00:00
|
|
|
using ResponseFrames::HouseKeeping::ResponseId;
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
2022-02-05 15:32:08 +00:00
|
|
|
EndSession()
|
|
|
|
|
);
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (responseFrame.id == ResponseId::FAILED) {
|
2022-02-05 15:32:08 +00:00
|
|
|
// Failed response returned!
|
|
|
|
|
throw DeviceFailure("Failed to end session with MPLAB PICkit 4!");
|
|
|
|
|
}
|
2021-11-28 22:41:41 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->sessionStarted = false;
|
2021-11-28 22:41:41 +00:00
|
|
|
}
|
|
|
|
|
}
|