2022-10-02 15:29:17 +01:00
|
|
|
#include "EdbgDevice.hpp"
|
|
|
|
|
|
|
|
|
|
#include "src/DebugToolDrivers/USB/HID/HidInterface.hpp"
|
Made the EDBG CMSIS-DAP command delay optional for all debug tools, and disabled it by default.
The command delay was really choking Bloom's EDBG driver, causing a very noticeable drag on Bloom's performance. It's much faster with the command delay disabled.
There was a good reason for why I introduced this some time ago. Without it, some EDBG debug tools were misbehaving - I remember that for certain. But now, I cannot seem to reproduce these issues. Very odd.
If the issues do reappear, I may have to enable the command delay by default, again, for some debug tools.
For now, if any users experience issues, I'll just suggest they manually enable the command delay via their project config.
Also, I'm not going to document this new config option, as I would prefer the user to approach me if they experience issues as a result of this, so that I'll know if it needs revisiting.
2024-10-27 00:25:42 +01:00
|
|
|
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/CmsisDapInterface.hpp"
|
2023-11-17 22:20:39 +00:00
|
|
|
#include "src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/CommandFrames/AvrCommandFrames.hpp"
|
2022-10-02 15:29:17 +01:00
|
|
|
|
Made the EDBG CMSIS-DAP command delay optional for all debug tools, and disabled it by default.
The command delay was really choking Bloom's EDBG driver, causing a very noticeable drag on Bloom's performance. It's much faster with the command delay disabled.
There was a good reason for why I introduced this some time ago. Without it, some EDBG debug tools were misbehaving - I remember that for certain. But now, I cannot seem to reproduce these issues. Very odd.
If the issues do reappear, I may have to enable the command delay by default, again, for some debug tools.
For now, if any users experience issues, I'll just suggest they manually enable the command delay via their project config.
Also, I'm not going to document this new config option, as I would prefer the user to approach me if they experience issues as a result of this, so that I'll know if it needs revisiting.
2024-10-27 00:25:42 +01:00
|
|
|
#include "src/Exceptions/InvalidConfig.hpp"
|
2022-10-02 15:29:17 +01:00
|
|
|
#include "src/TargetController/Exceptions/DeviceFailure.hpp"
|
|
|
|
|
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
|
|
|
|
|
|
2023-11-18 23:50:08 +00:00
|
|
|
namespace DebugToolDrivers::Microchip
|
2022-10-02 15:29:17 +01:00
|
|
|
{
|
2023-11-17 22:20:39 +00:00
|
|
|
using namespace Microchip::Protocols::Edbg::Avr;
|
2022-10-02 15:29:17 +01:00
|
|
|
|
2023-11-17 22:20:39 +00:00
|
|
|
using Exceptions::DeviceFailure;
|
|
|
|
|
using Exceptions::DeviceInitializationFailure;
|
2022-10-02 15:29:17 +01:00
|
|
|
|
|
|
|
|
EdbgDevice::EdbgDevice(
|
Made the EDBG CMSIS-DAP command delay optional for all debug tools, and disabled it by default.
The command delay was really choking Bloom's EDBG driver, causing a very noticeable drag on Bloom's performance. It's much faster with the command delay disabled.
There was a good reason for why I introduced this some time ago. Without it, some EDBG debug tools were misbehaving - I remember that for certain. But now, I cannot seem to reproduce these issues. Very odd.
If the issues do reappear, I may have to enable the command delay by default, again, for some debug tools.
For now, if any users experience issues, I'll just suggest they manually enable the command delay via their project config.
Also, I'm not going to document this new config option, as I would prefer the user to approach me if they experience issues as a result of this, so that I'll know if it needs revisiting.
2024-10-27 00:25:42 +01:00
|
|
|
const DebugToolConfig& debugToolConfig,
|
2022-10-02 15:29:17 +01:00
|
|
|
std::uint16_t vendorId,
|
|
|
|
|
std::uint16_t productId,
|
|
|
|
|
std::uint8_t cmsisHidInterfaceNumber,
|
|
|
|
|
bool supportsTargetPowerManagement,
|
|
|
|
|
std::optional<std::uint8_t> configurationIndex
|
|
|
|
|
)
|
|
|
|
|
: UsbDevice(vendorId, productId)
|
Made the EDBG CMSIS-DAP command delay optional for all debug tools, and disabled it by default.
The command delay was really choking Bloom's EDBG driver, causing a very noticeable drag on Bloom's performance. It's much faster with the command delay disabled.
There was a good reason for why I introduced this some time ago. Without it, some EDBG debug tools were misbehaving - I remember that for certain. But now, I cannot seem to reproduce these issues. Very odd.
If the issues do reappear, I may have to enable the command delay by default, again, for some debug tools.
For now, if any users experience issues, I'll just suggest they manually enable the command delay via their project config.
Also, I'm not going to document this new config option, as I would prefer the user to approach me if they experience issues as a result of this, so that I'll know if it needs revisiting.
2024-10-27 00:25:42 +01:00
|
|
|
, toolConfig(EdbgToolConfig{debugToolConfig})
|
2022-10-02 15:29:17 +01:00
|
|
|
, cmsisHidInterfaceNumber(cmsisHidInterfaceNumber)
|
|
|
|
|
, supportsTargetPowerManagement(supportsTargetPowerManagement)
|
|
|
|
|
, configurationIndex(configurationIndex)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void EdbgDevice::init() {
|
Made the EDBG CMSIS-DAP command delay optional for all debug tools, and disabled it by default.
The command delay was really choking Bloom's EDBG driver, causing a very noticeable drag on Bloom's performance. It's much faster with the command delay disabled.
There was a good reason for why I introduced this some time ago. Without it, some EDBG debug tools were misbehaving - I remember that for certain. But now, I cannot seem to reproduce these issues. Very odd.
If the issues do reappear, I may have to enable the command delay by default, again, for some debug tools.
For now, if any users experience issues, I'll just suggest they manually enable the command delay via their project config.
Also, I'm not going to document this new config option, as I would prefer the user to approach me if they experience issues as a result of this, so that I'll know if it needs revisiting.
2024-10-27 00:25:42 +01:00
|
|
|
using ::DebugToolDrivers::Protocols::CmsisDap::CmsisDapInterface;
|
2023-11-17 22:20:39 +00:00
|
|
|
using Microchip::Protocols::Edbg::EdbgInterface;
|
|
|
|
|
using Microchip::Protocols::Edbg::EdbgTargetPowerManagementInterface;
|
|
|
|
|
|
2022-10-02 15:29:17 +01:00
|
|
|
UsbDevice::init();
|
|
|
|
|
|
2023-01-14 03:03:10 +00:00
|
|
|
this->detachKernelDriverFromInterface(this->cmsisHidInterfaceNumber);
|
2022-10-02 15:29:17 +01:00
|
|
|
|
|
|
|
|
if (this->configurationIndex.has_value()) {
|
|
|
|
|
this->setConfiguration(this->configurationIndex.value());
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
auto cmsisHidInterface = Usb::HidInterface{
|
2023-01-14 03:03:10 +00:00
|
|
|
this->cmsisHidInterfaceNumber,
|
2023-12-02 19:31:20 +00:00
|
|
|
this->getEndpointMaxPacketSize(
|
|
|
|
|
this->getFirstEndpointAddress(this->cmsisHidInterfaceNumber, LIBUSB_ENDPOINT_IN)
|
|
|
|
|
),
|
2023-01-14 03:03:10 +00:00
|
|
|
this->vendorId,
|
|
|
|
|
this->productId
|
2024-07-23 21:14:22 +01:00
|
|
|
};
|
2023-01-14 03:03:10 +00:00
|
|
|
|
2022-11-16 23:51:07 +00:00
|
|
|
cmsisHidInterface.init();
|
2022-10-02 15:29:17 +01:00
|
|
|
|
2022-11-16 23:51:07 +00:00
|
|
|
this->edbgInterface = std::make_unique<EdbgInterface>(std::move(cmsisHidInterface));
|
2022-10-02 15:29:17 +01:00
|
|
|
|
|
|
|
|
/*
|
Made the EDBG CMSIS-DAP command delay optional for all debug tools, and disabled it by default.
The command delay was really choking Bloom's EDBG driver, causing a very noticeable drag on Bloom's performance. It's much faster with the command delay disabled.
There was a good reason for why I introduced this some time ago. Without it, some EDBG debug tools were misbehaving - I remember that for certain. But now, I cannot seem to reproduce these issues. Very odd.
If the issues do reappear, I may have to enable the command delay by default, again, for some debug tools.
For now, if any users experience issues, I'll just suggest they manually enable the command delay via their project config.
Also, I'm not going to document this new config option, as I would prefer the user to approach me if they experience issues as a result of this, so that I'll know if it needs revisiting.
2024-10-27 00:25:42 +01:00
|
|
|
* Sometimes, some EDBG tools misbehave when we send commands too quickly, even though we wait for a response
|
|
|
|
|
* for each command we send...
|
2022-10-02 15:29:17 +01:00
|
|
|
*
|
Made the EDBG CMSIS-DAP command delay optional for all debug tools, and disabled it by default.
The command delay was really choking Bloom's EDBG driver, causing a very noticeable drag on Bloom's performance. It's much faster with the command delay disabled.
There was a good reason for why I introduced this some time ago. Without it, some EDBG debug tools were misbehaving - I remember that for certain. But now, I cannot seem to reproduce these issues. Very odd.
If the issues do reappear, I may have to enable the command delay by default, again, for some debug tools.
For now, if any users experience issues, I'll just suggest they manually enable the command delay via their project config.
Also, I'm not going to document this new config option, as I would prefer the user to approach me if they experience issues as a result of this, so that I'll know if it needs revisiting.
2024-10-27 00:25:42 +01:00
|
|
|
* Because of this, we make available the option to enforce a minimum time gap between commands. This prevents
|
|
|
|
|
* the tool from misbehaving, but effectively chokes the EDBG driver, causing a drag on Bloom's performance.
|
|
|
|
|
*
|
|
|
|
|
* The command delay is disabled by default for all EDBG tools, but the user can enable it via their project
|
|
|
|
|
* config.
|
2022-10-02 15:29:17 +01:00
|
|
|
*/
|
Made the EDBG CMSIS-DAP command delay optional for all debug tools, and disabled it by default.
The command delay was really choking Bloom's EDBG driver, causing a very noticeable drag on Bloom's performance. It's much faster with the command delay disabled.
There was a good reason for why I introduced this some time ago. Without it, some EDBG debug tools were misbehaving - I remember that for certain. But now, I cannot seem to reproduce these issues. Very odd.
If the issues do reappear, I may have to enable the command delay by default, again, for some debug tools.
For now, if any users experience issues, I'll just suggest they manually enable the command delay via their project config.
Also, I'm not going to document this new config option, as I would prefer the user to approach me if they experience issues as a result of this, so that I'll know if it needs revisiting.
2024-10-27 00:25:42 +01:00
|
|
|
if (this->toolConfig.cmsisCommandDelay.has_value()) {
|
|
|
|
|
const auto& cmsisCommandDelay = *(this->toolConfig.cmsisCommandDelay);
|
|
|
|
|
|
|
|
|
|
if (cmsisCommandDelay > CmsisDapInterface::CMSIS_COMMAND_DELAY_MAX) {
|
|
|
|
|
throw Exceptions::InvalidConfig{
|
|
|
|
|
"CMSIS command delay value (" + std::to_string(cmsisCommandDelay.count())
|
|
|
|
|
+ " ms) is too high. Maximum value: " + std::to_string(
|
|
|
|
|
CmsisDapInterface::CMSIS_COMMAND_DELAY_MAX.count()
|
|
|
|
|
) + " ms"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->edbgInterface->setCommandDelay(cmsisCommandDelay);
|
|
|
|
|
}
|
2022-10-02 15:29:17 +01:00
|
|
|
|
|
|
|
|
// We don't need to claim the CMSISDAP interface here as the HIDAPI will have already done so.
|
|
|
|
|
if (!this->sessionStarted) {
|
|
|
|
|
this->startSession();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this->supportsTargetPowerManagement) {
|
|
|
|
|
this->targetPowerManagementInterface = std::make_unique<EdbgTargetPowerManagementInterface>(
|
|
|
|
|
this->edbgInterface.get()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
this->initialised = true;
|
2022-10-02 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EdbgDevice::close() {
|
|
|
|
|
if (this->sessionStarted) {
|
|
|
|
|
this->endSession();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->edbgInterface->getUsbHidInterface().close();
|
|
|
|
|
UsbDevice::close();
|
2024-07-23 21:14:22 +01:00
|
|
|
this->initialised = false;
|
2023-05-21 21:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:44:15 +01:00
|
|
|
void EdbgDevice::postInit() {
|
|
|
|
|
// TODO: Log firmware version of EDBG device
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
bool EdbgDevice::isInitialised() const {
|
|
|
|
|
return this->initialised;
|
2022-10-02 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string EdbgDevice::getSerialNumber() {
|
|
|
|
|
using namespace CommandFrames::Discovery;
|
|
|
|
|
using ResponseFrames::Discovery::ResponseId;
|
|
|
|
|
|
|
|
|
|
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
|
2024-07-23 21:14:22 +01:00
|
|
|
Query{QueryContext::SERIAL_NUMBER}
|
2022-10-02 15:29:17 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (responseFrame.id != ResponseId::OK) {
|
2024-07-23 21:14:22 +01:00
|
|
|
throw DeviceInitializationFailure{
|
2022-10-02 15:29:17 +01:00
|
|
|
"Failed to fetch serial number from device - invalid Discovery Protocol response ID."
|
2024-07-23 21:14:22 +01:00
|
|
|
};
|
2022-10-02 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto data = responseFrame.getPayloadData();
|
2024-07-23 21:14:22 +01:00
|
|
|
return std::string{data.begin(), data.end()};
|
2022-10-02 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
DebugToolDrivers::TargetInterfaces::TargetPowerManagementInterface* EdbgDevice::getTargetPowerManagementInterface()
|
|
|
|
|
{
|
|
|
|
|
return this->targetPowerManagementInterface.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TargetInterfaces::Microchip::Avr8::Avr8DebugInterface* EdbgDevice::getAvr8DebugInterface(
|
|
|
|
|
const Targets::Microchip::Avr8::TargetDescriptionFile& targetDescriptionFile,
|
|
|
|
|
const Targets::Microchip::Avr8::Avr8TargetConfig& targetConfig
|
|
|
|
|
) {
|
|
|
|
|
if (this->edbgAvr8Interface == nullptr) {
|
|
|
|
|
this->edbgAvr8Interface = std::make_unique<EdbgAvr8Interface>(
|
|
|
|
|
this->edbgInterface.get(),
|
|
|
|
|
targetDescriptionFile,
|
|
|
|
|
targetConfig
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this->configureAvr8Interface();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this->edbgAvr8Interface.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TargetInterfaces::Microchip::Avr8::AvrIspInterface* EdbgDevice::getAvrIspInterface(
|
|
|
|
|
const Targets::Microchip::Avr8::TargetDescriptionFile& targetDescriptionFile,
|
|
|
|
|
const Targets::Microchip::Avr8::Avr8TargetConfig& targetConfig
|
|
|
|
|
) {
|
|
|
|
|
if (this->edbgAvrIspInterface == nullptr) {
|
|
|
|
|
this->edbgAvrIspInterface = std::make_unique<EdbgAvrIspInterface>(
|
|
|
|
|
this->edbgInterface.get(),
|
|
|
|
|
targetDescriptionFile
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this->configureAvr8Interface();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this->edbgAvrIspInterface.get();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-02 15:29:17 +01:00
|
|
|
void EdbgDevice::startSession() {
|
|
|
|
|
using namespace CommandFrames::HouseKeeping;
|
|
|
|
|
using ResponseFrames::HouseKeeping::ResponseId;
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(StartSession{});
|
2022-10-02 15:29:17 +01:00
|
|
|
if (responseFrame.id == ResponseId::FAILED) {
|
|
|
|
|
// Failed response returned!
|
2024-07-23 21:14:22 +01:00
|
|
|
throw DeviceInitializationFailure{"Failed to start session with EDBG device!"};
|
2022-10-02 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->sessionStarted = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EdbgDevice::endSession() {
|
|
|
|
|
using namespace CommandFrames::HouseKeeping;
|
|
|
|
|
using ResponseFrames::HouseKeeping::ResponseId;
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(EndSession{});
|
2022-10-02 15:29:17 +01:00
|
|
|
if (responseFrame.id == ResponseId::FAILED) {
|
|
|
|
|
// Failed response returned!
|
2024-07-23 21:14:22 +01:00
|
|
|
throw DeviceFailure{"Failed to end session with EDBG device!"};
|
2022-10-02 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->sessionStarted = false;
|
|
|
|
|
}
|
|
|
|
|
}
|