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 <iostream>
25 #include <math.h>
26 #include <cmath>
27 #include <vector>
28 #include <LimeSuite.h>
29 #include <lime/LMS7002M_parameters.h>
30 
32 {
33 private:
34 
35  int open_devices = 0;
36 
37  bool list_read = false;
38  int device_count;
39 
40  struct device
41  {
42  // Device address
43  lms_device_t* address = NULL;
44 
45  // Flags and variables used to check
46  // shared settings and blocks usage
47  bool source_flag = false;
48  bool sink_flag = false;
49  bool mini_switch_workaround_flag = false;
50  int source_device_type = -1;
51  int sink_device_type = -1;
52  int source_chip_mode = -1;
53  int sink_chip_mode = -1;
54  double source_sample_rate = -1;
55  double sink_sample_rate = -1;
56  size_t source_oversample = -1;
57  size_t sink_oversample = -1;
58  int source_file_switch = -1;
59  int sink_file_switch = -1;
60  const char *source_filename = NULL;
61  const char *sink_filename = NULL;
62  };
63 
64  //Device list
65  lms_info_str_t* list = new lms_info_str_t[5];
66  // Device vector. Adds devices from the list
67  std::vector<device> device_vector;
68 
69  device_handler() {};
71  void operator=(device_handler const&);
72 
73  int LMS_CH_0 = 0;
74  int LMS_CH_1 = 1;
75 
76 public:
78  {
79  static device_handler instance;
80  return instance;
81  }
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 device_number Device number from the list of LMS_GetDeviceList.
102  *
103  * @param device_type LimeSDR-Mini(1), LimeSDR-USB(2).
104  */
105  void open_device(int device_number, int device_type);
106 
107  /**
108  * Disconnect from the device.
109  *
110  * @param device_number Device number from the list of LMS_GetDeviceList.
111  */
112  void close_device(int device_number);
113 
114  /**
115  * Disconnect from all devices.
116  */
117  void close_all_devices();
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 device_type LimeSDR-Mini(1), LimeSDR-USB(2).
125  *
126  * @param chip_mode SISO(1), MIMO(2).
127  *
128  * @param samp_rate Sample rate in Hz.
129  *
130  * @param oversample Oversampling value (0 (default),1,2,4,8,16,32).
131  *
132  * @param file_switch Load settings from file: NO(0),YES(1).
133  *
134  * @param filename Path to file if file switch is turned on.
135  */
136  void check_blocks(int device_number, int block_type, int device_type, int chip_mode, double sample_rate, size_t oversample, int file_switch, const char *filename);
137 
138  /**
139  * Load settings from .ini file.
140  *
141  * @param device_number Device number from the list of LMS_GetDeviceList.
142  *
143  * @param filename Path to file if file switch is turned on.
144  */
145  void settings_from_file(int device_number, const char* filename);
146 
147  /**
148  * Set chip mode (single-input single-output/multiple-input multiple-output)
149  * and check if the device supports it.
150  *
151  * @param device_number Device number from the list of LMS_GetDeviceList.
152  *
153  * @param device_type LimeSDR-Mini(1), LimeSDR-USB(2).
154  *
155  * @param chip_mode SISO(1), MIMO(2).
156  */
157  void set_chip_mode(int device_number, int device_type, int chip_mode, int channel, bool direction);
158 
159  /**
160  * Set the same sample rate for both channels.
161  *
162  * @param device_number Device number from the list of LMS_GetDeviceList.
163  *
164  * @param samp_rate Sample rate in S/s.
165  *
166  * @param oversample Oversampling value (0 (default),1,2,4,8,16,32).
167  */
168  void set_samp_rate(int device_number, const double rate, size_t oversample);
169 
170  /**
171  * Set sample rate for both channels (RX and TX seperately).
172  *
173  * @param device_number Device number from the list of LMS_GetDeviceList.
174  *
175  * @param direction Direction of samples RX(LMS_CH_RX), TX(LMS_CH_RX).
176  *
177  * @param samp_rate Sample rate in S/s.
178  *
179  * @param oversample Oversampling value (0 (default),1,2,4,8,16,32).
180  */
181  void set_samp_rate_dir(int device_number, const int direction, const double rate, size_t oversample);
182 
183  /**
184  * Set RF frequency of both channels (RX and TX seperately).
185  *
186  * @param device_number Device number from the list of LMS_GetDeviceList.
187  *
188  * @param device_type LimeSDR-Mini(1), LimeSDR-USB(2).
189  *
190  * @param direction Direction of samples RX(LMS_CH_RX), TX(LMS_CH_TX).
191  *
192  * @param rf_freq RF frequency in Hz.
193  */
194  void set_rf_freq(int device_number, int device_type, bool direction, int channel, float rf_freq);
195 
196  /**
197  * Perform device calibration.
198  *
199  * @param device_number Device number from the list of LMS_GetDeviceList.
200  *
201  * @param device_type LimeSDR-Mini(1), LimeSDR-USB(2).
202  *
203  * @param calibration Turn calibration: OFF(0),ON(1).
204  *
205  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_RX).
206  *
207  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
208  *
209  * @param bandwidth Set calibration bandwidth in Hz.
210  *
211  * @param rf_freq Pass RF frequency in Hz for calibration.
212  *
213  * @param path Pass path for calibration.
214  *
215  * @note rf_freq and path are necessary in order to perform calibration for LNAL (matching)
216  * and center frequencies below 30 MHz (NCO).
217  */
218  void calibrate(int device_number, int device_type, int calibration, int direction, int channel, double bandwidth, float rf_freq, int path);
219 
220  /**
221  * Workaround for LimeSDR-Mini switch bug. Call upon implementation.
222  *
223  * @param device_number Device number from the list of LMS_GetDeviceList.
224  */
225  void mini_switch_workaround(int device_number);
226 
227  /**
228  * LNA switch for LimeSDR-Mini.
229  *
230  * @param device_number Device number from the list of LMS_GetDeviceList.
231  *
232  * @param lna_path_mini LNA switch: LNAH(1),LNAW(3).
233  */
234  void set_lna_path_mini(int device_number, int lna_path_mini);
235 
236  /**
237  * Set LNA path.
238  *
239  * @param device_number Device number from the list of LMS_GetDeviceList.
240  *
241  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
242  *
243  * @param lna_path LNA path: no path(0),LNAH(1),LNAL(2),LNAW(3).
244  */
245  void set_lna_path(int device_number, int channel, int lna_path);
246 
247  /**
248  * PA switch for LimeSDR-Mini.
249  *
250  * @param device_number Device number from the list of LMS_GetDeviceList.
251  *
252  * @param pa_path_mini PA path: BAND1(1),BAND2(2).
253  */
254  void set_pa_path_mini(int device_number, int pa_path_mini);
255 
256  /**
257  * Set PA path.
258  *
259  * @param device_number Device number from the list of LMS_GetDeviceList.
260  *
261  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
262  *
263  * @param pa_path PA path: BAND1(1),BAND2(2).
264  */
265  void set_pa_path(int device_number, int channel, int pa_path);
266 
267  /**
268  * Set analog filters.
269  *
270  * @param device_number Device number from the list of LMS_GetDeviceList.
271  *
272  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_TX).
273  *
274  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
275  *
276  * @param analog_filter Turn analog filter: OFF(0),ON(1).
277  *
278  * @param analog_bandw Channel filter bandwidth in Hz.
279  */
280  void set_analog_filter(int device_number, bool direction, int channel, int analog_filter, float analog_bandw);
281 
282  /**
283  * Set digital filters (GFIR).
284  *
285  * @param device_number Device number from the list of LMS_GetDeviceList.
286  *
287  * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_TX).
288  *
289  * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
290  *
291  * @param digital_filter Turn digital filter: OFF(0),ON(1).
292  *
293  * @param digital_bandw Channel filter bandwidth in Hz.
294  */
295  void set_digital_filter(int device_number, bool direction, int channel, int digital_filter, float digital_bandw);
296 
297  /**
298  * Set the combined gain value in dB
299  * This function computes and sets the optimal gain values of various amplifiers
300  * that are present in the device based on desired gain value in dB.
301  *
302  * @note actual gain depends on LO frequency and analog LPF configuration and
303  * resulting output signal levle may be different when those values are changed
304  *
305  * @param device_number Device number from the list of LMS_GetDeviceList.
306  *
307  * @param direction Select RX or TX.
308  *
309  * @param channel Channel index.
310  *
311  * @param gain_dB Desired gain: [0,70] RX, [0,60] TX.
312  */
313  void set_gain(int device_number, bool direction, int channel, unsigned int gain_dB);
314 };
315 
316 
317 #endif
void mini_switch_workaround(int device_number)
void set_chip_mode(int device_number, int device_type, int chip_mode, int channel, bool direction)
void set_pa_path(int device_number, int channel, int pa_path)
void open_device(int device_number, int device_type)
void close_all_devices()
Definition: device_handler.h:31
static device_handler & getInstance()
Definition: device_handler.h:77
void set_gain(int device_number, bool direction, int channel, unsigned int gain_dB)
void set_samp_rate_dir(int device_number, const int direction, const double rate, size_t oversample)
void set_lna_path_mini(int device_number, int lna_path_mini)
void set_digital_filter(int device_number, bool direction, int channel, int digital_filter, float digital_bandw)
void error(int device_number)
void check_blocks(int device_number, int block_type, int device_type, int chip_mode, double sample_rate, size_t oversample, int file_switch, const char *filename)
void set_samp_rate(int device_number, const double rate, size_t oversample)
lms_device_t * get_device(int device_number)
void set_analog_filter(int device_number, bool direction, int channel, int analog_filter, float analog_bandw)
void set_lna_path(int device_number, int channel, int lna_path)
void close_device(int device_number)
void set_pa_path_mini(int device_number, int pa_path_mini)
void set_rf_freq(int device_number, int device_type, bool direction, int channel, float rf_freq)
void settings_from_file(int device_number, const char *filename)
void calibrate(int device_number, int device_type, int calibration, int direction, int channel, double bandwidth, float rf_freq, int path)