From fdcfbdcc21790a1e50a6cf5e27d30f7997da0f84 Mon Sep 17 00:00:00 2001 From: Richard Antony Burton Date: Mon, 30 May 2016 18:30:38 +0100 Subject: [PATCH] add l/h option to hw tune and optimize order of tuning --- .gitignore | 1 + CHANGELOG.md | 1 + armsrc/appmain.c | 63 +++++++++++++++++++++++++++++++-------------- armsrc/fpgaloader.c | 4 +++ armsrc/fpgaloader.h | 1 + client/cmddata.c | 14 ++++++++-- client/cmdhw.c | 2 +- include/usb_cmd.h | 5 ++++ 8 files changed, 69 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index fa74326e..4af73fb1 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ flasher version.c lua luac +fpga_compress fpga/* !fpga/tests diff --git a/CHANGELOG.md b/CHANGELOG.md index 7055d087..57fb549a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ of stream transmissions (marshmellow) - Added 'hf snoop'. This command take digitalized signal from FPGA and put in BigBuffer. (pwpiwi + enio) - Added Topaz (NFC type 1) protocol support ('hf topaz reader', 'hf list topaz', 'hf 14a raw -T', 'hf topaz snoop'). (piwi) - Added option c to 'hf list' (mark CRC bytes) (piwi) +- Added option `l` or `h` to `hw tune` to save time and unnecessary fpga writes if you are only interested in lf or hf. ### Changed - Added `[l] ` option to data printdemodbuffer diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 475b1c1b..eded359e 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -182,13 +182,9 @@ int AvgAdc(int ch) // was static - merlok return (a + 15) >> 5; } -void MeasureAntennaTuning(void) +void MeasureAntennaTuningLfOnly(int *vLf125, int *vLf134, int *peakf, int *peakv, uint8_t LF_Results[]) { - uint8_t LF_Results[256]; - int i, adcval = 0, peak = 0, peakv = 0, peakf = 0; //ptr = 0 - int vLf125 = 0, vLf134 = 0, vHf = 0; // in mV - - LED_B_ON(); + int i, adcval = 0, peak = 0; /* * Sweeps the useful LF range of the proxmark from @@ -198,38 +194,67 @@ void MeasureAntennaTuning(void) * the resonating frequency of your LF antenna * ( hopefully around 95 if it is tuned to 125kHz!) */ - - FpgaDownloadAndGo(FPGA_BITSTREAM_LF); + + FpgaDownloadAndGo(FPGA_BITSTREAM_LF); FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | FPGA_LF_ADC_READER_FIELD); for (i=255; i>=19; i--) { - WDT_HIT(); + WDT_HIT(); FpgaSendCommand(FPGA_CMD_SET_DIVISOR, i); SpinDelay(20); adcval = ((MAX_ADC_LF_VOLTAGE * AvgAdc(ADC_CHAN_LF)) >> 10); - if (i==95) vLf125 = adcval; // voltage at 125Khz - if (i==89) vLf134 = adcval; // voltage at 134Khz + if (i==95) *vLf125 = adcval; // voltage at 125Khz + if (i==89) *vLf134 = adcval; // voltage at 134Khz LF_Results[i] = adcval>>8; // scale int to fit in byte for graphing purposes if(LF_Results[i] > peak) { - peakv = adcval; + *peakv = adcval; peak = LF_Results[i]; - peakf = i; + *peakf = i; //ptr = i; } } for (i=18; i >= 0; i--) LF_Results[i] = 0; - - LED_A_ON(); + + return; +} + +void MeasureAntennaTuningHfOnly(int *vHf) +{ // Let the FPGA drive the high-frequency antenna around 13.56 MHz. - FpgaDownloadAndGo(FPGA_BITSTREAM_HF); + LED_A_ON(); + FpgaDownloadAndGo(FPGA_BITSTREAM_HF); FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); SpinDelay(20); - vHf = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10; + *vHf = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10; + LED_A_OFF(); + + return; +} + +void MeasureAntennaTuning(int mode) +{ + uint8_t LF_Results[256] = {0}; + int peakv = 0, peakf = 0; + int vLf125 = 0, vLf134 = 0, vHf = 0; // in mV + + LED_B_ON(); + + if (((mode & FLAG_TUNE_ALL) == FLAG_TUNE_ALL) && (FpgaGetCurrent() == FPGA_BITSTREAM_HF)) { + // Reverse "standard" order if HF already loaded, to avoid unnecessary swap. + MeasureAntennaTuningHfOnly(&vHf); + MeasureAntennaTuningLfOnly(&vLf125, &vLf134, &peakf, &peakv, LF_Results); + } else { + if (mode & FLAG_TUNE_LF) { + MeasureAntennaTuningLfOnly(&vLf125, &vLf134, &peakf, &peakv, LF_Results); + } + if (mode & FLAG_TUNE_HF) { + MeasureAntennaTuningHfOnly(&vHf); + } + } cmd_send(CMD_MEASURED_ANTENNA_TUNING, vLf125 | (vLf134<<16), vHf, peakf | (peakv<<16), LF_Results, 256); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); - LED_A_OFF(); LED_B_OFF(); return; } @@ -1231,7 +1256,7 @@ void UsbPacketReceived(uint8_t *packet, int len) break; case CMD_MEASURE_ANTENNA_TUNING: - MeasureAntennaTuning(); + MeasureAntennaTuning(c->arg[0]); break; case CMD_MEASURE_ANTENNA_TUNING_HF: diff --git a/armsrc/fpgaloader.c b/armsrc/fpgaloader.c index 308dda8c..e211c12f 100644 --- a/armsrc/fpgaloader.c +++ b/armsrc/fpgaloader.c @@ -566,3 +566,7 @@ void Fpga_print_status(void) else if(downloaded_bitstream == FPGA_BITSTREAM_LF) Dbprintf(" mode.............LF"); else Dbprintf(" mode.............%d", downloaded_bitstream); } + +int FpgaGetCurrent() { + return downloaded_bitstream; +} diff --git a/armsrc/fpgaloader.h b/armsrc/fpgaloader.h index 38724cdb..7dfc5c12 100644 --- a/armsrc/fpgaloader.h +++ b/armsrc/fpgaloader.h @@ -18,6 +18,7 @@ void FpgaSetupSsc(void); void SetupSpi(int mode); bool FpgaSetupSscDma(uint8_t *buf, int len); void Fpga_print_status(); +int FpgaGetCurrent(); #define FpgaDisableSscDma(void) AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS; #define FpgaEnableSscDma(void) AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN; void SetAdcMuxFor(uint32_t whichGpio); diff --git a/client/cmddata.c b/client/cmddata.c index c7fdc91e..394dcbc2 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -2048,10 +2048,20 @@ int CmdSamples(const char *Cmd) int CmdTuneSamples(const char *Cmd) { - int timeout = 0; + int timeout = 0, arg = FLAG_TUNE_ALL; + + if(*Cmd == 'l') { + arg = FLAG_TUNE_LF; + } else if (*Cmd == 'h') { + arg = FLAG_TUNE_HF; + } else if (*Cmd != '\0') { + PrintAndLog("use 'tune' or 'tune l' or 'tune h'"); + return 0; + } + printf("\nMeasuring antenna characteristics, please wait..."); - UsbCommand c = {CMD_MEASURE_ANTENNA_TUNING}; + UsbCommand c = {CMD_MEASURE_ANTENNA_TUNING, {arg, 0, 0}}; SendCommand(&c); UsbCommand resp; diff --git a/client/cmdhw.c b/client/cmdhw.c index 33dc78ae..74e476eb 100644 --- a/client/cmdhw.c +++ b/client/cmdhw.c @@ -467,7 +467,7 @@ static command_t CommandTable[] = {"reset", CmdReset, 0, "Reset the Proxmark3"}, {"setlfdivisor", CmdSetDivisor, 0, "<19 - 255> -- Drive LF antenna at 12Mhz/(divisor+1)"}, {"setmux", CmdSetMux, 0, " -- Set the ADC mux to a specific value"}, - {"tune", CmdTune, 0, "Measure antenna tuning"}, + {"tune", CmdTune, 0, "['l'|'h'] -- Measure antenna tuning (option 'l' or 'h' to limit to LF or HF)"}, {"version", CmdVersion, 0, "Show version information about the connected Proxmark"}, {"status", CmdStatus, 0, "Show runtime status information about the connected Proxmark"}, {"ping", CmdPing, 0, "Test if the pm3 is responsive"}, diff --git a/include/usb_cmd.h b/include/usb_cmd.h index 8448b06a..6978a3ad 100644 --- a/include/usb_cmd.h +++ b/include/usb_cmd.h @@ -228,6 +228,11 @@ typedef struct{ #define FLAG_ICLASS_READER_CEDITKEY 0x40 +//hw tune args +#define FLAG_TUNE_LF 1 +#define FLAG_TUNE_HF 2 +#define FLAG_TUNE_ALL 3 + // CMD_DEVICE_INFO response packet has flags in arg[0], flag definitions: /* Whether a bootloader that understands the common_area is present */ -- 2.39.5