Debug tool device driver for the MPLAB PICkit 4
This commit is contained in:
94
src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.cpp
Normal file
94
src/DebugToolDrivers/Microchip/MplabPickit4/MplabPickit4.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "MplabPickit4.hpp"
|
||||
|
||||
#include "src/TargetController/Exceptions/DeviceFailure.hpp"
|
||||
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
|
||||
|
||||
using namespace Bloom::DebugToolDrivers;
|
||||
using namespace Bloom::DebugToolDrivers::Protocols::CmsisDap::Edbg::Avr;
|
||||
using namespace Bloom::Exceptions;
|
||||
|
||||
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);
|
||||
|
||||
if (!usbHidInterface.isInitialised()) {
|
||||
usbHidInterface.init();
|
||||
}
|
||||
|
||||
this->getEdbgInterface().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);
|
||||
|
||||
/*
|
||||
* Like the MPLAB Snap, the PICkit 4 doesn't seem to operate correctly when actioning the masked memory read
|
||||
* command. The data returned in response to the command appears to be completely incorrect.
|
||||
*
|
||||
* For the above reason, we avoid using the masked memory read command by implementing the masking on our end.
|
||||
* See the EdbgAvr8Interface class for more.
|
||||
*/
|
||||
this->edbgAvr8Interface->setAvoidMaskedMemoryRead(true);
|
||||
|
||||
this->setInitialised(true);
|
||||
}
|
||||
|
||||
void MplabPickit4::close() {
|
||||
if (this->sessionStarted) {
|
||||
this->endSession();
|
||||
}
|
||||
|
||||
this->getEdbgInterface().getUsbHidInterface().close();
|
||||
UsbDevice::close();
|
||||
}
|
||||
|
||||
std::string MplabPickit4::getSerialNumber() {
|
||||
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
CommandFrames::Discovery::Query(CommandFrames::Discovery::QueryContext::SERIAL_NUMBER)
|
||||
);
|
||||
|
||||
if (response.getResponseId() != CommandFrames::Discovery::ResponseId::OK) {
|
||||
throw DeviceInitializationFailure(
|
||||
"Failed to fetch serial number from device - invalid Discovery Protocol response ID."
|
||||
);
|
||||
}
|
||||
|
||||
auto data = response.getPayloadData();
|
||||
return std::string(data.begin(), data.end());
|
||||
}
|
||||
|
||||
void MplabPickit4::startSession() {
|
||||
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
CommandFrames::HouseKeeping::StartSession()
|
||||
);
|
||||
|
||||
if (response.getResponseId() == CommandFrames::HouseKeeping::ResponseId::FAILED) {
|
||||
// Failed response returned!
|
||||
throw DeviceInitializationFailure("Failed to start session with MPLAB PICkit 4!");
|
||||
}
|
||||
|
||||
this->sessionStarted = true;
|
||||
}
|
||||
|
||||
void MplabPickit4::endSession() {
|
||||
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
|
||||
CommandFrames::HouseKeeping::EndSession()
|
||||
);
|
||||
|
||||
if (response.getResponseId() == CommandFrames::HouseKeeping::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