diff src/jtag.c @ 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 cc106f278d7d
children
line wrap: on
line diff
--- 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