From e26910fdad0cdbb930fbdc938e3b186f5dbaccb9 Mon Sep 17 00:00:00 2001 From: Antonio Borneo Date: Wed, 11 Jul 2018 10:21:04 +0200 Subject: [PATCH] jtag: simplify management of non-implemented handlers There is just a single entry point for the jtag API .khz(), .speed_div(), .power_dropout(), .srst_asserted(). Simplify the code by in-lining the default handler. The overall code behaviour is not changed. This change prevents modifying at run-time the content of struct jtag_interface. Inspired from change http://openocd.zylin.com/943 by Evan Hunter Change-Id: I09aeb76d614db57b1884ac7ee9f00c152cd77849 Signed-off-by: Antonio Borneo Reviewed-on: http://openocd.zylin.com/4892 Tested-by: jenkins Reviewed-by: Tomas Vanek --- src/jtag/adapter.c | 39 +-------------------------------------- src/jtag/core.c | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 48 deletions(-) diff --git a/src/jtag/adapter.c b/src/jtag/adapter.c index e11f4e906..b1be28e5f 100644 --- a/src/jtag/adapter.c +++ b/src/jtag/adapter.c @@ -66,30 +66,6 @@ static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv) return JIM_OK; } -static int default_khz(int khz, int *jtag_speed) -{ - LOG_ERROR("Translation from khz to jtag_speed not implemented"); - return ERROR_FAIL; -} - -static int default_speed_div(int speed, int *khz) -{ - LOG_ERROR("Translation from jtag_speed to khz not implemented"); - return ERROR_FAIL; -} - -static int default_power_dropout(int *dropout) -{ - *dropout = 0; /* by default we can't detect power dropout */ - return ERROR_OK; -} - -static int default_srst_asserted(int *srst_asserted) -{ - *srst_asserted = 0; /* by default we can't detect srst asserted */ - return ERROR_OK; -} - COMMAND_HANDLER(interface_transport_command) { char **transports; @@ -158,21 +134,8 @@ COMMAND_HANDLER(handle_interface_command) LOG_WARNING("Adapter driver '%s' did not declare " "which transports it allows; assuming " "legacy JTAG-only", jtag_interface->name); - retval = allow_transports(CMD_CTX, jtag_interface->transports + return allow_transports(CMD_CTX, jtag_interface->transports ? jtag_interface->transports : jtag_only); - if (ERROR_OK != retval) - return retval; - - if (jtag_interface->khz == NULL) - jtag_interface->khz = default_khz; - if (jtag_interface->speed_div == NULL) - jtag_interface->speed_div = default_speed_div; - if (jtag_interface->power_dropout == NULL) - jtag_interface->power_dropout = default_power_dropout; - if (jtag_interface->srst_asserted == NULL) - jtag_interface->srst_asserted = default_srst_asserted; - - return ERROR_OK; } /* no valid interface was found (i.e. the configuration option, diff --git a/src/jtag/core.c b/src/jtag/core.c index e57f62db7..84e939677 100644 --- a/src/jtag/core.c +++ b/src/jtag/core.c @@ -1611,14 +1611,18 @@ static int adapter_khz_to_speed(unsigned khz, int *speed) { LOG_DEBUG("convert khz to interface specific speed value"); speed_khz = khz; - if (jtag != NULL) { - LOG_DEBUG("have interface set up"); - int speed_div1; - int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1); - if (ERROR_OK != retval) - return retval; - *speed = speed_div1; + if (!jtag) + return ERROR_OK; + LOG_DEBUG("have interface set up"); + if (!jtag->khz) { + LOG_ERROR("Translation from khz to jtag_speed not implemented"); + return ERROR_FAIL; } + int speed_div1; + int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1); + if (ERROR_OK != retval) + return retval; + *speed = speed_div1; return ERROR_OK; } @@ -1681,7 +1685,13 @@ int jtag_get_speed_readable(int *khz) int retval = jtag_get_speed(&jtag_speed_var); if (retval != ERROR_OK) return retval; - return jtag ? jtag->speed_div(jtag_speed_var, khz) : ERROR_OK; + if (!jtag) + return ERROR_OK; + if (!jtag->speed_div) { + LOG_ERROR("Translation from jtag_speed to khz not implemented"); + return ERROR_FAIL; + } + return jtag->speed_div(jtag_speed_var, khz); } void jtag_set_verify(bool enable) @@ -1712,12 +1722,20 @@ int jtag_power_dropout(int *dropout) LOG_ERROR("No Valid JTAG Interface Configured."); exit(-1); } - return jtag->power_dropout(dropout); + if (jtag->power_dropout) + return jtag->power_dropout(dropout); + + *dropout = 0; /* by default we can't detect power dropout */ + return ERROR_OK; } int jtag_srst_asserted(int *srst_asserted) { - return jtag->srst_asserted(srst_asserted); + if (jtag->srst_asserted) + return jtag->srst_asserted(srst_asserted); + + *srst_asserted = 0; /* by default we can't detect srst asserted */ + return ERROR_OK; } enum reset_types jtag_get_reset_config(void)