rework towards a library interface

master
Dimitri Stolnikov 13 years ago
parent 40669a5a0a
commit d8da91cbc2
  1. 4
      src/Makefile
  2. 6
      src/i2c.h
  3. 358
      src/main.c
  4. 539
      src/rtl-sdr.c
  5. 73
      src/rtl-sdr.h
  6. 68
      src/tuner_e4000.c
  7. 80
      src/tuner_e4000.h
  8. 20
      src/tuner_fc0013.c
  9. 32
      src/tuner_fc0013.h

@ -1,9 +1,9 @@
LDFLAGS=`pkg-config --libs libusb-1.0` LDFLAGS=`pkg-config --libs libusb-1.0`
CFLAGS=-Wall -O2 `pkg-config --cflags libusb-1.0` CFLAGS=-Wall -g -O0 `pkg-config --cflags libusb-1.0`
all: rtl-sdr all: rtl-sdr
rtl-sdr: main.o tuner_e4000.o tuner_fc0013.c rtl-sdr: main.o rtl-sdr.o tuner_e4000.o tuner_fc0013.c
$(CC) -o $@ $^ $(LDFLAGS) $(CC) -o $@ $^ $(LDFLAGS)
clean: clean:

@ -1,7 +1,9 @@
#ifndef __I2C_H #ifndef __I2C_H
#define __I2C_H #define __I2C_H
int rtl_i2c_write(uint8_t i2c_addr, uint8_t *buffer, int len); typedef int rtlsdr_dev_t;
int rtl_i2c_read(uint8_t i2c_addr, uint8_t *buffer, int len);
int rtlsdr_i2c_write(rtlsdr_dev_t *dev, uint8_t i2c_addr, uint8_t *buffer, int len);
int rtlsdr_i2c_read(rtlsdr_dev_t *dev, uint8_t i2c_addr, uint8_t *buffer, int len);
#endif #endif

