Skip to content

Commit 9d5384a

Browse files
committed
Merge pull request #2 from hayguen/development
Development
2 parents 75a3e99 + aabe859 commit 9d5384a

File tree

6 files changed

+104
-61
lines changed

6 files changed

+104
-61
lines changed

include/rtl-sdr.h

+7
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ RTLSDR_API int rtlsdr_write_eeprom(rtlsdr_dev_t *dev, uint8_t *data,
143143
RTLSDR_API int rtlsdr_read_eeprom(rtlsdr_dev_t *dev, uint8_t *data,
144144
uint8_t offset, uint16_t len);
145145

146+
/*!
147+
* Set the frequency the device is tuned to.
148+
*
149+
* \param dev the device handle given by rtlsdr_open()
150+
* \param frequency in Hz
151+
* \return 0 on error, frequency in Hz otherwise
152+
*/
146153
RTLSDR_API int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq);
147154

148155
/*!

src/convenience/convenience.c

+21-13
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,21 @@ int verbose_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate)
162162

163163
int verbose_set_bandwidth(rtlsdr_dev_t *dev, uint32_t bandwidth)
164164
{
165-
int r;
166-
uint32_t applied_bw = 0;
167-
/* r = rtlsdr_set_tuner_bandwidth(dev, bandwidth); */
168-
r = rtlsdr_set_and_get_tuner_bandwidth(dev, bandwidth, &applied_bw, 1 /* =apply_bw */);
169-
if (r < 0) {
170-
fprintf(stderr, "WARNING: Failed to set bandwidth.\n");
171-
} else if (bandwidth > 0) {
172-
fprintf(stderr, "Bandwidth set to %u Hz resulted in %u Hz.\n", bandwidth, applied_bw);
173-
} else {
174-
fprintf(stderr, "Bandwidth set to automatic resulted in %u Hz.\n", applied_bw);
175-
}
176-
return r;
165+
int r;
166+
uint32_t applied_bw = 0;
167+
/* r = rtlsdr_set_tuner_bandwidth(dev, bandwidth); */
168+
r = rtlsdr_set_and_get_tuner_bandwidth(dev, bandwidth, &applied_bw, 1 /* =apply_bw */);
169+
if (r < 0) {
170+
fprintf(stderr, "WARNING: Failed to set bandwidth.\n");
171+
} else if (bandwidth > 0) {
172+
if (applied_bw)
173+
fprintf(stderr, "Bandwidth parameter %u Hz resulted in %u Hz.\n", bandwidth, applied_bw);
174+
else
175+
fprintf(stderr, "Set bandwidth parameter %u Hz.\n", bandwidth);
176+
} else {
177+
fprintf(stderr, "Bandwidth set to automatic resulted in %u Hz.\n", applied_bw);
178+
}
179+
return r;
177180
}
178181

179182
int verbose_direct_sampling(rtlsdr_dev_t *dev, int on)
@@ -198,7 +201,12 @@ int verbose_offset_tuning(rtlsdr_dev_t *dev)
198201
int r;
199202
r = rtlsdr_set_offset_tuning(dev, 1);
200203
if (r != 0) {
201-
fprintf(stderr, "WARNING: Failed to set offset tuning.\n");
204+
if ( r == -2 )
205+
fprintf(stderr, "WARNING: Failed to set offset tuning: tuner doesn't support offset tuning!\n");
206+
else if ( r == -3 )
207+
fprintf(stderr, "WARNING: Failed to set offset tuning: direct sampling not combinable with offset tuning!\n");
208+
else
209+
fprintf(stderr, "WARNING: Failed to set offset tuning.\n");
202210
} else {
203211
fprintf(stderr, "Offset tuning mode enabled.\n");
204212
}

src/librtlsdr.c

100755100644
File mode changed.

src/rtl_fm.c

