// LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8] void LED(int led, int ms) { if (led & LED_RED) LED_C_ON(); if (led & LED_ORANGE) LED_A_ON(); if (led & LED_GREEN) LED_B_ON(); if (led & LED_RED2) LED_D_ON(); if (!ms) return; SpinDelay(ms); if (led & LED_RED) LED_C_OFF(); if (led & LED_ORANGE) LED_A_OFF(); if (led & LED_GREEN) LED_B_OFF(); if (led & LED_RED2) LED_D_OFF(); }
void LEDsoff() { LED_A_OFF(); LED_B_OFF(); LED_C_OFF(); LED_D_OFF(); }
// Returns a demedulated frame or -1 on code violation // // Since TX to RX delay is arbitrary rx_frame has to: // - detect start of frame (first pause) // - forward prng based on ts/TAG_BIT_PERIOD // - receive the frame // - detect end of frame (last pause) static int32_t rx_frame(uint8_t *len) { int32_t frame = 0; // add 2 SSP clock cycles (1 for tx and 1 for rx pipeline delay) // those will be substracted at the end of the rx phase last_frame_end -= 2; // wait for first pause (start of frame) for(uint8_t i = 0; true; ++i) { // increment prng every TAG_BIT_PERIOD last_frame_end += TAG_BIT_PERIOD; legic_prng_forward(1); // if start of frame was received exit delay loop if(wait_for(RWD_PAUSE, last_frame_end)) { last_frame_end = GetCountSspClk(); break; } // check for code violation if(i > RWD_CMD_TIMEOUT) { return -1; } } // receive frame for(*len = 0; true; ++(*len)) { // receive next bit LED_D_ON(); int8_t bit = rx_bit(); LED_D_OFF(); // check for code violation and to short / long frame if((bit < 0) && ((*len < RWD_MIN_FRAME_LEN) || (*len > RWD_MAX_FRAME_LEN))) { return -1; } // check for code violation caused by end of frame if(bit < 0) { break; } // append bit frame |= (bit ^ legic_prng_get_bit()) << (*len); legic_prng_forward(1); } // rx_bit sets coordination timestamp to start of pause, append pause duration // and substract 2 SSP clock cycles (1 for rx and 1 for tx pipeline delay) to // obtain exact end of frame. last_frame_end += RWD_TIME_PAUSE - 2; return frame; }
// Download the fpga image starting at FpgaImage and with length FpgaImageLen bytes // If bytereversal is set: reverse the byte order in each 4-byte word static void DownloadFPGA(const char *FpgaImage, int FpgaImageLen, int bytereversal) { int i=0; AT91C_BASE_PIOA->PIO_OER = GPIO_FPGA_ON; AT91C_BASE_PIOA->PIO_PER = GPIO_FPGA_ON; HIGH(GPIO_FPGA_ON); // ensure everything is powered on SpinDelay(50); LED_D_ON(); // These pins are inputs AT91C_BASE_PIOA->PIO_ODR = GPIO_FPGA_NINIT | GPIO_FPGA_DONE; // PIO controls the following pins AT91C_BASE_PIOA->PIO_PER = GPIO_FPGA_NINIT | GPIO_FPGA_DONE; // Enable pull-ups AT91C_BASE_PIOA->PIO_PPUER = GPIO_FPGA_NINIT | GPIO_FPGA_DONE; // setup initial logic state HIGH(GPIO_FPGA_NPROGRAM); LOW(GPIO_FPGA_CCLK); LOW(GPIO_FPGA_DIN); // These pins are outputs AT91C_BASE_PIOA->PIO_OER = GPIO_FPGA_NPROGRAM | GPIO_FPGA_CCLK | GPIO_FPGA_DIN; // enter FPGA configuration mode LOW(GPIO_FPGA_NPROGRAM); SpinDelay(50); HIGH(GPIO_FPGA_NPROGRAM); i=100000; // wait for FPGA ready to accept data signal while ((i) && ( !(AT91C_BASE_PIOA->PIO_PDSR & GPIO_FPGA_NINIT ) ) ) { i--; } // crude error indicator, leave both red LEDs on and return if (i==0){ LED_C_ON(); LED_D_ON(); return; } if(bytereversal) { /* This is only supported for uint32_t aligned images */ if( ((int)FpgaImage % sizeof(uint32_t)) == 0 ) { i=0; while(FpgaImageLen-->0) DownloadFPGA_byte(FpgaImage[(i++)^0x3]); /* Explanation of the magic in the above line: * i^0x3 inverts the lower two bits of the integer i, counting backwards * for each 4 byte increment. The generated sequence of (i++)^3 is * 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 etc. pp. */ } } else { while(FpgaImageLen-->0) DownloadFPGA_byte(*FpgaImage++); } // continue to clock FPGA until ready signal goes high i=100000; while ( (i--) && ( !(AT91C_BASE_PIOA->PIO_PDSR & GPIO_FPGA_DONE ) ) ) { HIGH(GPIO_FPGA_CCLK); LOW(GPIO_FPGA_CCLK); } // crude error indicator, leave both red LEDs on and return if (i==0){ LED_C_ON(); LED_D_ON(); return; } LED_D_OFF(); }
// Download the fpga image starting at current stream position with length FpgaImageLen bytes static void DownloadFPGA(int bitstream_version, int FpgaImageLen, z_streamp compressed_fpga_stream, uint8_t *output_buffer) { Dbprintf("DownloadFPGA(len: %d)", FpgaImageLen); int i=0; AT91C_BASE_PIOA->PIO_OER = GPIO_FPGA_ON; AT91C_BASE_PIOA->PIO_PER = GPIO_FPGA_ON; HIGH(GPIO_FPGA_ON); // ensure everything is powered on SpinDelay(50); LED_D_ON(); // These pins are inputs AT91C_BASE_PIOA->PIO_ODR = GPIO_FPGA_NINIT | GPIO_FPGA_DONE; // PIO controls the following pins AT91C_BASE_PIOA->PIO_PER = GPIO_FPGA_NINIT | GPIO_FPGA_DONE; // Enable pull-ups AT91C_BASE_PIOA->PIO_PPUER = GPIO_FPGA_NINIT | GPIO_FPGA_DONE; // setup initial logic state HIGH(GPIO_FPGA_NPROGRAM); LOW(GPIO_FPGA_CCLK); LOW(GPIO_FPGA_DIN); // These pins are outputs AT91C_BASE_PIOA->PIO_OER = GPIO_FPGA_NPROGRAM | GPIO_FPGA_CCLK | GPIO_FPGA_DIN; // enter FPGA configuration mode LOW(GPIO_FPGA_NPROGRAM); SpinDelay(50); HIGH(GPIO_FPGA_NPROGRAM); i=100000; // wait for FPGA ready to accept data signal while ((i) && ( !(AT91C_BASE_PIOA->PIO_PDSR & GPIO_FPGA_NINIT ) ) ) { i--; } // crude error indicator, leave both red LEDs on and return if (i==0){ LED_C_ON(); LED_D_ON(); return; } for(i = 0; i < FpgaImageLen; i++) { int b = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer); if (b < 0) { Dbprintf("Error %d during FpgaDownload", b); break; } DownloadFPGA_byte(b); } // continue to clock FPGA until ready signal goes high i=100000; while ( (i--) && ( !(AT91C_BASE_PIOA->PIO_PDSR & GPIO_FPGA_DONE ) ) ) { HIGH(GPIO_FPGA_CCLK); LOW(GPIO_FPGA_CCLK); } // crude error indicator, leave both red LEDs on and return if (i==0){ LED_C_ON(); LED_D_ON(); return; } LED_D_OFF(); }
/** * 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; } } }