Moved most of the EDBG device code to an abstract base class, to reduce code duplication

This commit is contained in:
Nav
2022-10-02 15:29:17 +01:00
parent 1bf0e97248
commit 0d4216fd8f
21 changed files with 412 additions and 1431 deletions

View File

@@ -1,99 +1,12 @@
#include "MplabSnap.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;
MplabSnap::MplabSnap()
: UsbDevice(MplabSnap::USB_VENDOR_ID, MplabSnap::USB_PRODUCT_ID)
: EdbgDevice(
MplabSnap::USB_VENDOR_ID,
MplabSnap::USB_PRODUCT_ID,
MplabSnap::CMSIS_HID_INTERFACE_NUMBER
)
{}
void MplabSnap::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 MplabSnap::close() {
if (this->sessionStarted) {
this->endSession();
}
this->edbgInterface->getUsbHidInterface().close();
UsbDevice::close();
}
std::string MplabSnap::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 MplabSnap::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 Snap!");
}
this->sessionStarted = true;
}
void MplabSnap::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 Snap!");
}
this->sessionStarted = false;
}
}

View File

@@ -1,94 +1,37 @@
#pragma once
#include <cstdint>
#include <vector>
#include <memory>
#include <string>
#include "src/DebugToolDrivers/DebugTool.hpp"
#include "src/DebugToolDrivers/USB/UsbDevice.hpp"
#include "src/DebugToolDrivers/USB/HID/HidInterface.hpp"
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/CmsisDapInterface.hpp"
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/VendorSpecific/EDBG/EdbgInterface.hpp"
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/VendorSpecific/EDBG/AVR/EdbgAvr8Interface.hpp"
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/VendorSpecific/EDBG/AVR/EdbgAvrIspInterface.hpp"
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/VendorSpecific/EDBG/AVR/CommandFrames/AvrCommandFrames.hpp"
#include "src/DebugToolDrivers/Microchip/EdbgDevice.hpp"
namespace Bloom::DebugToolDrivers
{
/**
* The MPLAB Snap device is a hybrid device - that is, it can present itself as an "MPLAB Snap ICD" device, as well
* as an EDBG (Embedded Debugger) device. The device switches between these two modes via a firmware update, issued
* by Microchip software (the MPLAB IDE and IPE). It appears that it can only interface with AVR targets using the
* EDBG firmware, which, apparently, is why it is known to be in "AVR mode" when it presents itself as an EDBG
* device.
* as an EDBG (Embedded Debugger) device. The device switches between these two modes via firmware configuration.
* It appears that it can only interface with AVR targets using the EDBG firmware, which, apparently, is why it is
* known to be in "AVR mode" when it presents itself as an EDBG device.
*
* This debug tool driver currently only supports the device when in AVR mode. Because the device uses different
* vendor & product IDs depending on the mode, it is easy to determine which is which. In fact, Bloom will not even
* recognise the device if it's not in AVR mode.
* USB vendor and product IDs depending on the mode, it is trivial to determine which is which. In fact, Bloom will
* not even recognise the device if it's not in AVR mode.
*
* Communication:
* Uses the same EDBG protocol as described in the AtmelIce driver. See the AtmelIce debug tool class for more.
*
* USB Setup (when in AVR/EDBG mode):
* USB (when in AVR/EDBG mode):
* Vendor ID: 0x03eb (1003)
* Product ID: 0x2180 (8576)
*/
class MplabSnap: public DebugTool, public Usb::UsbDevice
class MplabSnap: public EdbgDevice
{
public:
static const std::uint16_t USB_VENDOR_ID = 1003;
static const std::uint16_t USB_PRODUCT_ID = 8576;
static const inline std::uint16_t USB_VENDOR_ID = 0x03eb;
static const inline std::uint16_t USB_PRODUCT_ID = 0x2180;
static const inline std::uint8_t CMSIS_HID_INTERFACE_NUMBER = 0;
MplabSnap();
void init() override;
void close() override;
TargetInterfaces::Microchip::Avr::Avr8::Avr8DebugInterface* getAvr8DebugInterface() override {
return this->edbgAvr8Interface.get();
}
TargetInterfaces::Microchip::Avr::AvrIspInterface* getAvrIspInterface() override {
return this->edbgAvrIspInterface.get();
}
std::string getName() override {
return "MPLAB Snap";
}
/**
* Retrieves the device serial number via the Discovery Protocol.
*
* @return
*/
std::string getSerialNumber() override;
/**
* Starts a session with the EDBG-based tool using the housekeeping protocol.
*/
void startSession();
/**
* Ends the active session with the debug tool.
*/
void endSession();
private:
std::unique_ptr<Protocols::CmsisDap::Edbg::EdbgInterface> edbgInterface = nullptr;
/**
* The MPLAB Snap employs the EDBG AVR8 Generic protocol, for debugging AVR8 targets. This protocol is
* implemented in EdbgAvr8Interface. See the EdbgAvr8Interface class for more information.
*/
std::unique_ptr<Protocols::CmsisDap::Edbg::Avr::EdbgAvr8Interface> edbgAvr8Interface = nullptr;
/**
* The MPLAB Snap employs the EDBG AVRISP protocol, for interfacing with AVR targets via the ISP interface.
* See the EdbgAvrIspInterface class for the protocol implementation.
*/
std::unique_ptr<Protocols::CmsisDap::Edbg::Avr::EdbgAvrIspInterface> edbgAvrIspInterface = nullptr;
bool sessionStarted = false;
};
}