- rename log functions to stop conflicts under win32 (wingdi)
[openocd.git] / src / jtag / bitbang.c
index b5ea8fe284773049127a2765f55a0fa7d79d8f37..054faf503be0f5fff3cccb2fdf083733682d9323 100644 (file)
@@ -41,13 +41,16 @@ bitbang_interface_t *bitbang_interface;
 
 int bitbang_execute_queue(void);
 
+/* The bitbang driver leaves the TCK 0 when in idle */
+
+
 void bitbang_end_state(enum tap_state state)
 {
        if (tap_move_map[state] != -1)
                end_state = state;
        else
        {
-               ERROR("BUG: %i is not a valid end state", state);
+               LOG_ERROR("BUG: %i is not a valid end state", state);
                exit(-1);
        }
 }
@@ -72,31 +75,35 @@ void bitbang_path_move(pathmove_command_t *cmd)
 {
        int num_states = cmd->num_states;
        int state_count;
+       int tms;
 
        state_count = 0;
        while (num_states)
        {
                if (tap_transitions[cur_state].low == cmd->path[state_count])
                {
-                       bitbang_interface->write(0, 0, 0);
-                       bitbang_interface->write(1, 0, 0);
+                       tms = 0;
                }
                else if (tap_transitions[cur_state].high == cmd->path[state_count])
                {
-                       bitbang_interface->write(0, 1, 0);
-                       bitbang_interface->write(1, 1, 0);
-               }                       
+                       tms = 1;
+               }
                else
                {
-                       ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
+                       LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
                        exit(-1);
                }
                
+               bitbang_interface->write(0, tms, 0);
+               bitbang_interface->write(1, tms, 0);
+
                cur_state = cmd->path[state_count];
                state_count++;
                num_states--;
        }
        
+       bitbang_interface->write(0, tms, 0);
+
        end_state = cur_state;
 }
 
@@ -131,6 +138,7 @@ void bitbang_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
 {
        enum tap_state saved_end_state = end_state;
        int bit_cnt;
+       int last_bit, last_bit_in;
        
        if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
        {
@@ -143,7 +151,7 @@ void bitbang_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
                bitbang_end_state(saved_end_state);
        }
 
-       for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++)
+       for (bit_cnt = 0; bit_cnt < scan_size - 1; bit_cnt++)
        {
                /* if we're just reading the scan, but don't care about the output
                 * default to outputting 'low', this also makes valgrind traces more readable,
@@ -151,11 +159,11 @@ void bitbang_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
                 */ 
                if ((type != SCAN_IN) && ((buffer[bit_cnt/8] >> (bit_cnt % 8)) & 0x1))
                {
-                       bitbang_interface->write(0, (bit_cnt==scan_size-1) ? 1 : 0, 1);
-                       bitbang_interface->write(1, (bit_cnt==scan_size-1) ? 1 : 0, 1);
+                       bitbang_interface->write(0, 0, 1);
+                       bitbang_interface->write(1, 0, 1);
                } else {
-                       bitbang_interface->write(0, (bit_cnt==scan_size-1) ? 1 : 0, 0);
-                       bitbang_interface->write(1, (bit_cnt==scan_size-1) ? 1 : 0, 0);
+                       bitbang_interface->write(0, 0, 0);
+                       bitbang_interface->write(1, 0, 0);
                }
                
                if (type != SCAN_OUT)
@@ -166,18 +174,54 @@ void bitbang_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
                                buffer[(bit_cnt)/8] &= ~(1 << ((bit_cnt) % 8));
                }
        }
-       
-       /* Exit1 -> Pause */
-       bitbang_interface->write(0, 0, 0);
-       bitbang_interface->write(1, 0, 0);
-       
-       if (ir_scan)
-               cur_state = TAP_PI;
+
+       if ((type != SCAN_IN) && ((buffer[bit_cnt/8] >> (bit_cnt % 8)) & 0x1))
+               last_bit = 1;
        else
