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
| FPGA_LF_READER_USE_134_KHZ
);
108 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
109 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
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
| FPGA_LF_READER_USE_134_KHZ
);
165 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
166 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
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
| FPGA_LF_READER_USE_134_KHZ
);
185 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
186 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
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
| FPGA_LF_READER_USE_134_KHZ
);
201 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
202 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
206 DoAcquisition125k(at134khz
);
209 //-----------------------------------------------------------------------------
210 // Read an ADC channel and block till it completes, then return the result
211 // in ADC units (0 to 1023). Also a routine to average 32 samples and
213 //-----------------------------------------------------------------------------
214 static int ReadAdc(int ch
)
218 ADC_CONTROL
= ADC_CONTROL_RESET
;
219 ADC_MODE
= ADC_MODE_PRESCALE(32) | ADC_MODE_STARTUP_TIME(16) |
220 ADC_MODE_SAMPLE_HOLD_TIME(8);
221 ADC_CHANNEL_ENABLE
= ADC_CHANNEL(ch
);
223 ADC_CONTROL
= ADC_CONTROL_START
;
224 while(!(ADC_STATUS
& ADC_END_OF_CONVERSION(ch
)))
226 d
= ADC_CHANNEL_DATA(ch
);
231 static int AvgAdc(int ch
)
236 for(i
= 0; i
< 32; i
++) {
240 return (a
+ 15) >> 5;
244 * Sweeps the useful LF range of the proxmark from
245 * 46.8kHz (divisor=255) to 600kHz (divisor=19) and
246 * reads the voltage in the antenna: the result is a graph
247 * which should clearly show the resonating frequency of your
248 * LF antenna ( hopefully around 90 if it is tuned to 125kHz!)
252 BYTE
*dest
= (BYTE
*)BigBuf
;
254 int i
, peak
= 0, ptr
= 0;
258 memset(BigBuf
,0,sizeof(BigBuf
));
260 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
261 for (i
=255; i
>19; i
--) {
262 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, i
);
264 dest
[i
] = (137500 * AvgAdc(ADC_CHAN_LF
)) >> 18;
275 freq
= 12000000/(ptr
+ 1);
276 for(i
= 6; i
> 3 ; --i
) {
277 dummy
[i
]= '0' + ((int) freq
) % 10;
281 for(i
= 2; i
>= 0 ; --i
) {
282 dummy
[i
]= '0' + ((int) freq
) % 10;
285 DbpString("Antenna resonates at:");
289 void MeasureAntennaTuning(void)
291 // Impedances are Zc = 1/(j*omega*C), in ohms
292 #define LF_TUNING_CAP_Z 1273 // 1 nF @ 125 kHz
293 #define HF_TUNING_CAP_Z 235 // 50 pF @ 13.56 MHz
295 int vLf125
, vLf134
, vHf
; // in mV
299 // Let the FPGA drive the low-frequency antenna around 125 kHz.
300 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
301 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
303 vLf125
= AvgAdc(ADC_CHAN_LF
);
304 // Vref = 3.3V, and a 10000:240 voltage divider on the input
305 // can measure voltages up to 137500 mV
306 vLf125
= (137500 * vLf125
) >> 10;
308 // Let the FPGA drive the low-frequency antenna around 134 kHz.
309 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
310 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_134_KHZ
);
312 vLf134
= AvgAdc(ADC_CHAN_LF
);
313 // Vref = 3.3V, and a 10000:240 voltage divider on the input
314 // can measure voltages up to 137500 mV
315 vLf134
= (137500 * vLf134
) >> 10;
317 // Let the FPGA drive the high-frequency antenna around 13.56 MHz.
318 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
320 vHf
= AvgAdc(ADC_CHAN_HF
);
321 // Vref = 3300mV, and an 10:1 voltage divider on the input
322 // can measure voltages up to 33000 mV
323 vHf
= (33000 * vHf
) >> 10;
325 c
.cmd
= CMD_MEASURED_ANTENNA_TUNING
;
326 c
.ext1
= (vLf125
<< 0) | (vLf134
<< 16);
328 c
.ext3
= (LF_TUNING_CAP_Z
<< 0) | (HF_TUNING_CAP_Z
<< 16);
329 UsbSendPacket((BYTE
*)&c
, sizeof(c
));
332 void SimulateTagLowFrequency(int period
, int ledcontrol
)
335 BYTE
*tab
= (BYTE
*)BigBuf
;
337 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_SIMULATOR
);
339 PIO_ENABLE
= (1 << GPIO_SSC_DOUT
) | (1 << GPIO_SSC_CLK
);
341 PIO_OUTPUT_ENABLE
= (1 << GPIO_SSC_DOUT
);
342 PIO_OUTPUT_DISABLE
= (1 << GPIO_SSC_CLK
);
344 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
345 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
349 while(!(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
))) {
351 DbpString("Stopped");
368 while(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
)) {
370 DbpString("Stopped");
377 if(i
== period
) i
= 0;
381 // compose fc/8 fc/10 waveform
382 static void fc(int c
, int *n
) {
383 BYTE
*dest
= (BYTE
*)BigBuf
;
386 // for when we want an fc8 pattern every 4 logical bits
397 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
399 for (idx
=0; idx
<6; idx
++) {
411 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
413 for (idx
=0; idx
<5; idx
++) {
428 // prepare a waveform pattern in the buffer based on the ID given then
429 // simulate a HID tag until the button is pressed
430 static void CmdHIDsimTAG(int hi
, int lo
, int ledcontrol
)
434 HID tag bitstream format
435 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
436 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
437 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
438 A fc8 is inserted before every 4 bits
439 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
440 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
444 DbpString("Tags can only have 44 bits.");
448 // special start of frame marker containing invalid bit sequences
449 fc(8, &n
); fc(8, &n
); // invalid
450 fc(8, &n
); fc(10, &n
); // logical 0
451 fc(10, &n
); fc(10, &n
); // invalid
452 fc(8, &n
); fc(10, &n
); // logical 0
455 // manchester encode bits 43 to 32
456 for (i
=11; i
>=0; i
--) {
457 if ((i
%4)==3) fc(0,&n
);
459 fc(10, &n
); fc(8, &n
); // low-high transition
461 fc(8, &n
); fc(10, &n
); // high-low transition
466 // manchester encode bits 31 to 0
467 for (i
=31; i
>=0; i
--) {
468 if ((i
%4)==3) fc(0,&n
);
470 fc(10, &n
); fc(8, &n
); // low-high transition
472 fc(8, &n
); fc(10, &n
); // high-low transition
478 SimulateTagLowFrequency(n
, ledcontrol
);
484 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
485 static void CmdHIDdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
487 BYTE
*dest
= (BYTE
*)BigBuf
;
488 int m
=0, n
=0, i
=0, idx
=0, found
=0, lastval
=0;
491 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
492 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
494 // Connect the A/D to the peak-detected low-frequency path.
495 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
497 // Give it a bit of time for the resonant antenna to settle.
500 // Now set up the SSC to get the ADC samples that are now streaming at us.
508 DbpString("Stopped");
518 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
519 SSC_TRANSMIT_HOLDING
= 0x43;
523 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
524 dest
[i
] = (BYTE
)SSC_RECEIVE_HOLDING
;
525 // we don't care about actual value, only if it's more or less than a
526 // threshold essentially we capture zero crossings for later analysis
527 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
539 // sync to first lo-hi transition
540 for( idx
=1; idx
<m
; idx
++) {
541 if (dest
[idx
-1]<dest
[idx
])
547 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
548 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
549 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
550 for( i
=0; idx
<m
; idx
++) {
551 if (dest
[idx
-1]<dest
[idx
]) {
566 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
571 for( idx
=0; idx
<m
; idx
++) {
572 if (dest
[idx
]==lastval
) {
575 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
576 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
577 // swallowed up by rounding
578 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
579 // special start of frame markers use invalid manchester states (no transitions) by using sequences
582 n
=(n
+1)/6; // fc/8 in sets of 6
584 n
=(n
+1)/5; // fc/10 in sets of 5
586 switch (n
) { // stuff appropriate bits in buffer
589 dest
[i
++]=dest
[idx
-1];
592 dest
[i
++]=dest
[idx
-1];
593 dest
[i
++]=dest
[idx
-1];
595 case 3: // 3 bit start of frame markers
596 dest
[i
++]=dest
[idx
-1];
597 dest
[i
++]=dest
[idx
-1];
598 dest
[i
++]=dest
[idx
-1];
600 // When a logic 0 is immediately followed by the start of the next transmisson
601 // (special pattern) a pattern of 4 bit duration lengths is created.
603 dest
[i
++]=dest
[idx
-1];
604 dest
[i
++]=dest
[idx
-1];
605 dest
[i
++]=dest
[idx
-1];
606 dest
[i
++]=dest
[idx
-1];
608 default: // this shouldn't happen, don't stuff any bits
618 // final loop, go over previously decoded manchester data and decode into usable tag ID
619 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
620 for( idx
=0; idx
<m
-6; idx
++) {
621 // search for a start of frame marker
622 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
626 if (found
&& (hi
|lo
)) {
628 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
629 /* if we're only looking for one tag */
642 if (dest
[idx
] && (!dest
[idx
+1]) ) {
645 } else if ( (!dest
[idx
]) && dest
[idx
+1]) {
655 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
659 if (found
&& (hi
|lo
)) {
661 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
662 /* if we're only looking for one tag */
679 void SimulateTagHfListen(void)
681 BYTE
*dest
= (BYTE
*)BigBuf
;
682 int n
= sizeof(BigBuf
);
687 // We're using this mode just so that I can test it out; the simulated
688 // tag mode would work just as well and be simpler.
689 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
| FPGA_HF_READER_RX_XCORR_SNOOP
);
691 // We need to listen to the high-frequency, peak-detected path.
692 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
698 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
699 SSC_TRANSMIT_HOLDING
= 0xff;
701 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
702 BYTE r
= (BYTE
)SSC_RECEIVE_HOLDING
;
722 DbpString("simulate tag (now type bitsamples)");
725 void UsbPacketReceived(BYTE
*packet
, int len
)
727 UsbCommand
*c
= (UsbCommand
*)packet
;
730 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
:
731 AcquireRawAdcSamples125k(c
->ext1
);
734 case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
:
735 ModThenAcquireRawAdcSamples125k(c
->ext1
,c
->ext2
,c
->ext3
,c
->d
.asBytes
);
738 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
:
739 AcquireRawAdcSamplesIso15693();
746 case CMD_READER_ISO_15693
:
747 ReaderIso15693(c
->ext1
);
750 case CMD_SIMTAG_ISO_15693
:
751 SimTagIso15693(c
->ext1
);
754 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
:
755 AcquireRawAdcSamplesIso14443(c
->ext1
);
758 case CMD_READ_SRI512_TAG
:
759 ReadSRI512Iso14443(c
->ext1
);
762 case CMD_READER_ISO_14443a
:
763 ReaderIso14443a(c
->ext1
);
766 case CMD_SNOOP_ISO_14443
:
770 case CMD_SNOOP_ISO_14443a
:
774 case CMD_SIMULATE_TAG_HF_LISTEN
:
775 SimulateTagHfListen();
778 case CMD_SIMULATE_TAG_ISO_14443
:
779 SimulateIso14443Tag();
782 case CMD_SIMULATE_TAG_ISO_14443a
:
783 SimulateIso14443aTag(c
->ext1
, c
->ext2
); // ## Simulate iso14443a tag - pass tag type & UID
786 case CMD_MEASURE_ANTENNA_TUNING
:
787 MeasureAntennaTuning();
790 case CMD_LISTEN_READER_FIELD
:
791 ListenReaderField(c
->ext1
);
794 case CMD_HID_DEMOD_FSK
:
795 CmdHIDdemodFSK(0, 0, 0, 1); // Demodulate HID tag
798 case CMD_HID_SIM_TAG
:
799 CmdHIDsimTAG(c
->ext1
, c
->ext2
, 1); // Simulate HID tag by ID
802 case CMD_FPGA_MAJOR_MODE_OFF
: // ## FPGA Control
803 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
805 LED_D_OFF(); // LED D indicates field ON or OFF
808 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
:
809 case CMD_DOWNLOAD_RAW_BITS_TI_TYPE
: {
811 if(c
->cmd
== CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
) {
812 n
.cmd
= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
;
814 n
.cmd
= CMD_DOWNLOADED_RAW_BITS_TI_TYPE
;
817 memcpy(n
.d
.asDwords
, BigBuf
+c
->ext1
, 12*sizeof(DWORD
));
818 UsbSendPacket((BYTE
*)&n
, sizeof(n
));
821 case CMD_DOWNLOADED_SIM_SAMPLES_125K
: {
822 BYTE
*b
= (BYTE
*)BigBuf
;
823 memcpy(b
+c
->ext1
, c
->d
.asBytes
, 48);
826 case CMD_SIMULATE_TAG_125K
:
828 SimulateTagLowFrequency(c
->ext1
, 1);
843 case CMD_SET_LF_DIVISOR
:
844 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, c
->ext1
);
851 case CMD_SETUP_WRITE
:
852 case CMD_FINISH_WRITE
:
853 case CMD_HARDWARE_RESET
:
854 USB_D_PLUS_PULLUP_OFF();
857 RSTC_CONTROL
= RST_CONTROL_KEY
| RST_CONTROL_PROCESSOR_RESET
;
859 // We're going to reset, and the bootrom will take control.
865 DbpString("unknown command");
870 void ReadMem(int addr
)
872 const DWORD
*data
= ((DWORD
*)addr
);
875 DbpString("Reading memory at address");
876 DbpIntegers(0, 0, addr
);
877 for (i
= 0; i
< 8; i
+= 2)
878 DbpIntegers(0, data
[i
], data
[i
+1]);
883 memset(BigBuf
,0,sizeof(BigBuf
));
893 // The FPGA gets its clock from us from PCK0 output, so set that up.
894 PIO_PERIPHERAL_B_SEL
= (1 << GPIO_PCK0
);
895 PIO_DISABLE
= (1 << GPIO_PCK0
);
896 PMC_SYS_CLK_ENABLE
= PMC_SYS_CLK_PROGRAMMABLE_CLK_0
;
897 // PCK0 is PLL clock / 4 = 96Mhz / 4 = 24Mhz
898 PMC_PROGRAMMABLE_CLK_0
= PMC_CLK_SELECTION_PLL_CLOCK
|
899 PMC_CLK_PRESCALE_DIV_4
;
900 PIO_OUTPUT_ENABLE
= (1 << GPIO_PCK0
);
903 SPI_CONTROL
= SPI_CONTROL_RESET
;
905 SSC_CONTROL
= SSC_CONTROL_RESET
;
907 // Load the FPGA image, which we have stored in our flash.
914 // test text on different colored backgrounds
915 LCDString(" The quick brown fox ", &FONT6x8
,1,1+8*0,WHITE
,BLACK
);
916 LCDString(" jumped over the ", &FONT6x8
,1,1+8*1,BLACK
,WHITE
);
917 LCDString(" lazy dog. ", &FONT6x8
,1,1+8*2,YELLOW
,RED
);
918 LCDString(" AaBbCcDdEeFfGgHhIiJj ", &FONT6x8
,1,1+8*3,RED
,GREEN
);
919 LCDString(" KkLlMmNnOoPpQqRrSsTt ", &FONT6x8
,1,1+8*4,MAGENTA
,BLUE
);
920 LCDString("UuVvWwXxYyZz0123456789", &FONT6x8
,1,1+8*5,BLUE
,YELLOW
);
921 LCDString("`-=[]_;',./~!@#$%^&*()", &FONT6x8
,1,1+8*6,BLACK
,CYAN
);
922 LCDString(" _+{}|:\\\"<>? ",&FONT6x8
,1,1+8*7,BLUE
,MAGENTA
);
925 LCDFill(0, 1+8* 8, 132, 8, BLACK
);
926 LCDFill(0, 1+8* 9, 132, 8, WHITE
);
927 LCDFill(0, 1+8*10, 132, 8, RED
);
928 LCDFill(0, 1+8*11, 132, 8, GREEN
);
929 LCDFill(0, 1+8*12, 132, 8, BLUE
);
930 LCDFill(0, 1+8*13, 132, 8, YELLOW
);
931 LCDFill(0, 1+8*14, 132, 8, CYAN
);
932 LCDFill(0, 1+8*15, 132, 8, MAGENTA
);
937 usbattached
= UsbPoll(FALSE
);
940 if (BUTTON_HELD(1000) > 0)
946 // samy's sniff and repeat routine
949 DbpString("Stand-alone mode! No PC necessary.");
951 // 3 possible options? no just 2 for now
954 int high
[OPTS
], low
[OPTS
];
956 // Oooh pretty -- notify user we're in elite samy mode now
958 LED(LED_ORANGE
, 200);
960 LED(LED_ORANGE
, 200);
962 LED(LED_ORANGE
, 200);
964 LED(LED_ORANGE
, 200);
970 // Turn on selected LED
971 LED(selected
+ 1, 0);
975 usbattached
= UsbPoll(FALSE
);
978 // Was our button held down or pressed?
979 int button_pressed
= BUTTON_HELD(1000);
982 // Button was held for a second, begin recording
983 if (button_pressed
> 0)
986 LED(selected
+ 1, 0);
990 DbpString("Starting recording");
992 /* need this delay to prevent catching some weird data */
994 CmdHIDdemodFSK(1, &high
[selected
], &low
[selected
], 0);
995 DbpString("Recorded");
996 DbpIntegers(selected
, high
[selected
], low
[selected
]);
999 LED(selected
+ 1, 0);
1000 // Finished recording
1002 // If we were previously playing, set playing off
1003 // so next button push begins playing what we recorded
1007 // Change where to record (or begin playing)
1008 else if (button_pressed
)
1010 // Next option if we were previously playing
1012 selected
= (selected
+ 1) % OPTS
;
1016 LED(selected
+ 1, 0);
1018 // Begin transmitting
1022 DbpString("Playing");
1023 DbpIntegers(selected
, high
[selected
], low
[selected
]);
1024 CmdHIDsimTAG(high
[selected
], low
[selected
], 0);
1025 DbpString("Done playing");
1027 /* We pressed a button so ignore it here with a delay */
1030 // when done, we're done playing, move to next option
1031 selected
= (selected
+ 1) % OPTS
;
1034 LED(selected
+ 1, 0);
1041 // listen for external reader
1042 void ListenReaderField(int limit
)
1044 int lf_av
, lf_av_new
, lf_baseline
= 0, lf_count
= 0;
1045 int hf_av
, hf_av_new
, hf_baseline
= 0, hf_count
= 0;
1055 lf_av
= ReadAdc(ADC_CHAN_LF
);
1057 if(limit
!= HF_ONLY
)
1059 DbpString("LF 125/134 Baseline:");
1060 DbpIntegers(lf_av
,0,0);
1064 hf_av
= ReadAdc(ADC_CHAN_HF
);
1067 if (limit
!= LF_ONLY
)
1069 DbpString("HF 13.56 Baseline:");
1070 DbpIntegers(hf_av
,0,0);
1078 DbpString("Stopped");
1086 if (limit
!= HF_ONLY
)
1088 if (abs(lf_av
- lf_baseline
) > 10)
1093 lf_av_new
= ReadAdc(ADC_CHAN_LF
);
1094 // see if there's a significant change
1095 if(abs(lf_av
- lf_av_new
) > 10)
1097 DbpString("LF 125/134 Field Change:");
1098 DbpIntegers(lf_av
,lf_av_new
,lf_count
);
1104 if (limit
!= LF_ONLY
)
1106 if (abs(hf_av
- hf_baseline
) > 10)
1111 hf_av_new
= ReadAdc(ADC_CHAN_HF
);
1112 // see if there's a significant change
1113 if(abs(hf_av
- hf_av_new
) > 10)
1115 DbpString("HF 13.56 Field Change:");
1116 DbpIntegers(hf_av
,hf_av_new
,hf_count
);