From 7d83f5de82aaf23663585e17561760507e7c15aa Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 4 Apr 2021 17:08:19 +0200 Subject: [PATCH] adaptations to lf hitag.. --- armsrc/lfadc.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- armsrc/lfadc.h | 1 + 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/armsrc/lfadc.c b/armsrc/lfadc.c index 1fb1bccde..037481ba7 100644 --- a/armsrc/lfadc.c +++ b/armsrc/lfadc.c @@ -52,9 +52,12 @@ bool lf_test_periods(size_t expected, size_t count) { ////////////////////////////////////////////////////////////////////////////// // Low frequency (LF) adc passthrough functionality ////////////////////////////////////////////////////////////////////////////// -static uint8_t previous_adc_val = 0; +static uint8_t previous_adc_val = 0; //0xFF; static uint8_t adc_avg = 0; +uint8_t get_adc_avg(void) { + return adc_avg; +} void lf_sample_mean(void) { uint8_t periods = 0; uint32_t adc_sum = 0; @@ -66,20 +69,44 @@ void lf_sample_mean(void) { } // division by 32 adc_avg = adc_sum >> 5; + previous_adc_val = adc_avg; if (DBGLEVEL >= DBG_EXTENDED) Dbprintf("LF ADC average %u", adc_avg); } static size_t lf_count_edge_periods_ex(size_t max, bool wait, bool detect_gap) { + +#define LIMIT_DEV 20 + + // timeout limit to 100 000 w/o + uint32_t timeout = 100000; size_t periods = 0; - uint8_t avg_peak = adc_avg + 3, avg_through = adc_avg - 3; + uint8_t avg_peak = adc_avg + LIMIT_DEV; + uint8_t avg_through = adc_avg - LIMIT_DEV; while (BUTTON_PRESS() == false) { + WDT_HIT(); + + timeout--; + if (timeout == 0) { + return 0; + } + + if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { + AT91C_BASE_SSC->SSC_THR = 0x00; + continue; + } + if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile uint8_t adc_val = AT91C_BASE_SSC->SSC_RHR; + periods++; + // reset timeout + timeout = 100000; + + volatile uint8_t adc_val = AT91C_BASE_SSC->SSC_RHR; + if (g_logging) logSampleSimple(adc_val); // Only test field changes if state of adc values matter @@ -94,21 +121,28 @@ static size_t lf_count_edge_periods_ex(size_t max, bool wait, bool detect_gap) { } else { // Trigger on a modulation swap by observing an edge change if (rising_edge) { + if ((previous_adc_val > avg_peak) && (adc_val <= previous_adc_val)) { rising_edge = false; return periods; } + } else { + if ((previous_adc_val < avg_through) && (adc_val >= previous_adc_val)) { rising_edge = true; return periods; } + } - } + } } + previous_adc_val = adc_val; - if (periods >= max) return 0; + if (periods >= max) { + return 0; + } } } @@ -169,10 +203,10 @@ void lf_init(bool reader, bool simulate) { FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | FPGA_LF_ADC_READER_FIELD); } else { if (simulate) -// FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT); FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC); else // Sniff + //FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC); FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_TOGGLE_MODE); } @@ -185,10 +219,10 @@ void lf_init(bool reader, bool simulate) { // When in reader mode, give the field a bit of time to settle. // 313T0 = 313 * 8us = 2504us = 2.5ms Hitag2 tags needs to be fully powered. - if (reader) { +// if (reader) { // 10 ms SpinDelay(10); - } +// } // Steal this pin from the SSP (SPI communication channel with fpga) and use it to control the modulation AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT; diff --git a/armsrc/lfadc.h b/armsrc/lfadc.h index 66c6da534..ee2ffbbab 100644 --- a/armsrc/lfadc.h +++ b/armsrc/lfadc.h @@ -17,6 +17,7 @@ extern bool g_logging; +uint8_t get_adc_avg(void); void lf_sample_mean(void); bool lf_test_periods(size_t expected, size_t count); size_t lf_count_edge_periods(size_t max);