@ -24,303 +24,12 @@
#include <math.h> #include <math.h>
#include <unistd.h> #include <unistd.h>
#include <libusb.h> #include "rtl-sdr.h"
#include "tuner_e4000.h"
#include "tuner_fc0013.h"
#define READLEN (16 * 16384) #define READLEN (16 * 16384)
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
/* ezcap USB 2.0 DVB-T/DAB/FM stick */
#define EZCAP_VID 0x0bda
#define EZCAP_PID 0x2838
/* Terratec NOXON DAB/DAB+ USB-Stick */
#define NOXON_VID 0x0ccd
#define NOXON_PID 0x00b3
#define CRYSTAL_FREQ 28800000
static struct libusb_device_handle *devh = NULL;
static int do_exit = 0; static int do_exit = 0;
enum TUNER_TYPE {
TUNER_E4000,
TUNER_FC0013
} tuner_type;
static int find_device(void)
{
devh = libusb_open_device_with_vid_pid(NULL, EZCAP_VID, EZCAP_PID);
if (devh > 0) {
tuner_type = TUNER_E4000;
printf("Found ezcap stick with E4000 tuner\n");
return 0;
}
devh = libusb_open_device_with_vid_pid(NULL, NOXON_VID, NOXON_PID);
if (devh > 0) {
tuner_type = TUNER_FC0013;
printf("Found Terratec NOXON stick with FC0013 tuner\n");
return 0;
}
return -EIO;
}
enum usb_reg {
USB_SYSCTL = 0x2000,
USB_CTRL = 0x2010,
USB_STAT = 0x2014,
USB_EPA_CFG = 0x2144,
USB_EPA_CTL = 0x2148,
USB_EPA_MAXPKT = 0x2158,
USB_EPA_MAXPKT_2 = 0x215a,
USB_EPA_FIFO_CFG = 0x2160,
};
enum sys_reg {
DEMOD_CTL = 0x3000,
GPO = 0x3001,
GPI = 0x3002,
GPOE = 0x3003,
GPD = 0x3004,
SYSINTE = 0x3005,
SYSINTS = 0x3006,
GP_CFG0 = 0x3007,
GP_CFG1 = 0x3008,
SYSINTE_1 = 0x3009,
SYSINTS_1 = 0x300a,
DEMOD_CTL_1 = 0x300b,
IR_SUSPEND = 0x300c,
};
enum blocks {
DEMODB = 0,
USBB = 1,
SYSB = 2,
TUNB = 3,
ROMB = 4,
IRB = 5,
IICB = 6,
};
int rtl_read_array(uint8_t block, uint16_t addr, uint8_t *array, uint8_t len)
{
int r;
uint16_t index = (block << 8);
r = libusb_control_transfer(devh, CTRL_IN, 0, addr, index, array, len, 0);
return r;
}
int rtl_write_array(uint8_t block, uint16_t addr, uint8_t *array, uint8_t len)
{
int r;
uint16_t index = (block << 8) | 0x10;
r = libusb_control_transfer(devh, CTRL_OUT, 0, addr, index, array, len, 0);
return r;
}
int rtl_i2c_write(uint8_t i2c_addr, uint8_t *buffer, int len)
{
uint16_t addr = i2c_addr;
return rtl_write_array(IICB, addr, buffer, len);
}
int rtl_i2c_read(uint8_t i2c_addr, uint8_t *buffer, int len)
{
uint16_t addr = i2c_addr;
return rtl_read_array(IICB, addr, buffer, len);
}
uint16_t rtl_read_reg(uint8_t block, uint16_t addr, uint8_t len)
{
int r;
unsigned char data[2];
uint16_t index = (block << 8);
uint16_t reg;
r = libusb_control_transfer(devh, CTRL_IN, 0, addr, index, data, len, 0);
if (r < 0)
printf("%s failed\n", __FUNCTION__);
reg = (data[1] << 8) | data[0];
return reg;
}
void rtl_write_reg(uint8_t block, uint16_t addr, uint16_t val, uint8_t len)
{
int r;
unsigned char data[2];
uint16_t index = (block << 8) | 0x10;
if (len == 1)
data[0] = val & 0xff;
else
data[0] = val >> 8;
data[1] = val & 0xff;
r = libusb_control_transfer(devh, CTRL_OUT, 0, addr, index, data, len, 0);
if (r < 0)
printf("%s failed\n", __FUNCTION__);
}
uint16_t demod_read_reg(uint8_t page, uint8_t addr, uint8_t len)
{
int r;
unsigned char data[2];
uint16_t index = page;
uint16_t reg;
addr = (addr << 8) | 0x20;
r = libusb_control_transfer(devh, CTRL_IN, 0, addr, index, data, len, 0);
if (r < 0)
printf("%s failed\n", __FUNCTION__);
reg = (data[1] << 8) | data[0];
return reg;
}
void demod_write_reg(uint8_t page, uint16_t addr, uint16_t val, uint8_t len)
{
int r;
unsigned char data[2];
uint16_t index = 0x10 | page;
addr = (addr << 8) | 0x20;
if (len == 1)
data[0] = val & 0xff;
else
data[0] = val >> 8;
data[1] = val & 0xff;
r = libusb_control_transfer(devh, CTRL_OUT, 0, addr, index, data, len, 0);
if (r < 0)
printf("%s failed\n", __FUNCTION__);
demod_read_reg(0x0a, 0x01, 1);
}
void set_samp_rate(uint32_t samp_rate)
{
uint16_t tmp;
uint32_t rsamp_ratio;
double real_rate;
/* check for the maximum rate the resampler supports */
if (samp_rate > 3200000)
samp_rate = 3200000;
rsamp_ratio = (CRYSTAL_FREQ * pow(2, 22)) / samp_rate;
rsamp_ratio &= ~3;
real_rate = (CRYSTAL_FREQ * pow(2, 22)) / rsamp_ratio;
printf("Setting sample rate: %.3f Hz\n", real_rate);
tmp = (rsamp_ratio >> 16);
demod_write_reg(1, 0x9f, tmp, 2);
tmp = rsamp_ratio & 0xffff;
demod_write_reg(1, 0xa1, tmp, 2);
}
void set_i2c_repeater(int on)
{
demod_write_reg(1, 0x01, on ? 0x18 : 0x10, 1);
}
void rtl_init(void)
{
unsigned int i;
/* default FIR coefficients used for DAB/FM by the Windows driver,
* the DVB driver uses different ones */
uint8_t fir_coeff[] = {
0xca, 0xdc, 0xd7, 0xd8, 0xe0, 0xf2, 0x0e, 0x35, 0x06, 0x50,
0x9c, 0x0d, 0x71, 0x11, 0x14, 0x71, 0x74, 0x19, 0x41, 0x00,
};
/* initialize USB */
rtl_write_reg(USBB, USB_SYSCTL, 0x09, 1);
rtl_write_reg(USBB, USB_EPA_MAXPKT, 0x0002, 2);
rtl_write_reg(USBB, USB_EPA_CTL, 0x1002, 2);
/* poweron demod */
rtl_write_reg(SYSB, DEMOD_CTL_1, 0x22, 1);
rtl_write_reg(SYSB, DEMOD_CTL, 0xe8, 1);
/* reset demod (bit 3, soft_rst) */
demod_write_reg(1, 0x01, 0x14, 1);
demod_write_reg(1, 0x01, 0x10, 1);
/* disable spectrum inversion and adjacent channel rejection */
demod_write_reg(1, 0x15, 0x00, 1);
demod_write_reg(1, 0x16, 0x0000, 2);
/* set IF-frequency to 0 Hz */
demod_write_reg(1, 0x19, 0x0000, 2);
/* set FIR coefficients */
for (i = 0; i < sizeof (fir_coeff); i++)
demod_write_reg(1, 0x1c + i, fir_coeff[i], 1);
demod_write_reg(0, 0x19, 0x25, 1);
/* init FSM state-holding register */
demod_write_reg(1, 0x93, 0xf0, 1);
/* disable AGC (en_dagc, bit 0) */
demod_write_reg(1, 0x11, 0x00, 1);
/* disable PID filter (enable_PID = 0) */
demod_write_reg(0, 0x61, 0x60, 1);
/* opt_adc_iq = 0, default ADC_I/ADC_Q datapath */
demod_write_reg(0, 0x06, 0x80, 1);
/* Enable Zero-IF mode (en_bbin bit), DC cancellation (en_dc_est),
* IQ estimation/compensation (en_iq_comp, en_iq_est) */
demod_write_reg(1, 0xb1, 0x1b, 1);
}
void tuner_init(int frequency)
{
set_i2c_repeater(1);
switch (tuner_type) {
case TUNER_E4000:
e4000_Initialize(1);
e4000_SetBandwidthHz(1, 8000000);
e4000_SetRfFreqHz(1, frequency);
break;
case TUNER_FC0013:
FC0013_Open();
FC0013_SetFrequency(frequency/1000, 8);
break;
default:
printf("No valid tuner available!");
break;
}
printf("Tuned to %i Hz\n", frequency);
set_i2c_repeater(0);
}
void usage(void) void usage(void)
{ {
printf("rtl-sdr, an I/Q recorder for RTL2832 based USB-sticks\n\n" printf("rtl-sdr, an I/Q recorder for RTL2832 based USB-sticks\n\n"
@ -342,8 +51,9 @@ int main(int argc, char **argv)
char *filename; char *filename;
uint32_t frequency = 0, samp_rate = 2048000; uint32_t frequency = 0, samp_rate = 2048000;
uint8_t buffer[READLEN]; uint8_t buffer[READLEN];
int n_read; uint32_t n_read;
FILE *file; FILE *file;
rtlsdr_dev_t *dev = NULL;
while ((opt = getopt(argc, argv, "f:s:")) != -1) { while ((opt = getopt(argc, argv, "f:s:")) != -1) {
switch (opt) { switch (opt) {
@ -365,22 +75,20 @@ int main(int argc, char **argv)
filename = argv[optind]; filename = argv[optind];
} }
r = libusb_init(NULL); rtlsdr_init();
if (r < 0) {
fprintf(stderr, "Failed to initialize libusb\n");
exit(1);
}
r = find_device(); int device_count = rtlsdr_get_device_count();
if (r < 0) { if (!device_count) {
fprintf(stderr, "Could not find/open device\n"); fprintf(stderr, "No supported devices found.\n");
goto out; exit(1);
} }
r = libusb_claim_interface(devh, 0); printf("Found %d device(s).\n", device_count);
if (r < 0) {
fprintf(stderr, "usb_claim_interface error %d\n", r); dev = rtlsdr_open(0); /* open the first device */
goto out; if (NULL == dev) {
fprintf(stderr, "Failed to open rtlsdr device.\n");
exit(1);
} }
sigact.sa_handler = sighandler; sigact.sa_handler = sighandler;
@ -390,12 +98,17 @@ int main(int argc, char **argv)
sigaction(SIGTERM, &sigact, NULL); sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL); sigaction(SIGQUIT, &sigact, NULL);
/* Initialize the RTL2832 */ /* Set the sample rate */
rtl_init(); r = rtlsdr_set_sample_rate(dev, samp_rate);
set_samp_rate(samp_rate); if (r < 0) {
fprintf(stderr, "WARNING: Failed to set sample rate.\n");
}
/* Initialize tuner & set frequency */ /* Set the frequency */
tuner_init(frequency); r = rtlsdr_set_center_freq(dev, frequency);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to set center freq.\n");
}
file = fopen(filename, "wb"); file = fopen(filename, "wb");
@ -404,27 +117,30 @@ int main(int argc, char **argv)
goto out; goto out;
} }
/* reset endpoint before we start reading */ /* Reset endpoint before we start reading from it */
rtl_write_reg(USBB, USB_EPA_CTL, 0x1002, 2); r = rtlsdr_reset_buffer(dev);
rtl_write_reg(USBB, USB_EPA_CTL, 0x0000, 2); if (r < 0) {
fprintf(stderr, "WARNING: Failed to reset buffers.\n");
}
printf("Reading samples...\n"); printf("Reading samples...\n");
while (!do_exit) { while (!do_exit) {
libusb_bulk_transfer(devh, 0x81, buffer, READLEN, &n_read, 3000); r = rtlsdr_read_sync(dev, buffer, READLEN, &n_read);
if (r < 0) {
fprintf(stderr, "WARNING: sync read failed.\n");
}
fwrite(buffer, n_read, 1, file); fwrite(buffer, n_read, 1, file);
if (n_read < READLEN) { if (n_read < READLEN) {
printf("Short bulk read, samples lost, exiting!\n"); printf("Short read, samples lost, exiting!\n");
break; break;
} }
} }
fclose(file); fclose(file);
libusb_release_interface(devh, 0);
rtlsdr_exit();
out: out:
libusb_close(devh);
libusb_exit(NULL);
return r >= 0 ? r : -r; return r >= 0 ? r : -r;
} }

