/** * 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; } } }
void ReadPCF7931() { uint8_t Blocks[8][17]; uint8_t tmpBlocks[4][16]; int i, j, ind, ind2, n; int num_blocks = 0; int max_blocks = 8; int ident = 0; int error = 0; int tries = 0; memset(Blocks, 0, 8*17*sizeof(uint8_t)); do { memset(tmpBlocks, 0, 4*16*sizeof(uint8_t)); n = DemodPCF7931((uint8_t**)tmpBlocks); if(!n) error++; if(error==10 && num_blocks == 0) { Dbprintf("Error, no tag or bad tag"); return; } else if (tries==20 || error==10) { Dbprintf("Error reading the tag"); Dbprintf("Here is the partial content"); goto end; } for(i=0; i<n; i++) Dbprintf("(dbg) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", tmpBlocks[i][0], tmpBlocks[i][1], tmpBlocks[i][2], tmpBlocks[i][3], tmpBlocks[i][4], tmpBlocks[i][5], tmpBlocks[i][6], tmpBlocks[i][7], tmpBlocks[i][8], tmpBlocks[i][9], tmpBlocks[i][10], tmpBlocks[i][11], tmpBlocks[i][12], tmpBlocks[i][13], tmpBlocks[i][14], tmpBlocks[i][15]); if(!ident) { for(i=0; i<n; i++) { if(IsBlock0PCF7931(tmpBlocks[i])) { // Found block 0 ? if(i < n-1 && IsBlock1PCF7931(tmpBlocks[i+1])) { // Found block 1! // \o/ ident = 1; memcpy(Blocks[0], tmpBlocks[i], 16); Blocks[0][ALLOC] = 1; memcpy(Blocks[1], tmpBlocks[i+1], 16); Blocks[1][ALLOC] = 1; max_blocks = MAX((Blocks[1][14] & 0x7f), Blocks[1][15]) + 1; // Debug print Dbprintf("(dbg) Max blocks: %d", max_blocks); num_blocks = 2; // Handle following blocks for(j=i+2, ind2=2; j!=i; j++, ind2++, num_blocks++) { if(j==n) j=0; if(j==i) break; memcpy(Blocks[ind2], tmpBlocks[j], 16); Blocks[ind2][ALLOC] = 1; } break; } } } } else { for(i=0; i<n; i++) { // Look for identical block in known blocks if(memcmp(tmpBlocks[i], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) { // Block is not full of 00 for(j=0; j<max_blocks; j++) { if(Blocks[j][ALLOC] == 1 && !memcmp(tmpBlocks[i], Blocks[j], 16)) { // Found an identical block for(ind=i-1,ind2=j-1; ind >= 0; ind--,ind2--) { if(ind2 < 0) ind2 = max_blocks; if(!Blocks[ind2][ALLOC]) { // Block ind2 not already found // Dbprintf("Tmp %d -> Block %d", ind, ind2); memcpy(Blocks[ind2], tmpBlocks[ind], 16); Blocks[ind2][ALLOC] = 1; num_blocks++; if(num_blocks == max_blocks) goto end; } } for(ind=i+1,ind2=j+1; ind < n; ind++,ind2++) { if(ind2 > max_blocks) ind2 = 0; if(!Blocks[ind2][ALLOC]) { // Block ind2 not already found // Dbprintf("Tmp %d -> Block %d", ind, ind2); memcpy(Blocks[ind2], tmpBlocks[ind], 16); Blocks[ind2][ALLOC] = 1; num_blocks++; if(num_blocks == max_blocks) goto end; } } } } } } } tries++; if (BUTTON_PRESS()) return; } while (num_blocks != max_blocks); end: Dbprintf("-----------------------------------------"); Dbprintf("Memory content:"); Dbprintf("-----------------------------------------"); for(i=0; i<max_blocks; i++) { if(Blocks[i][ALLOC]==1) Dbprintf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", Blocks[i][0], Blocks[i][1], Blocks[i][2], Blocks[i][3], Blocks[i][4], Blocks[i][5], Blocks[i][6], Blocks[i][7], Blocks[i][8], Blocks[i][9], Blocks[i][10], Blocks[i][11], Blocks[i][12], Blocks[i][13], Blocks[i][14], Blocks[i][15]); else Dbprintf("<missing block %d>", i); } Dbprintf("-----------------------------------------"); cmd_send(CMD_ACK,0,0,0,0,0); }