diff --git a/lib/bladerf/bladerf_common.cc b/lib/bladerf/bladerf_common.cc index 4f919a1..fb49158 100644 --- a/lib/bladerf/bladerf_common.cc +++ b/lib/bladerf/bladerf_common.cc @@ -40,49 +40,13 @@ #include "bladerf_common.h" -#define BLADERF_FIFO_SIZE_ENV "BLADERF_SAMPLE_FIFO_SIZE" - using namespace boost::assign; boost::mutex bladerf_common::_devs_mutex; std::list > bladerf_common::_devs; -bladerf_common::bladerf_common() : - _is_running(false) -{ - const char *env_fifo_size; - size_t fifo_size; - - 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; - } - } - - _fifo = new boost::circular_buffer(fifo_size); - if (!_fifo) - throw std::runtime_error( std::string(__FUNCTION__) + - " has failed to allocate a sample FIFO!" ); -} - -bladerf_common::~bladerf_common() -{ - delete _fifo; -} +bladerf_common::bladerf_common() : _is_running(false) {} +bladerf_common::~bladerf_common() {} bladerf_sptr bladerf_common:: get_cached_device(struct bladerf_devinfo devinfo) { diff --git a/lib/bladerf/bladerf_common.h b/lib/bladerf/bladerf_common.h index e5152f5..7eb7da5 100644 --- a/lib/bladerf/bladerf_common.h +++ b/lib/bladerf/bladerf_common.h @@ -39,16 +39,6 @@ /* We currently read/write 1024 samples (pairs of 16-bit signed ints) */ #define BLADERF_SAMPLE_BLOCK_SIZE (1024) -/* - * Default size of sample FIFO, in entries. - * This can be overridden by the environment variable BLADERF_SAMPLE_FIFO_SIZE. - */ -#ifndef BLADERF_SAMPLE_FIFO_SIZE -# define BLADERF_SAMPLE_FIFO_SIZE (2 * 1024 * 1024) -#endif - -#define BLADERF_SAMPLE_FIFO_MIN_SIZE (3 * BLADERF_SAMPLE_BLOCK_SIZE) - typedef boost::shared_ptr bladerf_sptr; class bladerf_common @@ -78,12 +68,9 @@ protected: gr::thread::thread _thread; - boost::circular_buffer *_fifo; - boost::mutex _fifo_lock; - boost::condition_variable _samp_avail; - osmosdr::gain_range_t _vga1_range; osmosdr::gain_range_t _vga2_range; + private: bool _is_running; boost::shared_mutex _state_lock; diff --git a/lib/bladerf/bladerf_sink_c.cc b/lib/bladerf/bladerf_sink_c.cc index 14d7931..f6f09b8 100644 --- a/lib/bladerf/bladerf_sink_c.cc +++ b/lib/bladerf/bladerf_sink_c.cc @@ -239,7 +239,7 @@ bladerf_sink_c::~bladerf_sink_c () /* Ensure work() or callbacks return from wait() calls */ _buf_status_lock.lock(); - _samp_avail.notify_all(); + _buffer_filled.notify_all(); _buffer_emptied.notify_all(); _buf_status_lock.unlock(); @@ -296,7 +296,7 @@ void *bladerf_sink_c::get_next_buffer( void *samples, size_t num_samples) /* Wait for our next buffer to become filled */ while ((running = is_running()) && !_filled[_next_to_tx]) { - _samp_avail.wait(lock); + _buffer_filled.wait(lock); } if (running) { @@ -363,7 +363,7 @@ int bladerf_sink_c::work( int noutput_items, _samples_left = _samples_per_buffer; /* Signal that we have filled a buffer */ - _samp_avail.notify_one(); + _buffer_filled.notify_one(); /* Wait here if the next buffer isn't full. The callback will * signal us when it has freed up a buffer */ diff --git a/lib/bladerf/bladerf_sink_c.h b/lib/bladerf/bladerf_sink_c.h index a0e028b..d969264 100644 --- a/lib/bladerf/bladerf_sink_c.h +++ b/lib/bladerf/bladerf_sink_c.h @@ -126,16 +126,16 @@ private: /* members */ bool *_filled; /* Acquire while updating _filled, and signalling/waiting on - * _buffer_emptied and _samples_avail */ + * _buffer_emptied and _buffer_filled */ boost::mutex _buf_status_lock; - /* wait() may block waiting for the TX callbacks to make a buffer availble. - * The callback uses this to signal when it has emptied out a buffer. */ + /* work() may block waiting for the stream callback to empty (consume) a + * buffer. The callback uses this to signal when it has emptied a buffer. */ boost::condition_variable _buffer_emptied; - /* The parent's _samples_avail is used to denote that work() has - * filled a buffer, unblocking a TX callback that's waiting for samples */ - + /* The stream callback may block waiting for work() to fill (produce) a + * buffer. work() uses this to signal that it has filled a buffer. */ + boost::condition_variable _buffer_filled; /* These values are only to be updated and accessed from within work() */ int16_t *_next_value; /* I/Q value insertion point in current buffer */ diff --git a/lib/bladerf/bladerf_source_c.cc b/lib/bladerf/bladerf_source_c.cc index 31b41fb..451ba0e 100644 --- a/lib/bladerf/bladerf_source_c.cc +++ b/lib/bladerf/bladerf_source_c.cc @@ -39,6 +39,14 @@ #include "arg_helpers.h" #include "bladerf_source_c.h" +/* + * Default size of sample FIFO, in entries. + */ +#define BLADERF_SAMPLE_FIFO_SIZE (2 * 1024 * 1024) + +#define BLADERF_SAMPLE_FIFO_MIN_SIZE (3 * BLADERF_SAMPLE_BLOCK_SIZE) + + using namespace boost::assign; /* @@ -74,6 +82,7 @@ bladerf_source_c::bladerf_source_c (const std::string &args) { int ret; unsigned int device_number = 0; + size_t fifo_size; std::string device_name; dict_t dict = params_to_dict(args); @@ -101,6 +110,29 @@ bladerf_source_c::bladerf_source_c (const std::string &args) "failed to open bladeRF device " + device_name ); } + /* Size of FIFO gr_complex samples, in # samples. */ + fifo_size = BLADERF_SAMPLE_FIFO_SIZE; + if (dict.count("fifo")) { + try { + fifo_size = boost::lexical_cast(dict["fifo"]); + } catch (const boost::bad_lexical_cast &e) { + std::cerr << "Warning: \"fifo\" value is invalid. Defaulting to " + << fifo_size; + } + + if (fifo_size < BLADERF_SAMPLE_FIFO_MIN_SIZE) { + fifo_size = BLADERF_SAMPLE_FIFO_MIN_SIZE; + std::cerr << "Warning: \"fifo\" value is too small. Defaulting to " + << BLADERF_SAMPLE_FIFO_MIN_SIZE; + } + } + + _fifo = new boost::circular_buffer(fifo_size); + if (!_fifo) { + throw std::runtime_error( std::string(__FUNCTION__) + + " has failed to allocate a sample FIFO!" ); + } + if (dict.count("fw")) { std::string fw = dict["fw"]; @@ -211,6 +243,8 @@ bladerf_source_c::~bladerf_source_c () /* Release stream resources */ bladerf_deinit_stream(_stream); + + delete _fifo; } void *bladerf_source_c::stream_callback( struct bladerf *dev, diff --git a/lib/bladerf/bladerf_source_c.h b/lib/bladerf/bladerf_source_c.h index e2a43ee..3c5e1b6 100644 --- a/lib/bladerf/bladerf_source_c.h +++ b/lib/bladerf/bladerf_source_c.h @@ -120,6 +120,17 @@ private: /* functions */ private: /* members */ osmosdr::gain_range_t _lna_range; + + /* The stream callback converts SC16Q12 samples from the bladeRF to gr_complex + * values, and adds them to this FIFO. work() fetches the gr_complex values + * from this queue */ + boost::circular_buffer *_fifo; + boost::mutex _fifo_lock; + + /* work() will block if the stream callback hasn't produced samples. The + * callback uses this to notify work of the availability of samples */ + boost::condition_variable _samp_avail; + }; #endif /* INCLUDED_BLADERF_SOURCE_C_H */