From d1c24b4fec392289337aec42d458638ca73a3ebb Mon Sep 17 00:00:00 2001 From: dragonmux Date: Sat, 16 Jul 2022 01:45:26 +0100 Subject: [PATCH] jtagtap: Cleaned up and fixed the signatures for the JTAG TAP implementation functions --- src/include/jtagtap.h | 10 +++--- src/platforms/common/jtagtap.c | 16 +++++----- src/platforms/hosted/cmsis_dap.c | 42 ++++++++++++++------------ src/platforms/hosted/ftdi_bmp.c | 3 +- src/platforms/hosted/ftdi_bmp.h | 7 ++--- src/platforms/hosted/jlink_jtagtap.c | 40 ++++++++++++------------ src/platforms/hosted/libftdi_jtagtap.c | 29 +++++++++--------- src/platforms/hosted/remote_jtagtap.c | 21 +++++-------- 8 files changed, 83 insertions(+), 85 deletions(-) diff --git a/src/include/jtagtap.h b/src/include/jtagtap.h index e844482..7493802 100644 --- a/src/include/jtagtap.h +++ b/src/include/jtagtap.h @@ -22,6 +22,8 @@ #define __JTAGTAP_H #include +#include +#include typedef struct jtag_proc_s { /* Note: Signal names are as for the device under test. */ @@ -36,8 +38,8 @@ typedef struct jtag_proc_s { * - Caputure the value on TDO * - Release TCK. */ - uint8_t (*jtagtap_next)(const uint8_t tms, const uint8_t tdi); - void (*jtagtap_tms_seq)(uint32_t tms_states, int clock_cycles); + bool (*jtagtap_next)(const bool tms, const bool tdi); + void (*jtagtap_tms_seq)(uint32_t tms_states, size_t clock_cycles); /* * Shift out a sequence on MS and DI, capture data to DO. @@ -45,8 +47,8 @@ typedef struct jtag_proc_s { * - DO may be NULL to ignore captured data. * - DO may be point to the same address as DI. */ - void (*jtagtap_tdi_tdo_seq)(uint8_t *data_out, const uint8_t final_tms, const uint8_t *data_in, int clock_cycles); - void (*jtagtap_tdi_seq)(const uint8_t final_tms, const uint8_t *data_in, int clock_cycles); + void (*jtagtap_tdi_tdo_seq)(uint8_t *data_out, const bool final_tms, const uint8_t *data_in, size_t clock_cycles); + void (*jtagtap_tdi_seq)(const bool final_tms, const uint8_t *data_in, size_t clock_cycles); } jtag_proc_t; extern jtag_proc_t jtag_proc; diff --git a/src/platforms/common/jtagtap.c b/src/platforms/common/jtagtap.c index 78a1f55..14f0799 100644 --- a/src/platforms/common/jtagtap.c +++ b/src/platforms/common/jtagtap.c @@ -29,10 +29,10 @@ jtag_proc_t jtag_proc; static void jtagtap_reset(void); -static void jtagtap_tms_seq(uint32_t tms_states, int ticks); -static void jtagtap_tdi_tdo_seq(uint8_t *data_out, uint8_t final_tms, const uint8_t *data_in, int ticks); -static void jtagtap_tdi_seq(uint8_t final_tms, const uint8_t *data_in, int ticks); -static uint8_t jtagtap_next(uint8_t tms, uint8_t tdi); +static void jtagtap_tms_seq(uint32_t tms_states, size_t ticks); +static void jtagtap_tdi_tdo_seq(uint8_t *data_out, bool final_tms, const uint8_t *data_in, size_t ticks); +static void jtagtap_tdi_seq(bool final_tms, const uint8_t *data_in, size_t ticks); +static bool jtagtap_next(bool tms, bool tdi); int jtagtap_init() { @@ -66,7 +66,7 @@ static void jtagtap_reset(void) jtagtap_soft_reset(); } -static uint8_t jtagtap_next(const uint8_t tms, const uint8_t tdi) +static bool jtagtap_next(const bool tms, const bool tdi) { register volatile int32_t cnt; @@ -113,7 +113,7 @@ static void jtagtap_tms_seq_no_delay(uint32_t tms_states, size_t ticks) } } -static void jtagtap_tms_seq(uint32_t tms_states, int ticks) +static void jtagtap_tms_seq(uint32_t tms_states, size_t ticks) { gpio_set(TDI_PORT, TDI_PIN); if (swd_delay_cnt) @@ -179,7 +179,7 @@ static void jtagtap_tdi_tdo_seq_no_delay(const uint8_t *data_in, uint8_t *data_o data_out[byte] = value; } -static void jtagtap_tdi_tdo_seq(uint8_t *data_out, const uint8_t final_tms, const uint8_t *data_in, int ticks) +static void jtagtap_tdi_tdo_seq(uint8_t *data_out, const bool final_tms, const uint8_t *data_in, size_t ticks) { gpio_clear(TDI_PORT, TDI_PIN); if (swd_delay_cnt) @@ -232,7 +232,7 @@ static void jtagtap_tdi_seq_no_delay(const uint8_t *data_in, const bool final_tm } } -static void jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *data_in, int ticks) +static void jtagtap_tdi_seq(const bool final_tms, const uint8_t *data_in, size_t ticks) { if (swd_delay_cnt) jtagtap_tdi_seq_swd_delay(data_in, final_tms, ticks); diff --git a/src/platforms/hosted/cmsis_dap.c b/src/platforms/hosted/cmsis_dap.c index 4f9943a..738a3c1 100644 --- a/src/platforms/hosted/cmsis_dap.c +++ b/src/platforms/hosted/cmsis_dap.c @@ -442,32 +442,34 @@ static void cmsis_dap_jtagtap_reset(void) /* Is there a way to know if TRST is available?*/ } -static void cmsis_dap_jtagtap_tms_seq(uint32_t MS, int ticks) +static void cmsis_dap_jtagtap_tms_seq(const uint32_t tms_states, const size_t ticks) { - uint8_t TMS[4] = {MS & 0xff, (MS >> 8) & 0xff, (MS >> 16) & 0xff, - (MS >> 24) & 0xff}; - dap_jtagtap_tdi_tdo_seq(NULL, false, TMS, NULL, ticks); - DEBUG_PROBE("tms_seq DI %08x %d\n", MS, ticks); + const uint8_t tms[4] = { + (uint8_t)tms_states, (uint8_t)(tms_states >> 8U), (uint8_t)(tms_states >> 16U), (uint8_t)(tms_states >> 24U)}; + dap_jtagtap_tdi_tdo_seq(NULL, false, tms, NULL, ticks); + DEBUG_PROBE("tms_seq data_in %08x %zu\n", tms_states, ticks); } -static void cmsis_dap_jtagtap_tdi_tdo_seq(uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks) +static void cmsis_dap_jtagtap_tdi_tdo_seq(uint8_t *const data_out, const bool final_tms, const uint8_t *const data_in, const size_t ticks) { - dap_jtagtap_tdi_tdo_seq(DO, (final_tms), NULL, DI, ticks); - DEBUG_PROBE("jtagtap_tdi_tdo_seq %d, %02x-> %02x\n", ticks, DI[0], (DO)? DO[0] : 0); + dap_jtagtap_tdi_tdo_seq(data_out, final_tms, NULL, data_in, ticks); + DEBUG_PROBE("jtagtap_tdi_tdo_seq %zu, %02x-> %02x\n", ticks, data_in[0], data_out ? data_out[0] : 0); } -static void cmsis_dap_jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *DI, int ticks) +static void cmsis_dap_jtagtap_tdi_seq(const bool final_tms, const uint8_t *const data_in, const size_t ticks) { - dap_jtagtap_tdi_tdo_seq(NULL, (final_tms), NULL, DI, ticks); - DEBUG_PROBE("jtagtap_tdi_seq %d, %02x\n", ticks, DI[0]); + dap_jtagtap_tdi_tdo_seq(NULL, final_tms, NULL, data_in, ticks); + DEBUG_PROBE("jtagtap_tdi_seq %zu, %02x\n", ticks, data_in[0]); } -static uint8_t cmsis_dap_jtagtap_next(uint8_t dTMS, uint8_t dTDI) +static bool cmsis_dap_jtagtap_next(const bool tms, const bool tdi) { - uint8_t tdo[1]; - dap_jtagtap_tdi_tdo_seq(tdo, false, &dTMS, &dTDI, 1); - DEBUG_PROBE("next tms %02x tdi %02x tdo %02x\n", dTMS, dTDI, tdo[0]); - return (tdo[0] & 1); + const uint8_t tms_byte = tms ? 1 : 0; + const uint8_t tdi_byte = tdi ? 1 : 0; + uint8_t tdo = 0; + dap_jtagtap_tdi_tdo_seq(&tdo, false, &tms_byte, &tdi_byte, 1); + DEBUG_PROBE("next tms %02x tdi %02x tdo %02x\n", tms, tdi, tdo); + return tdo; } int cmsis_dap_jtagtap_init(jtag_proc_t *jtag_proc) @@ -479,11 +481,11 @@ int cmsis_dap_jtagtap_init(jtag_proc_t *jtag_proc) dap_disconnect(); dap_connect(true); dap_reset_link(true); - jtag_proc->jtagtap_reset = cmsis_dap_jtagtap_reset; - jtag_proc->jtagtap_next = cmsis_dap_jtagtap_next; - jtag_proc->jtagtap_tms_seq = cmsis_dap_jtagtap_tms_seq; + jtag_proc->jtagtap_reset = cmsis_dap_jtagtap_reset; + jtag_proc->jtagtap_next = cmsis_dap_jtagtap_next; + jtag_proc->jtagtap_tms_seq = cmsis_dap_jtagtap_tms_seq; jtag_proc->jtagtap_tdi_tdo_seq = cmsis_dap_jtagtap_tdi_tdo_seq; - jtag_proc->jtagtap_tdi_seq = cmsis_dap_jtagtap_tdi_seq; + jtag_proc->jtagtap_tdi_seq = cmsis_dap_jtagtap_tdi_seq; return 0; } diff --git a/src/platforms/hosted/ftdi_bmp.c b/src/platforms/hosted/ftdi_bmp.c index 8f944c2..6851ecf 100644 --- a/src/platforms/hosted/ftdi_bmp.c +++ b/src/platforms/hosted/ftdi_bmp.c @@ -628,8 +628,7 @@ int libftdi_buffer_read(uint8_t *data, int size) return size; } -void libftdi_jtagtap_tdi_tdo_seq( - uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks) +void libftdi_jtagtap_tdi_tdo_seq(uint8_t *DO, const bool final_tms, const uint8_t *DI, size_t ticks) { int rsize, rticks; diff --git a/src/platforms/hosted/ftdi_bmp.h b/src/platforms/hosted/ftdi_bmp.h index 2ed711a..6e80ae9 100644 --- a/src/platforms/hosted/ftdi_bmp.h +++ b/src/platforms/hosted/ftdi_bmp.h @@ -109,8 +109,8 @@ void libftdi_buffer_flush(void) { } int libftdi_buffer_write(const uint8_t *data, int size) { return size; } int libftdi_buffer_read(uint8_t *data, int size) { return size; } const char *libftdi_target_voltage(void) { return "ERROR"; } -void libftdi_jtagtap_tdi_tdo_seq( - uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks) { } +void libftdi_jtagtap_tdi_tdo_seq(uint8_t *const data_out, const bool final_tms, + const uint8_t *const data_in, const size_t ticks) { } bool libftdi_swd_possible(bool *do_mpsse, bool *direct_bb_swd) { return false; } void libftdi_max_frequency_set(uint32_t freq) { } uint32_t libftdi_max_frequency_get(void) { return 0; } @@ -131,8 +131,7 @@ void libftdi_buffer_flush(void); int libftdi_buffer_write(const uint8_t *data, int size); int libftdi_buffer_read(uint8_t *data, int size); const char *libftdi_target_voltage(void); -void libftdi_jtagtap_tdi_tdo_seq( - uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks); +void libftdi_jtagtap_tdi_tdo_seq(uint8_t *data_out, bool final_tms, const uint8_t *data_in, size_t ticks); bool libftdi_swd_possible(bool *do_mpsse, bool *direct_bb_swd); void libftdi_max_frequency_set(uint32_t freq); uint32_t libftdi_max_frequency_get(void); diff --git a/src/platforms/hosted/jlink_jtagtap.c b/src/platforms/hosted/jlink_jtagtap.c index c70da7f..d8d7ca1 100644 --- a/src/platforms/hosted/jlink_jtagtap.c +++ b/src/platforms/hosted/jlink_jtagtap.c @@ -35,9 +35,9 @@ static void jtagtap_reset(void) jtagtap_soft_reset(); } -static void jtagtap_tms_seq(uint32_t MS, int ticks) +static void jtagtap_tms_seq(uint32_t tms_states, size_t ticks) { - DEBUG_PROBE("jtagtap_tms_seq 0x%08" PRIx32 ", ticks %d\n", MS, ticks); + DEBUG_PROBE("jtagtap_tms_seq 0x%08" PRIx32 ", ticks %d\n", tms_states, ticks); int len = (ticks + 7) / 8; uint8_t cmd[12]; cmd[0] = CMD_HW_JTAG3; @@ -46,10 +46,10 @@ static void jtagtap_tms_seq(uint32_t MS, int ticks) cmd[3] = 0; uint8_t *tms = cmd + 4; for (int i = 0; i < len; i++) { - *tms = MS & 0xff; + *tms = tms_states & 0xff; *(tms + len) = *tms; tms++; - MS >>= 8; + tms_states >>= 8; } uint8_t res[4]; send_recv(info.usb_link, cmd, 4 + 2 * len, res, len); @@ -58,17 +58,17 @@ static void jtagtap_tms_seq(uint32_t MS, int ticks) raise_exception(EXCEPTION_ERROR, "tagtap_tms_seq failed"); } -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 *data_out, const bool final_tms, + const uint8_t *data_in, size_t ticks) { if (!ticks) return; int len = (ticks + 7) / 8; if (cl_debuglevel & BMP_DEBUG_PROBE) { - DEBUG_PROBE("jtagtap_tdi_tdo %s, ticks %d, DI: ", + DEBUG_PROBE("jtagtap_tdi_tdo %s, ticks %d, data_in: ", (final_tms) ? "Final TMS" : "", ticks); for (int i = 0; i < len; i++) { - DEBUG_PROBE("%02x", DI[i]); + DEBUG_PROBE("%02x", data_in[i]); } DEBUG_PROBE("\n"); } @@ -83,11 +83,11 @@ static void jtagtap_tdi_tdo_seq(uint8_t *DO, const uint8_t final_tms, if (final_tms) cmd[4 + (ticks - 1) / 8] |= (1 << ((ticks - 1) % 8)); uint8_t *tdi = tms; - if (DI) + if (data_in) for (int i = 0; i < len; i++) - *tdi++ = DI[i]; - if (DO) - send_recv(info.usb_link, cmd, 4 + 2 * len, DO, len); + *tdi++ = data_in[i]; + if (data_out) + send_recv(info.usb_link, cmd, 4 + 2 * len, data_out, len); else send_recv(info.usb_link, cmd, 4 + 2 * len, cmd, len); uint8_t res[1]; @@ -96,12 +96,12 @@ static void jtagtap_tdi_tdo_seq(uint8_t *DO, const uint8_t final_tms, raise_exception(EXCEPTION_ERROR, "jtagtap_tdi_tdi failed"); } -static void jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *DI, - int ticks) +static void jtagtap_tdi_seq(const bool final_tms, const uint8_t *data_in, + size_t ticks) { if (cl_debuglevel & BMP_DEBUG_PROBE) { DEBUG_PROBE("jtagtap_tdi_seq %s:", (final_tms)? "final_tms" : ""); - const uint8_t *p = DI; + const uint8_t *p = data_in; unsigned int i = (ticks & 7) & ~7 ; if (i > 16) i = 16; @@ -111,19 +111,19 @@ static void jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *DI, DEBUG_PROBE(" ..."); DEBUG_PROBE("\n"); } - return jtagtap_tdi_tdo_seq(NULL, final_tms, DI, ticks); + return jtagtap_tdi_tdo_seq(NULL, final_tms, data_in, ticks); } -static uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDI) +static bool jtagtap_next(bool tms, bool tdi) { - DEBUG_PROBE("jtagtap_next TMS 0x%02x, TDI %02x\n", dTMS, dTDI); + DEBUG_PROBE("jtagtap_next TMS 0x%02x, TDI %02x\n", tms, tdi); uint8_t cmd[6]; cmd[0] = CMD_HW_JTAG3; cmd[1] = 0; cmd[2] = 1; cmd[3] = 0; - cmd[4] = (dTMS) ? 0xff : 0; - cmd[5] = (dTDI) ? 0xff : 0; + cmd[4] = tms ? 0xff : 0; + cmd[5] = tdi ? 0xff : 0; uint8_t ret[1]; send_recv(info.usb_link, cmd, 6, ret, 1); uint8_t res[1]; diff --git a/src/platforms/hosted/libftdi_jtagtap.c b/src/platforms/hosted/libftdi_jtagtap.c index 9443df5..5180875 100644 --- a/src/platforms/hosted/libftdi_jtagtap.c +++ b/src/platforms/hosted/libftdi_jtagtap.c @@ -33,10 +33,9 @@ extern cable_desc_t *active_cable; extern struct ftdi_context *ftdic; static void jtagtap_reset(void); -static void jtagtap_tms_seq(uint32_t MS, 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_tms_seq(uint32_t tms_states, size_t ticks); +static void jtagtap_tdi_seq(bool final_tms, const uint8_t *DI, size_t ticks); +static bool jtagtap_next(bool dTMS, bool dTDI); int libftdi_jtagtap_init(jtag_proc_t *jtag_proc) { @@ -95,26 +94,28 @@ static void jtagtap_reset(void) jtagtap_soft_reset(); } -static void jtagtap_tms_seq(uint32_t MS, int ticks) +static void jtagtap_tms_seq(uint32_t tms_states, size_t ticks) { - uint8_t tmp[3] = { - MPSSE_WRITE_TMS | MPSSE_LSB | MPSSE_BITMODE | MPSSE_WRITE_NEG, 0, 0}; - while(ticks >= 0) { - tmp[1] = ticks<7?ticks-1:6; - tmp[2] = 0x80 | (MS & 0x7F); + uint8_t tmp[3] = {MPSSE_WRITE_TMS | MPSSE_LSB | MPSSE_BITMODE | MPSSE_WRITE_NEG, 0, 0}; + while (ticks > 0) { + tmp[1] = ticks < 7 ? ticks - 1 : 6; + tmp[2] = 0x80 | (tms_states & 0x7F); libftdi_buffer_write(tmp, 3); - MS >>= 7; ticks -= 7; + tms_states >>= 7; + if (ticks < 7) + ticks = 0; + else + ticks -= 7; } } -static void jtagtap_tdi_seq( - const uint8_t final_tms, const uint8_t *DI, int ticks) +static void jtagtap_tdi_seq(const bool final_tms, const uint8_t *DI, size_t ticks) { return libftdi_jtagtap_tdi_tdo_seq(NULL, final_tms, DI, ticks); } -static uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDI) +static bool jtagtap_next(bool dTMS, bool dTDI) { uint8_t ret; uint8_t tmp[3] = {MPSSE_WRITE_TMS | MPSSE_DO_READ | MPSSE_LSB | diff --git a/src/platforms/hosted/remote_jtagtap.c b/src/platforms/hosted/remote_jtagtap.c index 8a0d91a..55bd7ef 100644 --- a/src/platforms/hosted/remote_jtagtap.c +++ b/src/platforms/hosted/remote_jtagtap.c @@ -36,12 +36,10 @@ #include "bmp_remote.h" 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_tms_seq(uint32_t MS, size_t ticks); +static void jtagtap_tdi_tdo_seq(uint8_t *DO, bool final_tms, const uint8_t *DI, size_t ticks); +static void jtagtap_tdi_seq(bool final_tms, const uint8_t *DI, size_t ticks); +static bool jtagtap_next(bool dTMS, bool dTDI); int remote_jtagtap_init(jtag_proc_t *jtag_proc) { @@ -87,7 +85,7 @@ static void jtagtap_reset(void) } } -static void jtagtap_tms_seq(uint32_t MS, int ticks) +static void jtagtap_tms_seq(uint32_t MS, size_t ticks) { uint8_t construct[REMOTE_MAX_MSG_SIZE]; int s; @@ -110,8 +108,7 @@ static void jtagtap_tms_seq(uint32_t MS, int ticks) * FIXME: Provide and test faster call and keep fallback * for old firmware */ -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 bool final_tms, const uint8_t *DI, size_t ticks) { uint8_t construct[REMOTE_MAX_MSG_SIZE]; int s; @@ -159,14 +156,12 @@ static void jtagtap_tdi_tdo_seq( } } -static void jtagtap_tdi_seq( - const uint8_t final_tms, const uint8_t *DI, int ticks) +static void jtagtap_tdi_seq(const bool final_tms, const uint8_t *DI, size_t ticks) { return jtagtap_tdi_tdo_seq(NULL, final_tms, DI, ticks); } - -static uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDI) +static bool jtagtap_next(bool dTMS, bool dTDI) { uint8_t construct[REMOTE_MAX_MSG_SIZE]; int s;