1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
5 //-----------------------------------------------------------------------------
6 // Miscellaneous routines for low frequency sampling.
7 //-----------------------------------------------------------------------------
13 #include "lfsampling.h"
14 #include "usb_cdc.h" // for usb_poll_validate_length
15 //#include "ticks.h" // for StartTicks
17 sample_config config
= { 1, 8, 1, 95, 0 } ;
21 Dbprintf("LF Sampling config: ");
22 Dbprintf(" [q] divisor: %d ", config
.divisor
);
23 Dbprintf(" [b] bps: %d ", config
.bits_per_sample
);
24 Dbprintf(" [d] decimation: %d ", config
.decimation
);
25 Dbprintf(" [a] averaging: %d ", config
.averaging
);
26 Dbprintf(" [t] trigger threshold: %d ", config
.trigger_threshold
);
31 * Called from the USB-handler to set the sampling configuration
32 * The sampling config is used for std reading and snooping.
34 * Other functions may read samples and ignore the sampling config,
35 * such as functions to read the UID from a prox tag or similar.
37 * Values set to '0' implies no change (except for averaging)
38 * @brief setSamplingConfig
41 void setSamplingConfig(sample_config
*sc
)
43 if(sc
->divisor
!= 0) config
.divisor
= sc
->divisor
;
44 if(sc
->bits_per_sample
!= 0) config
.bits_per_sample
= sc
->bits_per_sample
;
45 if(sc
->decimation
!= 0) config
.decimation
= sc
->decimation
;
46 if(sc
->trigger_threshold
!= -1) config
.trigger_threshold
= sc
->trigger_threshold
;
48 config
.averaging
= sc
->averaging
;
49 if(config
.bits_per_sample
> 8) config
.bits_per_sample
= 8;
50 if(config
.decimation
< 1) config
.decimation
= 1;
55 sample_config
* getSamplingConfig()
67 * @brief Pushes bit onto the stream
71 void pushBit( BitstreamOut
* stream
, uint8_t bit
)
73 int bytepos
= stream
->position
>> 3; // divide by 8
74 int bitpos
= stream
->position
& 7;
75 *(stream
->buffer
+bytepos
) |= (bit
> 0) << (7 - bitpos
);
81 * Setup the FPGA to listen for samples. This method downloads the FPGA bitstream
82 * if not already loaded, sets divisor and starts up the antenna.
83 * @param divisor : 1, 88> 255 or negative ==> 134.8 KHz
87 void LFSetupFPGAForADC(int divisor
, bool lf_field
)
89 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
90 if ( (divisor
== 1) || (divisor
< 0) || (divisor
> 255) )
91 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
92 else if (divisor
== 0)
93 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
95 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, divisor
);
97 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| (lf_field
? FPGA_LF_ADC_READER_FIELD
: 0));
99 // Connect the A/D to the peak-detected low-frequency path.
100 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
101 // Give it a bit of time for the resonant antenna to settle.
103 // Now set up the SSC to get the ADC samples that are now streaming at us.
108 * Does the sample acquisition. If threshold is specified, the actual sampling
109 * is not commenced until the threshold has been reached.
110 * This method implements decimation and quantization in order to
111 * be able to provide longer sample traces.
112 * Uses the following global settings:
113 * @param decimation - how much should the signal be decimated. A decimation of N means we keep 1 in N samples, etc.
114 * @param bits_per_sample - bits per sample. Max 8, min 1 bit per sample.
115 * @param averaging If set to true, decimation will use averaging, so that if e.g. decimation is 3, the sample
116 * value that will be used is the average value of the three samples.
117 * @param trigger_threshold - a threshold. The sampling won't commence until this threshold has been reached. Set
118 * to -1 to ignore threshold.
119 * @param silent - is true, now outputs are made. If false, dbprints the status
120 * @return the number of bits occupied by the samples.
122 uint32_t DoAcquisition(uint8_t decimation
, uint32_t bits_per_sample
, bool averaging
, int trigger_threshold
, bool silent
)
125 uint8_t *dest
= BigBuf_get_addr();
126 int bufsize
= BigBuf_max_traceLen();
128 //memset(dest, 0, bufsize); //creates issues with cmdread (marshmellow)
130 if(bits_per_sample
< 1) bits_per_sample
= 1;
131 if(bits_per_sample
> 8) bits_per_sample
= 8;
133 if(decimation
< 1) decimation
= 1;
135 // Use a bit stream to handle the output
136 BitstreamOut data
= { dest
, 0, 0};
137 int sample_counter
= 0;
139 //If we want to do averaging
140 uint32_t sample_sum
=0 ;
141 uint32_t sample_total_numbers
=0 ;
142 uint32_t sample_total_saved
=0 ;
144 while(!BUTTON_PRESS() && !usb_poll_validate_length() ) {
146 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
147 AT91C_BASE_SSC
->SSC_THR
= 0x43;
150 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
151 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
153 // threshold either high or low values 128 = center 0. if trigger = 178
154 if ((trigger_threshold
> 0) && (sample
< (trigger_threshold
+128)) && (sample
> (128-trigger_threshold
))) //
157 trigger_threshold
= 0;
158 sample_total_numbers
++;
162 sample_sum
+= sample
;
168 if(sample_counter
< decimation
) continue;
172 if(averaging
&& decimation
> 1) {
173 sample
= sample_sum
/ decimation
;
177 sample_total_saved
++;
178 if(bits_per_sample
== 8){
179 dest
[sample_total_saved
-1] = sample
;
180 data
.numbits
= sample_total_saved
<< 3;//Get the return value correct
181 if(sample_total_saved
>= bufsize
) break;
184 pushBit(&data
, sample
& 0x80);
185 if(bits_per_sample
> 1) pushBit(&data
, sample
& 0x40);
186 if(bits_per_sample
> 2) pushBit(&data
, sample
& 0x20);
187 if(bits_per_sample
> 3) pushBit(&data
, sample
& 0x10);
188 if(bits_per_sample
> 4) pushBit(&data
, sample
& 0x08);
189 if(bits_per_sample
> 5) pushBit(&data
, sample
& 0x04);
190 if(bits_per_sample
> 6) pushBit(&data
, sample
& 0x02);
191 //Not needed, 8bps is covered above
192 //if(bits_per_sample > 7) pushBit(&data, sample & 0x01);
193 if((data
.numbits
>> 3) +1 >= bufsize
) break;
200 Dbprintf("Done, saved %d out of %d seen samples at %d bits/sample",sample_total_saved
, sample_total_numbers
,bits_per_sample
);
201 Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...",
202 dest
[0], dest
[1], dest
[2], dest
[3], dest
[4], dest
[5], dest
[6], dest
[7]);
207 * @brief Does sample acquisition, ignoring the config values set in the sample_config.
208 * This method is typically used by tag-specific readers who just wants to read the samples
210 * @param trigger_threshold
212 * @return number of bits sampled
214 uint32_t DoAcquisition_default(int trigger_threshold
, bool silent
)
216 return DoAcquisition(1,8,0,trigger_threshold
,silent
);
218 uint32_t DoAcquisition_config( bool silent
)
220 return DoAcquisition(config
.decimation
221 ,config
.bits_per_sample
223 ,config
.trigger_threshold
227 uint32_t ReadLF(bool activeField
, bool silent
)
229 if (!silent
) printConfig();
230 LFSetupFPGAForADC(config
.divisor
, activeField
);
231 // Now call the acquisition routine
232 return DoAcquisition_config(silent
);
236 * Initializes the FPGA for reader-mode (field on), and acquires the samples.
237 * @return number of bits sampled
239 uint32_t SampleLF(bool printCfg
)
241 uint32_t ret
= ReadLF(true, printCfg
);
242 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
246 * Initializes the FPGA for snoop-mode (field off), and acquires the samples.
247 * @return number of bits sampled
252 uint32_t ret
= ReadLF(false, true);
253 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
258 * acquisition of T55x7 LF signal. Similart to other LF, but adjusted with @marshmellows thresholds
259 * the data is collected in BigBuf.
261 void doT55x7Acquisition(size_t sample_size
) {
263 #define T55xx_READ_UPPER_THRESHOLD 128+60 // 60 grph
264 #define T55xx_READ_LOWER_THRESHOLD 128-60 // -60 grph
265 #define T55xx_READ_TOL 5
267 uint8_t *dest
= BigBuf_get_addr();
268 uint16_t bufsize
= BigBuf_max_traceLen();
270 if ( bufsize
> sample_size
)
271 bufsize
= sample_size
;
274 bool startFound
= false;
275 bool highFound
= false;
276 bool lowFound
= false;
277 uint8_t curSample
= 0;
278 uint8_t lastSample
= 0;
279 uint16_t skipCnt
= 0;
280 while(!BUTTON_PRESS() && !usb_poll_validate_length() && skipCnt
<1000 && i
<bufsize
) {
282 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
283 AT91C_BASE_SSC
->SSC_THR
= 0x43;
286 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
287 curSample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
290 // skip until the first high sample above threshold
291 if (!startFound
&& curSample
> T55xx_READ_UPPER_THRESHOLD
) {
292 //if (curSample > lastSample)
293 // lastSample = curSample;
295 } else if (!highFound
) {
299 // skip until the first Low sample below threshold
300 if (!startFound
&& curSample
< T55xx_READ_LOWER_THRESHOLD
) {
301 //if (curSample > lastSample)
302 lastSample
= curSample
;
304 } else if (!lowFound
) {
310 // skip until first high samples begin to change
311 if (startFound
|| curSample
> T55xx_READ_LOWER_THRESHOLD
+T55xx_READ_TOL
){
312 // if just found start - recover last sample
314 dest
[i
++] = lastSample
;
318 dest
[i
++] = curSample
;
325 * acquisition of Cotag LF signal. Similart to other LF, since the Cotag has such long datarate RF/384
326 * and is Manchester?, we directly gather the manchester data into bigbuff
329 #define COTAG_T2 (COTAG_T1>>1)
330 #define COTAG_ONE_THRESHOLD 128+30
331 #define COTAG_ZERO_THRESHOLD 128-30
333 #define COTAG_BITS 264
335 void doCotagAcquisition(size_t sample_size
) {
337 uint8_t *dest
= BigBuf_get_addr();
338 uint16_t bufsize
= BigBuf_max_traceLen();
340 if ( bufsize
> sample_size
)
341 bufsize
= sample_size
;
344 uint8_t sample
= 0, firsthigh
= 0, firstlow
= 0;
347 while (!BUTTON_PRESS() && !usb_poll_validate_length() && (i
< bufsize
) ) {
349 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
350 AT91C_BASE_SSC
->SSC_THR
= 0x43;
354 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
355 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
360 if (sample
< COTAG_ONE_THRESHOLD
)
365 if (sample
> COTAG_ZERO_THRESHOLD
)
372 if ( sample
> COTAG_ONE_THRESHOLD
)
374 else if ( sample
< COTAG_ZERO_THRESHOLD
)
382 uint32_t doCotagAcquisitionManchester() {
384 uint8_t *dest
= BigBuf_get_addr();
385 uint16_t bufsize
= BigBuf_max_traceLen();
387 if ( bufsize
> COTAG_BITS
)
388 bufsize
= COTAG_BITS
;
391 uint8_t sample
= 0, firsthigh
= 0, firstlow
= 0;
392 uint16_t sample_counter
= 0, period
= 0;
393 uint8_t curr
= 0, prev
= 0;
395 while (!BUTTON_PRESS() && !usb_poll_validate_length() && (sample_counter
< bufsize
) ) {
397 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
398 AT91C_BASE_SSC
->SSC_THR
= 0x43;
402 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
403 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
408 if (sample
< COTAG_ONE_THRESHOLD
)
414 if (sample
> COTAG_ZERO_THRESHOLD
)
419 // set sample 255, 0, or previous
420 if ( sample
> COTAG_ONE_THRESHOLD
){
424 else if ( sample
< COTAG_ZERO_THRESHOLD
) {
438 dest
[sample_counter
] = curr
;
443 return sample_counter
;