100755100644
+41-28
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct dongle_state
108108
int dev_index;
109109
uint32_t freq;
110110
uint32_t rate;
111-
uint32_t bandwidth;
111+
uint32_t bandwidth;
112112
int gain;
113113
int16_t buf16[MAXIMUM_BUF_LENGTH];
114114
uint32_t buf_len;
@@ -197,15 +197,15 @@ void usage(void)
197197
"\t-f frequency_to_tune_to [Hz]\n"
198198
"\t use multiple -f for scanning (requires squelch)\n"
199199
"\t ranges supported, -f 118M:137M:25k\n"
200-
"\t[-v verbosity (default: 0)]\n"
200+
"\t[-v increase verbosity (default: 0)]\n"
201201
"\t[-M modulation (default: fm)]\n"
202202
"\t fm or nbfm or nfm, wbfm or wfm, raw or iq, am, usb, lsb\n"
203203
"\t wbfm == -M fm -s 170k -o 4 -A fast -r 32k -l 0 -E deemp\n"
204204
"\t raw mode outputs 2x16 bit IQ pairs\n"
205205
"\t[-s sample_rate (default: 24k)]\n"
206206
"\t[-d device_index (default: 0)]\n"
207-
"\t[-g tuner_gain (default: automatic)]\n"
208-
"\t[-w tuner_bandwidth (default: automatic)]\n"
207+
"\t[-g tuner_gain (default: automatic)]\n"
208+
"\t[-w tuner_bandwidth (default: automatic. enables offset tuning)]\n"
209209
"\t[-l squelch_level (default: 0/off)]\n"
210210
"\t[-L N prints levels every N calculations]\n"
211211
"\t output are comma separated values (csv):\n"
@@ -217,12 +217,14 @@ void usage(void)
217217
"\t[-E enable_option (default: none)]\n"
218218
"\t use multiple -E to enable multiple options\n"
219219
"\t edge: enable lower edge tuning\n"
220-
"\t rdc: enable dc blocking filter on raw I/Q data at capture rate\n"
221-
"\t adc: enable dc blocking filter on demodulated audio\n"
222-
"\t dc: same as adc\n"
220+
"\t rdc: enable dc blocking filter on raw I/Q data at capture rate\n"
221+
"\t adc: enable dc blocking filter on demodulated audio\n"
222+
"\t dc: same as adc\n"
223+
"\t rtlagc: enable rtl2832's digital agc (default: off)\n"
224+
"\t agc: same as rtlagc\n"
223225
"\t deemp: enable de-emphasis filter\n"
224-
"\t direct: enable direct sampling\n"
225-
"\t offset: enable offset tuning\n"
226+
"\t direct: enable direct sampling (bypasses tuner, uses rtl2832 xtal)\n"
227+
"\t offset: enable offset tuning (only e4000 tuner)\n"
226228
"\t[-q dc_avg_factor for option rdc (default: 9)]\n"
227229
"\tfilename ('-' means stdout)\n"
228230
"\t omitting the filename also uses stdout\n\n"
@@ -986,7 +988,7 @@ static void *controller_thread_fn(void *arg)
986988

987989
if (s->wb_mode) {
988990
if (verbosity)
989-
fprintf(stderr, "wbfm: adding 16000 Hz to every intput frequency\n");
991+
fprintf(stderr, "wbfm: adding 16000 Hz to every input frequency\n");
990992
for (i=0; i < s->freq_len; i++) {
991993
s->freqs[i] += 16000;}
992994
}
@@ -1001,7 +1003,8 @@ static void *controller_thread_fn(void *arg)
10011003
/* Set the frequency */
10021004
if (verbosity) {
10031005
fprintf(stderr, "verbose_set_frequency(%.0f Hz)\n", (double)dongle.freq);
1004-
fprintf(stderr, " frequency is away from parametrized one, to avoid negative impact from dc\n");
1006+
if (!dongle.offset_tuning)
1007+
fprintf(stderr, " frequency is away from parametrized one, to avoid negative impact from dc\n");
10051008
}
10061009
verbose_set_frequency(dongle.dev, dongle.freq);
10071010
fprintf(stderr, "Oversampling input by: %ix.\n", demod.downsample);
@@ -1158,12 +1161,13 @@ int main(int argc, char **argv)
11581161
int dev_given = 0;
11591162
int custom_ppm = 0;
11601163
int timeConstant = 75; /* default: U.S. 75 uS */
1164+
int rtlagc = 0;
11611165
dongle_init(&dongle);
11621166
demod_init(&demod);
11631167
output_init(&output);
11641168
controller_init(&controller);
11651169

