From e0fc21a2a715893331f49d9d838bc0d4a69cf84c Mon Sep 17 00:00:00 2001 From: Paul Fertser Date: Sun, 5 May 2013 00:46:47 +0400 Subject: [PATCH] adiv5_swdp: allow to connect to a sleeping target by pulling SRST This patch introduces a new command, "connect_srst [enable|disable]" which allows to enable special mode in which SRST would be pulled low before the SWD scan till attaching to a target. Since on Cortex-Mx the SRST signal doesn't gate JTAG and SWD, it's possible to connect to a target while holding reset, ask it to stop at reset vector and only then deassert reset, thus allowing to attach to the kind of firmware that goes immediately to sleep or disables debugging by other means early on start. Tested on an STM32VLDiscovery board with STM32F100 configured to go to STOP mode and executing WFI in the very beginning of main(). Signed-off-by: Paul Fertser --- src/adiv5_swdp.c | 3 +++ src/command.c | 13 +++++++++++++ src/cortexm.c | 8 ++++++-- src/include/jtagtap.h | 2 +- src/include/target.h | 1 + src/platforms/libftdi/jtagtap.c | 2 +- src/platforms/stm32/jtagtap.c | 15 +++++++++------ src/target.c | 1 + 8 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/adiv5_swdp.c b/src/adiv5_swdp.c index 06d5446..669bf42 100644 --- a/src/adiv5_swdp.c +++ b/src/adiv5_swdp.c @@ -27,6 +27,7 @@ #include "adiv5.h" #include "swdptap.h" +#include "jtagtap.h" #include "command.h" @@ -53,6 +54,8 @@ int adiv5_swdp_scan(void) ADIv5_DP_t *dp = (void*)calloc(1, sizeof(*dp)); swdptap_init(); + if(connect_assert_srst) + jtagtap_srst(true); /* will be deasserted after attach */ /* Read the SW-DP IDCODE register to syncronise */ /* This could be done with adiv_swdp_low_access(), but this doesn't * allow the ack to be checked here. */ diff --git a/src/command.c b/src/command.c index e1ed837..e9f7096 100644 --- a/src/command.c +++ b/src/command.c @@ -46,6 +46,7 @@ static bool cmd_jtag_scan(target *t, int argc, char **argv); static bool cmd_swdp_scan(void); static bool cmd_targets(target *t); static bool cmd_morse(void); +static bool cmd_connect_srst(target *t, int argc, const char **argv); #ifdef PLATFORM_HAS_TRACESWO static bool cmd_traceswo(void); #endif @@ -57,6 +58,7 @@ const struct command_s cmd_list[] = { {"swdp_scan", (cmd_handler)cmd_swdp_scan, "Scan SW-DP for devices" }, {"targets", (cmd_handler)cmd_targets, "Display list of available targets" }, {"morse", (cmd_handler)cmd_morse, "Display morse error message" }, + {"connect_srst", (cmd_handler)cmd_connect_srst, "Configure connect under SRST: (enable|disable)" }, #ifdef PLATFORM_HAS_TRACESWO {"traceswo", (cmd_handler)cmd_traceswo, "Start trace capture" }, #endif @@ -209,6 +211,17 @@ bool cmd_morse(void) return true; } +static bool cmd_connect_srst(target *t, int argc, const char **argv) +{ + (void)t; + if (argc == 1) + gdb_outf("Assert SRST during connect: %s\n", + connect_assert_srst ? "enabled" : "disabled"); + else + connect_assert_srst = !strcmp(argv[1], "enable"); + return true; +} + #ifdef PLATFORM_HAS_TRACESWO static bool cmd_traceswo(void) { diff --git a/src/cortexm.c b/src/cortexm.c index 9ec96d6..36319ee 100644 --- a/src/cortexm.c +++ b/src/cortexm.c @@ -385,7 +385,7 @@ cortexm_attach(struct target_s *target) target_halt_request(target); tries = 10; - while(!target_halt_wait(target) && --tries) + while(!connect_assert_srst && !target_halt_wait(target) && --tries) platform_delay(2); if(!tries) return false; @@ -429,6 +429,9 @@ cortexm_attach(struct target_s *target) target->clear_hw_wp = cortexm_clear_hw_wp; target->check_hw_wp = cortexm_check_hw_wp; + if(connect_assert_srst) + jtagtap_srst(false); + return true; } @@ -546,7 +549,8 @@ cortexm_reset(struct target_s *target) { ADIv5_AP_t *ap = adiv5_target_ap(target); - jtagtap_srst(); + jtagtap_srst(true); + jtagtap_srst(false); /* Read DHCSR here to clear S_RESET_ST bit before reset */ adiv5_ap_mem_read(ap, CORTEXM_DHCSR); diff --git a/src/include/jtagtap.h b/src/include/jtagtap.h index 9fe3e0d..f0588a0 100644 --- a/src/include/jtagtap.h +++ b/src/include/jtagtap.h @@ -29,7 +29,7 @@ int jtagtap_init(void); void jtagtap_reset(void); -void jtagtap_srst(void); +void jtagtap_srst(bool assert); uint8_t jtagtap_next(const uint8_t TMS, const uint8_t TDI); /* tap_next executes one state transision in the JTAG TAP state machine: diff --git a/src/include/target.h b/src/include/target.h index 31501f6..cd3079c 100644 --- a/src/include/target.h +++ b/src/include/target.h @@ -188,6 +188,7 @@ struct target_command_s { }; extern target *target_list; +extern bool connect_assert_srst; target *target_new(unsigned size); void target_list_free(void); diff --git a/src/platforms/libftdi/jtagtap.c b/src/platforms/libftdi/jtagtap.c index c914646..77e1867 100644 --- a/src/platforms/libftdi/jtagtap.c +++ b/src/platforms/libftdi/jtagtap.c @@ -67,7 +67,7 @@ void jtagtap_reset(void) jtagtap_soft_reset(); } -void jtagtap_srst(void) +void jtagtap_srst(bool assert) { platform_buffer_flush(); //ftdi_write_data(ftdic, "\x80\x88\xAB", 3); diff --git a/src/platforms/stm32/jtagtap.c b/src/platforms/stm32/jtagtap.c index d6e298b..78dd300 100644 --- a/src/platforms/stm32/jtagtap.c +++ b/src/platforms/stm32/jtagtap.c @@ -50,13 +50,16 @@ void jtagtap_reset(void) jtagtap_soft_reset(); } -void jtagtap_srst(void) +void jtagtap_srst(bool assert) { -#ifdef SRST_PORT - volatile int i; - gpio_set(SRST_PORT, SRST_PIN); - for(i = 0; i < 10000; i++) asm("nop"); - gpio_clear(SRST_PORT, SRST_PIN); + (void)assert; +#ifdef SRST_SET_VAL + SRST_SET_VAL(assert); + if(assert) { + int i; + for(i = 0; i < 10000; i++) + asm volatile("nop"); + } #endif } diff --git a/src/target.c b/src/target.c index b46f9e3..2a68f23 100644 --- a/src/target.c +++ b/src/target.c @@ -24,6 +24,7 @@ #include target *target_list = NULL; +bool connect_assert_srst; target *target_new(unsigned size) {