keep_alive() fix for reset warnings.
[fw/openocd] / src / jtag / jtag.c
index 1e0e889bff808b3a7d697f27facecec227e93f21..02272bd93b2278512034d9c8cc981bc317729310 100644 (file)
 
 #include "command.h"
 #include "log.h"
-#include "interpreter.h"
 
 #include "stdlib.h"
 #include "string.h"
 #include <unistd.h>
 
-
 /* note that this is not marked as static as it must be available from outside jtag.c for those 
    that implement the jtag_xxx() minidriver layer 
 */
@@ -106,7 +104,7 @@ tap_transition_t tap_transitions[16] =
 
 char* jtag_event_strings[] =
 {
-       "JTAG controller reset(tms or TRST)"
+       "JTAG controller reset (TLR or TRST)"
 };
 
 /* kludge!!!! these are just global variables that the
@@ -268,7 +266,7 @@ int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char *
 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_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);
+int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
 
 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 
@@ -602,6 +600,11 @@ int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields,
                        bypass_devices++;
                device = device->next;
        }
+       if (bypass_devices >= jtag_num_devices)
+       {
+               LOG_ERROR("all devices in bypass");
+               return ERROR_JTAG_DEVICE_ERROR;
+       }
        
        /* allocate memory for a new list member */
        *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
@@ -927,7 +930,7 @@ void jtag_add_reset(int req_tlr_or_trst, int req_srst)
        
        /* FIX!!! there are *many* different cases here. A better
         * approach is needed for legal combinations of transitions...
-       */
+        */
        if ((jtag_reset_config & RESET_HAS_SRST)&&
                        (jtag_reset_config & RESET_HAS_TRST)&& 
                        ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
@@ -937,7 +940,8 @@ void jtag_add_reset(int req_tlr_or_trst, int req_srst)
                                ((req_srst&&!jtag_srst)||
                                                (!req_srst&&jtag_srst)))
                {
-                       LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
+                       /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
+                       //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
                }
        }
        
@@ -999,7 +1003,7 @@ void jtag_add_reset(int req_tlr_or_trst, int req_srst)
        
        if (trst_with_tlr)
        {
-               LOG_DEBUG("JTAG reset with tms instead of TRST");
+               LOG_DEBUG("JTAG reset with TLR instead of TRST");
                jtag_add_end_state(TAP_TLR);
                jtag_add_tlr();
                jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
@@ -1067,6 +1071,7 @@ int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
 
 void jtag_add_sleep(u32 us)
 {
+       keep_alive(); /* we might be running on a very slow JTAG clk */
        int retval=interface_jtag_add_sleep(us);
        if (retval!=ERROR_OK)
                jtag_error=retval;
@@ -1235,7 +1240,7 @@ void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handle
                field->in_handler = jtag_check_value;
        else
                field->in_handler = NULL;       /* No check, e.g. embeddedice uses value==NULL to indicate no check */
-       field->in_handler_priv = NULL;  /* this will be filled in at the invocation site to point to the field duplicate */ 
+       field->in_handler_priv = NULL;
        field->in_check_value = value;
        field->in_check_mask = mask;
 }
@@ -1259,7 +1264,13 @@ enum scan_type jtag_scan_type(scan_command_t *cmd)
 int MINIDRIVER(interface_jtag_execute_queue)(void)
 {
        int retval;
-
+       
+       if (jtag==NULL)
+       {
+               LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
+               return ERROR_FAIL;
+       }
+       
        retval = jtag->execute_queue();
        
        cmd_queue_free();
@@ -1485,8 +1496,7 @@ int jtag_register_commands(struct command_context_s *cmd_ctx)
                COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
        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,
-               COMMAND_EXEC, "execute DR scan <device> <var> [dev2] [var2] ...");
+       register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
 
        register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
                COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
@@ -1571,11 +1581,11 @@ int jtag_init_reset(struct command_context_s *cmd_ctx)
        if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
                return retval;
 
-       LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / tms");
+       LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
 
        /* Reset can happen after a power cycle.
         * 
-        * Ideally we would only assert TRST or run tms before the target reset.
+        * Ideally we would only assert TRST or run TLR before the target reset.
         * 
         * However w/srst_pulls_trst, trst is asserted together with the target
         * reset whether we want it or not.
@@ -1588,11 +1598,12 @@ int jtag_init_reset(struct command_context_s *cmd_ctx)
         * NB! order matters!!!! srst *can* disconnect JTAG circuitry
         * 
         */
-       jtag_add_reset(1, 0); /* TMS or TRST */
+       jtag_add_reset(1, 0); /* TLR or TRST */
        if (jtag_reset_config & RESET_HAS_SRST)
        {
                jtag_add_reset(1, 1);
-               jtag_add_reset(0, 1);
+               if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
+                       jtag_add_reset(0, 1);
        }
        jtag_add_reset(0, 0);
        if ((retval = jtag_execute_queue()) != ERROR_OK)
@@ -2034,63 +2045,92 @@ int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **a
        return ERROR_OK;
 }
 