@ -0,0 +1,539 @@
/*
* rtl-sdr, a poor man's SDR using a Realtek RTL2832 based DVB-stick
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
*
* 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 <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <libusb.h>
#include "tuner_e4000.h"
#include "tuner_fc0013.h"
/* generic tuner interface functions, shall be moved to the tuner implementations */
int e4k_init(void *dev) { return e4000_Initialize(dev); }
int e4k_exit(void *dev) { return 0; }
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 fc0012_init(void *dev) { return 0; }
int fc0012_exit(void *dev) { return 0; }
int fc0012_tune(void *dev, int freq) { return 0; }
int fc0012_set_bw(void *dev, int bw) { return 0; }
int fc0013_init(void *dev) { return FC0013_Open(dev); }
int fc0013_exit(void *dev) { return 0; }
int fc0013_tune(void *dev, int freq) {
/* read bandwidth mode to reapply it */
int bw = 0;
//fc0013_GetBandwidthMode(dev, &bw); // FIXME: missing
return FC0013_SetFrequency(dev, freq/1000, bw & 0xff);
}
int fc0013_set_bw(void *dev, int bw) {
/* read frequency to reapply it */
unsigned long freq = 0;
//fc0013_GetRfFreqHz(dev, &freq); // FIXME: missing
return FC0013_SetFrequency(dev, freq/1000, 8);
}
enum rtlsdr_tuners {
RTLSDR_TUNER_UNDEF,
RTLSDR_TUNER_E4000,
RTLSDR_TUNER_FC0012,
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 },
{ RTLSDR_TUNER_FC0012, fc0012_init, fc0012_exit, fc0012_tune, fc0012_set_bw, 0, 0 },
{ RTLSDR_TUNER_FC0013, fc0013_init, fc0013_exit, fc0013_tune, fc0013_set_bw, 0, 0 },
};
struct rtlsdr_device {
uint16_t vid;
uint16_t pid;
} devices[] = {
{ 0x0bda, 0x2832, /*RTLSDR_TUNER_E4000,*/ }, /* default RTL2832U vid/pid (eg. hama nano) */
{ 0x0bda, 0x2838, /*RTLSDR_TUNER_E4000,*/ }, /* ezcap USB 2.0 DVB-T/DAB/FM stick */
{ 0x0ccd, 0x00b3, /*RTLSDR_TUNER_FC0013,*/ }, /* Terratec NOXON DAB/DAB+ USB-Stick */
{ 0x1f4d, 0xb803, /*RTLSDR_TUNER_FC0012,*/ }, /* GTek T803 */
{ 0x1b80, 0xd3a4, /*RTLSDR_TUNER_FC0013,*/ }, /* Twintech UT-40 */
};
typedef struct {
struct libusb_device_handle *devh;
rtlsdr_tuner_t *tuner;
} rtlsdr_dev_t;
#define CRYSTAL_FREQ 28800000
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
enum usb_reg {
USB_SYSCTL = 0x2000,
USB_CTRL = 0x2010,
USB_STAT = 0x2014,
USB_EPA_CFG = 0x2144,
USB_EPA_CTL = 0x2148,
USB_EPA_MAXPKT = 0x2158,
USB_EPA_MAXPKT_2 = 0x215a,
USB_EPA_FIFO_CFG = 0x2160,
};
enum sys_reg {
DEMOD_CTL = 0x3000,
GPO = 0x3001,
GPI = 0x3002,
GPOE = 0x3003,
GPD = 0x3004,
SYSINTE = 0x3005,
SYSINTS = 0x3006,
GP_CFG0 = 0x3007,
GP_CFG1 = 0x3008,
SYSINTE_1 = 0x3009,
SYSINTS_1 = 0x300a,
DEMOD_CTL_1 = 0x300b,
IR_SUSPEND = 0x300c,
};
enum blocks {
DEMODB = 0,
USBB = 1,
SYSB = 2,
TUNB = 3,
ROMB = 4,
IRB = 5,
IICB = 6,
};
int rtlsdr_read_array(rtlsdr_dev_t *dev, uint8_t block, uint16_t addr, uint8_t *array, uint8_t len)
{
int r;
uint16_t index = (block << 8);
r = libusb_control_transfer(dev->devh, CTRL_IN, 0, addr, index, array, len, 0);
return r;
}
int rtlsdr_write_array(rtlsdr_dev_t *dev, uint8_t block, uint16_t addr, uint8_t *array, uint8_t len)
{
int r;
uint16_t index = (block << 8) | 0x10;
r = libusb_control_transfer(dev->devh, CTRL_OUT, 0, addr, index, array, len, 0);
return r;
}
int rtlsdr_i2c_write(rtlsdr_dev_t *dev, uint8_t i2c_addr, uint8_t *buffer, int len)
{
uint16_t addr = i2c_addr;
return rtlsdr_write_array(dev, IICB, addr, buffer, len);
}
int rtlsdr_i2c_read(rtlsdr_dev_t *dev, uint8_t i2c_addr, uint8_t *buffer, int len)
{
uint16_t addr = i2c_addr;
return rtlsdr_read_array(dev, IICB, addr, buffer, len);
}
uint16_t rtlsdr_read_reg(rtlsdr_dev_t *dev, uint8_t block, uint16_t addr, uint8_t len)
{
int r;
unsigned char data[2];
uint16_t index = (block << 8);
uint16_t reg;
r = libusb_control_transfer(dev->devh, CTRL_IN, 0, addr, index, data, len, 0);
if (r < 0)
printf("%s failed\n", __FUNCTION__);
reg = (data[1] << 8) | data[0];
return reg;
}
void rtlsdr_write_reg(rtlsdr_dev_t *dev, uint8_t block, uint16_t addr, uint16_t val, uint8_t len)
{
int r;
unsigned char data[2];
uint16_t index = (block << 8) | 0x10;
if (len == 1)
data[0] = val & 0xff;
else
data[0] = val >> 8;
data[1] = val & 0xff;
r = libusb_control_transfer(dev->devh, CTRL_OUT, 0, addr, index, data, len, 0);
if (r < 0)
printf("%s failed\n", __FUNCTION__);
}
uint16_t rtlsdr_demod_read_reg(rtlsdr_dev_t *dev, uint8_t page, uint8_t addr, uint8_t len)
{
int r;
unsigned char data[2];
uint16_t index = page;
uint16_t reg;
addr = (addr << 8) | 0x20;
r = libusb_control_transfer(dev->devh, CTRL_IN, 0, addr, index, data, len, 0);
if (r < 0)
printf("%s failed\n", __FUNCTION__);
reg = (data[1] << 8) | data[0];
return reg;
}
void rtlsdr_demod_write_reg(rtlsdr_dev_t *dev, uint8_t page, uint16_t addr, uint16_t val, uint8_t len)
{
int r;
unsigned char data[2];
uint16_t index = 0x10 | page;
addr = (addr << 8) | 0x20;
if (len == 1)
data[0] = val & 0xff;
else
data[0] = val >> 8;
data[1] = val & 0xff;
r = libusb_control_transfer(dev->devh, CTRL_OUT, 0, addr, index, data, len, 0);
if (r < 0)
printf("%s failed\n", __FUNCTION__);
rtlsdr_demod_read_reg(dev, 0x0a, 0x01, 1);
}
void rtlsdr_set_i2c_repeater(rtlsdr_dev_t *dev, int on)
{
rtlsdr_demod_write_reg(dev, 1, 0x01, on ? 0x18 : 0x10, 1);
}
void rtlsdr_init_baseband(rtlsdr_dev_t *dev)
{
unsigned int i;
/* default FIR coefficients used for DAB/FM by the Windows driver,
* the DVB driver uses different ones */
uint8_t fir_coeff[] = {
0xca, 0xdc, 0xd7, 0xd8, 0xe0, 0xf2, 0x0e, 0x35, 0x06, 0x50,
0x9c, 0x0d, 0x71, 0x11, 0x14, 0x71, 0x74, 0x19, 0x41, 0x00,
};
/* initialize USB */
rtlsdr_write_reg(dev, USBB, USB_SYSCTL, 0x09, 1);
rtlsdr_write_reg(dev, USBB, USB_EPA_MAXPKT, 0x0002, 2);
rtlsdr_write_reg(dev, USBB, USB_EPA_CTL, 0x1002, 2);
/* poweron demod */
rtlsdr_write_reg(dev, SYSB, DEMOD_CTL_1, 0x22, 1);
rtlsdr_write_reg(dev, SYSB, DEMOD_CTL, 0xe8, 1);
/* reset demod (bit 3, soft_rst) */
rtlsdr_demod_write_reg(dev, 1, 0x01, 0x14, 1);
rtlsdr_demod_write_reg(dev, 1, 0x01, 0x10, 1);
/* disable spectrum inversion and adjacent channel rejection */
rtlsdr_demod_write_reg(dev, 1, 0x15, 0x00, 1);
rtlsdr_demod_write_reg(dev, 1, 0x16, 0x0000, 2);
/* set IF-frequency to 0 Hz */
rtlsdr_demod_write_reg(dev, 1, 0x19, 0x0000, 2);
/* set FIR coefficients */
for (i = 0; i < sizeof (fir_coeff); i++)
rtlsdr_demod_write_reg(dev, 1, 0x1c + i, fir_coeff[i], 1);
rtlsdr_demod_write_reg(dev, 0, 0x19, 0x25, 1);
/* init FSM state-holding register */
rtlsdr_demod_write_reg(dev, 1, 0x93, 0xf0, 1);
/* disable AGC (en_dagc, bit 0) */
rtlsdr_demod_write_reg(dev, 1, 0x11, 0x00, 1);
/* disable PID filter (enable_PID = 0) */
rtlsdr_demod_write_reg(dev, 0, 0x61, 0x60, 1);
/* opt_adc_iq = 0, default ADC_I/ADC_Q datapath */
rtlsdr_demod_write_reg(dev, 0, 0x06, 0x80, 1);
/* Enable Zero-IF mode (en_bbin bit), DC cancellation (en_dc_est),
* IQ estimation/compensation (en_iq_comp, en_iq_est) */
rtlsdr_demod_write_reg(dev, 1, 0xb1, 0x1b, 1);
}
int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq)
{
rtlsdr_set_i2c_repeater(dev, 1);
if (dev->tuner) {
dev->tuner->freq = freq;
double f = (double) freq;
f *= 1.0 + dev->tuner->corr / 1e6;
dev->tuner->tune((void *)dev, (int) f);
printf("Tuned to %i Hz\n", freq);
}
rtlsdr_set_i2c_repeater(dev, 0);
return 0;
}
int rtlsdr_get_center_freq(rtlsdr_dev_t *dev)
{
return 0; // TODO: implement
}
int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int32_t ppm)
{
if (dev->tuner) {
if (dev->tuner->corr == ppm)
return -1;
dev->tuner->corr = ppm;
/* retune to apply new correction value */
rtlsdr_set_center_freq(dev, dev->tuner->freq);
}
return 0;
}
int32_t rtlsdr_get_freq_correction(rtlsdr_dev_t *dev)
{
if (dev->tuner)
return dev->tuner->corr;
else
return 0;
}
void rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate)
{
uint16_t tmp;
uint32_t rsamp_ratio;
double real_rate;
/* check for the maximum rate the resampler supports */
if (samp_rate > 3200000)
samp_rate = 3200000;
rsamp_ratio = (CRYSTAL_FREQ * pow(2, 22)) / samp_rate;
rsamp_ratio &= ~3;
real_rate = (CRYSTAL_FREQ * pow(2, 22)) / rsamp_ratio;
printf("Setting sample rate: %.3f Hz\n", real_rate);
if (dev->tuner)
dev->tuner->set_bw((void *)dev, real_rate);
tmp = (rsamp_ratio >> 16);
rtlsdr_demod_write_reg(dev, 1, 0x9f, tmp, 2);
tmp = rsamp_ratio & 0xffff;
rtlsdr_demod_write_reg(dev, 1, 0xa1, tmp, 2);
}
int rtlsdr_get_sample_rate(rtlsdr_dev_t *dev)
{
return 0; // TODO: implement
}
int rtlsdr_init(void)
{
return libusb_init(NULL);
}
void rtlsdr_exit(void)
{
libusb_exit(NULL);
}
uint32_t rtlsdr_get_device_count(void)
{
int i, j;
libusb_device **list;
uint32_t device_count;
struct libusb_device_descriptor dd;
ssize_t cnt = libusb_get_device_list(NULL, &list);
for (i = 0; i < cnt; i++) {
libusb_get_device_descriptor(list[i], &dd);
for (j = 0; j < sizeof(devices)/sizeof(struct rtlsdr_device); j++ ) {
if ( devices[j].vid == dd.idVendor && devices[j].pid == dd.idProduct ) {
device_count++;
}
}
}
libusb_free_device_list(list, 0);
return device_count;
}
const char *rtlsdr_get_device_name(uint32_t index)
{
libusb_device **list;
ssize_t cnt = libusb_get_device_list(NULL, &list);
if (index > cnt - 1)
return NULL;
/*libusb_device *device = list[index];*/
libusb_free_device_list(list, 0);
return "TODO: implement";
}
rtlsdr_dev_t *rtlsdr_open(int index)
{
int r;
int i, j;
libusb_device **list;
rtlsdr_dev_t * dev = NULL;
libusb_device *device = NULL;
uint32_t device_count = 0;
struct libusb_device_descriptor dd;
dev = malloc(sizeof(rtlsdr_dev_t));
memset(dev, 0, sizeof(rtlsdr_dev_t));
ssize_t cnt = libusb_get_device_list(NULL, &list);
for (i = 0; i < cnt; i++) {
device = list[i];
libusb_get_device_descriptor(list[i], &dd);
for (j = 0; j < sizeof(devices)/sizeof(struct rtlsdr_device); j++ ) {
if ( devices[j].vid == dd.idVendor && devices[j].pid == dd.idProduct ) {
device_count++;
if (index == device_count - 1)
break;
}
}
if (index == device_count - 1)
break;
device = NULL;
}
if (!device)
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;
}
libusb_free_device_list(list, 0);
unsigned char buffer[256];
libusb_get_string_descriptor_ascii(dev->devh, 0, buffer, sizeof(buffer) );
printf("sn#: %s\n", buffer);
libusb_get_string_descriptor_ascii(dev->devh, 1, buffer, sizeof(buffer) );
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);
// TODO: probe the tuner and set dev->tuner member to appropriate tuner object
// dev->tuner = &tuners[...];
return dev;
err:
return NULL;
}
int rtlsdr_close(rtlsdr_dev_t *dev)
{
libusb_release_interface(dev->devh, 0);
libusb_close(dev->devh);
free(dev);
return 0;
}
int rtlsdr_reset_buffer(rtlsdr_dev_t *dev)
{
return 0; // TODO: implement
}
int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read)
{
return libusb_bulk_transfer(dev->devh, 0x81, buf, len, n_read, 3000);
}
#if 0
int rtlsdr_async_loop(rtlsdr_dev_t *dev, rtlsdr_async_read_cb_t cb, void *ctx)
{
return 0;
}
#endif

