From df08e7970c3d8959cd3692d20f088a9fafcc72cb Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 3 Oct 2019 21:18:37 +0200 Subject: [PATCH] fix divisor<>freq computations, add q to lf tune --- armsrc/appmain.c | 4 +-- armsrc/lfsampling.c | 2 +- client/cmddata.c | 10 ++++---- client/cmdlf.c | 60 +++++++++++++++++++++++++++++++++------------ include/pm3_cmd.h | 7 +++--- 5 files changed, 55 insertions(+), 28 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index af90060d5..eed1d97ed 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1473,7 +1473,7 @@ static void PacketReceived(PacketCommandNG *packet) { break; } case CMD_MEASURE_ANTENNA_TUNING_LF: { - if (packet->length != 1) + if (packet->length != 2) reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF, PM3_EINVARG, NULL, 0); switch (packet->data.asBytes[0]) { @@ -1481,7 +1481,7 @@ static void PacketReceived(PacketCommandNG *packet) { // Let the FPGA drive the low-frequency antenna around 125kHz FpgaDownloadAndGo(FPGA_BITSTREAM_LF); FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | FPGA_LF_ADC_READER_FIELD); - FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); + FpgaSendCommand(FPGA_CMD_SET_DIVISOR, packet->data.asBytes[1]); reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF, PM3_SUCCESS, NULL, 0); break; case 2: diff --git a/armsrc/lfsampling.c b/armsrc/lfsampling.c index 993418360..175e0a2ff 100644 --- a/armsrc/lfsampling.c +++ b/armsrc/lfsampling.c @@ -29,7 +29,7 @@ sample_config config = { 1, 8, 1, LF_DIVISOR_125, 0, 0 } ; void printConfig() { uint32_t d = config.divisor; DbpString(_BLUE_("LF Sampling config")); - Dbprintf(" [q] divisor.............%d ( "_GREEN_("%d.%02d kHz")")", d, 12000 / d, ((1200000 + d/2) / d) - ((12000 / d) * 100)); + Dbprintf(" [q] divisor.............%d ( "_GREEN_("%d.%02d kHz")")", d, 12000 / (d+1), ((1200000 + (d+1)/2) / (d+1)) - ((12000 / (d+1)) * 100)); Dbprintf(" [b] bps.................%d", config.bits_per_sample); Dbprintf(" [d] decimation..........%d", config.decimation); Dbprintf(" [a] averaging...........%s", (config.averaging) ? "Yes" : "No"); diff --git a/client/cmddata.c b/client/cmddata.c index 1e9d7855e..e025ded3f 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -1676,16 +1676,16 @@ int CmdTuneSamples(const char *Cmd) { struct p* package = (struct p*)resp.data.asBytes; if (package->v_lf125 > NON_VOLTAGE) - PrintAndLogEx(SUCCESS, "LF antenna: %5.2f V - %.2f kHz", (package->v_lf125 * ANTENNA_ERROR) / 1000.0, 12000.0 / LF_DIVISOR_125); + PrintAndLogEx(SUCCESS, "LF antenna: %5.2f V - %.2f kHz", (package->v_lf125 * ANTENNA_ERROR) / 1000.0, 12000.0 / (LF_DIVISOR_125 + 1)); if (package->v_lf134 > NON_VOLTAGE) - PrintAndLogEx(SUCCESS, "LF antenna: %5.2f V - %.2f kHz", (package->v_lf134 * ANTENNA_ERROR) / 1000.0, 12000.0 / LF_DIVISOR_134); + PrintAndLogEx(SUCCESS, "LF antenna: %5.2f V - %.2f kHz", (package->v_lf134 * ANTENNA_ERROR) / 1000.0, 12000.0 / (LF_DIVISOR_134 + 1)); if (package->v_lfconf > NON_VOLTAGE && package->divisor > 0 && package->divisor != LF_DIVISOR_125 && package->divisor != LF_DIVISOR_134) - PrintAndLogEx(SUCCESS, "LF antenna: %5.2f V - %.2f kHz", (package->v_lfconf * ANTENNA_ERROR) / 1000.0, 12000.0 / package->divisor); + PrintAndLogEx(SUCCESS, "LF antenna: %5.2f V - %.2f kHz", (package->v_lfconf * ANTENNA_ERROR) / 1000.0, 12000.0 / (package->divisor + 1)); if (package->peak_v > NON_VOLTAGE && package->peak_f > 0) - PrintAndLogEx(SUCCESS, "LF optimal: %5.2f V - %6.2f kHz", (package->peak_v * ANTENNA_ERROR) / 1000.0, 12000.0 / package->peak_f); + PrintAndLogEx(SUCCESS, "LF optimal: %5.2f V - %6.2f kHz", (package->peak_v * ANTENNA_ERROR) / 1000.0, 12000.0 / (package->peak_f + 1)); char judgement[20]; memset(judgement, 0, sizeof(judgement)); @@ -1730,7 +1730,7 @@ int CmdTuneSamples(const char *Cmd) { if (test1 > 0) { PrintAndLogEx(SUCCESS, "\nDisplaying LF tuning graph. Divisor %d is %.2f kHz, %d is %.2f kHz.\n\n", - LF_DIVISOR_134, 12000.0 / LF_DIVISOR_134, LF_DIVISOR_125, 12000.0 / LF_DIVISOR_125); + LF_DIVISOR_134, 12000.0 / (LF_DIVISOR_134 + 1), LF_DIVISOR_125, 12000.0 / (LF_DIVISOR_125 + 1)); GraphTraceLen = 256; ShowGraphWindow(); RepaintGraphWindow(); diff --git a/client/cmdlf.c b/client/cmdlf.c index aa7823b34..88ab0d200 100644 --- a/client/cmdlf.c +++ b/client/cmdlf.c @@ -109,7 +109,7 @@ static int usage_lf_config(void) { PrintAndLogEx(NORMAL, " h This help"); PrintAndLogEx(NORMAL, " L Low frequency (125 kHz)"); PrintAndLogEx(NORMAL, " H High frequency (134 kHz)"); - PrintAndLogEx(NORMAL, " q Manually set divisor. 88-> 134 kHz, 95-> 125 kHz"); + PrintAndLogEx(NORMAL, " q Manually set divisor. %d -> 134 kHz, %d -> 125 kHz", LF_DIVISOR_134, LF_DIVISOR_125); PrintAndLogEx(NORMAL, " b Sets resolution of bits per sample. Default (max): 8"); PrintAndLogEx(NORMAL, " d Sets decimation. A value of N saves only 1 in N samples. Default: 1"); PrintAndLogEx(NORMAL, " a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1"); @@ -193,39 +193,67 @@ static int usage_lf_find(void) { return PM3_SUCCESS; } static int usage_lf_tune(void) { - PrintAndLogEx(NORMAL, "Continuously measure LF antenna tuning at 125 kHz."); + PrintAndLogEx(NORMAL, "Continuously measure LF antenna tuning."); PrintAndLogEx(NORMAL, "Press button or Enter to interrupt."); - PrintAndLogEx(NORMAL, "Usage: lf tune [h] []"); + PrintAndLogEx(NORMAL, "Usage: lf tune [h] [n ] [q ]"); PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, " h - This help"); - PrintAndLogEx(NORMAL, " - number of iterations (default: 0=infinite)"); + PrintAndLogEx(NORMAL, " n - number of iterations (default: 0=infinite)"); + PrintAndLogEx(NORMAL, " q - Frequency divisor. %d -> 134 kHz, %d -> 125 kHz", LF_DIVISOR_134, LF_DIVISOR_125); return PM3_SUCCESS; } int CmdLFTune(const char *Cmd) { - char cmdp = tolower(param_getchar(Cmd, 0)); - if (cmdp == 'h') return usage_lf_tune(); - int iter = param_get32ex(Cmd, 0, 0, 10); + int iter = 0; + uint8_t divisor = LF_DIVISOR_125;//Frequency divisor + bool errors = false; + uint8_t cmdp = 0; + while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { + switch (param_getchar(Cmd, cmdp)) { + case 'h': + return usage_lf_tune(); + case 'q': + errors |= param_getdec(Cmd, cmdp + 1, &divisor); + cmdp += 2; + break; + case 'n': + iter = param_get32ex(Cmd, cmdp + 1, 0, 10); + cmdp += 2; + break; + default: + PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); + errors = 1; + break; + } + } - PrintAndLogEx(SUCCESS, "Measuring LF antenna at 125kHz, click button or press Enter to exit"); + //Validations + if (errors || divisor < 19) return usage_lf_tune(); + if (divisor < 19) { + PrintAndLogEx(ERR, "divisor must be between 19 and 255"); + return PM3_EINVARG; + } - uint8_t mode[] = {1}; + PrintAndLogEx(SUCCESS, "Measuring LF antenna at %.2f kHz, click button or press Enter to exit", 12000.0 / (divisor + 1)); + + uint8_t params[] = {1, 0}; + params[1] = divisor; PacketResponseNG resp; clearCommandBuffer(); - SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_LF, mode, sizeof(mode)); + SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_LF, params, sizeof(params)); if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_LF, &resp, 1000)) { PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark LF initialization, aborting"); return PM3_ETIMEOUT; } - mode[0] = 2; + params[0] = 2; // loop forever (till button pressed) if iter = 0 (default) for (uint8_t i = 0; iter == 0 || i < iter; i++) { if (kbd_enter_pressed()) { // abort by keyboard press break; } - SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_LF, mode, sizeof(mode)); + SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_LF, params, sizeof(params)); if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_LF, &resp, 1000)) { PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark LF measure, aborting"); return PM3_ETIMEOUT; @@ -235,8 +263,8 @@ int CmdLFTune(const char *Cmd) { uint32_t volt = resp.data.asDwords[0]; PrintAndLogEx(INPLACE, "%u mV / %5u V", volt, (uint32_t)(volt / 1000)); } - mode[0] = 3; - SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_LF, mode, sizeof(mode)); + params[0] = 3; + SendCommandNG(CMD_MEASURE_ANTENNA_TUNING_LF, params, sizeof(params)); if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_LF, &resp, 1000)) { PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark LF shutdown, aborting"); return PM3_ETIMEOUT; @@ -426,11 +454,11 @@ int CmdLFSetConfig(const char *Cmd) { case 'h': return usage_lf_config(); case 'H': - divisor = 88; + divisor = LF_DIVISOR_134; cmdp++; break; case 'L': - divisor = 95; + divisor = LF_DIVISOR_125; cmdp++; break; case 'q': diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index ed2a4585c..010759a30 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -579,10 +579,9 @@ typedef struct { #define PM3_EFATAL -99 // LF -//#define LF_DIVISOR(f) ((12000 + (f)/2)/(f)) -//Note that 90 = 133.33 is closer to 134 than 89 = 134.83 -#define LF_DIVISOR_125 96 -#define LF_DIVISOR_134 89 +#define LF_DIVISOR(f) (((12000 + (f)/2)/(f))-1) +#define LF_DIVISOR_125 LF_DIVISOR(125) +#define LF_DIVISOR_134 LF_DIVISOR(134) // Receiving from USART need more than 30ms as we used on USB // else we get errors about partial packet reception