forked from cozycactus/SoapyRX888
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSettings.cpp
More file actions
354 lines (298 loc) · 9.86 KB
/
Copy pathSettings.cpp
File metadata and controls
354 lines (298 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "SoapyRX888.hpp"
#include <SoapySDR/Time.hpp>
#include <SoapySDR/Types.hpp>
#include <algorithm>
#include <cmath>
// AD8370 VGA step index (0..126) -> gain in dB, matching the SDDC RX888R2 host.
static double rx888_if_index_to_db(int i)
{
if (i > 18) return 20.0 * std::log10(0.409 * (i - 18 + 3));
return 20.0 * std::log10(0.059 * (i + 1));
}
// Nearest VGA step index for a requested gain in dB.
static int rx888_if_db_to_index(double db)
{
int best = 0;
double bestErr = 1e9;
for (int i = 0; i <= 126; i++)
{
double err = std::fabs(rx888_if_index_to_db(i) - db);
if (err < bestErr) { bestErr = err; best = i; }
}
return best;
}
SoapyRX888::SoapyRX888(const SoapySDR::Kwargs &args):
deviceId(-1),
dev(nullptr),
rxFormat(RX888_RX_FORMAT_INT16),
sampleRate(64000000),
centerFrequency(0),
rfAtten(0),
ifGainIndex(50),
numBuffers(DEFAULT_NUM_BUFFERS)
{
if (args.count("label") != 0) SoapySDR_logf(SOAPY_SDR_INFO, "Opening %s...", args.at("label").c_str());
//if a serial is not present, then findRX888 had zero devices enumerated
if (args.count("serial") == 0) throw std::runtime_error("No RX888 devices found!");
const auto serial = args.at("serial");
deviceId = rx888_get_index_by_serial(serial.c_str());
if (deviceId < 0) throw std::runtime_error("rx888_get_index_by_serial("+serial+") - " + std::to_string(deviceId));
SoapySDR_logf(SOAPY_SDR_DEBUG, "RX888 opening device %d", deviceId);
if (rx888_open(&dev, deviceId) != 0) {
throw std::runtime_error("Unable to open RX888 device");
}
}
SoapyRX888::~SoapyRX888(void)
{
//cleanup device handles
rx888_close(dev);
}
/*******************************************************************
* Identification API
******************************************************************/
std::string SoapyRX888::getDriverKey(void) const
{
return "RX888";
}
std::string SoapyRX888::getHardwareKey(void) const
{
return "RX888";
}
SoapySDR::Kwargs SoapyRX888::getHardwareInfo(void) const
{
//key/value pairs for any useful information
//this also gets printed in --probe
SoapySDR::Kwargs args;
args["origin"] = "https://github.com/qoelet/SoapyRX888";
args["index"] = std::to_string(deviceId);
return args;
}
/*******************************************************************
* Channels API
******************************************************************/
size_t SoapyRX888::getNumChannels(const int dir) const
{
return (dir == SOAPY_SDR_RX) ? 1 : 0;
}
bool SoapyRX888::getFullDuplex(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
return false;
}
/*******************************************************************
* Antenna API
******************************************************************/
std::vector<std::string> SoapyRX888::listAntennas(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
std::vector<std::string> antennas;
antennas.push_back("RX");
return antennas;
}
void SoapyRX888::setAntenna(const int direction, const size_t channel, const std::string &name)
{
(void)channel;
(void)name;
if (direction != SOAPY_SDR_RX)
{
throw std::runtime_error("setAntena failed: RX888 only supports RX");
}
}
std::string SoapyRX888::getAntenna(const int direction, const size_t channel) const
{
(void)channel;
(void)direction;
return "RX";
}
/*******************************************************************
* Frontend corrections API
******************************************************************/
bool SoapyRX888::hasDCOffsetMode(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
return false;
}
bool SoapyRX888::hasFrequencyCorrection(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
return false;
}
/*******************************************************************
* Gain API
******************************************************************/
std::vector<std::string> SoapyRX888::listGains(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
std::vector<std::string> gains;
gains.push_back("IF"); // AD8370 VGA — the main HF gain
gains.push_back("RF"); // DAT-31 input attenuator
return gains;
}
bool SoapyRX888::hasGainMode(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
return false;
}
void SoapyRX888::setGain(const int direction, const size_t channel, const std::string &name, const double value)
{
(void)direction;
(void)channel;
if (name == "IF")
{
ifGainIndex = rx888_if_db_to_index(value);
SoapySDR_logf(SOAPY_SDR_DEBUG, "Set IF (VGA) gain: %.1f dB -> index %d", value, ifGainIndex);
rx888_set_if_gain(dev, ifGainIndex);
}
else if (name == "RF")
{
rfAtten = std::min(0.0, value);
SoapySDR_logf(SOAPY_SDR_DEBUG, "Set RF attenuation: %.1f dB", rfAtten);
rx888_set_hf_attenuation(dev, rfAtten);
}
}
double SoapyRX888::getGain(const int direction, const size_t channel, const std::string &name) const
{
(void)direction;
(void)channel;
if (name == "IF") return rx888_if_index_to_db(ifGainIndex);
if (name == "RF") return rfAtten;
return 0;
}
SoapySDR::Range SoapyRX888::getGainRange(const int direction, const size_t channel, const std::string &name) const
{
(void)direction;
(void)channel;
if (name == "IF") // AD8370 VGA, 127 steps
return SoapySDR::Range(rx888_if_index_to_db(0), rx888_if_index_to_db(126));
if (name == "RF") // DAT-31 attenuator, 0.5 dB steps
return SoapySDR::Range(-31.5, 0.0, 0.5);
return SoapySDR::Range(0, 0);
}
void SoapyRX888::setFrequency(const int direction, const size_t channel, const std::string &name, const double frequency, const SoapySDR::Kwargs &args)
{
(void)direction;
(void)channel;
(void)args;
//The RX888 HF path is a direct-sampling receiver: the LTC2208 ADC digitizes
//the whole 0..Fs/2 band at once, with no tuner LO to retune. There is no
//hardware frequency to set, so we just record the requested center for the
//host (gqrx reads it back) and let the application tune digitally in-band.
if (name == "RF" || name.empty())
{
centerFrequency = frequency;
SoapySDR_logf(SOAPY_SDR_DEBUG, "Set center frequency: %f Hz (direct sampling, informational)", frequency);
}
}
double SoapyRX888::getFrequency(const int direction, const size_t channel, const std::string &name) const
{
(void)direction;
(void)channel;
if (name == "RF" || name.empty()) return centerFrequency;
return 0;
}
std::vector<std::string> SoapyRX888::listFrequencies(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
std::vector<std::string> names;
names.push_back("RF");
return names;
}
SoapySDR::RangeList SoapyRX888::getFrequencyRange(const int direction, const size_t channel, const std::string &name) const
{
(void)direction;
(void)channel;
SoapySDR::RangeList ranges;
//first Nyquist zone of the direct-sampling ADC: DC .. Fs/2
if (name == "RF" || name.empty())
ranges.push_back(SoapySDR::Range(0, sampleRate / 2.0));
return ranges;
}
SoapySDR::ArgInfoList SoapyRX888::getFrequencyArgsInfo(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
SoapySDR::ArgInfoList freqArgs;
// TODO: frequency arguments
return freqArgs;
}
/*******************************************************************
* Sample Rate API
******************************************************************/
void SoapyRX888::setSampleRate(const int direction, const size_t channel, const double rate)
{
(void)direction;
(void)channel;
long long ns = SoapySDR::ticksToTimeNs(ticks, sampleRate);
sampleRate = rate;
resetBuffer = true;
SoapySDR_logf(SOAPY_SDR_DEBUG, "Setting sample rate: %d", sampleRate);
int r = rx888_set_sample_rate(dev, sampleRate);
if (r == -EINVAL)
{
throw std::runtime_error("setSampleRate failed: RX888 does not support this sample rate");
}
if (r != 0)
{
throw std::runtime_error("setSampleRate failed");
}
sampleRate = rx888_get_sample_rate(dev);
ticks = SoapySDR::timeNsToTicks(ns, sampleRate);
}
double SoapyRX888::getSampleRate(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
return sampleRate;
}
std::vector<double> SoapyRX888::listSampleRates(const int direction, const size_t channel) const
{
(void)direction;
(void)channel;
std::vector<double> results;
results.push_back(250000);
results.push_back(500000);
results.push_back(1000000);
results.push_back(2000000);
results.push_back(4000000);
results.push_back(8000000);
results.push_back(16000000);
results.push_back(32000000);
results.push_back(64000000);
results.push_back(128000000);
results.push_back(150000000);
return results;
}
/*******************************************************************
* Time API
******************************************************************/
std::vector<std::string> SoapyRX888::listTimeSources(void) const
{
std::vector<std::string> results;
results.push_back("sw_ticks");
return results;
}
std::string SoapyRX888::getTimeSource(void) const
{
return "sw_ticks";
}
bool SoapyRX888::hasHardwareTime(const std::string &what) const
{
return what == "" || what == "sw_ticks";
}
long long SoapyRX888::getHardwareTime(const std::string &what) const
{
(void)what;
return SoapySDR::ticksToTimeNs(ticks, sampleRate);
}
void SoapyRX888::setHardwareTime(const long long timeNs, const std::string &what)
{
(void)what;
ticks = SoapySDR::timeNsToTicks(timeNs, sampleRate);
}