Pavel pointed out that jtag_add_tlr() is better than jtag_add_tms().
[openocd.git] / src / jtag / jtag.c
index 8f55ea0bff88c18181523a9d616037838ddc945b..c73e0c9aa879663b1056e796215d2b2591c44a78 100644 (file)
@@ -109,6 +109,11 @@ char* jtag_event_strings[] =
        "JTAG controller reset(tms or TRST)"
 };
 
+/* kludge!!!! these are just global variables that the
+ * interface use internally. They really belong
+ * inside the drivers, but we don't want to break
+ * linking the drivers!!!!
+ */
 enum tap_state end_state = TAP_TLR;
 enum tap_state cur_state = TAP_TLR;
 int jtag_trst = 0;
@@ -136,6 +141,11 @@ int jtag_ntrst_delay = 0; /* default to no nTRST delay */
 /* callbacks to inform high-level handlers about JTAG state changes */
 jtag_event_callback_t *jtag_event_callbacks;
 
+/* speed in kHz*/
+static int speed1 = 0, speed2 = 0;
+/* flag if the kHz speed was defined */
+static int hasKHz = 0;
+
 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
  */
  
@@ -222,10 +232,8 @@ int jtag_speed_post_reset = 0;
 
 
 /* forward declarations */
-void jtag_add_statemove(enum tap_state endstate);
 void jtag_add_pathmove(int num_states, enum tap_state *path);
 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
-void jtag_add_reset(int trst, int srst);
 void jtag_add_end_state(enum tap_state endstate);
 void jtag_add_sleep(u32 us);
 int jtag_execute_queue(void);
@@ -245,7 +253,6 @@ int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char
 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
-int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 
@@ -777,18 +784,20 @@ int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *f
 
        return ERROR_OK;
 }
-void jtag_add_statemove(enum tap_state state)
-{
-       jtag_prelude(state);
 
+void jtag_add_tlr()
+{
+       jtag_prelude(TAP_TLR);
+       
        int retval;
-       retval=interface_jtag_add_statemove(cmd_queue_end_state);
+       retval=interface_jtag_add_tlr();
        if (retval!=ERROR_OK)
                jtag_error=retval;
 }
 
-int MINIDRIVER(interface_jtag_add_statemove)(enum tap_state state)
+int MINIDRIVER(interface_jtag_add_tlr)()
 {
+       enum tap_state state = TAP_TLR;
        jtag_command_t **last_cmd = jtag_get_last_command_p();
        
        /* allocate memory for a new list member */
@@ -884,14 +893,14 @@ void jtag_add_runtest(int num_cycles, enum tap_state state)
                jtag_error=retval;
 }
 
-void jtag_add_reset(int req_trst, int req_srst)
+void jtag_add_reset(int req_tlr_or_trst, int req_srst)
 {
-       int trst_with_tms = 0;
+       int trst_with_tlr = 0;
        int retval;
        
        /* Make sure that jtag_reset_config allows the requested reset */
        /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
-       if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (req_trst == 0))
+       if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
        {
                LOG_ERROR("BUG: requested reset would assert trst");
                jtag_error=ERROR_FAIL;
@@ -899,29 +908,35 @@ void jtag_add_reset(int req_trst, int req_srst)
        }
                
        /* if TRST pulls SRST, we reset with TAP T-L-R */
-       if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_trst == 1)) && (req_srst == 0))
+       if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
        {
-               req_trst = 0;
-               trst_with_tms = 1;
+               trst_with_tlr = 1;
        }
        
        if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
        {
-               LOG_ERROR("BUG: requested nSRST assertion, but the current configuration doesn't support this");
+               LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
                jtag_error=ERROR_FAIL;
                return;
        }
        
-       if (req_trst && !(jtag_reset_config & RESET_HAS_TRST))
+       if (req_tlr_or_trst)
        {
-               req_trst = 0;
-               trst_with_tms = 1;
+               if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
+               {
+                       jtag_trst = 1;
+               } else
+               {
+                       trst_with_tlr = 1;
+               }
+       } else
+       {
+               jtag_trst = 0;
        }
-
-       jtag_trst = req_trst;
+       
        jtag_srst = req_srst;
 
-       retval = interface_jtag_add_reset(req_trst, req_srst);
+       retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
        if (retval!=ERROR_OK)
        {
                jtag_error=retval;
@@ -939,11 +954,11 @@ void jtag_add_reset(int req_trst, int req_srst)
                        jtag_add_sleep(jtag_nsrst_delay * 1000);
        }
        
