Files
BloomPatched/src/DebugToolDrivers/Protocols/CMSIS-DAP/CmsisDapInterface.cpp

53 lines
1.9 KiB
C++
Raw Normal View History

#include "CmsisDapInterface.hpp"
2021-04-04 21:04:12 +01:00
#include <thread>
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/Command.hpp"
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/Response.hpp"
#include "src/TargetController/Exceptions/DeviceCommunicationFailure.hpp"
2021-04-04 21:04:12 +01:00
namespace Bloom::DebugToolDrivers::Protocols::CmsisDap
{
using namespace Bloom::Exceptions;
2021-04-04 21:04:12 +01:00
void CmsisDapInterface::sendCommand(const Command& cmsisDapCommand) {
if (this->msSendCommandDelay.count() > 0) {
using namespace std::chrono;
std::int64_t now = duration_cast<milliseconds>(high_resolution_clock::now().time_since_epoch()).count();
std::int64_t difference = (now - this->lastCommandSentTimeStamp);
2021-04-04 21:04:12 +01:00
if (difference < this->msSendCommandDelay.count()) {
std::this_thread::sleep_for(milliseconds(this->msSendCommandDelay.count() - difference));
}
this->lastCommandSentTimeStamp = now;
2021-04-04 21:04:12 +01:00
}
this->getUsbHidInterface().write(static_cast<std::vector<unsigned char>>(cmsisDapCommand));
2021-04-04 21:04:12 +01:00
}
std::unique_ptr<Response> CmsisDapInterface::getResponse() {
auto rawResponse = this->getUsbHidInterface().read(10000);
2021-04-04 21:04:12 +01:00
if (rawResponse.empty()) {
throw DeviceCommunicationFailure("Empty CMSIS-DAP response received");
}
2021-04-04 21:04:12 +01:00
auto response = std::make_unique<Response>(Response());
response->init(rawResponse);
return response;
2021-04-04 21:04:12 +01:00
}
std::unique_ptr<Response> CmsisDapInterface::sendCommandAndWaitForResponse(const Command& cmsisDapCommand) {
this->sendCommand(cmsisDapCommand);
auto response = this->getResponse();
2021-04-04 21:04:12 +01:00
if (response->getResponseId() != cmsisDapCommand.getCommandId()) {
// This response is not what we were expecting
throw DeviceCommunicationFailure("Unexpected response to CMSIS-DAP command.");
}
2021-04-04 21:04:12 +01:00
return response;
2021-04-04 21:04:12 +01:00
}
}