GNU Radio's OSMOSDR Package
bladerf_common.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2013-2017 Nuand LLC
4  * Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
5  *
6  * GNU Radio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  *
11  * GNU Radio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GNU Radio; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21 #ifndef INCLUDED_BLADERF_COMMON_H
22 #define INCLUDED_BLADERF_COMMON_H
23 
24 #include <list>
25 #include <map>
26 #include <string>
27 #include <vector>
28 
29 #include <boost/thread/mutex.hpp>
30 #include <boost/weak_ptr.hpp>
31 
32 #include <libbladeRF.h>
33 
34 #include "osmosdr/ranges.h"
35 #include "arg_helpers.h"
36 
37 #include "bladerf_compat.h"
38 
39 #ifdef _MSC_VER
40 #include <cstddef>
41 typedef ptrdiff_t ssize_t;
42 #endif //_MSC_VER
43 
44 #define BLADERF_DEBUG_ENABLE
45 
46 typedef boost::shared_ptr<struct bladerf> bladerf_sptr;
47 
48 /* Identification of the bladeRF hardware in use */
49 typedef enum {
50  BOARD_TYPE_UNKNOWN, /**< Board type is unknown */
51  BOARD_TYPE_NONE, /**< Uninitialized or no board present */
52  BOARD_TYPE_BLADERF_1, /**< bladeRF 1 (LMS6002D-based, 1RX/1TX) */
53  BOARD_TYPE_BLADERF_2, /**< bladeRF 2 (AD9361-based, 2RX/2TX) */
55 
56 /* Mapping of bladerf_channel to bool */
57 typedef std::map<bladerf_channel, bool> bladerf_channel_enable_map;
58 
59 /* Mapping of bladerf_channel to gnuradio port/chan */
60 typedef std::map<bladerf_channel, int> bladerf_channel_map;
61 
62 /* Convenience macros for throwing a runtime error */
63 #define BLADERF_THROW(message) \
64  { \
65  throw std::runtime_error(std::string(__FUNCTION__) + ": " + message); \
66  }
67 
68 #define BLADERF_THROW_STATUS(status, message) \
69  { \
70  BLADERF_THROW(boost::str(boost::format("%s: %s (%d)") % message \
71  % bladerf_strerror(status) % status)); \
72  }
73 
74 /* Convenience macros for printing a warning message to stderr */
75 #define BLADERF_WARNING(message) \
76  { \
77  std::cerr << _pfx << __FUNCTION__ << ": " << message << std::endl; \
78  }
79 
80 #define BLADERF_WARN_STATUS(status, message) \
81  { \
82  BLADERF_WARNING(message << ": " << bladerf_strerror(status)); \
83  } \
84 
85 /* Convenience macro for printing an informational message to stdout */
86 #define BLADERF_INFO(message) \
87  { \
88  std::cout << _pfx << __FUNCTION__ << ": " << message << std::endl; \
89  }
90 
91 /* Convenience macro for printing a debug message to stdout */
92 #ifdef BLADERF_DEBUG_ENABLE
93 #define BLADERF_DEBUG(message) BLADERF_INFO("DEBUG: " << message)
94 #else
95 #define BLADERF_DEBUG(message)
96 #endif // BLADERF_DEBUG_ENABLE
97 
98 /* Given a bladerf_channel_layout, calculate the number of streams */
99 size_t num_streams(bladerf_channel_layout layout);
100 
101 /**
102  * Common class for bladeRF interaction
103  */
105 {
106 public:
107  /*****************************************************************************
108  * Public methods
109  ****************************************************************************/
110  bladerf_common();
111 
112 protected:
113  /*****************************************************************************
114  * Protected methods
115  ****************************************************************************/
116 
117  /**
118  * Handle initialization and parameters common to both source & sink
119  *
120  * Specify arguments in key=value,key=value format, e.g.
121  * bladerf=0,buffers=512
122  *
123  * Recognized arguments:
124  * Key Allowed values
125  * ---------------------------------------------------------------------------
126  * REQUIRED:
127  * bladerf a valid instance or serial number
128  * USB INTERFACE CONTROL:
129  * buffers (default: NUM_BUFFERS)
130  * buflen (default: NUM_SAMPLES_PER_BUFFER)
131  * stream_timeout valid time in milliseconds (default: 3000)
132  * transfers (default: NUM_TRANSFERS)
133  * FPGA CONTROL:
134  * enable_metadata 1 to enable metadata
135  * fpga a path to a valid .rbf file
136  * fpga-reload 1 to force reloading the FPGA unconditionally
137  * RF CONTROL:
138  * agc 1 to enable, 0 to disable (default: hardware-dependent)
139  * agc_mode default, manual, fast, slow, hybrid (default: default)
140  * loopback bb_txlpf_rxvga2, bb_txlpf_rxlpf, bb_txvga1_rxvga2,
141  * bb_txvga1_rxlpf, rf_lna1, rf_lna2, rf_lna3, firmware,
142  * ad9361_bist, none (default: none)
143  * ** Note: valid on receive channels only
144  * rxmux baseband, 12bit, 32bit, digital (default: baseband)
145  * ** Note: valid on receive channels only
146  * smb a valid frequency
147  * tamer internal, external_1pps, external (default: internal)
148  * xb200 auto, auto3db, 50M, 144M, 222M, custom (default: auto)
149  * MISC:
150  * verbosity verbose, debug, info, warning, error, critical, silent
151  * (default: info)
152  * ** Note: applies only to libbladeRF logging
153  */
154  void init(dict_t const &dict, bladerf_direction direction);
155 
156  /* Get a vector of available devices */
157  static std::vector<std::string> devices();
158  /* Get the type of the open bladeRF board */
160  /* Get the maximum number of channels supported in a given direction */
161  size_t get_max_channels(bladerf_direction direction);
162 
163  void set_channel_enable(bladerf_channel ch, bool enable);
164  bool get_channel_enable(bladerf_channel ch);
165 
166  /* Set libbladeRF verbosity */
167  void set_verbosity(std::string const &verbosity);
168 
169  /* Convert an antenna/channel name (e.g. "RX2") to a bladerf_channel */
170  bladerf_channel str2channel(std::string const &ch);
171  /* Convert a bladerf_channel to an antenna/channel name (e.g. "RX2") */
172  std::string channel2str(bladerf_channel ch);
173  /* Convert a bladerf_channel to a hardware port identifier */
174  int channel2rfport(bladerf_channel ch);
175 
176  /* Using the channel map, get the bladerf_channel for a gnuradio chan */
177  bladerf_channel chan2channel(bladerf_direction direction, size_t chan = 0);
178 
179  /* Get range of supported sampling rates for channel ch */
180  osmosdr::meta_range_t sample_rates(bladerf_channel ch);
181  /* Set sampling rate on channel ch to rate */
182  double set_sample_rate(double rate, bladerf_channel ch);
183  /* Get the current sampling rate on channel ch */
184  double get_sample_rate(bladerf_channel ch);
185 
186  /* Get range of supported RF frequencies for channel ch */
187  osmosdr::freq_range_t freq_range(bladerf_channel ch);
188  /* Set center RF frequency of channel ch to freq */
189  double set_center_freq(double freq, bladerf_channel ch);
190  /* Get the center RF frequency of channel ch */
191  double get_center_freq(bladerf_channel ch);
192 
193  /* Get range of supported bandwidths for channel ch */
194  osmosdr::freq_range_t filter_bandwidths(bladerf_channel ch);
195  /* Set the bandwidth on channel ch to bandwidth */
196  double set_bandwidth(double bandwidth, bladerf_channel ch);
197  /* Get the current bandwidth of channel ch */
198  double get_bandwidth(bladerf_channel ch);
199 
200  /* Get the names of gain stages on channel ch */
201  std::vector<std::string> get_gain_names(bladerf_channel ch);
202  /* Get range of supported overall gain values on channel ch */
203  osmosdr::gain_range_t get_gain_range(bladerf_channel ch);
204  /* Get range of supported gain values for gain stage 'name' on channel ch */
205  osmosdr::gain_range_t get_gain_range(std::string const &name,
206  bladerf_channel ch);
207 
208  /* Enable or disable the automatic gain control on channel ch */
209  bool set_gain_mode(bool automatic, bladerf_channel ch,
210  bladerf_gain_mode agc_mode = BLADERF_GAIN_DEFAULT);
211  /* Get the current automatic gain control status on channel ch */
212  bool get_gain_mode(bladerf_channel ch);
213 
214  /* Set the overall gain value on channel ch */
215  double set_gain(double gain, bladerf_channel ch);
216  /* Set the gain of stage 'name' on channel ch */
217  double set_gain(double gain, std::string const &name, bladerf_channel ch);
218  /* Get the overall gain value on channel ch */
219  double get_gain(bladerf_channel ch);
220  /* Get the gain of stage 'name' on channel ch */
221  double get_gain(std::string const &name, bladerf_channel ch);
222 
223  /* Get the list of antennas supported by a channel */
224  std::vector<std::string> get_antennas(bladerf_direction dir);
225  bool set_antenna(bladerf_direction dir, size_t chan, const std::string &antenna);
226 
227  /* Set the DC offset on channel ch */
228  int set_dc_offset(std::complex<double> const &offset, bladerf_channel ch);
229  /* Set the IQ balance on channel ch */
230  int set_iq_balance(std::complex<double> const &balance, bladerf_channel ch);
231 
232  /* Get the list of supported clock sources */
233  std::vector<std::string> get_clock_sources(size_t mboard = 0);
234  /* Set the clock source to */
235  void set_clock_source(std::string const &source, size_t mboard = 0);
236  /* Get the name of the current clock source */
237  std::string get_clock_source(size_t mboard = 0);
238 
239  /* Set the SMB frequency */
240  void set_smb_frequency(double frequency);
241  /* Get the current SMB frequency */
242  double get_smb_frequency();
243 
244  /*****************************************************************************
245  * Protected members
246  ****************************************************************************/
247  bladerf_sptr _dev; /**< shared pointer for the active device */
248  std::string _pfx; /**< prefix for console messages */
249  unsigned int _failures; /**< counter for consecutive rx/tx failures */
250 
251  size_t _num_buffers; /**< number of buffers to allocate */
252  size_t _samples_per_buffer; /**< how many samples per buffer */
253  size_t _num_transfers; /**< number of active backend transfers */
254  unsigned int _stream_timeout; /**< timeout for backend transfers */
255 
256  bladerf_format _format; /**< sample format to use */
257 
258  bladerf_channel_map _chanmap; /**< map of antennas to channels */
259  bladerf_channel_enable_map _enables; /**< enabled channels */
260 
261  /*****************************************************************************
262  * Protected constants
263  ****************************************************************************/
264  /* Maximum bladerf_sync_{rx,tx} failures to allow before giving up */
265  static const unsigned int MAX_CONSECUTIVE_FAILURES = 3;
266 
267  /* BladeRF IQ correction parameters */
268  static const int16_t DCOFF_SCALE = 2048;
269  static const int16_t GAIN_SCALE = 4096;
270  static const int16_t PHASE_SCALE = 4096;
271 
272 private:
273  /*****************************************************************************
274  * Private methods
275  ****************************************************************************/
276  /* Open the bladeRF described by device_name. Returns a sptr if successful */
277  bladerf_sptr open(const std::string &device_name);
278  /* Called by shared_ptr when a bladerf_sptr hits a refcount of 0 */
279  static void close(void *dev);
280  /* If a device described by devinfo is open, this returns a sptr to it */
281  static bladerf_sptr get_cached_device(struct bladerf_devinfo devinfo);
282  /* Prints a summary of device information */
283  void print_device_info();
284 
285  bool is_antenna_valid(bladerf_direction dir, const std::string &antenna);
286 
287  /*****************************************************************************
288  * Private members
289  ****************************************************************************/
290  static boost::mutex _devs_mutex; /**< mutex for access to _devs */
291  static std::list<boost::weak_ptr<struct bladerf>> _devs; /**< dev cache */
292 };
293 
294 #endif
bladerf_common::set_clock_source
void set_clock_source(std::string const &source, size_t mboard=0)
num_streams
size_t num_streams(bladerf_channel_layout layout)
bladerf_common::set_gain_mode
bool set_gain_mode(bool automatic, bladerf_channel ch, bladerf_gain_mode agc_mode=BLADERF_GAIN_DEFAULT)
bladerf_common::_dev
bladerf_sptr _dev
Definition: bladerf_common.h:244
bladerf_common::channel2rfport
int channel2rfport(bladerf_channel ch)
arg_helpers.h
bladerf_common::_num_buffers
size_t _num_buffers
Definition: bladerf_common.h:248
bladerf_common::set_verbosity
void set_verbosity(std::string const &verbosity)
bladerf_common::freq_range
osmosdr::freq_range_t freq_range(bladerf_channel ch)
bladerf_common::set_iq_balance
int set_iq_balance(std::complex< double > const &balance, bladerf_channel ch)
bladerf_common::chan2channel
bladerf_channel chan2channel(bladerf_direction direction, size_t chan=0)
bladerf_common
Definition: bladerf_common.h:104
BOARD_TYPE_NONE
Definition: bladerf_common.h:51
bladerf_common::init
void init(dict_t const &dict, bladerf_direction direction)
bladerf_common::get_board_type
bladerf_board_type get_board_type()
bladerf_common::_pfx
std::string _pfx
Definition: bladerf_common.h:245
BOARD_TYPE_BLADERF_2
Definition: bladerf_common.h:53
bladerf_common::devices
static std::vector< std::string > devices()
bladerf_common::get_channel_enable
bool get_channel_enable(bladerf_channel ch)
bladerf_common::get_antennas
std::vector< std::string > get_antennas(bladerf_direction dir)
bladerf_common::MAX_CONSECUTIVE_FAILURES
static const unsigned int MAX_CONSECUTIVE_FAILURES
Definition: bladerf_common.h:261
bladerf_common::set_sample_rate
double set_sample_rate(double rate, bladerf_channel ch)
bladerf_common::DCOFF_SCALE
static const int16_t DCOFF_SCALE
Definition: bladerf_common.h:264
bladerf_common::filter_bandwidths
osmosdr::freq_range_t filter_bandwidths(bladerf_channel ch)
bladerf_common::get_max_channels
size_t get_max_channels(bladerf_direction direction)
bladerf_common::PHASE_SCALE
static const int16_t PHASE_SCALE
Definition: bladerf_common.h:266
bladerf_compat.h
bladerf_common::get_center_freq
double get_center_freq(bladerf_channel ch)
bladerf_board_type
bladerf_board_type
Definition: bladerf_common.h:49
bladerf_common::get_gain
double get_gain(bladerf_channel ch)
bladerf_common::str2channel
bladerf_channel str2channel(std::string const &ch)
bladerf_common::channel2str
std::string channel2str(bladerf_channel ch)
bladerf_common::get_bandwidth
double get_bandwidth(bladerf_channel ch)
bladerf_common::_samples_per_buffer
size_t _samples_per_buffer
Definition: bladerf_common.h:249
bladerf_common::get_gain_names
std::vector< std::string > get_gain_names(bladerf_channel ch)
bladerf_common::set_antenna
bool set_antenna(bladerf_direction dir, size_t chan, const std::string &antenna)
bladerf_common::_enables
bladerf_channel_enable_map _enables
Definition: bladerf_common.h:256
bladerf_common::get_gain_range
osmosdr::gain_range_t get_gain_range(bladerf_channel ch)
bladerf_common::get_clock_sources
std::vector< std::string > get_clock_sources(size_t mboard=0)
bladerf_common::_failures
unsigned int _failures
Definition: bladerf_common.h:246
bladerf_common::get_gain_mode
bool get_gain_mode(bladerf_channel ch)
bladerf_common::_format
bladerf_format _format
Definition: bladerf_common.h:253
bladerf_common::get_clock_source
std::string get_clock_source(size_t mboard=0)
bladerf_common::GAIN_SCALE
static const int16_t GAIN_SCALE
Definition: bladerf_common.h:265
dict_t
std::map< std::string, std::string > dict_t
Definition: arg_helpers.h:35
osmosdr::meta_range_t
Definition: ranges.h:75
bladerf_common::set_center_freq
double set_center_freq(double freq, bladerf_channel ch)
bladerf_common::get_sample_rate
double get_sample_rate(bladerf_channel ch)
bladerf_common::sample_rates
osmosdr::meta_range_t sample_rates(bladerf_channel ch)
bladerf_common::get_smb_frequency
double get_smb_frequency()
BOARD_TYPE_BLADERF_1
Definition: bladerf_common.h:52
bladerf_common::_num_transfers
size_t _num_transfers
Definition: bladerf_common.h:250
bladerf_channel_map
std::map< bladerf_channel, int > bladerf_channel_map
Definition: bladerf_common.h:60
bladerf_common::set_gain
double set_gain(double gain, bladerf_channel ch)
bladerf_common::_chanmap
bladerf_channel_map _chanmap
Definition: bladerf_common.h:255
BOARD_TYPE_UNKNOWN
Definition: bladerf_common.h:50
bladerf_common::_stream_timeout
unsigned int _stream_timeout
Definition: bladerf_common.h:251
bladerf_common::set_smb_frequency
void set_smb_frequency(double frequency)
bladerf_common::set_dc_offset
int set_dc_offset(std::complex< double > const &offset, bladerf_channel ch)
bladerf_common::set_channel_enable
void set_channel_enable(bladerf_channel ch, bool enable)
ranges.h
bladerf_common::set_bandwidth
double set_bandwidth(double bandwidth, bladerf_channel ch)
bladerf_channel_enable_map
std::map< bladerf_channel, bool > bladerf_channel_enable_map
Definition: bladerf_common.h:57
bladerf_common::bladerf_common
bladerf_common()