-               cur_state = TAP_PD;
-       
-       if (cur_state != end_state)
-               bitbang_state_move();
+               last_bit = 0;
+
+       if ((ir_scan && (end_state == TAP_SI)) ||
+               (!ir_scan && (end_state == TAP_SD)))
+       {
+               bitbang_interface->write(0, 0, last_bit);
+               bitbang_interface->write(1, 0, last_bit);
+
+               if (type != SCAN_OUT)
+                       last_bit_in = bitbang_interface->read();
+
+               bitbang_interface->write(0, 0, last_bit);
+       }
+       else
+       {
+               /* Shift-[ID]R -> Exit1-[ID]R */
+               bitbang_interface->write(0, 1, last_bit);
+               bitbang_interface->write(1, 1, last_bit);
+               
+               if (type != SCAN_OUT)
+                       last_bit_in = bitbang_interface->read();
+
+               /* Exit1-[ID]R -> Pause-[ID]R */
+               bitbang_interface->write(0, 0, 0);
+               bitbang_interface->write(1, 0, 0);
+               
+               if (cur_state == TAP_SI)
+                       cur_state = TAP_PI;
+               else
+                       cur_state = TAP_PD;
+
+               if (cur_state != end_state)
+                       bitbang_state_move();
+               else
+                       bitbang_interface->write(0, 0, 0);
+       }
+               
+       if (type != SCAN_OUT)
+       {
+               if (last_bit_in)
+                       buffer[(bit_cnt)/8] |= 1 << ((bit_cnt) % 8);
+               else
+                       buffer[(bit_cnt)/8] &= ~(1 << ((bit_cnt) % 8));
+       }
 }
 
 int bitbang_execute_queue(void)
@@ -190,7 +234,7 @@ int bitbang_execute_queue(void)
        
        if (!bitbang_interface)
        {
-               ERROR("BUG: Bitbang interface called, but not yet initialized");
+               LOG_ERROR("BUG: Bitbang interface called, but not yet initialized");
                exit(-1);
        }
        
@@ -208,14 +252,14 @@ int bitbang_execute_queue(void)
                {
                        case JTAG_END_STATE:
 #ifdef _DEBUG_JTAG_IO_
-                               DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
+                               LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
 #endif
                                if (cmd->cmd.end_state->end_state != -1)
                                        bitbang_end_state(cmd->cmd.end_state->end_state);
                                break;
                        case JTAG_RESET:
 #ifdef _DEBUG_JTAG_IO_
-                               DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
+                               LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
 #endif
                                if (cmd->cmd.reset->trst == 1)
                                {
@@ -225,7 +269,7 @@ int bitbang_execute_queue(void)
                                break;
                        case JTAG_RUNTEST:
 #ifdef _DEBUG_JTAG_IO_
-                               DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
+                               LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
 #endif
                                if (cmd->cmd.runtest->end_state != -1)
                                        bitbang_end_state(cmd->cmd.runtest->end_state);
@@ -233,7 +277,7 @@ int bitbang_execute_queue(void)
                                break;
                        case JTAG_STATEMOVE:
 #ifdef _DEBUG_JTAG_IO_
-                               DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
+                               LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
 #endif
                                if (cmd->cmd.statemove->end_state != -1)
                                        bitbang_end_state(cmd->cmd.statemove->end_state);
@@ -241,13 +285,13 @@ int bitbang_execute_queue(void)
                                break;
                        case JTAG_PATHMOVE:
 #ifdef _DEBUG_JTAG_IO_
-                               DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
+                               LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
 #endif
                                bitbang_path_move(cmd->cmd.pathmove);
                                break;
                        case JTAG_SCAN:
 #ifdef _DEBUG_JTAG_IO_
-                               DEBUG("%s scan end in %i",  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state);
+                               LOG_DEBUG("%s scan end in %i",  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state);
 #endif
                                if (cmd->cmd.scan->end_state != -1)
                                        bitbang_end_state(cmd->cmd.scan->end_state);
@@ -261,12 +305,12 @@ int bitbang_execute_queue(void)
                                break;
                        case JTAG_SLEEP:
 #ifdef _DEBUG_JTAG_IO_
-                               DEBUG("sleep %i", cmd->cmd.sleep->us);
+                               LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
 #endif
                                jtag_sleep(cmd->cmd.sleep->us);
                                break;
                        default:
-                               ERROR("BUG: unknown JTAG command type encountered");
+                               LOG_ERROR("BUG: unknown JTAG command type encountered");
                                exit(-1);
                }
                cmd = cmd->next;

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)