1166-
while ((opt = getopt(argc, argv, "d:f:g:s:b:l:L:o:t:r:p:E:q:F:A:M:c:v:h:w:")) != -1) {
1170+
while ((opt = getopt(argc, argv, "d:f:g:s:b:l:L:o:t:r:p:E:q:F:A:M:c:h:w:v")) != -1) {
11671171
switch (opt) {
11681172
case 'd':
11691173
dongle.dev_index = verbose_device_search(optarg);
@@ -1227,6 +1231,8 @@ int main(int argc, char **argv)
12271231
dongle.direct_sampling = 1;}
12281232
if (strcmp("offset", optarg) == 0) {
12291233
dongle.offset_tuning = 1;}
1234+
if (strcmp("rtlagc", optarg) == 0 || strcmp("agc", optarg) == 0) {
1235+
rtlagc = 1;}
12301236
break;
12311237
case 'q':
12321238
demod.rdc_block_const = atoi(optarg);
@@ -1275,18 +1281,24 @@ int main(int argc, char **argv)
12751281
timeConstant = (int)atof(optarg);
12761282
break;
12771283
case 'v':
1278-
verbosity = (int)atof(optarg);
1284+
++verbosity;
1285+
break;
1286+
case 'w':
1287+
dongle.bandwidth = (uint32_t)atofs(optarg);
1288+
if (dongle.bandwidth)
1289+
dongle.offset_tuning = 1; /* automatically switch offset tuning, when using bandwidth filter */
12791290
break;
1280-
case 'w':
1281-
dongle.bandwidth = (uint32_t)atofs(optarg);
1282-
break;
12831291
case 'h':
1292+
case '?':
12841293
default:
12851294
usage();
12861295
break;
12871296
}
12881297
}
12891298

1299+
if (verbosity)
1300+
fprintf(stderr, "verbosity set to %d\n", verbosity);
1301+
12901302
/* quadruple sample_rate to limit to Δθ to ±π/2 */
12911303
demod.rate_in *= demod.post_downsample;
12921304

@@ -1346,24 +1358,25 @@ int main(int argc, char **argv)
13461358
verbose_gain_set(dongle.dev, dongle.gain);
13471359
}
13481360

1361+
rtlsdr_set_agc_mode(dongle.dev, rtlagc);
1362+
13491363
verbose_ppm_set(dongle.dev, dongle.ppm_error);
13501364

13511365
verbose_set_bandwidth(dongle.dev, dongle.bandwidth);
13521366

1353-
verbose_set_bandwidth(dongle.dev, dongle.bandwidth);
1354-
13551367
if (verbosity && dongle.bandwidth)
13561368
{
1357-
int r;
1358-
uint32_t in_bw, out_bw, last_bw = 0;
1359-
for ( in_bw = 1; in_bw < 3200; ++in_bw )
1360-
{
1361-
r = rtlsdr_set_and_get_tuner_bandwidth(dongle.dev, in_bw*1000, &out_bw, 0 /* =apply_bw */);
1362-
if ( r == 0 && ( out_bw != last_bw || in_bw == 1 ) )
1363-
fprintf(stderr, "device sets bandwidth %u Hz for bw para >= %u kHz\n", out_bw, in_bw );
1364-
last_bw = out_bw;
1365-
}
1366-
fprintf(stderr,"\n");
1369+
int r;
1370+
uint32_t in_bw, out_bw, last_bw = 0;
1371+
fprintf(stderr, "Supported bandwidth values in kHz:\n");
1372+
for ( in_bw = 1; in_bw < 3200; ++in_bw )
1373+
{
1374+
r = rtlsdr_set_and_get_tuner_bandwidth(dongle.dev, in_bw*1000, &out_bw, 0 /* =apply_bw */);
1375+
if ( r == 0 && out_bw != 0 && ( out_bw != last_bw || in_bw == 1 ) )
1376+
fprintf(stderr, "%s%.1f", (in_bw==1 ? "" : ", "), out_bw/1000.0 );
1377+
last_bw = out_bw;
1378+
}
1379+
fprintf(stderr,"\n");
13671380
}
13681381

