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
) {
120 //bigbuf, to hold the aquired raw data signal
121 uint8_t *dest
= BigBuf_get_addr();
122 uint16_t 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
);
213 uint32_t DoAcquisition_config( bool silent
) {
214 return DoAcquisition(config
.decimation
215 ,config
.bits_per_sample
217 ,config
.trigger_threshold
221 uint32_t ReadLF(bool activeField
, bool silent
) {
224 LFSetupFPGAForADC(config
.divisor
, activeField
);
225 return DoAcquisition_config(silent
);
229 * Initializes the FPGA for reader-mode (field on), and acquires the samples.
230 * @return number of bits sampled
232 uint32_t SampleLF(bool printCfg
) {
233 BigBuf_Clear_ext(false);
234 uint32_t ret
= ReadLF(true, printCfg
);
235 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
239 * Initializes the FPGA for snoop-mode (field off), and acquires the samples.
240 * @return number of bits sampled
243 BigBuf_Clear_ext(false);
244 uint32_t ret
= ReadLF(false, true);
245 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
250 * acquisition of T55x7 LF signal. Similart to other LF, but adjusted with @marshmellows thresholds
251 * the data is collected in BigBuf.
253 void doT55x7Acquisition(size_t sample_size
) {
255 #define T55xx_READ_UPPER_THRESHOLD 128+40 // 60 grph
256 #define T55xx_READ_LOWER_THRESHOLD 128-40 // -60 grph
257 #define T55xx_READ_TOL 2
259 uint8_t *dest
= BigBuf_get_addr();
260 uint16_t bufsize
= BigBuf_max_traceLen();
262 if ( bufsize
> sample_size
)
263 bufsize
= sample_size
;
265 uint8_t curSample
= 0, lastSample
= 0;
266 uint16_t i
= 0, skipCnt
= 0;
267 bool startFound
= false;
268 bool highFound
= false;
269 bool lowFound
= false;
271 while(!BUTTON_PRESS() && !usb_poll_validate_length() && skipCnt
< 1000 && (i
< bufsize
) ) {
273 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
274 AT91C_BASE_SSC
->SSC_THR
= 0x43; //43
277 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
278 curSample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
281 // skip until the first high sample above threshold
282 if (!startFound
&& curSample
> T55xx_READ_UPPER_THRESHOLD
) {
283 //if (curSample > lastSample)
284 // lastSample = curSample;
286 } else if (!highFound
) {
290 // skip until the first low sample below threshold
291 if (!startFound
&& curSample
< T55xx_READ_LOWER_THRESHOLD
) {
292 //if (curSample > lastSample)
293 lastSample
= curSample
;
295 } else if (!lowFound
) {
300 // skip until first high samples begin to change
301 if (startFound
|| curSample
> T55xx_READ_LOWER_THRESHOLD
+ T55xx_READ_TOL
){
302 // if just found start - recover last sample
304 dest
[i
++] = lastSample
;
308 dest
[i
++] = curSample
;
314 * acquisition of Cotag LF signal. Similart to other LF, since the Cotag has such long datarate RF/384
315 * and is Manchester?, we directly gather the manchester data into bigbuff
319 #define COTAG_T2 (COTAG_T1>>1)
320 #define COTAG_ONE_THRESHOLD 128+30
321 #define COTAG_ZERO_THRESHOLD 128-30
322 void doCotagAcquisition(size_t sample_size
) {
324 uint8_t *dest
= BigBuf_get_addr();
325 uint16_t bufsize
= BigBuf_max_traceLen();
327 if ( bufsize
> sample_size
)
328 bufsize
= sample_size
;
331 uint8_t sample
= 0, firsthigh
= 0, firstlow
= 0;
334 while (!BUTTON_PRESS() && !usb_poll_validate_length() && (i
< bufsize
) ) {
336 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
337 AT91C_BASE_SSC
->SSC_THR
= 0x43;
341 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
342 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
347 if (sample
< COTAG_ONE_THRESHOLD
)
352 if (sample
> COTAG_ZERO_THRESHOLD
)
359 if ( sample
> COTAG_ONE_THRESHOLD
)
361 else if ( sample
< COTAG_ZERO_THRESHOLD
)
369 uint32_t doCotagAcquisitionManchester() {
371 uint8_t *dest
= BigBuf_get_addr();
372 uint16_t bufsize
= BigBuf_max_traceLen();
378 uint8_t sample
= 0, firsthigh
= 0, firstlow
= 0;
379 uint16_t sample_counter
= 0, period
= 0;
380 uint8_t curr
= 0, prev
= 0;
382 while (!BUTTON_PRESS() && !usb_poll_validate_length() && (sample_counter
< bufsize
) ) {
384 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
385 AT91C_BASE_SSC
->SSC_THR
= 0x43;
389 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
390 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
395 if (sample
< COTAG_ONE_THRESHOLD
)
401 if (sample
> COTAG_ZERO_THRESHOLD
)
406 // set sample 255, 0, or previous
407 if ( sample
> COTAG_ONE_THRESHOLD
){
411 else if ( sample
< COTAG_ZERO_THRESHOLD
) {
425 dest
[sample_counter
] = curr
;
430 return sample_counter
;