jtagtap: Reformatted and begun cleaning up the JTAG TAP primitives implementation

This commit is contained in:
dragonmux 2022-07-15 23:31:05 +01:00 committed by Piotr Esden-Tempski
parent a6f2b52d9b
commit 303fa15171
1 changed files with 57 additions and 48 deletions

View File

@ -30,11 +30,9 @@ jtag_proc_t jtag_proc;
static void jtagtap_reset(void); static void jtagtap_reset(void);
static void jtagtap_tms_seq(uint32_t MS, int ticks); static void jtagtap_tms_seq(uint32_t MS, int ticks);
static void jtagtap_tdi_tdo_seq( static void jtagtap_tdi_tdo_seq(uint8_t *DO, uint8_t final_tms, const uint8_t *DI, int ticks);
uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks); static void jtagtap_tdi_seq(uint8_t final_tms, const uint8_t *DI, int ticks);
static void jtagtap_tdi_seq( static uint8_t jtagtap_next(uint8_t tms, uint8_t tdi);
const uint8_t final_tms, const uint8_t *DI, int ticks);
static uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDI);
int jtagtap_init() int jtagtap_init()
{ {
@ -47,8 +45,9 @@ int jtagtap_init()
jtag_proc.jtagtap_tdi_seq = jtagtap_tdi_seq; jtag_proc.jtagtap_tdi_seq = jtagtap_tdi_seq;
/* Go to JTAG mode for SWJ-DP */ /* Go to JTAG mode for SWJ-DP */
for(int i = 0; i <= 50; i++) jtagtap_next(1, 0); /* Reset SW-DP */ for (size_t i = 0; i <= 50U; ++i)
jtagtap_tms_seq(0xE73C, 16); /* SWD to JTAG sequence */ jtagtap_next(1, 0); /* Reset SW-DP */
jtagtap_tms_seq(0xe73cU, 16U); /* SWD to JTAG sequence */
jtagtap_soft_reset(); jtagtap_soft_reset();
return 0; return 0;
@ -58,63 +57,65 @@ static void jtagtap_reset(void)
{ {
#ifdef TRST_PORT #ifdef TRST_PORT
if (platform_hwversion() == 0) { if (platform_hwversion() == 0) {
volatile int i;
gpio_clear(TRST_PORT, TRST_PIN); gpio_clear(TRST_PORT, TRST_PIN);
for(i = 0; i < 10000; i++) asm("nop"); for (volatile size_t i = 0; i < 10000; i++)
asm("nop");
gpio_set(TRST_PORT, TRST_PIN); gpio_set(TRST_PORT, TRST_PIN);
} }
#endif #endif
jtagtap_soft_reset(); jtagtap_soft_reset();
} }
static uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDI) static uint8_t jtagtap_next(const uint8_t tms, const uint8_t tdi)
{ {
uint16_t ret;
register volatile int32_t cnt; register volatile int32_t cnt;
gpio_set_val(TMS_PORT, TMS_PIN, dTMS); gpio_set_val(TMS_PORT, TMS_PIN, tms);
gpio_set_val(TDI_PORT, TDI_PIN, dTDI); gpio_set_val(TDI_PORT, TDI_PIN, tdi);
gpio_set(TCK_PORT, TCK_PIN); gpio_set(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2U; cnt > 0; cnt--)
ret = gpio_get(TDO_PORT, TDO_PIN); continue;
const uint16_t result = gpio_get(TDO_PORT, TDO_PIN);
gpio_clear(TCK_PORT, TCK_PIN); gpio_clear(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt - 2; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2U; cnt > 0; cnt--)
continue;
//DEBUG("jtagtap_next(TMS = %d, TDI = %d) = %d\n", dTMS, dTDI, ret); //DEBUG("jtagtap_next(TMS = %u, TDI = %u) = %u\n", tms, tdi, result);
return ret != 0; return result != 0;
} }
static void jtagtap_tms_seq(uint32_t MS, int ticks) static void jtagtap_tms_seq(uint32_t MS, int ticks)
{ {
gpio_set_val(TDI_PORT, TDI_PIN, 1); gpio_set_val(TDI_PORT, TDI_PIN, 1);
int data = MS & 1; bool state = MS & 1;
register volatile int32_t cnt; register volatile int32_t cnt;
if (swd_delay_cnt) { if (swd_delay_cnt) {
while (ticks) { while (ticks) {
gpio_set_val(TMS_PORT, TMS_PIN, data); gpio_set_val(TMS_PORT, TMS_PIN, state);
gpio_set(TCK_PORT, TCK_PIN); gpio_set(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2; cnt > 0; cnt--)
continue;
MS >>= 1; MS >>= 1;
data = MS & 1; state = MS & 1;
ticks--; ticks--;
gpio_clear(TCK_PORT, TCK_PIN); gpio_clear(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2; cnt > 0; cnt--)
continue;
} }
} else { } else {
while (ticks) { while (ticks) {
gpio_set_val(TMS_PORT, TMS_PIN, data); gpio_set_val(TMS_PORT, TMS_PIN, state);
gpio_set(TCK_PORT, TCK_PIN); gpio_set(TCK_PORT, TCK_PIN);
MS >>= 1; MS >>= 1;
data = MS & 1; state = MS & 1;
ticks--; ticks--;
gpio_clear(TCK_PORT, TCK_PIN); gpio_clear(TCK_PORT, TCK_PIN);
} }
} }
} }
static void jtagtap_tdi_tdo_seq( static void jtagtap_tdi_tdo_seq(uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks)
uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks)
{ {
uint8_t index = 1; uint8_t index = 1;
gpio_set_val(TMS_PORT, TMS_PIN, 0); gpio_set_val(TMS_PORT, TMS_PIN, 0);
@ -124,7 +125,8 @@ static void jtagtap_tdi_tdo_seq(
while (ticks > 1) { while (ticks > 1) {
gpio_set_val(TDI_PORT, TDI_PIN, *DI & index); gpio_set_val(TDI_PORT, TDI_PIN, *DI & index);
gpio_set(TCK_PORT, TCK_PIN); gpio_set(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2; cnt > 0; cnt--)
;
if (gpio_get(TDO_PORT, TDO_PIN)) { if (gpio_get(TDO_PORT, TDO_PIN)) {
res |= index; res |= index;
} }
@ -132,11 +134,13 @@ static void jtagtap_tdi_tdo_seq(
*DO = res; *DO = res;
res = 0; res = 0;
index = 1; index = 1;
DI++; DO++; DI++;
DO++;
} }
ticks--; ticks--;
gpio_clear(TCK_PORT, TCK_PIN); gpio_clear(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2; cnt > 0; cnt--)
;
} }
} else { } else {
while (ticks > 1) { while (ticks > 1) {
@ -149,7 +153,8 @@ static void jtagtap_tdi_tdo_seq(
*DO = res; *DO = res;
res = 0; res = 0;
index = 1; index = 1;
DI++; DO++; DI++;
DO++;
} }
ticks--; ticks--;
gpio_clear(TCK_PORT, TCK_PIN); gpio_clear(TCK_PORT, TCK_PIN);
@ -158,13 +163,15 @@ static void jtagtap_tdi_tdo_seq(
gpio_set_val(TMS_PORT, TMS_PIN, final_tms); gpio_set_val(TMS_PORT, TMS_PIN, final_tms);
gpio_set_val(TDI_PORT, TDI_PIN, *DI & index); gpio_set_val(TDI_PORT, TDI_PIN, *DI & index);
gpio_set(TCK_PORT, TCK_PIN); gpio_set(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2; cnt > 0; cnt--)
;
if (gpio_get(TDO_PORT, TDO_PIN)) { if (gpio_get(TDO_PORT, TDO_PIN)) {
res |= index; res |= index;
} }
*DO = res; *DO = res;
gpio_clear(TCK_PORT, TCK_PIN); gpio_clear(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2; cnt > 0; cnt--)
;
} }
static void jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *DI, int ticks) static void jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *DI, int ticks)
@ -176,13 +183,15 @@ static void jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *DI, int tick
gpio_set_val(TMS_PORT, TMS_PIN, ticks ? 0 : final_tms); gpio_set_val(TMS_PORT, TMS_PIN, ticks ? 0 : final_tms);
gpio_set_val(TDI_PORT, TDI_PIN, *DI & index); gpio_set_val(TDI_PORT, TDI_PIN, *DI & index);
gpio_set(TCK_PORT, TCK_PIN); gpio_set(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2; cnt > 0; cnt--)
;
if (!(index <<= 1)) { if (!(index <<= 1)) {
index = 1; index = 1;
DI++; DI++;
} }
gpio_clear(TCK_PORT, TCK_PIN); gpio_clear(TCK_PORT, TCK_PIN);
for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); for (cnt = swd_delay_cnt - 2; cnt > 0; cnt--)
;
} }
} else { } else {
while (ticks--) { while (ticks--) {