mirror of
https://github.com/rtlsdrblog/rtl-sdr-blog.git
synced 2024-12-27 19:38:27 +01:00
fix reading from usb by resetting the
buffers as required
This commit is contained in:
parent
25cad444b2
commit
c09fbb7c57
17
src/main.c
17
src/main.c
@ -54,9 +54,13 @@ 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;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "f:s:")) != -1) {
|
while ((opt = getopt(argc, argv, "d:f:s:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
|
case 'd':
|
||||||
|
dev_index = atoi(optarg);
|
||||||
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
frequency = atoi(optarg);
|
frequency = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
@ -85,12 +89,14 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
printf("Found %d device(s).\n", device_count);
|
printf("Found %d device(s).\n", device_count);
|
||||||
|
|
||||||
dev = rtlsdr_open(0); /* open the first device */
|
dev = rtlsdr_open(dev_index);
|
||||||
if (NULL == dev) {
|
if (NULL == dev) {
|
||||||
fprintf(stderr, "Failed to open rtlsdr device.\n");
|
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("Using %s\n", rtlsdr_get_device_name(dev_index));
|
||||||
|
|
||||||
sigact.sa_handler = sighandler;
|
sigact.sa_handler = sighandler;
|
||||||
sigemptyset(&sigact.sa_mask);
|
sigemptyset(&sigact.sa_mask);
|
||||||
sigact.sa_flags = 0;
|
sigact.sa_flags = 0;
|
||||||
@ -115,12 +121,11 @@ int main(int argc, char **argv)
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Reset endpoint before we start reading from it */
|
/* Reset endpoint before we start reading from it (mandatory) */
|
||||||
r = rtlsdr_reset_buffer(dev);
|
r = rtlsdr_reset_buffer(dev);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
fprintf(stderr, "WARNING: Failed to reset buffers.\n");
|
fprintf(stderr, "WARNING: Failed to reset buffers.\n");
|
||||||
|
|
||||||
|
|
||||||
printf("Reading samples...\n");
|
printf("Reading samples...\n");
|
||||||
while (!do_exit) {
|
while (!do_exit) {
|
||||||
r = rtlsdr_read_sync(dev, buffer, READLEN, &n_read);
|
r = rtlsdr_read_sync(dev, buffer, READLEN, &n_read);
|
||||||
@ -137,6 +142,8 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
|
rtlsdr_close(dev);
|
||||||
|
|
||||||
rtlsdr_exit();
|
rtlsdr_exit();
|
||||||
out:
|
out:
|
||||||
return r >= 0 ? r : -r;
|
return r >= 0 ? r : -r;
|
||||||
|
155
src/rtl-sdr.c
155
src/rtl-sdr.c
@ -79,16 +79,19 @@ rtlsdr_tuner_t tuners[] = {
|
|||||||
{ RTLSDR_TUNER_FC0013, fc0013_init, fc0013_exit, fc0013_tune, fc0013_set_bw, 0, 0 },
|
{ RTLSDR_TUNER_FC0013, fc0013_init, fc0013_exit, fc0013_tune, fc0013_set_bw, 0, 0 },
|
||||||
};
|
};
|
||||||
|
|
||||||
struct rtlsdr_device {
|
typedef struct rtlsdr_device {
|
||||||
uint16_t vid;
|
uint16_t vid;
|
||||||
uint16_t pid;
|
uint16_t pid;
|
||||||
} devices[] = {
|
const char *name;
|
||||||
{ 0x0bda, 0x2832 }, /* default RTL2832U vid/pid (eg. hama nano) */
|
} rtlsdr_device_t;
|
||||||
{ 0x0bda, 0x2838 }, /* ezcap USB 2.0 DVB-T/DAB/FM stick */
|
|
||||||
{ 0x0ccd, 0x00b3 }, /* Terratec NOXON DAB/DAB+ USB-Stick rev 1 */
|
rtlsdr_device_t devices[] = {
|
||||||
{ 0x1f4d, 0xb803 }, /* GTek T803 */
|
{ 0x0bda, 0x2832, "Generic RTL2832U (e.g. hama nano)" },
|
||||||
{ 0x1b80, 0xd3a4 }, /* Twintech UT-40 */
|
{ 0x0bda, 0x2838, "ezcap USB 2.0 DVB-T/DAB/FM dongle" },
|
||||||
{ 0x1d19, 0x1101 }, /* Dexatek DK DVB-T Dongle (Logilink VG0002A) */
|
{ 0x0ccd, 0x00b3, "Terratec NOXON DAB/DAB+ USB dongle (rev 1)" },
|
||||||
|
{ 0x1f4d, 0xb803, "GTek T803" },
|
||||||
|
{ 0x1b80, 0xd3a4, "Twintech UT-40" },
|
||||||
|
{ 0x1d19, 0x1101, "Dexatek DK DVB-T Dongle (Logilink VG0002A)" },
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -309,15 +312,16 @@ void rtlsdr_init_baseband(rtlsdr_dev_t *dev)
|
|||||||
|
|
||||||
int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq)
|
int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq)
|
||||||
{
|
{
|
||||||
|
if (!dev->tuner)
|
||||||
|
return -1;
|
||||||
|
|
||||||
rtlsdr_set_i2c_repeater(dev, 1);
|
rtlsdr_set_i2c_repeater(dev, 1);
|
||||||
|
|
||||||
if (dev->tuner) {
|
dev->tuner->freq = freq;
|
||||||
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;
|
dev->tuner->tune((void *)dev, (int) f);
|
||||||
dev->tuner->tune((void *)dev, (int) f);
|
printf("Tuned to %i Hz\n", freq);
|
||||||
printf("Tuned to %i Hz\n", freq);
|
|
||||||
}
|
|
||||||
|
|
||||||
rtlsdr_set_i2c_repeater(dev, 0);
|
rtlsdr_set_i2c_repeater(dev, 0);
|
||||||
|
|
||||||
@ -331,25 +335,26 @@ 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, int32_t ppm)
|
||||||
{
|
{
|
||||||
if (dev->tuner) {
|
if (!dev->tuner)
|
||||||
if (dev->tuner->corr == ppm)
|
return -1;
|
||||||
return -1;
|
|
||||||
|
|
||||||
dev->tuner->corr = ppm;
|
if (dev->tuner->corr == ppm)
|
||||||
|
return -1;
|
||||||
|
|
||||||
/* retune to apply new correction value */
|
dev->tuner->corr = ppm;
|
||||||
rtlsdr_set_center_freq(dev, dev->tuner->freq);
|
|
||||||
}
|
/* retune to apply new correction value */
|
||||||
|
rtlsdr_set_center_freq(dev, dev->tuner->freq);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t rtlsdr_get_freq_correction(rtlsdr_dev_t *dev)
|
int32_t rtlsdr_get_freq_correction(rtlsdr_dev_t *dev)
|
||||||
{
|
{
|
||||||
if (dev->tuner)
|
if (!dev->tuner)
|
||||||
return dev->tuner->corr;
|
return -1;
|
||||||
else
|
|
||||||
return 0;
|
return dev->tuner->corr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate)
|
void rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate)
|
||||||
@ -392,9 +397,24 @@ void rtlsdr_exit(void)
|
|||||||
libusb_exit(NULL);
|
libusb_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rtlsdr_device_t *find_known_device(uint16_t vid, uint16_t pid)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
rtlsdr_device_t *device = NULL;
|
||||||
|
|
||||||
|
for (i = 0; i < sizeof(devices)/sizeof(rtlsdr_device_t); i++ ) {
|
||||||
|
if (devices[i].vid == vid && devices[i].pid == pid) {
|
||||||
|
device = &devices[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t rtlsdr_get_device_count(void)
|
uint32_t rtlsdr_get_device_count(void)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i;
|
||||||
libusb_device **list;
|
libusb_device **list;
|
||||||
uint32_t device_count = 0;
|
uint32_t device_count = 0;
|
||||||
struct libusb_device_descriptor dd;
|
struct libusb_device_descriptor dd;
|
||||||
@ -404,10 +424,8 @@ uint32_t rtlsdr_get_device_count(void)
|
|||||||
for (i = 0; i < cnt; i++) {
|
for (i = 0; i < cnt; i++) {
|
||||||
libusb_get_device_descriptor(list[i], &dd);
|
libusb_get_device_descriptor(list[i], &dd);
|
||||||
|
|
||||||
for (j = 0; j < sizeof(devices)/sizeof(struct rtlsdr_device); j++ ) {
|
if (find_known_device(dd.idVendor, dd.idProduct))
|
||||||
if (devices[j].vid == dd.idVendor && devices[j].pid == dd.idProduct)
|
device_count++;
|
||||||
device_count++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
libusb_free_device_list(list, 0);
|
libusb_free_device_list(list, 0);
|
||||||
@ -417,26 +435,39 @@ uint32_t rtlsdr_get_device_count(void)
|
|||||||
|
|
||||||
const char *rtlsdr_get_device_name(uint32_t index)
|
const char *rtlsdr_get_device_name(uint32_t index)
|
||||||
{
|
{
|
||||||
|
int i;
|
||||||
libusb_device **list;
|
libusb_device **list;
|
||||||
|
struct libusb_device_descriptor dd;
|
||||||
|
rtlsdr_device_t *device = NULL;
|
||||||
|
uint32_t device_count = 0;
|
||||||
|
|
||||||
ssize_t cnt = libusb_get_device_list(NULL, &list);
|
ssize_t cnt = libusb_get_device_list(NULL, &list);
|
||||||
|
|
||||||
if (index > cnt - 1)
|
for (i = 0; i < cnt; i++) {
|
||||||
return NULL;
|
libusb_get_device_descriptor(list[i], &dd);
|
||||||
|
|
||||||
/*libusb_device *device = list[index];*/
|
device = find_known_device(dd.idVendor, dd.idProduct);
|
||||||
|
|
||||||
|
if (device) {
|
||||||
|
device_count++;
|
||||||
|
|
||||||
|
if (index == device_count - 1)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
libusb_free_device_list(list, 0);
|
libusb_free_device_list(list, 0);
|
||||||
|
|
||||||
return "TODO: implement";
|
if (device)
|
||||||
|
return device->name;
|
||||||
|
else
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
rtlsdr_dev_t *rtlsdr_open(int index)
|
rtlsdr_dev_t *rtlsdr_open(int index)
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
int i, j;
|
int i;
|
||||||
libusb_device **list;
|
libusb_device **list;
|
||||||
rtlsdr_dev_t * dev = NULL;
|
rtlsdr_dev_t * dev = NULL;
|
||||||
libusb_device *device = NULL;
|
libusb_device *device = NULL;
|
||||||
@ -453,48 +484,33 @@ rtlsdr_dev_t *rtlsdr_open(int index)
|
|||||||
|
|
||||||
libusb_get_device_descriptor(list[i], &dd);
|
libusb_get_device_descriptor(list[i], &dd);
|
||||||
|
|
||||||
for (j = 0; j < sizeof(devices)/sizeof(struct rtlsdr_device); j++ ) {
|
if (find_known_device(dd.idVendor, dd.idProduct)) {
|
||||||
if ( devices[j].vid == dd.idVendor && devices[j].pid == dd.idProduct ) {
|
device_count++;
|
||||||
device_count++;
|
|
||||||
if (index == device_count - 1)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index == device_count - 1)
|
if (index == device_count - 1)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
device = NULL;
|
device = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!device)
|
if (!device)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
r = libusb_open(device, &dev->devh);
|
|
||||||
if (r < 0) {
|
|
||||||
libusb_free_device_list(list, 0);
|
|
||||||
fprintf(stderr, "usb_open error %d\n", r);
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
r = libusb_open(device, &dev->devh);
|
||||||
|
if (r < 0) {
|
||||||
libusb_free_device_list(list, 0);
|
libusb_free_device_list(list, 0);
|
||||||
|
fprintf(stderr, "usb_open error %d\n", r);
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned char buffer[256];
|
libusb_free_device_list(list, 0);
|
||||||
|
|
||||||
libusb_get_string_descriptor_ascii(dev->devh, 0, buffer, sizeof(buffer));
|
r = libusb_claim_interface(dev->devh, 0);
|
||||||
printf("sn#: %s\n", buffer);
|
if (r < 0) {
|
||||||
|
fprintf(stderr, "usb_claim_interface error %d\n", r);
|
||||||
libusb_get_string_descriptor_ascii(dev->devh, 1, buffer, sizeof(buffer));
|
goto err;
|
||||||
printf("manufacturer: %s\n", buffer);
|
}
|
||||||
|
|
||||||
libusb_get_string_descriptor_ascii(dev->devh, 2, buffer, sizeof(buffer));
|
|
||||||
printf("product: %s\n", buffer);
|
|
||||||
|
|
||||||
r = libusb_claim_interface(dev->devh, 0);
|
|
||||||
if (r < 0) {
|
|
||||||
fprintf(stderr, "usb_claim_interface error %d\n", r);
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
rtlsdr_init_baseband(dev);
|
rtlsdr_init_baseband(dev);
|
||||||
|
|
||||||
@ -517,7 +533,10 @@ int rtlsdr_close(rtlsdr_dev_t *dev)
|
|||||||
|
|
||||||
int rtlsdr_reset_buffer(rtlsdr_dev_t *dev)
|
int rtlsdr_reset_buffer(rtlsdr_dev_t *dev)
|
||||||
{
|
{
|
||||||
return 0; // TODO: implement
|
rtlsdr_write_reg(dev, USBB, USB_EPA_CTL, 0x1002, 2);
|
||||||
|
rtlsdr_write_reg(dev, USBB, USB_EPA_CTL, 0x0000, 2);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read)
|
int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read)
|
||||||
|
Loading…
Reference in New Issue
Block a user