changeset 13:1ea479d26fce tip

Make sure shifting phase and add bypass.py. - shifting phase is started after entering SHIFT state Transition from CAP to SHIFT does not induce shifting. - shifting phase is stoped after leaving SHIFT state. Transition from SHIFT to EXIT1 also induce a bit of shifting.
author Thinker K.F. Li <thinker@branda.to>
date Wed, 25 Feb 2009 20:08:29 +0800
parents 68ecd42850d3
children
files py_avrjtag/jtagdev.py py_avrjtag/tms_ptns.py src/jtag.c tests/bypasstest.py
diffstat 4 files changed, 162 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/py_avrjtag/jtagdev.py	Tue Feb 24 18:53:16 2009 +0800
+++ b/py_avrjtag/jtagdev.py	Wed Feb 25 20:08:29 2009 +0800
@@ -3,6 +3,14 @@
 
 debug_frame = 0
 
+def dump_frame(frame):
+    r = ''
+    for c in frame:
+        r = r + ('%02x ' % (ord(c)))
+        pass
+    print r
+    pass
+
 class jtagdev(object):
     ST_RESET = 0
     ST_IDLE = 1
@@ -13,7 +21,7 @@
     
     def __init__(self, fo):
         self.fo = fo
-        self.state = self.ST_IDLE
+        self.state = self.ST_RESET
         self.seq = 0
         pass
 
@@ -58,7 +66,7 @@
         if not frame:
             return None
         if debug_frame:
-            print repr(frame)
+            dump_frame(frame)
             pass
         
         cmd = cmd_proto.cmd()
@@ -85,13 +93,14 @@
             return
         
         if self.state == self.ST_IDLE:
-            ptn, ptn_nbits = tms_ptns.TMS_CAP_IR
-            self._send_tms(ptn, ptn_nbits)
-        elif self.state == self.ST_SHIFT__DR:
-            ptn, ptn_nbits = tms_ptns.TMS_SHIFT_DR_2_CAP_IR
-            self._send_tms(ptn, ptn_nbits)
+            ptn, ptn_nbits = tms_ptns.TMS_SHIFT_IR
+        elif self.state == self.ST_EXIT1_DR:
+            ptn, ptn_nbits = tms_ptns.TMS_EXIT1_DR_2_SHIFT_IR
+        elif self.state == self.ST_EXIT1_IR:
+            ptn, ptn_nbits = tms_ptns.TMS_EXIT1_IR_2_SHIFT_IR
         else:
             raise RuntimeError, 'Transite to shift IR state from invalid state'
+        self._send_tms(ptn, ptn_nbits)
         self.state = self.ST_SHIFT_IR
         pass
 
@@ -100,14 +109,15 @@
             return
         
         if self.state == self.ST_IDLE:
-            ptn, ptn_nbits = tms_ptns.TMS_CAP_DR
-            self._send_tms(ptn, ptn_nbits)
-        elif self.state == self.ST_SHIFT_IR:
-            ptn, ptn_nbits = tms_ptns.TMS_SHIFT_IR_2_CAP_DR
-            self._send_tms(ptn, ptn_nbits)
+            ptn, ptn_nbits = tms_ptns.TMS_SHIFT_DR
+        elif self.state == self.ST_EXIT1_IR:
+            ptn, ptn_nbits = tms_ptns.TMS_EXIT1_IR_2_SHIFT_DR
+        elif self.state == self.ST_EXIT1_DR:
+            ptn, ptn_nbits = tms_ptns.TMS_EXIT1_DR_2_SHIFT_DR
         else:
             raise RuntimeError, 'Transite to shift DR state from invalid state'
             pass
+        self._send_tms(ptn, ptn_nbits)
         self.state = self.ST_SHIFT_DR
         pass
 
@@ -171,15 +181,15 @@
     return (ord(data[byte_off]) >> bit_off) & 0x1
 
 def identify_components(dev):
