From e5f7b28093c10f05d272bcf12c6c4b6583af7021 Mon Sep 17 00:00:00 2001 From: Dimitri Stolnikov Date: Sat, 20 Jul 2013 18:14:20 +0200 Subject: bladerf: add support for nuand LLC bladeRF (WIP) This is based on the original work (https://github.com/Nuand/gr-osmosdr) done by folks at nuand LLC for the gr3.6 branch of gr-osmosdr. The following modifications have been done in this commit: * port to gr-osmosdr master codebase (gr3.7) * moved shared properties to bladerf_common * added & verified IF filter bandwidth setters * set LMS6002D registers with values taken from FAQ 5.27 * print device information (serial/versions) on startup * added fpga= and fw= device arguments to program MCU/FPGA * added bladerf=# dev. arg. to select a specific bladeRF * grc gain field controls RF path VGA for RX/TX * grc BB gain field controls BB path VGA for RX/TX Usage example: osmocom_fft -a "bladerf,fpga=/tmp/hostedx115.rbf" The following RX named gain stages are available: LNA: 0 to 6 dB, in 3dB steps VGA1: 5 to 30 dB, in 1dB steps; nonlinear mapping done inside the lib VGA2: 0 to 60 dB, in 3dB steps; not recommended to be used above 30dB The following TX named gain stages are available: VGA1: -35 to -4 dB, in 1dB steps, BB side VGA2: 0 to 25 dB, in 1dB steps, RF side Thanks a lot to the team of nuand LLC for this major contribution. --- lib/bladerf/bladerf_common.cc | 168 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 lib/bladerf/bladerf_common.cc (limited to 'lib/bladerf/bladerf_common.cc') diff --git a/lib/bladerf/bladerf_common.cc b/lib/bladerf/bladerf_common.cc new file mode 100644 index 0000000..d12dd33 --- /dev/null +++ b/lib/bladerf/bladerf_common.cc @@ -0,0 +1,168 @@ +/* -*- c++ -*- */ +/* + * Copyright 2013 Nuand LLC + * Copyright 2013 Dimitri Stolnikov + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +/* + * config.h is generated by configure. It contains the results + * of probing for features, options etc. It should be the first + * file included in your .cc file. + */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include + +#include +#include +#include + +#include "bladerf_common.h" + +#define BLADERF_FIFO_SIZE_ENV "BLADERF_SAMPLE_FIFO_SIZE" + +using namespace boost::assign; + +bladerf_common::bladerf_common() : running(true) +{ + const char *env_fifo_size; + size_t fifo_size; + + /* 1 Sample = i,q (2 int16_t's) */ + this->raw_sample_buf = new int16_t[2 * BLADERF_SAMPLE_BLOCK_SIZE]; + if (!raw_sample_buf) { + throw std::runtime_error( std::string(__FUNCTION__) + + " has failed to allocate a raw sample buffer!" ); + } + + env_fifo_size = getenv(BLADERF_FIFO_SIZE_ENV); + fifo_size = BLADERF_SAMPLE_FIFO_SIZE; + + if (env_fifo_size != NULL) { + try { + fifo_size = boost::lexical_cast(env_fifo_size); + } catch (const boost::bad_lexical_cast &e) { + std::cerr << "Warning: \"" << BLADERF_FIFO_SIZE_ENV + << "\" is invalid" << "... defaulting to " + << fifo_size; + } + + if (fifo_size < BLADERF_SAMPLE_FIFO_MIN_SIZE) { + fifo_size = BLADERF_SAMPLE_FIFO_MIN_SIZE; + std::cerr << "Warning: \"" << BLADERF_FIFO_SIZE_ENV + << "\" is too small" << "... defaulting to " + << BLADERF_SAMPLE_FIFO_MIN_SIZE; + } + } + + this->sample_fifo = new boost::circular_buffer(fifo_size); + if (!this->sample_fifo) + throw std::runtime_error( std::string(__FUNCTION__) + + " has failed to allocate a sample FIFO!" ); +} + +bladerf_common::~bladerf_common() +{ + delete[] this->raw_sample_buf; + delete this->sample_fifo; +} + +void bladerf_common::setup_device() +{ + gpio_write( this->dev, 0x57 ); /* enable LMS RX & TX, select lower band */ + lms_spi_write( this->dev, 0x5a, 0xa0 ); /* polarity of the IQSel signal */ + /* values are taken from LMS6002D FAQ 5.27 */ + lms_spi_write( this->dev, 0x05, 0x3e ); /* enable the tx and rx modules */ + lms_spi_write( this->dev, 0x47, 0x40 ); /* Improving Tx spurious emission performance */ + lms_spi_write( this->dev, 0x59, 0x29 ); /* Improving ADC's performance */ + lms_spi_write( this->dev, 0x64, 0x36 ); /* Common Mode Voltage For ADC's */ + lms_spi_write( this->dev, 0x79, 0x37 ); /* Higher LNA Gain */ + return; +} + +osmosdr::freq_range_t bladerf_common::freq_range() +{ + /* assuming the same for RX & TX */ + return osmosdr::freq_range_t( 300e6, 3.8e9 ); +} + +osmosdr::meta_range_t bladerf_common::sample_rates() +{ + /* assuming the same for RX & TX */ + return osmosdr::meta_range_t( 160e3, 40e6, 1e6 ); +} + +osmosdr::freq_range_t bladerf_common::filter_bandwidths() +{ + /* the same for RX & TX according to the datasheet */ + osmosdr::freq_range_t bandwidths; + + std::vector half_bandwidths; + half_bandwidths += \ + 0.75, 0.875, 1.25, 1.375, 1.5, 1.92, 2.5, + 2.75, 3, 3.5, 4.375, 5, 6, 7, 10, 14; + + BOOST_FOREACH( double half_bws, half_bandwidths ) + bandwidths += osmosdr::range_t( half_bws * 2.e6 ); + + return bandwidths; +} + +std::vector< std::string > bladerf_common::devices() +{ + struct ::bladerf_devinfo *devices; + ssize_t n_devices; + std::vector< std::string > ret; + + n_devices = bladerf_get_device_list(&devices); + + if (n_devices > 0) { + for (ssize_t i = 0; i < n_devices; i++) { + + std::stringstream s; + std::string dev(devices[i].path); + + s << "bladerf=" << dev.substr(dev.find_first_of("01234567890")) << "," + << "label='nuand bladeRF SN " << std::setfill('0') << std::setw(16) + << devices[i].serial << "'"; + + ret.push_back(s.str()); + } + + bladerf_free_device_list(devices, n_devices); + } + + return ret; +} + +bool bladerf_common::is_running() +{ + boost::shared_lock lock(this->state_lock); + return this->running; +} + +void bladerf_common::set_running(bool is_running) +{ + boost::unique_lock lock(this->state_lock); + this->running = is_running; +} -- cgit v1.2.3