1 //-----------------------------------------------------------------------------
2 // The main application code. This is the first thing called after start.c
4 // Jonathan Westhues, Mar 2006
5 // Edits by Gerhard de Koning Gans, Sep 2007 (##)
6 //-----------------------------------------------------------------------------
17 // The large multi-purpose buffer, typically used to hold A/D samples,
18 // maybe pre-processed in some way.
22 //=============================================================================
23 // A buffer where we can queue things up to be sent through the FPGA, for
24 // any purpose (fake tag, as reader, whatever). We go MSB first, since that
25 // is the order in which they go out on the wire.
26 //=============================================================================
33 void BufferClear(void)
35 memset(BigBuf
,0,sizeof(BigBuf
));
36 DbpString("Buffer cleared");
39 void ToSendReset(void)
45 void ToSendStuffBit(int b
)
49 ToSend
[ToSendMax
] = 0;
54 ToSend
[ToSendMax
] |= (1 << (7 - ToSendBit
));
59 if(ToSendBit
>= sizeof(ToSend
)) {
61 DbpString("ToSendStuffBit overflowed!");
65 //=============================================================================
66 // Debug print functions, to go out over USB, to the usual PC-side client.
67 //=============================================================================
69 void DbpString(char *str
)
71 /* this holds up stuff unless we're connected to usb */
76 c
.cmd
= CMD_DEBUG_PRINT_STRING
;
78 memcpy(c
.d
.asBytes
, str
, c
.ext1
);
80 UsbSendPacket((BYTE
*)&c
, sizeof(c
));
81 // TODO fix USB so stupid things like this aren't req'd
85 void DbpIntegers(int x1
, int x2
, int x3
)
87 /* this holds up stuff unless we're connected to usb */
92 c
.cmd
= CMD_DEBUG_PRINT_INTEGERS
;
97 UsbSendPacket((BYTE
*)&c
, sizeof(c
));
102 void AcquireRawAdcSamples125k(BOOL at134khz
)
105 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
106 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
108 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
109 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
112 // Connect the A/D to the peak-detected low-frequency path.
113 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
115 // Give it a bit of time for the resonant antenna to settle.
118 // Now set up the SSC to get the ADC samples that are now streaming at us.
121 // Now call the acquisition routine
122 DoAcquisition125k(at134khz
);
125 // split into two routines so we can avoid timing issues after sending commands //
126 void DoAcquisition125k(BOOL at134khz
)
128 BYTE
*dest
= (BYTE
*)BigBuf
;
129 int n
= sizeof(BigBuf
);
135 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
136 SSC_TRANSMIT_HOLDING
= 0x43;
139 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
140 dest
[i
] = (BYTE
)SSC_RECEIVE_HOLDING
;
148 DbpIntegers(dest
[0], dest
[1], at134khz
);
151 void ModThenAcquireRawAdcSamples125k(int delay_off
,int period_0
,int period_1
,BYTE
*command
)
155 // see if 'h' was specified
156 if(command
[strlen((char *) command
) - 1] == 'h')
162 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
163 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
165 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
166 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
169 // Give it a bit of time for the resonant antenna to settle.
172 // Now set up the SSC to get the ADC samples that are now streaming at us.
175 // now modulate the reader field
176 while(*command
!= '\0' && *command
!= ' ')
178 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
180 SpinDelayUs(delay_off
);
182 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
183 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
185 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
186 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
189 if(*(command
++) == '0')
190 SpinDelayUs(period_0
);
192 SpinDelayUs(period_1
);
194 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
196 SpinDelayUs(delay_off
);
198 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
199 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
201 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
202 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
206 DoAcquisition125k(at134khz
);
209 void AcquireTiType(void)
215 memset(BigBuf
,0,sizeof(BigBuf
));
217 // Set up the synchronous serial port
218 PIO_DISABLE
= (1<<GPIO_SSC_DIN
);
219 PIO_PERIPHERAL_A_SEL
= (1<<GPIO_SSC_DIN
);
221 // steal this pin from the SSP and use it to control the modulation
222 PIO_ENABLE
= (1<<GPIO_SSC_DOUT
);
223 PIO_OUTPUT_ENABLE
= (1<<GPIO_SSC_DOUT
);
225 SSC_CONTROL
= SSC_CONTROL_RESET
;
226 SSC_CONTROL
= SSC_CONTROL_RX_ENABLE
| SSC_CONTROL_TX_ENABLE
;
228 // Sample at 2 Mbit/s, so TI tags are 16.2 vs. 14.9 clocks long
229 // 48/2 = 24 MHz clock must be divided by 12
230 SSC_CLOCK_DIVISOR
= 12;
232 SSC_RECEIVE_CLOCK_MODE
= SSC_CLOCK_MODE_SELECT(0);
233 SSC_RECEIVE_FRAME_MODE
= SSC_FRAME_MODE_BITS_IN_WORD(32) | SSC_FRAME_MODE_MSB_FIRST
;
234 SSC_TRANSMIT_CLOCK_MODE
= 0;
235 SSC_TRANSMIT_FRAME_MODE
= 0;
240 PIO_OUTPUT_DATA_SET
= (1<<GPIO_SSC_DOUT
);
242 // Charge TI tag for 50ms.
245 // stop modulating antenna and listen
246 PIO_OUTPUT_DATA_CLEAR
= (1<<GPIO_SSC_DOUT
);
252 if(SSC_STATUS
& SSC_STATUS_RX_READY
) {
253 BigBuf
[i
] = SSC_RECEIVE_HOLDING
; // store 32 bit values in buffer
254 i
++; if(i
>= n
) return;
259 // return stolen pin ro SSP
260 PIO_DISABLE
= (1<<GPIO_SSC_DOUT
);
261 PIO_PERIPHERAL_A_SEL
= (1<<GPIO_SSC_DIN
) | (1<<GPIO_SSC_DOUT
);
264 void AcquireRawBitsTI(void)
267 // TI tags charge at 134.2Khz
268 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
269 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
270 // connects to SSP_DIN and the SSP_DOUT logic level controls
271 // whether we're modulating the antenna (high)
272 // or listening to the antenna (low)
273 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
275 // get TI tag data into the buffer
278 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
281 //-----------------------------------------------------------------------------
282 // Read an ADC channel and block till it completes, then return the result
283 // in ADC units (0 to 1023). Also a routine to average 32 samples and
285 //-----------------------------------------------------------------------------
286 static int ReadAdc(int ch
)
290 ADC_CONTROL
= ADC_CONTROL_RESET
;
291 ADC_MODE
= ADC_MODE_PRESCALE(32) | ADC_MODE_STARTUP_TIME(16) |
292 ADC_MODE_SAMPLE_HOLD_TIME(8);
293 ADC_CHANNEL_ENABLE
= ADC_CHANNEL(ch
);
295 ADC_CONTROL
= ADC_CONTROL_START
;
296 while(!(ADC_STATUS
& ADC_END_OF_CONVERSION(ch
)))
298 d
= ADC_CHANNEL_DATA(ch
);
303 static int AvgAdc(int ch
)
308 for(i
= 0; i
< 32; i
++) {
312 return (a
+ 15) >> 5;
315 void MeasureAntennaTuning(void)
317 BYTE
*dest
= (BYTE
*)BigBuf
;
318 int i
, ptr
= 0, adcval
= 0, peak
= 0, peakv
= 0, peakf
= 0;;
319 int vLf125
= 0, vLf134
= 0, vHf
= 0; // in mV
323 DbpString("Measuring antenna characteristics, please wait.");
324 memset(BigBuf
,0,sizeof(BigBuf
));
327 * Sweeps the useful LF range of the proxmark from
328 * 46.8kHz (divisor=255) to 600kHz (divisor=19) and
329 * read the voltage in the antenna, the result left
330 * in the buffer is a graph which should clearly show
331 * the resonating frequency of your LF antenna
332 * ( hopefully around 95 if it is tuned to 125kHz!)
334 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
335 for (i
=255; i
>19; i
--) {
336 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, i
);
338 // Vref = 3.3V, and a 10000:240 voltage divider on the input
339 // can measure voltages up to 137500 mV
340 adcval
= ((137500 * AvgAdc(ADC_CHAN_LF
)) >> 10);
341 if (i
==95) vLf125
= adcval
; // voltage at 125Khz
342 if (i
==89) vLf134
= adcval
; // voltage at 134Khz
344 dest
[i
] = adcval
>>8; // scale int to fit in byte for graphing purposes
353 // Let the FPGA drive the high-frequency antenna around 13.56 MHz.
354 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
356 // Vref = 3300mV, and an 10:1 voltage divider on the input
357 // can measure voltages up to 33000 mV
358 vHf
= (33000 * AvgAdc(ADC_CHAN_HF
)) >> 10;
360 c
.cmd
= CMD_MEASURED_ANTENNA_TUNING
;
361 c
.ext1
= (vLf125
<< 0) | (vLf134
<< 16);
363 c
.ext3
= peakf
| (peakv
<< 16);
364 UsbSendPacket((BYTE
*)&c
, sizeof(c
));
367 void SimulateTagLowFrequency(int period
, int ledcontrol
)
370 BYTE
*tab
= (BYTE
*)BigBuf
;
372 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_SIMULATOR
);
374 PIO_ENABLE
= (1 << GPIO_SSC_DOUT
) | (1 << GPIO_SSC_CLK
);
376 PIO_OUTPUT_ENABLE
= (1 << GPIO_SSC_DOUT
);
377 PIO_OUTPUT_DISABLE
= (1 << GPIO_SSC_CLK
);
379 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
380 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
384 while(!(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
))) {
386 DbpString("Stopped");
403 while(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
)) {
405 DbpString("Stopped");
412 if(i
== period
) i
= 0;
416 // compose fc/8 fc/10 waveform
417 static void fc(int c
, int *n
) {
418 BYTE
*dest
= (BYTE
*)BigBuf
;
421 // for when we want an fc8 pattern every 4 logical bits
432 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
434 for (idx
=0; idx
<6; idx
++) {
446 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
448 for (idx
=0; idx
<5; idx
++) {
463 // prepare a waveform pattern in the buffer based on the ID given then
464 // simulate a HID tag until the button is pressed
465 static void CmdHIDsimTAG(int hi
, int lo
, int ledcontrol
)
469 HID tag bitstream format
470 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
471 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
472 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
473 A fc8 is inserted before every 4 bits
474 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
475 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
479 DbpString("Tags can only have 44 bits.");
483 // special start of frame marker containing invalid bit sequences
484 fc(8, &n
); fc(8, &n
); // invalid
485 fc(8, &n
); fc(10, &n
); // logical 0
486 fc(10, &n
); fc(10, &n
); // invalid
487 fc(8, &n
); fc(10, &n
); // logical 0
490 // manchester encode bits 43 to 32
491 for (i
=11; i
>=0; i
--) {
492 if ((i
%4)==3) fc(0,&n
);
494 fc(10, &n
); fc(8, &n
); // low-high transition
496 fc(8, &n
); fc(10, &n
); // high-low transition
501 // manchester encode bits 31 to 0
502 for (i
=31; i
>=0; i
--) {
503 if ((i
%4)==3) fc(0,&n
);
505 fc(10, &n
); fc(8, &n
); // low-high transition
507 fc(8, &n
); fc(10, &n
); // high-low transition
513 SimulateTagLowFrequency(n
, ledcontrol
);
519 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
520 static void CmdHIDdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
522 BYTE
*dest
= (BYTE
*)BigBuf
;
523 int m
=0, n
=0, i
=0, idx
=0, found
=0, lastval
=0;
526 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
527 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
529 // Connect the A/D to the peak-detected low-frequency path.
530 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
532 // Give it a bit of time for the resonant antenna to settle.
535 // Now set up the SSC to get the ADC samples that are now streaming at us.
543 DbpString("Stopped");
553 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
554 SSC_TRANSMIT_HOLDING
= 0x43;
558 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
559 dest
[i
] = (BYTE
)SSC_RECEIVE_HOLDING
;
560 // we don't care about actual value, only if it's more or less than a
561 // threshold essentially we capture zero crossings for later analysis
562 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
574 // sync to first lo-hi transition
575 for( idx
=1; idx
<m
; idx
++) {
576 if (dest
[idx
-1]<dest
[idx
])
582 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
583 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
584 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
585 for( i
=0; idx
<m
; idx
++) {
586 if (dest
[idx
-1]<dest
[idx
]) {
601 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
606 for( idx
=0; idx
<m
; idx
++) {
607 if (dest
[idx
]==lastval
) {
610 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
611 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
612 // swallowed up by rounding
613 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
614 // special start of frame markers use invalid manchester states (no transitions) by using sequences
617 n
=(n
+1)/6; // fc/8 in sets of 6
619 n
=(n
+1)/5; // fc/10 in sets of 5
621 switch (n
) { // stuff appropriate bits in buffer
624 dest
[i
++]=dest
[idx
-1];
627 dest
[i
++]=dest
[idx
-1];
628 dest
[i
++]=dest
[idx
-1];
630 case 3: // 3 bit start of frame markers
631 dest
[i
++]=dest
[idx
-1];
632 dest
[i
++]=dest
[idx
-1];
633 dest
[i
++]=dest
[idx
-1];
635 // When a logic 0 is immediately followed by the start of the next transmisson
636 // (special pattern) a pattern of 4 bit duration lengths is created.
638 dest
[i
++]=dest
[idx
-1];
639 dest
[i
++]=dest
[idx
-1];
640 dest
[i
++]=dest
[idx
-1];
641 dest
[i
++]=dest
[idx
-1];
643 default: // this shouldn't happen, don't stuff any bits
653 // final loop, go over previously decoded manchester data and decode into usable tag ID
654 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
655 for( idx
=0; idx
<m
-6; idx
++) {
656 // search for a start of frame marker
657 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
661 if (found
&& (hi
|lo
)) {
663 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
664 /* if we're only looking for one tag */
677 if (dest
[idx
] && (!dest
[idx
+1]) ) {
680 } else if ( (!dest
[idx
]) && dest
[idx
+1]) {
690 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
694 if (found
&& (hi
|lo
)) {
696 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
697 /* if we're only looking for one tag */
714 void SimulateTagHfListen(void)
716 BYTE
*dest
= (BYTE
*)BigBuf
;
717 int n
= sizeof(BigBuf
);
722 // We're using this mode just so that I can test it out; the simulated
723 // tag mode would work just as well and be simpler.
724 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
| FPGA_HF_READER_RX_XCORR_SNOOP
);
726 // We need to listen to the high-frequency, peak-detected path.
727 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
733 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
734 SSC_TRANSMIT_HOLDING
= 0xff;
736 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
737 BYTE r
= (BYTE
)SSC_RECEIVE_HOLDING
;
757 DbpString("simulate tag (now type bitsamples)");
760 void UsbPacketReceived(BYTE
*packet
, int len
)
762 UsbCommand
*c
= (UsbCommand
*)packet
;
765 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
:
766 AcquireRawAdcSamples125k(c
->ext1
);
769 case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
:
770 ModThenAcquireRawAdcSamples125k(c
->ext1
,c
->ext2
,c
->ext3
,c
->d
.asBytes
);
773 case CMD_ACQUIRE_RAW_BITS_TI_TYPE
:
777 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
:
778 AcquireRawAdcSamplesIso15693();
785 case CMD_READER_ISO_15693
:
786 ReaderIso15693(c
->ext1
);
789 case CMD_SIMTAG_ISO_15693
:
790 SimTagIso15693(c
->ext1
);
793 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
:
794 AcquireRawAdcSamplesIso14443(c
->ext1
);
797 case CMD_READ_SRI512_TAG
:
798 ReadSRI512Iso14443(c
->ext1
);
801 case CMD_READER_ISO_14443a
:
802 ReaderIso14443a(c
->ext1
);
805 case CMD_SNOOP_ISO_14443
:
809 case CMD_SNOOP_ISO_14443a
:
813 case CMD_SIMULATE_TAG_HF_LISTEN
:
814 SimulateTagHfListen();
817 case CMD_SIMULATE_TAG_ISO_14443
:
818 SimulateIso14443Tag();
821 case CMD_SIMULATE_TAG_ISO_14443a
:
822 SimulateIso14443aTag(c
->ext1
, c
->ext2
); // ## Simulate iso14443a tag - pass tag type & UID
825 case CMD_MEASURE_ANTENNA_TUNING
:
826 MeasureAntennaTuning();
829 case CMD_LISTEN_READER_FIELD
:
830 ListenReaderField(c
->ext1
);
833 case CMD_HID_DEMOD_FSK
:
834 CmdHIDdemodFSK(0, 0, 0, 1); // Demodulate HID tag
837 case CMD_HID_SIM_TAG
:
838 CmdHIDsimTAG(c
->ext1
, c
->ext2
, 1); // Simulate HID tag by ID
841 case CMD_FPGA_MAJOR_MODE_OFF
: // ## FPGA Control
842 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
844 LED_D_OFF(); // LED D indicates field ON or OFF
847 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
:
848 case CMD_DOWNLOAD_RAW_BITS_TI_TYPE
: {
850 if(c
->cmd
== CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
) {
851 n
.cmd
= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
;
853 n
.cmd
= CMD_DOWNLOADED_RAW_BITS_TI_TYPE
;
856 memcpy(n
.d
.asDwords
, BigBuf
+c
->ext1
, 12*sizeof(DWORD
));
857 UsbSendPacket((BYTE
*)&n
, sizeof(n
));
860 case CMD_DOWNLOADED_SIM_SAMPLES_125K
: {
861 BYTE
*b
= (BYTE
*)BigBuf
;
862 memcpy(b
+c
->ext1
, c
->d
.asBytes
, 48);
865 case CMD_SIMULATE_TAG_125K
:
867 SimulateTagLowFrequency(c
->ext1
, 1);
878 case CMD_SET_LF_DIVISOR
:
879 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, c
->ext1
);
886 case CMD_SETUP_WRITE
:
887 case CMD_FINISH_WRITE
:
888 case CMD_HARDWARE_RESET
:
889 USB_D_PLUS_PULLUP_OFF();
892 RSTC_CONTROL
= RST_CONTROL_KEY
| RST_CONTROL_PROCESSOR_RESET
;
894 // We're going to reset, and the bootrom will take control.
900 DbpString("unknown command");
905 void ReadMem(int addr
)
907 const DWORD
*data
= ((DWORD
*)addr
);
910 DbpString("Reading memory at address");
911 DbpIntegers(0, 0, addr
);
912 for (i
= 0; i
< 8; i
+= 2)
913 DbpIntegers(0, data
[i
], data
[i
+1]);
918 memset(BigBuf
,0,sizeof(BigBuf
));
928 // The FPGA gets its clock from us from PCK0 output, so set that up.
929 PIO_PERIPHERAL_B_SEL
= (1 << GPIO_PCK0
);
930 PIO_DISABLE
= (1 << GPIO_PCK0
);
931 PMC_SYS_CLK_ENABLE
= PMC_SYS_CLK_PROGRAMMABLE_CLK_0
;
932 // PCK0 is PLL clock / 4 = 96Mhz / 4 = 24Mhz
933 PMC_PROGRAMMABLE_CLK_0
= PMC_CLK_SELECTION_PLL_CLOCK
|
934 PMC_CLK_PRESCALE_DIV_4
;
935 PIO_OUTPUT_ENABLE
= (1 << GPIO_PCK0
);
938 SPI_CONTROL
= SPI_CONTROL_RESET
;
940 SSC_CONTROL
= SSC_CONTROL_RESET
;
942 // Load the FPGA image, which we have stored in our flash.
949 // test text on different colored backgrounds
950 LCDString(" The quick brown fox ", &FONT6x8
,1,1+8*0,WHITE
,BLACK
);
951 LCDString(" jumped over the ", &FONT6x8
,1,1+8*1,BLACK
,WHITE
);
952 LCDString(" lazy dog. ", &FONT6x8
,1,1+8*2,YELLOW
,RED
);
953 LCDString(" AaBbCcDdEeFfGgHhIiJj ", &FONT6x8
,1,1+8*3,RED
,GREEN
);
954 LCDString(" KkLlMmNnOoPpQqRrSsTt ", &FONT6x8
,1,1+8*4,MAGENTA
,BLUE
);
955 LCDString("UuVvWwXxYyZz0123456789", &FONT6x8
,1,1+8*5,BLUE
,YELLOW
);
956 LCDString("`-=[]_;',./~!@#$%^&*()", &FONT6x8
,1,1+8*6,BLACK
,CYAN
);
957 LCDString(" _+{}|:\\\"<>? ",&FONT6x8
,1,1+8*7,BLUE
,MAGENTA
);
960 LCDFill(0, 1+8* 8, 132, 8, BLACK
);
961 LCDFill(0, 1+8* 9, 132, 8, WHITE
);
962 LCDFill(0, 1+8*10, 132, 8, RED
);
963 LCDFill(0, 1+8*11, 132, 8, GREEN
);
964 LCDFill(0, 1+8*12, 132, 8, BLUE
);
965 LCDFill(0, 1+8*13, 132, 8, YELLOW
);
966 LCDFill(0, 1+8*14, 132, 8, CYAN
);
967 LCDFill(0, 1+8*15, 132, 8, MAGENTA
);
972 usbattached
= UsbPoll(FALSE
);
975 if (BUTTON_HELD(1000) > 0)
981 // samy's sniff and repeat routine
984 DbpString("Stand-alone mode! No PC necessary.");
986 // 3 possible options? no just 2 for now
989 int high
[OPTS
], low
[OPTS
];
991 // Oooh pretty -- notify user we're in elite samy mode now
993 LED(LED_ORANGE
, 200);
995 LED(LED_ORANGE
, 200);
997 LED(LED_ORANGE
, 200);
999 LED(LED_ORANGE
, 200);
1005 // Turn on selected LED
1006 LED(selected
+ 1, 0);
1010 usbattached
= UsbPoll(FALSE
);
1013 // Was our button held down or pressed?
1014 int button_pressed
= BUTTON_HELD(1000);
1017 // Button was held for a second, begin recording
1018 if (button_pressed
> 0)
1021 LED(selected
+ 1, 0);
1025 DbpString("Starting recording");
1027 // wait for button to be released
1028 while(BUTTON_PRESS())
1031 /* need this delay to prevent catching some weird data */
1034 CmdHIDdemodFSK(1, &high
[selected
], &low
[selected
], 0);
1035 DbpString("Recorded");
1036 DbpIntegers(selected
, high
[selected
], low
[selected
]);
1039 LED(selected
+ 1, 0);
1040 // Finished recording
1042 // If we were previously playing, set playing off
1043 // so next button push begins playing what we recorded
1047 // Change where to record (or begin playing)
1048 else if (button_pressed
)
1050 // Next option if we were previously playing
1052 selected
= (selected
+ 1) % OPTS
;
1056 LED(selected
+ 1, 0);
1058 // Begin transmitting
1062 DbpString("Playing");
1063 // wait for button to be released
1064 while(BUTTON_PRESS())
1066 DbpIntegers(selected
, high
[selected
], low
[selected
]);
1067 CmdHIDsimTAG(high
[selected
], low
[selected
], 0);
1068 DbpString("Done playing");
1069 if (BUTTON_HELD(1000) > 0)
1071 DbpString("Exiting");
1076 /* We pressed a button so ignore it here with a delay */
1079 // when done, we're done playing, move to next option
1080 selected
= (selected
+ 1) % OPTS
;
1083 LED(selected
+ 1, 0);
1086 while(BUTTON_PRESS())
1093 // listen for external reader
1094 void ListenReaderField(int limit
)
1096 int lf_av
, lf_av_new
, lf_baseline
= 0, lf_count
= 0;
1097 int hf_av
, hf_av_new
, hf_baseline
= 0, hf_count
= 0;
1107 lf_av
= ReadAdc(ADC_CHAN_LF
);
1109 if(limit
!= HF_ONLY
)
1111 DbpString("LF 125/134 Baseline:");
1112 DbpIntegers(lf_av
,0,0);
1116 hf_av
= ReadAdc(ADC_CHAN_HF
);
1119 if (limit
!= LF_ONLY
)
1121 DbpString("HF 13.56 Baseline:");
1122 DbpIntegers(hf_av
,0,0);
1130 DbpString("Stopped");
1138 if (limit
!= HF_ONLY
)
1140 if (abs(lf_av
- lf_baseline
) > 10)
1145 lf_av_new
= ReadAdc(ADC_CHAN_LF
);
1146 // see if there's a significant change
1147 if(abs(lf_av
- lf_av_new
) > 10)
1149 DbpString("LF 125/134 Field Change:");
1150 DbpIntegers(lf_av
,lf_av_new
,lf_count
);
1156 if (limit
!= LF_ONLY
)
1158 if (abs(hf_av
- hf_baseline
) > 10)
1163 hf_av_new
= ReadAdc(ADC_CHAN_HF
);
1164 // see if there's a significant change
1165 if(abs(hf_av
- hf_av_new
) > 10)
1167 DbpString("HF 13.56 Field Change:");
1168 DbpIntegers(hf_av
,hf_av_new
,hf_count
);