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