]>
Commit | Line | Data |
---|---|---|
31abe49f MHS |
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 | |
4 | // the license. | |
5 | //----------------------------------------------------------------------------- | |
6 | // Miscellaneous routines for low frequency sampling. | |
7 | //----------------------------------------------------------------------------- | |
8 | ||
9 | #include "proxmark3.h" | |
10 | #include "apps.h" | |
11 | #include "util.h" | |
12 | #include "string.h" | |
13 | ||
14 | #include "lfsampling.h" | |
15 | ||
16 | sample_config config = { 1, 8, 1, 88, 0 } ; | |
17 | ||
18 | void printConfig() | |
19 | { | |
20 | Dbprintf("Sampling config: "); | |
21 | Dbprintf(" [q] divisor: %d ", config.divisor); | |
22 | Dbprintf(" [b] bps: %d ", config.bits_per_sample); | |
23 | Dbprintf(" [d] decimation: %d ", config.decimation); | |
24 | Dbprintf(" [a] averaging: %d ", config.averaging); | |
25 | Dbprintf(" [t] trigger threshold: %d ", config.trigger_threshold); | |
26 | } | |
27 | ||
28 | ||
29 | /** | |
30 | * Called from the USB-handler to set the sampling configuration | |
31 | * The sampling config is used for std reading and snooping. | |
32 | * | |
33 | * Other functions may read samples and ignore the sampling config, | |
34 | * such as functions to read the UID from a prox tag or similar. | |
35 | * | |
36 | * Values set to '0' implies no change (except for averaging) | |
37 | * @brief setSamplingConfig | |
38 | * @param sc | |
39 | */ | |
40 | void setSamplingConfig(sample_config *sc) | |
41 | { | |
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->decimation!= 0) config.decimation= sc->decimation; | |
45 | if(sc->trigger_threshold != -1) config.trigger_threshold= sc->trigger_threshold; | |
46 | ||
47 | config.averaging= sc->averaging; | |
48 | if(config.bits_per_sample > 8) config.bits_per_sample = 8; | |
49 | if(config.decimation < 1) config.decimation = 1; | |
50 | ||
51 | printConfig(); | |
52 | } | |
53 | ||
54 | sample_config* getSamplingConfig() | |
55 | { | |
56 | return &config; | |
57 | } | |
58 | ||
59 | typedef struct { | |
60 | uint8_t * buffer; | |
61 | uint32_t numbits; | |
62 | uint32_t position; | |
63 | } BitstreamOut; | |
64 | ||
65 | ||
66 | /** | |
67 | * @brief Pushes bit onto the stream | |
68 | * @param stream | |
69 | * @param bit | |
70 | */ | |
71 | void pushBit( BitstreamOut* stream, uint8_t bit) | |
72 | { | |
73 | int bytepos = stream->position >> 3; // divide by 8 | |
74 | int bitpos = stream->position & 7; | |
75 | *(stream->buffer+bytepos) |= (bit > 0) << (7 - bitpos); | |
76 | stream->position++; | |
77 | stream->numbits++; | |
78 | } | |
79 | ||
80 | /** | |
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 | |
84 | * 0 or 95 ==> 125 KHz | |
85 | * | |
86 | **/ | |
87 | void LFSetupFPGAForADC(int divisor, bool lf_field) | |
88 | { | |
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 | |
94 | else | |
95 | FpgaSendCommand(FPGA_CMD_SET_DIVISOR, divisor); | |
96 | ||
97 | FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | (lf_field ? FPGA_LF_ADC_READER_FIELD : 0)); | |
98 | ||
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. | |
102 | SpinDelay(50); | |
103 | // Now set up the SSC to get the ADC samples that are now streaming at us. | |
104 | FpgaSetupSsc(); | |
105 | } | |
106 | ||
107 | ||
108 | /** | |
109 | * Does the sample acquisition. If threshold is specified, the actual sampling | |
110 | * is not commenced until the threshold has been reached. | |
111 | * This method implements decimation and quantization in order to | |
112 | * be able to provide longer sample traces. | |
113 | * Uses the following global settings: | |
114 | * @param decimation - how much should the signal be decimated. A decimation of N means we keep 1 in N samples, etc. | |
115 | * @param bits_per_sample - bits per sample. Max 8, min 1 bit per sample. | |
116 | * @param averaging If set to true, decimation will use averaging, so that if e.g. decimation is 3, the sample | |
117 | * value that will be used is the average value of the three samples. | |
118 | * @param trigger_threshold - a threshold. The sampling won't commence until this threshold has been reached. Set | |
119 | * to -1 to ignore threshold. | |
120 | * @param silent - is true, now outputs are made. If false, dbprints the status | |
121 | * @return the number of bits occupied by the samples. | |
122 | */ | |
123 | ||
124 | uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool averaging, int trigger_threshold,bool silent) | |
125 | { | |
126 | //. | |
0644d5e3 MHS |
127 | uint8_t *dest = BigBuf_get_addr(); |
128 | int bufsize = BigBuf_max_traceLen(); | |
129 | ||
31abe49f MHS |
130 | memset(dest, 0, bufsize); |
131 | ||
132 | if(bits_per_sample < 1) bits_per_sample = 1; | |
133 | if(bits_per_sample > 8) bits_per_sample = 8; | |
134 | ||
135 | if(decimation < 1) decimation = 1; | |
136 | ||
137 | // Use a bit stream to handle the output | |
138 | BitstreamOut data = { dest , 0, 0}; | |
139 | int sample_counter = 0; | |
140 | uint8_t sample = 0; | |
141 | //If we want to do averaging | |
142 | uint32_t sample_sum =0 ; | |
143 | uint32_t sample_total_numbers =0 ; | |
144 | uint32_t sample_total_saved =0 ; | |
145 | ||
146 | while(!BUTTON_PRESS()) { | |
147 | WDT_HIT(); | |
148 | if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) { | |
149 | AT91C_BASE_SSC->SSC_THR = 0x43; | |
150 | LED_D_ON(); | |
151 | } | |
152 | if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { | |
153 | sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; | |
154 | LED_D_OFF(); | |
155 | if (trigger_threshold > 0 && sample < trigger_threshold) | |
156 | continue; | |
157 | ||
158 | trigger_threshold = 0; | |
159 | sample_total_numbers++; | |
160 | ||
161 | if(averaging) | |
162 | { | |
163 | sample_sum += sample; | |
164 | } | |
165 | //Check decimation | |
166 | if(decimation > 1) | |
167 | { | |
168 | sample_counter++; | |
169 | if(sample_counter < decimation) continue; | |
170 | sample_counter = 0; | |
171 | } | |
172 | //Averaging | |
173 | if(averaging && decimation > 1) { | |
174 | sample = sample_sum / decimation; | |
175 | sample_sum =0; | |
176 | } | |
177 | //Store the sample | |
178 | sample_total_saved ++; | |
179 | if(bits_per_sample == 8){ | |
180 | dest[sample_total_saved-1] = sample; | |
181 | data.numbits = sample_total_saved << 3;//Get the return value correct | |
182 | if(sample_total_saved >= bufsize) break; | |
183 | } | |
184 | else{ | |
185 | pushBit(&data, sample & 0x80); | |
186 | if(bits_per_sample > 1) pushBit(&data, sample & 0x40); | |
187 | if(bits_per_sample > 2) pushBit(&data, sample & 0x20); | |
188 | if(bits_per_sample > 3) pushBit(&data, sample & 0x10); | |
189 | if(bits_per_sample > 4) pushBit(&data, sample & 0x08); | |
190 | if(bits_per_sample > 5) pushBit(&data, sample & 0x04); | |
191 | if(bits_per_sample > 6) pushBit(&data, sample & 0x02); | |
192 | //Not needed, 8bps is covered above | |
193 | //if(bits_per_sample > 7) pushBit(&data, sample & 0x01); | |
194 | if((data.numbits >> 3) +1 >= bufsize) break; | |
195 | } | |
196 | } | |
197 | } | |
198 | ||
199 | if(!silent) | |
200 | { | |
201 | Dbprintf("Done, saved %d out of %d seen samples at %d bits/sample",sample_total_saved, sample_total_numbers,bits_per_sample); | |
202 | Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...", | |
203 | dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]); | |
204 | } | |
205 | return data.numbits; | |
206 | } | |
207 | /** | |
208 | * @brief Does sample acquisition, ignoring the config values set in the sample_config. | |
209 | * This method is typically used by tag-specific readers who just wants to read the samples | |
210 | * the normal way | |
211 | * @param trigger_threshold | |
212 | * @param silent | |
213 | * @return number of bits sampled | |
214 | */ | |
215 | uint32_t DoAcquisition_default(int trigger_threshold, bool silent) | |
216 | { | |
217 | return DoAcquisition(1,8,0,trigger_threshold,silent); | |
218 | } | |
219 | uint32_t DoAcquisition_config( bool silent) | |
220 | { | |
221 | return DoAcquisition(config.decimation | |
222 | ,config.bits_per_sample | |
223 | ,config.averaging | |
224 | ,config.trigger_threshold | |
225 | ,silent); | |
226 | } | |
227 | ||
228 | uint32_t ReadLF(bool activeField) | |
229 | { | |
230 | printConfig(); | |
231 | LFSetupFPGAForADC(config.divisor, activeField); | |
232 | // Now call the acquisition routine | |
233 | return DoAcquisition_config(false); | |
234 | } | |
235 | ||
236 | /** | |
237 | * Initializes the FPGA for reader-mode (field on), and acquires the samples. | |
238 | * @return number of bits sampled | |
239 | **/ | |
240 | uint32_t SampleLF() | |
241 | { | |
242 | return ReadLF(true); | |
243 | } | |
244 | /** | |
245 | * Initializes the FPGA for snoop-mode (field off), and acquires the samples. | |
246 | * @return number of bits sampled | |
247 | **/ | |
248 | ||
249 | uint32_t SnoopLF() | |
250 | { | |
251 | return ReadLF(false); | |
252 | } |