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 <LimeSuite.h>
25 #include <math.h>
26 #include <cmath>
27 #include <iostream>
28 #include <list>
29 #include <mutex>
30 #include <string>
31 #include <vector>
32 
33 #define LMS_CH_0 0
34 #define LMS_CH_1 1
35 
36 #define LimeSDR_Mini 1
37 #define LimeNET_Micro 2
38 #define LimeSDR_USB 3
39 
41 {
42 private:
43  int open_devices = 0;
44  // Read device list once flag
45  bool list_read = false;
46  // Calculate open devices to close them all on close_all_devices
47  int device_count;
48 
49  struct device {
50  // Device address
51  lms_device_t* address = NULL;
52 
53  // Flags and variables used to check
54  // shared settings and blocks usage
55  bool source_flag = false;
56  bool sink_flag = false;
57  int source_channel_mode = -1;
58  int sink_channel_mode = -1;
59  std::string source_filename;
60  std::string sink_filename;
61  };
62 
63  // Device list
64  lms_info_str_t* list = new lms_info_str_t[20];
65  // Device vector. Adds devices from the list
66  std::vector<device> device_vector;
67  // Run close_all_devices once with this flag
68  bool close_flag = false;
69 
70  device_handler(){};
72  void operator=(device_handler const&);
73 
74 
75 public:
77  {
78  static device_handler instance;
79  return instance;
80  }
82 
83  mutable std::recursive_mutex block_mutex;
84 
85 
86  /**
87  * Print device error and close all devices.
88  *
89  * @param device_number Device number from the list of LMS_GetDeviceList.
90  */
91  void error(int device_number);
92 
93  /**
94  * Get device connection handler in order to configure it.
95  *
96  * @param device_number Device number from the list of LMS_GetDeviceList.
97  */
98  lms_device_t* get_device(int device_number);
99 
100  /**
101  * Connect to the device and create singletone.
102  *
103  * @param serial Device serial from the list of LMS_GetDeviceList.
104  */
105  int open_device(std::string& serial);
106 
107  /**
108  * Disconnect from the device.
109  *
110  * @param device_number Device number from the list of LMS_GetDeviceList.
111  *
112  * @param block_type Source block(1), Sink block(2).
113  */
114  void close_device(int device_number, int block_type);
115 
116  /**
117  * Disconnect from all devices.
118  */
119  void close_all_devices();
120 
121  /**
122  * Check what blocks are used for single device.
123  *
124  * @param device_number Device number from the list of LMS_GetDeviceList.
125  *
126  * @param block_type Source block(1), Sink block(2).
127  *
128  * @param channel_mode Channel A(0), Channel B(1), MIMO(2)
129  *
130  * @param filename Path to file if file switch is turned on.
131  */
132  void check_blocks(int device_number,
133  int block_type,
134  int channel_mode,
135  const std::string& filename);
136 
137  /**
138  * Load settings from .ini file.
139  *
140  * @param device_number Device number from the list of LMS_GetDeviceList.
141  *
142  * @param filename Path to file if file switch is turned on.
143  *
144  * @param antenna_tx Pointer to TX antenna, so PA path would be updated in sink
145  * block
146  */
147  void
148  settings_from_file(int device_number, const std::string& filename, int* antenna_tx);
149 
150  /**
151  * Set used channels
152  *
153  * @param device_number Device number from the list of LMS_GetDeviceList.
154  *
155  * @param channel_mode Channel A(0), Channel B(1), MIMO(2)
156  *
157  * @param direction Direction of samples RX(LMS_CH_RX), TX(LMS_CH_RX).
158  */
159  void enable_channels(int device_number, int channel_mode, bool direction);
160 
161  /**
162  * Set the same sample rate for both channels.
163  *
164  * @param device_number Device number from the list of LMS_GetDeviceList.
165  *
166  * @param rate Sample rate in S/s.
167  */
168  void set_samp_rate(int device_number, double& rate);
169 
170  /**
171  * Set oversampling value for both channels
172  *
173  * @param device_number Device number from the list of LMS_GetDeviceList.
174  *
175  * @param oversample Oversampling value (0 (default),1,2,4,8,16,32).
176  */
177  void set_oversampling(int device_number, int oversample);
178 
179  /**
180  * Set RF frequency of both channels (RX and TX separately).
181  *
182  * @param device_number Device number from the list of LMS_GetDeviceList.
183  *
184  * @param direction Direction of samples RX(LMS_CH_RX), TX(LMS_CH_TX).
185  *
186  * @param channel selection: A(LMS_CH_0),B(LMS_CH_1).
187  *
188  * @param rf_freq RF frequency in Hz.
189  *
190  * @return returns RF frequency in Hz
191  */
192  double set_rf_freq(int device_number, bool direction, int channel, float rf_freq);
193 
194  /**
195  * Perform device calibration.
196  *
197  * @param device_number Device number from the list of LMS_GetDeviceList.
198  *
199  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_RX).
200  *
201  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
202  *
203  * @param bandwidth Set calibration bandwidth in Hz.
204  *
205  */
206  void calibrate(int device_number, int direction, int channel, double bandwidth);
207 
208  /**
209  * Set which antenna is used
210  *
211  * @param device_number Device number from the list of LMS_GetDeviceList.
212  *
213  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
214  *
215  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_RX).
216  *
217  * @param antenna Antenna to set: None(0), LNAH(1), LNAL(2), LNAW(3) for RX
218  * None(0), BAND1(1), BAND(2), NONE(3) for TX
219  *
220  */
221  void set_antenna(int device_number, int channel, int direction, int antenna);
222 
223  /**
224  * Set analog filters.
225  *
226  * @param device_number Device number from the list of LMS_GetDeviceList.
227  *
228  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_TX).
229  *
230  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
231  *
232  * @param analog_bandw Channel filter bandwidth in Hz.
233  */
234  double set_analog_filter(int device_number,
235  bool direction,
236  int channel,
237  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,
251  bool direction,
252  int channel,
253  double digital_bandw);
254 
255  /**
256  * Set the combined gain value in dB
257  * This function computes and sets the optimal gain values of various amplifiers
258  * that are present in the device based on desired gain value in dB.
259  *
260  * @note actual gain depends on LO frequency and analog LPF configuration and
261  * resulting output signal level may be different when those values are changed
262  *
263  * @param device_number Device number from the list of LMS_GetDeviceList.
264  *
265  * @param direction Select RX or TX.
266  *
267  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
268  *
269  * @param gain_dB Desired gain: [0,70] RX, [0,60] TX.
270  */
271  unsigned set_gain(int device_number, bool direction, int channel, unsigned gain_dB);
272 
273  /**
274  * Set NCO (numerically controlled oscillator).
275  * By selecting NCO frequency
276  * configure NCO. When NCO frequency is 0, NCO is off.
277  *
278  * @param device_number Device number from the list of LMS_GetDeviceList.
279  *
280  * @param direction Select RX or TX.
281  *
282  * @param channel Channel index.
283  *
284  * @param nco_freq NCO frequency in Hz.
285  */
286  void set_nco(int device_number, bool direction, int channel, float nco_freq);
287 
288  void disable_DC_corrections(int device_number);
289 
290  /**
291  * Set TCXO DAC.
292  * @note Care must be taken as this parameter is returned to default value only after
293  * power off.
294  * @note LimeSDR-Mini default value is 180 range is [0,255]
295  * LimeSDR-USB default value is 125 range is [0,255]
296  * LimeSDR-PCIe default value is 134 range is [0,255]
297  * LimeNET-Micro default value is 30714 range is [0,65535]
298  *
299  * @param device_number Device number from the list of LMS_GetDeviceList.
300  *
301  * @param dacVal DAC value (0-65535)
302  */
303  void set_tcxo_dac(int device_number, uint16_t dacVal);
304 };
305 
306 
307 #endif
double set_analog_filter(int device_number, bool direction, int channel, double analog_bandw)
void disable_DC_corrections(int device_number)
void close_all_devices()
Definition: device_handler.h:40
static device_handler & getInstance()
Definition: device_handler.h:76
void close_device(int device_number, int block_type)
void set_oversampling(int device_number, int oversample)
int open_device(std::string &serial)
void set_samp_rate(int device_number, double &rate)
void set_nco(int device_number, bool direction, int channel, float nco_freq)
void check_blocks(int device_number, int block_type, int channel_mode, const std::string &filename)
void error(int device_number)
void set_antenna(int device_number, int channel, int direction, int antenna)
std::recursive_mutex block_mutex
Definition: device_handler.h:83
double set_digital_filter(int device_number, bool direction, int channel, double digital_bandw)
void enable_channels(int device_number, int channel_mode, bool direction)
lms_device_t * get_device(int device_number)
void settings_from_file(int device_number, const std::string &filename, int *antenna_tx)
void set_tcxo_dac(int device_number, uint16_t dacVal)
unsigned set_gain(int device_number, bool direction, int channel, unsigned gain_dB)
double set_rf_freq(int device_number, bool direction, int channel, float rf_freq)
void calibrate(int device_number, int direction, int channel, double bandwidth)