Beispiel #1
0
// internal sending function. not a RAMFUNC.
bool intMfSniffSend() {

	int pckSize = 0;
	int pckLen = BigBuf_get_traceLen();
	int pckNum = 0;
	uint8_t *trace = BigBuf_get_addr();
	
	FpgaDisableSscDma();
	while (pckLen > 0) {
		pckSize = MIN(USB_CMD_DATA_SIZE, pckLen);
		LED_B_ON();
		cmd_send(CMD_ACK, 1, BigBuf_get_traceLen(), pckSize, trace + BigBuf_get_traceLen() - pckLen, pckSize);
		LED_B_OFF();
		pckLen -= pckSize;
		pckNum++;
	}

	LED_B_ON();
	cmd_send(CMD_ACK,2,0,0,0,0);
	LED_B_OFF();

	clear_trace();
	
	return TRUE;
}
Beispiel #2
0
static void uncompress_data_section(void)
{
	z_stream data_section;

	next_free_memory = BigBuf_get_addr();
	
	// initialize zstream structure
	data_section.next_in = (uint8_t *) &__data_src_start__;
	data_section.avail_in = &__data_end__ - &__data_start__;  // uncompressed size. Wrong but doesn't matter.
	data_section.next_out = (uint8_t *) &__data_start__;
	data_section.avail_out = &__data_end__ - &__data_start__;  // uncompressed size. Correct.
	data_section.zalloc = &inflate_malloc;
	data_section.zfree = &inflate_free;
	data_section.opaque = NULL;

	// initialize zlib for inflate
	inflateInit2(&data_section, 15);

	// uncompress data segment to RAM
	inflate(&data_section, Z_FINISH);
	
	// save the size of the compressed data section
	common_area.arg1 = data_section.total_in;
}
Beispiel #3
0
static void init_tag() {
  // configure FPGA
  FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
  FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
                  | FPGA_HF_SIMULATOR_MODULATE_212K);
  SetAdcMuxFor(GPIO_MUXSEL_HIPKD);

  // configure SSC with defaults
  FpgaSetupSsc();

  // first pull output to low to prevent glitches then re-claim GPIO_SSC_DOUT
  LOW(GPIO_SSC_DOUT);
  AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
  AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;

  // reserve a cardmem, meaning we can use the tracelog function in bigbuff easier.
  legic_mem = BigBuf_get_addr();

  // init crc calculator
  crc_init(&legic_crc, 4, 0x19 >> 1, 0x05, 0);

  // start 212kHz timer (running from SSP Clock)
  StartCountSspClk();
}
Beispiel #4
0
/**
 * Does the sample acquisition. If threshold is specified, the actual sampling
 * is not commenced until the threshold has been reached.
 * This method implements decimation and quantization in order to
 * be able to provide longer sample traces.
 * Uses the following global settings:
 * @param decimation - how much should the signal be decimated. A decimation of N means we keep 1 in N samples, etc.
 * @param bits_per_sample - bits per sample. Max 8, min 1 bit per sample.
 * @param averaging If set to true, decimation will use averaging, so that if e.g. decimation is 3, the sample
 * value that will be used is the average value of the three samples.
 * @param trigger_threshold - a threshold. The sampling won't commence until this threshold has been reached. Set
 * to -1 to ignore threshold.
 * @param silent - is true, now outputs are made. If false, dbprints the status
 * @return the number of bits occupied by the samples.
 */
uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool averaging, int trigger_threshold,bool silent)
{
	//bigbuf, to hold the aquired raw data signal
	uint8_t *dest = BigBuf_get_addr();
    uint16_t bufsize = BigBuf_max_traceLen();

	BigBuf_Clear_ext(false);

	if(bits_per_sample < 1) bits_per_sample = 1;
	if(bits_per_sample > 8) bits_per_sample = 8;

	if(decimation < 1) decimation = 1;

	// Use a bit stream to handle the output
	BitstreamOut data = { dest , 0, 0};
	int sample_counter = 0;
	uint8_t sample = 0;
	//If we want to do averaging
	uint32_t sample_sum =0 ;
	uint32_t sample_total_numbers =0 ;
	uint32_t sample_total_saved =0 ;

	while(!BUTTON_PRESS() && !usb_poll_validate_length() ) {
		WDT_HIT();
		if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) {
			AT91C_BASE_SSC->SSC_THR = 0x43;
			LED_D_ON();
		}
		if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) {
			sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
			LED_D_OFF();
			// threshold either high or low values 128 = center 0.  if trigger = 178 
			if ((trigger_threshold > 0) && (sample < (trigger_threshold+128)) && (sample > (128-trigger_threshold))) // 
				continue;

			trigger_threshold = 0;
			sample_total_numbers++;

			if(averaging)
			{
				sample_sum += sample;
			}
			//Check decimation
			if(decimation > 1)
			{
				sample_counter++;
				if(sample_counter < decimation) continue;
				sample_counter = 0;
			}
			//Averaging
			if(averaging && decimation > 1) {
				sample = sample_sum / decimation;
				sample_sum =0;
			}
			//Store the sample
			sample_total_saved ++;
			if(bits_per_sample == 8){
				dest[sample_total_saved-1] = sample;
				data.numbits = sample_total_saved << 3;//Get the return value correct
				if(sample_total_saved >= bufsize) break;
			}
			else{
				pushBit(&data, sample & 0x80);
				if(bits_per_sample > 1)	pushBit(&data, sample & 0x40);
				if(bits_per_sample > 2)	pushBit(&data, sample & 0x20);
				if(bits_per_sample > 3)	pushBit(&data, sample & 0x10);
				if(bits_per_sample > 4)	pushBit(&data, sample & 0x08);
				if(bits_per_sample > 5)	pushBit(&data, sample & 0x04);
				if(bits_per_sample > 6)	pushBit(&data, sample & 0x02);
				//Not needed, 8bps is covered above
				//if(bits_per_sample > 7)	pushBit(&data, sample & 0x01);
				if((data.numbits >> 3) +1  >= bufsize) break;
			}
		}
	}
