Moved most of the EDBG device code to an abstract base class, to reduce code duplication
This commit is contained in:
@@ -1,101 +1,13 @@
|
||||
#include "XplainedNano.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;
|
||||
using Protocols::CmsisDap::Edbg::EdbgTargetPowerManagementInterface;
|
||||
|
||||
XplainedNano::XplainedNano()
|
||||
: UsbDevice(XplainedNano::USB_VENDOR_ID, XplainedNano::USB_PRODUCT_ID)
|
||||
: EdbgDevice(
|
||||
XplainedNano::USB_VENDOR_ID,
|
||||
XplainedNano::USB_PRODUCT_ID,
|
||||
XplainedNano::CMSIS_HID_INTERFACE_NUMBER,
|
||||
true
|
||||
)
|
||||
{}
|
||||
|
||||
void XplainedNano::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));
|
||||
|
||||
if (!this->sessionStarted) {
|
||||
this->startSession();
|
||||
}
|
||||
|
||||
this->targetPowerManagementInterface = std::make_unique<EdbgTargetPowerManagementInterface>(
|
||||
this->edbgInterface.get()
|
||||
);
|
||||
|
||||
this->edbgAvr8Interface = std::make_unique<EdbgAvr8Interface>(this->edbgInterface.get());
|
||||
this->setInitialised(true);
|
||||
}
|
||||
|
||||
void XplainedNano::close() {
|
||||
if (this->sessionStarted) {
|
||||
this->endSession();
|
||||
}
|
||||
|
||||
this->edbgInterface->getUsbHidInterface().close();
|
||||
UsbDevice::close();
|
||||
}
|
||||
|
||||
std::string XplainedNano::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 XplainedNano::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 Xplained Nano!");
|
||||
}
|
||||
|
||||
this->sessionStarted = true;
|
||||
}
|
||||
|
||||
void XplainedNano::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 Xplained Nano!");
|
||||
}
|
||||
|
||||
this->sessionStarted = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +1,30 @@
|
||||
#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/EdbgTargetPowerManagementInterface.hpp"
|
||||
|
||||
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/VendorSpecific/EDBG/AVR/EdbgAvr8Interface.hpp"
|
||||
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/VendorSpecific/EDBG/AVR/CommandFrames/AvrCommandFrames.hpp"
|
||||
#include "src/DebugToolDrivers/Microchip/EdbgDevice.hpp"
|
||||
|
||||
namespace Bloom::DebugToolDrivers
|
||||
{
|
||||
/**
|
||||
* The Xplained Nano is an evaluation board featuring an on-board debugger. The debugger is EDBG-based.
|
||||
*
|
||||
* Because the on-board debugger is EDBG-based, we can employ the same AVR8 driver implementation as we do with
|
||||
* other EDBG-based debuggers. See the EdbgAvr8Interface class for more.
|
||||
*
|
||||
* USB Setup:
|
||||
* USB:
|
||||
* Vendor ID: 0x03eb (1003)
|
||||
* Product ID: 0x2145 (8517)
|
||||
*/
|
||||
class XplainedNano: public DebugTool, public Usb::UsbDevice
|
||||
class XplainedNano: public EdbgDevice
|
||||
{
|
||||
public:
|
||||
static const std::uint16_t USB_VENDOR_ID = 1003;
|
||||
static const std::uint16_t USB_PRODUCT_ID = 8517;
|
||||
static const inline std::uint16_t USB_VENDOR_ID = 0x03eb;
|
||||
static const inline std::uint16_t USB_PRODUCT_ID = 0x2145;
|
||||
static const inline std::uint8_t CMSIS_HID_INTERFACE_NUMBER = 0;
|
||||
|
||||
XplainedNano();
|
||||
|
||||
void init() override;
|
||||
|
||||
void close() override;
|
||||
|
||||
DebugToolDrivers::TargetInterfaces::TargetPowerManagementInterface* getTargetPowerManagementInterface() override {
|
||||
return this->targetPowerManagementInterface.get();
|
||||
}
|
||||
|
||||
TargetInterfaces::Microchip::Avr::Avr8::Avr8DebugInterface* getAvr8DebugInterface() override {
|
||||
return this->edbgAvr8Interface.get();
|
||||
}
|
||||
|
||||
std::string getName() override {
|
||||
return "Xplained Nano";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
std::unique_ptr<Protocols::CmsisDap::Edbg::Avr::EdbgAvr8Interface> edbgAvr8Interface = nullptr;
|
||||
std::unique_ptr<
|
||||
Protocols::CmsisDap::Edbg::EdbgTargetPowerManagementInterface
|
||||
> targetPowerManagementInterface = nullptr;
|
||||
|
||||
bool sessionStarted = false;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user