Initial commit

This commit is contained in:
Nav
2021-04-04 21:04:12 +01:00
commit a29c5e1fec
549 changed files with 441216 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
#include <stdexcept>
#include "AtmelIce.hpp"
#include "src/Exceptions/Exception.hpp"
using namespace Bloom::DebugToolDrivers;
using namespace Protocols::CmsisDap::Edbg::Avr;
using namespace Bloom::Exceptions;
void AtmelIce::init() {
UsbDevice::init();
// TODO: Move away from hard-coding the CMSIS-DAP/EDBG interface number
auto& usbHidInterface = this->getEdbgInterface().getUsbHidInterface();
usbHidInterface.setNumber(0);
usbHidInterface.setUSBDevice(this->getLibUsbDevice());
usbHidInterface.setVendorId(this->getVendorId());
usbHidInterface.setProductId(this->getProductId());
if (!usbHidInterface.isInitialised()) {
usbHidInterface.init();
}
/**
* The Atmel-ICE EDBG/CMSIS-DAP interface doesn't operate properly when sending commands too quickly.
*
* Because of this, we have to enforce a minimum time gap between commands. See comment
* in CmsisDapInterface class declaration for more info.
*/
this->getEdbgInterface().setMinimumCommandTimeGap(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);
this->setInitialised(true);
}
void AtmelIce::close() {
if (this->sessionStarted) {
this->endSession();
}
this->getEdbgInterface().getUsbHidInterface().close();
UsbDevice::close();
}
std::string AtmelIce::getSerialNumber() {
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
CommandFrames::Discovery::Query(CommandFrames::Discovery::QueryContext::SERIAL_NUMBER)
);
if (response.getResponseId() != CommandFrames::Discovery::ResponseId::OK) {
throw Exception("Failed to fetch serial number from device - invalid Discovery Protocol response ID.");
}
auto data = response.getPayloadData();
return std::string(data.begin(), data.end());
}
void AtmelIce::startSession() {
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
CommandFrames::HouseKeeping::StartSession()
);
if (response.getResponseId() == CommandFrames::HouseKeeping::ResponseId::FAILED) {
// Failed response returned!
throw Exception("Failed to start session with Atmel-ICE!");
}
this->sessionStarted = true;
}
void AtmelIce::endSession() {
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
CommandFrames::HouseKeeping::EndSession()
);
if (response.getResponseId() == CommandFrames::HouseKeeping::ResponseId::FAILED) {
// Failed response returned!
throw Exception("Failed to end session with Atmel-ICE!");
}
this->sessionStarted = false;
}

View File

@@ -0,0 +1,104 @@
#pragma once
#include <cstdint>
#include <vector>
#include <memory>
#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/CommandFrames/AvrCommandFrames.hpp"
namespace Bloom::DebugToolDrivers
{
using namespace Protocols::CmsisDap;
using Protocols::CmsisDap::Edbg::EdbgInterface;
using Protocols::CmsisDap::Edbg::Avr::EdbgAvr8Interface;
/**
* The Atmel-ICE device is an EDBG (Embedded Debugger) device. It implements the CMSIS-DAP layer as well
* as an Atmel Data Gateway Interface (DGI).
*
* Communication:
* Using the Atmel-ICE device for AVR debugging/programming requires the AVR communication protocol, which
* is a sub-protocol of the CMSIS-DAP (AVR communication protocol commands are wrapped in CMSIS-DAP vendor
* commands). AVR communication protocol commands contain AVR frames, in which commands for numerous
* sub-protocols are enveloped.
*
* So to summarise, issuing an AVR command to the EDBG device, involves:
* Actual command data -> AVR sub-protocol -> AVR Frames -> CMSIS-DAP Vendor commands -> EDBG Device
*
* For more information on protocols and sub-protocols employed by Microchip EDBG devices, see
* the 'Embedded Debugger-Based Tools Protocols User's Guide' document by Microchip.
* @link http://ww1.microchip.com/downloads/en/DeviceDoc/50002630A.pdf
*
* USB Setup:
* Vendor ID: 0x03eb (1003)
* Product ID: 0x2141 (8513)
*
* The Atmel-ICE consists of two USB interfaces. One HID interface for CMSIS-DAP and one vendor specific
* interface for the DGI. We only work with the CMSIS-DAP interface, for now.
*/
class AtmelIce: public DebugTool, public Usb::UsbDevice
{
private:
/**
* The EDBG interface implements additional functionality via vendor specific CMSIS-DAP commands.
* In other words, all EDBG commands are just CMSIS-DAP vendor commands that allow the debug tool
* to support additional functionality, like AVR programming and debugging.
*
* Any non-EDBG CMSIS-DAP commands for the Atmel-ICE can be sent through the EdbgInterface (as the
* EdbgInterface extends the CmsisDapInterface).
*/
EdbgInterface edbgInterface = EdbgInterface();
/**
* The Atmel-ICE 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<EdbgAvr8Interface> edbgAvr8Interface = nullptr;
bool sessionStarted = false;
public:
static const std::uint16_t USB_VENDOR_ID = 1003;
static const std::uint16_t USB_PRODUCT_ID = 8513;
AtmelIce() : UsbDevice(AtmelIce::USB_VENDOR_ID, AtmelIce::USB_PRODUCT_ID) {}
void init() override;
void close() override;
EdbgInterface& getEdbgInterface() {
return this->edbgInterface;
}
Avr8Interface* getAvr8Interface() override {
return this->edbgAvr8Interface.get();
}
std::string getName() override {
return "Atmel-ICE";
};
/**
* 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();
};
}

View File

@@ -0,0 +1,87 @@
#include <stdexcept>
#include "PowerDebugger.hpp"
#include "src/Exceptions/Exception.hpp"
using namespace Bloom::DebugToolDrivers;
using namespace Bloom::DebugToolDrivers::Protocols::CmsisDap::Edbg::Avr;
using namespace Bloom::Exceptions;
void PowerDebugger::init() {
UsbDevice::init();
// TODO: Move away from hard-coding the CMSIS-DAP/EDBG interface number
auto& usbHidInterface = this->getEdbgInterface().getUsbHidInterface();
usbHidInterface.setNumber(0);
usbHidInterface.setUSBDevice(this->getLibUsbDevice());
usbHidInterface.setVendorId(this->getVendorId());
usbHidInterface.setProductId(this->getProductId());
if (!usbHidInterface.isInitialised()) {
usbHidInterface.init();
}
/**
* The Power Debugger EDBG/CMSIS-DAP interface doesn't operate properly when sending commands too quickly.
*
* Because of this, we have to enforce a minimum time gap between commands. See comment in
* CmsisDapInterface class declaration for more info.
*/
this->getEdbgInterface().setMinimumCommandTimeGap(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);
this->setInitialised(true);
}
void PowerDebugger::close() {
if (this->sessionStarted) {
this->endSession();
}
this->getEdbgInterface().getUsbHidInterface().close();
UsbDevice::close();
}
std::string PowerDebugger::getSerialNumber() {
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
CommandFrames::Discovery::Query(Discovery::QueryContext::SERIAL_NUMBER)
);
if (response.getResponseId() != Discovery::ResponseId::OK) {
throw Exception("Failed to fetch serial number from device - invalid Discovery Protocol response ID.");
}
auto data = response.getPayloadData();
return std::string(data.begin(), data.end());
}
void PowerDebugger::startSession() {
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
CommandFrames::HouseKeeping::StartSession()
);
if (response.getResponseId() == HouseKeeping::ResponseId::FAILED) {
// Failed response returned!
throw Exception("Failed to start session with the Power Debugger - device returned failed response ID");
}
this->sessionStarted = true;
}
void PowerDebugger::endSession() {
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
CommandFrames::HouseKeeping::EndSession()
);
if (response.getResponseId() == HouseKeeping::ResponseId::FAILED) {
// Failed response returned!
throw Exception("Failed to end session with the Power Debugger - device returned failed response ID");
}
this->sessionStarted = false;
}

