bladerf: formatting/consistency fixes

This commit is contained in:
Ryan Tucker 2017-06-28 14:03:29 -04:00 committed by Dimitri Stolnikov
parent ed4dda192b
commit 399db91385
3 changed files with 643 additions and 600 deletions

File diff suppressed because it is too large Load Diff

View File

@ -45,9 +45,9 @@
//#define DEBUG_BLADERF_SINK //#define DEBUG_BLADERF_SINK
#ifdef DEBUG_BLADERF_SINK #ifdef DEBUG_BLADERF_SINK
# define DBG(input) std::cerr << _pfx << input << std::endl #define DBG(input) std::cerr << _pfx << input << std::endl
#else #else
# define DBG(input) #define DBG(input)
#endif #endif
using namespace boost::assign; using namespace boost::assign;
@ -56,9 +56,9 @@ using namespace boost::assign;
* Create a new instance of bladerf_sink_c and return * Create a new instance of bladerf_sink_c and return
* a boost shared_ptr. This is effectively the public constructor. * a boost shared_ptr. This is effectively the public constructor.
*/ */
bladerf_sink_c_sptr make_bladerf_sink_c (const std::string &args) bladerf_sink_c_sptr make_bladerf_sink_c(const std::string &args)
{ {
return gnuradio::get_initial_sptr(new bladerf_sink_c (args)); return gnuradio::get_initial_sptr(new bladerf_sink_c(args));
} }
/* /*
@ -70,18 +70,18 @@ bladerf_sink_c_sptr make_bladerf_sink_c (const std::string &args)
* are connected to this block. In this case, we accept either * are connected to this block. In this case, we accept either
* 1 or 2 inputs. * 1 or 2 inputs.
*/ */
static const int MIN_IN = 1; // mininum number of input streams static const int MIN_IN = 1; // mininum number of input streams
static const int MAX_IN = 2; // maximum number of input streams static const int MAX_IN = 2; // maximum number of input streams
static const int MIN_OUT = 0; // minimum number of output streams static const int MIN_OUT = 0; // minimum number of output streams
static const int MAX_OUT = 0; // maximum number of output streams static const int MAX_OUT = 0; // maximum number of output streams
/* /*
* The private constructor * The private constructor
*/ */
bladerf_sink_c::bladerf_sink_c (const std::string &args) bladerf_sink_c::bladerf_sink_c(const std::string &args)
: gr::sync_block ("bladerf_sink_c", :gr::sync_block("bladerf_sink_c",
gr::io_signature::make (MIN_IN, MAX_IN, sizeof (gr_complex)), gr::io_signature::make(MIN_IN, MAX_IN, sizeof(gr_complex)),
gr::io_signature::make (MIN_OUT, MAX_OUT, sizeof (gr_complex))) gr::io_signature::make(MIN_OUT, MAX_OUT, sizeof(gr_complex)))
{ {
dict_t dict = params_to_dict(args); dict_t dict = params_to_dict(args);
@ -104,8 +104,8 @@ bool bladerf_sink_c::stop()
int bladerf_sink_c::transmit_with_tags(int noutput_items) int bladerf_sink_c::transmit_with_tags(int noutput_items)
{ {
int status;
int count = 0; int count = 0;
int status = 0;
// For a long burst, we may be transmitting the burst contents over // For a long burst, we may be transmitting the burst contents over
// multiple work calls, so we'll just be sending the entire buffer // multiple work calls, so we'll just be sending the entire buffer
@ -114,7 +114,7 @@ int bladerf_sink_c::transmit_with_tags(int noutput_items)
int end_idx = (noutput_items - 1); int end_idx = (noutput_items - 1);
struct bladerf_metadata meta; struct bladerf_metadata meta;
std::vector<gr::tag_t> tags; std::vector < gr::tag_t > tags;
int16_t zeros[8] = { 0 }; int16_t zeros[8] = { 0 };
@ -135,29 +135,30 @@ int bladerf_sink_c::transmit_with_tags(int noutput_items)
DBG("TX'ing " << noutput_items << " samples in within a burst..."); DBG("TX'ing " << noutput_items << " samples in within a burst...");
return bladerf_sync_tx(_dev.get(), return bladerf_sync_tx(_dev.get(),
static_cast<void *>(_conv_buf), static_cast < void *>(_conv_buf),
noutput_items, &meta, _stream_timeout_ms); noutput_items, &meta, _stream_timeout_ms);
} else { } else {
std::cerr << _pfx << "Dropping " << noutput_items std::cerr << _pfx
<< " samples not in a burst." << std::endl; << "Dropping " << noutput_items << " samples not in a burst."
<< std::endl;
} }
} }
BOOST_FOREACH(gr::tag_t tag, tags) {
BOOST_FOREACH( gr::tag_t tag, tags) {
// Upon seeing an SOB tag, update our offset. We'll TX the start of the // Upon seeing an SOB tag, update our offset. We'll TX the start of the
// burst when we see an EOB or at the end of this function - whichever // burst when we see an EOB or at the end of this function - whichever
// occurs first. // occurs first.
if (pmt::symbol_to_string(tag.key) == "tx_sob") { if (pmt::symbol_to_string(tag.key) == "tx_sob") {
if (_in_burst) { if (_in_burst) {
std::cerr << ("Got SOB while already within a burst"); std::cerr << ("Got SOB while already within a burst") << std::endl;
return BLADERF_ERR_INVAL; return BLADERF_ERR_INVAL;
} else { } else {
start_idx = static_cast<int>(tag.offset - nitems_read(0)); start_idx = static_cast < int >(tag.offset - nitems_read(0));
DBG("Got SOB " << start_idx << " samples into work payload"); DBG("Got SOB " << start_idx << " samples into work payload");
meta.flags |= (BLADERF_META_FLAG_TX_NOW | BLADERF_META_FLAG_TX_BURST_START); meta.flags |=
(BLADERF_META_FLAG_TX_NOW | BLADERF_META_FLAG_TX_BURST_START);
_in_burst = true; _in_burst = true;
} }
@ -166,12 +167,11 @@ int bladerf_sink_c::transmit_with_tags(int noutput_items)
std::cerr << _pfx << "Got EOB while not in burst" << std::endl; std::cerr << _pfx << "Got EOB while not in burst" << std::endl;
return BLADERF_ERR_INVAL; return BLADERF_ERR_INVAL;
} }
// Upon seeing an EOB, transmit what we have and reset our state // Upon seeing an EOB, transmit what we have and reset our state
end_idx = static_cast<int>(tag.offset - nitems_read(0)); end_idx = static_cast < int >(tag.offset - nitems_read(0));
DBG("Got EOB " << end_idx << " samples into work payload"); DBG("Got EOB " << end_idx << " samples into work payload");
if ( (start_idx == INVALID_IDX) || (start_idx > end_idx) ) { if ((start_idx == INVALID_IDX) || (start_idx > end_idx)) {
DBG("Buffer indicies are in an invalid state!"); DBG("Buffer indicies are in an invalid state!");
return BLADERF_ERR_INVAL; return BLADERF_ERR_INVAL;
} }
@ -181,7 +181,7 @@ int bladerf_sink_c::transmit_with_tags(int noutput_items)
DBG("TXing @ EOB [" << start_idx << ":" << end_idx << "]"); DBG("TXing @ EOB [" << start_idx << ":" << end_idx << "]");
status = bladerf_sync_tx(_dev.get(), status = bladerf_sync_tx(_dev.get(),
static_cast<void *>(&_conv_buf[2*start_idx]), static_cast < void *>(&_conv_buf[2 * start_idx]),
count, &meta, _stream_timeout_ms); count, &meta, _stream_timeout_ms);
if (status != 0) { if (status != 0) {
return status; return status;
@ -199,10 +199,9 @@ int bladerf_sink_c::transmit_with_tags(int noutput_items)
meta.flags |= BLADERF_META_FLAG_TX_BURST_END; meta.flags |= BLADERF_META_FLAG_TX_BURST_END;
status = bladerf_sync_tx(_dev.get(), status = bladerf_sync_tx(_dev.get(),
static_cast<void *>(zeros), static_cast < void *>(zeros),
4, &meta, _stream_timeout_ms); 4, &meta, _stream_timeout_ms);
/* Reset our state */ /* Reset our state */
start_idx = INVALID_IDX; start_idx = INVALID_IDX;
end_idx = (noutput_items - 1); end_idx = (noutput_items - 1);
@ -218,25 +217,25 @@ int bladerf_sink_c::transmit_with_tags(int noutput_items)
// We had a start of burst with no end yet - transmit those samples // We had a start of burst with no end yet - transmit those samples
if (_in_burst) { if (_in_burst) {
count = end_idx - start_idx + 1; count = end_idx - start_idx + 1;
DBG("TXing SOB [" << start_idx << ":" << end_idx << "]"); DBG("TXing SOB [" << start_idx << ":" << end_idx << "]");
status = bladerf_sync_tx(_dev.get(), status = bladerf_sync_tx(_dev.get(),
static_cast<void *>(&_conv_buf[2*start_idx]), static_cast < void *>(&_conv_buf[2 * start_idx]),
count, &meta, _stream_timeout_ms); count, &meta, _stream_timeout_ms);
} }
return status; return status;
} }
int bladerf_sink_c::work( int noutput_items, int bladerf_sink_c::work(int noutput_items,
gr_vector_const_void_star &input_items, gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items ) gr_vector_void_star &output_items)
{ {
const gr_complex *in = (const gr_complex *) input_items[0]; const gr_complex *in = (const gr_complex *) input_items[0];
const float scaling = 2000.0f; const float scaling = 2000.0f;
int ret; int status;
if (noutput_items > _conv_buf_size) { if (noutput_items > _conv_buf_size) {
void *tmp; void *tmp;
@ -244,36 +243,37 @@ int bladerf_sink_c::work( int noutput_items,
_conv_buf_size = noutput_items; _conv_buf_size = noutput_items;
tmp = realloc(_conv_buf, _conv_buf_size * 2 * sizeof(int16_t)); tmp = realloc(_conv_buf, _conv_buf_size * 2 * sizeof(int16_t));
if (tmp == NULL) { if (tmp == NULL) {
throw std::runtime_error( std::string(__FUNCTION__) + throw std::runtime_error(_pfx + "Failed to realloc _conv_buf");
"Failed to realloc _conv_buf" );
} else { } else {
DBG("Resized _conv_buf to " << _conv_buf_size << " samples"); DBG("Resized _conv_buf to " << _conv_buf_size << " samples");
} }
_conv_buf = static_cast<int16_t*>(tmp); _conv_buf = static_cast < int16_t * >(tmp);
} }
/* Convert floating point samples into fixed point */ /* Convert floating point samples into fixed point */
volk_32f_s32f_convert_16i(_conv_buf, (float*)in, scaling, 2 * noutput_items); volk_32f_s32f_convert_16i(_conv_buf, (float *) in, scaling,
2 * noutput_items);
if (_use_metadata) { if (_use_metadata) {
ret = transmit_with_tags(noutput_items); status = transmit_with_tags(noutput_items);
} else { } else {
ret = bladerf_sync_tx(_dev.get(), static_cast<void *>(_conv_buf), status = bladerf_sync_tx(_dev.get(), static_cast < void *>(_conv_buf),
noutput_items, NULL, _stream_timeout_ms); noutput_items, NULL, _stream_timeout_ms);
} }
if ( ret != 0 ) { if (status != 0) {
std::cerr << _pfx << "bladerf_sync_tx error: " std::cerr << _pfx
<< bladerf_strerror(ret) << std::endl; << "bladerf_sync_tx error: " << bladerf_strerror(status)
<< std::endl;
_consecutive_failures++; _consecutive_failures++;
if ( _consecutive_failures >= MAX_CONSECUTIVE_FAILURES ) { if (_consecutive_failures >= MAX_CONSECUTIVE_FAILURES) {
noutput_items = WORK_DONE; noutput_items = WORK_DONE;
std::cerr << _pfx std::cerr << _pfx
<< "Consecutive error limit hit. Shutting down." << "Consecutive error limit hit. Shutting down."
<< std::endl; << std::endl;
} }
} else { } else {
_consecutive_failures = 0; _consecutive_failures = 0;
@ -282,8 +282,7 @@ int bladerf_sink_c::work( int noutput_items,
return noutput_items; return noutput_items;
} }
std::vector < std::string > bladerf_sink_c::get_devices()
std::vector<std::string> bladerf_sink_c::get_devices()
{ {
return bladerf_common::devices(); return bladerf_common::devices();
} }
@ -323,66 +322,68 @@ double bladerf_sink_c::get_center_freq(size_t chan)
return bladerf_common::get_center_freq(BLADERF_CHANNEL_TX(chan)); return bladerf_common::get_center_freq(BLADERF_CHANNEL_TX(chan));
} }
double bladerf_sink_c::set_freq_corr( double ppm, size_t chan ) double bladerf_sink_c::set_freq_corr(double ppm, size_t chan)
{ {
/* TODO: Write the VCTCXO with a correction value (also changes RX ppm value!) */ /* TODO: Write the VCTCXO with a correction value (also changes RX ppm value!) */
return get_freq_corr( BLADERF_CHANNEL_TX(chan) ); return get_freq_corr(BLADERF_CHANNEL_TX(chan));
} }
double bladerf_sink_c::get_freq_corr( size_t chan ) double bladerf_sink_c::get_freq_corr(size_t chan)
{ {
/* TODO: Return back the frequency correction in ppm */ /* TODO: Return back the frequency correction in ppm */
return 0; return 0;
} }
std::vector<std::string> bladerf_sink_c::get_gain_names( size_t chan ) std::vector < std::string > bladerf_sink_c::get_gain_names(size_t chan)
{ {
return bladerf_common::get_gain_names(BLADERF_CHANNEL_TX(chan)); return bladerf_common::get_gain_names(BLADERF_CHANNEL_TX(chan));
} }
osmosdr::gain_range_t bladerf_sink_c::get_gain_range( size_t chan ) osmosdr::gain_range_t bladerf_sink_c::get_gain_range(size_t chan)
{ {
return bladerf_common::get_gain_range(BLADERF_CHANNEL_TX(chan)); return bladerf_common::get_gain_range(BLADERF_CHANNEL_TX(chan));
} }
osmosdr::gain_range_t bladerf_sink_c::get_gain_range( const std::string & name, size_t chan ) osmosdr::gain_range_t bladerf_sink_c::get_gain_range(const std::string &name,
size_t chan)
{ {
return bladerf_common::get_gain_range(name, BLADERF_CHANNEL_TX(chan)); return bladerf_common::get_gain_range(name, BLADERF_CHANNEL_TX(chan));
} }
bool bladerf_sink_c::set_gain_mode( bool automatic, size_t chan ) bool bladerf_sink_c::set_gain_mode(bool automatic, size_t chan)
{ {
return bladerf_common::set_gain_mode(automatic, BLADERF_CHANNEL_TX(chan)); return bladerf_common::set_gain_mode(automatic, BLADERF_CHANNEL_TX(chan));
} }
bool bladerf_sink_c::get_gain_mode( size_t chan ) bool bladerf_sink_c::get_gain_mode(size_t chan)
{ {
return bladerf_common::get_gain_mode(BLADERF_CHANNEL_TX(chan)); return bladerf_common::get_gain_mode(BLADERF_CHANNEL_TX(chan));
} }
double bladerf_sink_c::set_gain( double gain, size_t chan ) double bladerf_sink_c::set_gain(double gain, size_t chan)
{ {
return bladerf_common::set_gain(gain, BLADERF_CHANNEL_TX(chan)); return bladerf_common::set_gain(gain, BLADERF_CHANNEL_TX(chan));
} }
double bladerf_sink_c::set_gain( double gain, const std::string & name, size_t chan) double bladerf_sink_c::set_gain(double gain, const std::string &name,
size_t chan)
{ {
return bladerf_common::set_gain(gain, name, BLADERF_CHANNEL_TX(chan)); return bladerf_common::set_gain(gain, name, BLADERF_CHANNEL_TX(chan));
} }
double bladerf_sink_c::get_gain( size_t chan ) double bladerf_sink_c::get_gain(size_t chan)
{ {
return bladerf_common::get_gain(BLADERF_CHANNEL_TX(chan)); return bladerf_common::get_gain(BLADERF_CHANNEL_TX(chan));
} }
double bladerf_sink_c::get_gain( const std::string & name, size_t chan ) double bladerf_sink_c::get_gain(const std::string &name, size_t chan)
{ {
return bladerf_common::get_gain(name, BLADERF_CHANNEL_TX(chan)); return bladerf_common::get_gain(name, BLADERF_CHANNEL_TX(chan));
} }
std::vector< std::string > bladerf_sink_c::get_antennas( size_t chan ) std::vector < std::string > bladerf_sink_c::get_antennas(size_t chan)
{ {
std::vector< std::string > antennas; std::vector < std::string > antennas;
antennas += "TX0"; antennas += "TX0";
@ -393,83 +394,87 @@ std::vector< std::string > bladerf_sink_c::get_antennas( size_t chan )
return antennas; return antennas;
} }
std::string bladerf_sink_c::set_antenna( const std::string & antenna, size_t chan ) std::string bladerf_sink_c::set_antenna(const std::string &antenna,
size_t chan)
{ {
return get_antenna( BLADERF_CHANNEL_TX(chan) ); return get_antenna(BLADERF_CHANNEL_TX(chan));
} }
std::string bladerf_sink_c::get_antenna( size_t chan ) std::string bladerf_sink_c::get_antenna(size_t chan)
{ {
/* We only have a single transmit antenna here */ /* We only have a single transmit antenna here */
// TODO: the above is a lie // TODO: the above is a lie
return "TX0"; return "TX0";
} }
void bladerf_sink_c::set_dc_offset( const std::complex<double> &offset, size_t chan ) void bladerf_sink_c::set_dc_offset(const std::complex < double > &offset,
size_t chan)
{ {
int ret = 0; int status;
ret = bladerf_common::set_dc_offset(BLADERF_TX, offset, chan); status = bladerf_common::set_dc_offset(BLADERF_TX, offset, chan);
if( ret ) { if (status != 0) {
throw std::runtime_error( std::string(__FUNCTION__) + " " + throw std::runtime_error(_pfx + "could not set dc offset: " +
"could not set dc offset: " + bladerf_strerror(status));
std::string(bladerf_strerror(ret)) );
} }
} }
void bladerf_sink_c::set_iq_balance( const std::complex<double> &balance, size_t chan ) void bladerf_sink_c::set_iq_balance(const std::complex < double > &balance,
size_t chan)
{ {
int ret = 0; int status;
ret = bladerf_common::set_iq_balance(BLADERF_TX, balance, chan); status = bladerf_common::set_iq_balance(BLADERF_TX, balance, chan);
if( ret ) { if (status != 0) {
throw std::runtime_error( std::string(__FUNCTION__) + " " + throw std::runtime_error(_pfx + "could not set iq balance: " +
"could not set iq balance: " + bladerf_strerror(status));
std::string(bladerf_strerror(ret)) );
} }
} }
double bladerf_sink_c::set_bandwidth( double bandwidth, size_t chan ) double bladerf_sink_c::set_bandwidth(double bandwidth, size_t chan)
{ {
int ret; int status;
uint32_t actual; uint32_t actual;
if ( bandwidth == 0.0 ) /* bandwidth of 0 means automatic filter selection */ if (bandwidth == 0.0) {
bandwidth = get_sample_rate() * 0.75; /* select narrower filters to prevent aliasing */ /* bandwidth of 0 means automatic filter selection */
/* select narrower filters to prevent aliasing */
bandwidth = get_sample_rate() * 0.75;
}
ret = bladerf_set_bandwidth( _dev.get(), BLADERF_TX, (uint32_t)bandwidth, &actual ); status = bladerf_set_bandwidth(_dev.get(), BLADERF_TX, (uint32_t) bandwidth,
if( ret ) { &actual);
throw std::runtime_error( std::string(__FUNCTION__) + " " + if (status != 0) {
"could not set bandwidth:" + throw std::runtime_error(_pfx + "could not set bandwidth:" +
std::string(bladerf_strerror(ret)) ); bladerf_strerror(status));
} }
return get_bandwidth(); return get_bandwidth();
} }
double bladerf_sink_c::get_bandwidth( size_t chan ) double bladerf_sink_c::get_bandwidth(size_t chan)
{ {
int status;
uint32_t bandwidth; uint32_t bandwidth;
int ret;
ret = bladerf_get_bandwidth( _dev.get(), BLADERF_TX, &bandwidth ); status = bladerf_get_bandwidth(_dev.get(), BLADERF_TX, &bandwidth);
if( ret ) { if (status != 0) {
throw std::runtime_error( std::string(__FUNCTION__) + " " + throw std::runtime_error(_pfx + "could not get bandwidth: " +
"could not get bandwidth: " + bladerf_strerror(status));
std::string(bladerf_strerror(ret)) );
} }
return (double)bandwidth; return (double) bandwidth;
} }
osmosdr::freq_range_t bladerf_sink_c::get_bandwidth_range( size_t chan ) osmosdr::freq_range_t bladerf_sink_c::get_bandwidth_range(size_t chan)
{ {
return filter_bandwidths(); return filter_bandwidths();
} }
void bladerf_sink_c::set_clock_source(const std::string &source, const size_t mboard) void bladerf_sink_c::set_clock_source(const std::string &source,
const size_t mboard)
{ {
bladerf_common::set_clock_source(source, mboard); bladerf_common::set_clock_source(source, mboard);
} }
@ -479,7 +484,7 @@ std::string bladerf_sink_c::get_clock_source(const size_t mboard)
return bladerf_common::get_clock_source(mboard); return bladerf_common::get_clock_source(mboard);
} }
std::vector<std::string> bladerf_sink_c::get_clock_sources(const size_t mboard) std::vector < std::string > bladerf_sink_c::get_clock_sources(const size_t mboard)
{ {
return bladerf_common::get_clock_sources(mboard); return bladerf_common::get_clock_sources(mboard);
} }

View File

@ -48,9 +48,9 @@ using namespace boost::assign;
* Create a new instance of bladerf_source_c and return * Create a new instance of bladerf_source_c and return
* a boost shared_ptr. This is effectively the public constructor. * a boost shared_ptr. This is effectively the public constructor.
*/ */
bladerf_source_c_sptr make_bladerf_source_c (const std::string &args) bladerf_source_c_sptr make_bladerf_source_c(const std::string &args)
{ {
return gnuradio::get_initial_sptr(new bladerf_source_c (args)); return gnuradio::get_initial_sptr(new bladerf_source_c(args));
} }
/* /*
@ -62,20 +62,20 @@ bladerf_source_c_sptr make_bladerf_source_c (const std::string &args)
* are connected to this block. In this case, we accept either * are connected to this block. In this case, we accept either
* 1 or 2 outputs. * 1 or 2 outputs.
*/ */
static const int MIN_IN = 0; // mininum number of input streams static const int MIN_IN = 0; // mininum number of input streams
static const int MAX_IN = 0; // maximum number of input streams static const int MAX_IN = 0; // maximum number of input streams
static const int MIN_OUT = 1; // minimum number of output streams static const int MIN_OUT = 1; // minimum number of output streams
static const int MAX_OUT = 2; // maximum number of output streams static const int MAX_OUT = 2; // maximum number of output streams
/* /*
* The private constructor * The private constructor
*/ */
bladerf_source_c::bladerf_source_c (const std::string &args) bladerf_source_c::bladerf_source_c(const std::string &args)
: gr::sync_block ("bladerf_source_c", :gr::sync_block("bladerf_source_c",
gr::io_signature::make (MIN_IN, MAX_IN, sizeof (gr_complex)), gr::io_signature::make(MIN_IN, MAX_IN, sizeof(gr_complex)),
gr::io_signature::make (MIN_OUT, MAX_OUT, sizeof (gr_complex))) gr::io_signature::make(MIN_OUT, MAX_OUT, sizeof(gr_complex)))
{ {
int ret; int status;
std::string device_name; std::string device_name;
struct bladerf_version fpga_version; struct bladerf_version fpga_version;
@ -83,36 +83,46 @@ bladerf_source_c::bladerf_source_c (const std::string &args)
init(dict, BLADERF_RX); init(dict, BLADERF_RX);
if (dict.count("sampling")) if (dict.count("sampling")) {
{
std::string sampling = dict["sampling"]; std::string sampling = dict["sampling"];
std::cerr << _pfx << "Setting bladerf sampling to " << sampling << std::endl; std::cerr << _pfx
if( sampling == "internal") { << "Setting bladerf sampling to " << sampling
ret = bladerf_set_sampling( _dev.get(), BLADERF_SAMPLING_INTERNAL ); << std::endl;
if ( ret != 0 )
std::cerr << _pfx << "Problem while setting sampling mode:" if (sampling == "internal") {
<< bladerf_strerror(ret) << std::endl; status = bladerf_set_sampling(_dev.get(), BLADERF_SAMPLING_INTERNAL);
} else if( sampling == "external" ) { if (status != 0) {
ret = bladerf_set_sampling( _dev.get(), BLADERF_SAMPLING_EXTERNAL ); std::cerr << _pfx
if ( ret != 0 ) << "Problem while setting sampling mode: "
std::cerr << _pfx << "Problem while setting sampling mode:" << bladerf_strerror(status)
<< bladerf_strerror(ret) << std::endl; << std::endl;
}
} else if (sampling == "external") {
status = bladerf_set_sampling(_dev.get(), BLADERF_SAMPLING_EXTERNAL);
if (status != 0) {
std::cerr << _pfx
<< "Problem while setting sampling mode: "
<< bladerf_strerror(status)
<< std::endl;
}
} else { } else {
std::cerr << _pfx << "Invalid sampling mode " << sampling << std::endl; std::cerr << _pfx << "Invalid sampling mode " << sampling << std::endl;
} }
} }
/* Warn user about using an old FPGA version, as we no longer strip off the /* Warn user about using an old FPGA version, as we no longer strip off the
* markers that were pressent in the pre-v0.0.1 FPGA */ * markers that were pressent in the pre-v0.0.1 FPGA */
if (bladerf_fpga_version( _dev.get(), &fpga_version ) != 0) { if (bladerf_fpga_version(_dev.get(), &fpga_version) != 0) {
std::cerr << _pfx << "Failed to get FPGA version" << std::endl; std::cerr << _pfx << "Failed to get FPGA version" << std::endl;
} else if ( fpga_version.major <= 0 && } else if (fpga_version.major <= 0 &&
fpga_version.minor <= 0 && fpga_version.minor <= 0 && fpga_version.patch < 1) {
fpga_version.patch < 1 ) {
std::cerr << _pfx << "Warning: FPGA version v0.0.1 or later is required. " std::cerr << _pfx
<< "Using an earlier FPGA version will result in misinterpeted samples. " << "Warning: FPGA version v0.0.1 or later is required. Using an "
<< "earlier FPGA version will result in misinterpeted samples."
<< std::endl; << std::endl;
} }
} }
@ -127,12 +137,12 @@ bool bladerf_source_c::stop()
return bladerf_common::stop(BLADERF_RX); return bladerf_common::stop(BLADERF_RX);
} }
int bladerf_source_c::work( int noutput_items, int bladerf_source_c::work(int noutput_items,
gr_vector_const_void_star &input_items, gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items ) gr_vector_void_star &output_items)
{ {
int ret;
const float scaling = 2048.0f; const float scaling = 2048.0f;
int status;
gr_complex *out = static_cast<gr_complex *>(output_items[0]); gr_complex *out = static_cast<gr_complex *>(output_items[0]);
struct bladerf_metadata meta; struct bladerf_metadata meta;
struct bladerf_metadata *meta_ptr = NULL; struct bladerf_metadata *meta_ptr = NULL;
@ -143,11 +153,10 @@ int bladerf_source_c::work( int noutput_items,
_conv_buf_size = noutput_items; _conv_buf_size = noutput_items;
tmp = realloc(_conv_buf, _conv_buf_size * 2 * sizeof(int16_t)); tmp = realloc(_conv_buf, _conv_buf_size * 2 * sizeof(int16_t));
if (tmp == NULL) { if (tmp == NULL) {
throw std::runtime_error( std::string(__FUNCTION__) + throw std::runtime_error(_pfx + "Failed to realloc _conv_buf");
"Failed to realloc _conv_buf" );
} }
_conv_buf = static_cast<int16_t*>(tmp); _conv_buf = static_cast<int16_t *>(tmp);
} }
if (_use_metadata) { if (_use_metadata) {
@ -157,31 +166,33 @@ int bladerf_source_c::work( int noutput_items,
} }
/* Grab all the samples into the temporary buffer */ /* Grab all the samples into the temporary buffer */
ret = bladerf_sync_rx(_dev.get(), static_cast<void *>(_conv_buf), status = bladerf_sync_rx(_dev.get(), static_cast<void *>(_conv_buf),
noutput_items, meta_ptr, _stream_timeout_ms); noutput_items, meta_ptr, _stream_timeout_ms);
if ( ret != 0 ) { if (status != 0) {
std::cerr << _pfx << "bladerf_sync_rx error: " std::cerr << _pfx
<< bladerf_strerror(ret) << std::endl; << "bladerf_sync_rx error: " << bladerf_strerror(status)
<< std::endl;
_consecutive_failures++; _consecutive_failures++;
if ( _consecutive_failures >= MAX_CONSECUTIVE_FAILURES ) { if (_consecutive_failures >= MAX_CONSECUTIVE_FAILURES) {
std::cerr << _pfx std::cerr << _pfx
<< "Consecutive error limit hit. Shutting down." << "Consecutive error limit hit. Shutting down."
<< std::endl; << std::endl;
return WORK_DONE; return WORK_DONE;
} }
} else { } else {
_consecutive_failures = 0; _consecutive_failures = 0;
} }
/* Convert them from fixed to floating point */ /* Convert them from fixed to floating point */
volk_16i_s32f_convert_32f((float*)out, _conv_buf, scaling, 2*noutput_items); volk_16i_s32f_convert_32f((float *) out, _conv_buf, scaling,
2 * noutput_items);
return noutput_items; return noutput_items;
} }
std::vector<std::string> bladerf_source_c::get_devices() std::vector < std::string > bladerf_source_c::get_devices()
{ {
return bladerf_common::devices(); return bladerf_common::devices();
} }
@ -196,14 +207,14 @@ osmosdr::meta_range_t bladerf_source_c::get_sample_rates()
return sample_rates(); return sample_rates();
} }
double bladerf_source_c::set_sample_rate( double rate ) double bladerf_source_c::set_sample_rate(double rate)
{ {
return bladerf_common::set_sample_rate( BLADERF_RX, rate); return bladerf_common::set_sample_rate(BLADERF_RX, rate);
} }
double bladerf_source_c::get_sample_rate() double bladerf_source_c::get_sample_rate()
{ {
return bladerf_common::get_sample_rate( BLADERF_RX ); return bladerf_common::get_sample_rate(BLADERF_RX);
} }
osmosdr::freq_range_t bladerf_source_c::get_freq_range(size_t chan) osmosdr::freq_range_t bladerf_source_c::get_freq_range(size_t chan)
@ -221,66 +232,68 @@ double bladerf_source_c::get_center_freq(size_t chan)
return bladerf_common::get_center_freq(BLADERF_CHANNEL_RX(chan)); return bladerf_common::get_center_freq(BLADERF_CHANNEL_RX(chan));
} }
double bladerf_source_c::set_freq_corr( double ppm, size_t chan ) double bladerf_source_c::set_freq_corr(double ppm, size_t chan)
{ {
/* TODO: Write the VCTCXO with a correction value (also changes TX ppm value!) */ /* TODO: Write the VCTCXO with a correction value (also changes TX ppm value!) */
return get_freq_corr( BLADERF_CHANNEL_RX(chan) ); return get_freq_corr(BLADERF_CHANNEL_RX(chan));
} }
double bladerf_source_c::get_freq_corr( size_t chan ) double bladerf_source_c::get_freq_corr(size_t chan)
{ {
/* TODO: Return back the frequency correction in ppm */ /* TODO: Return back the frequency correction in ppm */
return 0; return 0;
} }
std::vector<std::string> bladerf_source_c::get_gain_names( size_t chan ) std::vector < std::string > bladerf_source_c::get_gain_names(size_t chan)
{ {
return bladerf_common::get_gain_names(BLADERF_CHANNEL_RX(chan)); return bladerf_common::get_gain_names(BLADERF_CHANNEL_RX(chan));
} }
osmosdr::gain_range_t bladerf_source_c::get_gain_range( size_t chan ) osmosdr::gain_range_t bladerf_source_c::get_gain_range(size_t chan)
{ {
return bladerf_common::get_gain_range(BLADERF_CHANNEL_RX(chan)); return bladerf_common::get_gain_range(BLADERF_CHANNEL_RX(chan));
} }
osmosdr::gain_range_t bladerf_source_c::get_gain_range( const std::string & name, size_t chan ) osmosdr::gain_range_t bladerf_source_c::get_gain_range(const std::string &name,
size_t chan)
{ {
return bladerf_common::get_gain_range(name, BLADERF_CHANNEL_RX(chan)); return bladerf_common::get_gain_range(name, BLADERF_CHANNEL_RX(chan));
} }
bool bladerf_source_c::set_gain_mode( bool automatic, size_t chan ) bool bladerf_source_c::set_gain_mode(bool automatic, size_t chan)
{ {
return bladerf_common::set_gain_mode(automatic, BLADERF_CHANNEL_RX(chan)); return bladerf_common::set_gain_mode(automatic, BLADERF_CHANNEL_RX(chan));
} }
bool bladerf_source_c::get_gain_mode( size_t chan ) bool bladerf_source_c::get_gain_mode(size_t chan)
{ {
return bladerf_common::get_gain_mode(BLADERF_CHANNEL_RX(chan)); return bladerf_common::get_gain_mode(BLADERF_CHANNEL_RX(chan));
} }
double bladerf_source_c::set_gain( double gain, size_t chan ) double bladerf_source_c::set_gain(double gain, size_t chan)
{ {
return bladerf_common::set_gain(gain, BLADERF_CHANNEL_RX(chan)); return bladerf_common::set_gain(gain, BLADERF_CHANNEL_RX(chan));
} }
double bladerf_source_c::set_gain( double gain, const std::string & name, size_t chan) double bladerf_source_c::set_gain(double gain, const std::string &name,
size_t chan)
{ {
return bladerf_common::set_gain(gain, name, BLADERF_CHANNEL_RX(chan)); return bladerf_common::set_gain(gain, name, BLADERF_CHANNEL_RX(chan));
} }
double bladerf_source_c::get_gain( size_t chan ) double bladerf_source_c::get_gain(size_t chan)
{ {
return bladerf_common::get_gain(BLADERF_CHANNEL_RX(chan)); return bladerf_common::get_gain(BLADERF_CHANNEL_RX(chan));
} }
double bladerf_source_c::get_gain( const std::string & name, size_t chan ) double bladerf_source_c::get_gain(const std::string &name, size_t chan)
{ {
return bladerf_common::get_gain(name, BLADERF_CHANNEL_RX(chan)); return bladerf_common::get_gain(name, BLADERF_CHANNEL_RX(chan));
} }
std::vector< std::string > bladerf_source_c::get_antennas( size_t chan ) std::vector < std::string > bladerf_source_c::get_antennas(size_t chan)
{ {
std::vector< std::string > antennas; std::vector < std::string > antennas;
antennas += "RX0"; antennas += "RX0";
@ -291,109 +304,121 @@ std::vector< std::string > bladerf_source_c::get_antennas( size_t chan )
return antennas; return antennas;
} }
std::string bladerf_source_c::set_antenna( const std::string & antenna, size_t chan ) std::string bladerf_source_c::set_antenna(const std::string &antenna,
size_t chan)
{ {
return get_antenna( BLADERF_CHANNEL_RX(chan) ); return get_antenna(BLADERF_CHANNEL_RX(chan));
} }
std::string bladerf_source_c::get_antenna( size_t chan ) std::string bladerf_source_c::get_antenna(size_t chan)
{ {
/* We only have a single receive antenna here */ /* We only have a single receive antenna here */
// TODO: this is a lie // TODO: this is a lie
return "RX0"; return "RX0";
} }
void bladerf_source_c::set_dc_offset_mode( int mode, size_t chan ) void bladerf_source_c::set_dc_offset_mode(int mode, size_t chan)
{ {
if ( osmosdr::source::DCOffsetOff == mode ) { if (osmosdr::source::DCOffsetOff == mode) {
//_src->set_auto_dc_offset( false, chan ); //_src->set_auto_dc_offset( false, chan );
set_dc_offset( std::complex<double>(0.0, 0.0), chan ); /* reset to default for off-state */ /* reset to default for off-state */
} else if ( osmosdr::source::DCOffsetManual == mode ) { set_dc_offset(std::complex < double >(0.0, 0.0), chan);
//_src->set_auto_dc_offset( false, chan ); /* disable auto mode, but keep correcting with last known values */ } else if (osmosdr::source::DCOffsetManual == mode) {
} else if ( osmosdr::source::DCOffsetAutomatic == mode ) { /* disable auto mode, but keep correcting with last known values */
//_src->set_auto_dc_offset( false, chan );
} else if (osmosdr::source::DCOffsetAutomatic == mode) {
//_src->set_auto_dc_offset( true, chan ); //_src->set_auto_dc_offset( true, chan );
std::cerr << "Automatic DC correction mode is not implemented." << std::endl; std::cerr << _pfx
<< "Automatic DC correction mode is not implemented."
<< std::endl;
} }
} }
void bladerf_source_c::set_dc_offset( const std::complex<double> &offset, size_t chan ) void bladerf_source_c::set_dc_offset(const std::complex < double > &offset,
size_t chan)
{ {
int ret = 0; int status;
ret = bladerf_common::set_dc_offset(BLADERF_RX, offset, chan); status = bladerf_common::set_dc_offset(BLADERF_RX, offset, chan);
if( ret ) { if (status != 0) {
throw std::runtime_error( std::string(__FUNCTION__) + " " + throw std::runtime_error(_pfx + "could not set dc offset: " +
"could not set dc offset: " + bladerf_strerror(status));
std::string(bladerf_strerror(ret)) );
} }
} }
void bladerf_source_c::set_iq_balance_mode( int mode, size_t chan ) void bladerf_source_c::set_iq_balance_mode(int mode, size_t chan)
{ {
if ( osmosdr::source::IQBalanceOff == mode ) { if (osmosdr::source::IQBalanceOff == mode) {
//_src->set_auto_iq_balance( false, chan ); //_src->set_auto_iq_balance( false, chan );
set_iq_balance( std::complex<double>(0.0, 0.0), chan ); /* reset to default for off-state */ /* reset to default for off-state */
} else if ( osmosdr::source::IQBalanceManual == mode ) { set_iq_balance(std::complex < double >(0.0, 0.0), chan);
//_src->set_auto_iq_balance( false, chan ); /* disable auto mode, but keep correcting with last known values */ } else if (osmosdr::source::IQBalanceManual == mode) {
} else if ( osmosdr::source::IQBalanceAutomatic == mode ) { /* disable auto mode, but keep correcting with last known values */
//_src->set_auto_iq_balance( false, chan );
} else if (osmosdr::source::IQBalanceAutomatic == mode) {
//_src->set_auto_iq_balance( true, chan ); //_src->set_auto_iq_balance( true, chan );
std::cerr << "Automatic IQ correction mode is not implemented." << std::endl; std::cerr << _pfx
<< "Automatic IQ correction mode is not implemented."
<< std::endl;
} }
} }
void bladerf_source_c::set_iq_balance( const std::complex<double> &balance, size_t chan ) void bladerf_source_c::set_iq_balance(const std::complex < double > &balance,
size_t chan)
{ {
int ret = 0; int status;
ret = bladerf_common::set_iq_balance(BLADERF_RX, balance, chan); status = bladerf_common::set_iq_balance(BLADERF_RX, balance, chan);
if( ret ) { if (status != 0) {
throw std::runtime_error( std::string(__FUNCTION__) + " " + throw std::runtime_error(_pfx + "could not set iq balance: " +
"could not set iq balance: " + bladerf_strerror(status));
std::string(bladerf_strerror(ret)) );
} }
} }
double bladerf_source_c::set_bandwidth( double bandwidth, size_t chan ) double bladerf_source_c::set_bandwidth(double bandwidth, size_t chan)
{ {
int ret; int status;
uint32_t actual; uint32_t actual;
if ( bandwidth == 0.0 ) /* bandwidth of 0 means automatic filter selection */ if (bandwidth == 0.0) {
bandwidth = get_sample_rate() * 0.75; /* select narrower filters to prevent aliasing */ /* bandwidth of 0 means automatic filter selection */
/* select narrower filters to prevent aliasing */
bandwidth = get_sample_rate() * 0.75;
}
ret = bladerf_set_bandwidth( _dev.get(), BLADERF_RX, (uint32_t)bandwidth, &actual ); status = bladerf_set_bandwidth(_dev.get(), BLADERF_RX, (uint32_t) bandwidth,
if( ret ) { &actual);
throw std::runtime_error( std::string(__FUNCTION__) + " " + if (status != 0) {
"could not set bandwidth: " + throw std::runtime_error(_pfx + "could not set bandwidth: " +
std::string(bladerf_strerror(ret)) ); bladerf_strerror(status));
} }
return get_bandwidth(); return get_bandwidth();
} }
double bladerf_source_c::get_bandwidth( size_t chan ) double bladerf_source_c::get_bandwidth(size_t chan)
{ {
int status;
uint32_t bandwidth; uint32_t bandwidth;
int ret;
ret = bladerf_get_bandwidth( _dev.get(), BLADERF_RX, &bandwidth ); status = bladerf_get_bandwidth(_dev.get(), BLADERF_RX, &bandwidth);
if( ret ) { if (status != 0) {
throw std::runtime_error( std::string(__FUNCTION__) + " " + throw std::runtime_error(_pfx + "could not get bandwidth: " +
"could not get bandwidth:" + bladerf_strerror(status));
std::string(bladerf_strerror(ret)) );
} }
return (double)bandwidth; return (double) bandwidth;
} }
osmosdr::freq_range_t bladerf_source_c::get_bandwidth_range( size_t chan ) osmosdr::freq_range_t bladerf_source_c::get_bandwidth_range(size_t chan)
{ {
return filter_bandwidths(); return filter_bandwidths();
} }
void bladerf_source_c::set_clock_source(const std::string &source, const size_t mboard) void bladerf_source_c::set_clock_source(const std::string &source,
const size_t mboard)
{ {
bladerf_common::set_clock_source(source, mboard); bladerf_common::set_clock_source(source, mboard);
} }
@ -403,7 +428,7 @@ std::string bladerf_source_c::get_clock_source(const size_t mboard)
return bladerf_common::get_clock_source(mboard); return bladerf_common::get_clock_source(mboard);
} }
std::vector<std::string> bladerf_source_c::get_clock_sources(const size_t mboard) std::vector < std::string > bladerf_source_c::get_clock_sources(const size_t mboard)
{ {
return bladerf_common::get_clock_sources(mboard); return bladerf_common::get_clock_sources(mboard);
} }