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