-    dev.go_shift_IR()
-    reply = dev.wait_reply()
-    if reply.code != cmd_proto.CPCMD_ACK:
-        raise RuntimeError, 'invalid reply code 0x%02x' % (reply.code)
+#     dev.go_shift_IR()
+#     reply = dev.wait_reply()
+#     if reply.code != cmd_proto.CPCMD_ACK:
+#         raise RuntimeError, 'invalid reply code 0x%02x' % (reply.code)
     
-    dev.shift_IR('\x02', 4)
-    reply = dev.wait_reply()
-    if reply.code != cmd_proto.CPCMD_ACK:
-        raise RuntimeError, 'invalid reply code 0x%02x' % (reply.code)
+#     dev.shift_IR('\x04', 4)
+#     reply = dev.wait_reply()
+#     if reply.code != cmd_proto.CPCMD_ACK:
+#         raise RuntimeError, 'invalid reply code 0x%02x' % (reply.code)
 
     dev.go_shift_DR()
     reply = dev.wait_reply()
--- a/py_avrjtag/tms_ptns.py	Tue Feb 24 18:53:16 2009 +0800
+++ b/py_avrjtag/tms_ptns.py	Wed Feb 25 20:08:29 2009 +0800
@@ -18,5 +18,13 @@
 TMS_EXIT1_2_PAUSE = ('\x00', 1)
 TMS_EXIT1_DR_2_PAUSE = TMS_EXIT1_2_PAUSE
 TMS_EXIT1_IR_2_PAUSE = TMS_EXIT1_2_PAUSE
-TMS_EXIT1_IR_2_SHIFT_DR = ('\x03', 3)
-TMS_EXIT1_DR_2_SHIFT_IR = ('\x07', 4)
+TMS_EXIT1_IR_2_SHIFT_DR = ('\x03', 4)
+TMS_EXIT1_DR_2_SHIFT_IR = ('\x07', 5)
+TMS_EXIT1_2_EXIT2 = ('\x02', 2)
+TMS_EXIT1_DR_2_EXIT2_DR = TMS_EXIT1_2_EXIT2
+TMS_EXIT1_IR_2_EXIT2_IR = TMS_EXIT1_2_EXIT2
+TMS_EXIT1_2_SHIFT = ('\x02', 3)
+TMS_EXIT1_DR_2_SHIFT_DR = TMS_EXIT1_2_SHIFT
+TMS_EXIT1_IR_2_SHIFT_IR = TMS_EXIT1_2_SHIFT
+TMS_EXIT1_DR_2_CAP_IR = ('\x07', 4)
+TMS_EXIT1_IR_2_CAP_DR = ('\x03', 3)
--- a/src/jtag.c	Tue Feb 24 18:53:16 2009 +0800
+++ b/src/jtag.c	Wed Feb 25 20:08:29 2009 +0800
@@ -4,7 +4,7 @@
 #include "avriotools.h"
 
 /* It is supposed to work at 1Mbps */
-#define CLK_DELAY() _delay_us(1)
+#define CLK_DELAY() _delay_us(2)
 
 void jtag_init(void) {
     pin_mode(&JTAG_PORT, JTAG_TCK, PM_OUTPUT);
@@ -18,41 +18,50 @@
     pin_hi(JTAG_PORT, JTAG_TRST);
 }
 
-#define TCK_LO() pin_lo(JTAG_PORT, JTAG_TCK)
-#define TCK_HI() pin_hi(JTAG_PORT, JTAG_TCK)
-#define SEND_BIT(pin, bit)			\
+#define _TDI_TMS_TCK(tdi, tms, tck)		\
+    ((tms? _BV(JTAG_TMS): 0) |			\
+     (tdi? _BV(JTAG_TDI): 0) |			\
+     (tck? _BV(JTAG_TCK): 0))
+#define _SET_PINS(pv, tdi, tms, tck)		\
     do {					\
-	if(bit)					\
-	    pin_hi(JTAG_PORT, pin);		\
-	else					\
-	    pin_lo(JTAG_PORT, pin);		\
+	pv = JTAG_PORT;				\
+	pv &= ~_TDI_TMS_TCK(1, 1, 1);		\
+	pv |= _TDI_TMS_TCK(tdi, tms, tck);	\
+	JTAG_PORT = pv;				\
+    } while(0)
+#define SEND_BIT(pv, tdi, tms)			\
+    do {					\
+	_SET_PINS(pv, tdi, tms, 0);		\
 	CLK_DELAY();				\
-	TCK_HI();				\
+	_SET_PINS(pv, tdi, tms, 1);		\
 	CLK_DELAY();				\
-	TCK_LO();				\
     } while(0)
 #define GET_TDO() (JTAG_PIN & _BV(JTAG_TDO))
