From fbce1590ad988e1207030f4a4eee9763546d6bd8 Mon Sep 17 00:00:00 2001 From: Erhan Kurubas Date: Tue, 5 Nov 2024 16:22:48 +0100 Subject: [PATCH] target/riscv: reset dm after unexpected resets --- src/target/riscv/riscv-013.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index d32cbef1fb..6db0698723 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -794,6 +794,19 @@ static int dmstatus_read_timeout(struct target *target, uint32_t *dmstatus, DM_DMSTATUS, 0, timeout_sec, false, true); if (result != ERROR_OK) return result; + + /* ESPRESSIF */ + if (*dmstatus == 0) { + uint32_t dmcontrol; + /* after unexpected resets on some targets, dmcontrol could be inactive. */ + if (dm_read(target, &dmcontrol, DM_DMCONTROL) == ERROR_OK) { + if (!get_field(dmcontrol, DM_DMCONTROL_DMACTIVE)) { + LOG_TARGET_ERROR(target, "Debug Module is not active! dmcontrol=0x%" PRIx32, dmcontrol); + return dm_write(target, DM_DMCONTROL, DM_DMCONTROL_DMACTIVE); + } + } + } + int dmstatus_version = get_field(*dmstatus, DM_DMSTATUS_VERSION); if (dmstatus_version != 2 && dmstatus_version != 3) { LOG_ERROR("OpenOCD only supports Debug Module version 2 (0.13) and 3 (1.0), not "