13691382
if (strcmp(output.filename, "-") == 0) { /* Write samples to stdout */

src/rtl_tcp.c

+35-20
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ typedef struct { /* structure size must be multiple of 2 bytes */
7878

7979
static rtlsdr_dev_t *dev = NULL;
8080

81+
static int verbosity = 0;
82+
static uint32_t bandwidth = 0;
8183
static int global_numq = 0;
8284
static struct llist *ll_buffers = 0;
8385
static int llbuf_num = 500;
@@ -94,9 +96,10 @@ void usage(void)
9496
"\t[-s samplerate in Hz (default: 2048000 Hz)]\n"
9597
"\t[-b number of buffers (default: 15, set by library)]\n"
9698
"\t[-n max number of linked list buffers to keep (default: 500)]\n"
97-
"\t[-w rtlsdr device bandwidth (for R820T device)\n"
99+
"\t[-w rtlsdr device bandwidth [Hz] (for R820T device)]\n"
98100
"\t[-d device index (default: 0)]\n"
99-
"\t[-P ppm_error (default: 0)]\n");
101+
"\t[-P ppm_error (default: 0)]\n"
102+
"\t[-v increase verbosity (default: 0)]\n");
100103
exit(1);
101104
}
102105

@@ -203,7 +206,7 @@ static void *tcp_worker(void *arg)
203206

204207
pthread_mutex_lock(&ll_mutex);
205208
gettimeofday(&tp, NULL);
206-
ts.tv_sec = tp.tv_sec+5;
209+
ts.tv_sec = tp.tv_sec+1;
207210
ts.tv_nsec = tp.tv_usec * 1000;
208211
r = pthread_cond_timedwait(&cond, &ll_mutex, &ts);
209212
if(r == ETIMEDOUT) {
@@ -257,6 +260,8 @@ static int set_gain_by_index(rtlsdr_dev_t *_dev, unsigned int index)
257260
count = rtlsdr_get_tuner_gains(_dev, gains);
258261

259262
res = rtlsdr_set_tuner_gain(_dev, gains[index]);
263+
if (verbosity)
264+
fprintf(stderr, "set tuner gain to %.1f dB\n", gains[index] / 10.0);
260265

261266
free(gains);
262267
}
@@ -310,6 +315,7 @@ static void *command_worker(void *arg)
310315
case SET_SAMPLE_RATE:
311316
printf("set sample rate %d\n", ntohl(cmd.param));
312317
rtlsdr_set_sample_rate(dev, ntohl(cmd.param));
318+
/*verbose_set_bandwidth(dev, bandwidth);*/
313319
break;
314320
case SET_GAIN_MODE:
315321
printf("set gain mode %d\n", ntohl(cmd.param));
@@ -356,10 +362,11 @@ static void *command_worker(void *arg)
356362
printf("set tuner gain by index %d\n", ntohl(cmd.param));
357363
set_gain_by_index(dev, ntohl(cmd.param));
358364
break;
359-
case SET_TUNER_BANDWIDTH:
360-
printf("set tuner bandwidth to %i Hz\n", ntohl(cmd.param));
361-
rtlsdr_set_tuner_bandwidth(dev, ntohl(cmd.param));
362-
break;
365+
case SET_TUNER_BANDWIDTH:
366+
bandwidth = ntohl(cmd.param);
367+
printf("set tuner bandwidth to %i Hz\n", bandwidth);
368+
verbose_set_bandwidth(dev, bandwidth);
369+
break;
363370
default:
364371
break;
365372
}
@@ -372,7 +379,7 @@ int main(int argc, char **argv)
372379
int r, opt, i;
373380
char* addr = "127.0.0.1";
374381
int port = 1234;
375-
uint32_t frequency = 100000000, samp_rate = 2048000, bandwidth = 0;
382+
uint32_t frequency = 100000000, samp_rate = 2048000;
376383
struct sockaddr_in local, remote;
377384
uint32_t buf_num = 0;
378385
int dev_index = 0;
@@ -389,14 +396,15 @@ int main(int argc, char **argv)
389396
fd_set readfds;
390397
u_long blockmode = 1;
391398
dongle_info_t dongle_info;
399+
int gains[100];
392400
#ifdef _WIN32
393401
WSADATA wsd;
394402
i = WSAStartup(MAKEWORD(2,2), &wsd);
395403
#else
396404
struct sigaction sigact, sigign;
397405
#endif
398406

