diff --git a/src/platforms/common/jtagtap.c b/src/platforms/common/jtagtap.c index 24c858f..0d0c7bd 100644 --- a/src/platforms/common/jtagtap.c +++ b/src/platforms/common/jtagtap.c @@ -30,25 +30,24 @@ jtag_proc_t jtag_proc; static void jtagtap_reset(void); static void jtagtap_tms_seq(uint32_t MS, int ticks); -static void jtagtap_tdi_tdo_seq( - uint8_t *DO, 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); -static uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDI); +static void jtagtap_tdi_tdo_seq(uint8_t *DO, 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 uint8_t jtagtap_next(uint8_t tms, uint8_t tdi); int jtagtap_init() { TMS_SET_MODE(); jtag_proc.jtagtap_reset = jtagtap_reset; - jtag_proc.jtagtap_next =jtagtap_next; + jtag_proc.jtagtap_next = jtagtap_next; jtag_proc.jtagtap_tms_seq = jtagtap_tms_seq; jtag_proc.jtagtap_tdi_tdo_seq = jtagtap_tdi_tdo_seq; jtag_proc.jtagtap_tdi_seq = jtagtap_tdi_seq; /* Go to JTAG mode for SWJ-DP */ - for(int i = 0; i <= 50; i++) jtagtap_next(1, 0); /* Reset SW-DP */ - jtagtap_tms_seq(0xE73C, 16); /* SWD to JTAG sequence */ + for (size_t i = 0; i <= 50U; ++i) + jtagtap_next(1, 0); /* Reset SW-DP */ + jtagtap_tms_seq(0xe73cU, 16U); /* SWD to JTAG sequence */ jtagtap_soft_reset(); return 0; @@ -58,98 +57,104 @@ static void jtagtap_reset(void) { #ifdef TRST_PORT if (platform_hwversion() == 0) { - volatile int i; 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); } #endif 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; - gpio_set_val(TMS_PORT, TMS_PIN, dTMS); - gpio_set_val(TDI_PORT, TDI_PIN, dTDI); + gpio_set_val(TMS_PORT, TMS_PIN, tms); + gpio_set_val(TDI_PORT, TDI_PIN, tdi); gpio_set(TCK_PORT, TCK_PIN); - for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); - ret = gpio_get(TDO_PORT, TDO_PIN); + for (cnt = swd_delay_cnt - 2U; cnt > 0; cnt--) + continue; + const uint16_t result = gpio_get(TDO_PORT, TDO_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) { gpio_set_val(TDI_PORT, TDI_PIN, 1); - int data = MS & 1; + bool state = MS & 1; register volatile int32_t cnt; if (swd_delay_cnt) { - while(ticks) { - gpio_set_val(TMS_PORT, TMS_PIN, data); + while (ticks) { + gpio_set_val(TMS_PORT, TMS_PIN, state); 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; - data = MS & 1; + state = MS & 1; ticks--; 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 { - while(ticks) { - gpio_set_val(TMS_PORT, TMS_PIN, data); + while (ticks) { + gpio_set_val(TMS_PORT, TMS_PIN, state); gpio_set(TCK_PORT, TCK_PIN); MS >>= 1; - data = MS & 1; + state = MS & 1; ticks--; gpio_clear(TCK_PORT, TCK_PIN); } } } -static void jtagtap_tdi_tdo_seq( - uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks) +static void jtagtap_tdi_tdo_seq(uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks) { uint8_t index = 1; gpio_set_val(TMS_PORT, TMS_PIN, 0); uint8_t res = 0; register volatile int32_t cnt; if (swd_delay_cnt) { - while(ticks > 1) { + while (ticks > 1) { gpio_set_val(TDI_PORT, TDI_PIN, *DI & index); 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)) { res |= index; } - if(!(index <<= 1)) { + if (!(index <<= 1)) { *DO = res; res = 0; index = 1; - DI++; DO++; + DI++; + DO++; } ticks--; 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 { - while(ticks > 1) { + while (ticks > 1) { gpio_set_val(TDI_PORT, TDI_PIN, *DI & index); gpio_set(TCK_PORT, TCK_PIN); if (gpio_get(TDO_PORT, TDO_PIN)) { res |= index; } - if(!(index <<= 1)) { + if (!(index <<= 1)) { *DO = res; res = 0; index = 1; - DI++; DO++; + DI++; + DO++; } ticks--; 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(TDI_PORT, TDI_PIN, *DI & index); 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)) { res |= index; } *DO = res; 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) @@ -172,24 +179,26 @@ static void jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *DI, int tick uint8_t index = 1; register volatile int32_t cnt; if (swd_delay_cnt) { - while(ticks--) { - gpio_set_val(TMS_PORT, TMS_PIN, ticks? 0 : final_tms); + while (ticks--) { + gpio_set_val(TMS_PORT, TMS_PIN, ticks ? 0 : final_tms); gpio_set_val(TDI_PORT, TDI_PIN, *DI & index); gpio_set(TCK_PORT, TCK_PIN); - for(cnt = swd_delay_cnt -2 ; cnt > 0; cnt--); - if(!(index <<= 1)) { + for (cnt = swd_delay_cnt - 2; cnt > 0; cnt--) + ; + if (!(index <<= 1)) { index = 1; DI++; } 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 { - while(ticks--) { - gpio_set_val(TMS_PORT, TMS_PIN, ticks? 0 : final_tms); + while (ticks--) { + gpio_set_val(TMS_PORT, TMS_PIN, ticks ? 0 : final_tms); gpio_set_val(TDI_PORT, TDI_PIN, *DI & index); gpio_set(TCK_PORT, TCK_PIN); - if(!(index <<= 1)) { + if (!(index <<= 1)) { index = 1; DI++; }