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.
21 //=============================================================================
22 // A buffer where we can queue things up to be sent through the FPGA, for
23 // any purpose (fake tag, as reader, whatever). We go MSB first, since that
24 // is the order in which they go out on the wire.
25 //=============================================================================
32 void BufferClear(void)
34 memset(BigBuf
,0,sizeof(BigBuf
));
35 DbpString("Buffer cleared");
38 void ToSendReset(void)
44 void ToSendStuffBit(int b
)
48 ToSend
[ToSendMax
] = 0;
53 ToSend
[ToSendMax
] |= (1 << (7 - ToSendBit
));
58 if(ToSendBit
>= sizeof(ToSend
)) {
60 DbpString("ToSendStuffBit overflowed!");
64 //=============================================================================
65 // Debug print functions, to go out over USB, to the usual PC-side client.
66 //=============================================================================
68 void DbpString(char *str
)
71 c
.cmd
= CMD_DEBUG_PRINT_STRING
;
73 memcpy(c
.d
.asBytes
, str
, c
.ext1
);
75 UsbSendPacket((BYTE
*)&c
, sizeof(c
));
76 // TODO fix USB so stupid things like this aren't req'd
80 void DbpIntegers(int x1
, int x2
, int x3
)
83 c
.cmd
= CMD_DEBUG_PRINT_INTEGERS
;
88 UsbSendPacket((BYTE
*)&c
, sizeof(c
));
93 void AcquireRawAdcSamples125k(BOOL at134khz
)
96 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
97 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_134_KHZ
);
99 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
100 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
103 // Connect the A/D to the peak-detected low-frequency path.
104 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
106 // Give it a bit of time for the resonant antenna to settle.
109 // Now set up the SSC to get the ADC samples that are now streaming at us.
112 // Now call the acquisition routine
113 DoAcquisition125k(at134khz
);
116 // split into two routines so we can avoid timing issues after sending commands //
117 void DoAcquisition125k(BOOL at134khz
)
119 BYTE
*dest
= (BYTE
*)BigBuf
;
120 int n
= sizeof(BigBuf
);
126 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
127 SSC_TRANSMIT_HOLDING
= 0x43;
130 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
131 dest
[i
] = (BYTE
)SSC_RECEIVE_HOLDING
;
139 DbpIntegers(dest
[0], dest
[1], at134khz
);
142 void ModThenAcquireRawAdcSamples125k(int delay_off
,int period_0
,int period_1
,BYTE
*command
)
146 // see if 'h' was specified
147 if(command
[strlen(command
) - 1] == 'h')
153 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
154 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_134_KHZ
);
156 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
157 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
160 // Give it a bit of time for the resonant antenna to settle.
163 // Now set up the SSC to get the ADC samples that are now streaming at us.
166 // now modulate the reader field
167 while(*command
!= '\0' && *command
!= ' ')
169 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
171 SpinDelayUs(delay_off
);
173 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
174 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_134_KHZ
);
176 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
177 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
180 if(*(command
++) == '0')
181 SpinDelayUs(period_0
);
183 SpinDelayUs(period_1
);
185 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
187 SpinDelayUs(delay_off
);
189 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
190 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_134_KHZ
);
192 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
193 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
197 DoAcquisition125k(at134khz
);
200 //-----------------------------------------------------------------------------
201 // Read an ADC channel and block till it completes, then return the result
202 // in ADC units (0 to 1023). Also a routine to average 32 samples and
204 //-----------------------------------------------------------------------------
205 static int ReadAdc(int ch
)
209 ADC_CONTROL
= ADC_CONTROL_RESET
;
210 ADC_MODE
= ADC_MODE_PRESCALE(32) | ADC_MODE_STARTUP_TIME(16) |
211 ADC_MODE_SAMPLE_HOLD_TIME(8);
212 ADC_CHANNEL_ENABLE
= ADC_CHANNEL(ch
);
214 ADC_CONTROL
= ADC_CONTROL_START
;
215 while(!(ADC_STATUS
& ADC_END_OF_CONVERSION(ch
)))
217 d
= ADC_CHANNEL_DATA(ch
);
222 static int AvgAdc(int ch
)
227 for(i
= 0; i
< 32; i
++) {
231 return (a
+ 15) >> 5;
235 * Sweeps the useful LF range of the proxmark from
236 * 46.8kHz (divisor=255) to 600kHz (divisor=19) and
237 * reads the voltage in the antenna: the result is a graph
238 * which should clearly show the resonating frequency of your
239 * LF antenna ( hopefully around 90 if it is tuned to 125kHz!)
243 BYTE
*dest
= (BYTE
*)BigBuf
;
247 memset(BigBuf
,0,sizeof(BigBuf
));
249 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
250 for (i
=255; i
>19; i
--) {
251 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, i
);
253 dest
[i
] = (137500 * AvgAdc(ADC_CHAN_LF
)) >> 18;
257 void MeasureAntennaTuning(void)
259 // Impedances are Zc = 1/(j*omega*C), in ohms
260 #define LF_TUNING_CAP_Z 1273 // 1 nF @ 125 kHz
261 #define HF_TUNING_CAP_Z 235 // 50 pF @ 13.56 MHz
263 int vLf125
, vLf134
, vHf
; // in mV
267 // Let the FPGA drive the low-frequency antenna around 125 kHz.
268 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
269 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
271 vLf125
= AvgAdc(ADC_CHAN_LF
);
272 // Vref = 3.3V, and a 10000:240 voltage divider on the input
273 // can measure voltages up to 137500 mV
274 vLf125
= (137500 * vLf125
) >> 10;
276 // Let the FPGA drive the low-frequency antenna around 134 kHz.
277 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
278 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_134_KHZ
);
280 vLf134
= AvgAdc(ADC_CHAN_LF
);
281 // Vref = 3.3V, and a 10000:240 voltage divider on the input
282 // can measure voltages up to 137500 mV
283 vLf134
= (137500 * vLf134
) >> 10;
285 // Let the FPGA drive the high-frequency antenna around 13.56 MHz.
286 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
288 vHf
= AvgAdc(ADC_CHAN_HF
);
289 // Vref = 3300mV, and an 10:1 voltage divider on the input
290 // can measure voltages up to 33000 mV
291 vHf
= (33000 * vHf
) >> 10;
293 c
.cmd
= CMD_MEASURED_ANTENNA_TUNING
;
294 c
.ext1
= (vLf125
<< 0) | (vLf134
<< 16);
296 c
.ext3
= (LF_TUNING_CAP_Z
<< 0) | (HF_TUNING_CAP_Z
<< 16);
297 UsbSendPacket((BYTE
*)&c
, sizeof(c
));
300 void SimulateTagLowFrequency(int period
)
303 BYTE
*tab
= (BYTE
*)BigBuf
;
305 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_SIMULATOR
);
307 PIO_ENABLE
= (1 << GPIO_SSC_DOUT
) | (1 << GPIO_SSC_CLK
);
309 PIO_OUTPUT_ENABLE
= (1 << GPIO_SSC_DOUT
);
310 PIO_OUTPUT_DISABLE
= (1 << GPIO_SSC_CLK
);
312 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
313 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
317 while(!(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
))) {
332 while(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
)) {
340 if(i
== period
) i
= 0;
344 // compose fc/8 fc/10 waveform
345 static void fc(int c
, int *n
) {
346 BYTE
*dest
= (BYTE
*)BigBuf
;
349 // for when we want an fc8 pattern every 4 logical bits
360 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
362 for (idx
=0; idx
<6; idx
++) {
374 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
376 for (idx
=0; idx
<5; idx
++) {
391 // prepare a waveform pattern in the buffer based on the ID given then
392 // simulate a HID tag until the button is pressed
393 static void CmdHIDsimTAG(int hi
, int lo
)
397 HID tag bitstream format
398 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
399 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
400 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
401 A fc8 is inserted before every 4 bits
402 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
403 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
407 DbpString("Tags can only have 44 bits.");
411 // special start of frame marker containing invalid bit sequences
412 fc(8, &n
); fc(8, &n
); // invalid
413 fc(8, &n
); fc(10, &n
); // logical 0
414 fc(10, &n
); fc(10, &n
); // invalid
415 fc(8, &n
); fc(10, &n
); // logical 0
418 // manchester encode bits 43 to 32
419 for (i
=11; i
>=0; i
--) {
420 if ((i
%4)==3) fc(0,&n
);
422 fc(10, &n
); fc(8, &n
); // low-high transition
424 fc(8, &n
); fc(10, &n
); // high-low transition
429 // manchester encode bits 31 to 0
430 for (i
=31; i
>=0; i
--) {
431 if ((i
%4)==3) fc(0,&n
);
433 fc(10, &n
); fc(8, &n
); // low-high transition
435 fc(8, &n
); fc(10, &n
); // high-low transition
440 SimulateTagLowFrequency(n
);
444 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
445 static void CmdHIDdemodFSK(void)
447 BYTE
*dest
= (BYTE
*)BigBuf
;
448 int m
=0, n
=0, i
=0, idx
=0, found
=0, lastval
=0;
451 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
452 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
454 // Connect the A/D to the peak-detected low-frequency path.
455 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
457 // Give it a bit of time for the resonant antenna to settle.
460 // Now set up the SSC to get the ADC samples that are now streaming at us.
475 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
476 SSC_TRANSMIT_HOLDING
= 0x43;
479 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
480 dest
[i
] = (BYTE
)SSC_RECEIVE_HOLDING
;
481 // we don't care about actual value, only if it's more or less than a
482 // threshold essentially we capture zero crossings for later analysis
483 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
494 // sync to first lo-hi transition
495 for( idx
=1; idx
<m
; idx
++) {
496 if (dest
[idx
-1]<dest
[idx
])
502 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
503 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
504 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
505 for( i
=0; idx
<m
; idx
++) {
506 if (dest
[idx
-1]<dest
[idx
]) {
521 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
526 for( idx
=0; idx
<m
; idx
++) {
527 if (dest
[idx
]==lastval
) {
530 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
531 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
532 // swallowed up by rounding
533 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
534 // special start of frame markers use invalid manchester states (no transitions) by using sequences
537 n
=(n
+1)/6; // fc/8 in sets of 6
539 n
=(n
+1)/5; // fc/10 in sets of 5
541 switch (n
) { // stuff appropriate bits in buffer
544 dest
[i
++]=dest
[idx
-1];
547 dest
[i
++]=dest
[idx
-1];
548 dest
[i
++]=dest
[idx
-1];
550 case 3: // 3 bit start of frame markers
551 dest
[i
++]=dest
[idx
-1];
552 dest
[i
++]=dest
[idx
-1];
553 dest
[i
++]=dest
[idx
-1];
555 // When a logic 0 is immediately followed by the start of the next transmisson
556 // (special pattern) a pattern of 4 bit duration lengths is created.
558 dest
[i
++]=dest
[idx
-1];
559 dest
[i
++]=dest
[idx
-1];
560 dest
[i
++]=dest
[idx
-1];
561 dest
[i
++]=dest
[idx
-1];
563 default: // this shouldn't happen, don't stuff any bits
573 // final loop, go over previously decoded manchester data and decode into usable tag ID
574 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
575 for( idx
=0; idx
<m
-6; idx
++) {
576 // search for a start of frame marker
577 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
581 if (found
&& (hi
|lo
)) {
583 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
590 if (dest
[idx
] && (!dest
[idx
+1]) ) {
593 } else if ( (!dest
[idx
]) && dest
[idx
+1]) {
603 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
607 if (found
&& (hi
|lo
)) {
609 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
620 void SimulateTagHfListen(void)
622 BYTE
*dest
= (BYTE
*)BigBuf
;
623 int n
= sizeof(BigBuf
);
628 // We're using this mode just so that I can test it out; the simulated
629 // tag mode would work just as well and be simpler.
630 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
| FPGA_HF_READER_RX_XCORR_SNOOP
);
632 // We need to listen to the high-frequency, peak-detected path.
633 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
639 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
640 SSC_TRANSMIT_HOLDING
= 0xff;
642 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
643 BYTE r
= (BYTE
)SSC_RECEIVE_HOLDING
;
663 DbpString("simulate tag (now type bitsamples)");
666 void UsbPacketReceived(BYTE
*packet
, int len
)
668 UsbCommand
*c
= (UsbCommand
*)packet
;
671 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
:
672 AcquireRawAdcSamples125k(c
->ext1
);
675 case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
:
676 ModThenAcquireRawAdcSamples125k(c
->ext1
,c
->ext2
,c
->ext3
,c
->d
.asBytes
);
679 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
:
680 AcquireRawAdcSamplesIso15693();
687 case CMD_READER_ISO_15693
:
688 ReaderIso15693(c
->ext1
);
691 case CMD_SIMTAG_ISO_15693
:
692 SimTagIso15693(c
->ext1
);
695 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
:
696 AcquireRawAdcSamplesIso14443(c
->ext1
);
699 case CMD_READ_SRI512_TAG
:
700 ReadSRI512Iso14443(c
->ext1
);
703 case CMD_READER_ISO_14443a
:
704 ReaderIso14443a(c
->ext1
);
707 case CMD_SNOOP_ISO_14443
:
711 case CMD_SNOOP_ISO_14443a
:
715 case CMD_SIMULATE_TAG_HF_LISTEN
:
716 SimulateTagHfListen();
719 case CMD_SIMULATE_TAG_ISO_14443
:
720 SimulateIso14443Tag();
723 case CMD_SIMULATE_TAG_ISO_14443a
:
724 SimulateIso14443aTag(c
->ext1
, c
->ext2
); // ## Simulate iso14443a tag - pass tag type & UID
727 case CMD_MEASURE_ANTENNA_TUNING
:
728 MeasureAntennaTuning();
731 case CMD_LISTEN_READER_FIELD
:
732 ListenReaderField(c
->ext1
);
735 case CMD_HID_DEMOD_FSK
:
736 CmdHIDdemodFSK(); // Demodulate HID tag
739 case CMD_HID_SIM_TAG
:
740 CmdHIDsimTAG(c
->ext1
, c
->ext2
); // Simulate HID tag by ID
743 case CMD_FPGA_MAJOR_MODE_OFF
: // ## FPGA Control
744 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
746 LED_D_OFF(); // LED D indicates field ON or OFF
749 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
:
750 case CMD_DOWNLOAD_RAW_BITS_TI_TYPE
: {
752 if(c
->cmd
== CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
) {
753 n
.cmd
= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
;
755 n
.cmd
= CMD_DOWNLOADED_RAW_BITS_TI_TYPE
;
758 memcpy(n
.d
.asDwords
, BigBuf
+c
->ext1
, 12*sizeof(DWORD
));
759 UsbSendPacket((BYTE
*)&n
, sizeof(n
));
762 case CMD_DOWNLOADED_SIM_SAMPLES_125K
: {
763 BYTE
*b
= (BYTE
*)BigBuf
;
764 memcpy(b
+c
->ext1
, c
->d
.asBytes
, 48);
767 case CMD_SIMULATE_TAG_125K
:
769 SimulateTagLowFrequency(c
->ext1
);
781 case CMD_SET_LF_DIVISOR
:
782 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, c
->ext1
);
789 case CMD_SETUP_WRITE
:
790 case CMD_FINISH_WRITE
:
791 case CMD_HARDWARE_RESET
:
792 USB_D_PLUS_PULLUP_OFF();
795 RSTC_CONTROL
= RST_CONTROL_KEY
| RST_CONTROL_PROCESSOR_RESET
;
797 // We're going to reset, and the bootrom will take control.
803 DbpString("unknown command");
810 memset(BigBuf
,0,sizeof(BigBuf
));
820 // The FPGA gets its clock from us from PCK0 output, so set that up.
821 PIO_PERIPHERAL_B_SEL
= (1 << GPIO_PCK0
);
822 PIO_DISABLE
= (1 << GPIO_PCK0
);
823 PMC_SYS_CLK_ENABLE
= PMC_SYS_CLK_PROGRAMMABLE_CLK_0
;
824 // PCK0 is PLL clock / 4 = 96Mhz / 4 = 24Mhz
825 PMC_PROGRAMMABLE_CLK_0
= PMC_CLK_SELECTION_PLL_CLOCK
|
826 PMC_CLK_PRESCALE_DIV_4
;
827 PIO_OUTPUT_ENABLE
= (1 << GPIO_PCK0
);
830 SPI_CONTROL
= SPI_CONTROL_RESET
;
832 SSC_CONTROL
= SSC_CONTROL_RESET
;
834 // Load the FPGA image, which we have stored in our flash.
841 // test text on different colored backgrounds
842 LCDString(" The quick brown fox ", &FONT6x8
,1,1+8*0,WHITE
,BLACK
);
843 LCDString(" jumped over the ", &FONT6x8
,1,1+8*1,BLACK
,WHITE
);
844 LCDString(" lazy dog. ", &FONT6x8
,1,1+8*2,YELLOW
,RED
);
845 LCDString(" AaBbCcDdEeFfGgHhIiJj ", &FONT6x8
,1,1+8*3,RED
,GREEN
);
846 LCDString(" KkLlMmNnOoPpQqRrSsTt ", &FONT6x8
,1,1+8*4,MAGENTA
,BLUE
);
847 LCDString("UuVvWwXxYyZz0123456789", &FONT6x8
,1,1+8*5,BLUE
,YELLOW
);
848 LCDString("`-=[]_;',./~!@#$%^&*()", &FONT6x8
,1,1+8*6,BLACK
,CYAN
);
849 LCDString(" _+{}|:\\\"<>? ",&FONT6x8
,1,1+8*7,BLUE
,MAGENTA
);
852 LCDFill(0, 1+8* 8, 132, 8, BLACK
);
853 LCDFill(0, 1+8* 9, 132, 8, WHITE
);
854 LCDFill(0, 1+8*10, 132, 8, RED
);
855 LCDFill(0, 1+8*11, 132, 8, GREEN
);
856 LCDFill(0, 1+8*12, 132, 8, BLUE
);
857 LCDFill(0, 1+8*13, 132, 8, YELLOW
);
858 LCDFill(0, 1+8*14, 132, 8, CYAN
);
859 LCDFill(0, 1+8*15, 132, 8, MAGENTA
);
869 void SpinDelayUs(int us
)
871 int ticks
= (48*us
) >> 10;
873 // Borrow a PWM unit for my real-time clock
874 PWM_ENABLE
= PWM_CHANNEL(0);
875 // 48 MHz / 1024 gives 46.875 kHz
876 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
877 PWM_CH_DUTY_CYCLE(0) = 0;
878 PWM_CH_PERIOD(0) = 0xffff;
880 WORD start
= (WORD
)PWM_CH_COUNTER(0);
883 WORD now
= (WORD
)PWM_CH_COUNTER(0);
884 if(now
== (WORD
)(start
+ ticks
)) {
891 void SpinDelay(int ms
)
893 int ticks
= (48000*ms
) >> 10;
895 // Borrow a PWM unit for my real-time clock
896 PWM_ENABLE
= PWM_CHANNEL(0);
897 // 48 MHz / 1024 gives 46.875 kHz
898 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
899 PWM_CH_DUTY_CYCLE(0) = 0;
900 PWM_CH_PERIOD(0) = 0xffff;
902 WORD start
= (WORD
)PWM_CH_COUNTER(0);
905 WORD now
= (WORD
)PWM_CH_COUNTER(0);
906 if(now
== (WORD
)(start
+ ticks
)) {
913 // listen for external reader
914 void ListenReaderField(int limit
)
916 int lf_av
, lf_av_new
, lf_baseline
= -1, lf_count
= 0;
917 int hf_av
, hf_av_new
, hf_baseline
= -1, hf_count
= 0;
927 lf_av
= ReadAdc(ADC_CHAN_LF
);
929 if(limit
!= HF_ONLY
&& lf_baseline
== -1)
931 DbpString("LF 125/134 Baseline:");
932 DbpIntegers(lf_av
,0,0);
936 hf_av
= ReadAdc(ADC_CHAN_HF
);
939 if (limit
!= LF_ONLY
&& hf_baseline
== -1)
941 DbpString("HF 13.56 Baseline:");
942 DbpIntegers(hf_av
,0,0);
957 if (limit
!= HF_ONLY
)
959 if (abs(lf_av
- lf_baseline
) > 10)
964 lf_av_new
= ReadAdc(ADC_CHAN_LF
);
965 // see if there's a significant change
966 if(abs(lf_av
- lf_av_new
) > 10)
968 DbpString("LF 125/134 Field Change:");
969 DbpIntegers(lf_av
,lf_av_new
,lf_count
);
975 if (limit
!= LF_ONLY
)
977 if (abs(hf_av
- hf_baseline
) > 10)
982 hf_av_new
= ReadAdc(ADC_CHAN_HF
);
983 // see if there's a significant change
984 if(abs(hf_av
- hf_av_new
) > 10)
986 DbpString("HF 13.56 Field Change:");
987 DbpIntegers(hf_av
,hf_av_new
,hf_count
);