- added myself to copyright on files i remember adding large contributions for over...
[openocd.git] / src / jtag / jtag.c
index 6199ed29cdb3381387d2b7e9f2047f25e6802431..90d52d0f0093c0a91e1b8da51c9ad06616b2156c 100644 (file)
@@ -2,6 +2,9 @@
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
+ *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
+ *   oyvind.harboe@zylin.com                                               *
+ *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
@@ -249,7 +252,7 @@ void jtag_add_runtest(int num_cycles, enum tap_state endstate);
 void jtag_add_end_state(enum tap_state endstate);
 void jtag_add_sleep(u32 us);
 int jtag_execute_queue(void);
-int jtag_cancel_queue(void);
+
 
 /* jtag commands */
 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
@@ -398,7 +401,7 @@ void* cmd_queue_alloc(size_t size)
        return t + offset;
 }
 
-void cmd_queue_free()
+void cmd_queue_free(void)
 {
        cmd_queue_page_t *page = cmd_queue_pages;
 
@@ -413,7 +416,7 @@ void cmd_queue_free()
        cmd_queue_pages = NULL;
 }
 
-static void jtag_prelude1()
+static void jtag_prelude1(void)
 {
        if (jtag_trst == 1)
        {
@@ -808,7 +811,7 @@ int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *f
        return ERROR_OK;
 }
 
-void jtag_add_tlr()
+void jtag_add_tlr(void)
 {
        jtag_prelude(TAP_TLR);
        
@@ -851,6 +854,11 @@ void jtag_add_pathmove(int num_states, enum tap_state *path)
 
        for (i=0; i<num_states; i++)
        {
+               if (path[i] == TAP_TLR)
+               {
+                       LOG_ERROR("BUG: TAP_TLR is not a valid state for pathmove sequences");
+                       exit(-1);
+               }
                if ((tap_transitions[cur_state].low != path[i])&&
                                (tap_transitions[cur_state].high != path[i]))
                {
@@ -862,9 +870,9 @@ void jtag_add_pathmove(int num_states, enum tap_state *path)
        
        jtag_prelude1();
        
-       cmd_queue_cur_state = path[num_states - 1];
 
        retval=interface_jtag_add_pathmove(num_states, path);
+       cmd_queue_cur_state = path[num_states - 1];
        if (retval!=ERROR_OK)
                jtag_error=retval;
 }
@@ -1042,7 +1050,7 @@ int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
 void jtag_add_end_state(enum tap_state state)
 {
        cmd_queue_end_state = state;
-       if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SD))
+       if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SI))
        {
                LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
        }
@@ -1303,12 +1311,12 @@ int jtag_reset_callback(enum jtag_event event, void *priv)
 
 void jtag_sleep(u32 us)
 {
-       usleep(us);
+       alive_sleep(us/1000);
 }
 
 /* Try to examine chain layout according to IEEE 1149.1 §12
  */
-int jtag_examine_chain()
+int jtag_examine_chain(void)
 {
        jtag_device_t *device = jtag_devices;
        scan_field_t field;
@@ -1356,7 +1364,8 @@ int jtag_examine_chain()
                if ((idcode & 1) == 0)
                {
                        /* LSB must not be 0, this indicates a device in bypass */
-                       device_count++;
+                       LOG_WARNING("Device does not have IDCODE");
+                       idcode=0;
                        
                        bit_count += 1;
                }
@@ -1368,17 +1377,31 @@ int jtag_examine_chain()
                        
                        if (idcode == 0x000000FF)
                        {
-                               /* End of chain (invalid manufacturer ID) */
+                               int unexpected=0;
+                               /* End of chain (invalid manufacturer ID) 
+                                * 
+                                * The JTAG examine is the very first thing that happens
+                                * 
+                                * A single JTAG device requires only 64 bits to be read back correctly.
+                                * 
+                                * The code below adds a check that the rest of the data scanned (640 bits)
+                                * are all as expected. This helps diagnose/catch problems with the JTAG chain
+                                * 
+                                * earlier and gives more helpful/explicit error messages.
+                                */
+                               for (bit_count += 32; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;bit_count += 32) 
+                               {
+                                       idcode = buf_get_u32(idcode_buffer, bit_count, 32);
+                                       if (unexpected||(idcode != 0x000000FF))
+                                       {
+                                               LOG_WARNING("Unexpected idcode after end of chain! %d 0x%08x", bit_count, idcode);
+                                               unexpected = 1;
+                                       }
+                               }
+                               
                                break;
                        }
                        
-                       if (device)
-                       {
-                               device->idcode = idcode;
-                               device = device->next;
-                       }
-                       device_count++;
-                       
                        manufacturer = (idcode & 0xffe) >> 1;
                        part = (idcode & 0xffff000) >> 12;
                        version = (idcode & 0xf0000000) >> 28;
@@ -1388,6 +1411,12 @@ int jtag_examine_chain()
                        
                        bit_count += 32;
                }
