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

95 lines
3.3 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
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
}
Protocols::CmsisDap::Edbg::Avr::AvrResponse EdbgInterface::getAvrResponse() {
auto cmsisResponse = this->getResponse();
2021-04-04 21:04:12 +01:00
if (cmsisResponse->getResponseId() == 0x81) {
// This is an AVR_RSP response
auto avrResponse = Protocols::CmsisDap::Edbg::Avr::AvrResponse();
avrResponse.init(*cmsisResponse);
return avrResponse;
}
2021-04-04 21:04:12 +01:00
throw DeviceCommunicationFailure("Unexpected response to AvrResponseCommand from device");
2021-04-04 21:04:12 +01:00
}
std::optional<Protocols::CmsisDap::Edbg::Avr::AvrEvent> EdbgInterface::requestAvrEvent() {
this->sendCommand(AvrEventCommand());
auto cmsisResponse = this->getResponse();
if (cmsisResponse->getResponseId() == 0x82) {
// This is an AVR_EVT response
auto avrEvent = Protocols::CmsisDap::Edbg::Avr::AvrEvent();
avrEvent.init(*cmsisResponse);
return avrEvent.getEventDataSize() > 0 ? std::optional(avrEvent) : std::nullopt;
}
2021-04-04 21:04:12 +01:00
throw DeviceCommunicationFailure("Unexpected response to AvrEventCommand from device");
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
this->sendCommand(responseCommand);
auto response = this->getAvrResponse();
responses.push_back(response);
int fragmentCount = response.getFragmentCount();
while (responses.size() < fragmentCount) {
// There are more response packets
this->sendCommand(responseCommand);
response = this->getAvrResponse();
if (response.getFragmentCount() != fragmentCount) {
throw DeviceCommunicationFailure(
"Failed to fetch AVRResponse objects - invalid fragment count returned."
);
}
if (response.getFragmentCount() == 0 && response.getFragmentNumber() == 0) {
throw DeviceCommunicationFailure(
"Failed to fetch AVRResponse objects - unexpected empty response"
);
}
if (response.getFragmentNumber() == 0) {
// End of response data ( &this packet can be ignored)
break;
}
responses.push_back(response);
2021-04-04 21:04:12 +01:00
}
return responses;
2021-04-04 21:04:12 +01:00
}
}