Skip to content

Commit 6c3c818

Browse files
maxd-nordicrlubos
authored andcommitted
[nrf fromtree] drivers: dp: swdp_bitbang: Update SWD clock calculation
This patch updates the SWD clock calculation to the latest behavior of DAPLink. Signed-off-by: Maximilian Deubel <[email protected]> (cherry picked from commit fd686a2)
1 parent 0a8d32e commit 6c3c818

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

drivers/dp/swdp_bitbang.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626
#include <zephyr/logging/log.h>
2727
LOG_MODULE_REGISTER(swdp, CONFIG_DP_DRIVER_LOG_LEVEL);
2828

29-
#define CLOCK_DELAY(swclk_freq, port_write_cycles) \
30-
((CPU_CLOCK / 2 / swclk_freq) - port_write_cycles)
29+
#define MAX_SWJ_CLOCK(delay_cycles, port_write_cycles) \
30+
((CPU_CLOCK / 2U) / (port_write_cycles + delay_cycles))
3131

3232
/*
3333
* Default SWCLK frequency in Hz.
3434
* sw_clock can be used to overwrite this default value.
3535
*/
3636
#define SWDP_DEFAULT_SWCLK_FREQUENCY 1000000U
3737

38+
#define DELAY_FAST_CYCLES 2U
3839
#define DELAY_SLOW_CYCLES 3U
3940

4041
struct sw_config {
@@ -528,14 +529,19 @@ static int sw_set_clock(const struct device *dev, const uint32_t clock)
528529
struct sw_cfg_data *sw_data = dev->data;
529530
uint32_t delay;
530531

531-
sw_data->fast_clock = false;
532-
delay = ((CPU_CLOCK / 2U) + (clock - 1U)) / clock;
533-
534-
if (delay > config->port_write_cycles) {
535-
delay -= config->port_write_cycles;
536-
delay = (delay + (DELAY_SLOW_CYCLES - 1U)) / DELAY_SLOW_CYCLES;
537-
} else {
532+
if (clock >= MAX_SWJ_CLOCK(DELAY_FAST_CYCLES, config->port_write_cycles)) {
533+
sw_data->fast_clock = true;
538534
delay = 1U;
535+
} else {
536+
sw_data->fast_clock = false;
537+
538+
delay = ((CPU_CLOCK / 2U) + (clock - 1U)) / clock;
539+
if (delay > config->port_write_cycles) {
540+
delay -= config->port_write_cycles;
541+
delay = (delay + (DELAY_SLOW_CYCLES - 1U)) / DELAY_SLOW_CYCLES;
542+
} else {
543+
delay = 1U;
544+
}
539545
}
540546

541547
sw_data->clock_delay = delay;
@@ -684,8 +690,7 @@ static int sw_gpio_init(const struct device *dev)
684690
sw_data->turnaround = 1U;
685691
sw_data->data_phase = false;
686692
sw_data->fast_clock = false;
687-
sw_data->clock_delay = CLOCK_DELAY(SWDP_DEFAULT_SWCLK_FREQUENCY,
688-
config->port_write_cycles);
693+
sw_set_clock(dev, SWDP_DEFAULT_SWCLK_FREQUENCY);
689694

690695
return 0;
691696
}

0 commit comments

Comments
 (0)