X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/902cb3c00b49535f0de9a3b6d5ba0c54260ccac1..696e8630782aa1707c23c2ce4b0e8394689835ac:/armsrc/iso15693.c

diff --git a/armsrc/iso15693.c b/armsrc/iso15693.c
index ace6f87c..c092b383 100644
--- a/armsrc/iso15693.c
+++ b/armsrc/iso15693.c
@@ -81,7 +81,10 @@
 #define AddCrc(data,datalen)  Iso15693AddCrc(data,datalen)
 #define sprintUID(target,uid)	Iso15693sprintUID(target,uid)
 
-int DEBUG=0;
+// approximate amplitude=sqrt(ci^2+cq^2) 
+#define AMPLITUDE(ci, cq) (MAX(ABS(ci), ABS(cq)) + (MIN(ABS(ci), ABS(cq))>>1))
+
+static int DEBUG = 0;
 
 
 // ---------------------------
@@ -224,26 +227,14 @@ static void TransmitTo15693Tag(const uint8_t *cmd, int len, int *samples, int *w
 {
     int c;
 
-//    FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
+	FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX);
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX);
 	if(*wait < 10) { *wait = 10; }
 
-//    for(c = 0; c < *wait;) {
-//        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-//            AT91C_BASE_SSC->SSC_THR = 0x00;		// For exact timing!
-//            c++;
-//        }
-//        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
-//            volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
-//            (void)r;
-//        }
-//        WDT_HIT();
-//    }
-
     c = 0;
     for(;;) {
         if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-            AT91C_BASE_SSC->SSC_THR = cmd[c];
+            AT91C_BASE_SSC->SSC_THR = ~cmd[c];
             c++;
             if(c >= len) {
                 break;
@@ -263,13 +254,10 @@ static void TransmitTo15693Tag(const uint8_t *cmd, int len, int *samples, int *w
 //-----------------------------------------------------------------------------
 static void TransmitTo15693Reader(const uint8_t *cmd, int len, int *samples, int *wait)
 {
-    int c;
-
-//	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX);
-	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR);	// No requirement to energise my coils
+    int c = 0;
+	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR|FPGA_HF_SIMULATOR_MODULATE_424K);
 	if(*wait < 10) { *wait = 10; }
 
-    c = 0;
     for(;;) {
         if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
             AT91C_BASE_SSC->SSC_THR = cmd[c];
@@ -299,52 +287,26 @@ static void TransmitTo15693Reader(const uint8_t *cmd, int len, int *samples, int
 static int GetIso15693AnswerFromTag(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed)
 {
 	int c = 0;
-	uint8_t *dest = (uint8_t *)BigBuf;
-	int getNext = 0;
-
-	int8_t prev = 0;
+	uint8_t *dest = BigBuf_get_addr();
 
 // NOW READ RESPONSE
+	FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
-	//spindelay(60);	// greg - experiment to get rid of some of the 0 byte/failed reads
 	c = 0;
-	getNext = FALSE;
 	for(;;) {
-		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-			AT91C_BASE_SSC->SSC_THR = 0x43;
-		}
 		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
-			int8_t b;
-			b = (int8_t)AT91C_BASE_SSC->SSC_RHR;
-
+			uint16_t iq = AT91C_BASE_SSC->SSC_RHR;
 			// The samples are correlations against I and Q versions of the
-			// tone that the tag AM-modulates, so every other sample is I,
-			// every other is Q. We just want power, so abs(I) + abs(Q) is
-			// close to what we want.
-			if(getNext) {
-				int8_t r;
-
-				if(b < 0) {
-					r = -b;
-				} else {
-					r = b;
-				}
-				if(prev < 0) {
-					r -= prev;
-				} else {
-					r += prev;
-				}
-
-				dest[c++] = (uint8_t)r;
-
-				if(c >= 2000) {
-					break;
-				}
-			} else {
-				prev = b;
-			}
+			// tone that the tag AM-modulates. We just want power. 
+			int8_t i = iq >> 8;
+			int8_t q = iq;
+			uint8_t r = AMPLITUDE(i, q);
 
-			getNext = !getNext;
+			dest[c++] = r;
+			
+			if(c >= 4000) {
+				break;
+			}
 		}
 	}
 
@@ -355,12 +317,10 @@ static int GetIso15693AnswerFromTag(uint8_t *receivedResponse, int maxLen, int *
 	int i, j;
 	int max = 0, maxPos=0;
 
-	int skip = 4;
-
-	//	if(GraphTraceLen < 1000) return;	// THIS CHECKS FOR A BUFFER TO SMALL
+	int skip = 2;
 
 	// First, correlate for SOF
-	for(i = 0; i < 100; i++) {
+	for(i = 0; i < 200; i++) {  // usually, SOF is found around i = 60
 		int corr = 0;
 		for(j = 0; j < arraylen(FrameSOF); j += skip) {
 			corr += FrameSOF[j]*dest[i+(j/skip)];
@@ -370,7 +330,7 @@ static int GetIso15693AnswerFromTag(uint8_t *receivedResponse, int maxLen, int *
 			maxPos = i;
 		}
 	}
-	//	DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
+	if (DEBUG) Dbprintf("SOF at %d, correlation %d", maxPos, max/(arraylen(FrameSOF)/skip));
 
 	int k = 0; // this will be our return value
 
@@ -384,10 +344,15 @@ static int GetIso15693AnswerFromTag(uint8_t *receivedResponse, int maxLen, int *
 		memset(outBuf, 0, sizeof(outBuf));
 		uint8_t mask = 0x01;
 		for(;;) {
-			int corr0 = 0, corr1 = 0, corrEOF = 0;
+			int corr0 = 0, corr00 = 0, corr01 = 0, corr1 = 0, corrEOF = 0;
 			for(j = 0; j < arraylen(Logic0); j += skip) {
 				corr0 += Logic0[j]*dest[i+(j/skip)];
 			}
+			corr01 = corr00 = corr0;
+			for(j = 0; j < arraylen(Logic0); j += skip) {
+				corr00 += Logic0[j]*dest[i+arraylen(Logic0)/skip+(j/skip)];
+				corr01 += Logic1[j]*dest[i+arraylen(Logic0)/skip+(j/skip)];
+			}
 			for(j = 0; j < arraylen(Logic1); j += skip) {
 				corr1 += Logic1[j]*dest[i+(j/skip)];
 			}
@@ -395,11 +360,14 @@ static int GetIso15693AnswerFromTag(uint8_t *receivedResponse, int maxLen, int *
 				corrEOF += FrameEOF[j]*dest[i+(j/skip)];
 			}
 			// Even things out by the length of the target waveform.
+			corr00 *= 2;
+			corr01 *= 2;
 			corr0 *= 4;
 			corr1 *= 4;
 	
-			if(corrEOF > corr1 && corrEOF > corr0) {
-	//			DbpString("EOF at %d", i);
+			if(corrEOF > corr1 && corrEOF > corr00 && corrEOF > corr01) {
+				if (DEBUG) Dbprintf("EOF at %d, correlation %d (corr01: %d, corr00: %d, corr1: %d, corr0: %d)", 
+					i, corrEOF, corr01, corr00, corr1, corr0);
 				break;
 			} else if(corr1 > corr0) {
 				i += arraylen(Logic1)/skip;
@@ -412,7 +380,7 @@ static int GetIso15693AnswerFromTag(uint8_t *receivedResponse, int maxLen, int *
 				k++;
 				mask = 0x01;
 			}
-			if((i+(int)arraylen(FrameEOF)) >= 2000) {
+			if((i+(int)arraylen(FrameEOF)/skip) >= 4000) {
 				DbpString("ran off end!");
 				break;
 			}
@@ -449,52 +417,27 @@ static int GetIso15693AnswerFromTag(uint8_t *receivedResponse, int maxLen, int *
 static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed)
 {
 	int c = 0;
-	uint8_t *dest = (uint8_t *)BigBuf;
-	int getNext = 0;
-
-	int8_t prev = 0;
+	uint8_t *dest = BigBuf_get_addr();
 
 // NOW READ RESPONSE
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
 	//spindelay(60);	// greg - experiment to get rid of some of the 0 byte/failed reads
 	c = 0;
-	getNext = FALSE;
 	for(;;) {
-		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-			AT91C_BASE_SSC->SSC_THR = 0x43;
-		}
 		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
-			int8_t b;
-			b = (int8_t)AT91C_BASE_SSC->SSC_RHR;
-
+			uint16_t iq = AT91C_BASE_SSC->SSC_RHR;
 			// The samples are correlations against I and Q versions of the
-			// tone that the tag AM-modulates, so every other sample is I,
-			// every other is Q. We just want power, so abs(I) + abs(Q) is
-			// close to what we want.
-			if(getNext) {
-				int8_t r;
-
-				if(b < 0) {
-					r = -b;
-				} else {
-					r = b;
-				}
-				if(prev < 0) {
-					r -= prev;
-				} else {
-					r += prev;
-				}
-
-				dest[c++] = (uint8_t)r;
-
-				if(c >= 20000) {
-					break;
-				}
-			} else {
-				prev = b;
-			}
+			// tone that the tag AM-modulates. We just want power, 
+			// so abs(I) + abs(Q) is close to what we want.
+			int8_t i = iq >> 8;
+			int8_t q = iq;
+			uint8_t r = AMPLITUDE(i, q);
 
-			getNext = !getNext;
+			dest[c++] = r;
+
+			if(c >= BIGBUF_SIZE) {
+				break;
+			}
 		}
 	}
 
@@ -505,12 +448,10 @@ static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse, int maxLen, int
 	int i, j;
 	int max = 0, maxPos=0;
 
-	int skip = 4;
-
-//	if(GraphTraceLen < 1000) return;	// THIS CHECKS FOR A BUFFER TO SMALL
+	int skip = 2;
 
 	// First, correlate for SOF
-	for(i = 0; i < 19000; i++) {
+	for(i = 0; i < 38000; i++) {
 		int corr = 0;
 		for(j = 0; j < arraylen(FrameSOF); j += skip) {
 			corr += FrameSOF[j]*dest[i+(j/skip)];
@@ -520,7 +461,7 @@ static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse, int maxLen, int
 			maxPos = i;
 		}
 	}
-//	DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
+	if (DEBUG) Dbprintf("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip));
 
 	int k = 0; // this will be our return value
 
@@ -534,10 +475,15 @@ static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse, int maxLen, int
 		memset(outBuf, 0, sizeof(outBuf));
 		uint8_t mask = 0x01;
 		for(;;) {
-			int corr0 = 0, corr1 = 0, corrEOF = 0;
+			int corr0 = 0, corr00 = 0, corr01 = 0, corr1 = 0, corrEOF = 0;
 			for(j = 0; j < arraylen(Logic0); j += skip) {
 				corr0 += Logic0[j]*dest[i+(j/skip)];
 			}
+			corr01 = corr00 = corr0;
+			for(j = 0; j < arraylen(Logic0); j += skip) {
+				corr00 += Logic0[j]*dest[i+arraylen(Logic0)/skip+(j/skip)];
+				corr01 += Logic1[j]*dest[i+arraylen(Logic0)/skip+(j/skip)];
+			}
 			for(j = 0; j < arraylen(Logic1); j += skip) {
 				corr1 += Logic1[j]*dest[i+(j/skip)];
 			}
@@ -545,11 +491,14 @@ static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse, int maxLen, int
 				corrEOF += FrameEOF[j]*dest[i+(j/skip)];
 			}
 			// Even things out by the length of the target waveform.
+			corr00 *= 2;
+			corr01 *= 2;
 			corr0 *= 4;
 			corr1 *= 4;
 	
-			if(corrEOF > corr1 && corrEOF > corr0) {
-	//			DbpString("EOF at %d", i);
+			if(corrEOF > corr1 && corrEOF > corr00 && corrEOF > corr01) {
+				if (DEBUG) Dbprintf("EOF at %d, correlation %d (corr01: %d, corr00: %d, corr1: %d, corr0: %d)", 
+					i, corrEOF, corr01, corr00, corr1, corr0);
 				break;
 			} else if(corr1 > corr0) {
 				i += arraylen(Logic1)/skip;
@@ -562,7 +511,7 @@ static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse, int maxLen, int
 				k++;
 				mask = 0x01;
 			}
-			if((i+(int)arraylen(FrameEOF)) >= 2000) {
+			if((i+(int)arraylen(FrameEOF)/skip) >= BIGBUF_SIZE) {
 				DbpString("ran off end!");
 				break;
 			}
@@ -600,12 +549,11 @@ static void BuildIdentifyRequest(void);
 //-----------------------------------------------------------------------------
 void AcquireRawAdcSamplesIso15693(void)
 {
-	int c = 0;
-	uint8_t *dest = (uint8_t *)BigBuf;
-	int getNext = 0;
+	uint8_t *dest = BigBuf_get_addr();
 
-	int8_t prev = 0;
+	int c = 0;
 
+	FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
 	BuildIdentifyRequest();
 
 	SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
@@ -615,65 +563,40 @@ void AcquireRawAdcSamplesIso15693(void)
 	SpinDelay(100);
 
 	// Now send the command
-	FpgaSetupSsc();
+	FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX);
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX);
 
 	c = 0;
 	for(;;) {
 		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-			AT91C_BASE_SSC->SSC_THR = ToSend[c];
+			AT91C_BASE_SSC->SSC_THR = ~ToSend[c];
 			c++;
 			if(c == ToSendMax+3) {
 				break;
 			}
 		}
-		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
-			volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
-			(void)r;
-		}
 		WDT_HIT();
 	}
 
+	FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
 
 	c = 0;
-	getNext = FALSE;
 	for(;;) {
-		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-			AT91C_BASE_SSC->SSC_THR = 0x43;
-		}
 		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
-			int8_t b;
-			b = (int8_t)AT91C_BASE_SSC->SSC_RHR;
-
+			uint16_t iq = AT91C_BASE_SSC->SSC_RHR;
 			// The samples are correlations against I and Q versions of the
-			// tone that the tag AM-modulates, so every other sample is I,
-			// every other is Q. We just want power, so abs(I) + abs(Q) is
-			// close to what we want.
-			if(getNext) {
-				int8_t r;
-
-				if(b < 0) {
-					r = -b;
-				} else {
-					r = b;
-				}
-				if(prev < 0) {
-					r -= prev;
-				} else {
-					r += prev;
-				}
-
-				dest[c++] = (uint8_t)r;
-
-				if(c >= 2000) {
-					break;
-				}
-			} else {
-				prev = b;
-			}
+			// tone that the tag AM-modulates. We just want power, 
+			// so abs(I) + abs(Q) is close to what we want.
+			int8_t i = iq >> 8;
+			int8_t q = iq;
+			uint8_t r = AMPLITUDE(i, q);
 
-			getNext = !getNext;
+			dest[c++] = r;
+
+			if(c >= 4000) {
+				break;
+			}
 		}
 	}
 }
@@ -681,18 +604,17 @@ void AcquireRawAdcSamplesIso15693(void)
 
 void RecordRawAdcSamplesIso15693(void)
 {
-	int c = 0;
-	uint8_t *dest = (uint8_t *)BigBuf;
-	int getNext = 0;
+	uint8_t *dest = BigBuf_get_addr();
 
-	int8_t prev = 0;
+	int c = 0;
 
+	FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
 	// Setup SSC
-	FpgaSetupSsc();
+	FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
 
 	// Start from off (no field generated)
-    	FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
-    	SpinDelay(200);
+   	FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+   	SpinDelay(200);
 
 	SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
 
@@ -701,47 +623,24 @@ void RecordRawAdcSamplesIso15693(void)
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
 
 	c = 0;
-	getNext = FALSE;
 	for(;;) {
-		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-			AT91C_BASE_SSC->SSC_THR = 0x43;
-		}
 		if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
-			int8_t b;
-			b = (int8_t)AT91C_BASE_SSC->SSC_RHR;
-
+			uint16_t iq = AT91C_BASE_SSC->SSC_RHR;
 			// The samples are correlations against I and Q versions of the
-			// tone that the tag AM-modulates, so every other sample is I,
-			// every other is Q. We just want power, so abs(I) + abs(Q) is
-			// close to what we want.
-			if(getNext) {
-				int8_t r;
-
-				if(b < 0) {
-					r = -b;
-				} else {
-					r = b;
-				}
-				if(prev < 0) {
-					r -= prev;
-				} else {
-					r += prev;
-				}
-
-				dest[c++] = (uint8_t)r;
-
-				if(c >= 7000) {
-					break;
-				}
-			} else {
-				prev = b;
-			}
+			// tone that the tag AM-modulates. We just want power, 
+			// so abs(I) + abs(Q) is close to what we want.
+			int8_t i = iq >> 8;
+			int8_t q = iq;
+			uint8_t r = AMPLITUDE(i, q);
 
-			getNext = !getNext;
-			WDT_HIT();
+			dest[c++] = r;
+
+			if(c >= 14000) {
+				break;
+			}
 		}
 	}
-	Dbprintf("fin record");
+	Dbprintf("finished recording");
 }
 
 
@@ -753,6 +652,7 @@ void Iso15693InitReader() {
 	LED_C_OFF();
 	LED_D_OFF();
 	
+	FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
 	// Setup SSC
 	// FpgaSetupSsc();
 
@@ -761,7 +661,7 @@ void Iso15693InitReader() {
 	SpinDelay(10);
 
 	SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
-	FpgaSetupSsc();
+	FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
 
 	// Give the tags time to energize
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
@@ -833,24 +733,25 @@ static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber )
 }
 
 // Now the VICC>VCD responses when we are simulating a tag
- static void BuildInventoryResponse(void)
+ static void BuildInventoryResponse( uint8_t *uid)
 {
 	uint8_t cmd[12];
 
 	uint16_t crc;
 	// one sub-carrier, inventory, 1 slot, fast rate
 	// AFI is at bit 5 (1<<4) when doing an INVENTORY
-	cmd[0] = 0; //(1 << 2) | (1 << 5) | (1 << 1);
-	cmd[1] = 0;
+    //(1 << 2) | (1 << 5) | (1 << 1);
+	cmd[0] = 0; // 
+	cmd[1] = 0; // DSFID (data storage format identifier).  0x00 = not supported
 	// 64-bit UID
-	cmd[2] = 0x32;
-	cmd[3]= 0x4b;
-	cmd[4] = 0x03;
-	cmd[5] = 0x01;
-	cmd[6] = 0x00;
-	cmd[7] = 0x10;
-	cmd[8] = 0x05;
-	cmd[9]= 0xe0;
+	cmd[2] = uid[7]; //0x32;
+	cmd[3] = uid[6]; //0x4b;
+	cmd[4] = uid[5]; //0x03;
+	cmd[5] = uid[4]; //0x01;
+	cmd[6] = uid[3]; //0x00;
+	cmd[7] = uid[2]; //0x10;
+	cmd[8] = uid[1]; //0x05;
+	cmd[9] = uid[0]; //0xe0;
 	//Now the CRC
 	crc = Crc(cmd, 10);
 	cmd[10] = crc & 0xff;
@@ -877,12 +778,12 @@ int SendDataTag(uint8_t *send, int sendlen, int init, int speed, uint8_t **recv)
 	LED_C_OFF();
 	LED_D_OFF();
 	
+	if (init) Iso15693InitReader();
+
 	int answerLen=0;
-	uint8_t *answer = (((uint8_t *)BigBuf) + 3660);
-	if (recv!=NULL) memset(BigBuf + 3660, 0, 100);
+	uint8_t *answer = BigBuf_get_addr() + 4000;
+	if (recv != NULL) memset(answer, 0, 100);
 
-	if (init) Iso15693InitReader();
-	
 	if (!speed) {
 		// low speed (1 out of 256)
 		CodeIso15693AsReader256(send, sendlen);
@@ -999,31 +900,31 @@ void ReaderIso15693(uint32_t parameter)
 	LED_C_OFF();
 	LED_D_OFF();
 
-//DbpString(parameter);
-
-	//uint8_t *answer0 = (((uint8_t *)BigBuf) + 3560); // allow 100 bytes per reponse (way too much)
-	uint8_t *answer1 = (((uint8_t *)BigBuf) + 3660); //
-	uint8_t *answer2 = (((uint8_t *)BigBuf) + 3760);
-	uint8_t *answer3 = (((uint8_t *)BigBuf) + 3860);
-	//uint8_t *TagUID= (((uint8_t *)BigBuf) + 3960);		// where we hold the uid for hi15reader
-//	int answerLen0 = 0;
 	int answerLen1 = 0;
 	int answerLen2 = 0;
-	int answerLen3 = 0;
-	int i=0; // counter
+	// int answerLen3 = 0;
+	int i = 0;
+	int samples = 0;
+	int tsamples = 0;
+	int wait = 0;
+	int elapsed = 0;
+	uint8_t TagUID[8] = {0x00};
+
+	FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
 
+	uint8_t *answer1 = BigBuf_get_addr() + 4000;
+	uint8_t *answer2 = BigBuf_get_addr() + 4100;
+	// uint8_t *answer3 = BigBuf_get_addr() + 4200;
 	// Blank arrays
-	memset(BigBuf + 3660, 0, 300);
+	memset(answer1, 0x00, 200);
 
+	SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
 	// Setup SSC
-	FpgaSetupSsc();
+	FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
 
 	// Start from off (no field generated)
-    	FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
-    	SpinDelay(200);
-
-	SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
-	FpgaSetupSsc();
+   	FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+   	SpinDelay(200);
 
 	// Give the tags time to energize
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
@@ -1034,44 +935,19 @@ void ReaderIso15693(uint32_t parameter)
 	LED_C_OFF();
 	LED_D_OFF();
 
-	int samples = 0;
-	int tsamples = 0;
-	int wait = 0;
-	int elapsed = 0;
-
 	// FIRST WE RUN AN INVENTORY TO GET THE TAG UID
 	// THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
-	 uint8_t TagUID[8];		// where we hold the uid for hi15reader
-
-//	BuildIdentifyRequest();
-//	//TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
-//	TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);	// No longer ToSendMax+3
-//	// Now wait for a response
-//	responseLen0 = GetIso15693AnswerFromTag(receivedAnswer0, 100, &samples, &elapsed) ;
-//	if (responseLen0 >=12) // we should do a better check than this
-//	{
-//		// really we should check it is a valid mesg
-//		// but for now just grab what we think is the uid
-//		TagUID[0] = receivedAnswer0[2];
-//		TagUID[1] = receivedAnswer0[3];
-//		TagUID[2] = receivedAnswer0[4];
-//		TagUID[3] = receivedAnswer0[5];
-//		TagUID[4] = receivedAnswer0[6];
-//		TagUID[5] = receivedAnswer0[7];
-//		TagUID[6] = receivedAnswer0[8]; // IC Manufacturer code
-//	DbpIntegers(TagUID[6],TagUID[5],TagUID[4]);
-//}
 
 	// Now send the IDENTIFY command
 	BuildIdentifyRequest();
-	//TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
-	TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);	// No longer ToSendMax+3
+	
+	TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);
+	
 	// Now wait for a response
 	answerLen1 = GetIso15693AnswerFromTag(answer1, 100, &samples, &elapsed) ;
 
 	if (answerLen1 >=12) // we should do a better check than this
 	{
-
 		TagUID[0] = answer1[2];
 		TagUID[1] = answer1[3];
 		TagUID[2] = answer1[4];
@@ -1081,23 +957,6 @@ void ReaderIso15693(uint32_t parameter)
 		TagUID[6] = answer1[8]; // IC Manufacturer code
 		TagUID[7] = answer1[9]; // always E0
 
-		// Now send the SELECT command
-		// since the SELECT command is optional, we should not rely on it.
-////				BuildSelectRequest(TagUID);
-//		TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);	// No longer ToSendMax+3
-		// Now wait for a response
-///		answerLen2 = GetIso15693AnswerFromTag(answer2, 100, &samples, &elapsed);
-
-		// Now send the MULTI READ command
-//		BuildArbitraryRequest(*TagUID,parameter);
-///		BuildArbitraryCustomRequest(TagUID,parameter);
-//		BuildReadBlockRequest(*TagUID,parameter);
-//		BuildSysInfoRequest(*TagUID);
-		//TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait);
-///		TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);	// No longer ToSendMax+3
-		// Now wait for a response
-///		answerLen3 = GetIso15693AnswerFromTag(answer3, 100, &samples, &elapsed) ;
-
 	}
 
 	Dbprintf("%d octets read from IDENTIFY request:", answerLen1);
@@ -1106,27 +965,26 @@ void ReaderIso15693(uint32_t parameter)
 
 	// UID is reverse
 	if (answerLen1>=12) 
-		//Dbprintf("UID = %*D",8,TagUID," ");
-		Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",TagUID[7],TagUID[6],TagUID[5],
-			TagUID[4],TagUID[3],TagUID[2],TagUID[1],TagUID[0]);
+		Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",
+			TagUID[7],TagUID[6],TagUID[5],TagUID[4],
+			TagUID[3],TagUID[2],TagUID[1],TagUID[0]);
 
 
-	Dbprintf("%d octets read from SELECT request:", answerLen2);
-	DbdecodeIso15693Answer(answerLen2,answer2);
-	Dbhexdump(answerLen2,answer2,true);
+	// Dbprintf("%d octets read from SELECT request:", answerLen2);
+	// DbdecodeIso15693Answer(answerLen2,answer2);
+	// Dbhexdump(answerLen2,answer2,true);
 
-	Dbprintf("%d octets read from XXX request:", answerLen3);
-	DbdecodeIso15693Answer(answerLen3,answer3);
-	Dbhexdump(answerLen3,answer3,true);
+	// Dbprintf("%d octets read from XXX request:", answerLen3);
+	// DbdecodeIso15693Answer(answerLen3,answer3);
+	// Dbhexdump(answerLen3,answer3,true);
 
- 
 	// read all pages
 	if (answerLen1>=12 && DEBUG) {
 		i=0;			
 		while (i<32) {  // sanity check, assume max 32 pages
 			BuildReadBlockRequest(TagUID,i);
-	      TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);  
-         answerLen2 = GetIso15693AnswerFromTag(answer2, 100, &samples, &elapsed);
+			TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait);  
+			answerLen2 = GetIso15693AnswerFromTag(answer2, 100, &samples, &elapsed);
 			if (answerLen2>0) {
 				Dbprintf("READ SINGLE BLOCK %d returned %d octets:",i,answerLen2);
 				DbdecodeIso15693Answer(answerLen2,answer2);
@@ -1137,13 +995,6 @@ void ReaderIso15693(uint32_t parameter)
 		} 
 	}
 
-//	str2[0]=0;
-//	for(i = 0; i < responseLen3; i++) {
-//		itoa(str1,receivedAnswer3[i]);
-//		strncat(str2,str1,8);
-//	}
-//	DbpString(str2);
-
 	LED_A_OFF();
 	LED_B_OFF();
 	LED_C_OFF();
@@ -1152,31 +1003,29 @@ void ReaderIso15693(uint32_t parameter)
 
 // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands
 // all demodulation performed in arm rather than host. - greg
-void SimTagIso15693(uint32_t parameter)
+void SimTagIso15693(uint32_t parameter, uint8_t *uid)
 {
 	LED_A_ON();
 	LED_B_ON();
 	LED_C_OFF();
 	LED_D_OFF();
 
-	uint8_t *answer1 = (((uint8_t *)BigBuf) + 3660); //
 	int answerLen1 = 0;
+	int samples = 0;
+	int tsamples = 0;
+	int wait = 0;
+	int elapsed = 0;
 
-	// Blank arrays
-	memset(answer1, 0, 100);
-
-	// Setup SSC
-	FpgaSetupSsc();
-
-	// Start from off (no field generated)
-    	FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
-    	SpinDelay(200);
+	FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
 
+	uint8_t *buf = BigBuf_get_addr() + 4000;
+	memset(buf, 0x00, 100);
+	
 	SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
-	FpgaSetupSsc();
+	FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
 
-	// Give the tags time to energize
-//	FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);	// NO GOOD FOR SIM TAG!!!!
+	// Start from off (no field generated)
+   	FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
 	SpinDelay(200);
 
 	LED_A_OFF();
@@ -1184,24 +1033,26 @@ void SimTagIso15693(uint32_t parameter)
 	LED_C_ON();
 	LED_D_OFF();
 
-	int samples = 0;
-	int tsamples = 0;
-	int wait = 0;
-	int elapsed = 0;
-
-	answerLen1 = GetIso15693AnswerFromSniff(answer1, 100, &samples, &elapsed) ;
+	// Listen to reader
+	answerLen1 = GetIso15693AnswerFromSniff(buf, 100, &samples, &elapsed) ;
 
 	if (answerLen1 >=1) // we should do a better check than this
 	{
 		// Build a suitable reponse to the reader INVENTORY cocmmand
-		BuildInventoryResponse();
+		// not so obsvious, but in the call to BuildInventoryResponse,  the command is copied to the global ToSend buffer used below.
+		
+		BuildInventoryResponse(uid);
+	
 		TransmitTo15693Reader(ToSend,ToSendMax, &tsamples, &wait);
 	}
 
 	Dbprintf("%d octets read from reader command: %x %x %x %x %x %x %x %x %x", answerLen1,
-		answer1[0], answer1[1], answer1[2],
-		answer1[3], answer1[4], answer1[5],
-		answer1[6], answer1[7], answer1[8]);
+		buf[0], buf[1], buf[2],	buf[3],
+		buf[4], buf[5],	buf[6], buf[7], buf[8]);
+
+	Dbprintf("Simulationg uid: %x %x %x %x %x %x %x %x",
+		uid[0], uid[1], uid[2],	uid[3],
+		uid[4], uid[5],	uid[6], uid[7]);
 
 	LED_A_OFF();
 	LED_B_OFF();
@@ -1259,7 +1110,7 @@ void BruteforceIso15693Afi(uint32_t speed)
 void DirectTag15693Command(uint32_t datalen,uint32_t speed, uint32_t recv, uint8_t data[]) {
 
 	int recvlen=0;
-	uint8_t *recvbuf=(uint8_t *)BigBuf;
+	uint8_t *recvbuf = BigBuf_get_addr();
 //	UsbCommand n;
 	
 	if (DEBUG) {
@@ -1270,12 +1121,8 @@ void DirectTag15693Command(uint32_t datalen,uint32_t speed, uint32_t recv, uint8
 	recvlen=SendDataTag(data,datalen,1,speed,(recv?&recvbuf:NULL));
 
 	if (recv) { 
-//		n.cmd=/* CMD_ISO_15693_COMMAND_DONE */ CMD_ACK;
-//		n.arg[0]=recvlen>48?48:recvlen;
-//		memcpy(n.d.asBytes, recvbuf, 48);
 		LED_B_ON();
-    cmd_send(CMD_ACK,recvlen>48?48:recvlen,0,0,recvbuf,48);
-//		UsbSendPacket((uint8_t *)&n, sizeof(n));
+		cmd_send(CMD_ACK,recvlen>48?48:recvlen,0,0,recvbuf,48);
 		LED_B_OFF();	
 		
 		if (DEBUG) {