From 418db1df99a7e24cce23daf221dc8151217ecc63 Mon Sep 17 00:00:00 2001 From: Nav Date: Sun, 6 Oct 2024 23:32:36 +0100 Subject: [PATCH] Added config struct for RISC-V debug translator implementation, and WCH debug tools. Also some tidying in the `DebugToolConfig` struct --- src/DebugToolDrivers/CMakeLists.txt | 2 + .../RiscVDebugSpec/DebugTranslator.cpp | 67 ++++++++++++------- .../RiscVDebugSpec/DebugTranslator.hpp | 6 ++ .../RiscVDebugSpec/DebugTranslatorConfig.cpp | 12 ++++ .../RiscVDebugSpec/DebugTranslatorConfig.hpp | 19 ++++++ src/DebugToolDrivers/WCH/WchLinkBase.cpp | 3 + src/DebugToolDrivers/WCH/WchLinkBase.hpp | 5 ++ .../WCH/WchLinkE/WchLinkE.cpp | 3 +- .../WCH/WchLinkE/WchLinkE.hpp | 4 +- .../WCH/WchLinkToolConfig.cpp | 16 +++++ .../WCH/WchLinkToolConfig.hpp | 20 ++++++ src/ProjectConfig.cpp | 10 +-- src/ProjectConfig.hpp | 6 +- .../TargetControllerComponent.cpp | 4 +- 14 files changed, 142 insertions(+), 35 deletions(-) create mode 100644 src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslatorConfig.cpp create mode 100644 src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslatorConfig.hpp create mode 100644 src/DebugToolDrivers/WCH/WchLinkToolConfig.cpp create mode 100644 src/DebugToolDrivers/WCH/WchLinkToolConfig.hpp diff --git a/src/DebugToolDrivers/CMakeLists.txt b/src/DebugToolDrivers/CMakeLists.txt index 216fb5e1..faf0f242 100755 --- a/src/DebugToolDrivers/CMakeLists.txt +++ b/src/DebugToolDrivers/CMakeLists.txt @@ -44,9 +44,11 @@ target_sources( # RISC-V debug tools ${CMAKE_CURRENT_SOURCE_DIR}/WCH/Protocols/WchLink/WchLinkInterface.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/WCH/WchLinkToolConfig.cpp ${CMAKE_CURRENT_SOURCE_DIR}/WCH/WchLinkBase.cpp ${CMAKE_CURRENT_SOURCE_DIR}/WCH/WchLinkE/WchLinkE.cpp # RISC-V Debug Translator implementation + ${CMAKE_CURRENT_SOURCE_DIR}/Protocols/RiscVDebugSpec/DebugTranslatorConfig.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Protocols/RiscVDebugSpec/DebugTranslator.cpp ) diff --git a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp index 5b14e4df..361cdf52 100644 --- a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp +++ b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp @@ -50,10 +50,12 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec DebugTranslator::DebugTranslator( DebugTransportModuleInterface& dtmInterface, + const DebugTranslatorConfig& config, const TargetDescriptionFile& targetDescriptionFile, const RiscVTargetConfig& targetConfig ) : dtmInterface(dtmInterface) + , config(config) , targetDescriptionFile(targetDescriptionFile) , targetConfig(targetConfig) {} @@ -130,12 +132,15 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec controlRegister.haltRequest = true; this->writeDebugModuleControlRegister(controlRegister); - - constexpr auto maxAttempts = 10; auto statusRegister = this->readDebugModuleStatusRegister(); - for (auto attempts = 1; !statusRegister.allHalted && attempts <= maxAttempts; ++attempts) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); + for ( + auto attempts = 1; + !statusRegister.allHalted + && (DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY * attempts) < this->config.targetResponseTimeout; + ++attempts + ) { + std::this_thread::sleep_for(DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY); statusRegister = this->readDebugModuleStatusRegister(); } @@ -154,12 +159,15 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec controlRegister.resumeRequest = true; this->writeDebugModuleControlRegister(controlRegister); - - constexpr auto maxAttempts = 10; auto statusRegister = this->readDebugModuleStatusRegister(); - for (auto attempts = 1; !statusRegister.allResumeAcknowledge && attempts <= maxAttempts; ++attempts) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); + for ( + auto attempts = 1; + !statusRegister.allResumeAcknowledge + && (DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY * attempts) < this->config.targetResponseTimeout; + ++attempts + ) { + std::this_thread::sleep_for(DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY); statusRegister = this->readDebugModuleStatusRegister(); } @@ -203,12 +211,15 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec controlRegister.ndmReset = false; this->writeDebugModuleControlRegister(controlRegister); - - constexpr auto maxAttempts = 10; auto statusRegister = this->readDebugModuleStatusRegister(); - for (auto attempts = 1; !statusRegister.allHaveReset && attempts <= maxAttempts; ++attempts) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); + for ( + auto attempts = 1; + !statusRegister.allHaveReset + && (DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY * attempts) < this->config.targetResponseTimeout; + ++attempts + ) { + std::this_thread::sleep_for(DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY); statusRegister = this->readDebugModuleStatusRegister(); } @@ -595,12 +606,15 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec controlRegister.selectedHartIndex = this->selectedHartIndex; this->writeDebugModuleControlRegister(controlRegister); - - constexpr auto maxAttempts = 10; controlRegister = this->readDebugModuleControlRegister(); - for (auto attempts = 1; !controlRegister.debugModuleActive && attempts <= maxAttempts; ++attempts) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); + for ( + auto attempts = 1; + !controlRegister.debugModuleActive + && (DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY * attempts) < this->config.targetResponseTimeout; + ++attempts + ) { + std::this_thread::sleep_for(DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY); controlRegister = this->readDebugModuleControlRegister(); } @@ -615,12 +629,15 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec controlRegister.selectedHartIndex = this->selectedHartIndex; this->writeDebugModuleControlRegister(controlRegister); - - constexpr auto maxAttempts = 10; controlRegister = this->readDebugModuleControlRegister(); - for (auto attempts = 1; controlRegister.debugModuleActive && attempts <= maxAttempts; ++attempts) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); + for ( + auto attempts = 1; + controlRegister.debugModuleActive + && (DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY * attempts) < this->config.targetResponseTimeout; + ++attempts + ) { + std::this_thread::sleep_for(DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY); controlRegister = this->readDebugModuleControlRegister(); } @@ -741,9 +758,13 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec return abstractStatusRegister.commandError; } - constexpr auto maxAttempts = 10; - for (auto attempts = 1; abstractStatusRegister.busy && attempts <= maxAttempts; ++attempts) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); + for ( + auto attempts = 1; + abstractStatusRegister.busy + && (DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY * attempts) < this->config.targetResponseTimeout; + ++attempts + ) { + std::this_thread::sleep_for(DebugTranslator::DEBUG_MODULE_RESPONSE_DELAY); abstractStatusRegister = this->readDebugModuleAbstractControlStatusRegister(); } diff --git a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.hpp b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.hpp index 9cd02337..a377d765 100644 --- a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.hpp +++ b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -11,6 +12,7 @@ #include "src/DebugToolDrivers/TargetInterfaces/RiscV/RiscVDebugInterface.hpp" #include "DebugTransportModuleInterface.hpp" +#include "DebugTranslatorConfig.hpp" #include "src/Targets/RiscV/TargetDescriptionFile.hpp" #include "src/Targets/RiscV/RiscVTargetConfig.hpp" @@ -40,6 +42,7 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec public: DebugTranslator( DebugTransportModuleInterface& dtmInterface, + const DebugTranslatorConfig& config, const ::Targets::RiscV::TargetDescriptionFile& targetDescriptionFile, const ::Targets::RiscV::RiscVTargetConfig& targetConfig ); @@ -85,7 +88,10 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec ) override; private: + static constexpr auto DEBUG_MODULE_RESPONSE_DELAY = std::chrono::microseconds{10}; + DebugTransportModuleInterface& dtmInterface; + const DebugTranslatorConfig& config; const ::Targets::RiscV::TargetDescriptionFile& targetDescriptionFile; const ::Targets::RiscV::RiscVTargetConfig& targetConfig; diff --git a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslatorConfig.cpp b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslatorConfig.cpp new file mode 100644 index 00000000..cd166fd3 --- /dev/null +++ b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslatorConfig.cpp @@ -0,0 +1,12 @@ +#include "DebugTranslatorConfig.hpp" + +namespace DebugToolDrivers::Protocols::RiscVDebugSpec +{ + DebugTranslatorConfig::DebugTranslatorConfig(const YAML::Node& configNode) { + if (configNode["targetResponseTimeout"]) { + this->targetResponseTimeout = std::chrono::microseconds{ + configNode["targetResponseTimeout"].as(this->targetResponseTimeout.count()) + }; + } + } +} diff --git a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslatorConfig.hpp b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslatorConfig.hpp new file mode 100644 index 00000000..574c9fe6 --- /dev/null +++ b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslatorConfig.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +namespace DebugToolDrivers::Protocols::RiscVDebugSpec +{ + struct DebugTranslatorConfig + { + public: + /** + * Microsecond timeout for the RISC-V target to action or respond to a command. + */ + std::chrono::microseconds targetResponseTimeout = std::chrono::microseconds{100}; + + DebugTranslatorConfig() = default; + explicit DebugTranslatorConfig(const YAML::Node& configNode); + }; +} diff --git a/src/DebugToolDrivers/WCH/WchLinkBase.cpp b/src/DebugToolDrivers/WCH/WchLinkBase.cpp index 4fd59958..9e25ff37 100644 --- a/src/DebugToolDrivers/WCH/WchLinkBase.cpp +++ b/src/DebugToolDrivers/WCH/WchLinkBase.cpp @@ -11,12 +11,14 @@ namespace DebugToolDrivers::Wch using Exceptions::DeviceInitializationFailure; WchLinkBase::WchLinkBase( + const DebugToolConfig& toolConfig, WchLinkVariant variant, std::uint16_t vendorId, std::uint16_t productId, std::uint8_t wchLinkUsbInterfaceNumber ) : UsbDevice(vendorId, productId) + , toolConfig(WchLinkToolConfig{toolConfig}) , variant(variant) , wchLinkUsbInterfaceNumber(wchLinkUsbInterfaceNumber) {} @@ -77,6 +79,7 @@ namespace DebugToolDrivers::Wch if (!this->wchRiscVTranslator) { this->wchRiscVTranslator = std::make_unique( *(this->wchLinkInterface.get()), + this->toolConfig.riscVDebugTranslatorConfig, targetDescriptionFile, targetConfig ); diff --git a/src/DebugToolDrivers/WCH/WchLinkBase.hpp b/src/DebugToolDrivers/WCH/WchLinkBase.hpp index 9013c9e4..2a173c24 100644 --- a/src/DebugToolDrivers/WCH/WchLinkBase.hpp +++ b/src/DebugToolDrivers/WCH/WchLinkBase.hpp @@ -8,6 +8,9 @@ #include "src/DebugToolDrivers/USB/UsbDevice.hpp" #include "src/DebugToolDrivers/USB/UsbInterface.hpp" +#include "WchLinkToolConfig.hpp" +#include "src/ProjectConfig.hpp" + #include "Protocols/WchLink/WchLinkInterface.hpp" #include "src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.hpp" @@ -20,6 +23,7 @@ namespace DebugToolDrivers::Wch { public: WchLinkBase( + const DebugToolConfig& toolConfig, WchLinkVariant variant, std::uint16_t vendorId, std::uint16_t productId, @@ -64,6 +68,7 @@ namespace DebugToolDrivers::Wch ) override; protected: + WchLinkToolConfig toolConfig; bool initialised = false; WchLinkVariant variant; diff --git a/src/DebugToolDrivers/WCH/WchLinkE/WchLinkE.cpp b/src/DebugToolDrivers/WCH/WchLinkE/WchLinkE.cpp index 7df8c0a8..75e57081 100644 --- a/src/DebugToolDrivers/WCH/WchLinkE/WchLinkE.cpp +++ b/src/DebugToolDrivers/WCH/WchLinkE/WchLinkE.cpp @@ -2,8 +2,9 @@ namespace DebugToolDrivers::Wch { - WchLinkE::WchLinkE() + WchLinkE::WchLinkE(const DebugToolConfig& toolConfig) : WchLinkBase( + toolConfig, WchLinkVariant::LINK_E_CH32V307, WchLinkE::USB_VENDOR_ID, WchLinkE::USB_PRODUCT_ID, diff --git a/src/DebugToolDrivers/WCH/WchLinkE/WchLinkE.hpp b/src/DebugToolDrivers/WCH/WchLinkE/WchLinkE.hpp index 7f1eb46d..c3db9875 100644 --- a/src/DebugToolDrivers/WCH/WchLinkE/WchLinkE.hpp +++ b/src/DebugToolDrivers/WCH/WchLinkE/WchLinkE.hpp @@ -5,6 +5,8 @@ #include "src/DebugToolDrivers/WCH/WchLinkBase.hpp" +#include "src/ProjectConfig.hpp" + namespace DebugToolDrivers::Wch { /** @@ -21,7 +23,7 @@ namespace DebugToolDrivers::Wch static const inline std::uint16_t USB_PRODUCT_ID = 0x8010; static const inline std::uint8_t WCH_LINK_INTERFACE_NUMBER = 0; - WchLinkE(); + WchLinkE(const DebugToolConfig& toolConfig); std::string getName() override { return "WCH-LinkE"; diff --git a/src/DebugToolDrivers/WCH/WchLinkToolConfig.cpp b/src/DebugToolDrivers/WCH/WchLinkToolConfig.cpp new file mode 100644 index 00000000..050562e9 --- /dev/null +++ b/src/DebugToolDrivers/WCH/WchLinkToolConfig.cpp @@ -0,0 +1,16 @@ +#include "WchLinkToolConfig.hpp" + +namespace DebugToolDrivers::Wch +{ + WchLinkToolConfig::WchLinkToolConfig(const DebugToolConfig& toolConfig) + : DebugToolConfig(toolConfig) + { + const auto& toolNode = toolConfig.toolNode; + + if (toolNode["riscVDebugTranslator"]) { + this->riscVDebugTranslatorConfig = Protocols::RiscVDebugSpec::DebugTranslatorConfig{ + toolNode["riscVDebugTranslator"] + }; + } + } +} diff --git a/src/DebugToolDrivers/WCH/WchLinkToolConfig.hpp b/src/DebugToolDrivers/WCH/WchLinkToolConfig.hpp new file mode 100644 index 00000000..08176e7f --- /dev/null +++ b/src/DebugToolDrivers/WCH/WchLinkToolConfig.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include "src/ProjectConfig.hpp" +#include "src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslatorConfig.hpp" + +namespace DebugToolDrivers::Wch +{ + /** + * Extending the generic DebugToolConfig struct to accommodate WCH-Link configuration parameters. + */ + struct WchLinkToolConfig: public DebugToolConfig + { + public: + Protocols::RiscVDebugSpec::DebugTranslatorConfig riscVDebugTranslatorConfig = {}; + + explicit WchLinkToolConfig(const DebugToolConfig& toolConfig); + }; +} diff --git a/src/ProjectConfig.cpp b/src/ProjectConfig.cpp index a1c5cc83..485ae767 100644 --- a/src/ProjectConfig.cpp +++ b/src/ProjectConfig.cpp @@ -212,19 +212,19 @@ TargetConfig::TargetConfig(const YAML::Node& targetNode) { this->targetNode = targetNode; } -DebugToolConfig::DebugToolConfig(const YAML::Node& debugToolNode) { - if (!debugToolNode.IsMap()) { +DebugToolConfig::DebugToolConfig(const YAML::Node& toolNode) { + if (!toolNode.IsMap()) { throw Exceptions::InvalidConfig{ "Invalid debug tool configuration provided - node must take the form of a YAML mapping." }; } - if (!debugToolNode["name"]) { + if (!toolNode["name"]) { throw Exceptions::InvalidConfig{"No debug tool name found."}; } - this->name = StringService::asciiToLower(debugToolNode["name"].as()); - this->debugToolNode = debugToolNode; + this->name = StringService::asciiToLower(toolNode["name"].as()); + this->toolNode = toolNode; } DebugServerConfig::DebugServerConfig(const YAML::Node& debugServerNode) { diff --git a/src/ProjectConfig.hpp b/src/ProjectConfig.hpp index 007f59ba..a66693a4 100644 --- a/src/ProjectConfig.hpp +++ b/src/ProjectConfig.hpp @@ -107,16 +107,16 @@ struct DebugToolConfig /** * For extracting any debug tool specific configuration. */ - YAML::Node debugToolNode; + YAML::Node toolNode; DebugToolConfig() = default; /** * Obtains config parameters from YAML node. * - * @param debugToolNode + * @param toolNode */ - explicit DebugToolConfig(const YAML::Node& debugToolNode); + explicit DebugToolConfig(const YAML::Node& toolNode); }; /** diff --git a/src/TargetController/TargetControllerComponent.cpp b/src/TargetController/TargetControllerComponent.cpp index 37065f62..080910f6 100644 --- a/src/TargetController/TargetControllerComponent.cpp +++ b/src/TargetController/TargetControllerComponent.cpp @@ -373,8 +373,8 @@ namespace TargetController }, { "wch-link-e", - [] { - return std::make_unique(); + [this] { + return std::make_unique(this->environmentConfig.debugToolConfig); } }, };