-int handle_drscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
 {
+       int retval;
        scan_field_t *fields;
-       int num_fields = 0;
+       int num_fields;
        int field_count = 0;
-       var_t *var;
-       int i, j;
-       
-       if ((argc < 2) || (argc % 2))
+       int i, j, e;
+       long device;
+
+       /* args[1] = device
+        * args[2] = num_bits
+        * args[3] = hex string
+        * ... repeat num bits and hex string ...
+        */
+       if ((argc < 4) || ((argc % 2)!=0))
        {
-               return ERROR_COMMAND_SYNTAX_ERROR;
+               Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
+               return JIM_ERR;
        }
 
-       for (i = 0; i < argc; i+=2)
+       for (i = 2; i < argc; i+=2)
        {
-               var = get_var_by_namenum(args[i+1]);
-               if (var)
-               {
-                       num_fields += var->num_fields;
-               }
-               else
-               {
-                       command_print(cmd_ctx, "variable %s doesn't exist", args[i+1]);
-                       return ERROR_OK;
-               }
+               long bits;
+
+               e = Jim_GetLong(interp, args[i], &bits);
+               if (e != JIM_OK)
+                       return e;
        }
 
-       fields = malloc(sizeof(scan_field_t) * num_fields);
+       e = Jim_GetLong(interp, args[1], &device);
+       if (e != JIM_OK)
+               return e;
 
-       for (i = 0; i < argc; i+=2)
+       num_fields=(argc-2)/2;
+       fields = malloc(sizeof(scan_field_t) * num_fields);
+       for (i = 2; i < argc; i+=2)
        {
-               var = get_var_by_namenum(args[i+1]);
-       
-               for (j = 0; j < var->num_fields; j++)
-               {
-                       fields[field_count].device = strtol(args[i], NULL, 0);
-                       fields[field_count].num_bits = var->fields[j].num_bits;
-                       fields[field_count].out_value = malloc(CEIL(var->fields[j].num_bits, 8));
-                       buf_set_u32(fields[field_count].out_value, 0, var->fields[j].num_bits, var->fields[j].value);
-                       fields[field_count].out_mask = NULL;
-                       fields[field_count].in_value = fields[field_count].out_value;
-                       fields[field_count].in_check_mask = NULL;
-                       fields[field_count].in_check_value = NULL;
-                       fields[field_count].in_handler = field_le_to_host;
-                       fields[field_count++].in_handler_priv = &(var->fields[j]);
-               }
+               long bits;
+               int len;
+               const char *str;
+
+               Jim_GetLong(interp, args[i], &bits);
+               str = Jim_GetString(args[i+1], &len);
+               
+               fields[field_count].device = device;
+               fields[field_count].num_bits = bits;
+               fields[field_count].out_value = malloc(CEIL(bits, 8));
+               str_to_buf(str, len, fields[field_count].out_value, bits, 0);
+               fields[field_count].out_mask = NULL;
+               fields[field_count].in_value = fields[field_count].out_value;
+               fields[field_count].in_check_mask = NULL;
+               fields[field_count].in_check_value = NULL;
+               fields[field_count].in_handler = NULL;
+               fields[field_count++].in_handler_priv = NULL;
        }
 
        jtag_add_dr_scan(num_fields, fields, -1);
-       jtag_execute_queue();
-       
-       for (i = 0; i < argc / 2; i++)
-               free(fields[i].out_value);
+       retval = jtag_execute_queue();
+       if (retval != ERROR_OK)
+       {
+               Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
+               Jim_AppendStrings(interp, Jim_GetResult(interp), "drscan: jtag execute failed", NULL);
+               return JIM_ERR;
+       }
+
+       field_count=0;
+       Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
+       for (i = 2; i < argc; i+=2)
+       {
+               long bits;
+               char *str;
 
+               Jim_GetLong(interp, args[i], &bits);
+               str = buf_to_str(fields[field_count].in_value, bits, 16);
+               free(fields[field_count].out_value);
+
+               Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
+               free(str);
+               field_count++;
+       }
+
+       Jim_SetResult(interp, list);
+       
        free(fields);
 
-       return ERROR_OK;
+       return JIM_OK;
 }
 
 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)