SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
devicelimesdr.cpp
Go to the documentation of this file.
1 // Copyright (C) 2017 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <cstdio>
19 #include <cstring>
20 #include <cmath>
21 #include "devicelimesdr.h"
22 
23 bool DeviceLimeSDR::setNCOFrequency(lms_device_t *device, bool dir_tx, std::size_t chan, bool enable, float frequency)
24 {
25  if (enable)
26  {
27  bool positive;
28  float_type freqs[LMS_NCO_VAL_COUNT];
29  float_type phos[LMS_NCO_VAL_COUNT];
30 
31  if (LMS_GetNCOFrequency(device, dir_tx, chan, freqs, phos) < 0)
32  {
33  fprintf(stderr, "DeviceLimeSDR::setNCOFrequency: cannot get NCO frequencies and phases\n");
34  }
35 
36  if (frequency < 0)
37  {
38  positive = false;
39  frequency = -frequency;
40  }
41  else
42  {
43  positive = true;
44  }
45 
46  freqs[0] = frequency;
47 
48  if (LMS_SetNCOFrequency(device, dir_tx, chan, freqs, 0.0f) < 0)
49  {
50  fprintf(stderr, "DeviceLimeSDR::setNCOFrequency: cannot set frequency to %f\n", frequency);
51  return false;
52  }
53 
54  if (LMS_SetNCOIndex(device, dir_tx, chan, 0, dir_tx^positive) < 0)
55  {
56  fprintf(stderr, "DeviceLimeSDR::setNCOFrequency: cannot set conversion direction %sfreq\n", positive ? "+" : "-");
57  return false;
58  }
59 
60  if (dir_tx)
61  {
62  if (LMS_WriteParam(device,LMS7param(CMIX_BYP_TXTSP),0) < 0) {
63  fprintf(stderr, "DeviceLimeSDR::setNCOFrequency: cannot enable Tx NCO\n");
64  return false;
65  }
66  }
67  else
68  {
69  if (LMS_WriteParam(device,LMS7param(CMIX_BYP_RXTSP),0) < 0) {
70  fprintf(stderr, "DeviceLimeSDR::setNCOFrequency: cannot enable Rx NCO\n");
71  return false;
72  }
73  }
74 
75  return true;
76  }
77  else
78  {
79  if (dir_tx)
80  {
81  if (LMS_WriteParam(device,LMS7param(CMIX_BYP_TXTSP),1) < 0) {
82  fprintf(stderr, "DeviceLimeSDR::setNCOFrequency: cannot disable Tx NCO\n");
83  return false;
84  }
85  }
86  else
87  {
88  if (LMS_WriteParam(device,LMS7param(CMIX_BYP_RXTSP),1) < 0) {
89  fprintf(stderr, "DeviceLimeSDR::setNCOFrequency: cannot disable Rx NCO\n");
90  return false;
91  }
92  }
93 
94  return true;
95  }
96 }
97 
98 bool DeviceLimeSDR::SetRFELNA_dB(lms_device_t *device, std::size_t chan, int value)
99 {
100  if (LMS_WriteParam(device, LMS7param(MAC), chan+1) < 0)
101  {
102  fprintf(stderr, "DeviceLimeSDR::SetRFELNA_dB: cannot set channel to #%lu\n", chan);
103  return false;
104  }
105 
106  if (value > 30) {
107  value = 30;
108  }
109 
110  int val = value - 30;
111 
112  int g_lna_rfe = 0;
113  if (val >= 0) g_lna_rfe = 15;
114  else if (val >= -1) g_lna_rfe = 14;
115  else if (val >= -2) g_lna_rfe = 13;
116  else if (val >= -3) g_lna_rfe = 12;
117  else if (val >= -4) g_lna_rfe = 11;
118  else if (val >= -5) g_lna_rfe = 10;
119  else if (val >= -6) g_lna_rfe = 9;
120  else if (val >= -9) g_lna_rfe = 8;
121  else if (val >= -12) g_lna_rfe = 7;
122  else if (val >= -15) g_lna_rfe = 6;
123  else if (val >= -18) g_lna_rfe = 5;
124  else if (val >= -21) g_lna_rfe = 4;
125  else if (val >= -24) g_lna_rfe = 3;
126  else if (val >= -27) g_lna_rfe = 2;
127  else g_lna_rfe = 1;
128 
129  if (LMS_WriteParam(device, LMS7param(G_LNA_RFE), g_lna_rfe) < 0)
130  {
131  fprintf(stderr, "DeviceLimeSDR::SetRFELNA_dB: cannot set LNA gain to %d (%d)\n", value, g_lna_rfe);
132  return false;
133  }
134 
135  return true;
136 }
137 
138 bool DeviceLimeSDR::SetRFETIA_dB(lms_device_t *device, std::size_t chan, int value)
139 {
140  if (LMS_WriteParam(device, LMS7param(MAC), chan+1) < 0)
141  {
142  fprintf(stderr, "DeviceLimeSDR::SetRFETIA_dB: cannot set channel to #%lu\n", chan);
143  return false;
144  }
145 
146  if (value > 3) {
147  value = 3;
148  } else if (value < 1) {
149  value = 1;
150  }
151 
152  int g_tia_rfe = value;
153 
154  if (LMS_WriteParam(device, LMS7param(G_TIA_RFE), g_tia_rfe) < 0)
155  {
156  fprintf(stderr, "DeviceLimeSDR::SetRFELNA_dB: cannot set TIA gain to %d (%d)\n", value, g_tia_rfe);
157  return false;
158  }
159 
160  return true;
161 }
162 
163 bool DeviceLimeSDR::SetRBBPGA_dB(lms_device_t *device, std::size_t chan, float value)
164 {
165  if (LMS_WriteParam(device, LMS7param(MAC), chan+1) < 0)
166  {
167  fprintf(stderr, "DeviceLimeSDR::SetRBBPGA_dB: cannot set channel to #%lu\n", chan);
168  return false;
169  }
170 
171  int g_pga_rbb = (int)(value + 12.5);
172  if (g_pga_rbb > 0x1f) g_pga_rbb = 0x1f;
173  if (g_pga_rbb < 0) g_pga_rbb = 0;
174 
175  if (LMS_WriteParam(device, LMS7param(G_PGA_RBB), g_pga_rbb) < 0)
176  {
177  fprintf(stderr, "DeviceLimeSDR::SetRBBPGA_dB: cannot set G_PGA_RBB to %d\n", g_pga_rbb);
178  return false;
179  }
180 
181  int rcc_ctl_pga_rbb = (430.0*pow(0.65, (g_pga_rbb/10.0))-110.35)/20.4516 + 16;
182 
183  int c_ctl_pga_rbb = 0;
184  if (g_pga_rbb < 8) { c_ctl_pga_rbb = 3; }
185  if (8 <= g_pga_rbb && g_pga_rbb < 13) { c_ctl_pga_rbb = 2; }
186  if (13 <= g_pga_rbb && g_pga_rbb < 21) { c_ctl_pga_rbb = 1; }
187  if (21 <= g_pga_rbb) { c_ctl_pga_rbb = 0; }
188 
189  if (LMS_WriteParam(device, LMS7param(RCC_CTL_PGA_RBB), rcc_ctl_pga_rbb) < 0)
190  {
191  fprintf(stderr, "DeviceLimeSDR::SetRBBPGA_dB: cannot set RCC_CTL_PGA_RBB to %d\n", rcc_ctl_pga_rbb);
192  return false;
193  }
194 
195  if (LMS_WriteParam(device, LMS7param(C_CTL_PGA_RBB), c_ctl_pga_rbb) < 0)
196  {
197  fprintf(stderr, "DeviceLimeSDR::SetRBBPGA_dB: cannot set C_CTL_PGA_RBB to %d\n", c_ctl_pga_rbb);
198  return false;
199  }
200 
201  return true;
202 }
203 
204 bool DeviceLimeSDR::setRxAntennaPath(lms_device_t *device, std::size_t chan, int path)
205 {
206 // if (LMS_WriteParam(device, LMS7param(MAC), chan+1) < 0)
207 // {
208 // fprintf(stderr, "DeviceLimeSDR::setAntennaPath: cannot set channel to #%lu\n", chan);
209 // return false;
210 // }
211 //
212 // int sel_path_rfe = 0;
213 // switch ((PathRFE) path)
214 // {
215 // case PATH_RFE_NONE: sel_path_rfe = 0; break;
216 // case PATH_RFE_LNAH: sel_path_rfe = 1; break;
217 // case PATH_RFE_LNAL: sel_path_rfe = 2; break;
218 // case PATH_RFE_LNAW: sel_path_rfe = 3; break;
219 // case PATH_RFE_LB1: sel_path_rfe = 3; break;
220 // case PATH_RFE_LB2: sel_path_rfe = 2; break;
221 // }
222 //
223 // int pd_lna_rfe = 1;
224 // switch ((PathRFE) path)
225 // {
226 // case PATH_RFE_LNAH:
227 // case PATH_RFE_LNAL:
228 // case PATH_RFE_LNAW: pd_lna_rfe = 0; break;
229 // default: break;
230 // }
231 //
232 // int pd_rloopb_1_rfe = (path == (int) PATH_RFE_LB1) ? 0 : 1;
233 // int pd_rloopb_2_rfe = (path == (int) PATH_RFE_LB2) ? 0 : 1;
234 // int en_inshsw_l_rfe = (path == (int) PATH_RFE_LNAL ) ? 0 : 1;
235 // int en_inshsw_w_rfe = (path == (int) PATH_RFE_LNAW) ? 0 : 1;
236 // int en_inshsw_lb1_rfe = (path == (int) PATH_RFE_LB1) ? 0 : 1;
237 // int en_inshsw_lb2_rfe = (path == (int) PATH_RFE_LB2) ? 0 : 1;
238 //
239 // int ret = 0;
240 //
241 // ret += LMS_WriteParam(device, LMS7param(PD_LNA_RFE), pd_lna_rfe);
242 // ret += LMS_WriteParam(device, LMS7param(PD_RLOOPB_1_RFE), pd_rloopb_1_rfe);
243 // ret += LMS_WriteParam(device, LMS7param(PD_RLOOPB_2_RFE), pd_rloopb_2_rfe);
244 // ret += LMS_WriteParam(device, LMS7param(EN_INSHSW_LB1_RFE), en_inshsw_lb1_rfe);
245 // ret += LMS_WriteParam(device, LMS7param(EN_INSHSW_LB2_RFE), en_inshsw_lb2_rfe);
246 // ret += LMS_WriteParam(device, LMS7param(EN_INSHSW_L_RFE), en_inshsw_l_rfe);
247 // ret += LMS_WriteParam(device, LMS7param(EN_INSHSW_W_RFE), en_inshsw_w_rfe);
248 // ret += LMS_WriteParam(device, LMS7param(SEL_PATH_RFE), sel_path_rfe);
249 //
250 // if (ret < 0)
251 // {
252 // fprintf(stderr, "DeviceLimeSDR::setAntennaPath: cannot set channel #%lu to %d\n", chan, path);
253 // return false;
254 // }
255 //
256 // //enable/disable the loopback path
257 // const bool loopback = (path == (int) PATH_RFE_LB1) or (path == (int) PATH_RFE_LB2);
258 //
259 // if (LMS_WriteParam(device, LMS7param(EN_LOOPB_TXPAD_TRF), loopback ? 1 : 0) < 0)
260 // {
261 // fprintf(stderr, "DeviceLimeSDR::setAntennaPath: cannot %sset loopback on channel #%lu\n", loopback ? "" : "re", chan);
262 // return false;
263 // }
264 //
265 // //update external band-selection to match
266 // //this->UpdateExternalBandSelect();
267 //
268 // return true;
269 
270  switch ((PathRxRFE) path)
271  {
272  case PATH_RFE_LNAH:
273  if (LMS_SetAntenna(device, LMS_CH_RX, chan, 1) < 0)
274  {
275  fprintf(stderr, "DeviceLimeSDR::setRxAntennaPath: cannot set to LNAH\n");
276  return false;
277  }
278  break;
279  case PATH_RFE_LNAL:
280  if (LMS_SetAntenna(device, LMS_CH_RX, chan, 2) < 0)
281  {
282  fprintf(stderr, "DeviceLimeSDR::setRxAntennaPath: cannot set to LNAL\n");
283  return false;
284  }
285  break;
286  case PATH_RFE_LNAW:
287  if (LMS_SetAntenna(device, LMS_CH_RX, chan, 3) < 0)
288  {
289  fprintf(stderr, "DeviceLimeSDR::setRxAntennaPath: cannot set to LNAW\n");
290  return false;
291  }
292  break;
293  case PATH_RFE_LB1:
294  if (LMS_SetAntenna(device, LMS_CH_TX, chan, 1) < 0)
295  {
296  fprintf(stderr, "DeviceLimeSDR::setRxAntennaPath: cannot set to Loopback TX1\n");
297  return false;
298  }
299  break;
300  case PATH_RFE_LB2:
301  if (LMS_SetAntenna(device, LMS_CH_TX, chan, 2) < 0)
302  {
303  fprintf(stderr, "DeviceLimeSDR::setRxAntennaPath: cannot set to Loopback TX2\n");
304  return false;
305  }
306  break;
307  case PATH_RFE_RX_NONE:
308  default:
309  if (LMS_SetAntenna(device, LMS_CH_RX, chan, 0) < 0)
310  {
311  fprintf(stderr, "DeviceLimeSDR::setRxAntennaPath: cannot set to none\n");
312  return false;
313  }
314  }
315 
316  return true;
317 }
318 
319 bool DeviceLimeSDR::setTxAntennaPath(lms_device_t *device, std::size_t chan, int path)
320 {
321  switch ((PathTxRFE) path)
322  {
323  case PATH_RFE_TXRF1:
324  if (LMS_SetAntenna(device, LMS_CH_TX, chan, 1) < 0)
325  {
326  fprintf(stderr, "DeviceLimeSDR::setTxAntennaPath: cannot set to TXRF1\n");
327  return false;
328  }
329  break;
330  case PATH_RFE_TXRF2:
331  if (LMS_SetAntenna(device, LMS_CH_TX, chan, 2) < 0)
332  {
333  fprintf(stderr, "DeviceLimeSDR::setTxAntennaPath: cannot set to TXRF2\n");
334  return false;
335  }
336  break;
337  case PATH_RFE_TX_NONE:
338  default:
339  if (LMS_SetAntenna(device, LMS_CH_TX, chan, 0) < 0)
340  {
341  fprintf(stderr, "DeviceLimeSDR::setTxAntennaPath: cannot set to none\n");
342  return false;
343  }
344  }
345 
346  return true;
347 }
348 
349 bool DeviceLimeSDR::setClockSource(lms_device_t *device, bool extClock, uint32_t extClockFrequency)
350 {
351  if (extClock)
352  {
353  if (LMS_SetClockFreq(device, LMS_CLOCK_EXTREF, (float) extClockFrequency) < 0)
354  {
355  fprintf(stderr, "DeviceLimeSDR::setClockSource: cannot set to external\n");
356  return false;
357  }
358  }
359  else
360  {
361  uint16_t vcoTrimValue;
362 
363  if (LMS_VCTCXORead(device, &vcoTrimValue))
364  {
365  fprintf(stderr, "DeviceLimeSDR::setClockSource: cannot read VCTXO trim value\n");
366  return false;
367  }
368 
369  if (LMS_VCTCXOWrite(device, vcoTrimValue))
370  {
371  fprintf(stderr, "DeviceLimeSDR::setClockSource: cannot write VCTXO trim value\n");
372  return false;
373  }
374  }
375 
376  return true;
377 }
static bool SetRFELNA_dB(lms_device_t *device, std::size_t chan, int value)
static bool setTxAntennaPath(lms_device_t *device, std::size_t chan, int path)
static bool SetRBBPGA_dB(lms_device_t *device, std::size_t chan, float value)
static bool SetRFETIA_dB(lms_device_t *device, std::size_t chan, int value)
unsigned int uint32_t
Definition: rtptypes_win.h:46
unsigned short uint16_t
Definition: rtptypes_win.h:44
static bool setNCOFrequency(lms_device_t *device, bool dir_tx, std::size_t chan, bool enable, float frequency)
static bool setClockSource(lms_device_t *device, bool extClock, uint32_t extClockFrequency)
static bool setRxAntennaPath(lms_device_t *device, std::size_t chan, int path)