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((char *) 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
;
245 int i
, peak
= 0, ptr
= 0;
249 memset(BigBuf
,0,sizeof(BigBuf
));
251 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
252 for (i
=255; i
>19; i
--) {
253 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, i
);
255 dest
[i
] = (137500 * AvgAdc(ADC_CHAN_LF
)) >> 18;
266 freq
= 12000000/(ptr
+ 1);
267 for(i
= 6; i
> 3 ; --i
) {
268 dummy
[i
]= '0' + ((int) freq
) % 10;
272 for(i
= 2; i
>= 0 ; --i
) {
273 dummy
[i
]= '0' + ((int) freq
) % 10;
276 DbpString("Antenna resonates at:");
280 void MeasureAntennaTuning(void)
282 // Impedances are Zc = 1/(j*omega*C), in ohms
283 #define LF_TUNING_CAP_Z 1273 // 1 nF @ 125 kHz
284 #define HF_TUNING_CAP_Z 235 // 50 pF @ 13.56 MHz
286 int vLf125
, vLf134
, vHf
; // in mV
290 // Let the FPGA drive the low-frequency antenna around 125 kHz.
291 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
292 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
294 vLf125
= AvgAdc(ADC_CHAN_LF
);
295 // Vref = 3.3V, and a 10000:240 voltage divider on the input
296 // can measure voltages up to 137500 mV
297 vLf125
= (137500 * vLf125
) >> 10;
299 // Let the FPGA drive the low-frequency antenna around 134 kHz.
300 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
301 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_134_KHZ
);
303 vLf134
= 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 vLf134
= (137500 * vLf134
) >> 10;
308 // Let the FPGA drive the high-frequency antenna around 13.56 MHz.
309 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
311 vHf
= AvgAdc(ADC_CHAN_HF
);
312 // Vref = 3300mV, and an 10:1 voltage divider on the input
313 // can measure voltages up to 33000 mV
314 vHf
= (33000 * vHf
) >> 10;
316 c
.cmd
= CMD_MEASURED_ANTENNA_TUNING
;
317 c
.ext1
= (vLf125
<< 0) | (vLf134
<< 16);
319 c
.ext3
= (LF_TUNING_CAP_Z
<< 0) | (HF_TUNING_CAP_Z
<< 16);
320 UsbSendPacket((BYTE
*)&c
, sizeof(c
));
323 void SimulateTagLowFrequency(int period
)
326 BYTE
*tab
= (BYTE
*)BigBuf
;
328 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_SIMULATOR
);
330 PIO_ENABLE
= (1 << GPIO_SSC_DOUT
) | (1 << GPIO_SSC_CLK
);
332 PIO_OUTPUT_ENABLE
= (1 << GPIO_SSC_DOUT
);
333 PIO_OUTPUT_DISABLE
= (1 << GPIO_SSC_CLK
);
335 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
336 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
340 while(!(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
))) {
342 DbpString("Stopped");
356 while(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
)) {
358 DbpString("Stopped");
365 if(i
== period
) i
= 0;
369 // compose fc/8 fc/10 waveform
370 static void fc(int c
, int *n
) {
371 BYTE
*dest
= (BYTE
*)BigBuf
;
374 // for when we want an fc8 pattern every 4 logical bits
385 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
387 for (idx
=0; idx
<6; idx
++) {
399 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
401 for (idx
=0; idx
<5; idx
++) {
416 // prepare a waveform pattern in the buffer based on the ID given then
417 // simulate a HID tag until the button is pressed
418 static void CmdHIDsimTAG(int hi
, int lo
)
422 HID tag bitstream format
423 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
424 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
425 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
426 A fc8 is inserted before every 4 bits
427 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
428 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
432 DbpString("Tags can only have 44 bits.");
436 // special start of frame marker containing invalid bit sequences
437 fc(8, &n
); fc(8, &n
); // invalid
438 fc(8, &n
); fc(10, &n
); // logical 0
439 fc(10, &n
); fc(10, &n
); // invalid
440 fc(8, &n
); fc(10, &n
); // logical 0
443 // manchester encode bits 43 to 32
444 for (i
=11; i
>=0; i
--) {
445 if ((i
%4)==3) fc(0,&n
);
447 fc(10, &n
); fc(8, &n
); // low-high transition
449 fc(8, &n
); fc(10, &n
); // high-low transition
454 // manchester encode bits 31 to 0
455 for (i
=31; i
>=0; i
--) {
456 if ((i
%4)==3) fc(0,&n
);
458 fc(10, &n
); fc(8, &n
); // low-high transition
460 fc(8, &n
); fc(10, &n
); // high-low transition
465 SimulateTagLowFrequency(n
);
469 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
470 static void CmdHIDdemodFSK(void)
472 BYTE
*dest
= (BYTE
*)BigBuf
;
473 int m
=0, n
=0, i
=0, idx
=0, found
=0, lastval
=0;
476 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
477 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
479 // Connect the A/D to the peak-detected low-frequency path.
480 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
482 // Give it a bit of time for the resonant antenna to settle.
485 // Now set up the SSC to get the ADC samples that are now streaming at us.
492 DbpString("Stopped");
501 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
502 SSC_TRANSMIT_HOLDING
= 0x43;
505 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
506 dest
[i
] = (BYTE
)SSC_RECEIVE_HOLDING
;
507 // we don't care about actual value, only if it's more or less than a
508 // threshold essentially we capture zero crossings for later analysis
509 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
520 // sync to first lo-hi transition
521 for( idx
=1; idx
<m
; idx
++) {
522 if (dest
[idx
-1]<dest
[idx
])
528 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
529 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
530 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
531 for( i
=0; idx
<m
; idx
++) {
532 if (dest
[idx
-1]<dest
[idx
]) {
547 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
552 for( idx
=0; idx
<m
; idx
++) {
553 if (dest
[idx
]==lastval
) {
556 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
557 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
558 // swallowed up by rounding
559 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
560 // special start of frame markers use invalid manchester states (no transitions) by using sequences
563 n
=(n
+1)/6; // fc/8 in sets of 6
565 n
=(n
+1)/5; // fc/10 in sets of 5
567 switch (n
) { // stuff appropriate bits in buffer
570 dest
[i
++]=dest
[idx
-1];
573 dest
[i
++]=dest
[idx
-1];
574 dest
[i
++]=dest
[idx
-1];
576 case 3: // 3 bit start of frame markers
577 dest
[i
++]=dest
[idx
-1];
578 dest
[i
++]=dest
[idx
-1];
579 dest
[i
++]=dest
[idx
-1];
581 // When a logic 0 is immediately followed by the start of the next transmisson
582 // (special pattern) a pattern of 4 bit duration lengths is created.
584 dest
[i
++]=dest
[idx
-1];
585 dest
[i
++]=dest
[idx
-1];
586 dest
[i
++]=dest
[idx
-1];
587 dest
[i
++]=dest
[idx
-1];
589 default: // this shouldn't happen, don't stuff any bits
599 // final loop, go over previously decoded manchester data and decode into usable tag ID
600 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
601 for( idx
=0; idx
<m
-6; idx
++) {
602 // search for a start of frame marker
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);
616 if (dest
[idx
] && (!dest
[idx
+1]) ) {
619 } else if ( (!dest
[idx
]) && dest
[idx
+1]) {
629 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
633 if (found
&& (hi
|lo
)) {
635 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
646 void SimulateTagHfListen(void)
648 BYTE
*dest
= (BYTE
*)BigBuf
;
649 int n
= sizeof(BigBuf
);
654 // We're using this mode just so that I can test it out; the simulated
655 // tag mode would work just as well and be simpler.
656 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
| FPGA_HF_READER_RX_XCORR_SNOOP
);
658 // We need to listen to the high-frequency, peak-detected path.
659 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
665 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
666 SSC_TRANSMIT_HOLDING
= 0xff;
668 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
669 BYTE r
= (BYTE
)SSC_RECEIVE_HOLDING
;
689 DbpString("simulate tag (now type bitsamples)");
692 void UsbPacketReceived(BYTE
*packet
, int len
)
694 UsbCommand
*c
= (UsbCommand
*)packet
;
697 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
:
698 AcquireRawAdcSamples125k(c
->ext1
);
701 case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
:
702 ModThenAcquireRawAdcSamples125k(c
->ext1
,c
->ext2
,c
->ext3
,c
->d
.asBytes
);
705 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
:
706 AcquireRawAdcSamplesIso15693();
713 case CMD_READER_ISO_15693
:
714 ReaderIso15693(c
->ext1
);
717 case CMD_SIMTAG_ISO_15693
:
718 SimTagIso15693(c
->ext1
);
721 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
:
722 AcquireRawAdcSamplesIso14443(c
->ext1
);
725 case CMD_READ_SRI512_TAG
:
726 ReadSRI512Iso14443(c
->ext1
);
729 case CMD_READER_ISO_14443a
:
730 ReaderIso14443a(c
->ext1
);
733 case CMD_SNOOP_ISO_14443
:
737 case CMD_SNOOP_ISO_14443a
:
741 case CMD_SIMULATE_TAG_HF_LISTEN
:
742 SimulateTagHfListen();
745 case CMD_SIMULATE_TAG_ISO_14443
:
746 SimulateIso14443Tag();
749 case CMD_SIMULATE_TAG_ISO_14443a
:
750 SimulateIso14443aTag(c
->ext1
, c
->ext2
); // ## Simulate iso14443a tag - pass tag type & UID
753 case CMD_MEASURE_ANTENNA_TUNING
:
754 MeasureAntennaTuning();
757 case CMD_LISTEN_READER_FIELD
:
758 ListenReaderField(c
->ext1
);
761 case CMD_HID_DEMOD_FSK
:
762 CmdHIDdemodFSK(); // Demodulate HID tag
765 case CMD_HID_SIM_TAG
:
766 CmdHIDsimTAG(c
->ext1
, c
->ext2
); // Simulate HID tag by ID
769 case CMD_FPGA_MAJOR_MODE_OFF
: // ## FPGA Control
770 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
772 LED_D_OFF(); // LED D indicates field ON or OFF
775 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
:
776 case CMD_DOWNLOAD_RAW_BITS_TI_TYPE
: {
778 if(c
->cmd
== CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
) {
779 n
.cmd
= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
;
781 n
.cmd
= CMD_DOWNLOADED_RAW_BITS_TI_TYPE
;
784 memcpy(n
.d
.asDwords
, BigBuf
+c
->ext1
, 12*sizeof(DWORD
));
785 UsbSendPacket((BYTE
*)&n
, sizeof(n
));
788 case CMD_DOWNLOADED_SIM_SAMPLES_125K
: {
789 BYTE
*b
= (BYTE
*)BigBuf
;
790 memcpy(b
+c
->ext1
, c
->d
.asBytes
, 48);
793 case CMD_SIMULATE_TAG_125K
:
795 SimulateTagLowFrequency(c
->ext1
);
807 case CMD_SET_LF_DIVISOR
:
808 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, c
->ext1
);
815 case CMD_SETUP_WRITE
:
816 case CMD_FINISH_WRITE
:
817 case CMD_HARDWARE_RESET
:
818 USB_D_PLUS_PULLUP_OFF();
821 RSTC_CONTROL
= RST_CONTROL_KEY
| RST_CONTROL_PROCESSOR_RESET
;
823 // We're going to reset, and the bootrom will take control.
829 DbpString("unknown command");
836 memset(BigBuf
,0,sizeof(BigBuf
));
846 // The FPGA gets its clock from us from PCK0 output, so set that up.
847 PIO_PERIPHERAL_B_SEL
= (1 << GPIO_PCK0
);
848 PIO_DISABLE
= (1 << GPIO_PCK0
);
849 PMC_SYS_CLK_ENABLE
= PMC_SYS_CLK_PROGRAMMABLE_CLK_0
;
850 // PCK0 is PLL clock / 4 = 96Mhz / 4 = 24Mhz
851 PMC_PROGRAMMABLE_CLK_0
= PMC_CLK_SELECTION_PLL_CLOCK
|
852 PMC_CLK_PRESCALE_DIV_4
;
853 PIO_OUTPUT_ENABLE
= (1 << GPIO_PCK0
);
856 SPI_CONTROL
= SPI_CONTROL_RESET
;
858 SSC_CONTROL
= SSC_CONTROL_RESET
;
860 // Load the FPGA image, which we have stored in our flash.
867 // test text on different colored backgrounds
868 LCDString(" The quick brown fox ", &FONT6x8
,1,1+8*0,WHITE
,BLACK
);
869 LCDString(" jumped over the ", &FONT6x8
,1,1+8*1,BLACK
,WHITE
);
870 LCDString(" lazy dog. ", &FONT6x8
,1,1+8*2,YELLOW
,RED
);
871 LCDString(" AaBbCcDdEeFfGgHhIiJj ", &FONT6x8
,1,1+8*3,RED
,GREEN
);
872 LCDString(" KkLlMmNnOoPpQqRrSsTt ", &FONT6x8
,1,1+8*4,MAGENTA
,BLUE
);
873 LCDString("UuVvWwXxYyZz0123456789", &FONT6x8
,1,1+8*5,BLUE
,YELLOW
);
874 LCDString("`-=[]_;',./~!@#$%^&*()", &FONT6x8
,1,1+8*6,BLACK
,CYAN
);
875 LCDString(" _+{}|:\\\"<>? ",&FONT6x8
,1,1+8*7,BLUE
,MAGENTA
);
878 LCDFill(0, 1+8* 8, 132, 8, BLACK
);
879 LCDFill(0, 1+8* 9, 132, 8, WHITE
);
880 LCDFill(0, 1+8*10, 132, 8, RED
);
881 LCDFill(0, 1+8*11, 132, 8, GREEN
);
882 LCDFill(0, 1+8*12, 132, 8, BLUE
);
883 LCDFill(0, 1+8*13, 132, 8, YELLOW
);
884 LCDFill(0, 1+8*14, 132, 8, CYAN
);
885 LCDFill(0, 1+8*15, 132, 8, MAGENTA
);
895 void SpinDelayUs(int us
)
897 int ticks
= (48*us
) >> 10;
899 // Borrow a PWM unit for my real-time clock
900 PWM_ENABLE
= PWM_CHANNEL(0);
901 // 48 MHz / 1024 gives 46.875 kHz
902 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
903 PWM_CH_DUTY_CYCLE(0) = 0;
904 PWM_CH_PERIOD(0) = 0xffff;
906 WORD start
= (WORD
)PWM_CH_COUNTER(0);
909 WORD now
= (WORD
)PWM_CH_COUNTER(0);
910 if(now
== (WORD
)(start
+ ticks
)) {
917 void SpinDelay(int ms
)
919 int ticks
= (48000*ms
) >> 10;
921 // Borrow a PWM unit for my real-time clock
922 PWM_ENABLE
= PWM_CHANNEL(0);
923 // 48 MHz / 1024 gives 46.875 kHz
924 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
925 PWM_CH_DUTY_CYCLE(0) = 0;
926 PWM_CH_PERIOD(0) = 0xffff;
928 WORD start
= (WORD
)PWM_CH_COUNTER(0);
931 WORD now
= (WORD
)PWM_CH_COUNTER(0);
932 if(now
== (WORD
)(start
+ ticks
)) {
939 // listen for external reader
940 void ListenReaderField(int limit
)
942 int lf_av
, lf_av_new
, lf_baseline
= 0, lf_count
= 0;
943 int hf_av
, hf_av_new
, hf_baseline
= 0, hf_count
= 0;
953 lf_av
= ReadAdc(ADC_CHAN_LF
);
957 DbpString("LF 125/134 Baseline:");
958 DbpIntegers(lf_av
,0,0);
962 hf_av
= ReadAdc(ADC_CHAN_HF
);
965 if (limit
!= LF_ONLY
)
967 DbpString("HF 13.56 Baseline:");
968 DbpIntegers(hf_av
,0,0);
976 DbpString("Stopped");
984 if (limit
!= HF_ONLY
)
986 if (abs(lf_av
- lf_baseline
) > 10)
991 lf_av_new
= ReadAdc(ADC_CHAN_LF
);
992 // see if there's a significant change
993 if(abs(lf_av
- lf_av_new
) > 10)
995 DbpString("LF 125/134 Field Change:");
996 DbpIntegers(lf_av
,lf_av_new
,lf_count
);
1002 if (limit
!= LF_ONLY
)
1004 if (abs(hf_av
- hf_baseline
) > 10)
1009 hf_av_new
= ReadAdc(ADC_CHAN_HF
);
1010 // see if there's a significant change
1011 if(abs(hf_av
- hf_av_new
) > 10)
1013 DbpString("HF 13.56 Field Change:");
1014 DbpIntegers(hf_av
,hf_av_new
,hf_count
);