Skip to content

Commit 050aa18

Browse files
authored
RDV40 compatibility fixes (#678)
* detect and use RDV40 higher voltage ADC channel for hw tune, hf tune, hw detectreader * fix mode switching in hw detectreader * detect Smartcard Slot in hw version * i2c changes from https://github.com/RfidResearchGroup/proxmark3 * some formatting in proxmark3.h
1 parent 2758d83 commit 050aa18

File tree

8 files changed

+358
-250
lines changed

8 files changed

+358
-250
lines changed

armsrc/Makefile

+2-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ SRC_ISO14443a = epa.c iso14443a.c mifareutil.c mifarecmd.c mifaresniff.c mifares
2626
SRC_ISO14443b = iso14443b.c
2727
SRC_CRAPTO1 = crypto1.c des.c
2828
SRC_CRC = iso14443crc.c crc.c crc16.c crc32.c parity.c
29-
ifneq (,$(findstring WITH_SMARTCARD,$(APP_CFLAGS)))
30-
SRC_SMARTCARD = i2c.c
31-
else
32-
SRC_SMARTCARD =
33-
endif
29+
SRC_SMARTCARD = i2c.c
30+
3431
#the FPGA bitstream files. Note: order matters!
3532
FPGA_BITSTREAMS = fpga_lf.bit fpga_hf.bit
3633

armsrc/appmain.c

+61-34
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@
2828
#include "BigBuf.h"
2929
#include "mifareutil.h"
3030
#include "pcf7931.h"
31+
#include "i2c.h"
3132
#ifdef WITH_LCD
3233
#include "LCD.h"
3334
#endif
34-
#ifdef WITH_SMARTCARD
35-
#include "i2c.h"
36-
#endif
3735

3836

3937
// Craig Young - 14a stand-alone code
@@ -143,7 +141,7 @@ void Dbhexdump(int len, uint8_t *d, bool bAsci) {
143141
static int ReadAdc(int ch)
144142
{
145143
// Note: ADC_MODE_PRESCALE and ADC_MODE_SAMPLE_HOLD_TIME are set to the maximum allowed value.
146-
// AMPL_HI is are high impedance (10MOhm || 1MOhm) output, the input capacitance of the ADC is 12pF (typical). This results in a time constant
144+
// AMPL_HI is a high impedance (10MOhm || 1MOhm) output, the input capacitance of the ADC is 12pF (typical). This results in a time constant
147145
// of RC = (0.91MOhm) * 12pF = 10.9us. Even after the maximum configurable sample&hold time of 40us the input capacitor will not be fully charged.
148146
//
149147
// The maths are:
@@ -162,7 +160,7 @@ static int ReadAdc(int ch)
162160

163161
while(!(AT91C_BASE_ADC->ADC_SR & ADC_END_OF_CONVERSION(ch))) {};
164162

165-
return AT91C_BASE_ADC->ADC_CDR[ch];
163+
return AT91C_BASE_ADC->ADC_CDR[ch] & 0x3ff;
166164
}
167165

168166
int AvgAdc(int ch) // was static - merlok
@@ -177,6 +175,26 @@ int AvgAdc(int ch) // was static - merlok
177175
return (a + 15) >> 5;
178176
}
179177

178+
static int AvgAdc_Voltage_HF(void)
179+
{
180+
int AvgAdc_Voltage_Low, AvgAdc_Voltage_High;
181+
182+
AvgAdc_Voltage_Low= (MAX_ADC_HF_VOLTAGE_LOW * AvgAdc(ADC_CHAN_HF_LOW)) >> 10;
183+
// if voltage range is about to be exceeded, use high voltage ADC channel if available (RDV40 only)
184+
if (AvgAdc_Voltage_Low > MAX_ADC_HF_VOLTAGE_LOW - 300) {
185+
AvgAdc_Voltage_High = (MAX_ADC_HF_VOLTAGE_HIGH * AvgAdc(ADC_CHAN_HF_HIGH)) >> 10;
186+
if (AvgAdc_Voltage_High >= AvgAdc_Voltage_Low) {
187+
return AvgAdc_Voltage_High;
188+
}
189+
}
190+
return AvgAdc_Voltage_Low;
191+
}
192+
193+
static int AvgAdc_Voltage_LF(void)
194+
{
195+
return (MAX_ADC_LF_VOLTAGE * AvgAdc(ADC_CHAN_LF)) >> 10;
196+
}
197+
180198
void MeasureAntennaTuningLfOnly(int *vLf125, int *vLf134, int *peakf, int *peakv, uint8_t LF_Results[])
181199
{
182200
int i, adcval = 0, peak = 0;
@@ -198,7 +216,7 @@ void MeasureAntennaTuningLfOnly(int *vLf125, int *vLf134, int *peakf, int *peakv
198216
WDT_HIT();
199217
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, i);
200218
SpinDelay(20);
201-
adcval = ((MAX_ADC_LF_VOLTAGE * AvgAdc(ADC_CHAN_LF)) >> 10);
219+
adcval = AvgAdc_Voltage_LF();
202220
if (i==95) *vLf125 = adcval; // voltage at 125Khz
203221
if (i==89) *vLf134 = adcval; // voltage at 134Khz
204222

@@ -223,9 +241,8 @@ void MeasureAntennaTuningHfOnly(int *vHf)
223241
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
224242
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
225243
SpinDelay(20);
226-
*vHf = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10;
244+
*vHf = AvgAdc_Voltage_HF();
227245
LED_A_OFF();
228-
229246
return;
230247
}
231248

@@ -267,8 +284,8 @@ void MeasureAntennaTuningHf(void)
267284
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
268285

269286
for (;;) {
270-
SpinDelay(20);
271-
vHf = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10;
287+
SpinDelay(500);
288+
vHf = AvgAdc_Voltage_HF();
272289

273290
Dbprintf("%d mV",vHf);
274291
if (BUTTON_PRESS()) break;
@@ -293,6 +310,7 @@ extern struct version_information version_information;
293310
/* bootrom version information is pointed to from _bootphase1_version_pointer */
294311
extern char *_bootphase1_version_pointer, _flash_start, _flash_end, _bootrom_start, _bootrom_end, __data_src_start__;
295312

313+
296314
void SendVersion(void)
297315
{
298316
char temp[USB_CMD_DATA_SIZE]; /* Limited data payload in USB packets */
@@ -315,11 +333,16 @@ void SendVersion(void)
315333

316334
for (int i = 0; i < fpga_bitstream_num; i++) {
317335
strncat(VersionString, fpga_version_information[i], sizeof(VersionString) - strlen(VersionString) - 1);
318-
if (i < fpga_bitstream_num - 1) {
319-
strncat(VersionString, "\n", sizeof(VersionString) - strlen(VersionString) - 1);
320-
}
336+
strncat(VersionString, "\n", sizeof(VersionString) - strlen(VersionString) - 1);
321337
}
322-
338+
339+
// test availability of SmartCard slot
340+
if (I2C_is_available()) {
341+
strncat(VersionString, "SmartCard Slot: available\n", sizeof(VersionString) - strlen(VersionString) - 1);
342+
} else {
343+
strncat(VersionString, "SmartCard Slot: not available\n", sizeof(VersionString) - strlen(VersionString) - 1);
344+
}
345+
323346
// Send Chip ID and used flash memory
324347
uint32_t text_and_rodata_section_size = (uint32_t)&__data_src_start__ - (uint32_t)&_flash_start;
325348
uint32_t compressed_data_section_size = common_area.arg1;
@@ -828,13 +851,15 @@ static const int LIGHT_LEN = sizeof(LIGHT_SCHEME)/sizeof(LIGHT_SCHEME[0]);
828851

829852
void ListenReaderField(int limit)
830853
{
831-
int lf_av, lf_av_new, lf_baseline= 0, lf_max;
832-
int hf_av, hf_av_new, hf_baseline= 0, hf_max;
854+
int lf_av, lf_av_new=0, lf_baseline= 0, lf_max;
855+
int hf_av, hf_av_new=0, hf_baseline= 0, hf_max;
833856
int mode=1, display_val, display_max, i;
834857

835-
#define LF_ONLY 1
836-
#define HF_ONLY 2
837-
#define REPORT_CHANGE 10 // report new values only if they have changed at least by REPORT_CHANGE
858+
#define LF_ONLY 1
859+
#define HF_ONLY 2
860+
#define REPORT_CHANGE_PERCENT 5 // report new values only if they have changed at least by REPORT_CHANGE_PERCENT
861+
#define MIN_HF_FIELD 300 // in mode 1 signal HF field greater than MIN_HF_FIELD above baseline
862+
#define MIN_LF_FIELD 1200 // in mode 1 signal LF field greater than MIN_LF_FIELD above baseline
838863

839864

840865
// switch off FPGA - we don't want to measure our own signal
@@ -843,23 +868,23 @@ void ListenReaderField(int limit)
843868

844869
LEDsoff();
845870

846-
lf_av = lf_max = AvgAdc(ADC_CHAN_LF);
871+
lf_av = lf_max = AvgAdc_Voltage_LF();
847872

848873
if(limit != HF_ONLY) {
849-
Dbprintf("LF 125/134kHz Baseline: %dmV", (MAX_ADC_LF_VOLTAGE * lf_av) >> 10);
874+
Dbprintf("LF 125/134kHz Baseline: %dmV", lf_av);
850875
lf_baseline = lf_av;
851876
}
852877

853-
hf_av = hf_max = AvgAdc(ADC_CHAN_HF);
854-
878+
hf_av = hf_max = AvgAdc_Voltage_HF();
879+
855880
if (limit != LF_ONLY) {
856-
Dbprintf("HF 13.56MHz Baseline: %dmV", (MAX_ADC_HF_VOLTAGE * hf_av) >> 10);
881+
Dbprintf("HF 13.56MHz Baseline: %dmV", hf_av);
857882
hf_baseline = hf_av;
858883
}
859884

860885
for(;;) {
886+
SpinDelay(500);
861887
if (BUTTON_PRESS()) {
862-
SpinDelay(500);
863888
switch (mode) {
864889
case 1:
865890
mode=2;
@@ -872,21 +897,22 @@ void ListenReaderField(int limit)
872897
return;
873898
break;
874899
}
900+
while (BUTTON_PRESS());
875901
}
876902
WDT_HIT();
877903

878904
if (limit != HF_ONLY) {
879905
if(mode == 1) {
880-
if (ABS(lf_av - lf_baseline) > REPORT_CHANGE)
906+
if (lf_av - lf_baseline > MIN_LF_FIELD)
881907
LED_D_ON();
882908
else
883909
LED_D_OFF();
884910
}
885911

886-
lf_av_new = AvgAdc(ADC_CHAN_LF);
912+
lf_av_new = AvgAdc_Voltage_LF();
887913
// see if there's a significant change
888-
if(ABS(lf_av - lf_av_new) > REPORT_CHANGE) {
889-
Dbprintf("LF 125/134kHz Field Change: %5dmV", (MAX_ADC_LF_VOLTAGE * lf_av_new) >> 10);
914+
if (ABS((lf_av - lf_av_new)*100/(lf_av?lf_av:1)) > REPORT_CHANGE_PERCENT) {
915+
Dbprintf("LF 125/134kHz Field Change: %5dmV", lf_av_new);
890916
lf_av = lf_av_new;
891917
if (lf_av > lf_max)
892918
lf_max = lf_av;
@@ -895,16 +921,17 @@ void ListenReaderField(int limit)
895921

896922
if (limit != LF_ONLY) {
897923
if (mode == 1){
898-
if (ABS(hf_av - hf_baseline) > REPORT_CHANGE)
924+
if (hf_av - hf_baseline > MIN_HF_FIELD)
899925
LED_B_ON();
900926
else
901927
LED_B_OFF();
902928
}
903929

904-
hf_av_new = AvgAdc(ADC_CHAN_HF);
930+
hf_av_new = AvgAdc_Voltage_HF();
931+
905932
// see if there's a significant change
906-
if(ABS(hf_av - hf_av_new) > REPORT_CHANGE) {
907-
Dbprintf("HF 13.56MHz Field Change: %5dmV", (MAX_ADC_HF_VOLTAGE * hf_av_new) >> 10);
933+
if (ABS((hf_av - hf_av_new)*100/(hf_av?hf_av:1)) > REPORT_CHANGE_PERCENT) {
934+
Dbprintf("HF 13.56MHz Field Change: %5dmV", hf_av_new);
908935
hf_av = hf_av_new;
909936
if (hf_av > hf_max)
910937
hf_max = hf_av;
@@ -1436,7 +1463,7 @@ void __attribute__((noreturn)) AppMain(void)
14361463
LED_A_OFF();
14371464

14381465
// Init USB device
1439-
usb_enable();
1466+
usb_enable();
14401467

14411468
// The FPGA gets its clock from us from PCK0 output, so set that up.
14421469
AT91C_BASE_PIOA->PIO_BSR = GPIO_PCK0;

armsrc/apps.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ void Dbprintf(const char *fmt, ...);
4242
void Dbhexdump(int len, uint8_t *d, bool bAsci);
4343

4444
// ADC Vref = 3300mV, and an (10M+1M):1M voltage divider on the HF input can measure voltages up to 36300 mV
45-
#define MAX_ADC_HF_VOLTAGE 36300
45+
#define MAX_ADC_HF_VOLTAGE_LOW 36300
4646
// ADC Vref = 3300mV, and an (10000k+240k):240k voltage divider on the LF input can measure voltages up to 140800 mV
47-
#define MAX_ADC_LF_VOLTAGE 140800
47+
#define MAX_ADC_HF_VOLTAGE_HIGH 140800
48+
#define MAX_ADC_LF_VOLTAGE 140800
4849
int AvgAdc(int ch);
4950

5051
void ToSendStuffBit(int b);

0 commit comments

Comments
 (0)