-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplay_sdr.c
385 lines (324 loc) · 11.8 KB
/
play_sdr.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
* Copyright (C) 2012 by Steve Markgraf <[email protected]>
* Copyright (C) 2012-2013 by Hoernchen <[email protected]>
*
* 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 2 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/>.
*
* ************************************** THIS IS A FORK ******************* ORIGINAL COPYRIGHT SEE ABOVE.
*
*
__/\\\________/\\\__/\\\\\\\\\\\\\________/\\\\\\\\\_____/\\\\\\\\\\\\\\\__/\\\_______/\\\________/\\\_______
_\/\\\_______\/\\\_\/\\\/////////\\\____/\\\///////\\\__\/\\\///////////__\///\\\___/\\\/______/\\\\/\\\\____
_\/\\\_______\/\\\_\/\\\_______\/\\\___/\\\______\//\\\_\/\\\_______________\///\\\\\\/______/\\\//\////\\\__
_\/\\\\\\\\\\\\\\\_\/\\\\\\\\\\\\\\___\//\\\_____/\\\\\_\/\\\\\\\\\\\_________\//\\\\_______/\\\______\//\\\_
_\/\\\/////////\\\_\/\\\/////////\\\___\///\\\\\\\\/\\\_\/\\\///////___________\/\\\\______\//\\\______/\\\__
_\/\\\_______\/\\\_\/\\\_______\/\\\_____\////////\/\\\_\/\\\__________________/\\\\\\______\///\\\\/\\\\/___
_\/\\\_______\/\\\_\/\\\_______\/\\\___/\\________/\\\__\/\\\________________/\\\////\\\______\////\\\//_____
_\/\\\_______\/\\\_\/\\\\\\\\\\\\\/___\//\\\\\\\\\\\/___\/\\\______________/\\\/___\///\\\_______\///\\\\\\__
_\///________\///__\/////////////______\///////////_____\///______________\///_______\///__________\//////___
*
*
*
*
* SDRPlayPorts
* Ports of some parts of rtl-sdr for the SDRPlay (original: git://git.osmocom.org/rtl-sdr.git /)
* 2016: Fork by HB9FXQ (Frank Werner-Krippendorf, [email protected])
*
* Code changes I've done:
* - removed rtl_sdr related calls and replaced them with mir_* to work with the SDRPlay
* - removed various local variables
*
* 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 2 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 <stdint.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mirsdrapi-rsp.h"
#define DEFAULT_SAMPLE_RATE 2048000
#define DEFAULT_LNA 0;
#define DEFAULT_AGC_SETPOINT -30;
#define DEFAULT_FREQUENCY 100000000;
#define DEFAULT_RESULT_BITS 8; // more compatible with RTL_SDR
static int do_exit = 0;
int samplesPerPacket;
int resultBits = DEFAULT_RESULT_BITS;
int flipComplex = 0;
uint8_t *buffer8;
short *buffer16;
FILE *file;
void* context = NULL; /* currently unused - let the API the thread stuff */
void adjust_bw(int bwHz, mir_sdr_Bw_MHzT *ptr);
void adjust_if(int ifFreq, mir_sdr_If_kHzT *ptr);
void adjust_result_bits(int bits, int *ptrResultBits);
double atofs(char *s)
/* standard suffixes */
{
char last;
int len;
double suff = 1.0;
len = strlen(s);
last = s[len - 1];
s[len - 1] = '\0';
switch (last) {
case 'g':
case 'G':
suff *= 1e3;
case 'm':
case 'M':
suff *= 1e3;
case 'k':
case 'K':
suff *= 1e3;
suff *= atof(s);
s[len - 1] = last;
return suff;
}
s[len - 1] = last;
return atof(s);
}
void usage(void) {
fprintf(stderr,
"play_sdr, an I/Q recorder for SDRplay RSP receivers\n\n"
"Usage:\t -f frequency_to_tune_to (Hz)\n"
"\t[-s samplerate (default: 2048000 Hz)]\n"
"\t[-b Band width in kHz (default: 1536) possible values: 200 300 600 1536 5000 6000 7000 8000]\n"
"\t[-i IF in kHz (default: 0 (=Zero IF)) possible values: 0 450 1620 2048]\n"
"\t[-r ADC set point is in dBfs\n"
"(decibels relative to full scale) and will normally lie in the range 0dBfs (full scale) to -50dBfs (50dB below\n"
"full scale). Default: -30)]\n\n"
"\t[-l RSP LNA enable (default: 0, disabled)]\n"
"\t[-y Flipcomplex I-Q => Q-I (default: 0, disabled) 1 = enabled\n"
"\t[-x Result I/Q bit resolution (uint8 / short) (default: 8, possible values: 8 16)]\n"
"\t[-v Verbose mode, prints debug information. Default 0, 1 = enabled\n"
"\tfilename (a '-' dumps samples to stdout)\n\n");
exit(1);
}
void callbackGC(unsigned int gRdB, unsigned int lnaGRdB, void *cbContext)
{
return;
}
unsigned int firstBufferCallback = 1;
void streamCallback (short *xi, short *xq, unsigned int firstSampleNum, int grChanged, int rfChanged, int fsChanged, unsigned int numSamples, unsigned int reset, void *cbContext){
if(firstBufferCallback == 1 || reset == 1){
if (resultBits == 8) {
buffer8 = malloc(numSamples * 2 * sizeof(uint8_t));
}
else {
buffer16 = malloc(numSamples * 2 * sizeof(short));
}
firstBufferCallback = 0;
}
int j = 0;
int i = 0;
for (i = 0; i < numSamples; i++) {
if (resultBits == 8) {
if (flipComplex == 0) {
buffer8[j++] = (unsigned char) (xi[i] >> 8);
buffer8[j++] = (unsigned char) (xq[i] >> 8);
} else {
buffer8[j++] = (unsigned char) (xq[i] >> 8);
buffer8[j++] = (unsigned char) (xi[i] >> 8);
}
}
else {
if (flipComplex == 0) {
buffer16[j++] = (xi[i]);
buffer16[j++] = (xq[i]);
} else {
buffer16[j++] = (xq[i]);
buffer16[j++] = (xi[i]);
}
}
}
if (resultBits == 8) {
if (fwrite(buffer8, sizeof(uint8_t), numSamples*2, file) != (size_t) numSamples*2) {
fprintf(stderr, "Short write, samples lost, exiting 8!\n"); // TODO: take firstSampleNum into account
return;
}
}
else {
if (fwrite(buffer16, sizeof(short), numSamples * 2, file) != (size_t) numSamples*2) {
fprintf(stderr, "Short write, samples lost, exiting 16!\n"); // TODO: take firstSampleNum into account
return;
}
}
}
static void sighandler(int signum) {
fprintf(stderr, "Signal (%d) caught, exiting!\n", signum);
do_exit = 1;
}
int main(int argc, char **argv) {
struct sigaction sigact;
char *filename = NULL;
mir_sdr_ErrT r;
int opt;
int agcSetPoint = DEFAULT_AGC_SETPOINT;
int verbose = 0;
uint32_t frequency = DEFAULT_FREQUENCY;
uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
int rspLNA = DEFAULT_LNA;
int i, j;
mir_sdr_Bw_MHzT bandwidth = mir_sdr_BW_1_536;
mir_sdr_If_kHzT ifKhz = mir_sdr_IF_Zero;
while ((opt = getopt(argc, argv, "f:g:s:n:l:b:i:x:y:v:")) != -1) {
switch (opt) {
case 'f':
frequency = (uint32_t) atofs(optarg);
break;
case 'g':
agcSetPoint = (int) atof(optarg);
break;
case 's':
samp_rate = (uint32_t) atofs(optarg);
break;
case 'l':
rspLNA = atoi(optarg);
break;
case 'b':
adjust_bw(atoi(optarg), &bandwidth);
break;
case 'i':
adjust_if(atoi(optarg), &ifKhz);
break;
case 'x':
adjust_result_bits(atoi(optarg), &resultBits);
break;
case 'y':
flipComplex = atoi(optarg);
break;
case 'v':
verbose = atoi(optarg);
break;
default:
usage();
break;
}
}
if (argc <= optind) {
usage();
} else {
filename = argv[optind];
}
if (verbose == 1) {
fprintf(stderr, "[DEBUG] *************** play_sdr init summary *********************\n");
fprintf(stderr, "[DEBUG] LNA: %d\n", rspLNA);
fprintf(stderr, "[DEBUG] samp_rate: %d\n", samp_rate);
fprintf(stderr, "[DEBUG] agcSetPoint: %d\n", agcSetPoint);
fprintf(stderr, "[DEBUG] frequency: [Hz] %d / [MHz] %f\n", frequency, frequency / 1e6);
fprintf(stderr, "[DEBUG] bandwidth: [kHz] %d\n", bandwidth);
fprintf(stderr, "[DEBUG] IF: %d\n", ifKhz);
fprintf(stderr, "[DEBUG] Result I/Q bit resolution (bit): %d\n", resultBits);
fprintf(stderr, "[DEBUG] *************************************************************\n");
}
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
sigaction(SIGPIPE, &sigact, NULL);
if (strcmp(filename, "-") == 0) { /* Write samples to stdout */
file = stdout;
} else {
file = fopen(filename, "wb");
if (!file) {
fprintf(stderr, "Failed to open %s\n", filename);
goto out;
}
}
mir_sdr_AgcControl(1, agcSetPoint, 0, 0, 0, 0, rspLNA);
int infoOverallGr;
r = mir_sdr_StreamInit(&agcSetPoint, (samp_rate/1e6), (frequency/1e6), mir_sdr_BW_1_536, mir_sdr_IF_Zero, rspLNA, &infoOverallGr, 1 /* use internal gr tables acording to band */, &samplesPerPacket, streamCallback,
callbackGC, &context);
if (r != mir_sdr_Success) {
fprintf(stderr, "Failed to start SDRplay RSP device.\n");
exit(r);
}
mir_sdr_SetDcMode(4, 0);
mir_sdr_SetDcTrackTime(63);
fprintf(stderr, "Writing samples...\n");
while (!do_exit) {
sleep(1);
}
if (do_exit)
fprintf(stderr, "\nUser cancel, exiting...\n");
else
fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
if (file != stdout)
fclose(file);
if (resultBits == 8) {
free(buffer8);
}
else {
free(buffer16);
}
out:
return r >= 0 ? r : -r;
}
void adjust_result_bits(int bits, int *ptrResultBits) {
switch (bits) {
case 8:
case 16:
*ptrResultBits = bits;
return;
}
fprintf(stderr, "Invalid result I/Q resolution (-x) !\n");
usage();
}
void adjust_if(int ifFreq, mir_sdr_If_kHzT *ptrIFKhz) {
switch (ifFreq) {
case 0:
case 450:
case 1620:
case 2048:
*ptrIFKhz = ifFreq;
return;
}
fprintf(stderr, "Invalid IF frequency (-i) !\n");
usage();
}
void adjust_bw(int bwHz, mir_sdr_Bw_MHzT *ptrBw) {
switch (bwHz) {
case 200:
case 300:
case 600:
case 1536:
case 5000:
case 6000:
case 8000:
*ptrBw = bwHz;
return;
}
fprintf(stderr, "Invalid Band Width (-b) !\n");
usage();
}