From cdc3787008c3f95dd9563ae7100473c5ad07fa35 Mon Sep 17 00:00:00 2001
From: mwalker33 <mwal5099@bigpond.net.au>
Date: Wed, 24 Jul 2019 08:12:25 +1000
Subject: [PATCH 1/3] clean

---
 armsrc/lfops.c      | 2 ++
 client/cmdlft55xx.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/armsrc/lfops.c b/armsrc/lfops.c
index 17ac49dd2..75b5c934b 100644
--- a/armsrc/lfops.c
+++ b/armsrc/lfops.c
@@ -22,6 +22,8 @@
 #include "pmflash.h"
 #include "flashmem.h" // persistence on mem
 
+
+
 //#define START_GAP 31*8 // was 250 // SPEC:  1*8 to 50*8 - typ 15*8 (15fc)
 //#define WRITE_GAP 8*8 // 17*8 // was 160 // SPEC:  1*8 to 20*8 - typ 10*8 (10fc)
 //#define WRITE_0   15*8 // 18*8 // was 144 // SPEC: 16*8 to 32*8 - typ 24*8 (24fc)
diff --git a/client/cmdlft55xx.c b/client/cmdlft55xx.c
index bb8463692..ec98847db 100644
--- a/client/cmdlft55xx.c
+++ b/client/cmdlft55xx.c
@@ -2606,7 +2606,7 @@ static int CmdT55xxSetDeviceConfig(const char *Cmd) {
 
 //	printf ("DLmode %d\n",downlink_mode);
     t55xx_config conf = {0};
-	 printf ("Size conf %lld\n",sizeof(conf));
+//	 printf ("Size conf %lld\n",sizeof(conf));
  	 if (erase) {
 		memset (&conf,0xff, sizeof(conf));
 		printf ("Conf.m[0] %x\n",conf.m[0].start_gap);

From 77d96ff3b749cec6221ffa742c83894dd7b67a8b Mon Sep 17 00:00:00 2001
From: mwalker33 <mwal5099@bigpond.net.au>
Date: Thu, 25 Jul 2019 20:01:03 +1000
Subject: [PATCH 2/3] Bug Fix Timing order

Timing for backward compatibility
---
 CHANGELOG.md                                  |  1 +
 armsrc/lfops.c                                | 23 ++++++++-----------
 client/cmdlft55xx.c                           | 16 ++++++-------
 .../2_Configuration-and-Verification.md       |  3 +++
 include/pm3_cmd.h                             |  3 ++-
 5 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 76b3db976..b283ba462 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
 This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...
 
 ## [unreleased][unreleased]
+ - Fix T55x7 Downlink timings backward compatible (@mwalker33)
  - Add T55x7 Downlink mode support (@mwalker33)
  - Add SPIFFS Flash filesystem support (@cjbrigato)
  - Fix support for flashing 512K units with old bootrom (@slurdge/@doegox)
diff --git a/armsrc/lfops.c b/armsrc/lfops.c
index 98896184e..f238e745c 100644
--- a/armsrc/lfops.c
+++ b/armsrc/lfops.c
@@ -123,12 +123,12 @@ t55xx_config T55xx_Timing  = {{
     }
 };
 */
-//                              StartGap WriteGap Bit 0/00 Bit 1/01  Bit 10  Bit 11  ReadGap
+//      StartGap WriteGap Bit 0/00 Bit 1/01  ReadGap Bit 10  Bit 11  
 t55xx_config T55xx_Timing  = {{
-        { 29 * 8, 17 * 8, 15 * 8, 50 * 8, 0, 0, 15 * 8 },           // Default Fixed
-        { 31 * 8, 20 * 8, 18 * 8, 50 * 8, 0, 0, 15 * 8 },           // Long Leading Ref.
-        { 31 * 8, 20 * 8, 18 * 8, 40 * 8, 0, 0, 15 * 8 },           // Leading 0
-        { 29 * 8, 17 * 8, 15 * 8, 31 * 8, 47 * 8, 63 * 8, 15 * 8 }  // 1 of 4
+        { 29 * 8, 17 * 8, 15 * 8, 50 * 8, 15 * 8, 0, 0 },           // Default Fixed
+        { 31 * 8, 20 * 8, 18 * 8, 50 * 8, 15 * 8, 0, 0 },           // Long Leading Ref.
+        { 31 * 8, 20 * 8, 18 * 8, 40 * 8, 15 * 8, 0, 0 },           // Leading 0
+        { 29 * 8, 17 * 8, 15 * 8, 31 * 8, 15 * 8, 47 * 8, 63 * 8 }  // 1 of 4
     }
 };
 
@@ -163,11 +163,11 @@ void printT55xxConfig(void) {
         Dbprintf("  [b] writegap............%d*8 (%d)", T55xx_Timing.m[DLMode].write_gap / 8, T55xx_Timing.m[DLMode].write_gap);
         Dbprintf("  [c] write_0.............%d*8 (%d)", T55xx_Timing.m[DLMode].write_0   / 8, T55xx_Timing.m[DLMode].write_0);
         Dbprintf("  [d] write_1.............%d*8 (%d)", T55xx_Timing.m[DLMode].write_1   / 8, T55xx_Timing.m[DLMode].write_1);
+        Dbprintf("  [e] readgap.............%d*8 (%d)", T55xx_Timing.m[DLMode].read_gap / 8, T55xx_Timing.m[DLMode].read_gap);
         if (DLMode ==  T55xx_DLMode_1of4) {
-            Dbprintf("  [e] write_2.............%d*8 (%d)", T55xx_Timing.m[DLMode].write_2 / 8, T55xx_Timing.m[DLMode].write_2);
-            Dbprintf("  [f] write_3.............%d*8 (%d)", T55xx_Timing.m[DLMode].write_3 / 8, T55xx_Timing.m[DLMode].write_3);
+            Dbprintf("  [f] write_2.............%d*8 (%d)", T55xx_Timing.m[DLMode].write_2 / 8, T55xx_Timing.m[DLMode].write_2);
+            Dbprintf("  [g] write_3.............%d*8 (%d)", T55xx_Timing.m[DLMode].write_3 / 8, T55xx_Timing.m[DLMode].write_3);
         }
-        Dbprintf("  [g] readgap.............%d*8 (%d)", T55xx_Timing.m[DLMode].read_gap / 8, T55xx_Timing.m[DLMode].read_gap);
     }
 }
 
@@ -213,21 +213,17 @@ void setT55xxConfig(uint8_t arg0, t55xx_config *c) {
         return;
     }
 
-    // if ( ClearT55Settings) // dont copy over new timings
     memcpy(buf, &T55xx_Timing, T55XX_CONFIG_LEN);
 
     Flash_CheckBusy(BUSY_TIMEOUT);
     Flash_WriteEnable();
     Flash_Erase4k(3, 0xD);
 
-    // if not a settings erase, write data
-    // if ( ClearT55Settings) {
     res = Flash_Write(T55XX_CONFIG_OFFSET, buf, T55XX_CONFIG_LEN);
 
     if (res == T55XX_CONFIG_LEN && DBGLEVEL > 1) {
         DbpString("T55XX Config save success");
     }
-    // }
 
     BigBuf_free();
 #endif
@@ -1484,6 +1480,7 @@ void TurnReadLF_off(uint32_t delay) {
 // Macro for code readability
 #define BitStream_Byte(X) ((X) >> 3)
 #define BitStream_Bit(X)  ((X) &  7)
+#define t55_llr_ref  			(136 * 8)
 #define t55_send_PwdMode      (arg & 0x01)
 #define t55_send_Page        ((arg & 0x02) >> 1)
 #define t55_send_TestMode    ((arg & 0x04) >> 2)
@@ -1510,7 +1507,7 @@ void T55xxWriteBit(uint8_t bit, uint8_t downlink_idx) {
             TurnReadLFOn(T55xx_Timing.m[downlink_idx].write_3);
             break; // Send bits   11 (1 of 4)
         case 4 :
-            TurnReadLFOn(T55xx_Timing.m[downlink_idx].write_0 + (136 * 8));
+            TurnReadLFOn(T55xx_Timing.m[downlink_idx].write_0 + t55_llr_ref);
             break; // Send Long Leading Reference
     }
 
diff --git a/client/cmdlft55xx.c b/client/cmdlft55xx.c
index 61383066c..cee13f5e1 100644
--- a/client/cmdlft55xx.c
+++ b/client/cmdlft55xx.c
@@ -254,9 +254,9 @@ static int usage_lf_deviceconfig() {
     PrintAndLogEx(NORMAL, "     b <8..255>   - Set write gap");
     PrintAndLogEx(NORMAL, "     c <8..255>   - Set write ZERO gap");
     PrintAndLogEx(NORMAL, "     d <8..255>   - Set write ONE gap");
-    PrintAndLogEx(NORMAL, "     e <8..255>   - Set write TWO gap (1 of 4 only)");
-    PrintAndLogEx(NORMAL, "     f <8..255>   - Set write THREE gap (1 of 4 only)");
-    PrintAndLogEx(NORMAL, "     g <8..255>   - Set read gap");
+    PrintAndLogEx(NORMAL, "     e <8..255>   - Set read gap");
+    PrintAndLogEx(NORMAL, "     f <8..255>   - Set write TWO gap (1 of 4 only)");
+    PrintAndLogEx(NORMAL, "     g <8..255>   - Set write THREE gap (1 of 4 only)");
     PrintAndLogEx(NORMAL, "     p            - persist to flashmemory");
     PrintAndLogEx(NORMAL, "     r <mode>     - downlink encoding '0' fixed bit length (default), '1' long leading ref.");
     PrintAndLogEx(NORMAL, "                                      '2' leading zero,               '3' 1 of 4 coding ref.");
@@ -2572,15 +2572,15 @@ static int CmdT55xxSetDeviceConfig(const char *Cmd) {
                 cmdp += 2;
                 break;
             case 'e':
-                errors |= param_getdec(Cmd, cmdp + 1, &write2);
+                errors |= param_getdec(Cmd, cmdp + 1, &readgap);
                 cmdp += 2;
                 break;
             case 'f':
-                errors |= param_getdec(Cmd, cmdp + 1, &write3);
+                errors |= param_getdec(Cmd, cmdp + 1, &write2);
                 cmdp += 2;
                 break;
             case 'g':
-                errors |= param_getdec(Cmd, cmdp + 1, &readgap);
+                errors |= param_getdec(Cmd, cmdp + 1, &write3);
                 cmdp += 2;
                 break;
             case 'r':
@@ -2606,9 +2606,7 @@ static int CmdT55xxSetDeviceConfig(const char *Cmd) {
     //Validations
     if (errors || cmdp == 0) return usage_lf_deviceconfig();
 
-//    printf ("DLmode %d\n",downlink_mode);
     t55xx_config conf = {0};
-//	 printf ("Size conf %lld\n",sizeof(conf));
  	 if (erase) {
 		memset (&conf,0xff, sizeof(conf));
 		printf ("Conf.m[0] %x\n",conf.m[0].start_gap);
@@ -2619,9 +2617,9 @@ static int CmdT55xxSetDeviceConfig(const char *Cmd) {
 		conf.m[downlink_mode].write_gap  = writegap * 8;
 		conf.m[downlink_mode].write_0    = write0   * 8;
 		conf.m[downlink_mode].write_1    = write1   * 8;
+		conf.m[downlink_mode].read_gap   = readgap  * 8;
 		conf.m[downlink_mode].write_2    = write2   * 8;
 		conf.m[downlink_mode].write_3    = write3   * 8;
-		conf.m[downlink_mode].read_gap   = readgap  * 8;
 	 }
     clearCommandBuffer();
     SendCommandOLD(CMD_SET_LF_T55XX_CONFIG, shall_persist, 0, 0, &conf, sizeof(t55xx_config));
diff --git a/doc/md/Use_of_Proxmark/2_Configuration-and-Verification.md b/doc/md/Use_of_Proxmark/2_Configuration-and-Verification.md
index 736ef70a5..5c8420d9d 100644
--- a/doc/md/Use_of_Proxmark/2_Configuration-and-Verification.md
+++ b/doc/md/Use_of_Proxmark/2_Configuration-and-Verification.md
@@ -5,6 +5,9 @@ pm3 --> mem load f default_keys m
 pm3 --> mem load f default_pwd t
 pm3 --> mem load f default_iclass_keys i
 pm3 --> lf t55xx deviceconfig a 29 b 17 c 15 d 47 e 15 p
+pm3 --> lf t55xx deviceconfig r 1 a 31 b 20 c 18 d 50 e 15 p
+pm3 --> lf t55xx deviceconfig r 2 a 31 b 20 c 18 d 40 e 15 p
+pm3 --> lf t55xx deviceconfig r 3 a 29 b 17 c 15 d 31 e 15 f 47 g 63 p
 ```
 
 ### Verify sim module firmware version
diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h
index 63054d3a1..c4cc05cee 100644
--- a/include/pm3_cmd.h
+++ b/include/pm3_cmd.h
@@ -135,9 +135,10 @@ typedef struct  {
     uint16_t write_gap ;
     uint16_t write_0   ;
     uint16_t write_1   ;
+    uint16_t read_gap  ;
     uint16_t write_2   ;
     uint16_t write_3   ;
-    uint16_t read_gap  ;
+
 } t55xx_config_t;
 // This setup will allow for the 4 downlink modes "m" as well as other items if needed.
 // Given the one struct we can then read/write to flash/client in one go.

From 3211f35062bf249c5c0869d99bf7d413cf232183 Mon Sep 17 00:00:00 2001
From: mwalker33 <mwal5099@bigpond.net.au>
Date: Thu, 25 Jul 2019 21:00:30 +1000
Subject: [PATCH 3/3] Added set default timings

---
 armsrc/lfops.c                                |  4 +-
 client/cmdlft55xx.c                           | 55 +++++++++++++++++--
 .../2_Configuration-and-Verification.md       |  3 +
 3 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/armsrc/lfops.c b/armsrc/lfops.c
index f238e745c..af80e2f6c 100644
--- a/armsrc/lfops.c
+++ b/armsrc/lfops.c
@@ -116,7 +116,7 @@
 // Note: Moved * 8 to apply when used. Saving 28 bytes here (- the *8) and 28 bytes flash.
 //                              StartGap WriteGap Bit 0/00 Bit 1/01  Bit 10  Bit 11  ReadGap
 t55xx_config T55xx_Timing  = {{
-        { 29    , 17    , 15    , 50    , 0     , 0     , 15     }, // Default Fixed
+        { 29    , 17    , 15    , 47    , 0     , 0     , 15     }, // Default Fixed
         { 31    , 20    , 18    , 50    , 0     , 0     , 15     }, // Long Leading Ref.
         { 31    , 20    , 18    , 40    , 0     , 0     , 15     }, // Leading 0
         { 29    , 17    , 15    , 31    , 47    , 63    , 15     }  // 1 of 4
@@ -125,7 +125,7 @@ t55xx_config T55xx_Timing  = {{
 */
 //      StartGap WriteGap Bit 0/00 Bit 1/01  ReadGap Bit 10  Bit 11  
 t55xx_config T55xx_Timing  = {{
-        { 29 * 8, 17 * 8, 15 * 8, 50 * 8, 15 * 8, 0, 0 },           // Default Fixed
+        { 29 * 8, 17 * 8, 15 * 8, 47 * 8, 15 * 8, 0, 0 },           // Default Fixed
         { 31 * 8, 20 * 8, 18 * 8, 50 * 8, 15 * 8, 0, 0 },           // Long Leading Ref.
         { 31 * 8, 20 * 8, 18 * 8, 40 * 8, 15 * 8, 0, 0 },           // Leading 0
         { 29 * 8, 17 * 8, 15 * 8, 31 * 8, 15 * 8, 47 * 8, 63 * 8 }  // 1 of 4
diff --git a/client/cmdlft55xx.c b/client/cmdlft55xx.c
index cee13f5e1..3d0871893 100644
--- a/client/cmdlft55xx.c
+++ b/client/cmdlft55xx.c
@@ -14,6 +14,13 @@
 
 #include "cmdlft55xx.h"
 
+// Some defines for readability
+#define T55xx_DLMode_Fixed         0 // Default Mode
+#define T55xx_DLMode_LLR           1 // Long Leading Reference
+#define T55xx_DLMode_Leading0      2 // Leading Zero
+#define T55xx_DLMode_1of4          3 // 1 of 4
+#define T55xx_LongLeadingReference 4 // Value to tell Write Bit to send long reference
+
 // Default configuration
 t55xx_conf_block_t config = { .modulation = DEMOD_ASK, .inverted = false, .offset = 0x00, .block0 = 0x00, .Q5 = false };
 
@@ -260,7 +267,7 @@ static int usage_lf_deviceconfig() {
     PrintAndLogEx(NORMAL, "     p            - persist to flashmemory");
     PrintAndLogEx(NORMAL, "     r <mode>     - downlink encoding '0' fixed bit length (default), '1' long leading ref.");
     PrintAndLogEx(NORMAL, "                                      '2' leading zero,               '3' 1 of 4 coding ref.");
-    PrintAndLogEx(NORMAL, "     z            - erase t55x7 timings (needs p and reboot to load defaults)");
+    PrintAndLogEx(NORMAL, "     z            - Set default t55x7 timings (use p to save if required)");
     PrintAndLogEx(NORMAL, "");
     PrintAndLogEx(NORMAL, "Examples:");
     PrintAndLogEx(NORMAL, "      lf t55xx deviceconfig a 29 b 17 c 15 d 47 e 15   - default T55XX");
@@ -2549,7 +2556,7 @@ static int CmdT55xxSetDeviceConfig(const char *Cmd) {
     bool    errors        = false, shall_persist = false;
     uint8_t cmdp          = 0;
     uint8_t downlink_mode = 0;
-    bool    erase         = false;
+    bool    set_defaults  = false;
 
     while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
         switch (tolower(param_getchar(Cmd, cmdp))) {
@@ -2593,7 +2600,7 @@ static int CmdT55xxSetDeviceConfig(const char *Cmd) {
                 cmdp++;
                 break;
             case 'z':
-                erase = true;
+                set_defaults = true;
                 cmdp++;
                 break;
             default:
@@ -2607,9 +2614,48 @@ static int CmdT55xxSetDeviceConfig(const char *Cmd) {
     if (errors || cmdp == 0) return usage_lf_deviceconfig();
 
     t55xx_config conf = {0};
- 	 if (erase) {
+/* 	 if (erase) {
 		memset (&conf,0xff, sizeof(conf));
 		printf ("Conf.m[0] %x\n",conf.m[0].start_gap);
+		*/
+		//
+    if (set_defaults){
+		// fixed bit length
+		conf.m[T55xx_DLMode_Fixed].start_gap  = 29 * 8;
+		conf.m[T55xx_DLMode_Fixed].write_gap  = 17 * 8;
+		conf.m[T55xx_DLMode_Fixed].write_0    = 15 * 8;
+		conf.m[T55xx_DLMode_Fixed].write_1    = 47 * 8;
+		conf.m[T55xx_DLMode_Fixed].read_gap   = 15 * 8;
+		conf.m[T55xx_DLMode_Fixed].write_2    = 0;
+		conf.m[T55xx_DLMode_Fixed].write_3    = 0;
+	
+	   // long leading reference
+		conf.m[T55xx_DLMode_LLR].start_gap  = 31 * 8;
+		conf.m[T55xx_DLMode_LLR].write_gap  = 20 * 8;
+		conf.m[T55xx_DLMode_LLR].write_0    = 18 * 8;
+		conf.m[T55xx_DLMode_LLR].write_1    = 50 * 8;
+		conf.m[T55xx_DLMode_LLR].read_gap   = 15 * 8;
+		conf.m[T55xx_DLMode_LLR].write_2    = 0;
+		conf.m[T55xx_DLMode_LLR].write_3    = 0;
+   
+	   // leading zero
+		conf.m[T55xx_DLMode_Leading0].start_gap  = 31 * 8;
+		conf.m[T55xx_DLMode_Leading0].write_gap  = 20 * 8;
+		conf.m[T55xx_DLMode_Leading0].write_0    = 18 * 8;
+		conf.m[T55xx_DLMode_Leading0].write_1    = 40 * 8;
+		conf.m[T55xx_DLMode_Leading0].read_gap   = 15 * 8;
+		conf.m[T55xx_DLMode_Leading0].write_2    = 0;
+		conf.m[T55xx_DLMode_Leading0].write_3    = 0;
+
+		// 1 of 4 coding reference
+		conf.m[T55xx_DLMode_1of4].start_gap  = 29 * 8;
+		conf.m[T55xx_DLMode_1of4].write_gap  = 17 * 8;
+		conf.m[T55xx_DLMode_1of4].write_0    = 15 * 8;
+		conf.m[T55xx_DLMode_1of4].write_1    = 31 * 8;
+		conf.m[T55xx_DLMode_1of4].read_gap   = 15 * 8;
+		conf.m[T55xx_DLMode_1of4].write_2    = 47 * 8;
+		conf.m[T55xx_DLMode_1of4].write_3    = 63 * 8;
+
 	 }
 	 else {
 	 
@@ -2621,6 +2667,7 @@ static int CmdT55xxSetDeviceConfig(const char *Cmd) {
 		conf.m[downlink_mode].write_2    = write2   * 8;
 		conf.m[downlink_mode].write_3    = write3   * 8;
 	 }
+	 
     clearCommandBuffer();
     SendCommandOLD(CMD_SET_LF_T55XX_CONFIG, shall_persist, 0, 0, &conf, sizeof(t55xx_config));
     return PM3_SUCCESS;
diff --git a/doc/md/Use_of_Proxmark/2_Configuration-and-Verification.md b/doc/md/Use_of_Proxmark/2_Configuration-and-Verification.md
index 5c8420d9d..0d3665f37 100644
--- a/doc/md/Use_of_Proxmark/2_Configuration-and-Verification.md
+++ b/doc/md/Use_of_Proxmark/2_Configuration-and-Verification.md
@@ -8,6 +8,9 @@ pm3 --> lf t55xx deviceconfig a 29 b 17 c 15 d 47 e 15 p
 pm3 --> lf t55xx deviceconfig r 1 a 31 b 20 c 18 d 50 e 15 p
 pm3 --> lf t55xx deviceconfig r 2 a 31 b 20 c 18 d 40 e 15 p
 pm3 --> lf t55xx deviceconfig r 3 a 29 b 17 c 15 d 31 e 15 f 47 g 63 p
+
+Set all t55xx settings to defaults (will set all 4 at once)
+pm3 --> lf t55xx deviceconfig z p
 ```
 
 ### Verify sim module firmware version