From d0724780ab600fd4c8e4b3a7dc43a9a01a3e8fb8 Mon Sep 17 00:00:00 2001
From: iceman1001 <iceman@iuse.se>
Date: Sat, 30 Jul 2016 19:30:53 +0200
Subject: [PATCH] FIX:   Found a minor bug in 'LF CMDREAD'  where it on device
 side didn't compare with the right char 'h' instead of 'H'.   Re-wrote that
 whole part anyway,  I changed periods to be max 0xFFFF in length,  doubtful
 that a zero or one delay will be bigger than 65535...

---
 armsrc/appmain.c |  2 +-
 armsrc/lfops.c   | 26 ++++++++---------
 client/cmdlf.c   | 72 ++++++++++++++++++++++++++----------------------
 3 files changed, 52 insertions(+), 48 deletions(-)

diff --git a/armsrc/appmain.c b/armsrc/appmain.c
index 5896eae7..b069fb4b 100644
--- a/armsrc/appmain.c
+++ b/armsrc/appmain.c
@@ -910,7 +910,7 @@ void UsbPacketReceived(uint8_t *packet, int len)
 			cmd_send(CMD_ACK, SampleLF(c->arg[0]),0,0,0,0);
 			break;
 		case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K:
-			ModThenAcquireRawAdcSamples125k(c->arg[0],c->arg[1],c->arg[2],c->d.asBytes);
+			ModThenAcquireRawAdcSamples125k(c->arg[0], c->arg[1], c->arg[2], c->d.asBytes);
 			break;
 		case CMD_LF_SNOOP_RAW_ADC_SAMPLES:
 			cmd_send(CMD_ACK,SnoopLF(),0,0,0,0);
diff --git a/armsrc/lfops.c b/armsrc/lfops.c
index b9ae4521..294bfb5e 100644
--- a/armsrc/lfops.c
+++ b/armsrc/lfops.c
@@ -26,29 +26,28 @@
  * @param period_1
  * @param command
  */
