From 76a020770193ebdb5229322ae33eb9d145ac7690 Mon Sep 17 00:00:00 2001 From: Nav Date: Sun, 11 Dec 2022 23:25:15 +0000 Subject: [PATCH] Added EraseTargetMemory TC command --- .../Gdb/AvrGdb/CommandPackets/FlashErase.cpp | 22 +++---------- .../Commands/CommandTypes.hpp | 1 + .../Commands/EraseTargetMemory.hpp | 33 +++++++++++++++++++ .../TargetControllerComponent.cpp | 20 +++++++++++ .../TargetControllerComponent.hpp | 2 ++ .../TargetControllerConsole.cpp | 9 +++++ .../TargetControllerConsole.hpp | 7 ++++ 7 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 src/TargetController/Commands/EraseTargetMemory.hpp diff --git a/src/DebugServer/Gdb/AvrGdb/CommandPackets/FlashErase.cpp b/src/DebugServer/Gdb/AvrGdb/CommandPackets/FlashErase.cpp index faa05ac6..0d9bfab4 100644 --- a/src/DebugServer/Gdb/AvrGdb/CommandPackets/FlashErase.cpp +++ b/src/DebugServer/Gdb/AvrGdb/CommandPackets/FlashErase.cpp @@ -27,7 +27,7 @@ namespace Bloom::DebugServer::Gdb::AvrGdb::CommandPackets * The flash erase ('vFlashErase') packet consists of two segments, an address and a length, separated by a * comma. */ - auto packetSegments = packetString.split(","); + const auto packetSegments = packetString.split(","); if (packetSegments.size() != 2) { throw Exception( "Unexpected number of segments in packet data: " + std::to_string(packetSegments.size()) @@ -52,24 +52,12 @@ namespace Bloom::DebugServer::Gdb::AvrGdb::CommandPackets Logger::debug("Handling FlashErase packet"); try { - const auto flashPageSize = debugSession.gdbTargetDescriptor.targetDescriptor.memoryDescriptorsByType.at( - Targets::TargetMemoryType::FLASH - ).pageSize.value(); - - if ((this->bytes % flashPageSize) != 0) { - throw Exception( - "Invalid erase size (" + std::to_string(this->bytes) + " bytes) provided by GDB - must be a " - "multiple of the target's flash page size (" + std::to_string(flashPageSize) + " bytes)" - ); - } - targetControllerConsole.enableProgrammingMode(); - targetControllerConsole.writeMemory( - Targets::TargetMemoryType::FLASH, - this->startAddress, - Targets::TargetMemoryBuffer(this->bytes, 0xFF) - ); + Logger::warning("Erasing entire chip, in preparation for programming"); + + // We don't erase a specific address range - we just erase the entire program memory. + targetControllerConsole.eraseMemory(Targets::TargetMemoryType::FLASH); debugSession.connection.writePacket(OkResponsePacket()); diff --git a/src/TargetController/Commands/CommandTypes.hpp b/src/TargetController/Commands/CommandTypes.hpp index 0d35aefb..1b5dcfa1 100644 --- a/src/TargetController/Commands/CommandTypes.hpp +++ b/src/TargetController/Commands/CommandTypes.hpp @@ -18,6 +18,7 @@ namespace Bloom::TargetController::Commands WRITE_TARGET_REGISTERS, READ_TARGET_MEMORY, WRITE_TARGET_MEMORY, + ERASE_TARGET_MEMORY, GET_TARGET_STATE, STEP_TARGET_EXECUTION, SET_BREAKPOINT, diff --git a/src/TargetController/Commands/EraseTargetMemory.hpp b/src/TargetController/Commands/EraseTargetMemory.hpp new file mode 100644 index 00000000..e98708a2 --- /dev/null +++ b/src/TargetController/Commands/EraseTargetMemory.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include "Command.hpp" + +#include "src/Targets/TargetMemory.hpp" + +namespace Bloom::TargetController::Commands +{ + class EraseTargetMemory: public Command + { + public: + static constexpr CommandType type = CommandType::ERASE_TARGET_MEMORY; + static const inline std::string name = "EraseTargetMemory"; + + Targets::TargetMemoryType memoryType; + + EraseTargetMemory(Targets::TargetMemoryType memoryType) + : memoryType(memoryType) + {}; + + [[nodiscard]] CommandType getType() const override { + return EraseTargetMemory::type; + } + + [[nodiscard]] bool requiresStoppedTargetState() const override { + return true; + } + + [[nodiscard]] bool requiresDebugMode() const override { + return this->memoryType == Targets::TargetMemoryType::RAM; + } + }; +} diff --git a/src/TargetController/TargetControllerComponent.cpp b/src/TargetController/TargetControllerComponent.cpp index d0959fbb..8498fa02 100644 --- a/src/TargetController/TargetControllerComponent.cpp +++ b/src/TargetController/TargetControllerComponent.cpp @@ -37,6 +37,7 @@ namespace Bloom::TargetController using Commands::WriteTargetRegisters; using Commands::ReadTargetMemory; using Commands::WriteTargetMemory; + using Commands::EraseTargetMemory; using Commands::StepTargetExecution; using Commands::SetBreakpoint; using Commands::RemoveBreakpoint; @@ -220,6 +221,10 @@ namespace Bloom::TargetController std::bind(&TargetControllerComponent::handleWriteTargetMemory, this, std::placeholders::_1) ); + this->registerCommandHandler( + std::bind(&TargetControllerComponent::handleEraseTargetMemory, this, std::placeholders::_1) + ); + this->registerCommandHandler( std::bind(&TargetControllerComponent::handleStepTargetExecution, this, std::placeholders::_1) ); @@ -930,6 +935,21 @@ namespace Bloom::TargetController return std::make_unique(); } + std::unique_ptr TargetControllerComponent::handleEraseTargetMemory(EraseTargetMemory& command) { + const auto& targetDescriptor = this->getTargetDescriptor(); + + if ( + command.memoryType == this->getTargetDescriptor().programMemoryType + && !this->target->programmingModeEnabled() + ) { + throw Exception("Cannot erase program memory - programming mode not enabled."); + } + + this->target->eraseMemory(command.memoryType); + + return std::make_unique(); + } + std::unique_ptr TargetControllerComponent::handleStepTargetExecution(StepTargetExecution& command) { if (command.fromProgramCounter.has_value()) { this->target->setProgramCounter(command.fromProgramCounter.value()); diff --git a/src/TargetController/TargetControllerComponent.hpp b/src/TargetController/TargetControllerComponent.hpp index 881040f9..4444b4d1 100644 --- a/src/TargetController/TargetControllerComponent.hpp +++ b/src/TargetController/TargetControllerComponent.hpp @@ -32,6 +32,7 @@ #include "Commands/WriteTargetRegisters.hpp" #include "Commands/ReadTargetMemory.hpp" #include "Commands/WriteTargetMemory.hpp" +#include "Commands/EraseTargetMemory.hpp" #include "Commands/StepTargetExecution.hpp" #include "Commands/SetBreakpoint.hpp" #include "Commands/RemoveBreakpoint.hpp" @@ -350,6 +351,7 @@ namespace Bloom::TargetController std::unique_ptr handleWriteTargetRegisters(Commands::WriteTargetRegisters& command); std::unique_ptr handleReadTargetMemory(Commands::ReadTargetMemory& command); std::unique_ptr handleWriteTargetMemory(Commands::WriteTargetMemory& command); + std::unique_ptr handleEraseTargetMemory(Commands::EraseTargetMemory& command); std::unique_ptr handleStepTargetExecution(Commands::StepTargetExecution& command); std::unique_ptr handleSetBreakpoint(Commands::SetBreakpoint& command); std::unique_ptr handleRemoveBreakpoint(Commands::RemoveBreakpoint& command); diff --git a/src/TargetController/TargetControllerConsole.cpp b/src/TargetController/TargetControllerConsole.cpp index 0ddce64c..958febe9 100644 --- a/src/TargetController/TargetControllerConsole.cpp +++ b/src/TargetController/TargetControllerConsole.cpp @@ -13,6 +13,7 @@ #include "Commands/WriteTargetRegisters.hpp" #include "Commands/ReadTargetMemory.hpp" #include "Commands/WriteTargetMemory.hpp" +#include "Commands/EraseTargetMemory.hpp" #include "Commands/StepTargetExecution.hpp" #include "Commands/SetBreakpoint.hpp" #include "Commands/RemoveBreakpoint.hpp" @@ -38,6 +39,7 @@ namespace Bloom::TargetController using Commands::WriteTargetRegisters; using Commands::ReadTargetMemory; using Commands::WriteTargetMemory; + using Commands::EraseTargetMemory; using Commands::StepTargetExecution; using Commands::SetBreakpoint; using Commands::RemoveBreakpoint; @@ -190,6 +192,13 @@ namespace Bloom::TargetController ); } + void TargetControllerConsole::eraseMemory(Targets::TargetMemoryType memoryType) { + this->commandManager.sendCommandAndWaitForResponse( + std::make_unique(memoryType), + this->defaultTimeout + ); + } + void TargetControllerConsole::setBreakpoint(TargetBreakpoint breakpoint) { this->commandManager.sendCommandAndWaitForResponse( std::make_unique(breakpoint), diff --git a/src/TargetController/TargetControllerConsole.hpp b/src/TargetController/TargetControllerConsole.hpp index dd7f5a83..540928a0 100644 --- a/src/TargetController/TargetControllerConsole.hpp +++ b/src/TargetController/TargetControllerConsole.hpp @@ -138,6 +138,13 @@ namespace Bloom::TargetController const Targets::TargetMemoryBuffer& buffer ); + /** + * Requests the TargetController to erase the given target memory type. + * + * @param memoryType + */ + void eraseMemory(Targets::TargetMemoryType memoryType); + /** * Requests the TargetController to set a breakpoint on the target. *