Files
BloomPatched/src/DebugToolDrivers/Protocols/CMSIS-DAP/VendorSpecific/EDBG/EdbgInterface.cpp

80 lines
2.8 KiB
C++
Raw Normal View History

#include "EdbgInterface.hpp"
2021-04-04 21:04:12 +01:00
#include <memory>
#include "src/TargetController/Exceptions/DeviceCommunicationFailure.hpp"
2021-04-04 21:04:12 +01:00
namespace Bloom::DebugToolDrivers::Protocols::CmsisDap::Edbg
{
using namespace Bloom::Exceptions;
2021-04-04 21:04:12 +01:00
EdbgInterface::EdbgInterface(Usb::HidInterface&& usbHidInterface)
: CmsisDapInterface(std::move(usbHidInterface))
{}
Protocols::CmsisDap::Response EdbgInterface::sendAvrCommandsAndWaitForResponse(
const std::vector<Avr::AvrCommand>& avrCommands
) {
for (const auto& avrCommand : avrCommands) {
// Send command to device
auto response = this->sendCommandAndWaitForResponse(avrCommand);
2021-04-04 21:04:12 +01:00
if (&avrCommand == &avrCommands.back()) {
return response;
}
2021-04-04 21:04:12 +01:00
}
// This should never happen
throw DeviceCommunicationFailure(
"Cannot send AVR command frame - failed to generate CMSIS-DAP Vendor (AVR) commands"
);
2021-04-04 21:04:12 +01:00
}
std::optional<Protocols::CmsisDap::Edbg::Avr::AvrEvent> EdbgInterface::requestAvrEvent() {
auto avrEventResponse = this->sendCommandAndWaitForResponse(Avr::AvrEventCommand());
if (avrEventResponse.id != 0x82) {
throw DeviceCommunicationFailure("Unexpected response to AvrEventCommand from device");
}
2021-04-04 21:04:12 +01:00
return !avrEventResponse.eventData.empty() ? std::optional(avrEventResponse) : std::nullopt;
2021-04-04 21:04:12 +01:00
}
std::vector<Protocols::CmsisDap::Edbg::Avr::AvrResponse> EdbgInterface::requestAvrResponses() {
using Protocols::CmsisDap::Edbg::Avr::AvrResponseCommand;
2021-04-04 21:04:12 +01:00
std::vector<Protocols::CmsisDap::Edbg::Avr::AvrResponse> responses;
AvrResponseCommand responseCommand;
2021-04-04 21:04:12 +01:00
auto avrResponse = this->sendCommandAndWaitForResponse(responseCommand);
responses.push_back(avrResponse);
const auto fragmentCount = avrResponse.fragmentCount;
while (responses.size() < fragmentCount) {
// There are more response packets
auto avrResponse = this->sendCommandAndWaitForResponse(responseCommand);
if (avrResponse.fragmentCount != fragmentCount) {
throw DeviceCommunicationFailure(
2022-03-01 19:58:04 +00:00
"Failed to fetch AvrResponse objects - invalid fragment count returned."
);
}
if (avrResponse.fragmentCount == 0 && avrResponse.fragmentNumber == 0) {
throw DeviceCommunicationFailure(
2022-03-01 19:58:04 +00:00
"Failed to fetch AvrResponse objects - unexpected empty response"
);
}
if (avrResponse.fragmentNumber == 0) {
// End of response data ( &this packet can be ignored)
break;
}
responses.push_back(avrResponse);
2021-04-04 21:04:12 +01:00
}
return responses;
2021-04-04 21:04:12 +01:00
}
}