399-
while ((opt = getopt(argc, argv, "a:p:f:g:s:b:n:d:P:w:")) != -1) {
407+
while ((opt = getopt(argc, argv, "a:p:f:g:s:b:n:d:P:w:v")) != -1) {
400408
switch (opt) {
401409
case 'd':
402410
dev_index = verbose_device_search(optarg);
@@ -425,9 +433,12 @@ int main(int argc, char **argv)
425433
break;
426434
case 'P':
427435
ppm_error = atoi(optarg);
428-
break;
429-
case 'w':
430-
bandwidth = (uint32_t)atofs(optarg);
436+
break;
437+
case 'w':
438+
bandwidth = (uint32_t)atofs(optarg);
439+
break;
440+
case 'v':
441+
++verbosity;
431442
break;
432443
default:
433444
usage();
@@ -438,6 +449,9 @@ int main(int argc, char **argv)
438449
if (argc < optind)
439450
usage();
440451

452+
if (verbosity)
453+
fprintf(stderr, "verbosity set to %d\n", verbosity);
454+
441455
if (!dev_given) {
442456
dev_index = verbose_device_search("0");
443457
}
@@ -499,13 +513,7 @@ int main(int argc, char **argv)
499513
fprintf(stderr, "Tuner gain set to %f dB.\n", gain/10.0);
500514
}
501515

502-
r = rtlsdr_set_tuner_bandwidth(dev, bandwidth);
503-
if (r < 0)
504-
fprintf(stderr, "WARNING: Failed to set tuner bandwidth.\n");
505-
else if (bandwidth != 0)
506-
fprintf(stderr, "Tuner bandwidth set to %i.\n", bandwidth);
507-
else
508-
fprintf(stderr, "Tuner bandwidth set to automatic.\n");
516+
verbose_set_bandwidth(dev, bandwidth);
509517

510518
/* Reset endpoint before we start reading from it (mandatory) */
511519
r = rtlsdr_reset_buffer(dev);
@@ -571,9 +579,16 @@ int main(int argc, char **argv)
571579
if (r >= 0)
572580
dongle_info.tuner_type = htonl(r);
573581

574-
r = rtlsdr_get_tuner_gains(dev, NULL);
582+
r = rtlsdr_get_tuner_gains(dev, gains);
575583
if (r >= 0)
576584
dongle_info.tuner_gain_count = htonl(r);
585+
if (verbosity)
586+
{
587+
fprintf(stderr, "Supported gain values (%d): ", r);
588+
for (i = 0; i < r; i++)
589+
fprintf(stderr, "%.1f ", gains[i] / 10.0);
590+
fprintf(stderr, "\n");
591+
}
577592

578593
r = send(s, (const char *)&dongle_info, sizeof(dongle_info), 0);
579594
if (sizeof(dongle_info) != r)

src/tuner_r82xx.c

100755100644
File mode changed.

0 commit comments

Comments
 (0)