2 * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
3 * Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 #include "getopt/getopt.h"
41 #define DEFAULT_SAMPLE_RATE 2048000
42 #define DEFAULT_ASYNC_BUF_NUMBER 32
43 #define DEFAULT_BUF_LENGTH (16 * 16384)
44 #define MINIMAL_BUF_LENGTH 512
45 #define MAXIMAL_BUF_LENGTH (256 * 16384)
47 #define MHZ(x) ((x)*1000*1000)
49 #define PPM_DURATION 10
51 static int do_exit = 0;
52 static rtlsdr_dev_t *dev = NULL;
54 static int ppm_benchmark = 0;
55 static int64_t ppm_count = 0L;
56 static int64_t ppm_total = 0L;
59 static struct timespec ppm_start;
60 static struct timespec ppm_recent;
61 static struct timespec ppm_now;
65 static struct timeval tv;
71 "rtl_test, a benchmark tool for RTL2832 based DVB-T receivers\n\n"
73 "\t[-s samplerate (default: 2048000 Hz)]\n"
74 "\t[-d device_index (default: 0)]\n"
75 "\t[-t enable Elonics E4000 tuner benchmark]\n"
77 "\t[-p enable PPM error measurement]\n"
79 "\t[-b output_block_size (default: 16 * 16384)]\n"
80 "\t[-S force sync output (default: async)]\n");
86 sighandler(int signum)
88 if (CTRL_C_EVENT == signum) {
89 fprintf(stderr, "Signal caught, exiting!\n");
91 rtlsdr_cancel_async(dev);
97 static void sighandler(int signum)
99 fprintf(stderr, "Signal caught, exiting!\n");
101 rtlsdr_cancel_async(dev);
105 uint8_t bcnt, uninit = 1;
107 static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
109 uint32_t i, lost = 0;
117 for (i = 0; i < len; i++) {
119 lost += (buf[i] > bcnt) ? (buf[i] - bcnt) : (bcnt - buf[i]);
127 printf("lost at least %d bytes\n", lost);
129 if (!ppm_benchmark) {
132 ppm_count += (int64_t)len;
135 clock_gettime(CLOCK_REALTIME, &ppm_now);
137 gettimeofday(&tv, NULL);
138 ppm_now.tv_sec = tv.tv_sec;
139 ppm_now.tv_nsec = tv.tv_usec*1000;
141 if (ppm_now.tv_sec - ppm_recent.tv_sec > PPM_DURATION) {
142 ns = 1000000000L * (int64_t)(ppm_now.tv_sec - ppm_recent.tv_sec);
143 ns += (int64_t)(ppm_now.tv_nsec - ppm_recent.tv_nsec);
144 printf("real sample rate: %i\n",
145 (int)((1000000000L * ppm_count / 2L) / ns));
147 clock_gettime(CLOCK_REALTIME, &ppm_recent);
149 gettimeofday(&tv, NULL);
150 ppm_recent.tv_sec = tv.tv_sec;
151 ppm_recent.tv_nsec = tv.tv_usec*1000;
153 ppm_total += ppm_count / 2L;
159 void e4k_benchmark(void)
161 uint32_t freq, gap_start = 0, gap_end = 0;
162 uint32_t range_start = 0, range_end = 0;
164 fprintf(stderr, "Benchmarking E4000 PLL...\n");
166 /* find tuner range start */
167 for (freq = MHZ(70); freq > MHZ(1); freq -= MHZ(1)) {
168 if (rtlsdr_set_center_freq(dev, freq) < 0) {
174 /* find tuner range end */
175 for (freq = MHZ(2000); freq < MHZ(2300UL); freq += MHZ(1)) {
176 if (rtlsdr_set_center_freq(dev, freq) < 0) {
182 /* find start of L-band gap */
183 for (freq = MHZ(1000); freq < MHZ(1300); freq += MHZ(1)) {
184 if (rtlsdr_set_center_freq(dev, freq) < 0) {
190 /* find end of L-band gap */
191 for (freq = MHZ(1300); freq > MHZ(1000); freq -= MHZ(1)) {
192 if (rtlsdr_set_center_freq(dev, freq) < 0) {
198 fprintf(stderr, "E4K range: %i to %i MHz\n",
199 range_start/MHZ(1) + 1, range_end/MHZ(1) - 1);
201 fprintf(stderr, "E4K L-band gap: %i to %i MHz\n",
202 gap_start/MHZ(1), gap_end/MHZ(1));
205 int main(int argc, char **argv)
208 struct sigaction sigact;
212 int i, tuner_benchmark = 0;
215 uint32_t dev_index = 0;
216 uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
217 uint32_t out_block_size = DEFAULT_BUF_LENGTH;
224 while ((opt = getopt(argc, argv, "d:s:b:tpS::")) != -1) {
227 dev_index = atoi(optarg);
230 samp_rate = (uint32_t)atof(optarg);
233 out_block_size = (uint32_t)atof(optarg);
239 ppm_benchmark = PPM_DURATION;
250 if(out_block_size < MINIMAL_BUF_LENGTH ||
251 out_block_size > MAXIMAL_BUF_LENGTH ){
253 "Output block size wrong value, falling back to default\n");
255 "Minimal length: %u\n", MINIMAL_BUF_LENGTH);
257 "Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
258 out_block_size = DEFAULT_BUF_LENGTH;
261 buffer = malloc(out_block_size * sizeof(uint8_t));
263 device_count = rtlsdr_get_device_count();
265 fprintf(stderr, "No supported devices found.\n");
269 fprintf(stderr, "Found %d device(s):\n", device_count);
270 for (i = 0; i < device_count; i++)
271 fprintf(stderr, " %d: %s\n", i, rtlsdr_get_device_name(i));
272 fprintf(stderr, "\n");
274 fprintf(stderr, "Using device %d: %s\n",
276 rtlsdr_get_device_name(dev_index));
278 r = rtlsdr_open(&dev, dev_index);
280 fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
284 sigact.sa_handler = sighandler;
285 sigemptyset(&sigact.sa_mask);
287 sigaction(SIGINT, &sigact, NULL);
288 sigaction(SIGTERM, &sigact, NULL);
289 sigaction(SIGQUIT, &sigact, NULL);
290 sigaction(SIGPIPE, &sigact, NULL);
292 SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
294 count = rtlsdr_get_tuner_gains(dev, NULL);
295 fprintf(stderr, "Supported gain values (%d): ", count);
297 count = rtlsdr_get_tuner_gains(dev, gains);
298 for (i = 0; i < count; i++)
299 fprintf(stderr, "%.1f ", gains[i] / 10.0);
300 fprintf(stderr, "\n");
302 /* Set the sample rate */
303 r = rtlsdr_set_sample_rate(dev, samp_rate);
305 fprintf(stderr, "WARNING: Failed to set sample rate.\n");
307 if (tuner_benchmark) {
308 if (rtlsdr_get_tuner_type(dev) == RTLSDR_TUNER_E4000)
311 fprintf(stderr, "No E4000 tuner found, aborting.\n");
316 /* Enable test mode */
317 r = rtlsdr_set_testmode(dev, 1);
319 /* Reset endpoint before we start reading from it (mandatory) */
320 r = rtlsdr_reset_buffer(dev);
322 fprintf(stderr, "WARNING: Failed to reset buffers.\n");
324 if (ppm_benchmark && !sync_mode) {
325 fprintf(stderr, "Reporting PPM error measurement every %i seconds...\n", ppm_benchmark);
326 fprintf(stderr, "Press ^C after a few minutes.\n");
328 gettimeofday(&tv, NULL);
329 ppm_recent.tv_sec = tv.tv_sec;
330 ppm_recent.tv_nsec = tv.tv_usec*1000;
331 ppm_start.tv_sec = tv.tv_sec;
332 ppm_start.tv_nsec = tv.tv_usec*1000;
334 clock_gettime(CLOCK_REALTIME, &ppm_recent);
335 clock_gettime(CLOCK_REALTIME, &ppm_start);
340 fprintf(stderr, "Reading samples in sync mode...\n");
342 r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
344 fprintf(stderr, "WARNING: sync read failed.\n");
348 if ((uint32_t)n_read < out_block_size) {
349 fprintf(stderr, "Short read, samples lost, exiting!\n");
354 fprintf(stderr, "Reading samples in async mode...\n");
355 r = rtlsdr_read_async(dev, rtlsdr_callback, NULL,
356 DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
360 fprintf(stderr, "\nUser cancel, exiting...\n");
363 ns = 1000000000L * (int64_t)(ppm_recent.tv_sec - ppm_start.tv_sec);
364 ns += (int64_t)(ppm_recent.tv_nsec - ppm_start.tv_nsec);
365 real_rate = (int)(ppm_total * 1000000000L / ns);
366 printf("Cumulative PPM error: %i\n",
367 (int)round((double)(1000000 * (real_rate - (int)samp_rate)) / (double)samp_rate));
372 fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
378 return r >= 0 ? r : -r;