From 58281ec232b518b56da9597c3b2d238ce834e34a Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Wed, 2 Sep 2020 18:11:57 +0200 Subject: [PATCH] SWD Timing: Enhance SWD high time with low speed loss. On F103 no high time < 80 ns and no low time < 100 ns was seen with a scope --- src/platforms/common/swdptap.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/platforms/common/swdptap.c b/src/platforms/common/swdptap.c index 29278cc..d6e82d2 100644 --- a/src/platforms/common/swdptap.c +++ b/src/platforms/common/swdptap.c @@ -52,6 +52,8 @@ static void swdptap_turnaround(int dir) SWDIO_MODE_FLOAT(); gpio_set(SWCLK_PORT, SWCLK_PIN); gpio_set(SWCLK_PORT, SWCLK_PIN); + gpio_set(SWCLK_PORT, SWCLK_PIN); + gpio_set(SWCLK_PORT, SWCLK_PIN); gpio_clear(SWCLK_PORT, SWCLK_PIN); if(dir == SWDIO_STATUS_DRIVE) SWDIO_MODE_DRIVE(); @@ -68,6 +70,9 @@ static uint32_t swdptap_seq_in(int ticks) int res; res = gpio_get(SWDIO_PORT, SWDIO_PIN); gpio_set(SWCLK_PORT, SWCLK_PIN); + gpio_set(SWCLK_PORT, SWCLK_PIN); + gpio_set(SWCLK_PORT, SWCLK_PIN); + gpio_set(SWCLK_PORT, SWCLK_PIN); if (res) ret |= index; index <<= 1; @@ -90,20 +95,22 @@ static bool swdptap_seq_in_parity(uint32_t *ret, int ticks) swdptap_turnaround(SWDIO_STATUS_FLOAT); while (len--) { - gpio_clear(SWCLK_PORT, SWCLK_PIN); bit = gpio_get(SWDIO_PORT, SWDIO_PIN); gpio_set(SWCLK_PORT, SWCLK_PIN); - if (bit) { + gpio_set(SWCLK_PORT, SWCLK_PIN); + gpio_set(SWCLK_PORT, SWCLK_PIN); + if (bit) res |= index; - } index <<= 1; + gpio_clear(SWCLK_PORT, SWCLK_PIN); } - gpio_clear(SWCLK_PORT, SWCLK_PIN); int parity = __builtin_popcount(res); bit = gpio_get(SWDIO_PORT, SWDIO_PIN); gpio_set(SWCLK_PORT, SWCLK_PIN); if (bit) parity++; + else + gpio_set(SWCLK_PORT, SWCLK_PIN); gpio_clear(SWCLK_PORT, SWCLK_PIN); #ifdef DEBUG_SWD_BITS for (int i = 0; i < len; i++) @@ -115,42 +122,39 @@ static bool swdptap_seq_in_parity(uint32_t *ret, int ticks) static void swdptap_seq_out(uint32_t MS, int ticks) { - int data = MS & 1; #ifdef DEBUG_SWD_BITS for (int i = 0; i < ticks; i++) DEBUG("%d", (MS & (1 << i)) ? 1 : 0); #endif swdptap_turnaround(SWDIO_STATUS_DRIVE); + gpio_set_val(SWDIO_PORT, SWDIO_PIN, MS & 1); while (ticks--) { - gpio_set_val(SWDIO_PORT, SWDIO_PIN, data); - gpio_clear(SWCLK_PORT, SWCLK_PIN); - MS >>= 1; - data = MS & 1; gpio_set(SWCLK_PORT, SWCLK_PIN); + MS >>= 1; + gpio_set_val(SWDIO_PORT, SWDIO_PIN, MS & 1); + gpio_clear(SWCLK_PORT, SWCLK_PIN); + gpio_clear(SWCLK_PORT, SWCLK_PIN); } - gpio_clear(SWCLK_PORT, SWCLK_PIN); } static void swdptap_seq_out_parity(uint32_t MS, int ticks) { int parity = __builtin_popcount(MS); - int data = MS & 1; #ifdef DEBUG_SWD_BITS for (int i = 0; i < ticks; i++) DEBUG("%d", (MS & (1 << i)) ? 1 : 0); #endif swdptap_turnaround(SWDIO_STATUS_DRIVE); - gpio_set_val(SWDIO_PORT, SWDIO_PIN, data); + gpio_set_val(SWDIO_PORT, SWDIO_PIN, MS & 1); MS >>= 1; while (ticks--) { - data = MS & 1; gpio_set(SWCLK_PORT, SWCLK_PIN); - gpio_set_val(SWDIO_PORT, SWDIO_PIN, data); + gpio_set_val(SWDIO_PORT, SWDIO_PIN, MS & 1); MS >>= 1; gpio_clear(SWCLK_PORT, SWCLK_PIN); } gpio_set_val(SWDIO_PORT, SWDIO_PIN, parity & 1); - gpio_clear(SWCLK_PORT, SWCLK_PIN); + gpio_set(SWCLK_PORT, SWCLK_PIN); gpio_set(SWCLK_PORT, SWCLK_PIN); gpio_set(SWCLK_PORT, SWCLK_PIN); gpio_clear(SWCLK_PORT, SWCLK_PIN);