View File

@@ -0,0 +1,96 @@
#pragma once
#include <cstdint>
#include <vector>
#include <memory>
#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/CommandFrames/AvrCommandFrames.hpp"
namespace Bloom::DebugToolDrivers
{
using namespace Protocols::CmsisDap;
using namespace Protocols::CmsisDap::Edbg::Avr::CommandFrames;
using namespace Protocols::CmsisDap::Edbg::Avr::CommandFrames::Discovery;
using namespace Protocols::CmsisDap::Edbg::Avr::CommandFrames::HouseKeeping;
using Protocols::CmsisDap::Edbg::EdbgInterface;
using Protocols::CmsisDap::Edbg::Avr::EdbgAvr8Interface;
/**
* The Power Debugger device is very similar to the Atmel-ICE. It implements the CMSIS-DAP layer as well
* as an Atmel Data Gateway Interface (DGI).
*
* Communication:
* Like the Atmel-ICE, using the Power Debugger device for AVR debugging/programming requires the AVR
* communication protocol, which is a sub-protocol of the CMSIS-DAP.
*
* For more information on the communication protocol, see the DebugToolDrivers::AtmelIce class.
*
* USB Setup:
* Vendor ID: 0x03eb (1003)
* Product ID: 0x2141 (8513)
*/
class PowerDebugger: public DebugTool, public Usb::UsbDevice
{
private:
/**
* The EDBG interface implements additional functionality via vendor specific CMSIS-DAP commands.
* In other words, all EDBG commands are just CMSIS-DAP vendor commands that allow the debug tool
* to support additional functionality, like AVR programming and debugging.
*
* Any non-EDBG CMSIS-DAP commands for the Power Debugger can be sent through the EDBGInterface (as the
* EdbgInterface extends the CmsisDapInterface).
*/
EdbgInterface edbgInterface = EdbgInterface();
/**
* The Power Debugger employs the EDBG AVR8Generic protocol for interfacing with AVR8 targets.
*/
std::unique_ptr<EdbgAvr8Interface> edbgAvr8Interface;
bool sessionStarted = false;
public:
static const std::uint16_t USB_VENDOR_ID = 1003;
static const std::uint16_t USB_PRODUCT_ID = 8516;
PowerDebugger() : UsbDevice(PowerDebugger::USB_VENDOR_ID, PowerDebugger::USB_PRODUCT_ID) {}
void init() override;
void close() override;
EdbgInterface& getEdbgInterface() {
return this->edbgInterface;
}
Avr8Interface* getAvr8Interface() override {
return this->edbgAvr8Interface.get();
}
std::string getName() override {
return "Power Debugger";
};
/**
* 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();
};
}