+               if (device)
+               {
+                       device->idcode = idcode;
+                       device = device->next;
+               }
+               device_count++;
        }
        
        /* see if number of discovered devices matches configuration */
@@ -1402,7 +1431,7 @@ int jtag_examine_chain()
        return ERROR_OK;
 }
 
-int jtag_validate_chain()
+int jtag_validate_chain(void)
 {
        jtag_device_t *device = jtag_devices;
        int total_ir_length = 0;
@@ -1462,8 +1491,69 @@ int jtag_validate_chain()
        return ERROR_OK;
 }
 
+
+static int
+jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
+{
+       Jim_GetOptInfo goi;
+       int e;
+       Jim_Nvp *n;
+       struct command_context_s *context;
+
+       enum {
+               JTAG_CMD_INTERFACE,
+               JTAG_CMD_INIT_RESET,
+       };
+       
+       const Jim_Nvp jtag_cmds[] = {
+               { .name = "interface"     , .value = JTAG_CMD_INTERFACE },
+               { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
+               
+               { .name = NULL, .value = -1 },
+       };
+
+       context = Jim_GetAssocData(interp, "context");
+       // go past the command 
+       Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
+
+       e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
+       if( e != JIM_OK ){
+               Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
+               return e;
+       }
+               Jim_SetEmptyResult( goi.interp );
+       switch( n->value ){
+       case JTAG_CMD_INTERFACE:
+               // return the name of the interface
+               // TCL code might need to know the exact type...
+               // FUTURE: we allow this as a means to "set" the interface.
+               if( goi.argc != 0 ){
+                       Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
+                       return JIM_ERR;
+               }
+               Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
+               return JIM_OK;
+       case JTAG_CMD_INIT_RESET:
+               if( goi.argc != 0 ){
+                       Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
+                       return JIM_ERR;
+               }
+               e = jtag_init_reset(context);
+               if( e != ERROR_OK ){
+                       Jim_SetResult_sprintf( goi.interp, "error: %d", e);
+                       return JIM_ERR;
+               }
+               return JIM_OK;
+       }
+               
+
+       return JIM_ERR;
+}
+
 int jtag_register_commands(struct command_context_s *cmd_ctx)
 {
+       register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
+
        register_command(cmd_ctx, NULL, "interface", handle_interface_command,
                COMMAND_CONFIG, NULL);
        register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
@@ -1525,7 +1615,6 @@ int jtag_interface_init(struct command_context_s *cmd_ctx)
 
 static int jtag_init_inner(struct command_context_s *cmd_ctx)
 {
-       int validate_tries = 0;
        jtag_device_t *device;
        int retval;
 
@@ -1551,16 +1640,9 @@ static int jtag_init_inner(struct command_context_s *cmd_ctx)
                LOG_ERROR("trying to validate configured JTAG chain anyway...");
        }
        
-       while (jtag_validate_chain() != ERROR_OK)
+       if (jtag_validate_chain() != ERROR_OK)
        {
-               validate_tries++;
-               
-               if (validate_tries > 5)
-               {
-                       LOG_ERROR("Could not validate JTAG chain, exit");
-                       return ERROR_JTAG_INVALID_INTERFACE;
-               }
-               usleep(10000);
+               LOG_ERROR("Could not validate JTAG chain, continuing anyway...");
        }
        
        return ERROR_OK;
@@ -1631,6 +1713,7 @@ static int default_khz(int khz, int *jtag_speed)
 
 static int default_speed_div(int speed, int *khz)
 {
+       LOG_ERROR("Translation from jtag_speed to khz not implemented");
        return ERROR_FAIL;      
 }
 
@@ -1866,7 +1949,7 @@ int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char
        {
        } else
        {
-               retval=ERROR_COMMAND_SYNTAX_ERROR;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
        command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
        
@@ -1903,8 +1986,15 @@ int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char *
        {
        } else
        {
-               retval=ERROR_COMMAND_SYNTAX_ERROR;
+               return ERROR_COMMAND_SYNTAX_ERROR;
        }
+
+       if (jtag!=NULL) 
+       {
+               if ((retval=jtag->speed_div(jtag_speed, &speed_khz))!=ERROR_OK)
+                       return retval;
+       }
+       
        command_print(cmd_ctx, "jtag_khz: %d", speed_khz);
        return retval;
 

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)