forked from szpajder/dumpvdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdrplay.c
358 lines (318 loc) · 11.4 KB
/
sdrplay.c
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
355
356
357
358
/*
* dumpvdl2 - a VDL Mode 2 message decoder and protocol analyzer
*
* Copyright (c) 2017 Fabrice Crohas <fcrohas@gmail.com>
* Copyright (c) 2017 Tomasz Lemiech <szpajder@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h> // fprintf
#include <stdlib.h> // calloc, strtol
#include <string.h> // strcmp
#include <unistd.h> // _exit, usleep
#include <mirsdrapi-rsp.h>
#include "sdrplay.h"
static int initialized = 0;
#define NUM_LNA_STATES 10 // Max number of LNA states of all hw types
static int lnaGRtables[NUM_HW_TYPES][NUM_LNA_STATES] = {
[HW_RSP1] = { 0, 24, 19, 43, 0, 0, 0, 0, 0, 0 },
[HW_RSP2] = { 0, 10, 15, 21, 24, 34, 39, 45, 64, 0 },
[HW_RSP1A] = { 0, 6, 12, 18, 20, 26, 32, 38, 57, 62 },
[HW_RSP2duo] = { 0, 10, 15, 21, 24, 34, 39, 45, 64, 0 }
};
static int num_lnaGRs[NUM_HW_TYPES] = {
[HW_RSP1] = 4,
[HW_RSP2] = 9,
[HW_RSP1A] = 10,
[HW_RSP2duo] = 9
};
static char *hw_descr[NUM_HW_TYPES] = {
[HW_RSP1] = "RSP1",
[HW_RSP2] = "RSP2",
[HW_RSP1A] = "RSP1A",
[HW_RSP2duo] = "RSP2duo",
};
static void sdrplay_streamCallback(short *xi, short *xq, unsigned int firstSampleNum, int grChanged,
int rfChanged, int fsChanged, unsigned int numSamples, unsigned int reset, unsigned int hwRemoved, void *cbContext) {
int i, j, count1, count2, new_buf_flag;
int end, input_index;
sdrplay_ctx_t *SDRPlay = (sdrplay_ctx_t*)cbContext;
if(numSamples == 0) {
return;
}
unsigned char *dptr = SDRPlay->sdrplay_data;
// data_index counts samples
// numSamples counts I/Q sample pairs
end = SDRPlay->data_index + (numSamples * 2);
count2 = end - (ASYNC_BUF_SIZE * ASYNC_BUF_NUMBER);
if(count2 < 0) count2 = 0; // count2 is samples wrapping around to start of buf
count1 = (numSamples * 2) - count2; // count1 is samples fitting before the end of buf
// flag is set if this packet takes us past a multiple of ASYNC_BUF_SIZE
new_buf_flag = (SDRPlay->data_index / ASYNC_BUF_SIZE == end / ASYNC_BUF_SIZE ? 0 : 1);
// now interleave data from I/Q into circular buffer
input_index = 0;
for (i = 0, j = SDRPlay->data_index * sizeof(short); i < count1 / 2; i++) {
// Copy I low / high part
dptr[j++] = xi[input_index] & 0xff;
dptr[j++] = (xi[input_index] >> 8) & 0xff;
// Copy Q low / high part
dptr[j++] = xq[input_index] & 0xff;
dptr[j++] = (xq[input_index] >> 8 ) & 0xff;
input_index++;
}
SDRPlay->data_index += count1;
if(SDRPlay->data_index >= ASYNC_BUF_SIZE * ASYNC_BUF_NUMBER) {
SDRPlay->data_index = 0; // pointer back to start of buffer */
}
// insert remaining samples at the start of the buffer
for (i = 0, j = SDRPlay->data_index * sizeof(short); i < count2 / 2; i++) {
// Copy I low / high part
dptr[j++] = xi[input_index] & 0xff;
dptr[j++] = (xi[input_index] >> 8) & 0xff;
// Copy Q low / high part
dptr[j++] = xq[input_index] & 0xff;
dptr[j++] = (xq[input_index] >> 8 ) & 0xff;
input_index++;
}
SDRPlay->data_index += count2;
// send ASYNC_BUF_SIZE samples downstream, if available
if(new_buf_flag) {
/* go back by one buffer length, then round down further to start of buffer */
end = SDRPlay->data_index - ASYNC_BUF_SIZE;
if(end < 0) end += ASYNC_BUF_SIZE * ASYNC_BUF_NUMBER;
end -= end % ASYNC_BUF_SIZE;
process_buf_short(&SDRPlay->sdrplay_data[end * sizeof(short)], ASYNC_BUF_SIZE * sizeof(short), SDRPlay->context);
}
}
static void sdrplay_streamCallback_legacy(short *xi, short *xq, unsigned int firstSampleNum, int grChanged,
int rfChanged, int fsChanged, unsigned int numSamples, unsigned int reset, void *cbContext) {
return sdrplay_streamCallback(xi, xq, firstSampleNum, grChanged,rfChanged, fsChanged, numSamples, reset, 0, cbContext);
}
static void sdrplay_gainCallback(unsigned int gRdB, unsigned int lnaGRdB, void *cbContext) {
debug_print("Gain change: gRdb=%d lnaGRdB=%d \n", gRdB, lnaGRdB);
}
static int sdrplay_verbose_device_search(char * const dev, sdrplay_hw_type *hw_type) {
*hw_type = HW_UNKNOWN;
int devIdx = -1;
if(dev == NULL) {
return -1;
}
mir_sdr_DeviceT devices[4];
unsigned int numDevs;
mir_sdr_ErrT err = mir_sdr_GetDevices(devices, &numDevs, 4);
if(err != mir_sdr_Success) {
fprintf(stderr, "Unable to enumerate connected SDRPlay devices, error %d\n", err);
return -1;
}
if(numDevs < 1) {
fprintf(stderr, "No RSP devices found\n");
return -1;
}
fprintf(stderr, "\nFound %d device(s):\n", numDevs);
for(int i = 0; i < numDevs; i++) {
fprintf(stderr, " %s %d: SN: %s\n",
devices[i].devAvail ? " " : "(in use)",
i,
devices[i].SerNo != NULL ? devices[i].SerNo : "<none>"
);
}
fprintf(stderr, "\n");
// Does the string look like a raw ID number?
char *endptr = dev;
long num = strtol(dev, &endptr, 0);
if(endptr[0] == '\0' && num >= 0 && num < numDevs) {
devIdx = (unsigned int)num;
goto dev_found;
}
// Does the string match a serial number?
for (int i = 0; i < numDevs; i++) {
if(devices[i].SerNo == NULL) {
continue;
} else if(strcmp(dev, devices[i].SerNo) != 0) {
continue;
}
devIdx = (unsigned int)i;
goto dev_found;
}
fprintf(stderr, "No matching devices found\n");
return -1;
dev_found:
if(devices[devIdx].devAvail != 1) {
fprintf(stderr, "Selected device #%d is not available\n", devIdx);
return -1;
}
if(devices[devIdx].hwVer == 1) {
*hw_type = HW_RSP1;
} else if(devices[devIdx].hwVer == 2) {
*hw_type = HW_RSP2;
} else if(devices[devIdx].hwVer == 3) {
*hw_type = HW_RSP2duo;
} else if(devices[devIdx].hwVer > 253) {
*hw_type = HW_RSP1A;
} else {
fprintf(stderr, "Selected device #%d is unsupported: hardware version %d\n",
devIdx, devices[devIdx].hwVer);
return -1;
}
fprintf(stderr, "Selected device #%d (type: %s SN: %s Hardware_version: %d)\n",
devIdx,
hw_descr[*hw_type],
(devices[devIdx].SerNo != NULL ? devices[devIdx].SerNo : "unknown"),
devices[devIdx].hwVer
);
return devIdx;
}
void sdrplay_init(vdl2_state_t * const ctx, char * const dev, char * const antenna,
uint32_t const freq, int const gr, int const ppm_error, int const enable_biast,
int const enable_notch_filter, int enable_agc) {
mir_sdr_ErrT err;
float ver;
sdrplay_ctx_t SDRPlay;
sdrplay_hw_type hw_type = HW_UNKNOWN;
SDRPlay.context = ctx;
err = mir_sdr_ApiVersion(&ver);
if((err != mir_sdr_Success) || (ver != MIR_SDR_API_VERSION)) {
fprintf(stderr, "Incorrect API version %f\n", ver);
_exit(1);
}
fprintf(stderr, "Using SDRPlay API version %.3f\n", ver);
#if DEBUG
mir_sdr_DebugEnable(1);
#endif
int devIdx = sdrplay_verbose_device_search(dev, &hw_type);
if(devIdx < 0) {
_exit(1);
}
err = mir_sdr_SetDeviceIdx(devIdx);
if(err != mir_sdr_Success) {
fprintf(stderr, "Unable to select device #%d, error %d\n", devIdx, err);
_exit(1);
}
if((hw_type == HW_RSP2) || (hw_type == HW_RSP2duo) ) {
if(enable_biast) {
fprintf(stderr, "Bias-T activated\n");
err = mir_sdr_RSPII_BiasTControl(1);
if(err != mir_sdr_Success) {
fprintf(stderr, "Unable to activate Bias-T, error %d\n", err);
_exit(1);
}
}
if(strcmp(antenna, "A") == 0) {
err = mir_sdr_RSPII_AntennaControl(mir_sdr_RSPII_ANTENNA_A);
} else if(strcmp(antenna, "B") == 0) {
err = mir_sdr_RSPII_AntennaControl(mir_sdr_RSPII_ANTENNA_B);
} else {
fprintf(stderr, "Invalid antenna port specified\n");
_exit(1);
}
if(err != mir_sdr_Success) {
fprintf(stderr, "Unable to select antenna port %s, error %d\n", antenna, err);
_exit(1);
}
fprintf(stderr, "Using antenna port %s\n", antenna);
if(enable_notch_filter) {
fprintf(stderr, "AM/FM notch filter enabled\n");
err = mir_sdr_RSPII_RfNotchEnable(1);
if(err != mir_sdr_Success) {
fprintf(stderr, "Unable to activate notch filter, error %d\n", err);
_exit(1);
}
}
}
err = mir_sdr_DCoffsetIQimbalanceControl(1, 0);
if(err != mir_sdr_Success) {
fprintf(stderr, "Failed to set DC/IQ correction, error %d\n", err);
_exit(1);
}
err = mir_sdr_SetPpm(ppm_error);
if(err != mir_sdr_Success) {
fprintf(stderr, "Unable to set frequency correction, error %d\n", err);
_exit(1);
}
fprintf(stderr, "Frequency correction set to %d ppm\n", ppm_error);
SDRPlay.sdrplay_data = XCALLOC(ASYNC_BUF_SIZE * ASYNC_BUF_NUMBER, sizeof(short));
ctx->sbuf = XCALLOC(ASYNC_BUF_SIZE, sizeof(float));
int gRdBsystem = gr;
if(gr == SDR_AUTO_GAIN) {
gRdBsystem = MIN_IF_GR; // too low, but we enable AGC, which shall correct this
}
int gRdb = 0;
int lna_state = -1;
// Find the correct LNA state setting using LNA Gr table
// Start from lowest LNA Gr
for (int i = 0; i < num_lnaGRs[hw_type]; i++) {
// Can requested gain reduction be reached with this LNA Gr?
if((gRdBsystem >= lnaGRtables[hw_type][i] + MIN_IF_GR) && (gRdBsystem <= lnaGRtables[hw_type][i] + MAX_IF_GR)) {
gRdb = gRdBsystem - lnaGRtables[hw_type][i];
lna_state = i;
fprintf(stderr, "Selected IF gain reduction: %d dB, LNA gain reduction: %d dB\n",
gRdb, lnaGRtables[hw_type][i]);
break;
}
}
// Bail out on impossible gain reduction setting
if(lna_state < 0) {
int min_gr = MIN_IF_GR + lnaGRtables[hw_type][0];
int max_gr = MAX_IF_GR + lnaGRtables[hw_type][num_lnaGRs[hw_type]-1];
if(hw_type == HW_RSP1A) {
max_gr += MIXER_GR; // other RSP types have mixer GR included in the highest LNA state
}
fprintf(stderr, "Gain reduction value is out of range (min=%d max=%d)\n", min_gr, max_gr);
_exit(1);
}
SDRPlay.data_index = 0;
int sdrplaySamplesPerPacket = 0;
err = mir_sdr_StreamInit (&gRdb, (double)SDRPLAY_RATE/1e6, (double)freq/1e6, mir_sdr_BW_1_536, mir_sdr_IF_Zero,
lna_state, &gRdBsystem, mir_sdr_USE_RSP_SET_GR, &sdrplaySamplesPerPacket,
(hw_type == HW_RSP2duo)?sdrplay_streamCallback:sdrplay_streamCallback_legacy, sdrplay_gainCallback, &SDRPlay);
if(err != mir_sdr_Success) {
fprintf(stderr, "Unable to initialize RSP stream, error %d\n", err);
_exit(1);
}
initialized = 1;
debug_print("Stream initialized (sdrplaySamplesPerPacket=%d gRdB=%d gRdBsystem=%d)\n",
sdrplaySamplesPerPacket, gRdb, gRdBsystem);
// If no GR has been specified, enable AGC with a default set point (unless configured otherwise)
if(gr == SDR_AUTO_GAIN && enable_agc == 0) {
enable_agc = DEFAULT_AGC_SETPOINT;
}
if(enable_agc != 0) {
err = mir_sdr_AgcControl(mir_sdr_AGC_5HZ, enable_agc, 0, 0, 0, 0, 0);
if(err != mir_sdr_Success) {
fprintf(stderr, "Unable to activate AGC, error %d\n", err);
_exit(1);
}
fprintf(stderr, "AGC activated with set point at %d dBFS\n", enable_agc);
} else {
err = mir_sdr_AgcControl(mir_sdr_AGC_DISABLE, DEFAULT_AGC_SETPOINT, 0, 0, 0, 0, 0);
if(err != mir_sdr_Success) {
fprintf(stderr, "Unable to deactivate AGC, error %d\n", err);
_exit(1);
}
}
if(mir_sdr_SetDcMode(4, 0) != mir_sdr_Success || mir_sdr_SetDcTrackTime(63) != mir_sdr_Success) {
fprintf(stderr, "Set DC tracking failed, %d\n", err);
_exit(1);
}
fprintf(stderr, "Device #%d started\n", devIdx);
while(!do_exit) {
usleep(1000000);
}
}
void sdrplay_cancel() {
if(initialized) {
mir_sdr_StreamUninit();
mir_sdr_ReleaseDeviceIdx();
}
}