-void ModThenAcquireRawAdcSamples125k(uint32_t delay_off, uint32_t period_0, uint32_t period_1, uint8_t *command)
+void ModThenAcquireRawAdcSamples125k(uint32_t delay_off, uint32_t periods, uint32_t useHighFreq, uint8_t *command)
 {
+	/* Make sure the tag is reset */
+	FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
+	FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+	SpinDelay(200);
 
-	int divisor_used = 95; // 125 KHz
-	// see if 'h' was specified
-
-	if (command[strlen((char *) command) - 1] == 'h')
-		divisor_used = 88; // 134.8 KHz
-
+	uint16_t period_0 =  periods >> 16;
+	uint16_t period_1 =  periods & 0xFFFF;
+	
+	// 95 == 125 KHz  88 == 124.8 KHz
+	int divisor_used = (useHighFreq) ? 88 : 95 ; // 125 KHz
 	sample_config sc = { 0,0,1, divisor_used, 0};
 	setSamplingConfig(&sc);
+
 	//clear read buffer
 	BigBuf_Clear_keep_EM();
 
-	/* Make sure the tag is reset */
-	FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
-	FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
-	SpinDelay(2500);
-
 	LFSetupFPGAForADC(sc.divisor, 1);
 
 	// And a little more time for the tag to fully power up
-	SpinDelay(2000);
+	SpinDelay(50);
 
 	// now modulate the reader field
 	while(*command != '\0' && *command != ' ') {
@@ -68,7 +67,6 @@ void ModThenAcquireRawAdcSamples125k(uint32_t delay_off, uint32_t period_0, uint
 	LED_D_OFF();
 	SpinDelayUs(delay_off);
 	FpgaSendCommand(FPGA_CMD_SET_DIVISOR, sc.divisor);
-
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | FPGA_LF_ADC_READER_FIELD);
 
 	// now do the read
diff --git a/client/cmdlf.c b/client/cmdlf.c
index f48a4371..0453effe 100644
--- a/client/cmdlf.c
+++ b/client/cmdlf.c
@@ -12,14 +12,14 @@ static int CmdHelp(const char *Cmd);
 
 int usage_lf_cmdread(void) {
 	PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> [H]");
-	PrintAndLog("Options:        ");
+	PrintAndLog("Options:");
 	PrintAndLog("       h             This help");
 	PrintAndLog("       L             Low frequency (125 KHz)");
 	PrintAndLog("       H             High frequency (134 KHz)");
-	PrintAndLog("       d <delay>     delay OFF period, (dec)");
-	PrintAndLog("       z <zero>      time period ZERO, (dec)");
-	PrintAndLog("       o <one>       time period ONE, (dec)");
-	PrintAndLog("       c <cmd>       Command bytes");
+	PrintAndLog("       d <delay>     delay OFF period, (decimal)");
+	PrintAndLog("       z <zero>      time period ZERO, (decimal)");
+	PrintAndLog("       o <one>       time period ONE, (decimal)");
+	PrintAndLog("       c <cmd>       Command bytes  (in ones and zeros)");
 	PrintAndLog("       ************* All periods in microseconds (ms)");
 	PrintAndLog("Examples:");
 	PrintAndLog("      lf cmdread d 80 z 100 o 200 c 11000");
@@ -28,7 +28,7 @@ int usage_lf_cmdread(void) {
 }
 int usage_lf_read(void){
 	PrintAndLog("Usage: lf read [h] [s]");
-	PrintAndLog("Options:        ");
+	PrintAndLog("Options:");
 	PrintAndLog("       h            This help");
 	PrintAndLog("       s            silent run no printout");
 	PrintAndLog("This function takes no arguments. ");
@@ -37,15 +37,15 @@ int usage_lf_read(void){
 }
 int usage_lf_snoop(void) {
 	PrintAndLog("Usage: lf snoop");
-	PrintAndLog("Options:        ");
+	PrintAndLog("Options:");
 	PrintAndLog("       h            This help");
 	PrintAndLog("This function takes no arguments. ");
 	PrintAndLog("Use 'lf config' to set parameters.");
 	return 0;
 }
 int usage_lf_config(void) {
-	PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
-	PrintAndLog("Options:        ");
+	PrintAndLog("Usage: lf config [h] [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
+	PrintAndLog("Options:");
 	PrintAndLog("       h             This help");
 	PrintAndLog("       L             Low frequency (125 KHz)");
 	PrintAndLog("       H             High frequency (134 KHz)");
@@ -68,7 +68,7 @@ int usage_lf_config(void) {
 }
 int usage_lf_simfsk(void) {
 	PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
-	PrintAndLog("Options:        ");
+	PrintAndLog("Options:");
 	PrintAndLog("       h              This help");
 	PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer");
 	PrintAndLog("       i              invert data");
@@ -81,7 +81,7 @@ int usage_lf_simfsk(void) {
 }
 int usage_lf_simask(void) {
 	PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
-	PrintAndLog("Options:        ");
+	PrintAndLog("Options:");
 	PrintAndLog("       h              This help");
 	PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer");
 	PrintAndLog("       i              invert data");
@@ -94,7 +94,7 @@ int usage_lf_simask(void) {
 }
 int usage_lf_simpsk(void) {
 	PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
-	PrintAndLog("Options:        ");
+	PrintAndLog("Options:");
 	PrintAndLog("       h              This help");
 	PrintAndLog("       c <clock>      Manually set clock - can autodetect if using DemodBuffer");
 	PrintAndLog("       i              invert data");
@@ -106,35 +106,38 @@ int usage_lf_simpsk(void) {
 	return 0;
 }
 int usage_lf_find(void){
-    PrintAndLog("Usage:  lf search <0|1> [u]");
-    PrintAndLog("     <use data from Graphbuffer> , if not set, try reading data from tag.");
-    PrintAndLog("     [Search for Unknown tags] , if not set, reads only known tags.");
+    PrintAndLog("Usage:  lf search [h] <0|1> [u]");
     PrintAndLog("");
-    PrintAndLog("    sample: lf search     = try reading data from tag & search for known tags");
-    PrintAndLog("          : lf search 1   = use data from GraphBuffer & search for known tags");
-    PrintAndLog("          : lf search u   = try reading data from tag & search for known and unknown tags");
-    PrintAndLog("          : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
+	PrintAndLog("Options:");
+	PrintAndLog("       h             This help");
+	PrintAndLog("       <0|1>         Use data from Graphbuffer, if not set, try reading data from tag.");
+    PrintAndLog("       u             Search for Unknown tags, if not set, reads only known tags.");
+	PrintAndLog("Examples:");
+    PrintAndLog("      lf search     = try reading data from tag & search for known tags");
+    PrintAndLog("      lf search 1   = use data from GraphBuffer & search for known tags");
+    PrintAndLog("      lf search u   = try reading data from tag & search for known and unknown tags");
+    PrintAndLog("      lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
 	return 0;
 }
 
 
 /* send a LF command before reading */
-int CmdLFCommandRead(const char *Cmd)
-{
-	static char dummy[3] = {0x20,0x00,0x00};
-	UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
+int CmdLFCommandRead(const char *Cmd) {
+//	static char dummy[3] = {0x20,0x00,0x00};
 	bool errors = FALSE;
-
+	bool useHighFreq = FALSE;
+	uint16_t one = 0, zero = 0;
   	uint8_t cmdp = 0;
 	int strLength = 0;
-
+	UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K, {0,0,0}};
+	
 	while(param_getchar(Cmd, cmdp) != 0x00) {
-		switch(param_getchar(Cmd, cmdp))
-		{
+		switch(param_getchar(Cmd, cmdp)) {
 		case 'h':
 			return usage_lf_cmdread();
 		case 'H':
-			dummy[1]='h';
+			//dummy[1]='h';
+			useHighFreq = TRUE;
 			cmdp++;
 			break;
 		case 'L':
@@ -149,11 +152,11 @@ int CmdLFCommandRead(const char *Cmd)
 			cmdp+=2;
 			break;
 		case 'z':
-			c.arg[1] = param_get32ex(Cmd, cmdp+1, 0, 10);
+			zero = param_get32ex(Cmd, cmdp+1, 0, 10) & 0xFFFF;
 			cmdp+=2;
 			break;
 		case 'o':
-			c.arg[2] = param_get32ex(Cmd, cmdp+1, 0, 10);
+			one = param_get32ex(Cmd, cmdp+1, 0, 10) & 0xFFFF;
 			cmdp+=2;
 			break;
 		default:
@@ -164,13 +167,16 @@ int CmdLFCommandRead(const char *Cmd)
 		if(errors) break;
 	}
 	// No args
-	if (cmdp == 0) errors = 1;
+	if (cmdp == 0) errors = TRUE;
 
 	//Validations
 	if (errors) return usage_lf_cmdread();
 	
-	// in case they specified 'H'
-	strcpy((char *)&c.d.asBytes + strLength, dummy);
+	// zero and one lengths
+	c.arg[1] = zero << 16 | one;
+	
+	// add frequency 125 or 134
+	c.arg[2] = useHighFreq;
 
 	clearCommandBuffer();
 	SendCommand(&c);
-- 
2.39.5