2021-08-15 01:47:48 +01:00
|
|
|
#include "EdbgInterface.hpp"
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include <memory>
|
|
|
|
|
|
2021-08-15 01:47:48 +01:00
|
|
|
#include "src/TargetController/Exceptions/DeviceCommunicationFailure.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
namespace Bloom::DebugToolDrivers::Protocols::CmsisDap::Edbg
|
|
|
|
|
{
|
|
|
|
|
using namespace Bloom::Exceptions;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-10-01 16:50:57 +01:00
|
|
|
EdbgInterface::EdbgInterface(Usb::HidInterface&& usbHidInterface)
|
|
|
|
|
: CmsisDapInterface(std::move(usbHidInterface))
|
|
|
|
|
{}
|
|
|
|
|
|
2022-02-27 20:29:26 +00:00
|
|
|
Protocols::CmsisDap::Response EdbgInterface::sendAvrCommandsAndWaitForResponse(
|
|
|
|
|
const std::vector<Avr::AvrCommand>& avrCommands
|
2022-02-05 15:32:08 +00:00
|
|
|
) {
|
2022-02-27 20:29:26 +00:00
|
|
|
for (const auto& avrCommand : avrCommands) {
|
2022-02-05 15:32:08 +00:00
|
|
|
// Send command to device
|
|
|
|
|
auto response = this->sendCommandAndWaitForResponse(avrCommand);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (&avrCommand == &avrCommands.back()) {
|
2022-02-28 16:27:24 +00:00
|
|
|
return response;
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
2022-02-05 15:32:08 +00: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
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
std::optional<Protocols::CmsisDap::Edbg::Avr::AvrEvent> EdbgInterface::requestAvrEvent() {
|
2022-02-28 16:27:24 +00:00
|
|
|
auto avrEventResponse = this->sendCommandAndWaitForResponse(Avr::AvrEventCommand());
|
|
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (avrEventResponse.id != 0x82) {
|
2022-02-28 16:27:24 +00:00
|
|
|
throw DeviceCommunicationFailure("Unexpected response to AvrEventCommand from device");
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
return !avrEventResponse.eventData.empty() ? std::optional(avrEventResponse) : std::nullopt;
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
std::vector<Protocols::CmsisDap::Edbg::Avr::AvrResponse> EdbgInterface::requestAvrResponses() {
|
|
|
|
|
using Protocols::CmsisDap::Edbg::Avr::AvrResponseCommand;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
std::vector<Protocols::CmsisDap::Edbg::Avr::AvrResponse> responses;
|
|
|
|
|
AvrResponseCommand responseCommand;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-28 16:27:24 +00:00
|
|
|
auto avrResponse = this->sendCommandAndWaitForResponse(responseCommand);
|
|
|
|
|
responses.push_back(avrResponse);
|
2022-10-01 20:42:37 +01:00
|
|
|
const auto fragmentCount = avrResponse.fragmentCount;
|
2022-02-05 15:32:08 +00:00
|
|
|
|
|
|
|
|
while (responses.size() < fragmentCount) {
|
|
|
|
|
// There are more response packets
|
2022-02-28 16:27:24 +00:00
|
|
|
auto avrResponse = this->sendCommandAndWaitForResponse(responseCommand);
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (avrResponse.fragmentCount != fragmentCount) {
|
2022-02-05 15:32:08 +00:00
|
|
|
throw DeviceCommunicationFailure(
|
2022-03-01 19:58:04 +00:00
|
|
|
"Failed to fetch AvrResponse objects - invalid fragment count returned."
|
2022-02-05 15:32:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (avrResponse.fragmentCount == 0 && avrResponse.fragmentNumber == 0) {
|
2022-02-05 15:32:08 +00:00
|
|
|
throw DeviceCommunicationFailure(
|
2022-03-01 19:58:04 +00:00
|
|
|
"Failed to fetch AvrResponse objects - unexpected empty response"
|
2022-02-05 15:32:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-01 20:42:37 +01:00
|
|
|
if (avrResponse.fragmentNumber == 0) {
|
2022-02-05 15:32:08 +00:00
|
|
|
// End of response data ( &this packet can be ignored)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 16:27:24 +00:00
|
|
|
responses.push_back(avrResponse);
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
return responses;
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
2021-06-22 23:52:31 +01:00
|
|
|
}
|