Skip to content

Commit

Permalink
swdptap: Replace popcount with parity
Browse files Browse the repository at this point in the history
* BMF on Cortex-M3 relies on libgcc2 software builtin impls,
  not the x86-64-v2 popcnt insn, so adjust accordingly (parity is cheaper)
* More type- and const-correctness (bmp_gpio_get/set)
  • Loading branch information
ALTracer committed Dec 31, 2023
1 parent 4e42c24 commit fe3405f
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/platforms/common/swdptap.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ static bool swdptap_seq_in_parity(uint32_t *ret, size_t clock_cycles)
for (volatile uint32_t counter = target_clk_divider + 1; counter > 0; --counter)
continue;

size_t parity = __builtin_popcount(result);
parity += gpio_get(SWDIO_IN_PORT, SWDIO_IN_PIN) ? 1U : 0U;
const bool parity = __builtin_parity(result);
const bool bit = gpio_get(SWDIO_IN_PORT, SWDIO_IN_PIN);

gpio_set(SWCLK_PORT, SWCLK_PIN);
for (volatile uint32_t counter = target_clk_divider + 1; counter > 0; --counter)
Expand All @@ -140,7 +140,7 @@ static bool swdptap_seq_in_parity(uint32_t *ret, size_t clock_cycles)
*ret = result;
/* Terminate the read cycle now */
swdptap_turnaround(SWDIO_STATUS_DRIVE);
return parity & 1U;
return parity != bit;
}

static void swdptap_seq_out_clk_delay(uint32_t tms_states, size_t clock_cycles) __attribute__((optimize(3)));
Expand Down Expand Up @@ -182,9 +182,9 @@ static void swdptap_seq_out(const uint32_t tms_states, const size_t clock_cycles

static void swdptap_seq_out_parity(const uint32_t tms_states, const size_t clock_cycles)
{
int parity = __builtin_popcount(tms_states);
const bool parity = __builtin_parity(tms_states);
swdptap_seq_out(tms_states, clock_cycles);
gpio_set_val(SWDIO_PORT, SWDIO_PIN, parity & 1U);
gpio_set_val(SWDIO_PORT, SWDIO_PIN, parity);
for (volatile uint32_t counter = target_clk_divider + 1; counter > 0; --counter)
continue;
gpio_set(SWCLK_PORT, SWCLK_PIN);
Expand Down

0 comments on commit fe3405f

Please sign in to comment.