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
;
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
))) {
355 while(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
)) {
363 if(i
== period
) i
= 0;
367 // compose fc/8 fc/10 waveform
368 static void fc(int c
, int *n
) {
369 BYTE
*dest
= (BYTE
*)BigBuf
;
372 // for when we want an fc8 pattern every 4 logical bits
383 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
385 for (idx
=0; idx
<6; idx
++) {
397 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
399 for (idx
=0; idx
<5; idx
++) {
414 // prepare a waveform pattern in the buffer based on the ID given then
415 // simulate a HID tag until the button is pressed
416 static void CmdHIDsimTAG(int hi
, int lo
)
420 HID tag bitstream format
421 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
422 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
423 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
424 A fc8 is inserted before every 4 bits
425 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
426 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
430 DbpString("Tags can only have 44 bits.");
434 // special start of frame marker containing invalid bit sequences
435 fc(8, &n
); fc(8, &n
); // invalid
436 fc(8, &n
); fc(10, &n
); // logical 0
437 fc(10, &n
); fc(10, &n
); // invalid
438 fc(8, &n
); fc(10, &n
); // logical 0
441 // manchester encode bits 43 to 32
442 for (i
=11; i
>=0; i
--) {
443 if ((i
%4)==3) fc(0,&n
);
445 fc(10, &n
); fc(8, &n
); // low-high transition
447 fc(8, &n
); fc(10, &n
); // high-low transition
452 // manchester encode bits 31 to 0
453 for (i
=31; i
>=0; i
--) {
454 if ((i
%4)==3) fc(0,&n
);
456 fc(10, &n
); fc(8, &n
); // low-high transition
458 fc(8, &n
); fc(10, &n
); // high-low transition
463 SimulateTagLowFrequency(n
);
467 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
468 static void CmdHIDdemodFSK(void)
470 BYTE
*dest
= (BYTE
*)BigBuf
;
471 int m
=0, n
=0, i
=0, idx
=0, found
=0, lastval
=0;
474 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
475 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_READER_USE_125_KHZ
);
477 // Connect the A/D to the peak-detected low-frequency path.
478 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
480 // Give it a bit of time for the resonant antenna to settle.
483 // Now set up the SSC to get the ADC samples that are now streaming at us.
498 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
499 SSC_TRANSMIT_HOLDING
= 0x43;
502 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
503 dest
[i
] = (BYTE
)SSC_RECEIVE_HOLDING
;
504 // we don't care about actual value, only if it's more or less than a
505 // threshold essentially we capture zero crossings for later analysis
506 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
517 // sync to first lo-hi transition
518 for( idx
=1; idx
<m
; idx
++) {
519 if (dest
[idx
-1]<dest
[idx
])
525 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
526 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
527 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
528 for( i
=0; idx
<m
; idx
++) {
529 if (dest
[idx
-1]<dest
[idx
]) {
544 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
549 for( idx
=0; idx
<m
; idx
++) {
550 if (dest
[idx
]==lastval
) {
553 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
554 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
555 // swallowed up by rounding
556 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
557 // special start of frame markers use invalid manchester states (no transitions) by using sequences
560 n
=(n
+1)/6; // fc/8 in sets of 6
562 n
=(n
+1)/5; // fc/10 in sets of 5
564 switch (n
) { // stuff appropriate bits in buffer
567 dest
[i
++]=dest
[idx
-1];
570 dest
[i
++]=dest
[idx
-1];
571 dest
[i
++]=dest
[idx
-1];
573 case 3: // 3 bit start of frame markers
574 dest
[i
++]=dest
[idx
-1];
575 dest
[i
++]=dest
[idx
-1];
576 dest
[i
++]=dest
[idx
-1];
578 // When a logic 0 is immediately followed by the start of the next transmisson
579 // (special pattern) a pattern of 4 bit duration lengths is created.
581 dest
[i
++]=dest
[idx
-1];
582 dest
[i
++]=dest
[idx
-1];
583 dest
[i
++]=dest
[idx
-1];
584 dest
[i
++]=dest
[idx
-1];
586 default: // this shouldn't happen, don't stuff any bits
596 // final loop, go over previously decoded manchester data and decode into usable tag ID
597 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
598 for( idx
=0; idx
<m
-6; idx
++) {
599 // search for a start of frame marker
600 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
604 if (found
&& (hi
|lo
)) {
606 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
613 if (dest
[idx
] && (!dest
[idx
+1]) ) {
616 } else if ( (!dest
[idx
]) && dest
[idx
+1]) {
626 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
630 if (found
&& (hi
|lo
)) {
632 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
643 void SimulateTagHfListen(void)
645 BYTE
*dest
= (BYTE
*)BigBuf
;
646 int n
= sizeof(BigBuf
);
651 // We're using this mode just so that I can test it out; the simulated
652 // tag mode would work just as well and be simpler.
653 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
| FPGA_HF_READER_RX_XCORR_SNOOP
);
655 // We need to listen to the high-frequency, peak-detected path.
656 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
662 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
663 SSC_TRANSMIT_HOLDING
= 0xff;
665 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
666 BYTE r
= (BYTE
)SSC_RECEIVE_HOLDING
;
686 DbpString("simulate tag (now type bitsamples)");
689 void UsbPacketReceived(BYTE
*packet
, int len
)
691 UsbCommand
*c
= (UsbCommand
*)packet
;
694 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
:
695 AcquireRawAdcSamples125k(c
->ext1
);
698 case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
:
699 ModThenAcquireRawAdcSamples125k(c
->ext1
,c
->ext2
,c
->ext3
,c
->d
.asBytes
);
702 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
:
703 AcquireRawAdcSamplesIso15693();
710 case CMD_READER_ISO_15693
:
711 ReaderIso15693(c
->ext1
);
714 case CMD_SIMTAG_ISO_15693
:
715 SimTagIso15693(c
->ext1
);
718 case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
:
719 AcquireRawAdcSamplesIso14443(c
->ext1
);
722 case CMD_READ_SRI512_TAG
:
723 ReadSRI512Iso14443(c
->ext1
);
726 case CMD_READER_ISO_14443a
:
727 ReaderIso14443a(c
->ext1
);
730 case CMD_SNOOP_ISO_14443
:
734 case CMD_SNOOP_ISO_14443a
:
738 case CMD_SIMULATE_TAG_HF_LISTEN
:
739 SimulateTagHfListen();
742 case CMD_SIMULATE_TAG_ISO_14443
:
743 SimulateIso14443Tag();
746 case CMD_SIMULATE_TAG_ISO_14443a
:
747 SimulateIso14443aTag(c
->ext1
, c
->ext2
); // ## Simulate iso14443a tag - pass tag type & UID
750 case CMD_MEASURE_ANTENNA_TUNING
:
751 MeasureAntennaTuning();
754 case CMD_LISTEN_READER_FIELD
:
755 ListenReaderField(c
->ext1
);
758 case CMD_HID_DEMOD_FSK
:
759 CmdHIDdemodFSK(); // Demodulate HID tag
762 case CMD_HID_SIM_TAG
:
763 CmdHIDsimTAG(c
->ext1
, c
->ext2
); // Simulate HID tag by ID
766 case CMD_FPGA_MAJOR_MODE_OFF
: // ## FPGA Control
767 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
769 LED_D_OFF(); // LED D indicates field ON or OFF
772 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
:
773 case CMD_DOWNLOAD_RAW_BITS_TI_TYPE
: {
775 if(c
->cmd
== CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
) {
776 n
.cmd
= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
;
778 n
.cmd
= CMD_DOWNLOADED_RAW_BITS_TI_TYPE
;
781 memcpy(n
.d
.asDwords
, BigBuf
+c
->ext1
, 12*sizeof(DWORD
));
782 UsbSendPacket((BYTE
*)&n
, sizeof(n
));
785 case CMD_DOWNLOADED_SIM_SAMPLES_125K
: {
786 BYTE
*b
= (BYTE
*)BigBuf
;
787 memcpy(b
+c
->ext1
, c
->d
.asBytes
, 48);
790 case CMD_SIMULATE_TAG_125K
:
792 SimulateTagLowFrequency(c
->ext1
);
804 case CMD_SET_LF_DIVISOR
:
805 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, c
->ext1
);
812 case CMD_SETUP_WRITE
:
813 case CMD_FINISH_WRITE
:
814 case CMD_HARDWARE_RESET
:
815 USB_D_PLUS_PULLUP_OFF();
818 RSTC_CONTROL
= RST_CONTROL_KEY
| RST_CONTROL_PROCESSOR_RESET
;
820 // We're going to reset, and the bootrom will take control.
826 DbpString("unknown command");
833 memset(BigBuf
,0,sizeof(BigBuf
));
843 // The FPGA gets its clock from us from PCK0 output, so set that up.
844 PIO_PERIPHERAL_B_SEL
= (1 << GPIO_PCK0
);
845 PIO_DISABLE
= (1 << GPIO_PCK0
);
846 PMC_SYS_CLK_ENABLE
= PMC_SYS_CLK_PROGRAMMABLE_CLK_0
;
847 // PCK0 is PLL clock / 4 = 96Mhz / 4 = 24Mhz
848 PMC_PROGRAMMABLE_CLK_0
= PMC_CLK_SELECTION_PLL_CLOCK
|
849 PMC_CLK_PRESCALE_DIV_4
;
850 PIO_OUTPUT_ENABLE
= (1 << GPIO_PCK0
);
853 SPI_CONTROL
= SPI_CONTROL_RESET
;
855 SSC_CONTROL
= SSC_CONTROL_RESET
;
857 // Load the FPGA image, which we have stored in our flash.
864 // test text on different colored backgrounds
865 LCDString(" The quick brown fox ", &FONT6x8
,1,1+8*0,WHITE
,BLACK
);
866 LCDString(" jumped over the ", &FONT6x8
,1,1+8*1,BLACK
,WHITE
);
867 LCDString(" lazy dog. ", &FONT6x8
,1,1+8*2,YELLOW
,RED
);
868 LCDString(" AaBbCcDdEeFfGgHhIiJj ", &FONT6x8
,1,1+8*3,RED
,GREEN
);
869 LCDString(" KkLlMmNnOoPpQqRrSsTt ", &FONT6x8
,1,1+8*4,MAGENTA
,BLUE
);
870 LCDString("UuVvWwXxYyZz0123456789", &FONT6x8
,1,1+8*5,BLUE
,YELLOW
);
871 LCDString("`-=[]_;',./~!@#$%^&*()", &FONT6x8
,1,1+8*6,BLACK
,CYAN
);
872 LCDString(" _+{}|:\\\"<>? ",&FONT6x8
,1,1+8*7,BLUE
,MAGENTA
);
875 LCDFill(0, 1+8* 8, 132, 8, BLACK
);
876 LCDFill(0, 1+8* 9, 132, 8, WHITE
);
877 LCDFill(0, 1+8*10, 132, 8, RED
);
878 LCDFill(0, 1+8*11, 132, 8, GREEN
);
879 LCDFill(0, 1+8*12, 132, 8, BLUE
);
880 LCDFill(0, 1+8*13, 132, 8, YELLOW
);
881 LCDFill(0, 1+8*14, 132, 8, CYAN
);
882 LCDFill(0, 1+8*15, 132, 8, MAGENTA
);
892 void SpinDelayUs(int us
)
894 int ticks
= (48*us
) >> 10;
896 // Borrow a PWM unit for my real-time clock
897 PWM_ENABLE
= PWM_CHANNEL(0);
898 // 48 MHz / 1024 gives 46.875 kHz
899 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
900 PWM_CH_DUTY_CYCLE(0) = 0;
901 PWM_CH_PERIOD(0) = 0xffff;
903 WORD start
= (WORD
)PWM_CH_COUNTER(0);
906 WORD now
= (WORD
)PWM_CH_COUNTER(0);
907 if(now
== (WORD
)(start
+ ticks
)) {
914 void SpinDelay(int ms
)
916 int ticks
= (48000*ms
) >> 10;
918 // Borrow a PWM unit for my real-time clock
919 PWM_ENABLE
= PWM_CHANNEL(0);
920 // 48 MHz / 1024 gives 46.875 kHz
921 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
922 PWM_CH_DUTY_CYCLE(0) = 0;
923 PWM_CH_PERIOD(0) = 0xffff;
925 WORD start
= (WORD
)PWM_CH_COUNTER(0);
928 WORD now
= (WORD
)PWM_CH_COUNTER(0);
929 if(now
== (WORD
)(start
+ ticks
)) {
936 // listen for external reader
937 void ListenReaderField(int limit
)
939 int lf_av
, lf_av_new
, lf_baseline
= -1, lf_count
= 0;
940 int hf_av
, hf_av_new
, hf_baseline
= -1, hf_count
= 0;
950 lf_av
= ReadAdc(ADC_CHAN_LF
);
952 if(limit
!= HF_ONLY
&& lf_baseline
== -1)
954 DbpString("LF 125/134 Baseline:");
955 DbpIntegers(lf_av
,0,0);
959 hf_av
= ReadAdc(ADC_CHAN_HF
);
962 if (limit
!= LF_ONLY
&& hf_baseline
== -1)
964 DbpString("HF 13.56 Baseline:");
965 DbpIntegers(hf_av
,0,0);
980 if (limit
!= HF_ONLY
)
982 if (abs(lf_av
- lf_baseline
) > 10)
987 lf_av_new
= ReadAdc(ADC_CHAN_LF
);
988 // see if there's a significant change
989 if(abs(lf_av
- lf_av_new
) > 10)
991 DbpString("LF 125/134 Field Change:");
992 DbpIntegers(lf_av
,lf_av_new
,lf_count
);
998 if (limit
!= LF_ONLY
)
1000 if (abs(hf_av
- hf_baseline
) > 10)
1005 hf_av_new
= ReadAdc(ADC_CHAN_HF
);
1006 // see if there's a significant change
1007 if(abs(hf_av
- hf_av_new
) > 10)
1009 DbpString("HF 13.56 Field Change:");
1010 DbpIntegers(hf_av
,hf_av_new
,hf_count
);