From 07f700caf7db1d12cf0dde7627ced350d162d107 Mon Sep 17 00:00:00 2001
From: hackbard <hackbard>
Date: Mon, 9 Apr 2007 14:42:05 +0000
Subject: [PATCH] jtag via bitbanging of port d

---
 fx2/fx2.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)

diff --git a/fx2/fx2.c b/fx2/fx2.c
index 90e40e9..3a57b12 100644
--- a/fx2/fx2.c
+++ b/fx2/fx2.c
@@ -32,6 +32,8 @@ xdata at 0xe601 volatile u8 IFCONFIG;
 /* endpoint configuration */
 xdata at 0xe604 volatile u8 FIFORESET;
 xdata at 0xe60b volatile u8 REVCTL;
+xdata at 0xe610 volatile u8 EP1OUTCFG;
+xdata at 0xe611 volatile u8 EP1INCFG;
 xdata at 0xe612 volatile u8 EP2CFG;
 xdata at 0xe613 volatile u8 EP4CFG;
 xdata at 0xe614 volatile u8 EP6CFG;
@@ -45,6 +47,19 @@ xdata at 0xe621 volatile u8 EP2AUTOINLENL;
 xdata at 0xe624 volatile u8 EP6AUTOINLENH;
 xdata at 0xe625 volatile u8 EP6AUTOINLENL;
 
+/* endpoint control/status */
+xdata at 0xe6a1 volatile u8 EP1OUTCS;
+xdata at 0xe6a2 volatile u8 EP1INCS;
+xdata at 0xe68d volatile u16 EP1OUTBC;
+xdata at 0xe68f volatile u16 EP1INBC;
+
+#define STALL	0x01
+#define BUSY	0x02
+
+/* access to endpoint buffers */
+xdata at 0xe780 volatile u8 EP1OUTBUF[64];
+xdata at 0xe7c0 volatile u8 EP1INBUF[64];
+
 /* special funtion registers */
 sfr at 0xb5 OED;
 sfr at 0xb0 IOD;
@@ -83,6 +98,23 @@ void toggle_power() {
 
 }
 
+void jtag_init() {
+
+	/* pin 5 of port d disables tdi -> tdo forward */
+	OED|=(1<<5);
+	IOD|=(1<<5);
+
+	/* jtag pins:
+	 * tdi - pin 0 (input)
+	 * tdo - pin 2 (output)
+	 * tms - pin 3 (output)
+	 * tck - pin 4 (output)
+	 */
+	OED|=((1<<2)|(1<<3)|(1<<4));
+	OED&=~(1<<0);
+
+}
+
 void cpu_init() {
 
 	/* cpu initialization: (0x10)
@@ -186,6 +218,11 @@ void ep1_init() {
 	 * default (valid, bulk) fits!
 	 */
 
+	/* arm ep1out, clear ep1out and ep1in stall bit */
+	EP1OUTBC=1;
+	EP1OUTCS&=~STALL;
+	EP1INCS&=~STALL;
+
 }
 
 void fx2_init() {
@@ -206,12 +243,24 @@ void fx2_init() {
 
 void main() {
 
+	u8 buf;
+
 	/* initialize the fx2 */
 	fx2_init();
 
-	/* do the job ... */
+	/* jtag by polling ep1 */
 	while(1) {
-	
+		if(!(EP1OUTCS&BUSY)) {
+			buf=EP1OUTBUF[0];
+			buf&=0x1c;
+			IOD|=buf;
+			EP1OUTBC=1;
+		}
+		if(!(EP1INCS&BUSY)) {
+			buf=IOD&0x01?1:0;
+			EP1INBUF[0]=buf;
+			EP1INBC=1;
+		}
 	}
 
 }
-- 
2.39.5