int bladerf_init_device(struct bladerf *dev) { int status; unsigned int actual; uint32_t val; /* Readback the GPIO values to see if they are default or already set */ status = bladerf_config_gpio_read( dev, &val ); if (status != 0) { log_warning("Failed to read GPIO config, skipping device initialization: %s\n", bladerf_strerror(status)); return 0; } if ((val&0x7f) == 0) { log_verbose( "Default GPIO value found - initializing device\n" ); /* Set the GPIO pins to enable the LMS and select the low band */ bladerf_config_gpio_write( dev, 0x57 ); /* Disable the front ends */ lms_enable_rffe(dev, BLADERF_MODULE_TX, false); lms_enable_rffe(dev, BLADERF_MODULE_RX, false); /* Set the internal LMS register to enable RX and TX */ bladerf_lms_write( dev, 0x05, 0x3e ); /* LMS FAQ: Improve TX spurious emission performance */ bladerf_lms_write( dev, 0x47, 0x40 ); /* LMS FAQ: Improve ADC performance */ bladerf_lms_write( dev, 0x59, 0x29 ); /* LMS FAQ: Common mode voltage for ADC */ bladerf_lms_write( dev, 0x64, 0x36 ); /* LMS FAQ: Higher LNA Gain */ bladerf_lms_write( dev, 0x79, 0x37 ); /* Set a default saplerate */ bladerf_set_sample_rate( dev, BLADERF_MODULE_TX, 1000000, &actual ); bladerf_set_sample_rate( dev, BLADERF_MODULE_RX, 1000000, &actual ); /* Set a default frequency of 1GHz */ bladerf_set_frequency( dev, BLADERF_MODULE_TX, 1000000000 ); bladerf_set_frequency( dev, BLADERF_MODULE_RX, 1000000000 ); /* Set the calibrated VCTCXO DAC value */ bladerf_dac_write( dev, dev->dac_trim ); } /* TODO: Read this return from the SPI calls */ return 0; }
int bladerf_init_device(struct bladerf *dev) { unsigned int actual; uint32_t val; /* Readback the GPIO values to see if they are default or already set */ bladerf_config_gpio_read( dev, &val ); if (val == 0) { log_verbose( "Default GPIO value found - initializing device\n" ); /* Set the GPIO pins to enable the LMS and select the low band */ bladerf_config_gpio_write( dev, 0x57 ); /* Set the internal LMS register to enable RX and TX */ bladerf_lms_write( dev, 0x05, 0x3e ); /* LMS FAQ: Improve TX spurious emission performance */ bladerf_lms_write( dev, 0x47, 0x40 ); /* LMS FAQ: Improve ADC performance */ bladerf_lms_write( dev, 0x59, 0x29 ); /* LMS FAQ: Common mode voltage for ADC */ bladerf_lms_write( dev, 0x64, 0x36 ); /* LMS FAQ: Higher LNA Gain */ bladerf_lms_write( dev, 0x79, 0x37 ); /* FPGA workaround: Set IQ polarity for RX */ bladerf_lms_write( dev, 0x5a, 0xa0 ); /* Set a default saplerate */ bladerf_set_sample_rate( dev, BLADERF_MODULE_TX, 1000000, &actual ); bladerf_set_sample_rate( dev, BLADERF_MODULE_RX, 1000000, &actual ); /* Enable TX and RX */ bladerf_enable_module( dev, BLADERF_MODULE_TX, false ); bladerf_enable_module( dev, BLADERF_MODULE_RX, false ); /* Set a default frequency of 1GHz */ bladerf_set_frequency( dev, BLADERF_MODULE_TX, 1000000000 ); bladerf_set_frequency( dev, BLADERF_MODULE_RX, 1000000000 ); /* Set the calibrated VCTCXO DAC value */ bladerf_dac_write( dev, dev->dac_trim ); } /* TODO: Read this return from the SPI calls */ return 0; }
double rf_blade_set_rx_srate(void *h, double freq) { uint32_t bw; rf_blade_handler_t *handler = (rf_blade_handler_t*) h; int status = bladerf_set_sample_rate(handler->dev, BLADERF_MODULE_RX, (uint32_t) freq, &handler->rx_rate); if (status != 0) { fprintf(stderr, "Failed to set samplerate = %u: %s\n", (uint32_t) freq, bladerf_strerror(status)); return -1; } if (handler->rx_rate < 2000000) { status = bladerf_set_bandwidth(handler->dev, BLADERF_MODULE_RX, handler->rx_rate, &bw); if (status != 0) { fprintf(stderr, "Failed to set bandwidth = %u: %s\n", handler->rx_rate, bladerf_strerror(status)); return -1; } } else { status = bladerf_set_bandwidth(handler->dev, BLADERF_MODULE_RX, handler->rx_rate*0.8, &bw); if (status != 0) { fprintf(stderr, "Failed to set bandwidth = %u: %s\n", handler->rx_rate, bladerf_strerror(status)); return -1; } } printf("Set RX sampling rate %.2f Mhz, filter BW: %.2f Mhz\n", (float) handler->rx_rate/1e6, (float) bw/1e6); return (double) handler->rx_rate; }
static inline int backup_and_update_settings(struct bladerf *dev, bladerf_module module, struct settings *settings) { int status; status = bladerf_get_bandwidth(dev, module, &settings->bandwidth); if (status != 0) { return status; } status = bladerf_get_frequency(dev, module, &settings->frequency); if (status != 0) { return status; } status = bladerf_get_rational_sample_rate(dev, module, &settings->samplerate); if (status != 0) { return status; } status = bladerf_set_bandwidth(dev, module, CAL_BANDWIDTH, NULL); if (status != 0) { return status; } status = bladerf_set_sample_rate(dev, module, CAL_SAMPLERATE, NULL); return status; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { struct bladerf *dev; bladerf_module mod; int ret; long long *ip; int rate, actual; if (nrhs < 3) return; ip = (long long *)mxGetPr(prhs[0]); dev = (struct bladerf *)*ip; mod = (bladerf_module)*(double *)mxGetData(prhs[1]); rate = (int)*(double *)mxGetData(prhs[2]); ret = bladerf_set_sample_rate(dev, mod, rate, &actual); if (nlhs >= 2) { plhs[1] = mxCreateNumericMatrix(1,1,mxINT64_CLASS,mxREAL); ip = (long long *)mxGetData(plhs[1]); *ip = actual; } if (nlhs >= 1) { plhs[0] = mxCreateNumericMatrix(1,1,mxINT64_CLASS,mxREAL); ip = (long long *)mxGetData(plhs[0]); *ip = ret; } }
/** [configure_channel] */ int configure_channel(struct bladerf *dev, struct channel_config *c) { int status; status = bladerf_set_frequency(dev, c->channel, c->frequency); if (status != 0) { fprintf(stderr, "Failed to set frequency = %u: %s\n", c->frequency, bladerf_strerror(status)); return status; } status = bladerf_set_sample_rate(dev, c->channel, c->samplerate, NULL); if (status != 0) { fprintf(stderr, "Failed to set samplerate = %u: %s\n", c->samplerate, bladerf_strerror(status)); return status; } status = bladerf_set_bandwidth(dev, c->channel, c->bandwidth, NULL); if (status != 0) { fprintf(stderr, "Failed to set bandwidth = %u: %s\n", c->bandwidth, bladerf_strerror(status)); return status; } status = bladerf_set_gain(dev, c->channel, c->gain); if (status != 0) { fprintf(stderr, "Failed to set gain: %s\n", bladerf_strerror(status)); return status; } return status; }
static inline int apply_params(struct bladerf *dev, struct app_params *p) { int status; randval_init(&p->prng_state, p->prng_seed); status = bladerf_set_sample_rate(dev, BLADERF_MODULE_RX, p->samplerate, NULL); if (status != 0) { fprintf(stderr, "Failed to set RX sample rate: %s\n", bladerf_strerror(status)); return status; } status = bladerf_set_sample_rate(dev, BLADERF_MODULE_TX, p->samplerate, NULL); if (status != 0) { fprintf(stderr, "Failed to set TX sample rate: %s\n", bladerf_strerror(status)); return status; } return 0; }
double rf_blade_set_tx_srate(void *h, double freq) { uint32_t bw; rf_blade_handler_t *handler = (rf_blade_handler_t*) h; int status = bladerf_set_sample_rate(handler->dev, BLADERF_MODULE_TX, (uint32_t) freq, &handler->tx_rate); if (status != 0) { fprintf(stderr, "Failed to set samplerate = %u: %s\n", (uint32_t) freq, bladerf_strerror(status)); return -1; } status = bladerf_set_bandwidth(handler->dev, BLADERF_MODULE_TX, handler->tx_rate, &bw); if (status != 0) { fprintf(stderr, "Failed to set bandwidth = %u: %s\n", handler->tx_rate, bladerf_strerror(status)); return -1; } return (double) handler->tx_rate; }
//! This gets called whenever a parameter is reconfigured void BladeRfTxComponent::parameterHasChanged(std::string name) { try { if(name == "frequency") { // Set center frequency uint32_t actualValue; int ret = bladerf_set_frequency(device_, BLADERF_MODULE_TX, frequency_x); if (ret != 0) { throw IrisException("Failed to set center frequency!"); } bladerf_get_frequency(device_, BLADERF_MODULE_TX, &actualValue); LOG(LINFO) << "Actual Tx center frequency is: " << actualValue << " Hz"; } else if(name == "rate") { uint32_t actualValue; int ret = bladerf_set_sample_rate(device_, BLADERF_MODULE_TX, (uint32_t)rate_x, &actualValue); if (ret != 0) { throw IrisException("Failed to set sample rate!"); } LOG(LINFO) << "Actual Tx sample rate is: " << actualValue << " Hz"; } else if(name == "vga1gain") { if (bladerf_set_txvga1(device_, vga1Gain_x) != 0) { throw IrisException("Failed to set VGA1 gain!"); } int actualGain; bladerf_get_txvga1(device_, &actualGain); LOG(LINFO) << "Actual VGA1 gain is " << actualGain << " dB"; } else if(name == "vga2gain") { if (bladerf_set_txvga2(device_, vga2Gain_x) != 0) { throw IrisException("Failed to set VGA2 gain!"); } int actualGain; bladerf_get_txvga2(device_, &actualGain); LOG(LINFO) << "Actual VGA2 gain is " << actualGain << " dB"; } } catch(std::exception &e) { throw IrisException(e.what()); } }
static int set_and_check(struct bladerf *dev, bladerf_module m, unsigned int rate) { int status; unsigned int actual, readback; status = bladerf_set_sample_rate(dev, m, rate, &actual); if (status != 0) { PR_ERROR("Failed to set sample rate: %s\n", bladerf_strerror(status)); return status; } status = bladerf_get_sample_rate(dev, m, &readback); if (status != 0) { PR_ERROR("Failed to read back sample rate: %s\n", bladerf_strerror(status)); return status; } return 0; }
// Configure RTL-SDR tuner and prepare for streaming. bool BladeRFSource::configure(uint32_t changeFlags, uint32_t sample_rate, uint32_t frequency, uint32_t bandwidth, int lna_gainIndex, int vga1_gain, int vga2_gain) { m_frequency = frequency; m_vga1Gain = vga1_gain; m_vga2Gain = vga2_gain; m_lnaGain = m_lnaGains[lna_gainIndex-1]; if (changeFlags & 0x1) { if (bladerf_set_sample_rate(m_dev, BLADERF_MODULE_RX, sample_rate, &m_actualSampleRate) < 0) { m_error = "Cannot set sample rate"; return false; } } if (changeFlags & 0x2) { if (bladerf_set_frequency( m_dev, BLADERF_MODULE_RX, frequency ) != 0) { m_error = "Cannot set Rx frequency"; return false; } } if (changeFlags & 0x4) { if (bladerf_set_bandwidth(m_dev, BLADERF_MODULE_RX, bandwidth, &m_actualBandwidth) < 0) { m_error = "Cannot set Rx bandwidth"; return false; } } if (changeFlags & 0x8) { if (bladerf_set_lna_gain(m_dev, static_cast<bladerf_lna_gain>(lna_gainIndex)) != 0) { m_error = "Cannot set LNA gain"; return false; } } if (changeFlags & 0x10) { if (bladerf_set_rxvga1(m_dev, vga1_gain) != 0) { m_error = "Cannot set VGA1 gain"; return false; } } if (changeFlags & 0x20) { if (bladerf_set_rxvga2(m_dev, vga2_gain) != 0) { m_error = "Cannot set VGA2 gain"; return false; } } return true; }
int main(int argc, char *argv[]) { sim_t s; char *devstr = NULL; int c; int result; double duration; datetime_t t0; if (argc<3) { usage(); exit(1); } s.opt.navfile[0] = 0; s.opt.umfile[0] = 0; s.opt.g0.week = -1; s.opt.g0.sec = 0.0; s.opt.iduration = USER_MOTION_SIZE; s.opt.verb = TRUE; s.opt.nmeaGGA = FALSE; s.opt.staticLocationMode = TRUE; // default user motion s.opt.llh[0] = 35.274016 / R2D; s.opt.llh[1] = 137.013765 / R2D; s.opt.llh[2] = 100.0; while ((result=getopt(argc,argv,"e:u:g:l:t:d:"))!=-1) { switch (result) { case 'e': strcpy(s.opt.navfile, optarg); break; case 'u': strcpy(s.opt.umfile, optarg); s.opt.nmeaGGA = FALSE; s.opt.staticLocationMode = FALSE; break; case 'g': strcpy(s.opt.umfile, optarg); s.opt.nmeaGGA = TRUE; s.opt.staticLocationMode = FALSE; break; case 'l': // Static geodetic coordinates input mode // Added by [email protected] s.opt.nmeaGGA = FALSE; s.opt.staticLocationMode = TRUE; sscanf(optarg,"%lf,%lf,%lf",&s.opt.llh[0],&s.opt.llh[1],&s.opt.llh[2]); s.opt.llh[0] /= R2D; // convert to RAD s.opt.llh[1] /= R2D; // convert to RAD break; case 't': sscanf(optarg, "%d/%d/%d,%d:%d:%lf", &t0.y, &t0.m, &t0.d, &t0.hh, &t0.mm, &t0.sec); if (t0.y<=1980 || t0.m<1 || t0.m>12 || t0.d<1 || t0.d>31 || t0.hh<0 || t0.hh>23 || t0.mm<0 || t0.mm>59 || t0.sec<0.0 || t0.sec>=60.0) { printf("ERROR: Invalid date and time.\n"); exit(1); } t0.sec = floor(t0.sec); date2gps(&t0, &s.opt.g0); break; case 'd': duration = atof(optarg); if (duration<0.0 || duration>((double)USER_MOTION_SIZE)/10.0) { printf("ERROR: Invalid duration.\n"); exit(1); } s.opt.iduration = (int)(duration*10.0+0.5); break; case ':': case '?': usage(); exit(1); default: break; } } if (s.opt.navfile[0]==0) { printf("ERROR: GPS ephemeris file is not specified.\n"); exit(1); } if (s.opt.umfile[0]==0 && !s.opt.staticLocationMode) { printf("ERROR: User motion file / NMEA GGA stream is not specified.\n"); printf("You may use -l to specify the static location directly.\n"); exit(1); } // Initialize simulator init_sim(&s); // Allocate TX buffer to hold each block of samples to transmit. s.tx.buffer = (int16_t *)malloc(SAMPLES_PER_BUFFER * sizeof(int16_t) * 2); // for 16-bit I and Q samples if (s.tx.buffer == NULL) { fprintf(stderr, "Failed to allocate TX buffer.\n"); goto out; } // Allocate FIFOs to hold 0.1 seconds of I/Q samples each. s.fifo = (int16_t *)malloc(FIFO_LENGTH * sizeof(int16_t) * 2); // for 16-bit I and Q samples if (s.fifo == NULL) { fprintf(stderr, "Failed to allocate I/Q sample buffer.\n"); goto out; } // Initializing device. printf("Opening and initializing device...\n"); s.status = bladerf_open(&s.tx.dev, devstr); if (s.status != 0) { fprintf(stderr, "Failed to open device: %s\n", bladerf_strerror(s.status)); goto out; } s.status = bladerf_set_frequency(s.tx.dev, BLADERF_MODULE_TX, TX_FREQUENCY); if (s.status != 0) { fprintf(stderr, "Faield to set TX frequency: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX frequency: %u Hz\n", TX_FREQUENCY); } s.status = bladerf_set_sample_rate(s.tx.dev, BLADERF_MODULE_TX, TX_SAMPLERATE, NULL); if (s.status != 0) { fprintf(stderr, "Failed to set TX sample rate: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX sample rate: %u sps\n", TX_SAMPLERATE); } s.status = bladerf_set_bandwidth(s.tx.dev, BLADERF_MODULE_TX, TX_BANDWIDTH, NULL); if (s.status != 0) { fprintf(stderr, "Failed to set TX bandwidth: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX bandwidth: %u Hz\n", TX_BANDWIDTH); } s.status = bladerf_set_txvga1(s.tx.dev, TX_VGA1); if (s.status != 0) { fprintf(stderr, "Failed to set TX VGA1 gain: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX VGA1 gain: %d dB\n", TX_VGA1); } s.status = bladerf_set_txvga2(s.tx.dev, TX_VGA2); if (s.status != 0) { fprintf(stderr, "Failed to set TX VGA2 gain: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX VGA2 gain: %d dB\n", TX_VGA2); } // Start GPS task. s.status = start_gps_task(&s); if (s.status < 0) { fprintf(stderr, "Failed to start GPS task.\n"); goto out; } else printf("Creating GPS task...\n"); // Wait until GPS task is initialized pthread_mutex_lock(&(s.tx.lock)); while (!s.gps.ready) pthread_cond_wait(&(s.gps.initialization_done), &(s.tx.lock)); pthread_mutex_unlock(&(s.tx.lock)); // Fillfull the FIFO. if (is_fifo_write_ready(&s)) pthread_cond_signal(&(s.fifo_write_ready)); // Configure the TX module for use with the synchronous interface. s.status = bladerf_sync_config(s.tx.dev, BLADERF_MODULE_TX, BLADERF_FORMAT_SC16_Q11, NUM_BUFFERS, SAMPLES_PER_BUFFER, NUM_TRANSFERS, TIMEOUT_MS); if (s.status != 0) { fprintf(stderr, "Failed to configure TX sync interface: %s\n", bladerf_strerror(s.status)); goto out; } // We must always enable the modules *after* calling bladerf_sync_config(). s.status = bladerf_enable_module(s.tx.dev, BLADERF_MODULE_TX, true); if (s.status != 0) { fprintf(stderr, "Failed to enable TX module: %s\n", bladerf_strerror(s.status)); goto out; } // Start TX task s.status = start_tx_task(&s); if (s.status < 0) { fprintf(stderr, "Failed to start TX task.\n"); goto out; } else printf("Creating TX task...\n"); // Running... printf("Running...\n"); printf("Press 'q' to exit.\n"); while (1) { c = _getch(); if (c=='q') break; } // // TODO: Cleaning up the threads properly. // printf("\nDone!\n"); // Disable TX module, shutting down our underlying TX stream. s.status = bladerf_enable_module(s.tx.dev, BLADERF_MODULE_TX, false); if (s.status != 0) { fprintf(stderr, "Failed to disable TX module: %s\n", bladerf_strerror(s.status)); } out: // Free up resources if (s.tx.buffer != NULL) free(s.tx.buffer); if (s.fifo != NULL) free(s.fifo); printf("Closing device...\n"); bladerf_close(s.tx.dev); return(0); }
/* We've found that running samples through the LMS6 tends to be required * for the TX LPF calibration to converge */ static inline int tx_lpf_dummy_tx(struct bladerf *dev) { int status; int retval = 0; struct bladerf_metadata meta; int16_t zero_sample[] = { 0, 0 }; bladerf_loopback loopback_backup; struct bladerf_rational_rate sample_rate_backup; memset(&meta, 0, sizeof(meta)); status = bladerf_get_loopback(dev, &loopback_backup); if (status != 0) { return status; } status = bladerf_get_rational_sample_rate(dev, BLADERF_MODULE_TX, &sample_rate_backup); if (status != 0) { return status; } status = bladerf_set_loopback(dev, BLADERF_LB_BB_TXVGA1_RXVGA2); if (status != 0) { goto out; } status = bladerf_set_sample_rate(dev, BLADERF_MODULE_TX, 3000000, NULL); if (status != 0) { goto out; } status = bladerf_sync_config(dev, BLADERF_MODULE_TX, BLADERF_FORMAT_SC16_Q11_META, 64, 16384, 16, 1000); if (status != 0) { goto out; } status = bladerf_enable_module(dev, BLADERF_MODULE_TX, true); if (status != 0) { goto out; } meta.flags = BLADERF_META_FLAG_TX_BURST_START | BLADERF_META_FLAG_TX_BURST_END | BLADERF_META_FLAG_TX_NOW; status = bladerf_sync_tx(dev, zero_sample, 1, &meta, 2000); if (status != 0) { goto out; } out: status = bladerf_enable_module(dev, BLADERF_MODULE_TX, false); if (status != 0 && retval == 0) { retval = status; } status = bladerf_set_rational_sample_rate(dev, BLADERF_MODULE_TX, &sample_rate_backup, NULL); if (status != 0 && retval == 0) { retval = status; } status = bladerf_set_loopback(dev, loopback_backup); if (status != 0 && retval == 0) { retval = status; } return retval; }
int main(int argc, char *argv[]) { int status; unsigned int actual; struct bladerf *dev; struct bladerf_stream *stream; struct test_data test_data; bool conv_ok; if (argc != 4 && argc != 5) { fprintf(stderr, "Usage: %s [tx|rx] <samples per buffer> <# buffers> [# samples]\n", argv[0]); return EXIT_FAILURE; } if (strcasecmp(argv[1], "rx") == 0 ) { test_data.module = BLADERF_MODULE_RX ; } else if (strcasecmp(argv[1], "tx") == 0 ) { test_data.module = BLADERF_MODULE_TX; } else { fprintf(stderr, "Invalid module: %s\n", argv[1]); return EXIT_FAILURE; } test_data.idx = 0; test_data.fout = NULL; test_data.samples_per_buffer = str2int(argv[2], 1, INT_MAX, &conv_ok); if (!conv_ok) { fprintf(stderr, "Invalid samples per buffer value: %s\n", argv[2]); return EXIT_FAILURE; } test_data.num_buffers = str2int(argv[3], 1, INT_MAX, &conv_ok); if (!conv_ok) { fprintf(stderr, "Invalid # buffers: %s\n", argv[3]); return EXIT_FAILURE; } if(test_data.module == BLADERF_MODULE_RX && argc == 5) { test_data.samples_left = str2int(argv[4], 1, INT_MAX, &conv_ok); if(!conv_ok) { fprintf(stderr, "Invalid number of samples: %s\n", argv[4]); return EXIT_FAILURE; } } if (signal(SIGINT, handler) == SIG_ERR || signal(SIGTERM, handler) == SIG_ERR) { fprintf(stderr, "Failed to set up signal handler\n"); return EXIT_FAILURE; } status = bladerf_open(&dev, NULL); if (status < 0) { fprintf(stderr, "Failed to open device: %s\n", bladerf_strerror(status)); return EXIT_FAILURE; } status = bladerf_is_fpga_configured(dev); if (status < 0) { fprintf(stderr, "Failed to determine FPGA state: %s\n", bladerf_strerror(status)); return EXIT_FAILURE; } else if (status == 0) { fprintf(stderr, "Error: FPGA is not loaded.\n"); bladerf_close(dev); return EXIT_FAILURE; } if (!status) { status = bladerf_set_frequency(dev, test_data.module, 1000000000); if (status < 0) { fprintf(stderr, "Failed to set frequency: %s\n", bladerf_strerror(status)); bladerf_close(dev); return EXIT_FAILURE; } } if (!status) { status = bladerf_set_sample_rate(dev, test_data.module, 40000000, &actual); if (status < 0) { fprintf(stderr, "Failed to set sample rate: %s\n", bladerf_strerror(status)); bladerf_close(dev); return EXIT_FAILURE; } } /* Initialize the stream */ status = bladerf_init_stream( &stream, dev, stream_callback, &test_data.buffers, test_data.num_buffers, BLADERF_FORMAT_SC16_Q12, test_data.samples_per_buffer, test_data.num_buffers, &test_data ) ; /* Populate buffers with test data */ if( test_data.module == BLADERF_MODULE_TX ) { if (populate_test_data(&test_data) ) { fprintf(stderr, "Failed to populated test data\n"); bladerf_deinit_stream(stream); bladerf_close(dev); return EXIT_FAILURE; } } else { /* Open up file we'll read test data to */ test_data.fout = fopen( "samples.txt", "w" ); if (!test_data.fout) { fprintf(stderr, "Failed to open samples.txt: %s\n", strerror(errno)); bladerf_deinit_stream(stream); bladerf_close(dev); return EXIT_FAILURE; } } status = bladerf_enable_module(dev, test_data.module, true); if (status < 0) { fprintf(stderr, "Failed to enable module: %s\n", bladerf_strerror(status)); } if (!status) { /* Start stream and stay there until we kill the stream */ status = bladerf_stream(stream, test_data.module); if (status < 0) { fprintf(stderr, "Stream error: %s\n", bladerf_strerror(status)); } } status = bladerf_enable_module(dev, test_data.module, false); if (status < 0) { fprintf(stderr, "Failed to enable module: %s\n", bladerf_strerror(status)); } bladerf_deinit_stream(stream); bladerf_close(dev); if (test_data.fout) { fclose(test_data.fout); } return 0; }
/* Initialization and stuff */ int main(int argc, char **argv) { struct bladerf_devinfo *devs; struct sigaction sigact; struct devinfo_s device; int show_help = false; int n, ret; char ch; /* Set up default values, bandwidth and num_transfers * are automatically calculated later */ device.device_id = DEFAULT_DEVICE_ID; device.frequency = DEFAULT_FREQUENCY; device.samplerate = DEFAULT_SAMPLERATE; device.bandwidth = 0; device.txvga1 = DEFAULT_TXVGA1; device.txvga2 = DEFAULT_TXVGA2; device.buffers.gain = DEFAULT_GAIN; device.buffers.again = DEFAULT_AGAIN; device.buffers.num_buffers = DEFAULT_BUFFERS; device.buffers.num_samples = DEFAULT_SAMPLES; device.buffers.num_transfers = 0; device.buffers.pos = 0; /* Evaluate command line options */ while((ch = getopt(argc, argv, "hd:f:r:b:g:G:a:m:n:s:t:")) != -1) { switch(ch) { case 'd': device.device_id = optarg; break; case 'f': device.frequency = atoi(optarg); break; case 'r': device.samplerate = atoi(optarg); break; case 'b': device.bandwidth = atoi(optarg); break; case 'g': device.txvga1 = atoi(optarg); break; case 'G': device.txvga2 = atoi(optarg); break; case 'm': device.buffers.gain = atof(optarg); break; case 'a': device.buffers.again = atof(optarg); break; case 'n': device.buffers.num_buffers = atoi(optarg); break; case 's': device.buffers.num_samples = atoi(optarg); break; case 't': device.buffers.num_transfers = atoi(optarg); break; case 'h': default: show_help = true; } } /* Now calculate bandwidth and num_transfers if the user didn't * configure them manually */ if(device.bandwidth == 0) device.bandwidth = device.samplerate * 3 / 4; if(device.buffers.num_transfers == 0) device.buffers.num_transfers = device.buffers.num_buffers / 2; if(show_help) { usage(argv[0], &device); return EXIT_FAILURE; } argc -= optind; argv += optind; /* Allocate the float input buffer */ device.buffers.fbuf = malloc(device.buffers.num_samples * 2 * sizeof(float)); /* Set up signal handler to enable clean shutdowns */ sigact.sa_handler = sighandler; sigemptyset(&sigact.sa_mask); sigact.sa_flags = 0; sigaction(SIGINT, &sigact, NULL); sigaction(SIGTERM, &sigact, NULL); sigaction(SIGQUIT, &sigact, NULL); sigaction(SIGPIPE, &sigact, NULL); /* Look for devices attached */ ret = bladerf_get_device_list(&devs); if(ret < 1) { fprintf(stderr, "No devices found.\n"); return EXIT_FAILURE; } /* Print some information about all the devices */ for(n = 0; n < ret; n++) { fprintf(stderr, "Serial:\t%s\n" "USB bus:\t%i\n" "USB address:\t%i\n" "Instance:\t%i\n\n", devs[n].serial, devs[n].usb_bus, devs[n].usb_addr, devs[n].instance ); } /* the list is not needed any more */ bladerf_free_device_list(devs); /* Open a device by given device string */ ret = bladerf_open(&device.dev, device.device_id); if(ret != 0) { fprintf(stderr, "Error opening device %s: %s.\n", device.device_id, bladerf_strerror(ret)); goto out0; } else { fprintf(stderr, "Device \"%s\" opened successfully.\n", device.device_id); } /* Set the device parameters */ ret = bladerf_set_sample_rate(device.dev, BLADERF_MODULE_TX, device.samplerate, &device.samplerate); if(ret != 0) { fprintf(stderr, "Error setting sample rate to %i: %s.\n", device.samplerate, bladerf_strerror(ret)); goto out1; } else { fprintf(stderr, "Actual sample rate is %i.\n", device.samplerate); } ret = bladerf_set_frequency(device.dev, BLADERF_MODULE_TX, device.frequency); if(ret != 0) { fprintf(stderr, "Error setting frequency to %iHz: %s.\n", device.frequency, bladerf_strerror(ret)); goto out1; } else { fprintf(stderr, "Frequency set to %iHz.\n", device.frequency); } ret = bladerf_set_txvga1(device.dev, device.txvga1); if(ret != 0) { fprintf(stderr, "Error setting gain for txvga1: %s.\n", bladerf_strerror(ret)); goto out1; } ret = bladerf_set_txvga2(device.dev, device.txvga2); if(ret != 0) { fprintf(stderr, "Error setting gain for txvga2: %s.\n", bladerf_strerror(ret)); goto out1; } ret = bladerf_set_bandwidth(device.dev, BLADERF_MODULE_TX, device.bandwidth, &device.bandwidth); if(ret != 0) { fprintf(stderr, "Error setting LPF bandwidth: %s.\n", bladerf_strerror(ret)); goto out1; } else { fprintf(stderr, "Bandwidth set to %iHz.\n", device.bandwidth); } /* Set up the sample stream */ ret = bladerf_init_stream(&device.stream, device.dev, stream_callback, &device.buffers.sbuf, device.buffers.num_buffers, BLADERF_FORMAT_SC16_Q12, device.buffers.num_samples, device.buffers.num_transfers, &device.buffers); if(ret != 0) { fprintf(stderr, "Failed setting up stream: %s.\n", bladerf_strerror(ret)); goto out1; } /* Finally enable TX... */ ret = bladerf_enable_module(device.dev, BLADERF_MODULE_TX, true); if(ret != 0) { fprintf(stderr, "Error enabling TX module: %s.\n", bladerf_strerror(ret)); goto out1; } else { fprintf(stderr, "Successfully enabled TX module.\n"); } /* ...and start the stream. * Execution stops here until stream has finished. */ ret = bladerf_stream(device.stream, BLADERF_MODULE_TX); if(ret != 0) { fprintf(stderr, "Failed starting stream: %s.\n", bladerf_strerror(ret)); goto out2; } /* Cleanup the mess */ out2: bladerf_deinit_stream(device.stream); out1: ret = bladerf_enable_module(device.dev, BLADERF_MODULE_TX, false); if(ret != 0) { fprintf(stderr, "Error disabling TX module: %s.\n", bladerf_strerror(ret)); } else { fprintf(stderr, "Successfully disabled TX module.\n"); } bladerf_close(device.dev); fprintf(stderr, "Device closed.\n"); out0: return EXIT_SUCCESS; }
static int init_device(struct repeater *repeater, struct repeater_config *config) { int status; unsigned int actual_value; status = bladerf_open(&repeater->device, config->device_str); if (!repeater->device) { fprintf(stderr, "Failed to open %s: %s\n", config->device_str, bladerf_strerror(status)); return -1; } status = bladerf_is_fpga_configured(repeater->device); if (status < 0) { fprintf(stderr, "Failed to determine if FPGA is loaded: %s\n", bladerf_strerror(status)); goto init_device_error; } else if (status == 0) { fprintf(stderr, "FPGA is not loaded. Aborting.\n"); status = BLADERF_ERR_NODEV; goto init_device_error; } status = bladerf_set_bandwidth(repeater->device, BLADERF_MODULE_TX, config->bandwidth, &actual_value); if (status < 0) { fprintf(stderr, "Failed to set TX bandwidth: %s\n", bladerf_strerror(status)); goto init_device_error; } else { printf("Actual TX bandwidth: %d Hz\n", actual_value); } status = bladerf_set_bandwidth(repeater->device, BLADERF_MODULE_RX, config->bandwidth, &actual_value); if (status < 0) { fprintf(stderr, "Failed to set RX bandwidth: %s\n", bladerf_strerror(status)); goto init_device_error; } else { printf("Actual RX bandwidth: %d Hz\n", actual_value); } status = bladerf_set_sample_rate(repeater->device, BLADERF_MODULE_TX, config->sample_rate, &actual_value); if (status < 0) { fprintf(stderr, "Failed to set TX sample rate: %s\n", bladerf_strerror(status)); goto init_device_error; } else { printf("Actual TX sample rate is %d Hz\n", actual_value); } status = bladerf_set_sample_rate(repeater->device, BLADERF_MODULE_RX, config->sample_rate, &actual_value); if (status < 0) { fprintf(stderr, "Failed to set RX sample rate: %s\n", bladerf_strerror(status)); goto init_device_error; } else { printf("Actual RX sample rate is %d Hz\n", actual_value); } status = bladerf_set_frequency(repeater->device, BLADERF_MODULE_TX, config->tx_freq); if (status < 0) { fprintf(stderr, "Failed to set TX frequency: %s\n", bladerf_strerror(status)); goto init_device_error; } else { printf("Set TX frequency to %d Hz\n", config->tx_freq); } status = bladerf_set_frequency(repeater->device, BLADERF_MODULE_RX, config->rx_freq); if (status < 0) { fprintf(stderr, "Failed to set RX frequency: %s\n", bladerf_strerror(status)); goto init_device_error; } else { printf("Set RX frequency to %d Hz\n", config->rx_freq); } status = bladerf_enable_module(repeater->device, BLADERF_MODULE_RX, true); if (status < 0) { fprintf(stderr, "Failed to enable RX module: %s\n", bladerf_strerror(status)); goto init_device_error; } else { printf("Enabled RX module\n"); } status = bladerf_enable_module(repeater->device, BLADERF_MODULE_TX, true); if (status < 0) { bladerf_enable_module(repeater->device, BLADERF_MODULE_RX, false); fprintf(stderr, "Failed to enable TX module: %s\n", bladerf_strerror(status)); goto init_device_error; } else { printf("Enabled TX module\n"); } return status; init_device_error: bladerf_close(repeater->device); repeater->device = NULL; return status; }
/** * Configure RX/TX module */ static int radio_configure_module(struct bladerf *dev, struct module_config *c) { int status; status = bladerf_set_frequency(dev, c->module, c->frequency); if (status != 0) { fprintf(stderr, "Failed to set frequency = %u: %s\n", c->frequency, bladerf_strerror(status)); return status; } status = bladerf_set_sample_rate(dev, c->module, c->samplerate, NULL); if (status != 0) { fprintf(stderr, "Failed to set samplerate = %u: %s\n", c->samplerate, bladerf_strerror(status)); return status; } status = bladerf_set_bandwidth(dev, c->module, c->bandwidth, NULL); if (status != 0) { fprintf(stderr, "Failed to set bandwidth = %u: %s\n", c->bandwidth, bladerf_strerror(status)); return status; } switch (c->module) { case BLADERF_MODULE_RX: /* Configure the gains of the RX LNA, RX VGA1, and RX VGA2 */ status = bladerf_set_lna_gain(dev, c->rx_lna); if (status != 0) { fprintf(stderr, "Failed to set RX LNA gain: %s\n", bladerf_strerror(status)); return status; } status = bladerf_set_rxvga1(dev, c->vga1); if (status != 0) { fprintf(stderr, "Failed to set RX VGA1 gain: %s\n", bladerf_strerror(status)); return status; } status = bladerf_set_rxvga2(dev, c->vga2); if (status != 0) { fprintf(stderr, "Failed to set RX VGA2 gain: %s\n", bladerf_strerror(status)); return status; } break; case BLADERF_MODULE_TX: /* Configure the TX VGA1 and TX VGA2 gains */ status = bladerf_set_txvga1(dev, c->vga1); if (status != 0) { fprintf(stderr, "Failed to set TX VGA1 gain: %s\n", bladerf_strerror(status)); return status; } status = bladerf_set_txvga2(dev, c->vga2); if (status != 0) { fprintf(stderr, "Failed to set TX VGA2 gain: %s\n", bladerf_strerror(status)); return status; } break; default: status = BLADERF_ERR_INVAL; fprintf(stderr, "%s: Invalid module specified (%d)\n", __FUNCTION__, c->module); } return status; }
int main(int argc, char *argv[]) { sim_t s; char *devstr = NULL; int c; // Initialize structures init_sim(&s); // Allocate TX buffer to hold each block of samples to transmit. s.tx.buffer = (int16_t *)malloc(SAMPLES_PER_BUFFER * sizeof(int16_t) * 2); // for 16-bit I and Q samples if (s.tx.buffer == NULL) { fprintf(stderr, "Failed to allocate TX buffer.\n"); goto out; } // Allocate FIFOs to hold 0.1 seconds of I/Q samples each. s.fifo = (int16_t *)malloc(FIFO_LENGTH * sizeof(int16_t) * 2); // for 16-bit I and Q samples if (s.fifo == NULL) { fprintf(stderr, "Failed to allocate I/Q sample buffer.\n"); goto out; } // Initializing device. printf("Opening and initializing device...\n"); s.status = bladerf_open(&s.tx.dev, devstr); if (s.status != 0) { fprintf(stderr, "Failed to open device: %s\n", bladerf_strerror(s.status)); goto out; } s.status = bladerf_set_frequency(s.tx.dev, BLADERF_MODULE_TX, TX_FREQUENCY); if (s.status != 0) { fprintf(stderr, "Faield to set TX frequency: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX frequency: %u Hz\n", TX_FREQUENCY); } s.status = bladerf_set_sample_rate(s.tx.dev, BLADERF_MODULE_TX, TX_SAMPLERATE, NULL); if (s.status != 0) { fprintf(stderr, "Failed to set TX sample rate: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX sample rate: %u sps\n", TX_SAMPLERATE); } s.status = bladerf_set_bandwidth(s.tx.dev, BLADERF_MODULE_TX, TX_BANDWIDTH, NULL); if (s.status != 0) { fprintf(stderr, "Failed to set TX bandwidth: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX bandwidth: %u Hz\n", TX_BANDWIDTH); } s.status = bladerf_set_txvga1(s.tx.dev, TX_VGA1); if (s.status != 0) { fprintf(stderr, "Failed to set TX VGA1 gain: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX VGA1 gain: %d dB\n", TX_VGA1); } s.status = bladerf_set_txvga2(s.tx.dev, TX_VGA2); if (s.status != 0) { fprintf(stderr, "Failed to set TX VGA2 gain: %s\n", bladerf_strerror(s.status)); goto out; } else { printf("TX VGA2 gain: %d dB\n", TX_VGA2); } // Start GPS task. s.status = start_gps_task(&s); if (s.status < 0) { fprintf(stderr, "Failed to start GPS task.\n"); goto out; } else printf("Creating GPS task...\n"); // Wait until GPS task is initialized pthread_mutex_lock(&(s.tx.lock)); while (!s.gps.ready) pthread_cond_wait(&(s.gps.initialization_done), &(s.tx.lock)); pthread_mutex_unlock(&(s.tx.lock)); // Fillfull the FIFO. if (is_fifo_write_ready(&s)) pthread_cond_signal(&(s.fifo_write_ready)); // Configure the TX module for use with the synchronous interface. s.status = bladerf_sync_config(s.tx.dev, BLADERF_MODULE_TX, BLADERF_FORMAT_SC16_Q11, NUM_BUFFERS, SAMPLES_PER_BUFFER, NUM_TRANSFERS, TIMEOUT_MS); if (s.status != 0) { fprintf(stderr, "Failed to configure TX sync interface: %s\n", bladerf_strerror(s.status)); goto out; } // We must always enable the modules *after* calling bladerf_sync_config(). s.status = bladerf_enable_module(s.tx.dev, BLADERF_MODULE_TX, true); if (s.status != 0) { fprintf(stderr, "Failed to enable TX module: %s\n", bladerf_strerror(s.status)); goto out; } // Start TX task s.status = start_tx_task(&s); if (s.status < 0) { fprintf(stderr, "Failed to start TX task.\n"); goto out; } else printf("Creating TX task...\n"); // Running... printf("Running...\n"); printf("Press 'q' to exit.\n"); while (1) { c = _getch(); if (c=='q') break; } // // TODO: Cleaning up the threads properly. // printf("\nDone!\n"); // Disable TX module, shutting down our underlying TX stream. s.status = bladerf_enable_module(s.tx.dev, BLADERF_MODULE_TX, false); if (s.status != 0) { fprintf(stderr, "Failed to disable TX module: %s\n", bladerf_strerror(s.status)); } out: // Free up resources if (s.tx.buffer != NULL) free(s.tx.buffer); if (s.fifo != NULL) free(s.fifo); printf("Closing device...\n"); bladerf_close(s.tx.dev); return(0); }
bool Bladerf1Output::applySettings(const BladeRF1OutputSettings& settings, bool force) { bool forwardChange = false; bool suspendOwnThread = false; bool threadWasRunning = false; QList<QString> reverseAPIKeys; // QMutexLocker mutexLocker(&m_mutex); qDebug() << "BladerfOutput::applySettings: m_dev: " << m_dev; if ((m_settings.m_centerFrequency != settings.m_centerFrequency) || force) { reverseAPIKeys.append("centerFrequency"); } if ((m_settings.m_devSampleRate != settings.m_devSampleRate) || force) { reverseAPIKeys.append("devSampleRate"); } if ((m_settings.m_log2Interp != settings.m_log2Interp) || force) { reverseAPIKeys.append("log2Interp"); } if ((m_settings.m_devSampleRate != settings.m_devSampleRate) || (m_settings.m_log2Interp != settings.m_log2Interp) || force) { suspendOwnThread = true; } if (suspendOwnThread) { if (m_bladerfThread) { if (m_bladerfThread->isRunning()) { m_bladerfThread->stopWork(); threadWasRunning = true; } } } if ((m_settings.m_devSampleRate != settings.m_devSampleRate) || (m_settings.m_log2Interp != settings.m_log2Interp) || force) { int fifoSize; if (settings.m_log2Interp >= 5) { fifoSize = DeviceBladeRF1Shared::m_sampleFifoMinSize32; } else { fifoSize = (std::max)( (int) ((settings.m_devSampleRate/(1<<settings.m_log2Interp)) * DeviceBladeRF1Shared::m_sampleFifoLengthInSeconds), DeviceBladeRF1Shared::m_sampleFifoMinSize); } m_sampleSourceFifo.resize(fifoSize); } if ((m_settings.m_devSampleRate != settings.m_devSampleRate) || force) { forwardChange = true; if (m_dev != 0) { unsigned int actualSamplerate; if (bladerf_set_sample_rate(m_dev, BLADERF_MODULE_TX, settings.m_devSampleRate, &actualSamplerate) < 0) { qCritical("BladerfOutput::applySettings: could not set sample rate: %d", settings.m_devSampleRate); } else { qDebug() << "BladerfOutput::applySettings: bladerf_set_sample_rate(BLADERF_MODULE_TX) actual sample rate is " << actualSamplerate; } } } if ((m_settings.m_log2Interp != settings.m_log2Interp) || force) { forwardChange = true; if (m_bladerfThread != 0) { m_bladerfThread->setLog2Interpolation(settings.m_log2Interp); qDebug() << "BladerfOutput::applySettings: set interpolation to " << (1<<settings.m_log2Interp); } } if ((m_settings.m_vga1 != settings.m_vga1) || force) { reverseAPIKeys.append("vga1"); if (m_dev != 0) { if (bladerf_set_txvga1(m_dev, settings.m_vga1) != 0) { qDebug("BladerfOutput::applySettings: bladerf_set_txvga1() failed"); } else { qDebug() << "BladerfOutput::applySettings: VGA1 gain set to " << settings.m_vga1; } } } if ((m_settings.m_vga2 != settings.m_vga2) || force) { reverseAPIKeys.append("vga2"); if(m_dev != 0) { if (bladerf_set_txvga2(m_dev, settings.m_vga2) != 0) { qDebug("BladerfOutput::applySettings:bladerf_set_rxvga2() failed"); } else { qDebug() << "BladerfOutput::applySettings: VGA2 gain set to " << settings.m_vga2; } } } if ((m_settings.m_xb200 != settings.m_xb200) || force) { reverseAPIKeys.append("xb200"); if (m_dev != 0) { bool changeSettings; if (m_deviceAPI->getSourceBuddies().size() > 0) { DeviceSourceAPI *buddy = m_deviceAPI->getSourceBuddies()[0]; if (buddy->getDeviceSourceEngine()->state() == DSPDeviceSourceEngine::StRunning) { // Tx side running changeSettings = false; } else { changeSettings = true; } } else // No Rx open { changeSettings = true; } if (changeSettings) { if (settings.m_xb200) { if (bladerf_expansion_attach(m_dev, BLADERF_XB_200) != 0) { qDebug("BladerfOutput::applySettings: bladerf_expansion_attach(xb200) failed"); } else { qDebug() << "BladerfOutput::applySettings: Attach XB200"; } } else { if (bladerf_expansion_attach(m_dev, BLADERF_XB_NONE) != 0) { qDebug("BladerfOutput::applySettings: bladerf_expansion_attach(none) failed"); } else { qDebug() << "BladerfOutput::applySettings: Detach XB200"; } } m_sharedParams.m_xb200Attached = settings.m_xb200; } } } if ((m_settings.m_xb200Path != settings.m_xb200Path) || force) { reverseAPIKeys.append("xb200Path"); if (m_dev != 0) { if (bladerf_xb200_set_path(m_dev, BLADERF_MODULE_TX, settings.m_xb200Path) != 0) { qDebug("BladerfOutput::applySettings: bladerf_xb200_set_path(BLADERF_MODULE_TX) failed"); } else { qDebug() << "BladerfOutput::applySettings: set xb200 path to " << settings.m_xb200Path; } } } if ((m_settings.m_xb200Filter != settings.m_xb200Filter) || force) { reverseAPIKeys.append("xb200Filter"); if (m_dev != 0) { if (bladerf_xb200_set_filterbank(m_dev, BLADERF_MODULE_TX, settings.m_xb200Filter) != 0) { qDebug("BladerfOutput::applySettings: bladerf_xb200_set_filterbank(BLADERF_MODULE_TX) failed"); } else { qDebug() << "BladerfOutput::applySettings: set xb200 filter to " << settings.m_xb200Filter; } } } if ((m_settings.m_bandwidth != settings.m_bandwidth) || force) { reverseAPIKeys.append("bandwidth"); if (m_dev != 0) { unsigned int actualBandwidth; if (bladerf_set_bandwidth(m_dev, BLADERF_MODULE_TX, settings.m_bandwidth, &actualBandwidth) < 0) { qCritical("BladerfOutput::applySettings: could not set bandwidth: %d", settings.m_bandwidth); } else { qDebug() << "BladerfOutput::applySettings: bladerf_set_bandwidth(BLADERF_MODULE_TX) actual bandwidth is " << actualBandwidth; } } } if (m_settings.m_centerFrequency != settings.m_centerFrequency) { forwardChange = true; } if (m_dev != 0) { if (bladerf_set_frequency( m_dev, BLADERF_MODULE_TX, settings.m_centerFrequency ) != 0) { qDebug("BladerfOutput::applySettings: bladerf_set_frequency(%lld) failed", settings.m_centerFrequency); } } if (threadWasRunning) { m_bladerfThread->startWork(); } if (settings.m_useReverseAPI) { bool fullUpdate = ((m_settings.m_useReverseAPI != settings.m_useReverseAPI) && settings.m_useReverseAPI) || (m_settings.m_reverseAPIAddress != settings.m_reverseAPIAddress) || (m_settings.m_reverseAPIPort != settings.m_reverseAPIPort) || (m_settings.m_reverseAPIDeviceIndex != settings.m_reverseAPIDeviceIndex); webapiReverseSendSettings(reverseAPIKeys, settings, fullUpdate || force); } m_settings = settings; if (forwardChange) { int sampleRate = m_settings.m_devSampleRate/(1<<m_settings.m_log2Interp); DSPSignalNotification *notif = new DSPSignalNotification(sampleRate, m_settings.m_centerFrequency); m_deviceAPI->getDeviceEngineInputMessageQueue()->push(notif); } qDebug() << "BladerfOutput::applySettings: center freq: " << m_settings.m_centerFrequency << " Hz" << " device sample rate: " << m_settings.m_devSampleRate << "S/s" << " baseband sample rate: " << m_settings.m_devSampleRate/(1<<m_settings.m_log2Interp) << "S/s" << " BW: " << m_settings.m_bandwidth << "Hz"; return true; }
int configure_bladerf(struct bladerf** dev, struct bladerf_config* config) { unsigned int abw, asr; int status; setlocale(LC_NUMERIC, ""); printf("%-50s", "Connecting to device... "); fflush(stdout); status = bladerf_open(dev, NULL); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-50s", "Checking FPGA status... "); fflush(stdout); status = bladerf_is_fpga_configured(*dev); if(status < 0) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } else if(status == 0) { printf(KRED "Failed: FPGA not loaded" KNRM "\n"); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %'13uHz... ", "Tuning TX to:", config->tx_freq); fflush(stdout); status = bladerf_set_frequency(*dev, BLADERF_MODULE_TX, config->tx_freq); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %'13uHz... ", "Tuning RX to:", config->rx_freq); fflush(stdout); status = bladerf_set_frequency(*dev, BLADERF_MODULE_RX, config->rx_freq); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %'13uHz... ", "Setting TX bandwidth to:", config->tx_bw); fflush(stdout); status = bladerf_set_bandwidth(*dev, BLADERF_MODULE_TX, config->tx_bw, &abw); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %'13uHz\n", "Actual bandwidth:", abw); if(abw != config->tx_bw) { printf("Actual bandwidth not equal to desired bandwidth, quitting.\n"); return 1; } printf("%-30s %'13uHz... ", "Setting RX bandwidth to:", config->rx_bw); fflush(stdout); status = bladerf_set_bandwidth(*dev, BLADERF_MODULE_RX, config->rx_bw, &abw); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %'13uHz\n", "Actual bandwidth:", abw); if(abw != config->rx_bw) { printf("Actual bandwidth not equal to desired bandwidth, quitting.\n"); return 1; } printf("%-30s %'12usps... ", "Setting TX sampling rate to:", config->tx_sr); fflush(stdout); status = bladerf_set_sample_rate(*dev, BLADERF_MODULE_TX, config->tx_sr, &asr); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %'12usps\n", "Actual sampling rate:", asr); if(asr != config->tx_sr) { printf("Actual sampling rate not equal to desired sampling rate, " "quitting.\n"); return 1; } printf("%-30s %'12usps... ", "Setting RX sampling rate to:", config->rx_sr); fflush(stdout); status = bladerf_set_sample_rate(*dev, BLADERF_MODULE_RX, config->rx_sr, &asr); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %'12usps\n", "Actual sampling rate:", asr); if(asr != config->rx_sr) { printf("Actual sampling rate not equal to desired sampling rate, " "quitting.\n"); return 1; } printf("%-30s %+13ddB... ", "Setting TXVGA1 gain to:", config->txvga1); fflush(stdout); status = bladerf_set_txvga1(*dev, config->txvga1); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %+13ddB... ", "Setting TXVGA2 gain to:", config->txvga2); fflush(stdout); status = bladerf_set_txvga2(*dev, config->txvga2); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %+13ddB... ", "Setting RXVGA1 gain to:", config->rxvga1); fflush(stdout); status = bladerf_set_rxvga1(*dev, config->rxvga1); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %+13ddB... ", "Setting RXVGA2 gain to:", config->rxvga2); fflush(stdout); status = bladerf_set_rxvga2(*dev, config->rxvga2); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("%-30s %15d... ", "Setting LNA gain to:", config->lna); fflush(stdout); status = bladerf_set_lna_gain(*dev, config->lna); if(status) { printf(KRED "Failed: %s" KNRM "\n", bladerf_strerror(status)); bladerf_close(*dev); return 1; } printf(KGRN "OK" KNRM "\n"); printf("All set up.\n"); return 0; }
//! Do any initialization required void BladeRfTxComponent::initialize() { // Set up the input DataBuffer inBuf_ = castToType< complex<float> >(inputBuffers.at(0)); // Initialize raw sample vector to some multiple of block size rawSampleBuffer_.data.resize(128 * BLADERF_SAMPLE_BLOCK_SIZE); // Set up the BladeRF try { // Create the device LOG(LINFO) << "Trying to open device " << deviceName_x; int ret = bladerf_open(&device_, deviceName_x.c_str()); if (ret != 0) { throw IrisException("Failed to open bladeRF device!"); } // Check whether FPGA is configured yet if (bladerf_is_fpga_configured(device_) != 1 ) { // try to load FPGA image if (not fpgaImage_x.empty()) { ret = bladerf_load_fpga(device_, fpgaImage_x.c_str()); if (ret != 0) { throw IrisException("Failed to load FPGA to bladeRF!"); } else { LOG(LINFO) << "FPGA image successfully loaded."; } } else { throw IrisException("BladeRF FPGA is not configured and no FPGA image given!"); } } // Print some information about device struct bladerf_version version; if (bladerf_fw_version(device_, &version) == 0) { LOG(LINFO) << "Using FW " << version.describe; } if (bladerf_fpga_version(device_, &version) == 0) { LOG(LINFO) << "Using FPGA " << version.describe; } if (bladerf_is_fpga_configured(device_) != 1 ) { throw IrisException("BladeRF FPGA is not configured!"); } // setting up sync config ret = bladerf_sync_config(device_, BLADERF_MODULE_TX, BLADERF_FORMAT_SC16_Q11, BLADERF_DEFAULT_STREAM_BUFFERS, BLADERF_DEFAULT_STREAM_SAMPLES, BLADERF_DEFAULT_STREAM_XFERS, BLADERF_SYNC_TIMEOUT_MS); if (ret != 0) { throw IrisException("Couldn't enable BladeRF Tx sync handle!"); LOG(LERROR) << bladerf_strerror(ret); } // Turn on transmitter ret = bladerf_enable_module(device_, BLADERF_MODULE_TX, true); if ( ret != 0 ) { throw IrisException("Couldn't enable BladeRF Tx module!"); } // Set sample rate uint32_t actualValue; ret = bladerf_set_sample_rate(device_, BLADERF_MODULE_TX, (uint32_t)rate_x, &actualValue); if (ret != 0) { throw IrisException("Failed to set sample rate!"); } LOG(LINFO) << "Actual Tx sample rate is: " << actualValue << " Hz"; // Set center frequency ret = bladerf_set_frequency(device_, BLADERF_MODULE_TX, frequency_x); if (ret != 0) { throw IrisException("Failed to set center frequency!"); } bladerf_get_frequency(device_, BLADERF_MODULE_TX, &actualValue); LOG(LINFO) << "Actual Tx center frequency is: " << actualValue << " Hz"; // Set bandwidth ret = bladerf_set_bandwidth(device_, BLADERF_MODULE_TX, bw_x, &actualValue); if (ret != 0) { throw IrisException("Failed to set receive bandwidth!"); } LOG(LINFO) << "Actual Tx bandwidth is " << actualValue << " Hz"; // Set VGA1 gain int actualGain; ret = bladerf_set_txvga1(device_, vga1Gain_x); if (ret != 0) { throw IrisException("Failed to set VGA1 gain!"); } bladerf_get_txvga1(device_, &actualGain); LOG(LINFO) << "Actual VGA1 gain is " << actualGain << " dB"; // Set VGA2 gain ret = bladerf_set_txvga2(device_, vga2Gain_x); if (ret != 0) { throw IrisException("Failed to set VGA2 gain!"); } bladerf_get_txvga2(device_, &actualGain); LOG(LINFO) << "Actual VGA2 gain is " << actualGain << " dB"; } catch(const boost::exception &e) { throw IrisException(boost::diagnostic_information(e)); } catch(std::exception& e) { throw IrisException(e.what()); } }