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 //-----------------------------------------------------------------------------
9 #include "lfsampling.h"
12 Default LF config is set to:
13 decimation = 1 (we keep 1 out of 1 samples)
19 sample_config config
= { 1, 8, 1, 95, 0 } ;
22 Dbprintf("LF Sampling config: ");
23 Dbprintf(" [q] divisor: %d (%d KHz)", config
.divisor
, 12000 / (config
.divisor
+1));
24 Dbprintf(" [b] bps: %d ", config
.bits_per_sample
);
25 Dbprintf(" [d] decimation: %d ", config
.decimation
);
26 Dbprintf(" [a] averaging: %s ", (config
.averaging
) ? "Yes" : "No");
27 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
) {
42 if(sc
->divisor
!= 0) config
.divisor
= sc
->divisor
;
43 if(sc
->bits_per_sample
!= 0) config
.bits_per_sample
= sc
->bits_per_sample
;
44 if(sc
->trigger_threshold
!= -1) config
.trigger_threshold
= sc
->trigger_threshold
;
46 config
.decimation
= (sc
->decimation
!= 0) ? sc
->decimation
: 1;
47 config
.averaging
= sc
->averaging
;
48 if(config
.bits_per_sample
> 8) config
.bits_per_sample
= 8;
53 sample_config
* getSamplingConfig() {
64 * @brief Pushes bit onto the stream
68 void pushBit( BitstreamOut
* stream
, uint8_t bit
) {
69 int bytepos
= stream
->position
>> 3; // divide by 8
70 int bitpos
= stream
->position
& 7;
71 *(stream
->buffer
+bytepos
) |= (bit
> 0) << (7 - bitpos
);
77 * Setup the FPGA to listen for samples. This method downloads the FPGA bitstream
78 * if not already loaded, sets divisor and starts up the antenna.
79 * @param divisor : 1, 88> 255 or negative ==> 134.8 KHz
83 void LFSetupFPGAForADC(int divisor
, bool lf_field
) {
84 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
85 if ( (divisor
== 1) || (divisor
< 0) || (divisor
> 255) )
86 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
87 else if (divisor
== 0)
88 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
90 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, divisor
);
92 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| (lf_field
? FPGA_LF_ADC_READER_FIELD
: 0));
94 // Connect the A/D to the peak-detected low-frequency path.
95 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
96 // 50ms for the resonant antenna to settle.
98 // Now set up the SSC to get the ADC samples that are now streaming at us.
100 // start a 1.5ticks is 1us
105 * Does the sample acquisition. If threshold is specified, the actual sampling
106 * is not commenced until the threshold has been reached.
107 * This method implements decimation and quantization in order to
108 * be able to provide longer sample traces.
109 * Uses the following global settings:
110 * @param decimation - how much should the signal be decimated. A decimation of N means we keep 1 in N samples, etc.
111 * @param bits_per_sample - bits per sample. Max 8, min 1 bit per sample.
112 * @param averaging If set to true, decimation will use averaging, so that if e.g. decimation is 3, the sample
113 * value that will be used is the average value of the three samples.
114 * @param trigger_threshold - a threshold. The sampling won't commence until this threshold has been reached. Set
115 * to -1 to ignore threshold.
116 * @param silent - is true, now outputs are made. If false, dbprints the status
117 * @return the number of bits occupied by the samples.
119 uint32_t DoAcquisition(uint8_t decimation
, uint32_t bits_per_sample
, bool averaging
, int trigger_threshold
, bool silent
, int bufsize
) {
120 //bigbuf, to hold the aquired raw data signal
121 uint8_t *dest
= BigBuf_get_addr();
122 bufsize
= (bufsize
> 0 && bufsize
< BigBuf_max_traceLen()) ? bufsize
: BigBuf_max_traceLen();
124 //BigBuf_Clear_ext(false); //creates issues with cmdread (marshmellow)
126 if(bits_per_sample
< 1) bits_per_sample
= 1;
127 if(bits_per_sample
> 8) bits_per_sample
= 8;
129 if(decimation
< 1) decimation
= 1;
131 // Use a bit stream to handle the output
132 BitstreamOut data
= { dest
, 0, 0};
133 int sample_counter
= 0;
135 //If we want to do averaging
136 uint32_t sample_sum
=0 ;
137 uint32_t sample_total_numbers
=0 ;
138 uint32_t sample_total_saved
=0 ;
140 while(!BUTTON_PRESS() && !usb_poll_validate_length() ) {
142 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
143 AT91C_BASE_SSC
->SSC_THR
= 0x43;
146 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
147 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
149 // threshold either high or low values 128 = center 0. if trigger = 178
150 if ((trigger_threshold
> 0) && (sample
< (trigger_threshold
+128)) && (sample
> (128-trigger_threshold
))) //
153 trigger_threshold
= 0;
154 sample_total_numbers
++;
158 sample_sum
+= sample
;
164 if(sample_counter
< decimation
) continue;
168 if(averaging
&& decimation
> 1) {
169 sample
= sample_sum
/ decimation
;
173 sample_total_saved
++;
174 if(bits_per_sample
== 8){
175 dest
[sample_total_saved
-1] = sample
;
176 data
.numbits
= sample_total_saved
<< 3;//Get the return value correct
177 if(sample_total_saved
>= bufsize
) break;
180 pushBit(&data
, sample
& 0x80);
181 if(bits_per_sample
> 1) pushBit(&data
, sample
& 0x40);
182 if(bits_per_sample
> 2) pushBit(&data
, sample
& 0x20);
183 if(bits_per_sample
> 3) pushBit(&data
, sample
& 0x10);
184 if(bits_per_sample
> 4) pushBit(&data
, sample
& 0x08);
185 if(bits_per_sample
> 5) pushBit(&data
, sample
& 0x04);
186 if(bits_per_sample
> 6) pushBit(&data
, sample
& 0x02);
187 //Not needed, 8bps is covered above
188 //if(bits_per_sample > 7) pushBit(&data, sample & 0x01);
189 if((data
.numbits
>> 3) +1 >= bufsize
) break;
196 Dbprintf("Done, saved %d out of %d seen samples at %d bits/sample",sample_total_saved
, sample_total_numbers
,bits_per_sample
);
197 Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...",
198 dest
[0], dest
[1], dest
[2], dest
[3], dest
[4], dest
[5], dest
[6], dest
[7]);
203 * @brief Does sample acquisition, ignoring the config values set in the sample_config.
204 * This method is typically used by tag-specific readers who just wants to read the samples
206 * @param trigger_threshold
208 * @return number of bits sampled
210 uint32_t DoAcquisition_default(int trigger_threshold
, bool silent
) {
211 return DoAcquisition(1, 8, 0,trigger_threshold
, silent
, 0);
213 uint32_t DoAcquisition_config( bool silent
) {
214 return DoAcquisition(config
.decimation
215 ,config
.bits_per_sample
217 ,config
.trigger_threshold
222 uint32_t DoPartialAcquisition(int trigger_threshold
, bool silent
, int sample_size
) {
223 return DoAcquisition(1, 8, 0, trigger_threshold
, silent
, sample_size
);
226 uint32_t ReadLF(bool activeField
, bool silent
) {
229 LFSetupFPGAForADC(config
.divisor
, activeField
);
230 return DoAcquisition_config(silent
);
234 * Initializes the FPGA for reader-mode (field on), and acquires the samples.
235 * @return number of bits sampled
237 uint32_t SampleLF(bool printCfg
) {
238 BigBuf_Clear_ext(false);
239 uint32_t ret
= ReadLF(true, printCfg
);
240 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
244 * Initializes the FPGA for snoop-mode (field off), and acquires the samples.
245 * @return number of bits sampled
248 BigBuf_Clear_ext(false);
249 uint32_t ret
= ReadLF(false, true);
250 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
255 * acquisition of T55x7 LF signal. Similart to other LF, but adjusted with @marshmellows thresholds
256 * the data is collected in BigBuf.
258 void doT55x7Acquisition(size_t sample_size
) {
260 #define T55xx_READ_UPPER_THRESHOLD 128+60 // 60 grph
261 #define T55xx_READ_LOWER_THRESHOLD 128-60 // -60 grph
262 #define T55xx_READ_TOL 5
264 uint8_t *dest
= BigBuf_get_addr();
265 uint16_t bufsize
= BigBuf_max_traceLen();
267 if ( bufsize
> sample_size
)
268 bufsize
= sample_size
;
270 uint8_t curSample
= 0, lastSample
= 0;
271 uint16_t i
= 0, skipCnt
= 0;
272 bool startFound
= false;
273 bool highFound
= false;
274 bool lowFound
= false;
276 while(!BUTTON_PRESS() && !usb_poll_validate_length() && skipCnt
< 1000 && (i
< bufsize
) ) {
278 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
279 AT91C_BASE_SSC
->SSC_THR
= 0x43; //43
282 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
283 curSample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
286 // skip until the first high sample above threshold
287 if (!startFound
&& curSample
> T55xx_READ_UPPER_THRESHOLD
) {
288 //if (curSample > lastSample)
289 // lastSample = curSample;
291 } else if (!highFound
) {
295 // skip until the first low sample below threshold
296 if (!startFound
&& curSample
< T55xx_READ_LOWER_THRESHOLD
) {
297 //if (curSample > lastSample)
298 lastSample
= curSample
;
300 } else if (!lowFound
) {
305 // skip until first high samples begin to change
306 if (startFound
|| curSample
> T55xx_READ_LOWER_THRESHOLD
+ T55xx_READ_TOL
){
307 // if just found start - recover last sample
309 dest
[i
++] = lastSample
;
313 dest
[i
++] = curSample
;
319 * acquisition of Cotag LF signal. Similart to other LF, since the Cotag has such long datarate RF/384
320 * and is Manchester?, we directly gather the manchester data into bigbuff
324 #define COTAG_T2 (COTAG_T1>>1)
325 #define COTAG_ONE_THRESHOLD 128+30
326 #define COTAG_ZERO_THRESHOLD 128-30
328 #define COTAG_BITS 264
330 void doCotagAcquisition(size_t sample_size
) {
332 uint8_t *dest
= BigBuf_get_addr();
333 uint16_t bufsize
= BigBuf_max_traceLen();
335 if ( bufsize
> sample_size
)
336 bufsize
= sample_size
;
339 uint8_t sample
= 0, firsthigh
= 0, firstlow
= 0;
342 while (!BUTTON_PRESS() && !usb_poll_validate_length() && (i
< bufsize
) ) {
344 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
345 AT91C_BASE_SSC
->SSC_THR
= 0x43;
349 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
350 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
355 if (sample
< COTAG_ONE_THRESHOLD
)
360 if (sample
> COTAG_ZERO_THRESHOLD
)
367 if ( sample
> COTAG_ONE_THRESHOLD
)
369 else if ( sample
< COTAG_ZERO_THRESHOLD
)
377 uint32_t doCotagAcquisitionManchester() {
379 uint8_t *dest
= BigBuf_get_addr();
380 uint16_t bufsize
= BigBuf_max_traceLen();
382 if ( bufsize
> COTAG_BITS
)
383 bufsize
= COTAG_BITS
;
386 uint8_t sample
= 0, firsthigh
= 0, firstlow
= 0;
387 uint16_t sample_counter
= 0, period
= 0;
388 uint8_t curr
= 0, prev
= 0;
390 while (!BUTTON_PRESS() && !usb_poll_validate_length() && (sample_counter
< bufsize
) ) {
392 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
393 AT91C_BASE_SSC
->SSC_THR
= 0x43;
397 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
398 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
403 if (sample
< COTAG_ONE_THRESHOLD
)
409 if (sample
> COTAG_ZERO_THRESHOLD
)
414 // set sample 255, 0, or previous
415 if ( sample
> COTAG_ONE_THRESHOLD
){
419 else if ( sample
< COTAG_ZERO_THRESHOLD
) {
433 dest
[sample_counter
] = curr
;
438 return sample_counter
;