-       if (trst_with_tms)
+       if (trst_with_tlr)
        {
                LOG_DEBUG("JTAG reset with tms instead of TRST");
                jtag_add_end_state(TAP_TLR);
-               jtag_add_statemove(TAP_TLR);
+               jtag_add_tlr();
                jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
                return;
        }
@@ -959,10 +974,6 @@ void jtag_add_reset(int req_trst, int req_srst)
        }
        else
        {
-               /* the nTRST line got deasserted, so we're still in Test-Logic-Reset,
-                * but we might want to add a delay to give the TAP time to settle
-                */
-               LOG_DEBUG("Now in TAP_TLR - Test-Logic-Reset(either due to TRST line asserted or tms reset)");
                if (jtag_ntrst_delay)
                        jtag_add_sleep(jtag_ntrst_delay * 1000);
        }
@@ -1416,9 +1427,9 @@ int jtag_register_commands(struct command_context_s *cmd_ctx)
        register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
                COMMAND_CONFIG, NULL);
        register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
-               COMMAND_CONFIG, NULL);
+               COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
        register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
-               COMMAND_CONFIG, NULL);
+               COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
                
        register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
                COMMAND_EXEC, "print current scan chain configuration");
@@ -1429,8 +1440,6 @@ int jtag_register_commands(struct command_context_s *cmd_ctx)
                COMMAND_EXEC, "toggle reset lines <trst> <srst>");
        register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
                COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
-       register_command(cmd_ctx, NULL, "statemove", handle_statemove_command,
-               COMMAND_EXEC, "move to current endstate or [tap_state]");
        register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
                COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
        register_command(cmd_ctx, NULL, "drscan", handle_drscan_command,
@@ -1449,10 +1458,21 @@ int jtag_interface_init(struct command_context_s *cmd_ctx)
                LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
                return ERROR_JTAG_INVALID_INTERFACE;
        }
+       if(hasKHz)
+       {
+               /*stay on "reset speed"*/
+               if (jtag_interface->khz(speed1, &speed1) == ERROR_OK)
+                       jtag_speed = speed1;
+               if (jtag_interface->khz(speed2, &speed2) == ERROR_OK)
+                       jtag_speed_post_reset = speed2;
+               hasKHz = 0;
+       }
 
        if (jtag_interface->init() != ERROR_OK)
                return ERROR_JTAG_INIT_FAILED;
 
+       
+       
        jtag = jtag_interface;
        return ERROR_OK;
 }
@@ -1477,7 +1497,7 @@ int jtag_init(struct command_context_s *cmd_ctx)
                device = device->next;
        }
        
-       jtag_add_statemove(TAP_TLR);
+       jtag_add_tlr();
        jtag_execute_queue();
 
        /* examine chain first, as this could discover the real chain layout */
@@ -1489,6 +1509,7 @@ int jtag_init(struct command_context_s *cmd_ctx)
        while (jtag_validate_chain() != ERROR_OK)
        {
                validate_tries++;
+               
                if (validate_tries > 5)
                {
                        LOG_ERROR("Could not validate JTAG chain, exit");
@@ -1496,7 +1517,7 @@ int jtag_init(struct command_context_s *cmd_ctx)
                }
                usleep(10000);
        }
-
+       
        return ERROR_OK;
 }
 
@@ -1610,6 +1631,9 @@ int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char
 
 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
