2021-11-27 14:54:43 +00:00
|
|
|
#include "CuriosityNano.hpp"
|
|
|
|
|
|
|
|
|
|
#include "src/TargetController/Exceptions/DeviceFailure.hpp"
|
|
|
|
|
#include "src/TargetController/Exceptions/DeviceInitializationFailure.hpp"
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
namespace Bloom::DebugToolDrivers
|
|
|
|
|
{
|
|
|
|
|
using namespace Protocols::CmsisDap::Edbg::Avr;
|
|
|
|
|
using namespace Bloom::Exceptions;
|
|
|
|
|
|
2022-03-16 17:39:08 +00:00
|
|
|
using Protocols::CmsisDap::Edbg::EdbgTargetPowerManagementInterface;
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void CuriosityNano::init() {
|
|
|
|
|
UsbDevice::init();
|
|
|
|
|
|
|
|
|
|
// TODO: Move away from hard-coding the CMSIS-DAP/EDBG interface number
|
|
|
|
|
auto& usbHidInterface = this->getEdbgInterface().getUsbHidInterface();
|
|
|
|
|
usbHidInterface.setNumber(0);
|
|
|
|
|
usbHidInterface.setLibUsbDevice(this->libUsbDevice);
|
|
|
|
|
usbHidInterface.setLibUsbDeviceHandle(this->libUsbDeviceHandle);
|
|
|
|
|
usbHidInterface.setVendorId(this->vendorId);
|
|
|
|
|
usbHidInterface.setProductId(this->productId);
|
|
|
|
|
|
|
|
|
|
if (!usbHidInterface.isInitialised()) {
|
|
|
|
|
usbHidInterface.detachKernelDriver();
|
|
|
|
|
usbHidInterface.init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->getEdbgInterface().setMinimumCommandTimeGap(std::chrono::milliseconds(35));
|
|
|
|
|
|
|
|
|
|
if (!this->sessionStarted) {
|
|
|
|
|
this->startSession();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 17:39:08 +00:00
|
|
|
this->targetPowerManagementInterface = std::make_unique<EdbgTargetPowerManagementInterface>(
|
|
|
|
|
this->edbgInterface
|
|
|
|
|
);
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->edbgAvr8Interface = std::make_unique<EdbgAvr8Interface>(this->edbgInterface);
|
2022-03-09 21:54:58 +00:00
|
|
|
this->edbgAvrIspInterface = std::make_unique<EdbgAvrIspInterface>(this->edbgInterface);
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->setInitialised(true);
|
2021-11-27 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void CuriosityNano::close() {
|
|
|
|
|
if (this->sessionStarted) {
|
|
|
|
|
this->endSession();
|
|
|
|
|
}
|
2021-11-27 14:54:43 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->getEdbgInterface().getUsbHidInterface().close();
|
|
|
|
|
UsbDevice::close();
|
2021-11-27 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
std::string CuriosityNano::getSerialNumber() {
|
|
|
|
|
using namespace CommandFrames::Discovery;
|
2022-03-01 20:18:45 +00:00
|
|
|
using ResponseFrames::Discovery::ResponseId;
|
2021-11-27 14:54:43 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
|
|
|
|
|
Query(QueryContext::SERIAL_NUMBER)
|
|
|
|
|
);
|
2021-11-27 14:54:43 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (response.getResponseId() != ResponseId::OK) {
|
|
|
|
|
throw DeviceInitializationFailure(
|
|
|
|
|
"Failed to fetch serial number from device - invalid Discovery Protocol response ID."
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-11-27 14:54:43 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
auto data = response.getPayloadData();
|
|
|
|
|
return std::string(data.begin(), data.end());
|
2021-11-27 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void CuriosityNano::startSession() {
|
|
|
|
|
using namespace CommandFrames::HouseKeeping;
|
2022-03-01 20:25:16 +00:00
|
|
|
using ResponseFrames::HouseKeeping::ResponseId;
|
2021-11-27 14:54:43 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
|
|
|
|
|
StartSession()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (response.getResponseId() == ResponseId::FAILED) {
|
|
|
|
|
// Failed response returned!
|
|
|
|
|
throw DeviceInitializationFailure("Failed to start session with Curiosity Nano!");
|
|
|
|
|
}
|
2021-11-27 14:54:43 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->sessionStarted = true;
|
2021-11-27 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void CuriosityNano::endSession() {
|
|
|
|
|
using namespace CommandFrames::HouseKeeping;
|
2022-03-01 20:25:16 +00:00
|
|
|
using ResponseFrames::HouseKeeping::ResponseId;
|
2021-11-27 14:54:43 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
auto response = this->getEdbgInterface().sendAvrCommandFrameAndWaitForResponseFrame(
|
|
|
|
|
EndSession()
|
|
|
|
|
);
|
2021-11-27 14:54:43 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (response.getResponseId() == ResponseId::FAILED) {
|
|
|
|
|
// Failed response returned!
|
|
|
|
|
throw DeviceFailure("Failed to end session with Curiosity Nano!");
|
|
|
|
|
}
|
2021-11-27 14:54:43 +00:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->sessionStarted = false;
|
|
|
|
|
}
|
2021-11-27 14:54:43 +00:00
|
|
|
}
|