fix divisor<>freq computations, add q to lf tune

This commit is contained in:
Philippe Teuwen 2019-10-03 21:18:37 +02:00
commit df08e7970c
5 changed files with 55 additions and 28 deletions

View file

@ -1473,7 +1473,7 @@ static void PacketReceived(PacketCommandNG *packet) {
break; break;
} }
case CMD_MEASURE_ANTENNA_TUNING_LF: { 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); reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF, PM3_EINVARG, NULL, 0);
switch (packet->data.asBytes[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 // Let the FPGA drive the low-frequency antenna around 125kHz
FpgaDownloadAndGo(FPGA_BITSTREAM_LF); FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | FPGA_LF_ADC_READER_FIELD); 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); reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF, PM3_SUCCESS, NULL, 0);
break; break;
case 2: case 2:

View file

@ -29,7 +29,7 @@ sample_config config = { 1, 8, 1, LF_DIVISOR_125, 0, 0 } ;
void printConfig() { void printConfig() {
uint32_t d = config.divisor; uint32_t d = config.divisor;
DbpString(_BLUE_("LF Sampling config")); 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(" [b] bps.................%d", config.bits_per_sample);
Dbprintf(" [d] decimation..........%d", config.decimation); Dbprintf(" [d] decimation..........%d", config.decimation);
Dbprintf(" [a] averaging...........%s", (config.averaging) ? "Yes" : "No"); Dbprintf(" [a] averaging...........%s", (config.averaging) ? "Yes" : "No");

View file

@ -1676,16 +1676,16 @@ int CmdTuneSamples(const char *Cmd) {
struct p* package = (struct p*)resp.data.asBytes; struct p* package = (struct p*)resp.data.asBytes;
if (package->v_lf125 > NON_VOLTAGE) 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) 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) 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) 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]; char judgement[20];
memset(judgement, 0, sizeof(judgement)); memset(judgement, 0, sizeof(judgement));
@ -1730,7 +1730,7 @@ int CmdTuneSamples(const char *Cmd) {
if (test1 > 0) { if (test1 > 0) {
PrintAndLogEx(SUCCESS, "\nDisplaying LF tuning graph. Divisor %d is %.2f kHz, %d is %.2f kHz.\n\n", 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; GraphTraceLen = 256;
ShowGraphWindow(); ShowGraphWindow();
RepaintGraphWindow(); RepaintGraphWindow();

View file

@ -109,7 +109,7 @@ static int usage_lf_config(void) {
PrintAndLogEx(NORMAL, " h This help"); PrintAndLogEx(NORMAL, " h This help");
PrintAndLogEx(NORMAL, " L Low frequency (125 kHz)"); PrintAndLogEx(NORMAL, " L Low frequency (125 kHz)");
PrintAndLogEx(NORMAL, " H High frequency (134 kHz)"); PrintAndLogEx(NORMAL, " H High frequency (134 kHz)");
PrintAndLogEx(NORMAL, " q <divisor> Manually set divisor. 88-> 134 kHz, 95-> 125 kHz"); PrintAndLogEx(NORMAL, " q <divisor> Manually set divisor. %d -> 134 kHz, %d -> 125 kHz", LF_DIVISOR_134, LF_DIVISOR_125);
PrintAndLogEx(NORMAL, " b <bps> Sets resolution of bits per sample. Default (max): 8"); PrintAndLogEx(NORMAL, " b <bps> Sets resolution of bits per sample. Default (max): 8");
PrintAndLogEx(NORMAL, " d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1"); PrintAndLogEx(NORMAL, " d <decim> 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"); 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; return PM3_SUCCESS;
} }
static int usage_lf_tune(void) { 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, "Press button or Enter to interrupt.");
PrintAndLogEx(NORMAL, "Usage: lf tune [h] [<iter>]"); PrintAndLogEx(NORMAL, "Usage: lf tune [h] [n <iter>] [q <divisor>]");
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h - This help"); PrintAndLogEx(NORMAL, " h - This help");
PrintAndLogEx(NORMAL, " <iter> - number of iterations (default: 0=infinite)"); PrintAndLogEx(NORMAL, " n <iter> - number of iterations (default: 0=infinite)");
PrintAndLogEx(NORMAL, " q <divisor> - Frequency divisor. %d -> 134 kHz, %d -> 125 kHz", LF_DIVISOR_134, LF_DIVISOR_125);
return PM3_SUCCESS; return PM3_SUCCESS;
} }
int CmdLFTune(const char *Cmd) { int CmdLFTune(const char *Cmd) {
char cmdp = tolower(param_getchar(Cmd, 0)); int iter = 0;
if (cmdp == 'h') return usage_lf_tune(); uint8_t divisor = LF_DIVISOR_125;//Frequency divisor
int iter = param_get32ex(Cmd, 0, 0, 10); 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; PacketResponseNG resp;
clearCommandBuffer(); 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)) { if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_LF, &resp, 1000)) {
PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark LF initialization, aborting"); PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark LF initialization, aborting");
return PM3_ETIMEOUT; return PM3_ETIMEOUT;
} }
mode[0] = 2; params[0] = 2;
// loop forever (till button pressed) if iter = 0 (default) // loop forever (till button pressed) if iter = 0 (default)
for (uint8_t i = 0; iter == 0 || i < iter; i++) { for (uint8_t i = 0; iter == 0 || i < iter; i++) {
if (kbd_enter_pressed()) { // abort by keyboard press if (kbd_enter_pressed()) { // abort by keyboard press
break; 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)) { if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_LF, &resp, 1000)) {
PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark LF measure, aborting"); PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark LF measure, aborting");
return PM3_ETIMEOUT; return PM3_ETIMEOUT;
@ -235,8 +263,8 @@ int CmdLFTune(const char *Cmd) {
uint32_t volt = resp.data.asDwords[0]; uint32_t volt = resp.data.asDwords[0];
PrintAndLogEx(INPLACE, "%u mV / %5u V", volt, (uint32_t)(volt / 1000)); PrintAndLogEx(INPLACE, "%u mV / %5u V", volt, (uint32_t)(volt / 1000));
} }
mode[0] = 3; params[0] = 3;
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)) { if (!WaitForResponseTimeout(CMD_MEASURE_ANTENNA_TUNING_LF, &resp, 1000)) {
PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark LF shutdown, aborting"); PrintAndLogEx(WARNING, "Timeout while waiting for Proxmark LF shutdown, aborting");
return PM3_ETIMEOUT; return PM3_ETIMEOUT;
@ -426,11 +454,11 @@ int CmdLFSetConfig(const char *Cmd) {
case 'h': case 'h':
return usage_lf_config(); return usage_lf_config();
case 'H': case 'H':
divisor = 88; divisor = LF_DIVISOR_134;
cmdp++; cmdp++;
break; break;
case 'L': case 'L':
divisor = 95; divisor = LF_DIVISOR_125;
cmdp++; cmdp++;
break; break;
case 'q': case 'q':

View file

@ -579,10 +579,9 @@ typedef struct {
#define PM3_EFATAL -99 #define PM3_EFATAL -99
// LF // LF
//#define LF_DIVISOR(f) ((12000 + (f)/2)/(f)) #define LF_DIVISOR(f) (((12000 + (f)/2)/(f))-1)
//Note that 90 = 133.33 is closer to 134 than 89 = 134.83 #define LF_DIVISOR_125 LF_DIVISOR(125)
#define LF_DIVISOR_125 96 #define LF_DIVISOR_134 LF_DIVISOR(134)
#define LF_DIVISOR_134 89
// Receiving from USART need more than 30ms as we used on USB // Receiving from USART need more than 30ms as we used on USB
// else we get errors about partial packet reception // else we get errors about partial packet reception