From f7156e7485e03b22d9f8077caccd0a173328b28f Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 Jan 2020 22:05:01 +0100 Subject: [PATCH] chg: adapted lfsampling, and swapped from 'silent' logic to the more natural 'verbose' logic --- armsrc/appmain.c | 6 +- armsrc/lfadc.c | 24 +++-- armsrc/lfops.c | 34 +++--- armsrc/lfsampling.c | 246 ++++++++++++++++++++++++++------------------ armsrc/lfsampling.h | 32 ++++-- client/cmddata.c | 12 +-- client/cmddata.h | 2 +- client/cmdhf15.c | 6 +- client/cmdlf.c | 32 +++--- client/cmdlf.h | 2 +- client/cmdlfcotag.c | 2 +- client/cmdlft55xx.c | 4 +- 12 files changed, 236 insertions(+), 166 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 20c26eb4d..a54967262 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -730,11 +730,11 @@ static void PacketReceived(PacketCommandNG *packet) { } case CMD_LF_ACQ_RAW_ADC: { struct p { - uint8_t silent; + uint8_t verbose; uint32_t samples; } PACKED; struct p *payload = (struct p *)packet->data.asBytes; - uint32_t bits = SampleLF(payload->silent, payload->samples); + uint32_t bits = SampleLF(payload->verbose, payload->samples); reply_ng(CMD_LF_ACQ_RAW_ADC, PM3_SUCCESS, (uint8_t *)&bits, sizeof(bits)); break; } @@ -1603,7 +1603,7 @@ static void PacketReceived(PacketCommandNG *packet) { BigBuf_Clear_ext(false); BigBuf_free(); } - + // 40 000 - (512-3) 509 = 39491 uint16_t offset = MIN(BIGBUF_SIZE - PM3_CMD_DATA_SIZE - 3, payload->offset); diff --git a/armsrc/lfadc.c b/armsrc/lfadc.c index c192087c4..a42fe8c52 100644 --- a/armsrc/lfadc.c +++ b/armsrc/lfadc.c @@ -61,7 +61,7 @@ size_t lf_count_edge_periods_ex(size_t max, bool wait, bool detect_gap) { adc_val = AT91C_BASE_SSC->SSC_RHR; periods++; - if (logging) logSample(adc_val, 1, 8, 0, 0); + if (logging) logSample(adc_val, 1, 8, 0); // Only test field changes if state of adc values matter if (!wait) { @@ -91,7 +91,7 @@ size_t lf_count_edge_periods_ex(size_t max, bool wait, bool detect_gap) { if (periods == max) return 0; } } - if (logging) logSample(255, 1, 8, 0, 0); + if (logging) logSample(255, 1, 8, 0); return 0; } @@ -168,7 +168,7 @@ void lf_init(bool reader) { AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // Prepare data trace - if (logging) initSamplingBuffer(); + if (logging) initSamplingBuffer(NULL); } @@ -189,9 +189,21 @@ void lf_finalize() { size_t lf_detect_field_drop(size_t max) { size_t periods = 0; volatile uint8_t adc_val; + int16_t checked = 0; + + while (true) { + + // only every 1000th times, in order to save time when collecting samples. + if (checked == 1000) { + if (BUTTON_PRESS() || data_available()) { + checked = -1; + break; + } else { + checked = 0; + } + } + ++checked; - // usb check? - while (!BUTTON_PRESS()) { // Watchdog hit WDT_HIT(); @@ -199,7 +211,7 @@ size_t lf_detect_field_drop(size_t max) { periods++; adc_val = AT91C_BASE_SSC->SSC_RHR; - if (logging) logSample(adc_val, 1, 8, 0, 0); + if (logging) logSample(adc_val, 1, 8, 0); if (adc_val == 0) { rising_edge = false; diff --git a/armsrc/lfops.c b/armsrc/lfops.c index 12cd82e43..ed23e75ea 100644 --- a/armsrc/lfops.c +++ b/armsrc/lfops.c @@ -473,7 +473,7 @@ void ModThenAcquireRawAdcSamples125k(uint32_t delay_off, uint32_t period_0, uint FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER | FPGA_LF_ADC_READER_FIELD); // now do the read - DoAcquisition_config(false, 0); + DoAcquisition_config(true, 0); // Turn off antenna FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); @@ -1182,7 +1182,7 @@ static void nrzSimBit(uint8_t c, int *n, uint8_t clock) { *n += clock; } -// args clock, +// args clock, void CmdNRZsimTAG(uint8_t invert, uint8_t separator, uint8_t clk, uint16_t size, uint8_t *bits, bool ledcontrol) { FpgaDownloadAndGo(FPGA_BITSTREAM_LF); @@ -1198,7 +1198,7 @@ void CmdNRZsimTAG(uint8_t invert, uint8_t separator, uint8_t clk, uint16_t size, nrzSimBit(bits[i] ^ invert, &n, clk); } - if (bits[0] == bits[size - 1]) { //run a second set inverted (for ask/raw || biphase phase) + if (bits[0] == bits[size - 1]) { for (i = 0; i < size; i++) { nrzSimBit(bits[i] ^ invert ^ 1, &n, clk); } @@ -1239,7 +1239,7 @@ void CmdHIDdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol) WDT_HIT(); if (ledcontrol) LED_A_ON(); - DoAcquisition_default(-1, true); + DoAcquisition_default(-1, false); // FSK demodulator size = 50 * 128 * 2; //big enough to catch 2 sequences of largest format int idx = HIDdemodFSK(dest, &size, &hi2, &hi, &lo, &dummyIdx); @@ -1330,7 +1330,7 @@ void CmdAWIDdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol) WDT_HIT(); if (ledcontrol) LED_A_ON(); - DoAcquisition_default(-1, true); + DoAcquisition_default(-1, false); // FSK demodulator size = MIN(12800, BigBuf_max_traceLen()); @@ -1421,11 +1421,11 @@ void CmdEM410xdemod(int findone, uint32_t *high, uint64_t *low, int ledcontrol) WDT_HIT(); if (ledcontrol) LED_A_ON(); - DoAcquisition_default(-1, true); + DoAcquisition_default(-1, false); size = MIN(16385, BigBuf_max_traceLen()); - - //askdemod and manchester decode + + //askdemod and manchester decode int errCnt = askdemod(dest, &size, &clk, &invert, maxErr, 0, 1); WDT_HIT(); @@ -1486,10 +1486,10 @@ void CmdIOdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol) { WDT_HIT(); if (ledcontrol) LED_A_ON(); - DoAcquisition_default(-1, true); + DoAcquisition_default(-1, false); size = MIN(12000, BigBuf_max_traceLen()); - + //fskdemod and get start index int idx = detectIOProx(dest, &size, &dummyIdx); if (idx < 0) continue; @@ -1772,7 +1772,7 @@ void T55xxResetRead(uint8_t flags) { TurnReadLFOn(T55xx_Timing.m[downlink_mode].read_gap); // Acquisition - DoPartialAcquisition(0, true, BigBuf_max_traceLen(), 0); + DoPartialAcquisition(0, false, BigBuf_max_traceLen(), 0); // Turn the field off FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); @@ -1869,7 +1869,7 @@ void T55xxWriteBlock(uint8_t *data) { // response should be (for t55x7) a 0 bit then (ST if on) // block data written in on repeat until reset. - //DoPartialAcquisition(20, true, 12000); + //DoPartialAcquisition(20, false, 12000); } // turn field off FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); @@ -1930,7 +1930,7 @@ bool brute_mem = (flags & 0x0100) >> 8; // Acquisition // Now do the acquisition - DoPartialAcquisition(0, true, samples, 0); + DoPartialAcquisition(0, false, samples, 0); // Turn the field off if (!brute_mem) { @@ -1990,7 +1990,7 @@ void T55xxReadBlock(uint8_t page, bool pwd_mode, bool brute_mem, uint8_t block, // Acquisition // Now do the acquisition - DoPartialAcquisition(0, true, samples, 0); + DoPartialAcquisition(0, false, samples, 0); // Turn the field off if (!brute_mem) { @@ -2447,7 +2447,7 @@ void EM4xReadWord(uint8_t addr, uint32_t pwd, uint8_t usepwd) { WaitUS(400); - DoPartialAcquisition(20, true, 6000, 1000); + DoPartialAcquisition(20, false, 6000, 1000); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); reply_ng(CMD_LF_EM4X_READWORD, PM3_SUCCESS, NULL, 0); @@ -2480,7 +2480,7 @@ void EM4xWriteWord(uint8_t addr, uint32_t data, uint32_t pwd, uint8_t usepwd) { //Wait 20ms for write to complete? WaitMS(7); - DoPartialAcquisition(20, true, 6000, 1000); + DoPartialAcquisition(20, false, 6000, 1000); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); reply_ng(CMD_LF_EM4X_WRITEWORD, PM3_SUCCESS, NULL, 0); @@ -2550,7 +2550,7 @@ void Cotag(uint32_t arg0) { doCotagAcquisitionManchester(); break; case 2: - DoAcquisition_config(true, 0); + DoAcquisition_config(true, true); break; } diff --git a/armsrc/lfsampling.c b/armsrc/lfsampling.c index bdce67ef9..d836accfa 100644 --- a/armsrc/lfsampling.c +++ b/armsrc/lfsampling.c @@ -15,6 +15,7 @@ #include "dbprint.h" #include "util.h" #include "lfdemod.h" +#include "string.h" // memset /* Default LF config is set to: @@ -32,7 +33,7 @@ void printConfig() { uint32_t d = config.divisor; DbpString(_BLUE_("LF Sampling config")); 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] bits per sample.....%d", config.bits_per_sample); Dbprintf(" [d] decimation..........%d", config.decimation); Dbprintf(" [a] averaging...........%s", (config.averaging) ? "Yes" : "No"); Dbprintf(" [t] trigger threshold...%d", config.trigger_threshold); @@ -69,12 +70,6 @@ sample_config *getSamplingConfig() { return &config; } -struct BitstreamOut { - uint8_t *buffer; - uint32_t numbits; - uint32_t position; -}; - /** * @brief Pushes bit onto the stream * @param stream @@ -88,6 +83,87 @@ void pushBit(BitstreamOut *stream, uint8_t bit) { stream->numbits++; } +// Holds bit packed struct of samples. +BitstreamOut data = {0, 0, 0}; + +// internal struct to keep track of samples gathered +sampling_t samples = {0, 0, 0, 0}; + +void initSampleBuffer(uint32_t *sample_size) { + + if (sample_size == NULL || *sample_size == 0) { + *sample_size = BigBuf_max_traceLen(); + } else { + *sample_size = MIN(*sample_size, BigBuf_max_traceLen()); + } + + // use a bitstream to handle the output + data.buffer = BigBuf_get_addr(); + + memset(data.buffer, 0, *sample_size); + + // + samples.dec_counter = 0; + samples.sum = 0; + samples.counter = 0; + samples.total_saved = 0; +} + +uint32_t getSampleCounter() { + return samples.total_saved; +} + +void logSample(uint8_t sample, uint8_t decimation, uint32_t bits_per_sample, bool avg) { + + if (!data.buffer) return; + + if (bits_per_sample == 0) bits_per_sample = 1; + if (bits_per_sample > 8) bits_per_sample = 8; + if (decimation == 0) decimation = 1; + + // keep track of total gather samples regardless how many was discarded. + samples.counter++; + + if (avg) { + samples.sum += sample; + } + + // check decimation + if (decimation > 1) { + samples.dec_counter++; + + if (samples.dec_counter < decimation) return; + + samples.dec_counter = 0; + } + + // averaging + if (avg && decimation > 1) { + sample = samples.sum / decimation; + samples.sum = 0; + } + + // store the sample + samples.total_saved++; + + if (bits_per_sample == 8) { + + data.buffer[samples.total_saved - 1] = sample; + + // add number of bits. + data.numbits = samples.total_saved << 3; + + } else { + pushBit(&data, sample & 0x80); + if (bits_per_sample > 1) pushBit(&data, sample & 0x40); + if (bits_per_sample > 2) pushBit(&data, sample & 0x20); + if (bits_per_sample > 3) pushBit(&data, sample & 0x10); + if (bits_per_sample > 4) pushBit(&data, sample & 0x08); + if (bits_per_sample > 5) pushBit(&data, sample & 0x04); + if (bits_per_sample > 6) pushBit(&data, sample & 0x02); + } +} + /** * Setup the FPGA to listen for samples. This method downloads the FPGA bitstream * if not already loaded, sets divisor and starts up the antenna. @@ -128,45 +204,39 @@ void LFSetupFPGAForADC(int divisor, bool lf_field) { * value that will be used is the average value of the three samples. * @param trigger_threshold - a threshold. The sampling won't commence until this threshold has been reached. Set * to -1 to ignore threshold. - * @param silent - is true, now outputs are made. If false, dbprints the status + * @param verbose - is true, dbprints the status, else no outputs * @return the number of bits occupied by the samples. */ -uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool averaging, int trigger_threshold, bool silent, int bufsize, uint32_t cancel_after, uint32_t samples_to_skip) { +uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool avg, int trigger_threshold, + bool verbose, uint32_t sample_size, uint32_t cancel_after, uint32_t samples_to_skip) { - uint8_t *dest = BigBuf_get_addr(); - bufsize = (bufsize > 0 && bufsize < BigBuf_max_traceLen()) ? bufsize : BigBuf_max_traceLen(); + initSampleBuffer(&sample_size); - if (bits_per_sample < 1) bits_per_sample = 1; - if (bits_per_sample > 8) bits_per_sample = 8; - - if (decimation < 1) decimation = 1; - - // use a bit stream to handle the output - BitstreamOut data = { dest, 0, 0}; - int sample_counter = 0; - uint8_t sample; - - // if we want to do averaging - uint32_t sample_sum = 0 ; - uint32_t sample_total_numbers = 0; - uint32_t sample_total_saved = 0; uint32_t cancel_counter = 0; - - uint16_t checked = 0; + int16_t checked = 0; while (true) { + + // only every 1000th times, in order to save time when collecting samples. if (checked == 1000) { - if (BUTTON_PRESS() || data_available()) + if (BUTTON_PRESS() || data_available()) { + checked = -1; break; - else + } else { checked = 0; + } } ++checked; WDT_HIT(); + if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) { +// AT91C_BASE_SSC->SSC_THR = 0x43; + LED_D_ON(); + } + if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { - sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; + volatile uint8_t sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; // Testpoint 8 (TP8) can be used to trigger oscilliscope LED_D_OFF(); @@ -188,57 +258,23 @@ uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool averag continue; } - sample_total_numbers++; + logSample(sample, decimation, bits_per_sample, avg); - if (averaging) - sample_sum += sample; - - // check decimation - if (decimation > 1) { - sample_counter++; - if (sample_counter < decimation) continue; - sample_counter = 0; - } - - // averaging - if (averaging && decimation > 1) { - sample = sample_sum / decimation; - sample_sum = 0; - } - - // store the sample - sample_total_saved ++; - - if (bits_per_sample == 8) { - dest[sample_total_saved - 1] = sample; - - // Get the return value correct - data.numbits = sample_total_saved << 3; - if (sample_total_saved >= bufsize) break; - - } else { - pushBit(&data, sample & 0x80); - if (bits_per_sample > 1) pushBit(&data, sample & 0x40); - if (bits_per_sample > 2) pushBit(&data, sample & 0x20); - if (bits_per_sample > 3) pushBit(&data, sample & 0x10); - if (bits_per_sample > 4) pushBit(&data, sample & 0x08); - if (bits_per_sample > 5) pushBit(&data, sample & 0x04); - if (bits_per_sample > 6) pushBit(&data, sample & 0x02); - - if ((data.numbits >> 3) + 1 >= bufsize) break; - } + if (samples.total_saved >= sample_size) break; } } - if (!silent) { - Dbprintf("Done, saved " _YELLOW_("%d")"out of " _YELLOW_("%d")"seen samples at " _YELLOW_("%d")"bits/sample", sample_total_saved, sample_total_numbers, bits_per_sample); - Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...", - dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]); + if (checked == -1 && verbose) { + Dbprintf("lf sampling aborted"); + } + + if (verbose) { + Dbprintf("Done, saved " _YELLOW_("%d")"out of " _YELLOW_("%d")"seen samples at " _YELLOW_("%d")"bits/sample", samples.total_saved, samples.counter, bits_per_sample); } // Ensure that DC offset removal and noise check is performed for any device-side processing - removeSignalOffset(dest, bufsize); - computeSignalProperties(dest, bufsize); + removeSignalOffset(data.buffer, samples.total_saved); + computeSignalProperties(data.buffer, samples.total_saved); return data.numbits; } @@ -247,32 +283,33 @@ uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool averag * This method is typically used by tag-specific readers who just wants to read the samples * the normal way * @param trigger_threshold - * @param silent + * @param verbose * @return number of bits sampled */ -uint32_t DoAcquisition_default(int trigger_threshold, bool silent) { - return DoAcquisition(1, 8, 0, trigger_threshold, silent, 0, 0, 0); +uint32_t DoAcquisition_default(int trigger_threshold, bool verbose) { + return DoAcquisition(1, 8, 0, trigger_threshold, verbose, 0, 0, 0); } -uint32_t DoAcquisition_config(bool silent, int sample_size) { +uint32_t DoAcquisition_config(bool verbose, uint32_t sample_size) { return DoAcquisition(config.decimation , config.bits_per_sample , config.averaging , config.trigger_threshold - , silent + , verbose , sample_size , 0 , config.samples_to_skip); } -uint32_t DoPartialAcquisition(int trigger_threshold, bool silent, int sample_size, uint32_t cancel_after) { - return DoAcquisition(1, 8, 0, trigger_threshold, silent, sample_size, cancel_after, 0); +uint32_t DoPartialAcquisition(int trigger_threshold, bool verbose, uint32_t sample_size, uint32_t cancel_after) { + return DoAcquisition(1, 8, 0, trigger_threshold, verbose, sample_size, cancel_after, 0); } -uint32_t ReadLF(bool activeField, bool silent, int sample_size) { - if (!silent) +uint32_t ReadLF(bool activeField, bool verbose, uint32_t sample_size) { + if (verbose) printConfig(); + LFSetupFPGAForADC(config.divisor, activeField); - uint32_t ret = DoAcquisition_config(silent, sample_size); + uint32_t ret = DoAcquisition_config(verbose, sample_size); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); return ret; } @@ -281,9 +318,9 @@ uint32_t ReadLF(bool activeField, bool silent, int sample_size) { * Initializes the FPGA for reader-mode (field on), and acquires the samples. * @return number of bits sampled **/ -uint32_t SampleLF(bool silent, int sample_size) { +uint32_t SampleLF(bool verbose, uint32_t sample_size) { BigBuf_Clear_ext(false); - return ReadLF(true, silent, sample_size); + return ReadLF(true, verbose, sample_size); } /** * Initializes the FPGA for sniffer-mode (field off), and acquires the samples. @@ -310,7 +347,7 @@ void doT55x7Acquisition(size_t sample_size) { if (bufsize > sample_size) bufsize = sample_size; - uint8_t curSample, lastSample = 0; + uint8_t lastSample = 0; uint16_t i = 0, skipCnt = 0; bool startFound = false; bool highFound = false; @@ -330,24 +367,24 @@ void doT55x7Acquisition(size_t sample_size) { WDT_HIT(); + if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) { + LED_D_ON(); + } if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { - curSample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; + volatile uint8_t sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; LED_D_OFF(); // skip until the first high sample above threshold - if (!startFound && curSample > T55xx_READ_UPPER_THRESHOLD) { - //if (curSample > lastSample) - // lastSample = curSample; + if (!startFound && sample > T55xx_READ_UPPER_THRESHOLD) { highFound = true; } else if (!highFound) { skipCnt++; continue; } // skip until the first low sample below threshold - if (!startFound && curSample < T55xx_READ_LOWER_THRESHOLD) { - //if (curSample > lastSample) - lastSample = curSample; + if (!startFound && sample < T55xx_READ_LOWER_THRESHOLD) { + lastSample = sample; lowFound = true; } else if (!lowFound) { skipCnt++; @@ -355,14 +392,14 @@ void doT55x7Acquisition(size_t sample_size) { } // skip until first high samples begin to change - if (startFound || curSample > T55xx_READ_LOWER_THRESHOLD + T55xx_READ_TOL) { + if (startFound || sample > T55xx_READ_LOWER_THRESHOLD + T55xx_READ_TOL) { // if just found start - recover last sample if (!startFound) { dest[i++] = lastSample; startFound = true; } // collect samples - dest[i++] = curSample; + dest[i++] = sample; } } } @@ -388,7 +425,7 @@ void doCotagAcquisition(size_t sample_size) { bufsize = sample_size; dest[0] = 0; - uint8_t sample, firsthigh = 0, firstlow = 0; + uint8_t firsthigh = 0, firstlow = 0; uint16_t i = 0; uint16_t noise_counter = 0; @@ -406,8 +443,12 @@ void doCotagAcquisition(size_t sample_size) { WDT_HIT(); + if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) { + LED_D_ON(); + } + if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { - sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; + volatile uint8_t sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; // find first peak if (!firsthigh) { @@ -441,7 +482,6 @@ void doCotagAcquisition(size_t sample_size) { // Ensure that DC offset removal and noise check is performed for any device-side processing removeSignalOffset(dest, bufsize); computeSignalProperties(dest, bufsize); - } uint32_t doCotagAcquisitionManchester() { @@ -453,7 +493,7 @@ uint32_t doCotagAcquisitionManchester() { bufsize = COTAG_BITS; dest[0] = 0; - uint8_t sample, firsthigh = 0, firstlow = 0; + uint8_t firsthigh = 0, firstlow = 0; uint16_t sample_counter = 0, period = 0; uint8_t curr = 0, prev = 0; uint16_t noise_counter = 0; @@ -471,8 +511,12 @@ uint32_t doCotagAcquisitionManchester() { WDT_HIT(); + if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) { + LED_D_ON(); + } + if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { - sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; + volatile uint8_t sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; // find first peak if (!firsthigh) { diff --git a/armsrc/lfsampling.h b/armsrc/lfsampling.h index 21968cdd2..8ccaa5579 100644 --- a/armsrc/lfsampling.h +++ b/armsrc/lfsampling.h @@ -4,7 +4,18 @@ #include "common.h" #include "pm3_cmd.h" -typedef struct BitstreamOut BitstreamOut; +typedef struct { + uint8_t *buffer; + uint32_t numbits; + uint32_t position; +} BitstreamOut; + +typedef struct { + int dec_counter; + uint32_t sum; + uint32_t counter; + uint32_t total_saved; +} sampling_t; /** * acquisition of Cotag LF signal. Similar to other LF, since the Cotag has such long datarate RF/384 @@ -23,7 +34,7 @@ void doT55x7Acquisition(size_t sample_size); * Initializes the FPGA for reader-mode (field on), and acquires the samples. * @return number of bits sampled **/ -uint32_t SampleLF(bool silent, int sample_size); +uint32_t SampleLF(bool verbose, uint32_t sample_size); /** * Initializes the FPGA for sniff-mode (field off), and acquires the samples. @@ -31,32 +42,35 @@ uint32_t SampleLF(bool silent, int sample_size); **/ uint32_t SniffLF(); +uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool avg, int trigger_threshold, + bool verbose, uint32_t sample_size, uint32_t cancel_after, uint32_t samples_to_skip); + // adds sample size to default options -uint32_t DoPartialAcquisition(int trigger_threshold, bool silent, int sample_size, uint32_t cancel_after); +uint32_t DoPartialAcquisition(int trigger_threshold, bool verbose, uint32_t sample_size, uint32_t cancel_after); /** * @brief Does sample acquisition, ignoring the config values set in the sample_config. * This method is typically used by tag-specific readers who just wants to read the samples * the normal way * @param trigger_threshold - * @param silent + * @param verbose * @return number of bits sampled */ -uint32_t DoAcquisition_default(int trigger_threshold, bool silent); +uint32_t DoAcquisition_default(int trigger_threshold, bool verbose); /** * @brief Does sample acquisition, using the config values set in the sample_config. * @param trigger_threshold - * @param silent + * @param verbose * @return number of bits sampled */ -uint32_t DoAcquisition_config(bool silent, int sample_size); +uint32_t DoAcquisition_config(bool verbose, uint32_t sample_size); /** * Refactoring of lf sampling buffer */ -void initSamplingBuffer(void); -void logSample(uint8_t sample, uint8_t decimation, uint32_t bits_per_sample, bool averaging, int trigger_threshold); +void initSamplingBuffer(uint32_t *sample_size); +void logSample(uint8_t sample, uint8_t decimation, uint32_t bits_per_sample, bool avg); uint32_t getSampleCounter(); /** diff --git a/client/cmddata.c b/client/cmddata.c index 0469bf837..ddd87fdb8 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -1589,7 +1589,7 @@ static uint8_t getByte(uint8_t bits_per_sample, BitstreamOut *b) { return val; } -int getSamples(uint32_t n, bool silent) { +int getSamples(uint32_t n, bool verbose) { //If we get all but the last byte in bigbuf, // we don't have to worry about remaining trash // in the last byte in case the bits-per-sample @@ -1599,7 +1599,7 @@ int getSamples(uint32_t n, bool silent) { if (n == 0 || n > sizeof(got)) n = sizeof(got); - if (!silent) PrintAndLogEx(NORMAL, "Reading %d bytes from device memory\n", n); + if (verbose) PrintAndLogEx(NORMAL, "Reading %d bytes from device memory\n", n); PacketResponseNG response; if (!GetFromDevice(BIG_BUF, got, n, 0, NULL, 0, &response, 10000, true)) { @@ -1607,20 +1607,20 @@ int getSamples(uint32_t n, bool silent) { return PM3_ETIMEOUT; } - if (!silent) PrintAndLogEx(NORMAL, "Data fetched"); + if (verbose) PrintAndLogEx(NORMAL, "Data fetched"); uint8_t bits_per_sample = 8; //Old devices without this feature would send 0 at arg[0] if (response.oldarg[0] > 0) { sample_config *sc = (sample_config *) response.data.asBytes; - if (!silent) PrintAndLogEx(NORMAL, "Samples @ %d bits/smpl, decimation 1:%d ", sc->bits_per_sample, sc->decimation); + if (verbose) PrintAndLogEx(NORMAL, "Samples @ %d bits/smpl, decimation 1:%d ", sc->bits_per_sample, sc->decimation); bits_per_sample = sc->bits_per_sample; } if (bits_per_sample < 8) { - if (!silent) PrintAndLogEx(NORMAL, "Unpacking..."); + if (verbose) PrintAndLogEx(NORMAL, "Unpacking..."); BitstreamOut bout = { got, bits_per_sample * n, 0}; int j = 0; @@ -1630,7 +1630,7 @@ int getSamples(uint32_t n, bool silent) { } GraphTraceLen = j; - if (!silent) PrintAndLogEx(NORMAL, "Unpacked %d samples", j); + if (verbose) PrintAndLogEx(NORMAL, "Unpacked %d samples", j); } else { for (int j = 0; j < n; j++) { diff --git a/client/cmddata.h b/client/cmddata.h index a4bba407c..a19bead27 100644 --- a/client/cmddata.h +++ b/client/cmddata.h @@ -68,7 +68,7 @@ void setDemodBuff(uint8_t *buff, size_t size, size_t start_idx); bool getDemodBuff(uint8_t *buff, size_t *size); void save_restoreDB(uint8_t saveOpt);// option '1' to save DemodBuffer any other to restore int AutoCorrelate(const int *in, int *out, size_t len, size_t window, bool SaveGrph, bool verbose); -int getSamples(uint32_t n, bool silent); +int getSamples(uint32_t n, bool verbose); void setClockGrid(uint32_t clk, int offset); int directionalThreshold(const int *in, int *out, size_t len, int8_t up, int8_t down); int AskEdgeDetect(const int *in, int *out, int len, int threshold); diff --git a/client/cmdhf15.c b/client/cmdhf15.c index 2e02aff68..9a5665a31 100644 --- a/client/cmdhf15.c +++ b/client/cmdhf15.c @@ -633,7 +633,7 @@ static int CmdHF15Samples(const char *Cmd) { clearCommandBuffer(); SendCommandNG(CMD_HF_ISO15693_ACQ_RAW_ADC, NULL, 0); - getSamples(0, false); + getSamples(0, true); return PM3_SUCCESS; } @@ -684,9 +684,9 @@ static int NxpSysInfo(uint8_t *uid) { return PM3_EWRONGANSVER; } - bool support_signature = (recv[5] & 0x01); + bool support_signature = (recv[5] & 0x01); bool support_easmode = (recv[4] & 0x03); - + PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, " NXP SYSINFO : %s", sprint_hex(recv, 8)); PrintAndLogEx(NORMAL, " Password protection configuration:"); diff --git a/client/cmdlf.c b/client/cmdlf.c index 7c454a7a0..64ff0ed9a 100644 --- a/client/cmdlf.c +++ b/client/cmdlf.c @@ -77,13 +77,13 @@ static int usage_lf_read(void) { PrintAndLogEx(NORMAL, "Usage: lf read [h] [s] [d numofsamples]"); PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, " h This help"); - PrintAndLogEx(NORMAL, " s silent run, no printout"); PrintAndLogEx(NORMAL, " d #samples # samples to collect (optional)"); + PrintAndLogEx(NORMAL, " s silent"); PrintAndLogEx(NORMAL, "Use 'lf config' to set parameters."); PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "Examples:"); - PrintAndLogEx(NORMAL, " lf read s d 12000 - collects 12000samples silent"); - PrintAndLogEx(NORMAL, " lf read s"); + PrintAndLogEx(NORMAL, " lf read s d 12000 - collects 12000 samples silent"); + PrintAndLogEx(NORMAL, " lf read"); return PM3_SUCCESS; } static int usage_lf_sim(void) { @@ -367,7 +367,7 @@ int CmdLFCommandRead(const char *Cmd) { if (resp.status == PM3_SUCCESS) { if (i) { PrintAndLogEx(SUCCESS, "Downloading response signal data"); - getSamples(0, true); + getSamples(0, false); return PM3_SUCCESS; } else { PrintAndLogEx(WARNING, "timeout while waiting for reply."); @@ -552,16 +552,16 @@ int CmdLFConfig(const char *Cmd) { return lf_config(&config); } -int lf_read(bool silent, uint32_t samples) { +int lf_read(bool verbose, uint32_t samples) { if (!session.pm3_present) return PM3_ENOTTY; struct p { - uint8_t silent; + uint8_t verbose; uint32_t samples; } PACKED; struct p payload; - payload.silent = silent; + payload.verbose = verbose; payload.samples = samples; clearCommandBuffer(); @@ -579,7 +579,7 @@ int lf_read(bool silent, uint32_t samples) { // resp.oldarg[0] is bits read not bytes read. uint32_t bits = (resp.data.asDwords[0] / 8); - getSamples(bits, silent); + getSamples(bits, verbose); return PM3_SUCCESS; } @@ -589,21 +589,21 @@ int CmdLFRead(const char *Cmd) { if (!session.pm3_present) return PM3_ENOTTY; bool errors = false; - bool silent = false; + bool verbose = true; uint32_t samples = 0; uint8_t cmdp = 0; while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { switch (tolower(param_getchar(Cmd, cmdp))) { case 'h': return usage_lf_read(); - case 's': - silent = true; - cmdp++; - break; case 'd': samples = param_get32ex(Cmd, cmdp + 1, 0, 10); cmdp += 2; break; + case 's': + verbose = false; + cmdp++; + break; default: PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); errors = true; @@ -614,7 +614,7 @@ int CmdLFRead(const char *Cmd) { //Validations if (errors) return usage_lf_read(); - return lf_read(silent, samples); + return lf_read(verbose, samples); } int CmdLFSniff(const char *Cmd) { @@ -627,7 +627,7 @@ int CmdLFSniff(const char *Cmd) { clearCommandBuffer(); SendCommandNG(CMD_LF_SNIFF_RAW_ADC, NULL, 0); WaitForResponse(CMD_ACK, NULL); - getSamples(0, false); + getSamples(0, true); return PM3_SUCCESS; } @@ -1183,7 +1183,7 @@ int CmdLFfind(const char *Cmd) { bool isOnline = (session.pm3_present && (cmdp != '1')); if (isOnline) - lf_read(true, 30000); + lf_read(false, 30000); if (GraphTraceLen < minLength) { PrintAndLogEx(FAILED, "Data in Graphbuffer was too small."); diff --git a/client/cmdlf.h b/client/cmdlf.h index 3f0670d1f..7393405ba 100644 --- a/client/cmdlf.h +++ b/client/cmdlf.h @@ -32,7 +32,7 @@ int CmdLFSniff(const char *Cmd); int CmdVchDemod(const char *Cmd); int CmdLFfind(const char *Cmd); -int lf_read(bool silent, uint32_t samples); +int lf_read(bool verbose, uint32_t samples); int lf_config(sample_config *config); #endif diff --git a/client/cmdlfcotag.c b/client/cmdlfcotag.c index 9c55bf453..abe78a4ea 100644 --- a/client/cmdlfcotag.c +++ b/client/cmdlfcotag.c @@ -96,7 +96,7 @@ static int CmdCOTAGRead(const char *Cmd) { case 2: { CmdPlot(""); CmdGrid("384"); - getSamples(0, true); + getSamples(0, false); break; } case 1: { diff --git a/client/cmdlft55xx.c b/client/cmdlft55xx.c index d992b6d58..f25626d94 100644 --- a/client/cmdlft55xx.c +++ b/client/cmdlft55xx.c @@ -2528,7 +2528,7 @@ bool AcquireData(uint8_t page, uint8_t block, bool pwdmode, uint32_t password, u return false; } - getSamples(12000, true); + getSamples(12000, false); return !getSignalProperties()->isnoise; } @@ -3485,7 +3485,7 @@ static int CmdT55xxDetectPage1(const char *Cmd) { found = AcquireData(T55x7_PAGE1, T55x7_TRACE_BLOCK1, usepwd, password, dl_mode); if (found == false) continue; - + if (tryDetectP1(false)) { found = true; found_mode = dl_mode;