return (a + 15) >> 5;
}
+
void MeasureAntennaTuning(void) {
- uint8_t LF_Results[256];
- int i, adcval = 0, peak = 0, peakv = 0, peakf = 0; //ptr = 0
- int vLf125 = 0, vLf134 = 0, vHf = 0; // in mV
+ uint8_t* LF_Results = BigBuf_malloc(256);
+ int i, adcval = 0, peak = 0, peakv = 0, peakf = 0;
+ int vLf125 = 0, vLf134 = 0, vHf = 0; // in mV
LED_B_ON();
/*
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | FPGA_LF_ADC_READER_FIELD);
- for (i=255; i>=19; i--) {
+ for (i = 255; i >= 19; i--) {
WDT_HIT();
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, i);
SpinDelay(20);
peakv = adcval;
peak = LF_Results[i];
peakf = i;
- //ptr = i;
}
}
- for (i=18; i >= 0; i--) LF_Results[i] = 0;
+ for (i = 18; i >= 0; i--) LF_Results[i] = 0;
LED_A_ON();
// Let the FPGA drive the high-frequency antenna around 13.56 MHz.
cmd_send(CMD_MEASURED_ANTENNA_TUNING, vLf125 | (vLf134<<16), vHf, peakf | (peakv<<16), LF_Results, 256);
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+ BigBuf_free(); BigBuf_Clear_ext(false);
LEDsoff();
}
{
UsbCommand *c = (UsbCommand *)packet;
- //Dbprintf("received %d bytes, with command: 0x%04x and args: %d %d %d",len,c->cmd,c->arg[0],c->arg[1],c->arg[2]);
+ //Dbprintf("received %d bytes, with command: 0x%04x and args: %d %d %d",len,c->cmd,c->arg[0],c->arg[1],c->arg[2]);
switch(c->cmd) {
#ifdef WITH_LF
WriteTItag(c->arg[0],c->arg[1],c->arg[2]);
break;
case CMD_SIMULATE_TAG_125K:
- LED_A_ON();
+ LED_A_ON();
SimulateTagLowFrequency(c->arg[0], c->arg[1], 1);
LED_A_OFF();
break;
break;
case CMD_WRITER_LEGIC_RF:
- LegicRfWriter(c->arg[1], c->arg[0]);
+ LegicRfWriter( c->arg[0], c->arg[1], c->arg[2]);
break;
case CMD_RAW_WRITER_LEGIC_RF:
- LegicRfRawWriter(c->arg[0], c->arg[1]);
+ LegicRfRawWriter(c->arg[0], c->arg[1], c->arg[2]);
break;
case CMD_READER_LEGIC_RF:
- LegicRfReader(c->arg[0], c->arg[1]);
+ LegicRfReader(c->arg[0], c->arg[1], c->arg[2]);
break;
#endif
}
common_area.flags.osimage_present = 1;
- LED_D_OFF();
- LED_C_OFF();
- LED_B_OFF();
- LED_A_OFF();
+ LEDsoff();
// Init USB device
usb_enable();
AT91C_BASE_PIOA->PIO_PDR = GPIO_PCK0;
AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_PCK0;
// PCK0 is PLL clock / 4 = 96Mhz / 4 = 24Mhz
- AT91C_BASE_PMC->PMC_PCKR[0] = AT91C_PMC_CSS_PLL_CLK |
- AT91C_PMC_PRES_CLK_4; // 4 for 24Mhz pck0, 2 for 48 MHZ pck0
+ AT91C_BASE_PMC->PMC_PCKR[0] = AT91C_PMC_CSS_PLL_CLK | AT91C_PMC_PRES_CLK_4; // 4 for 24Mhz pck0, 2 for 48 MHZ pck0
AT91C_BASE_PIOA->PIO_OER = GPIO_PCK0;
// Reset SPI
size_t rx_len;
for(;;) {
- if (usb_poll()) {
- rx_len = usb_read(rx,sizeof(UsbCommand));
+ if ( usb_poll_validate_length() ) {
+ rx_len = usb_read(rx, sizeof(UsbCommand));
+
if (rx_len)
- UsbPacketReceived(rx,rx_len);
+ UsbPacketReceived(rx, rx_len);
}
WDT_HIT();
#include "protocols.h"
#include "usb_cdc.h" // for usb_poll_validate_length
+#ifndef SHORT_COIL
+# define SHORT_COIL() LOW(GPIO_SSC_DOUT)
+#endif
+#ifndef OPEN_COIL
+# define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
+#endif
+
/**
* Function to do a modulation and then get samples.
* @param delay_off
void SimulateTagLowFrequency(int period, int gap, int ledcontrol)
{
- int i;
+ int i = 0;
uint8_t *tab = BigBuf_get_addr();
- FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
- FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT);
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_READER_FIELD);
AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT | GPIO_SSC_CLK;
AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
AT91C_BASE_PIOA->PIO_ODR = GPIO_SSC_CLK;
- #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
- #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
-
- i = 0;
for(;;) {
+ WDT_HIT();
+
+ if (ledcontrol) LED_D_ON();
+
//wait until SSC_CLK goes HIGH
while(!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)) {
- if(BUTTON_PRESS() || usb_poll_validate_length() ) {
- DbpString("Stopped");
- return;
- }
WDT_HIT();
+ if ( usb_poll_validate_length() || BUTTON_PRESS() ) {
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+ LED_D_OFF();
+ return;
+ }
}
- if (ledcontrol) LED_D_ON();
-
+
if(tab[i])
OPEN_COIL();
else
//wait until SSC_CLK goes LOW
while(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK) {
- if( BUTTON_PRESS() || usb_poll_validate_length() ) {
- DbpString("Stopped");
- return;
- }
WDT_HIT();
+ if ( usb_poll_validate_length() || BUTTON_PRESS() ) {
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+ LED_D_OFF();
+ return;
+ }
}
i++;
if(i == period) {
-
i = 0;
if (gap) {
+ WDT_HIT();
SHORT_COIL();
- SpinDelayUs(gap);
+ SpinDelayUs(gap);
}
}
}
// simulate a HID tag until the button is pressed
void CmdHIDsimTAG(int hi, int lo, int ledcontrol)
{
- int n=0, i=0;
+ FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
+ set_tracing(FALSE);
+
+ int n = 0, i = 0;
/*
HID tag bitstream format
The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
*/
- if (hi>0xFFF) {
+ if (hi > 0xFFF) {
DbpString("Tags can only have 44 bits. - USE lf simfsk for larger tags");
return;
}
fc(8, &n); fc(10, &n); // high-low transition
}
}
-
+ WDT_HIT();
+
if (ledcontrol) LED_A_ON();
SimulateTagLowFrequency(n, 0, ledcontrol);
if (ledcontrol) LED_A_OFF();
// arg1 contains fcHigh and fcLow, arg2 contains invert and clock
void CmdFSKsimTAG(uint16_t arg1, uint16_t arg2, size_t size, uint8_t *BitStream)
{
- int ledcontrol=1;
- int n=0, i=0;
+ FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
+
+ // free eventually allocated BigBuf memory
+ BigBuf_free(); BigBuf_Clear_ext(false);
+ clear_trace();
+ set_tracing(FALSE);
+
+ int ledcontrol = 1, n = 0, i = 0;
uint8_t fcHigh = arg1 >> 8;
uint8_t fcLow = arg1 & 0xFF;
uint16_t modCnt = 0;
uint8_t invert = (arg2 >> 8) & 1;
for (i=0; i<size; i++){
- if (BitStream[i] == invert){
+
+ if (BitStream[i] == invert)
fcAll(fcLow, &n, clk, &modCnt);
- } else {
+ else
fcAll(fcHigh, &n, clk, &modCnt);
- }
}
- Dbprintf("Simulating with fcHigh: %d, fcLow: %d, clk: %d, invert: %d, n: %d",fcHigh, fcLow, clk, invert, n);
+ WDT_HIT();
+
+ Dbprintf("Simulating with fcHigh: %d, fcLow: %d, clk: %d, invert: %d, n: %d", fcHigh, fcLow, clk, invert, n);
if (ledcontrol) LED_A_ON();
SimulateTagLowFrequency(n, 0, ledcontrol);
// args clock, ask/man or askraw, invert, transmission separator
void CmdASKsimTag(uint16_t arg1, uint16_t arg2, size_t size, uint8_t *BitStream)
{
- int ledcontrol = 1;
- int n=0, i=0;
+ FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
+ set_tracing(FALSE);
+
+ int ledcontrol = 1, n = 0, i = 0;
uint8_t clk = (arg1 >> 8) & 0xFF;
uint8_t encoding = arg1 & 0xFF;
uint8_t separator = arg2 & 1;
uint8_t invert = (arg2 >> 8) & 1;
- if (encoding==2){ //biphase
- uint8_t phase=0;
+ if (encoding == 2){ //biphase
+ uint8_t phase = 0;
for (i=0; i<size; i++){
biphaseSimBit(BitStream[i]^invert, &n, clk, &phase);
}
- if (phase==1) { //run a second set inverted to keep phase in check
+ if (phase == 1) { //run a second set inverted to keep phase in check
for (i=0; i<size; i++){
biphaseSimBit(BitStream[i]^invert, &n, clk, &phase);
}
else if (separator==1)
Dbprintf("sorry but separator option not yet available");
+ WDT_HIT();
+
Dbprintf("Simulating with clk: %d, invert: %d, encoding: %d, separator: %d, n: %d",clk, invert, encoding, separator, n);
if (ledcontrol) LED_A_ON();
// args clock, carrier, invert,
void CmdPSKsimTag(uint16_t arg1, uint16_t arg2, size_t size, uint8_t *BitStream)
{
- int ledcontrol = 1;
- int n=0, i=0;
+ FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
+ set_tracing(FALSE);
+
+ int ledcontrol = 1, n = 0, i = 0;
uint8_t clk = arg1 >> 8;
uint8_t carrier = arg1 & 0xFF;
uint8_t invert = arg2 & 0xFF;
pskSimBit(carrier, &n, clk, &curPhase, TRUE);
}
}
+
+ WDT_HIT();
+
Dbprintf("Simulating with Carrier: %d, clk: %d, invert: %d, n: %d",carrier, clk, invert, n);
if (ledcontrol) LED_A_ON();
return 0;
}
+static int sendPing(void){
+ UsbCommand ping = {CMD_PING, {1, 2, 3}};
+ SendCommand(&ping);
+ SendCommand(&ping);
+ SendCommand(&ping);
+
+ clearCommandBuffer();
+ UsbCommand resp;
+ if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
+ return 0;
+ }
+ return 1;
+}
+
static bool sendTry(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint32_t delay, uint8_t *bs, size_t bs_len){
PrintAndLog("Trying FC: %u; CN: %u", fc, cn);
memcpy(c.d.asBytes, bs, bs_len);
clearCommandBuffer();
SendCommand(&c);
+
msleep(delay);
+ sendPing();
return TRUE;
}
-static int sendPing(){
- UsbCommand resp;
- UsbCommand ping = {CMD_PING};
- clearCommandBuffer(); SendCommand(&ping);
- clearCommandBuffer(); SendCommand(&ping);
- clearCommandBuffer(); SendCommand(&ping);
- if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
- PrintAndLog("aborted via keyboard!");
- return 0;
- }
- PrintAndLog("Device didnt respond to ABORT");
- return 1;
-}
+
int CmdAWIDDemodFSK(const char *Cmd) {
int findone = 0;
for (;;){
+ if ( offline ) {
+ printf("Device offline\n");
+ return 2;
+ }
+
if (ukbhit()) return sendPing();
// Do one up