Beispiel #5
0
int DemodPCF7931(uint8_t **outBlocks) {

    uint8_t bits[256] = {0x00};
	uint8_t blocks[8][16];
    uint8_t *dest = BigBuf_get_addr();
    
	int GraphTraceLen = BigBuf_max_traceLen();
	if (  GraphTraceLen > 18000 )
		GraphTraceLen = 18000;
	
	
	int i, j, lastval, bitidx, half_switch;
	int clock = 64;
	int tolerance = clock / 8;
	int pmc, block_done;
	int lc, warnings = 0;
	int num_blocks = 0;
	int lmin=128, lmax=128;
	uint8_t dir;
	//clear read buffer
	BigBuf_Clear_keep_EM();

	LFSetupFPGAForADC(95, true);
	DoAcquisition_default(0, true);

	lmin = 64;
	lmax = 192;

	i = 2;

	/* Find first local max/min */
    if(dest[1] > dest[0]) {
		while(i < GraphTraceLen) {
            if( !(dest[i] > dest[i-1]) && dest[i] > lmax)
				break;
			i++;
		}
		dir = 0;
	}
	else {
		while(i < GraphTraceLen) {
            if( !(dest[i] < dest[i-1]) && dest[i] < lmin)
				break;
			i++;
		}
		dir = 1;
	}

	lastval = i++;
	half_switch = 0;
	pmc = 0;
	block_done = 0;

	for (bitidx = 0; i < GraphTraceLen; i++)
	{
        if ( (dest[i-1] > dest[i] && dir == 1 && dest[i] > lmax) || (dest[i-1] < dest[i] && dir == 0 && dest[i] < lmin))
		{
			lc = i - lastval;
			lastval = i;

			// Switch depending on lc length:
			// Tolerance is 1/8 of clock rate (arbitrary)
			if (ABS(lc-clock/4) < tolerance) {
				// 16T0
				if((i - pmc) == lc) { /* 16T0 was previous one */
					/* It's a PMC ! */
					i += (128+127+16+32+33+16)-1;
					lastval = i;
					pmc = 0;
					block_done = 1;
				}
				else {
					pmc = i;
				}
			} else if (ABS(lc-clock/2) < tolerance) {
				// 32TO
				if((i - pmc) == lc) { /* 16T0 was previous one */
					/* It's a PMC ! */
					i += (128+127+16+32+33)-1;
					lastval = i;
					pmc = 0;
					block_done = 1;
				}
				else if(half_switch == 1) {
                    bits[bitidx++] = 0;
					half_switch = 0;
				}
				else
					half_switch++;
			} else if (ABS(lc-clock) < tolerance) {
				// 64TO
                bits[bitidx++] = 1;
			} else {
				// Error
				warnings++;
				if (warnings > 10)
				{
					Dbprintf("Error: too many detection errors, aborting.");
					return 0;
				}
			}

			if(block_done == 1) {
				if(bitidx == 128) {
					for(j=0; j<16; j++) {
                        blocks[num_blocks][j] = 128*bits[j*8+7]+
                                64*bits[j*8+6]+
                                32*bits[j*8+5]+
                                16*bits[j*8+4]+
                                8*bits[j*8+3]+
                                4*bits[j*8+2]+
                                2*bits[j*8+1]+
                                bits[j*8];
						
					}
					num_blocks++;
				}
				bitidx = 0;
				block_done = 0;
				half_switch = 0;
			}
			if(i < GraphTraceLen)
                dir =(dest[i-1] > dest[i]) ? 0 : 1;
		}
		if(bitidx==255)
			bitidx=0;
		warnings = 0;
		if(num_blocks == 4) break;
	}
    memcpy(outBlocks, blocks, 16*num_blocks);
	return num_blocks;
}