@ -0,0 +1,73 @@
/*
* rtl-sdr, a poor man's SDR using a Realtek RTL2832 based DVB-stick
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
*
* 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/>.
*/
#ifndef __RTL_SDR_H
#define __RTL_SDR_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
typedef int rtlsdr_dev_t;
/* must be called once before using any other library functions */
int rtlsdr_init(void);
/* must be called once at application shutdown */
void rtlsdr_exit(void);
uint32_t rtlsdr_get_device_count(void);
const char *rtlsdr_get_device_name(uint32_t index);
rtlsdr_dev_t *rtlsdr_open(uint32_t index);
int rtlsdr_close(rtlsdr_dev_t *dev);
/* configuration functions */
int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq);
int rtlsdr_get_center_freq(rtlsdr_dev_t *dev);
int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int32_t ppm);
int32_t rtlsdr_get_freq_correction(rtlsdr_dev_t *dev);
/* 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_get_sample_rate(rtlsdr_dev_t *dev);
/* streaming functions */
int rtlsdr_reset_buffer(rtlsdr_dev_t *dev);
int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, uint32_t len, uint32_t *n_read);
typedef void(*rtlsdr_async_read_cb_t)(const char *buf, uint32_t len, void *ctx);
int rtlsdr_async_loop(rtlsdr_dev_t *dev, rtlsdr_async_read_cb_t cb, void *ctx);
#ifdef __cplusplus
}
#endif
#endif /* __RTL_SDR_H */