+       if (argc < 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+       
        if (argc >= 1)
        {
                if (strcmp(args[0], "none") == 0)
@@ -1630,19 +1654,23 @@ int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, ch
        
        if (argc >= 2)
        {
-               if (strcmp(args[1], "srst_pulls_trst") == 0)
-                       jtag_reset_config |= RESET_SRST_PULLS_TRST;
-               else if (strcmp(args[1], "trst_pulls_srst") == 0)
-                       jtag_reset_config |= RESET_TRST_PULLS_SRST;
-               else if (strcmp(args[1], "combined") == 0)
-                       jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
-               else if (strcmp(args[1], "separate") == 0)
-                       jtag_reset_config &= ~(RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST);
-               else
+               if (strcmp(args[1], "separate") == 0)
                {
-                       LOG_ERROR("invalid reset_config argument, defaulting to none");
-                       jtag_reset_config = RESET_NONE;
-                       return ERROR_INVALID_ARGUMENTS;
+                       /* seperate reset lines - default */
+               } else
+               {
+                       if (strcmp(args[1], "srst_pulls_trst") == 0)
+                               jtag_reset_config |= RESET_SRST_PULLS_TRST;
+                       else if (strcmp(args[1], "trst_pulls_srst") == 0)
+                               jtag_reset_config |= RESET_TRST_PULLS_SRST;
+                       else if (strcmp(args[1], "combined") == 0)
+                               jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
+                       else
+                       {
+                               LOG_ERROR("invalid reset_config argument, defaulting to none");
+                               jtag_reset_config = RESET_NONE;
+                               return ERROR_INVALID_ARGUMENTS;
+                       }
                }
        }
        
@@ -1710,54 +1738,62 @@ int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd
 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
        int cur_speed = 0;
-       if ((argc<1) || (argc>2))
-               return ERROR_COMMAND_SYNTAX_ERROR;
-
-       if (argc >= 1)
-               cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
-       if (argc == 2)
-               cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
+       
+       if (argc != 0)
+       {
+               if ((argc<1) || (argc>2))
+                       return ERROR_COMMAND_SYNTAX_ERROR;
                
-       /* this command can be called during CONFIG, 
-        * in which case jtag isn't initialized */
-       if (jtag)
-               jtag->speed(cur_speed);
-
+               LOG_DEBUG("handle jtag speed");
+               
+               if (argc >= 1)
+                       cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
+               if (argc == 2)
+                       cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
+                       
+               /* this command can be called during CONFIG, 
+                * in which case jtag isn't initialized */
+               if (jtag)
+                       jtag->speed(cur_speed);
+       }               
+       command_print(cmd_ctx, "jtag_speed: %d, %d", jtag_speed, jtag_speed_post_reset);
+       
        return ERROR_OK;
 }
 
 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
-       int cur_speed = 0;
-       int speed1, speed2;
+       LOG_DEBUG("handle jtag khz");
+       
        if ((argc<1) || (argc>2))
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       if (jtag == NULL)
-       {
-               LOG_ERROR("Interface not selected yet");
-               return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-       
        if (argc >= 1)
-               speed1 = strtoul(args[0], NULL, 0);
+               speed1 = speed2 = strtoul(args[0], NULL, 0);
        if (argc == 2)
                speed2 = strtoul(args[1], NULL, 0);
-       
-       if (jtag->khz(speed1, &speed1)!=ERROR_OK)
-               return ERROR_OK;
-       
-       if (jtag->khz(speed2, &speed2)!=ERROR_OK)
-               return ERROR_OK;
-       
-       if (argc >= 1)
-               cur_speed = jtag_speed = jtag_speed_post_reset = speed1;
-       
-       if (argc == 2)
-               cur_speed = jtag_speed_post_reset = speed2;
-               
-       jtag->speed(cur_speed);
 
+       if (jtag != NULL)
+       {
+               int cur_speed = 0;
+               LOG_DEBUG("have interface set up");
+               int speed_div1, speed_div2;
+               if (jtag->khz(speed1, &speed_div1)!=ERROR_OK)
+                       return ERROR_OK;
+               if (jtag->khz(speed2, &speed_div2)!=ERROR_OK)
+                       return ERROR_OK;
+
+               if (argc >= 1)
+                       cur_speed = jtag_speed = jtag_speed_post_reset = speed_div1;
+               if (argc == 2)
+                       cur_speed = jtag_speed_post_reset = speed_div2;
+
+               jtag->speed(cur_speed);
+       } else
+       {
+               hasKHz = 1;
+       }
+       
        return ERROR_OK;
 }
 
@@ -1781,7 +1817,7 @@ int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char *
                        }
                }
        }
-       command_print(cmd_ctx, "current endstate: %s", tap_state_strings[end_state]);
+       command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
        
        return ERROR_OK;
 }
@@ -1837,28 +1873,6 @@ int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **
 
 }
 
-int handle_statemove_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       enum tap_state state;
-
-       state = -1;
-       if (argc == 1)
-       {
-               for (state = 0; state < 16; state++)
-               {
-                       if (strcmp(args[0], tap_state_strings[state]) == 0)
-                       {
-                               break;
-                       }
-               }
-       }
-
-       jtag_add_statemove(state);
-       jtag_execute_queue();
-
-       return ERROR_OK;
-
-}
 
 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)