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 * This software 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 * This software 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 this software; 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 <gnuradio/logger.h>
27#include <math.h>
28#include <cmath>
29#include <iostream>
30#include <list>
31#include <mutex>
32#include <string>
33#include <vector>
34
35#define LMS_CH_0 0
36#define LMS_CH_1 1
37
38#define LimeSDR_Mini 1
39#define LimeNET_Micro 2
40#define LimeSDR_USB 3
41
43{
44private:
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 int rx_channel = 0;
67 int tx_channel = 0;
68 rfe_dev_t* rfe_dev = nullptr;
69 } rfe_device;
70
71
72 // Device list
73 lms_info_str_t* list = new lms_info_str_t[20];
74 // Device vector. Adds devices from the list
75 std::vector<device> device_vector;
76 // Run close_all_devices once with this flag
77 bool close_flag = false;
78 // Loggers
79 gr::logger_ptr d_logger;
80 gr::logger_ptr d_debug_logger;
81
84 void operator=(device_handler const&);
85
86
87public:
89 {
90 static device_handler instance;
91 return instance;
92 }
94
95 mutable std::recursive_mutex block_mutex;
96
97
98 /**
99 * Print device error and close all devices.
100 *
101 * @param device_number Device number from the list of LMS_GetDeviceList.
102 */
103 void error(int device_number);
104
105 /**
106 * Get device connection handler in order to configure it.
107 *
108 * @param device_number Device number from the list of LMS_GetDeviceList.
109 */
110 lms_device_t* get_device(int device_number);
111
112 /**
113 * Connect to the device and create singletone.
114 *
115 * @param serial Device serial from the list of LMS_GetDeviceList.
116 */
117 int open_device(std::string& serial);
118
119 /**
120 * Disconnect from the 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 void close_device(int device_number, int block_type);
127
128 /**
129 * Disconnect from all devices.
130 */
132
133 /**
134 * Check what blocks are used for single device.
135 *
136 * @param device_number Device number from the list of LMS_GetDeviceList.
137 *
138 * @param block_type Source block(1), Sink block(2).
139 *
140 * @param channel_mode Channel A(0), Channel B(1), MIMO(2)
141 *
142 * @param filename Path to file if file switch is turned on.
143 */
144 void check_blocks(int device_number,
145 int block_type,
146 int channel_mode,
147 const std::string& filename);
148
149 /**
150 * Load settings from .ini file.
151 *
152 * @param device_number Device number from the list of LMS_GetDeviceList.
153 *
154 * @param filename Path to file if file switch is turned on.
155 *
156 * @param antenna_tx Pointer to TX antenna, so PA path would be updated in sink
157 * block
158 */
159 void
160 settings_from_file(int device_number, const std::string& filename, int* antenna_tx);
161
162 /**
163 * Set used channels
164 *
165 * @param device_number Device number from the list of LMS_GetDeviceList.
166 *
167 * @param channel_mode Channel A(0), Channel B(1), MIMO(2)
168 *
169 * @param direction Direction of samples RX(LMS_CH_RX), TX(LMS_CH_RX).
170 */
171 void enable_channels(int device_number, int channel_mode, bool direction);
172
173 /**
174 * Set the same sample rate for both channels.
175 *
176 * @param device_number Device number from the list of LMS_GetDeviceList.
177 *
178 * @param rate Sample rate in S/s.
179 */
180 void set_samp_rate(int device_number, double& rate);
181
182 /**
183 * Set oversampling value for both channels
184 *
185 * @param device_number Device number from the list of LMS_GetDeviceList.
186 *
187 * @param oversample Oversampling value (0 (default),1,2,4,8,16,32).
188 */
189 void set_oversampling(int device_number, int oversample);
190
191 /**
192 * Set RF frequency of both channels (RX and TX separately).
193 *
194 * @param device_number Device number from the list of LMS_GetDeviceList.
195 *
196 * @param direction Direction of samples RX(LMS_CH_RX), TX(LMS_CH_TX).
197 *
198 * @param channel selection: A(LMS_CH_0),B(LMS_CH_1).
199 *
200 * @param rf_freq RF frequency in Hz.
201 *
202 * @return returns RF frequency in Hz
203 */
204 double set_rf_freq(int device_number, bool direction, int channel, float rf_freq);
205
206 /**
207 * Perform device calibration.
208 *
209 * @param device_number Device number from the list of LMS_GetDeviceList.
210 *
211 * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_RX).
212 *
213 * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
214 *
215 * @param bandwidth Set calibration bandwidth in Hz.
216 *
217 */
218 void calibrate(int device_number, int direction, int channel, double bandwidth);
219
220 /**
221 * Set which antenna is used
222 *
223 * @param device_number Device number from the list of LMS_GetDeviceList.
224 *
225 * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
226 *
227 * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_RX).
228 *
229 * @param antenna Antenna to set: None(0), LNAH(1), LNAL(2), LNAW(3) for RX
230 * None(0), BAND1(1), BAND(2), NONE(3) for TX
231 *
232 */
233 void set_antenna(int device_number, int channel, int direction, int antenna);
234
235 /**
236 * Set analog filters.
237 *
238 * @param device_number Device number from the list of LMS_GetDeviceList.
239 *
240 * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_TX).
241 *
242 * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
243 *
244 * @param analog_bandw Channel filter bandwidth in Hz.
245 */
246 double set_analog_filter(int device_number,
247 bool direction,
248 int channel,
249 double analog_bandw);
250
251 /**
252 * Set digital filters (GFIR).
253 *
254 * @param device_number Device number from the list of LMS_GetDeviceList.
255 *
256 * @param direction Direction of samples: RX(LMS_CH_RX),TX(LMS_CH_TX).
257 *
258 * @param channel Channel selection: A(LMS_CH_0),B(LMS_CH_1).
259 *
260 * @param digital_bandw Channel filter bandwidth in Hz.
261 */
262 double set_digital_filter(int device_number,
263 bool direction,
264 int channel,
265 double digital_bandw);
266
267 /**
268 * Set the combined gain value in dB
269 * This function computes and sets the optimal gain values of various amplifiers
270 * that are present in the device based on desired gain value in dB.
271 *
272 * @note actual gain depends on LO frequency and analog LPF configuration and
273 * resulting output signal level may be different when those values are changed
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 selection: A(LMS_CH_0),B(LMS_CH_1).
280 *
281 * @param gain_dB Desired gain: [0,73] dB
282 */
283 unsigned set_gain(int device_number, bool direction, int channel, unsigned gain_dB);
284
285 /**
286 * Set NCO (numerically controlled oscillator).
287 * By selecting NCO frequency
288 * configure NCO. When NCO frequency is 0, NCO is off.
289 *
290 * @param device_number Device number from the list of LMS_GetDeviceList.
291 *
292 * @param direction Select RX or TX.
293 *
294 * @param channel Channel index.
295 *
296 * @param nco_freq NCO frequency in Hz.
297 */
298 void set_nco(int device_number, bool direction, int channel, float nco_freq);
299 /**
300 * Disables LimeSDR boards DC corrections
301 */
302 void disable_DC_corrections(int device_number);
303
304 /**
305 * Set TCXO DAC.
306 * @note Care must be taken as this parameter is returned to default value only after
307 * power off.
308 * @note LimeSDR-Mini default value is 180 range is [0,255]
309 * LimeSDR-USB default value is 125 range is [0,255]
310 * LimeSDR-PCIe default value is 134 range is [0,255]
311 * LimeNET-Micro default value is 30714 range is [0,65535]
312 *
313 * @param device_number Device number from the list of LMS_GetDeviceList.
314 *
315 * @param dacVal DAC value (0-65535)
316 */
317 void set_tcxo_dac(int device_number, uint16_t dacVal);
318 /**
319 * Sets up LimeRFE device pointer so that automatic channel configuration could be
320 * made
321 * @param rfe_dev Pointer to LimeRFE device descriptor
322 */
323 void set_rfe_device(rfe_dev_t* rfe_dev);
324 /**
325 * Assigns configured LimeSDR channels to LimeRFE for automatic channel switching
326 */
328
329 /**
330 * Writes an LMS register by calling LMS_WriteLMSReg()
331 */
332 void write_lms_reg(int device_number, uint32_t address, uint16_t val);
333
334 // Set GPIO pin directions, one bit per pin
335 void set_gpio_dir(int device_number, uint8_t dir);
336
337 // Write GPIO outputs, one bit per pin
338 void write_gpio(int device_number, uint8_t out);
339
340 // Read GPIO inputs, one bit per pin
341 uint8_t read_gpio(int device_number);
342};
343
344
345#endif
Definition device_handler.h:43
void close_device(int device_number, int block_type)
lms_device_t * get_device(int device_number)
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)
void update_rfe_channels()
void write_gpio(int device_number, uint8_t out)
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:95
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)
void set_gpio_dir(int device_number, uint8_t dir)
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)
static device_handler & getInstance()
Definition device_handler.h:88
uint8_t read_gpio(int device_number)
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)