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

130 lines
4.9 KiB
C++
Raw Normal View History

2021-04-04 21:04:12 +01:00
#pragma once
#include <memory>
#include <chrono>
#include <cstdint>
2021-04-04 21:04:12 +01:00
#include "src/DebugToolDrivers/USB/HID/HidInterface.hpp"
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/Response.hpp"
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/Command.hpp"
#include "src/DebugToolDrivers/Protocols/CMSIS-DAP/VendorSpecific/EDBG/AVR/AvrCommand.hpp"
#include "src/TargetController/Exceptions/DeviceCommunicationFailure.hpp"
2021-04-04 21:04:12 +01:00
namespace Bloom::DebugToolDrivers::Protocols::CmsisDap
{
/**
* The CmsisDapInterface class implements the CMSIS-DAP protocol.
*
* See https://www.keil.com/support/man/docs/dapdebug/dapdebug_introduction.htm for more on the CMSIS-DAP protocol.
*/
class CmsisDapInterface
{
public:
explicit CmsisDapInterface() = default;
2022-01-11 21:12:25 +00:00
virtual ~CmsisDapInterface() = default;
CmsisDapInterface(const CmsisDapInterface& other) = default;
CmsisDapInterface(CmsisDapInterface&& other) = default;
CmsisDapInterface& operator = (const CmsisDapInterface& other) = default;
CmsisDapInterface& operator = (CmsisDapInterface&& other) = default;
2021-04-04 21:04:12 +01:00
Usb::HidInterface& getUsbHidInterface() {
return this->usbHidInterface;
}
std::size_t getUsbHidInputReportSize() {
2021-04-04 21:04:12 +01:00
return this->usbHidInterface.getInputReportSize();
}
2021-06-11 23:59:17 +01:00
void setMinimumCommandTimeGap(std::chrono::milliseconds commandTimeGap) {
this->msSendCommandDelay = commandTimeGap;
2021-04-04 21:04:12 +01:00
}
/**
* Sends a CMSIS-DAP command to the device.
*
* @param cmsisDapCommand
*/
virtual void sendCommand(const Command& cmsisDapCommand);
2021-04-04 21:04:12 +01:00
/**
* Listens for a CMSIS-DAP response from the device.
*
* @TODO: There is a hard-coded timeout in this method. Review.
*
* @return
* An instance to ResponseType, which must be derived from the Response class. The instance is constructed via
* its raw buffer constructor: ResponseType(const std::vector<unsigned char>&).
2021-04-04 21:04:12 +01:00
*/
template<class ResponseType>
auto getResponse() {
static_assert(
std::is_base_of<Response, ResponseType>::value,
"CMSIS Response type must be derived from the Response class."
);
const auto rawResponse = this->getUsbHidInterface().read(15000);
if (rawResponse.empty()) {
throw Exceptions::DeviceCommunicationFailure("Empty CMSIS-DAP response received");
}
return ResponseType(rawResponse);
}
2021-04-04 21:04:12 +01:00
/**
* Sends a CMSIS-DAP command and waits for a response.
*
* @param cmsisDapCommand
*
* @return
* An instance of the ExpectedResponseType alias defined in CommandType.
* See the CmsisDapInterface::getResponse() template function for more.
2021-04-04 21:04:12 +01:00
*/
template<class CommandType>
auto sendCommandAndWaitForResponse(const CommandType& cmsisDapCommand) {
static_assert(
std::is_base_of<Command, CommandType>::value,
"CMSIS Command type must be derived from the Command class."
);
static_assert(
std::is_base_of<Response, typename CommandType::ExpectedResponseType>::value,
"CMSIS Command type must specify a valid expected response type, derived from the Response class."
);
this->sendCommand(cmsisDapCommand);
auto response = this->getResponse<typename CommandType::ExpectedResponseType>();
if (response.getResponseId() != cmsisDapCommand.getCommandId()) {
throw Exceptions::DeviceCommunicationFailure("Unexpected response to CMSIS-DAP command.");
}
return response;
}
private:
/**
* All CMSIS-DAP devices employ the USB HID interface for communication.
*
* For many CMSIS-DAP devices, the USB HID interface parameters (interface number, endpoint config, etc) vary
* amongst devices, so we'll need to be able to preActivationConfigure the CMSISDAPInterface from a
* higher level. For an example, see the constructor of the AtmelIce device class.
*/
Usb::HidInterface usbHidInterface = Usb::HidInterface();
/**
* Some CMSIS-DAP debug tools fail to operate properly when we send commands too quickly. Even if we've
* received a response from every previous command.
*
* Because of this, we may need to enforce a minimum time gap between sending CMSIS commands.
* Setting msSendCommandDelay to any value above 0 will enforce an x millisecond gap between each command
* being sent, where x is the value of msSendCommandDelay.
*/
std::chrono::milliseconds msSendCommandDelay = std::chrono::milliseconds(0);
2022-01-11 21:12:25 +00:00
std::int64_t lastCommandSentTimeStamp = 0;
2021-04-04 21:04:12 +01:00
};
}