add skeleton routines for setting tuner gain

master
Dimitri Stolnikov 13 years ago
parent 2f20f6fe9e
commit 4ad93906a4
  1. 8
      include/rtl-sdr.h
  2. 15
      src/main.c
  3. 75
      src/rtl-sdr.c

@ -47,9 +47,13 @@ int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq);
int rtlsdr_get_center_freq(rtlsdr_dev_t *dev); int rtlsdr_get_center_freq(rtlsdr_dev_t *dev);
int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int32_t ppm); int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm);
int32_t rtlsdr_get_freq_correction(rtlsdr_dev_t *dev); int rtlsdr_get_freq_correction(rtlsdr_dev_t *dev);
int rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain);
int rtlsdr_get_tuner_gain(rtlsdr_dev_t *dev);
/* this will select the baseband filters according to the requested sample rate */ /* this will select the baseband filters according to the requested sample rate */
int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t rate); int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t rate);

@ -35,6 +35,8 @@ void usage(void)
printf("rtl-sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n" printf("rtl-sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n"
"Usage:\t -f frequency to tune to [Hz]\n" "Usage:\t -f frequency to tune to [Hz]\n"
"\t[-s samplerate (default: 2048000 Hz)]\n" "\t[-s samplerate (default: 2048000 Hz)]\n"
"\t[-d device index (default: 0)]\n"
"\t[-g tuner gain (default: 0 dB)]\n"
"\toutput filename\n"); "\toutput filename\n");
exit(1); exit(1);
} }
@ -54,9 +56,9 @@ int main(int argc, char **argv)
uint32_t n_read; uint32_t n_read;
FILE *file; FILE *file;
rtlsdr_dev_t *dev = NULL; rtlsdr_dev_t *dev = NULL;
uint32_t dev_index = 0; uint32_t dev_index = 0, gain = 0;
while ((opt = getopt(argc, argv, "d:f:s:")) != -1) { while ((opt = getopt(argc, argv, "d:f:g:s:")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
dev_index = atoi(optarg); dev_index = atoi(optarg);
@ -64,6 +66,9 @@ int main(int argc, char **argv)
case 'f': case 'f':
frequency = atoi(optarg); frequency = atoi(optarg);
break; break;
case 'g':
gain = atoi(optarg);
break;
case 's': case 's':
samp_rate = atoi(optarg); samp_rate = atoi(optarg);
break; break;
@ -116,6 +121,12 @@ int main(int argc, char **argv)
else else
fprintf(stderr, "Tuned to %i Hz.\n", frequency); fprintf(stderr, "Tuned to %i Hz.\n", frequency);
r = rtlsdr_set_tuner_gain(dev, gain);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
else
fprintf(stderr, "Tuner gain set to %i dB.\n", gain);
file = fopen(filename, "wb"); file = fopen(filename, "wb");
if (!file) { if (!file) {

@ -29,32 +29,41 @@
#include "tuner_e4000.h" #include "tuner_e4000.h"
#include "tuner_fc0013.h" #include "tuner_fc0013.h"
typedef struct rtlsdr_tuner {
int(*init)(void *);
int(*exit)(void *);
int(*tune)(void *, int freq /* Hz */);
int(*set_bw)(void *, int bw /* Hz */);
int(*set_gain)(void *, int gain /* dB */);
int freq; /* Hz */
int corr; /* ppm */
int gain; /* dB */
} rtlsdr_tuner_t;
/* generic tuner interface functions, shall be moved to the tuner implementations */ /* generic tuner interface functions, shall be moved to the tuner implementations */
int e4k_init(void *dev) { return e4000_Initialize(dev); } int e4k_init(void *dev) { return e4000_Initialize(dev); }
int e4k_exit(void *dev) { return 0; } int e4k_exit(void *dev) { return 0; }
int e4k_tune(void *dev, int freq) { return e4000_SetRfFreqHz(dev, freq); } int e4k_tune(void *dev, int freq) { return e4000_SetRfFreqHz(dev, freq); }
int e4k_set_bw(void *dev, int bw) { return e4000_SetBandwidthHz(dev, 8000000); } int e4k_set_bw(void *dev, int bw) { return e4000_SetBandwidthHz(dev, 8000000); }
int e4k_set_gain(void *dev, int gain) { return 0; }
int fc0012_init(void *dev) { return 0; } int fc0012_init(void *dev) { return 0; }
int fc0012_exit(void *dev) { return 0; } int fc0012_exit(void *dev) { return 0; }
int fc0012_tune(void *dev, int freq) { return 0; } int fc0012_tune(void *dev, int freq) { return 0; }
int fc0012_set_bw(void *dev, int bw) { return 0; } int fc0012_set_bw(void *dev, int bw) { return 0; }
int fc0012_set_gain(void *dev, int gain) { return 0; }
int fc0013_init(void *dev) { return FC0013_Open(dev); } int fc0013_init(void *dev) { return FC0013_Open(dev); }
int fc0013_exit(void *dev) { return 0; } int fc0013_exit(void *dev) { return 0; }
int fc0013_tune(void *dev, int freq) { int fc0013_tune(void *dev, int freq) {
/* read bandwidth mode to reapply it */
unsigned int bw = 6; unsigned int bw = 6;
//fc0013_GetBandwidthMode(dev, &bw); // FIXME: missing
return FC0013_SetFrequency(dev, freq/1000, bw & 0xff); return FC0013_SetFrequency(dev, freq/1000, bw & 0xff);
} }
int fc0013_set_bw(void *dev, int bw) { int fc0013_set_bw(void *dev, int bw) {
/* read frequency to reapply it */ unsigned long freq = ((rtlsdr_tuner_t *)dev)->freq;
unsigned long freq = 0; return FC0013_SetFrequency(dev, freq/1000, bw/1000000);
//fc0013_GetRfFreqHz(dev, &freq); // FIXME: missing
return FC0013_SetFrequency(dev, freq/1000, 8);
} }
int fc0013_set_gain(void *dev, int gain) { return 0; }
enum rtlsdr_tuners { enum rtlsdr_tuners {
RTLSDR_TUNER_E4000, RTLSDR_TUNER_E4000,
@ -62,20 +71,12 @@ enum rtlsdr_tuners {
RTLSDR_TUNER_FC0013, RTLSDR_TUNER_FC0013,
}; };
typedef struct rtlsdr_tuner {
enum rtlsdr_tuners tuner;
int(*init)(void *);
int(*exit)(void *);
int(*tune)(void *, int freq /* Hz */);
int(*set_bw)(void *, int bw /* Hz */);
int freq; /* Hz */
int corr; /* ppm */
} rtlsdr_tuner_t;
rtlsdr_tuner_t tuners[] = {
{ RTLSDR_TUNER_E4000, e4k_init, e4k_exit, e4k_tune, e4k_set_bw, 0, 0 }, static rtlsdr_tuner_t tuners[] = {
{ RTLSDR_TUNER_FC0012, fc0012_init, fc0012_exit, fc0012_tune, fc0012_set_bw, 0, 0 }, { e4k_init, e4k_exit, e4k_tune, e4k_set_bw, e4k_set_gain, 0, 0, 0 },
{ RTLSDR_TUNER_FC0013, fc0013_init, fc0013_exit, fc0013_tune, fc0013_set_bw, 0, 0 }, { fc0012_init, fc0012_exit, fc0012_tune, fc0012_set_bw, fc0012_set_gain, 0, 0, 0 },
{ fc0013_init, fc0013_exit, fc0013_tune, fc0013_set_bw, fc0013_set_gain, 0, 0, 0 },
}; };
typedef struct rtlsdr_device { typedef struct rtlsdr_device {
@ -84,7 +85,7 @@ typedef struct rtlsdr_device {
const char *name; const char *name;
} rtlsdr_device_t; } rtlsdr_device_t;
rtlsdr_device_t devices[] = { static rtlsdr_device_t devices[] = {
{ 0x0bda, 0x2832, "Generic RTL2832U (e.g. hama nano)" }, { 0x0bda, 0x2832, "Generic RTL2832U (e.g. hama nano)" },
{ 0x0bda, 0x2838, "ezcap USB 2.0 DVB-T/DAB/FM dongle" }, { 0x0bda, 0x2838, "ezcap USB 2.0 DVB-T/DAB/FM dongle" },
{ 0x0ccd, 0x00b3, "Terratec NOXON DAB/DAB+ USB dongle (rev 1)" }, { 0x0ccd, 0x00b3, "Terratec NOXON DAB/DAB+ USB dongle (rev 1)" },
@ -370,13 +371,16 @@ int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq)
rtlsdr_set_i2c_repeater(dev, 1); rtlsdr_set_i2c_repeater(dev, 1);
dev->tuner->freq = freq;
double f = (double) freq; double f = (double) freq;
f *= 1.0 + dev->tuner->corr / 1e6; f *= 1.0 + dev->tuner->corr / 1e6;
r = dev->tuner->tune((void *)dev, (int) f); r = dev->tuner->tune((void *)dev, (int) f);
rtlsdr_set_i2c_repeater(dev, 0); rtlsdr_set_i2c_repeater(dev, 0);
if (!r)
dev->tuner->freq = freq;
return r; return r;
} }
@ -388,7 +392,7 @@ int rtlsdr_get_center_freq(rtlsdr_dev_t *dev)
return dev->tuner->freq; return dev->tuner->freq;
} }
int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int32_t ppm) int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm)
{ {
int r; int r;
@ -406,7 +410,7 @@ int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int32_t ppm)
return r; return r;
} }
int32_t rtlsdr_get_freq_correction(rtlsdr_dev_t *dev) int rtlsdr_get_freq_correction(rtlsdr_dev_t *dev)
{ {
if (!dev || !dev->tuner) if (!dev || !dev->tuner)
return -1; return -1;
@ -414,6 +418,29 @@ int32_t rtlsdr_get_freq_correction(rtlsdr_dev_t *dev)
return dev->tuner->corr; return dev->tuner->corr;
} }
int rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain)
{
int r;
if (!dev || !dev->tuner)
return -1;
r = dev->tuner->set_gain((void *)dev, gain);
if (!r)
dev->tuner->gain = gain;
return r;
}
int rtlsdr_get_tuner_gain(rtlsdr_dev_t *dev)
{
if (!dev || !dev->tuner)
return -1;
return dev->tuner->gain;
}
int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate) int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate)
{ {
uint16_t tmp; uint16_t tmp;

Loading…
Cancel
Save