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