- fix incorrect parsing of whitespace in command.c (thanks to Magnus Lundin)
[openocd.git] / src / jtag / jtag.c
index 4ba227c09b39b1d3480625609ac66e7c6b4ed1e4..cc9f8dc4502368b3e68cc964c033f44ae01738fc 100644 (file)
@@ -115,6 +115,10 @@ int jtag_verify_capture_ir = 1;
 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
 int jtag_ntrst_delay = 0; /* default to no nTRST delay */ 
 
+/* maximum number of JTAG devices expected in the chain
+ */
+#define JTAG_MAX_CHAIN_SIZE 20 
+
 /* callbacks to inform high-level handlers about JTAG state changes */
 jtag_event_callback_t *jtag_event_callbacks;
 
@@ -144,6 +148,10 @@ jtag_event_callback_t *jtag_event_callbacks;
        extern jtag_interface_t at91rm9200_interface;
 #endif
 
+#if BUILD_GW16012 == 1
+       extern jtag_interface_t gw16012_interface;
+#endif
+
 jtag_interface_t *jtag_interfaces[] = {
 #if BUILD_PARPORT == 1
        &parport_interface,
@@ -162,6 +170,9 @@ jtag_interface_t *jtag_interfaces[] = {
 #endif
 #if BUILD_AT91RM9200 == 1
        &at91rm9200_interface,
+#endif
+#if BUILD_GW16012 == 1
+       &gw16012_interface,
 #endif
        NULL,
 };
@@ -590,8 +601,7 @@ int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
                        /* if a device is listed, the BYPASS register must not be selected */
                        if (jtag_get_device(i)->bypass)
                        {
-                               ERROR("BUG: scan data for a device in BYPASS");
-                               exit(-1);
+                               WARNING("scan data for a device in BYPASS");
                        }
                }
        }
@@ -707,7 +717,7 @@ int jtag_add_pathmove(int num_states, enum tap_state *path)
                *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
                last_comand_pointer = &((*last_cmd)->next);
                (*last_cmd)->next = NULL;
-               (*last_cmd)->type = JTAG_RUNTEST;
+               (*last_cmd)->type = JTAG_PATHMOVE;
        
                (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
                (*last_cmd)->cmd.pathmove->num_states = num_states;
@@ -959,7 +969,7 @@ int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
                if (cmd->fields[i].out_value)
                {
 #ifdef _DEBUG_JTAG_IO_
-                       char* char_buf = buf_to_str(cmd->fields[i].out_value, cmd->fields[i].num_bits, 16);
+                       char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
 #endif
                        buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
 #ifdef _DEBUG_JTAG_IO_
@@ -993,7 +1003,7 @@ int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
                        #ifdef _DEBUG_JTAG_IO_
                                char *char_buf;
 
-                               char_buf = buf_to_str(captured, num_bits, 16);
+                               char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
                                DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
                                free(char_buf);
                        #endif
@@ -1031,9 +1041,9 @@ int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
                                if ((cmd->fields[i].in_check_mask && buf_cmp_mask(captured, cmd->fields[i].in_check_value, cmd->fields[i].in_check_mask, num_bits))
                                        || (!cmd->fields[i].in_check_mask && buf_cmp(captured, cmd->fields[i].in_check_mask, num_bits)))
                                {
-                                       char *captured_char = buf_to_str(captured, num_bits, 16);
-                                       char *in_check_value_char = buf_to_str(cmd->fields[i].in_check_value, num_bits, 16);
-                                       char *in_check_mask_char = buf_to_str(cmd->fields[i].in_check_mask, num_bits, 16);
+                                       char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
+                                       char *in_check_value_char = buf_to_str(cmd->fields[i].in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
+                                       char *in_check_mask_char = buf_to_str(cmd->fields[i].in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
                                        /* TODO: error reporting */
                                        WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s check_mask: 0x%s", captured_char, in_check_value_char, in_check_mask_char);
                                        retval = ERROR_JTAG_QUEUE_FAILED;
@@ -1109,6 +1119,93 @@ void jtag_sleep(u32 us)
        usleep(us);
 }
 
+/* Try to examine chain layout according to IEEE 1149.1 ยง12
+ */
+int jtag_examine_chain()
+{
+       scan_field_t field;
+       u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
+       int i;
+       int bit_count;
+       int device_count = 0;
+       u8 valid = 0x0;
+       
+       field.device = 0;
+       field.num_bits = sizeof(idcode_buffer) * 8;
+       field.out_value = idcode_buffer;
+       field.out_mask = NULL;
+       field.in_value = idcode_buffer;
+       field.in_check_value = NULL;
+       field.in_check_mask = NULL;
+       field.in_handler = NULL;
+       field.in_handler_priv = NULL;
+       
+       for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
+       {
+               buf_set_u32(idcode_buffer, 0, 32, 0x000000FF);
+       }
+       
+       jtag_add_plain_dr_scan(1, &field, TAP_TLR);
+       jtag_execute_queue();
+       
+       for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
+       {
+               valid |= idcode_buffer[i];
+       }
+       
+       /* if there wasn't a single non-zero bit, the scan isn't valid */
+       if (!valid)
+       {
+               ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
+               exit(-1);
+       }
+       
+       for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
+       {
+               u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
+               if ((idcode & 1) == 0)
+               {
+                       /* LSB must not be 0, this indicates a device in bypass */
+                       device_count++;
+                       
+                       bit_count += 1;
+               }
+               else
+               {
+                       u32 manufacturer;
+                       u32 part;
+                       u32 version;
+                       
+                       if (idcode == 0x000000FF)
+                       {
+                               /* End of chain (invalid manufacturer ID) */
+                               break;
+                       }
+                       
+                       device_count++;
+                       
+                       manufacturer = (idcode & 0xffe) >> 1;
+                       part = (idcode & 0xffff000) >> 12;
+                       version = (idcode & 0xf0000000) >> 28;
+
+                       DEBUG("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x", 
+                               idcode, manufacturer, part, version);
+                       
+                       bit_count += 32;
+               }
+       }
+       
+       /* see if number of discovered devices matches configuration */
+       if (device_count != jtag_num_devices)
+       {
+               ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)", 
+                       device_count, jtag_num_devices);
+               exit(-1);
+       }
+       
+       return ERROR_OK;
+}
+
 int jtag_validate_chain()
 {
        jtag_device_t *device = jtag_devices;
@@ -1237,6 +1334,8 @@ int jtag_init(struct command_context_s *cmd_ctx)
                                jtag_add_statemove(TAP_TLR);
                                jtag_execute_queue();
                                
+                               jtag_examine_chain();
+                               
                                jtag_validate_chain();
                                
                                return ERROR_OK;

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)