+ if (strlen(Cmd)==0)\r
+ AquireData( CONFIGURATION_BLOCK );\r
+\r
+ if ( !tryDetectModulation() )\r
+ PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");\r
+\r
+ return 0;\r
+}\r
+\r
+// detect configuration?\r
+bool tryDetectModulation(){\r
+ char cmdStr[8] = {0};\r
+ uint8_t hits = 0;\r
+ t55xx_conf_block_t tests[15];\r
+ int bitRate=0;\r
+ uint8_t fc1 = 0, fc2 = 0, clk=0;\r
+ save_restoreGB(1);\r
+ if (GetFskClock("", FALSE, FALSE)){ \r
+ fskClocks(&fc1, &fc2, &clk, FALSE);\r
+ sprintf(cmdStr,"%d", clk/2);\r
+ CmdLtrim(cmdStr);\r
+ if ( FSKrawDemod("0 0", FALSE) && test(DEMOD_FSK, &tests[hits].offset, &bitRate)){\r
+ tests[hits].modulation = DEMOD_FSK;\r
+ if (fc1==8 && fc2 == 5)\r
+ tests[hits].modulation = DEMOD_FSK1a;\r
+ else if (fc1==10 && fc2 == 8)\r
+ tests[hits].modulation = DEMOD_FSK2;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( FSKrawDemod("0 1", FALSE) && test(DEMOD_FSK, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_FSK;\r
+ if (fc1 == 8 && fc2 == 5)\r
+ tests[hits].modulation = DEMOD_FSK1;\r
+ else if (fc1 == 10 && fc2 == 8)\r
+ tests[hits].modulation = DEMOD_FSK2a;\r
+\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ } else {\r
+ clk = GetAskClock("", FALSE, FALSE);\r
+ if (clk>0) {\r
+ sprintf(cmdStr,"%d", clk/2);\r
+ CmdLtrim(cmdStr);\r
+ if ( ASKDemod("0 0 0", FALSE, FALSE, 1) && test(DEMOD_ASK, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_ASK;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( ASKDemod("0 1 0", FALSE, FALSE, 1) && test(DEMOD_ASK, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_ASK;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( ASKbiphaseDemod("0 0 0 0", FALSE) && test(DEMOD_BI, &tests[hits].offset, &bitRate) ) {\r
+ tests[hits].modulation = DEMOD_BI;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( ASKbiphaseDemod("0 0 1 0", FALSE) && test(DEMOD_BIa, &tests[hits].offset, &bitRate) ) {\r
+ tests[hits].modulation = DEMOD_BIa;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ }\r
+ //undo trim from ask\r
+ save_restoreGB(0);\r
+ clk = GetNrzClock("", FALSE, FALSE);\r
+ if (clk>0) {\r
+ sprintf(cmdStr,"%d", clk/2);\r
+ CmdLtrim(cmdStr);\r
+ if ( NRZrawDemod("0 0 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_NRZ;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+\r
+ if ( NRZrawDemod("0 1 1", FALSE) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_NRZ;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ }\r
+ \r
+ //undo trim from nrz\r
+ save_restoreGB(0);\r
+ clk = GetPskClock("", FALSE, FALSE);\r
+ if (clk>0) {\r
+ PrintAndLog("clk %d",clk);\r
+ sprintf(cmdStr,"%d", clk/2);\r
+ CmdLtrim(cmdStr); \r
+ if ( PSKDemod("0 0 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_PSK1;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ if ( PSKDemod("0 1 1", FALSE) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate)) {\r
+ tests[hits].modulation = DEMOD_PSK1;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = TRUE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ // PSK2 - needs a call to psk1TOpsk2.\r
+ if ( PSKDemod("0 0 1", FALSE)) {\r
+ psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
+ if (test(DEMOD_PSK2, &tests[hits].offset, &bitRate)){\r
+ tests[hits].modulation = DEMOD_PSK2;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ } // inverse waves does not affect this demod\r
+ // PSK3 - needs a call to psk1TOpsk2.\r
+ if ( PSKDemod("0 0 1", FALSE)) {\r
+ psk1TOpsk2(DemodBuffer, DemodBufferLen);\r
+ if (test(DEMOD_PSK3, &tests[hits].offset, &bitRate)){\r
+ tests[hits].modulation = DEMOD_PSK3;\r
+ tests[hits].bitrate = bitRate;\r
+ tests[hits].inverted = FALSE;\r
+ tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);\r
+ ++hits;\r
+ }\r
+ } // inverse waves does not affect this demod\r
+ }\r
+ } \r
+ if ( hits == 1) {\r
+ config.modulation = tests[0].modulation;\r
+ config.bitrate = tests[0].bitrate;\r
+ config.inverted = tests[0].inverted;\r
+ config.offset = tests[0].offset;\r
+ config.block0 = tests[0].block0;\r
+ printConfiguration( config );\r
+ return TRUE;\r
+ }\r