@ -25,7 +25,7 @@
/* glue functions to rtl-sdr code */ /* glue functions to rtl-sdr code */
int int
I2CReadByte( I2CReadByte(
int pTuner, void *pTuner,
unsigned char NoUse, unsigned char NoUse,
unsigned char RegAddr, unsigned char RegAddr,
unsigned char *pReadingByte unsigned char *pReadingByte
@ -33,10 +33,10 @@ I2CReadByte(
{ {
uint8_t data = RegAddr; uint8_t data = RegAddr;
if (rtl_i2c_write(E4K_I2C_ADDR, &data, 1) < 0) if (rtlsdr_i2c_write((rtlsdr_dev_t *)pTuner, E4K_I2C_ADDR, &data, 1) < 0)
return E4000_I2C_FAIL; return E4000_I2C_FAIL;
if (rtl_i2c_read(E4K_I2C_ADDR, &data, 1) < 0) if (rtlsdr_i2c_read((rtlsdr_dev_t *)pTuner, E4K_I2C_ADDR, &data, 1) < 0)
return E4000_I2C_FAIL; return E4000_I2C_FAIL;
*pReadingByte = data; *pReadingByte = data;
@ -45,19 +45,18 @@ I2CReadByte(
} }
int int
I2CWriteByte( I2CWriteByte(void *pTuner,
int pTuner, unsigned char NoUse,
unsigned char NoUse, unsigned char RegAddr,
unsigned char RegAddr, unsigned char WritingByte
unsigned char WritingByte )
)
{ {
uint8_t data[2]; uint8_t data[2];
data[0] = RegAddr; data[0] = RegAddr;
data[1] = WritingByte; data[1] = WritingByte;
if (rtl_i2c_write(E4K_I2C_ADDR, data, 2) < 0) if (rtlsdr_i2c_write((rtlsdr_dev_t *)pTuner, E4K_I2C_ADDR, data, 2) < 0)
return E4000_I2C_FAIL; return E4000_I2C_FAIL;
return E4000_I2C_SUCCESS; return E4000_I2C_SUCCESS;
@ -65,7 +64,7 @@ I2CWriteByte(
int int
I2CWriteArray( I2CWriteArray(
int pTuner, void *pTuner,
unsigned char NoUse, unsigned char NoUse,
unsigned char RegStartAddr, unsigned char RegStartAddr,
unsigned char ByteNum, unsigned char ByteNum,
@ -80,7 +79,7 @@ I2CWriteArray(
for(i = 0; i < ByteNum; i++) for(i = 0; i < ByteNum; i++)
WritingBuffer[1 + i] = pWritingBytes[i]; WritingBuffer[1 + i] = pWritingBytes[i];
if (rtl_i2c_write(E4K_I2C_ADDR, WritingBuffer, ByteNum + 1) < 0) if (rtlsdr_i2c_write((rtlsdr_dev_t *)pTuner, E4K_I2C_ADDR, WritingBuffer, ByteNum + 1) < 0)
return E4000_I2C_FAIL; return E4000_I2C_FAIL;
return E4000_I2C_SUCCESS; return E4000_I2C_SUCCESS;
@ -92,9 +91,8 @@ I2CWriteArray(
*/ */
int int
e4000_Initialize( e4000_Initialize(void *pTuner
int pTuner )
)
{ {
// Initialize tuner. // Initialize tuner.
@ -129,7 +127,7 @@ error_status_execute_function:
*/ */
int int
e4000_SetRfFreqHz( e4000_SetRfFreqHz(
int pTuner, void *pTuner,
unsigned long RfFreqHz unsigned long RfFreqHz
) )
{ {
@ -182,7 +180,7 @@ error_status_execute_function:
*/ */
int int
e4000_SetBandwidthHz( e4000_SetBandwidthHz(
int pTuner, void *pTuner,
unsigned long BandwidthHz unsigned long BandwidthHz
) )
{ {
@ -285,7 +283,7 @@ int GainControlinit();
* *
\****************************************************************************/ \****************************************************************************/
int tunerreset(int pTuner) int tunerreset(void *pTuner)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -332,7 +330,7 @@ int tunerreset(int pTuner)
* Function disables the clock - values can be modified to enable if required. * Function disables the clock - values can be modified to enable if required.
\****************************************************************************/ \****************************************************************************/
int Tunerclock(int pTuner) int Tunerclock(void *pTuner)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -364,7 +362,7 @@ int Tunerclock(int pTuner)
* *
\****************************************************************************/ \****************************************************************************/
/* /*
int filtercal(int pTuner) int filtercal(void *pTuner)
{ {
//writearray[0] = 1; //writearray[0] = 1;
//I2CWriteByte (pTuner, 200,123,writearray[0]); //I2CWriteByte (pTuner, 200,123,writearray[0]);
@ -382,7 +380,7 @@ int filtercal(int pTuner)
* *
\****************************************************************************/ \****************************************************************************/
int Qpeak(int pTuner) int Qpeak(void *pTuner)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -431,7 +429,7 @@ int Qpeak(int pTuner)
* 0xa3 to 0xa7. Also 0x24. * 0xa3 to 0xa7. Also 0x24.
* *
\****************************************************************************/ \****************************************************************************/
int E4000_gain_freq(int pTuner, int Freq) int E4000_gain_freq(void *pTuner, int Freq)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -512,7 +510,7 @@ int E4000_gain_freq(int pTuner, int Freq)
* Populates DC offset LUT. (Registers 0x2d, 0x70, 0x71). * Populates DC offset LUT. (Registers 0x2d, 0x70, 0x71).
* Turns on DC offset LUT and time varying DC offset. * Turns on DC offset LUT and time varying DC offset.
\****************************************************************************/ \****************************************************************************/
int DCoffloop(int pTuner) int DCoffloop(void *pTuner)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -548,7 +546,7 @@ int DCoffloop(int pTuner)
* *
\****************************************************************************/ \****************************************************************************/
/* /*
int commonmode(int pTuner) int commonmode(void *pTuner)
{ {
//writearray[0] = 0; //writearray[0] = 0;
//I2CWriteByte(Device_address,47,writearray[0]); //I2CWriteByte(Device_address,47,writearray[0]);
@ -571,7 +569,7 @@ int commonmode(int pTuner)
* Sensitivity / Linearity mode: manual switch * Sensitivity / Linearity mode: manual switch
* *
\****************************************************************************/ \****************************************************************************/
int GainControlinit(int pTuner) int GainControlinit(void *pTuner)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
unsigned char read1[1]; unsigned char read1[1];
@ -808,7 +806,7 @@ int GainControlauto();
* Sets Gain control to serial interface control. * Sets Gain control to serial interface control.
* *
\****************************************************************************/ \****************************************************************************/
int Gainmanual(int pTuner) int Gainmanual(void *pTuner)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -845,7 +843,7 @@ int Gainmanual(int pTuner)
* Configures E4000 PLL divider & sigma delta. 0x0d,0x09, 0x0a, 0x0b). * Configures E4000 PLL divider & sigma delta. 0x0d,0x09, 0x0a, 0x0b).
* *
\****************************************************************************/ \****************************************************************************/
int PLL(int pTuner, int Ref_clk, int Freq) int PLL(void *pTuner, int Ref_clk, int Freq)
{ {
int VCO_freq; int VCO_freq;
unsigned char writearray[5]; unsigned char writearray[5];
@ -1185,7 +1183,7 @@ int PLL(int pTuner, int Ref_clk, int Freq)
* *
\****************************************************************************/ \****************************************************************************/
int LNAfilter(int pTuner, int Freq) int LNAfilter(void *pTuner, int Freq)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -1334,7 +1332,7 @@ int LNAfilter(int pTuner, int Freq)
* The function configures the E4000 IF filter. (Register 0x11,0x12). * The function configures the E4000 IF filter. (Register 0x11,0x12).
* *
\****************************************************************************/ \****************************************************************************/
int IFfilter(int pTuner, int bandwidth, int Ref_clk) int IFfilter(void *pTuner, int bandwidth, int Ref_clk)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -1524,7 +1522,7 @@ int IFfilter(int pTuner, int bandwidth, int Ref_clk)
* Configures the E4000 frequency band. (Registers 0x07, 0x78). * Configures the E4000 frequency band. (Registers 0x07, 0x78).
* *
\****************************************************************************/ \****************************************************************************/
int freqband(int pTuner, int Freq) int freqband(void *pTuner, int Freq)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -1582,7 +1580,7 @@ int freqband(int pTuner, int Freq)
* Populates DC offset LUT. (Registers 0x50 - 0x53, 0x60 - 0x63). * Populates DC offset LUT. (Registers 0x50 - 0x53, 0x60 - 0x63).
* *
\****************************************************************************/ \****************************************************************************/
int DCoffLUT(int pTuner) int DCoffLUT(void *pTuner)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -1856,7 +1854,7 @@ int DCoffLUT(int pTuner)
* Configures gain control mode. (Registers 0x1a) * Configures gain control mode. (Registers 0x1a)
* *
\****************************************************************************/ \****************************************************************************/
int GainControlauto(int pTuner) int GainControlauto(void *pTuner)
{ {
unsigned char writearray[5]; unsigned char writearray[5];
int status; int status;
@ -1902,7 +1900,7 @@ int main()
* *
\****************************************************************************/ \****************************************************************************/
int E4000_sensitivity(int pTuner, int Freq, int bandwidth) int E4000_sensitivity(void *pTuner, int Freq, int bandwidth)
{ {
unsigned char writearray[2]; unsigned char writearray[2];
int status; int status;
@ -1960,7 +1958,7 @@ int E4000_sensitivity(int pTuner, int Freq, int bandwidth)
* The function configures the E4000 for linearity mode. * The function configures the E4000 for linearity mode.
* *
\****************************************************************************/ \****************************************************************************/
int E4000_linearity(int pTuner, int Freq, int bandwidth) int E4000_linearity(void *pTuner, int Freq, int bandwidth)
{ {
unsigned char writearray[2]; unsigned char writearray[2];
@ -2019,7 +2017,7 @@ int E4000_linearity(int pTuner, int Freq, int bandwidth)
* The function configures the E4000 for nominal * The function configures the E4000 for nominal
* *
\****************************************************************************/ \****************************************************************************/
int E4000_nominal(int pTuner, int Freq, int bandwidth) int E4000_nominal(void *pTuner, int Freq, int bandwidth)
{ {
unsigned char writearray[2]; unsigned char writearray[2];
int status; int status;

@ -118,52 +118,50 @@ int main(void)
// Function (implemeted for E4000) // Function (implemeted for E4000)
int int
I2CReadByte( I2CReadByte(void *pTuner,
int pTuner, unsigned char NoUse,
unsigned char NoUse, unsigned char RegAddr,
unsigned char RegAddr, unsigned char *pReadingByte
unsigned char *pReadingByte );
);
int int
I2CWriteByte( I2CWriteByte(
int pTuner, void *pTuner,
unsigned char NoUse, unsigned char NoUse,
unsigned char RegAddr, unsigned char RegAddr,
unsigned char WritingByte unsigned char WritingByte
); );
int int
I2CWriteArray( I2CWriteArray(void *pTuner,
int pTuner, unsigned char NoUse,
unsigned char NoUse, unsigned char RegStartAddr,
unsigned char RegStartAddr, unsigned char ByteNum,
unsigned char ByteNum, unsigned char *pWritingBytes
unsigned char *pWritingBytes );
);
// Functions (from E4000 source code) // Functions (from E4000 source code)
int tunerreset (int pTuner); int tunerreset (void *pTuner);
int Tunerclock(int pTuner); int Tunerclock(void *pTuner);
int Qpeak(int pTuner); int Qpeak(void *pTuner);
int DCoffloop(int pTuner); int DCoffloop(void *pTuner);
int GainControlinit(int pTuner); int GainControlinit(void *pTuner);
int Gainmanual(int pTuner); int Gainmanual(void *pTuner);
int E4000_gain_freq(int pTuner, int frequency); int E4000_gain_freq(void *pTuner, int frequency);
int PLL(int pTuner, int Ref_clk, int Freq); int PLL(void *pTuner, int Ref_clk, int Freq);
int LNAfilter(int pTuner, int Freq); int LNAfilter(void *pTuner, int Freq);
int IFfilter(int pTuner, int bandwidth, int Ref_clk); int IFfilter(void *pTuner, int bandwidth, int Ref_clk);
int freqband(int pTuner, int Freq); int freqband(void *pTuner, int Freq);
int DCoffLUT(int pTuner); int DCoffLUT(void *pTuner);
int GainControlauto(int pTuner); int GainControlauto(void *pTuner);
int E4000_sensitivity(int pTuner, int Freq, int bandwidth); int E4000_sensitivity(void *pTuner, int Freq, int bandwidth);
int E4000_linearity(int pTuner, int Freq, int bandwidth); int E4000_linearity(void *pTuner, int Freq, int bandwidth);
int E4000_high_linearity(int pTuner); int E4000_high_linearity(void *pTuner);
int E4000_nominal(int pTuner, int Freq, int bandwidth); int E4000_nominal(void *pTuner, int Freq, int bandwidth);
// The following context is E4000 tuner API source code // The following context is E4000 tuner API source code
@ -182,30 +180,30 @@ enum E4000_BANDWIDTH_HZ
// Manipulaing functions // Manipulaing functions
void void
e4000_GetTunerType( e4000_GetTunerType(
int pTuner, void *pTuner,
int *pTunerType int *pTunerType
); );
void void
e4000_GetDeviceAddr( e4000_GetDeviceAddr(
int pTuner, void *pTuner,
unsigned char *pDeviceAddr unsigned char *pDeviceAddr
); );
int int
e4000_Initialize( e4000_Initialize(
int pTuner void *pTuner
); );
int int
e4000_SetRfFreqHz( e4000_SetRfFreqHz(
int pTuner, void *pTuner,
unsigned long RfFreqHz unsigned long RfFreqHz
); );
int int
e4000_GetRfFreqHz( e4000_GetRfFreqHz(
int pTuner, void *pTuner,
unsigned long *pRfFreqHz unsigned long *pRfFreqHz
); );
@ -216,20 +214,20 @@ e4000_GetRfFreqHz(
// Extra manipulaing functions // Extra manipulaing functions
int int
e4000_GetRegByte( e4000_GetRegByte(
int pTuner, void *pTuner,
unsigned char RegAddr, unsigned char RegAddr,
unsigned char *pReadingByte unsigned char *pReadingByte
); );
int int
e4000_SetBandwidthHz( e4000_SetBandwidthHz(
int pTuner, void *pTuner,
unsigned long BandwidthHz unsigned long BandwidthHz
); );
int int
e4000_GetBandwidthHz( e4000_GetBandwidthHz(
int pTuner, void *pTuner,
unsigned long *pBandwidthHz unsigned long *pBandwidthHz
); );

@ -7,33 +7,34 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include "i2c.h"
#include "tuner_fc0013.h" #include "tuner_fc0013.h"
#define CRYSTAL_FREQ 28800000 #define CRYSTAL_FREQ 28800000
#define FC0013_I2C_ADDR 0xc6 #define FC0013_I2C_ADDR 0xc6
/* glue functions to rtl-sdr code */ /* glue functions to rtl-sdr code */
int FC0013_Write(int pTuner, unsigned char RegAddr, unsigned char Byte) int FC0013_Write(void *pTuner, unsigned char RegAddr, unsigned char Byte)
{ {
uint8_t data[2]; uint8_t data[2];
data[0] = RegAddr; data[0] = RegAddr;
data[1] = Byte; data[1] = Byte;
if (rtl_i2c_write(FC0013_I2C_ADDR, data, 2) < 0) if (rtlsdr_i2c_write((rtlsdr_dev_t *)pTuner, FC0013_I2C_ADDR, data, 2) < 0)
return FC0013_I2C_ERROR; return FC0013_I2C_ERROR;
return FC0013_I2C_SUCCESS; return FC0013_I2C_SUCCESS;
} }
int FC0013_Read(int pTuner, unsigned char RegAddr, unsigned char *pByte) int FC0013_Read(void *pTuner, unsigned char RegAddr, unsigned char *pByte)
{ {
uint8_t data = RegAddr; uint8_t data = RegAddr;
if (rtl_i2c_write(FC0013_I2C_ADDR, &data, 1) < 0) if (rtlsdr_i2c_write((rtlsdr_dev_t *)pTuner, FC0013_I2C_ADDR, &data, 1) < 0)
return FC0013_I2C_ERROR; return FC0013_I2C_ERROR;
if (rtl_i2c_read(FC0013_I2C_ADDR, &data, 1) < 0) if (rtlsdr_i2c_read((rtlsdr_dev_t *)pTuner, FC0013_I2C_ADDR, &data, 1) < 0)
return FC0013_I2C_ERROR; return FC0013_I2C_ERROR;
*pByte = data; *pByte = data;
@ -41,7 +42,7 @@ int FC0013_Read(int pTuner, unsigned char RegAddr, unsigned char *pByte)
return FC0013_I2C_SUCCESS; return FC0013_I2C_SUCCESS;
} }
int FC0013_SetVhfTrack(int pTuner, unsigned long FrequencyKHz) int FC0013_SetVhfTrack(void *pTuner, unsigned long FrequencyKHz)
{ {
unsigned char read_byte; unsigned char read_byte;
@ -120,9 +121,8 @@ error_status:
// FC0013 Open Function, includes enable/reset pin control and registers initialization. // FC0013 Open Function, includes enable/reset pin control and registers initialization.
//void FC0013_Open() //void FC0013_Open()
int FC0013_Open() int FC0013_Open(void *pTuner)
{ {
int pTuner = 1;
// Enable FC0013 Power // Enable FC0013 Power
// (...) // (...)
// FC0013 Enable = High // FC0013 Enable = High
@ -171,7 +171,7 @@ error_status:
} }
int FC0013_SetFrequency(unsigned long Frequency, unsigned short Bandwidth) int FC0013_SetFrequency(void *pTuner, unsigned long Frequency, unsigned short Bandwidth)
{ {
// bool VCO1 = false; // bool VCO1 = false;
// unsigned int doubleVCO; // unsigned int doubleVCO;
@ -186,8 +186,6 @@ int FC0013_SetFrequency(unsigned long Frequency, unsigned short Bandwidth)
unsigned long CrystalFreqKhz; unsigned long CrystalFreqKhz;
int pTuner =1;
int CrystalFreqHz = CRYSTAL_FREQ; int CrystalFreqHz = CRYSTAL_FREQ;
// Get tuner crystal frequency in KHz. // Get tuner crystal frequency in KHz.

@ -39,12 +39,12 @@ enum FC0013_FUNCTION_STATUS
// Functions // Functions
int FC0013_Read(int pTuner, unsigned char RegAddr, unsigned char *pByte); int FC0013_Read(void *pTuner, unsigned char RegAddr, unsigned char *pByte);
int FC0013_Write(int pTuner, unsigned char RegAddr, unsigned char Byte); int FC0013_Write(void *pTuner, unsigned char RegAddr, unsigned char Byte);
int int
fc0013_SetRegMaskBits( fc0013_SetRegMaskBits(
int pTuner, void *pTuner,
unsigned char RegAddr, unsigned char RegAddr,
unsigned char Msb, unsigned char Msb,
unsigned char Lsb, unsigned char Lsb,
@ -53,18 +53,18 @@ fc0013_SetRegMaskBits(
int int
fc0013_GetRegMaskBits( fc0013_GetRegMaskBits(
int pTuner, void *pTuner,
unsigned char RegAddr, unsigned char RegAddr,
unsigned char Msb, unsigned char Msb,
unsigned char Lsb, unsigned char Lsb,
unsigned char *pReadingValue unsigned char *pReadingValue
); );
int FC0013_Open(); int FC0013_Open(void *pTuner);
int FC0013_SetFrequency(unsigned long Frequency, unsigned short Bandwidth); int FC0013_SetFrequency(void *pTuner, unsigned long Frequency, unsigned short Bandwidth);
// Set VHF Track depends on input frequency // Set VHF Track depends on input frequency
int FC0013_SetVhfTrack(int pTuner, unsigned long Frequency); int FC0013_SetVhfTrack(void *pTuner, unsigned long Frequency);
// The following context is FC0013 tuner API source code // The following context is FC0013 tuner API source code
@ -98,54 +98,54 @@ enum FC0013_LNA_GAIN_VALUE
// Manipulaing functions // Manipulaing functions
void void
fc0013_GetTunerType( fc0013_GetTunerType(
int pTuner, void *pTuner,
int *pTunerType int *pTunerType
); );
void void
fc0013_GetDeviceAddr( fc0013_GetDeviceAddr(
int pTuner, void *pTuner,
unsigned char *pDeviceAddr unsigned char *pDeviceAddr
); );
int int
fc0013_Initialize( fc0013_Initialize(
int pTuner void *pTuner
); );
int int
fc0013_SetRfFreqHz( fc0013_SetRfFreqHz(
int pTuner, void *pTuner,
unsigned long RfFreqHz unsigned long RfFreqHz
); );
int int
fc0013_GetRfFreqHz( fc0013_GetRfFreqHz(
int pTuner, void *pTuner,
unsigned long *pRfFreqHz unsigned long *pRfFreqHz
); );
// Extra manipulaing functions // Extra manipulaing functions
int int
fc0013_SetBandwidthMode( fc0013_SetBandwidthMode(
int pTuner, void *pTuner,
int BandwidthMode int BandwidthMode
); );
int int
fc0013_GetBandwidthMode( fc0013_GetBandwidthMode(
int pTuner, void *pTuner,
int *pBandwidthMode int *pBandwidthMode
); );
int int
fc0013_RcCalReset( fc0013_RcCalReset(
int pTuner void *pTuner
); );
int int
fc0013_RcCalAdd( fc0013_RcCalAdd(
int pTuner, void *pTuner,
int RcValue int RcValue
); );

Loading…
Cancel
Save