-#define SEND_GET_BIT(pin, bit, out)		\
+#define SEND_GET_BIT(pv, tdi, tms, out)		\
     do {					\
-	if(bit)					\
-	    pin_hi(JTAG_PORT, pin);		\
-	else					\
-	    pin_lo(JTAG_PORT, pin);		\
-	CLK_DELAY();				\
-	TCK_HI();				\
+	_SET_PINS(pv, tdi, tms, 0);		\
 	CLK_DELAY();				\
 	out = GET_TDO();			\
-	TCK_LO();				\
+	_SET_PINS(pv, tdi, tms, 1);		\
+	CLK_DELAY();				\
     } while(0)
 
 void jtag_trst(void) {
+    unsigned char pv;
+    
+    pin_lo(JTAG_PORT, JTAG_TCK);
     pin_lo(JTAG_PORT, JTAG_TRST);
-    SEND_BIT(JTAG_TMS, 1);
-    SEND_BIT(JTAG_TMS, 1);
-    SEND_BIT(JTAG_TMS, 1);
-    SEND_BIT(JTAG_TMS, 1);
-    SEND_BIT(JTAG_TMS, 1);
+    
+    SEND_BIT(pv, 1, 1);
+    SEND_BIT(pv, 1, 1);
+    SEND_BIT(pv, 1, 1);
+    SEND_BIT(pv, 1, 1);
+    SEND_BIT(pv, 1, 1);
+    
+    pin_lo(JTAG_PORT, JTAG_TCK);
     pin_hi(JTAG_PORT, JTAG_TRST);
+    
+    SEND_BIT(pv, 1, 1);
 }
 
 /*!
@@ -62,124 +71,119 @@
  */
 void jtag_tms(unsigned char *buf, int nbits) {
     int i;
-    int nbytes, byte;
+    int nbytes;
     int byteoff, bitoff;
-    int bit;
+    unsigned char byte;
+    unsigned char bit;
+    unsigned char pv;
     
-    pin_hi(JTAG_PORT, JTAG_TDI);
-
     nbytes = nbits / 8;
     for(i = 0; i < nbytes; i++) {
 	byte = buf[i];
 	
 	bit = byte & 0x01;
-	SEND_BIT(JTAG_TMS, bit);
+	SEND_BIT(pv, 1, bit);
 	
 	bit = byte & 0x02;
-	SEND_BIT(JTAG_TMS, bit);
+	SEND_BIT(pv, 1, bit);
 	
 	bit = byte & 0x04;
-	SEND_BIT(JTAG_TMS, bit);
+	SEND_BIT(pv, 1, bit);
 	
 	bit = byte & 0x08;
-	SEND_BIT(JTAG_TMS, bit);
+	SEND_BIT(pv, 1, bit);
 	
 	bit = byte & 0x10;
-	SEND_BIT(JTAG_TMS, bit);
+	SEND_BIT(pv, 1, bit);
 	
 	bit = byte & 0x20;
-	SEND_BIT(JTAG_TMS, bit);
+	SEND_BIT(pv, 1, bit);
 	
 	bit = byte & 0x40;
-	SEND_BIT(JTAG_TMS, bit);
+	SEND_BIT(pv, 1, bit);
 	
 	bit = byte & 0x80;
-	SEND_BIT(JTAG_TMS, bit);
+	SEND_BIT(pv, 1, bit);
     }
     
     byte = buf[i];
     nbits %= 8;
     for(i = 0; i < nbits; i++) {
 	bit = byte & (1 << i);
-	SEND_BIT(JTAG_TMS, bit);
+	SEND_BIT(pv, 1, bit);
     }
 }
 
 void jtag_shift(unsigned char *buf, int nbits) {
     int i;
     int nbits_1;
-    int nbytes, byte;
+    int nbytes;
+    int remain;
     int byteoff, bitoff;
-    int bit, remain;
+    unsigned char byte;
+    unsigned char bit;
+    unsigned char pv;
 
     if(nbits == 0)
 	return;
     
-    /* Transite to shift state.
-     * \sa jtag_tms()
-     */
-    pin_lo(JTAG_PORT, JTAG_TMS);
-
     nbits_1 = nbits - 1;
     nbytes = nbits_1 / 8;
     for(i = 0; i < nbytes; i++) {
 	byte = buf[i];
 	
 	bit = byte & 0x01;
-	SEND_BIT(JTAG_TDI, bit);
+	SEND_BIT(pv, bit, 0);
 	
 	bit = byte & 0x02;
-	SEND_BIT(JTAG_TDI, bit);
+	SEND_BIT(pv, bit, 0);
 	
 	bit = byte & 0x04;
-	SEND_BIT(JTAG_TDI, bit);
+	SEND_BIT(pv, bit, 0);
 	
 	bit = byte & 0x08;
-	SEND_BIT(JTAG_TDI, bit);
+	SEND_BIT(pv, bit, 0);
 	
 	bit = byte & 0x10;
-	SEND_BIT(JTAG_TDI, bit);
+	SEND_BIT(pv, bit, 0);
 	
 	bit = byte & 0x20;
-	SEND_BIT(JTAG_TDI, bit);
+	SEND_BIT(pv, bit, 0);
 	
 	bit = byte & 0x40;
-	SEND_BIT(JTAG_TDI, bit);
+	SEND_BIT(pv, bit, 0);
 	
 	bit = byte & 0x80;
-	SEND_BIT(JTAG_TDI, bit);
+	SEND_BIT(pv, bit, 0);
     }
     
     remain = nbits_1 % 8;
     byte = buf[i];
     for(i = 0; i < remain; i++) {
 	bit = byte & (1 << i);
-	SEND_BIT(JTAG_TDI, bit);
+	SEND_BIT(pv, bit, 0);
     }
 
     byte = buf[nbits / 8];
     bit = byte & (1 << (nbits_1 % 8));
     
-    pin_hi(JTAG_PORT, JTAG_TMS);
-    SEND_BIT(JTAG_TDI, bit);
+    SEND_BIT(pv, bit, 1);
 }
 
 void jtag_shift_inout(unsigned char *ibuf, unsigned char *obuf, int nbits) {
     int i, j;
     int nbits_1;
-    int nbytes, byte, obyte;
+    int nbytes;
     int tdo;
     int byteoff, bitoff;
-    int bit, remain;
+    int remain;
+    unsigned char byte, obyte;
+    unsigned char bit;
+    unsigned char pv;
     
     if(nbits == 0)
 	return;
     
-    /* Transite to shift state.
-     * \sa jtag_tms()
-     */
-    pin_lo(JTAG_PORT, JTAG_TMS);
-
     nbits_1 = nbits - 1;
     nbytes = nbits_1 / 8;
     for(i = 0; i < nbytes; i++) {
@@ -187,61 +191,45 @@
 	obyte = 0;
 	
 	bit = byte & 0x01;
-	SEND_GET_BIT(JTAG_TDI, bit, tdo);
+	SEND_GET_BIT(pv, bit, 0, tdo);
 	if(tdo)
 	    obyte |= 0x01;
-	else
-	    obyte &= ~0x01;
 	
 	bit = byte & 0x02;
-	SEND_GET_BIT(JTAG_TDI, bit, tdo);
+	SEND_GET_BIT(pv, bit, 0, tdo);
 	tdo = GET_TDO();
 	if(tdo)
 	    obyte |= 0x02;
-	else
-	    obyte &= ~0x02;
 	
 	bit = byte & 0x04;
-	SEND_GET_BIT(JTAG_TDI, bit, tdo);
+	SEND_GET_BIT(pv, bit, 0, tdo);
 	if(tdo)
 	    obyte |= 0x04;
-	else
-	    obyte &= ~0x04;
 	
 	bit = byte & 0x08;
-	SEND_GET_BIT(JTAG_TDI, bit, tdo);
+	SEND_GET_BIT(pv, bit, 0, tdo);
 	if(tdo)
 	    obyte |= 0x08;
-	else
-	    obyte &= ~0x08;
 	
 	bit = byte & 0x10;
-	SEND_GET_BIT(JTAG_TDI, bit, tdo);
+	SEND_GET_BIT(pv, bit, 0, tdo);
 	if(tdo)
 	    obyte |= 0x10;
-	else
-	    obyte &= ~0x10;
 	
 	bit = byte & 0x20;
-	SEND_GET_BIT(JTAG_TDI, bit, tdo);
+	SEND_GET_BIT(pv, bit, 0, tdo);
 	if(tdo)
 	    obyte |= 0x20;
-	else
-	    obyte &= ~0x20;
 	
 	bit = byte & 0x40;
-	SEND_GET_BIT(JTAG_TDI, bit, tdo);
+	SEND_GET_BIT(pv, bit, 0, tdo);
 	if(tdo)
 	    obyte |= 0x40;
-	else
-	    obyte &= ~0x40;
 	
 	bit = byte & 0x80;
-	SEND_GET_BIT(JTAG_TDI, bit, tdo);
+	SEND_GET_BIT(pv, bit, 0, tdo);
 	if(tdo)
 	    obyte |= 0x80;
-	else
-	    obyte &= ~0x80;
 
 	obuf[i] = obyte;
     }
@@ -251,7 +239,7 @@
     obyte = 0;
     for(j = 0; j < remain; j++) {
 	bit = byte & (1 << j);
-	SEND_GET_BIT(JTAG_TDI, bit, tdo);
+	SEND_GET_BIT(pv, bit, 0, tdo);
 	if(tdo)
 	    obyte |= 1 << j;
 	else
@@ -262,8 +250,7 @@
     byte = ibuf[nbits / 8];
     bit = byte & (1 << (nbits_1 % 8));
 
-    pin_hi(JTAG_PORT, JTAG_TMS);
-    SEND_GET_BIT(JTAG_TDI, bit, tdo);
+    SEND_GET_BIT(pv, bit, 1, tdo);
     if(tdo)
 	obuf[nbits / 8] |= 1 << j;
     else
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/bypasstest.py	Wed Feb 25 20:08:29 2009 +0800
@@ -0,0 +1,47 @@
+import sys
+import os
+import fcntl
+import jtagdev
+
+if len(sys.argv) != 2:
+    print >>sys.stderr, 'Usage: %s <UART Port>' % (sys.argv[0])
+    sys.exit(1)
+uart_fname = sys.argv[1]
+
+try:
+    uart_fo = file(uart_fname, 'r+b')
+except IOError, e:
+    print e
+    sys.exit(1)
+    pass
+
+flags = fcntl.fcntl(uart_fo, fcntl.F_GETFL)
+fcntl.fcntl(uart_fo, fcntl.F_SETFL, os.O_NONBLOCK | flags)
+
+jtagdev.debug_frame = 1
+
+dev = jtagdev.jtagdev(uart_fo)
+
+dev.reset()
+dev.wait_reply()
+
+dev.idle();
+dev.wait_reply()
+
+dev.go_shift_IR()
+dev.wait_reply()
+
+dev.shift_IR_n_out('\x0f', 4)
+dev.wait_reply()
+
+dev.go_shift_DR()
+dev.wait_reply()
+
+dev.shift_DR_n_out('\x01' * 128, 1024)
+dev.wait_reply()
+
+dev.go_shift_DR()
+dev.wait_reply()
+
+dev.shift_DR_n_out('\xff\xff', 16)
+dev.wait_reply()