GNU Radio's LIMESDR Package
device_handler.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2018 Lime Microsystems info@limemicro.com
4  *
5  * GNU Radio is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3, or (at your option)
8  * any later version.
9  *
10  * GNU Radio is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with GNU Radio; see the file COPYING. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifndef DEVICE_HANDLER_H
22 #define DEVICE_HANDLER_H
23 
24 #include <lime/LimeSuite.h>
25 #include <lime/limeRFE.h>
26 #include <cmath>
27 #include <iostream>
28 #include <list>
29 #include <math.h>
30 #include <mutex>
31 #include <string>
32 #include <vector>
33 
34 #define LMS_CH_0 0
35 #define LMS_CH_1 1
36 
37 #define LimeSDR_Mini 1
38 #define LimeNET_Micro 2
39 #define LimeSDR_USB 3
40 
41 #define GR_LIMESDR_VER "2.2.7"
42 
44  private:
45  int open_devices = 0;
46  // Read device list once flag
47  bool list_read = false;
48  // Calculate open devices to close them all on close_all_devices
49  int device_count;
50 
51  struct device {
52  // Device address
53  lms_device_t* address = NULL;
54 
55  // Flags and variables used to check
56  // shared settings and blocks usage
57  bool source_flag = false;
58  bool sink_flag = false;
59  int source_channel_mode = -1;
60  int sink_channel_mode = -1;
61  std::string source_filename;
62  std::string sink_filename;
63  };
64 
65  struct rfe_device
66  {
67  int rx_channel = 0;
68  int tx_channel = 0;
69  rfe_dev_t* rfe_dev = nullptr;
70  }rfe_device;
71  // Device list
72  lms_info_str_t* list = new lms_info_str_t[20];
73  // Device vector. Adds devices from the list
74  std::vector<device> device_vector;
75  // Run close_all_devices once with this flag
76  bool close_flag = false;
77 
78  device_handler(){};
80  void operator=(device_handler const&);
81 
82 
83  public:
85  static device_handler instance;
86  return instance;
87  }
89 
90  mutable std::recursive_mutex block_mutex;
91 
92 
93  /**
94  * Print device error and close all devices.
95  *
96  * @param device_number Device number from the list of LMS_GetDeviceList.
97  */
98  void error(int device_number);
99 
100  /**
101  * Get device connection handler in order to configure it.
102  *
103  * @param device_number Device number from the list of LMS_GetDeviceList.
104  */
105  lms_device_t* get_device(int device_number);
106 
107  /**
108  * Connect to the device and create singletone.
109  *
110  * @param serial Device serial from the list of LMS_GetDeviceList.
111  */
112  int open_device(std::string& serial);
113 
114  /**
115  * Disconnect from the device.
116  *
117  * @param device_number Device number from the list of LMS_GetDeviceList.
118  *
119  * @param block_type Source block(1), Sink block(2).
120  */
121  void close_device(int device_number, int block_type);
122 
123  /**
124  * Disconnect from all devices.
125  */
127 
128  /**
129  * Check what blocks are used for single device.
130  *
131  * @param device_number Device number from the list of LMS_GetDeviceList.
132  *
133  * @param block_type Source block(1), Sink block(2).
134  *
135  * @param channel_mode Channel A(0), Channel B(1), MIMO(2)
136  *
137  * @param filename Path to file if file switch is turned on.
138  */
139  void
140  check_blocks(int device_number, int block_type, int channel_mode, const std::string& filename);
141 
142  /**
143  * Load settings from .ini file.
144  *
145  * @param device_number Device number from the list of LMS_GetDeviceList.
146  *
147  * @param filename Path to file if file switch is turned on.
148  *
149  * @param antenna_tx Pointer to TX antenna, so PA path would be updated in sink block
150  */
151  void settings_from_file(int device_number, const std::string& filename, int* antenna_tx);
152 
153  /**
154  * Set used channels
155  *
156  * @param device_number Device number from the list of LMS_GetDeviceList.
157  *
158  * @param channel_mode Channel A(0), Channel B(1), MIMO(2)
159  *
160  * @param direction Direction of samples RX(LMS_CH_RX), TX(LMS_CH_RX).
161  */
162  void enable_channels(int device_number, int channel_mode, bool direction);
163 
164  /**
165  * Set the same sample rate for both channels.
166  *
167  * @param device_number Device number from the list of LMS_GetDeviceList.
168  *
169  * @param rate Sample rate in S/s.
170  */
171  void set_samp_rate(int device_number, double& rate);
172 
173  /**
174  * Set oversampling value for both channels
175  *
176  * @param device_number Device number from the list of LMS_GetDeviceList.
177  *
178  * @param oversample Oversampling value (0 (default),1,2,4,8,16,32).
179  */
180  void set_oversampling(int device_number, int oversample);
181 
182  /**
183  * Set RF frequency of both channels (RX and TX separately).
184  *
185  * @param device_number Device number from the list of LMS_GetDeviceList.
186  *
187  * @param direction Direction of samples RX(LMS_CH_RX), TX(LMS_CH_TX).
188  *
189  * @param channel selection: A(LMS_CH_0),B(LMS_CH_1).
190  *
191  * @param rf_freq RF frequency in Hz.
192  *
193  * @return returns RF frequency in Hz
194  */
195  double set_rf_freq(int device_number, bool direction, int channel, float rf_freq);
196 
197  /**
198  * Perform device calibration.
199  *
200  * @param device_number Device number from the list of LMS_GetDeviceList.
201  *
202  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_RX).
203  *
204  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
205  *
206  * @param bandwidth Set calibration bandwidth in Hz.
207  *
208  */
209  void calibrate(int device_number, int direction, int channel, double bandwidth);
210 
211  /**
212  * Set which antenna is used
213  *
214  * @param device_number Device number from the list of LMS_GetDeviceList.
215  *
216  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
217  *
218  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_RX).
219  *
220  * @param antenna Antenna to set: None(0), LNAH(1), LNAL(2), LNAW(3) for RX
221  * None(0), BAND1(1), BAND(2), NONE(3) for TX
222  *
223  */
224  void set_antenna(int device_number, int channel, int direction, int antenna);
225 
226  /**
227  * Set analog filters.
228  *
229  * @param device_number Device number from the list of LMS_GetDeviceList.
230  *
231  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_TX).
232  *
233  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
234  *
235  * @param analog_bandw Channel filter bandwidth in Hz.
236  */
237  double set_analog_filter(int device_number, bool direction, int channel, double analog_bandw);
238 
239  /**
240  * Set digital filters (GFIR).
241  *
242  * @param device_number Device number from the list of LMS_GetDeviceList.
243  *
244  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_TX).
245  *
246  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
247  *
248  * @param digital_bandw Channel filter bandwidth in Hz.
249  */
250  double set_digital_filter(int device_number, bool direction, int channel, double digital_bandw);
251 
252  /**
253  * Set the combined gain value in dB
254  * This function computes and sets the optimal gain values of various amplifiers
255  * that are present in the device based on desired gain value in dB.
256  *
257  * @note actual gain depends on LO frequency and analog LPF configuration and
258  * resulting output signal level may be different when those values are changed
259  *
260  * @param device_number Device number from the list of LMS_GetDeviceList.
261  *
262  * @param direction Select RX or TX.
263  *
264  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
265  *
266  * @param gain_dB Desired gain: [0,73] dB
267  */
268  unsigned set_gain(int device_number, bool direction, int channel, unsigned gain_dB);
269 
270  /**
271  * Set NCO (numerically controlled oscillator).
272  * By selecting NCO frequency
273  * configure NCO. When NCO frequency is 0, NCO is off.
274  *
275  * @param device_number Device number from the list of LMS_GetDeviceList.
276  *
277  * @param direction Select RX or TX.
278  *
279  * @param channel Channel index.
280  *
281  * @param nco_freq NCO frequency in Hz.
282  */
283  void set_nco(int device_number, bool direction, int channel, float nco_freq);
284 
285  void disable_DC_corrections(int device_number);
286 
287  /**
288  * Set TCXO DAC.
289  * @note Care must be taken as this parameter is returned to default value only after power off.
290  * @note LimeSDR-Mini default value is 180 range is [0,255]
291  * LimeSDR-USB default value is 125 range is [0,255]
292  * LimeSDR-PCIe default value is 134 range is [0,255]
293  * LimeNET-Micro default value is 30714 range is [0,65535]
294  *
295  * @param device_number Device number from the list of LMS_GetDeviceList.
296  *
297  * @param dacVal DAC value (0-65535)
298  */
299  void set_tcxo_dac(int device_number, uint16_t dacVal);
300  /**
301  * Sets up LimeRFE device pointer so that automatic channel configuration could be made
302  * @param rfe_dev Pointer to LimeRFE device descriptor
303  */
304  void set_rfe_device(rfe_dev_t* rfe_dev);
305  /**
306  * Assigns configured LimeSDR channels to LimeRFE for automatic channel switching
307  */
309 
310 };
311 
312 
313 #endif
Definition: device_handler.h:43
void close_device(int device_number, int block_type)
static device_handler & getInstance()
Definition: device_handler.h:84
double set_digital_filter(int device_number, bool direction, int channel, double digital_bandw)
void set_nco(int device_number, bool direction, int channel, float nco_freq)
void disable_DC_corrections(int device_number)
void set_samp_rate(int device_number, double &rate)
lms_device_t * get_device(int device_number)
void update_rfe_channels()
unsigned set_gain(int device_number, bool direction, int channel, unsigned gain_dB)
void settings_from_file(int device_number, const std::string &filename, int *antenna_tx)
std::recursive_mutex block_mutex
Definition: device_handler.h:90
void error(int device_number)
void set_oversampling(int device_number, int oversample)
double set_analog_filter(int device_number, bool direction, int channel, double analog_bandw)
int open_device(std::string &serial)
void set_rfe_device(rfe_dev_t *rfe_dev)
void set_antenna(int device_number, int channel, int direction, int antenna)
void set_tcxo_dac(int device_number, uint16_t dacVal)
void enable_channels(int device_number, int channel_mode, bool direction)
void close_all_devices()
void calibrate(int device_number, int direction, int channel, double bandwidth)
double set_rf_freq(int device_number, bool direction, int channel, float rf_freq)
void check_blocks(int device_number, int block_type, int channel_mode, const std::string &filename)