initial commit

Signed-off-by: Steve Markgraf <steve@steve-m.de>
This commit is contained in:
Steve Markgraf 2012-03-12 00:30:54 +01:00
commit 5a4fd14581
7 changed files with 3329 additions and 0 deletions

10
src/Makefile Normal file
View File

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

7
src/i2c.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef __I2C_H
#define __I2C_H
int rtl_i2c_write(uint8_t i2c_addr, uint8_t *buffer, int len);
int rtl_i2c_read(uint8_t i2c_addr, uint8_t *buffer, int len);
#endif

415
src/main.c Normal file
View File

@ -0,0 +1,415 @@
/*
* 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 <unistd.h>
#include <libusb.h>
#include "tuner_e4000.h"
#include "tuner_fc0013.h"
#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
static struct libusb_device_handle *devh = NULL;
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_resampler(uint32_t rsamp_ratio)
{
uint16_t tmp;
rsamp_ratio <<= 2;
tmp = (rsamp_ratio >> 16) & 0xffff;
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);
/* TODO setting resampler test value, max value is 0xC99999,
* value for DAB/FM is 0xE10000*/
set_resampler(1 << 24);
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, 80000);
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)
{
printf("rtl-sdr, an I/Q recorder for RTL2832 based USB-sticks\n\n"
"Usage:\t-f frequency to tune to [Hz]\n"
"\toutput filename\n");
exit(1);
}
static void sighandler(int signum)
{
do_exit = 1;
}
int main(int argc, char **argv)
{
struct sigaction sigact;
int r, opt;
char *filename;
unsigned int frequency = 0;
uint8_t buffer[READLEN];
int n_read;
FILE *file;
while ((opt = getopt(argc, argv, "f:")) != -1) {
switch (opt) {
case 'f':
frequency = atoi(optarg);
break;
default:
usage();
break;
}
}
if (argc <= optind) {
usage();
} else {
filename = argv[optind];
}
r = libusb_init(NULL);
if (r < 0) {
fprintf(stderr, "Failed to initialize libusb\n");
exit(1);
}
r = find_device();
if (r < 0) {
fprintf(stderr, "Could not find/open device\n");
goto out;
}
r = libusb_claim_interface(devh, 0);
if (r < 0) {
fprintf(stderr, "usb_claim_interface error %d\n", r);
goto out;
}
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
/* Initialize the RTL2832 */
rtl_init();
/* Initialize tuner & set frequency */
tuner_init(frequency);
file = fopen(filename, "wb");
if (!file) {
printf("Failed to open %s\n", filename);
goto out;
}
/* reset endpoint before we start reading */
rtl_write_reg(USBB, USB_EPA_CTL, 0x1002, 2);
rtl_write_reg(USBB, USB_EPA_CTL, 0x0000, 2);
printf("Reading samples...\n");
while (!do_exit) {
libusb_bulk_transfer(devh, 0x81, buffer, READLEN, &n_read, 3000);
fwrite(buffer, n_read, 1, file);
if (n_read < READLEN) {
printf("Short bulk read, samples lost, exiting!\n");
break;
}
}
fclose(file);
libusb_release_interface(devh, 0);
out:
libusb_close(devh);
libusb_exit(NULL);
return r >= 0 ? r : -r;
}

2073
src/tuner_e4000.c Normal file

File diff suppressed because it is too large Load Diff

236
src/tuner_e4000.h Normal file
View File

@ -0,0 +1,236 @@
#ifndef __TUNER_E4000_H
#define __TUNER_E4000_H
/**
@file
@brief E4000 tuner module declaration
One can manipulate E4000 tuner through E4000 module.
E4000 module is derived from tuner module.
@par Example:
@code
// The example is the same as the tuner example in tuner_base.h except the listed lines.
#include "tuner_e4000.h"
...
int main(void)
{
TUNER_MODULE *pTuner;
E4000_EXTRA_MODULE *pTunerExtra;
TUNER_MODULE TunerModuleMemory;
BASE_INTERFACE_MODULE BaseInterfaceModuleMemory;
// I2C_BRIDGE_MODULE I2cBridgeModuleMemory;
unsigned long BandwidthMode;
...
// Build E4000 tuner module.
BuildE4000Module(
&pTuner,
&TunerModuleMemory,
&BaseInterfaceModuleMemory,
&I2cBridgeModuleMemory,
0xac, // I2C device address is 0xac in 8-bit format.
CRYSTAL_FREQ_16384000HZ, // Crystal frequency is 16.384 MHz.
E4000_AGC_INTERNAL // The E4000 AGC mode is internal AGC mode.
);
// Get E4000 tuner extra module.
pTunerExtra = (T2266_EXTRA_MODULE *)(pTuner->pExtra);
// ==== Initialize tuner and set its parameters =====
...
// Set E4000 bandwidth.
pTunerExtra->SetBandwidthMode(pTuner, E4000_BANDWIDTH_6MHZ);
// ==== Get tuner information =====
...
// Get E4000 bandwidth.
pTunerExtra->GetBandwidthMode(pTuner, &BandwidthMode);
// See the example for other tuner functions in tuner_base.h
return 0;
}
@endcode
*/
//#include "tuner_base.h"
// The following context is implemented for E4000 source code.
// Definition (implemeted for E4000)
#define E4000_1_SUCCESS 1
#define E4000_1_FAIL 0
#define E4000_I2C_SUCCESS 1
#define E4000_I2C_FAIL 0
// Function (implemeted for E4000)
int
I2CReadByte(
int pTuner,
unsigned char NoUse,
unsigned char RegAddr,
unsigned char *pReadingByte
);
int
I2CWriteByte(
int pTuner,
unsigned char NoUse,
unsigned char RegAddr,
unsigned char WritingByte
);
int
I2CWriteArray(
int pTuner,
unsigned char NoUse,
unsigned char RegStartAddr,
unsigned char ByteNum,
unsigned char *pWritingBytes
);
// Functions (from E4000 source code)
int tunerreset (int pTuner);
int Tunerclock(int pTuner);
int Qpeak(int pTuner);
int DCoffloop(int pTuner);
int GainControlinit(int pTuner);
int Gainmanual(int pTuner);
int E4000_gain_freq(int pTuner, int frequency);
int PLL(int pTuner, int Ref_clk, int Freq);
int LNAfilter(int pTuner, int Freq);
int IFfilter(int pTuner, int bandwidth, int Ref_clk);
int freqband(int pTuner, int Freq);
int DCoffLUT(int pTuner);
int GainControlauto(int pTuner);
int E4000_sensitivity(int pTuner, int Freq, int bandwidth);
int E4000_linearity(int pTuner, int Freq, int bandwidth);
int E4000_high_linearity(int pTuner);
int E4000_nominal(int pTuner, int Freq, int bandwidth);
// The following context is E4000 tuner API source code
// Definitions
// Bandwidth in Hz
enum E4000_BANDWIDTH_HZ
{
E4000_BANDWIDTH_6000000HZ = 6000000,
E4000_BANDWIDTH_7000000HZ = 7000000,
E4000_BANDWIDTH_8000000HZ = 8000000,
};
// Manipulaing functions
void
e4000_GetTunerType(
int pTuner,
int *pTunerType
);
void
e4000_GetDeviceAddr(
int pTuner,
unsigned char *pDeviceAddr
);
int
e4000_Initialize(
int pTuner
);
int
e4000_SetRfFreqHz(
int pTuner,
unsigned long RfFreqHz
);
int
e4000_GetRfFreqHz(
int pTuner,
unsigned long *pRfFreqHz
);
// Extra manipulaing functions
int
e4000_GetRegByte(
int pTuner,
unsigned char RegAddr,
unsigned char *pReadingByte
);
int
e4000_SetBandwidthHz(
int pTuner,
unsigned long BandwidthHz
);
int
e4000_GetBandwidthHz(
int pTuner,
unsigned long *pBandwidthHz
);
#endif

433
src/tuner_fc0013.c Normal file
View File

@ -0,0 +1,433 @@
/*
* Fitipower FC0013 tuner driver, taken from the kernel driver that can be found
* on http://linux.terratec.de/tv_en.html
*
* This driver is a mess, and should be cleaned up/rewritten.
*
*/
#include <stdint.h>
#include "tuner_fc0013.h"
#define CRYSTAL_FREQ 28800000
#define FC0013_I2C_ADDR 0xc6
/* glue functions to rtl-sdr code */
int FC0013_Write(int pTuner, unsigned char RegAddr, unsigned char Byte)
{
uint8_t data[2];
data[0] = RegAddr;
data[1] = Byte;
if (rtl_i2c_write(FC0013_I2C_ADDR, data, 2) < 0)
return FC0013_I2C_ERROR;
return FC0013_I2C_SUCCESS;
}
int FC0013_Read(int pTuner, unsigned char RegAddr, unsigned char *pByte)
{
uint8_t data = RegAddr;
if (rtl_i2c_write(FC0013_I2C_ADDR, &data, 1) < 0)
return FC0013_I2C_ERROR;
if (rtl_i2c_read(FC0013_I2C_ADDR, &data, 1) < 0)
return FC0013_I2C_ERROR;
*pByte = data;
return FC0013_I2C_SUCCESS;
}
int FC0013_SetVhfTrack(int pTuner, unsigned long FrequencyKHz)
{
unsigned char read_byte;
if (FrequencyKHz <= 177500) // VHF Track: 7
{
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x1C) != FC0013_I2C_SUCCESS) goto error_status;
}
else if (FrequencyKHz <= 184500) // VHF Track: 6
{
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x18) != FC0013_I2C_SUCCESS) goto error_status;
}
else if (FrequencyKHz <= 191500) // VHF Track: 5
{
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x14) != FC0013_I2C_SUCCESS) goto error_status;
}
else if (FrequencyKHz <= 198500) // VHF Track: 4
{
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x10) != FC0013_I2C_SUCCESS) goto error_status;
}
else if (FrequencyKHz <= 205500) // VHF Track: 3
{
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x0C) != FC0013_I2C_SUCCESS) goto error_status;
}
else if (FrequencyKHz <= 212500) // VHF Track: 2
{
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x08) != FC0013_I2C_SUCCESS) goto error_status;
}
else if (FrequencyKHz <= 219500) // VHF Track: 2
{
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x08) != FC0013_I2C_SUCCESS) goto error_status;
}
else if (FrequencyKHz <= 226500) // VHF Track: 1
{
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x04) != FC0013_I2C_SUCCESS) goto error_status;
}
else // VHF Track: 1
{
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x04) != FC0013_I2C_SUCCESS) goto error_status;
}
//------------------------------------------------ arios modify 2010-12-24
// " | 0x10" ==> " | 0x30" (make sure reg[0x07] bit5 = 1)
// Enable VHF filter.
if(FC0013_Read(pTuner, 0x07, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x07, read_byte | 0x10) != FC0013_I2C_SUCCESS) goto error_status;
// Disable UHF & GPS.
if(FC0013_Read(pTuner, 0x14, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x14, read_byte & 0x1F) != FC0013_I2C_SUCCESS) goto error_status;
return FC0013_FUNCTION_SUCCESS;
error_status:
return FC0013_FUNCTION_ERROR;
}
// FC0013 Open Function, includes enable/reset pin control and registers initialization.
//void FC0013_Open()
int FC0013_Open()
{
int pTuner = 1;
// Enable FC0013 Power
// (...)
// FC0013 Enable = High
// (...)
// FC0013 Reset = High -> Low
// (...)
//================================ update base on new FC0013 register bank
if(FC0013_Write(pTuner, 0x01, 0x09) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x02, 0x16) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x03, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x04, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x05, 0x17) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x06, 0x02) != FC0013_I2C_SUCCESS) goto error_status;
// if(FC0013_Write(pTuner, 0x07, 0x27) != FC0013_I2C_SUCCESS) goto error_status; // 28.8MHz, GainShift: 15
if(FC0013_Write(pTuner, 0x07, 0x2A) != FC0013_I2C_SUCCESS) goto error_status; // 28.8MHz, modified by Realtek
if(FC0013_Write(pTuner, 0x08, 0xFF) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x09, 0x6F) != FC0013_I2C_SUCCESS) goto error_status; // Enable Loop Through
if(FC0013_Write(pTuner, 0x0A, 0xB8) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0B, 0x82) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0C, 0xFE) != FC0013_I2C_SUCCESS) goto error_status; // Modified for up-dowm AGC by Realtek(for master, and for 2836BU dongle).
// if(FC0013_Write(pTuner, 0x0C, 0xFC) != FC0013_I2C_SUCCESS) goto error_status; // Modified for up-dowm AGC by Realtek(for slave, and for 2832 mini dongle).
// if(FC0013_Write(pTuner, 0x0D, 0x09) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0D, 0x01) != FC0013_I2C_SUCCESS) goto error_status; // Modified for AGC non-forcing by Realtek.
if(FC0013_Write(pTuner, 0x0E, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0F, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x10, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x11, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x12, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x13, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x14, 0x50) != FC0013_I2C_SUCCESS) goto error_status; // DVB-T, High Gain
// if(FC0013_Write(pTuner, 0x14, 0x48) != FC0013_I2C_SUCCESS) goto error_status; // DVB-T, Middle Gain
// if(FC0013_Write(pTuner, 0x14, 0x40) != FC0013_I2C_SUCCESS) goto error_status; // DVB-T, Low Gain
if(FC0013_Write(pTuner, 0x15, 0x01) != FC0013_I2C_SUCCESS) goto error_status;
return FC0013_FUNCTION_SUCCESS;
error_status:
return FC0013_FUNCTION_ERROR;
}
int FC0013_SetFrequency(unsigned long Frequency, unsigned short Bandwidth)
{
// bool VCO1 = false;
// unsigned int doubleVCO;
// unsigned short xin, xdiv;
// unsigned char reg[21], am, pm, multi;
int VCO1 = FC0013_FALSE;
unsigned long doubleVCO;
unsigned short xin, xdiv;
unsigned char reg[21], am, pm, multi;
unsigned char read_byte;
unsigned long CrystalFreqKhz;
int pTuner =1;
int CrystalFreqHz = CRYSTAL_FREQ;
// Get tuner crystal frequency in KHz.
// Note: CrystalFreqKhz = round(CrystalFreqHz / 1000)
CrystalFreqKhz = (CrystalFreqHz + 500) / 1000;
// modified 2011-02-09: for D-Book test
// set VHF_Track = 7
if(FC0013_Read(pTuner, 0x1D, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
// VHF Track: 7
if(FC0013_Write(pTuner, 0x1D, (read_byte & 0xE3) | 0x1C) != FC0013_I2C_SUCCESS) goto error_status;
if( Frequency < 300000 )
{
// Set VHF Track.
if(FC0013_SetVhfTrack(pTuner, Frequency) != FC0013_I2C_SUCCESS) goto error_status;
// Enable VHF filter.
if(FC0013_Read(pTuner, 0x07, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x07, read_byte | 0x10) != FC0013_I2C_SUCCESS) goto error_status;
// Disable UHF & disable GPS.
if(FC0013_Read(pTuner, 0x14, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x14, read_byte & 0x1F) != FC0013_I2C_SUCCESS) goto error_status;
}
else if ( (Frequency >= 300000) && (Frequency <= 862000) )
{
// Disable VHF filter.
if(FC0013_Read(pTuner, 0x07, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x07, read_byte & 0xEF) != FC0013_I2C_SUCCESS) goto error_status;
// enable UHF & disable GPS.
if(FC0013_Read(pTuner, 0x14, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x14, (read_byte & 0x1F) | 0x40) != FC0013_I2C_SUCCESS) goto error_status;
}
else if (Frequency > 862000)
{
// Disable VHF filter
if(FC0013_Read(pTuner, 0x07, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x07, read_byte & 0xEF) != FC0013_I2C_SUCCESS) goto error_status;
// Disable UHF & enable GPS
if(FC0013_Read(pTuner, 0x14, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x14, (read_byte & 0x1F) | 0x20) != FC0013_I2C_SUCCESS) goto error_status;
}
if (Frequency * 96 < 3560000)
{
multi = 96;
reg[5] = 0x82;
reg[6] = 0x00;
}
else if (Frequency * 64 < 3560000)
{
multi = 64;
reg[5] = 0x02;
reg[6] = 0x02;
}
else if (Frequency * 48 < 3560000)
{
multi = 48;
reg[5] = 0x42;
reg[6] = 0x00;
}
else if (Frequency * 32 < 3560000)
{
multi = 32;
reg[5] = 0x82;
reg[6] = 0x02;
}
else if (Frequency * 24 < 3560000)
{
multi = 24;
reg[5] = 0x22;
reg[6] = 0x00;
}
else if (Frequency * 16 < 3560000)
{
multi = 16;
reg[5] = 0x42;
reg[6] = 0x02;
}
else if (Frequency * 12 < 3560000)
{
multi = 12;
reg[5] = 0x12;
reg[6] = 0x00;
}
else if (Frequency * 8 < 3560000)
{
multi = 8;
reg[5] = 0x22;
reg[6] = 0x02;
}
else if (Frequency * 6 < 3560000)
{
multi = 6;
reg[5] = 0x0A;
reg[6] = 0x00;
}
else if (Frequency * 4 < 3800000)
{
multi = 4;
reg[5] = 0x12;
reg[6] = 0x02;
}
else
{
Frequency = Frequency / 2;
multi = 4;
reg[5] = 0x0A;
reg[6] = 0x02;
}
doubleVCO = Frequency * multi;
reg[6] = reg[6] | 0x08;
// VCO1 = true;
VCO1 = FC0013_TRUE;
// Calculate VCO parameters: ap & pm & xin.
// xdiv = (unsigned short)(doubleVCO / (Crystal_Frequency/2));
xdiv = (unsigned short)(doubleVCO / (CrystalFreqKhz/2));
// if( (doubleVCO - xdiv * (Crystal_Frequency/2)) >= (Crystal_Frequency/4) )
if( (doubleVCO - xdiv * (CrystalFreqKhz/2)) >= (CrystalFreqKhz/4) )
{
xdiv = xdiv + 1;
}
pm = (unsigned char)( xdiv / 8 );
am = (unsigned char)( xdiv - (8 * pm));
if (am < 2)
{
reg[1] = am + 8;
reg[2] = pm - 1;
}
else
{
reg[1] = am;
reg[2] = pm;
}
// xin = (unsigned short)(doubleVCO - ((unsigned short)(doubleVCO / (Crystal_Frequency/2))) * (Crystal_Frequency/2));
xin = (unsigned short)(doubleVCO - ((unsigned short)(doubleVCO / (CrystalFreqKhz/2))) * (CrystalFreqKhz/2));
// xin = ((xin << 15)/(Crystal_Frequency/2));
xin = (unsigned short)((xin << 15)/(CrystalFreqKhz/2));
// if( xin >= (unsigned short) pow( (double)2, (double)14) )
// {
// xin = xin + (unsigned short) pow( (double)2, (double)15);
// }
if( xin >= (unsigned short) 16384 )
xin = xin + (unsigned short) 32768;
reg[3] = (unsigned char)(xin >> 8);
reg[4] = (unsigned char)(xin & 0x00FF);
//===================================== Only for testing
// printf("Frequency: %d, Fa: %d, Fp: %d, Xin:%d \n", Frequency, am, pm, xin);
// Set Low-Pass Filter Bandwidth.
switch(Bandwidth)
{
case 6:
reg[6] = 0x80 | reg[6];
break;
case 7:
reg[6] = ~0x80 & reg[6];
reg[6] = 0x40 | reg[6];
break;
case 8:
default:
reg[6] = ~0xC0 & reg[6];
break;
}
reg[5] = reg[5] | 0x07;
if(FC0013_Write(pTuner, 0x01, reg[1]) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x02, reg[2]) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x03, reg[3]) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x04, reg[4]) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x05, reg[5]) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x06, reg[6]) != FC0013_I2C_SUCCESS) goto error_status;
if (multi == 64)
{
// FC0013_Write(0x11, FC0013_Read(0x11) | 0x04);
if(FC0013_Read(pTuner, 0x11, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x11, read_byte | 0x04) != FC0013_I2C_SUCCESS) goto error_status;
}
else
{
// FC0013_Write(0x11, FC0013_Read(0x11) & 0xFB);
if(FC0013_Read(pTuner, 0x11, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x11, read_byte & 0xFB) != FC0013_I2C_SUCCESS) goto error_status;
}
if(FC0013_Write(pTuner, 0x0E, 0x80) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0E, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0E, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
// reg[14] = 0x3F & FC0013_Read(0x0E);
if(FC0013_Read(pTuner, 0x0E, &read_byte) != FC0013_I2C_SUCCESS) goto error_status;
reg[14] = 0x3F & read_byte;
if (VCO1)
{
if (reg[14] > 0x3C)
{
reg[6] = ~0x08 & reg[6];
if(FC0013_Write(pTuner, 0x06, reg[6]) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0E, 0x80) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0E, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
}
}
else
{
if (reg[14] < 0x02)
{
reg[6] = 0x08 | reg[6];
if(FC0013_Write(pTuner, 0x06, reg[6]) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0E, 0x80) != FC0013_I2C_SUCCESS) goto error_status;
if(FC0013_Write(pTuner, 0x0E, 0x00) != FC0013_I2C_SUCCESS) goto error_status;
}
}
return 1;
error_status:
return 0;
}

155
src/tuner_fc0013.h Normal file
View File

@ -0,0 +1,155 @@
#ifndef __TUNER_FC0013_H
#define __TUNER_FC0013_H
/**
@file
@brief FC0013 tuner module declaration
One can manipulate FC0013 tuner through FC0013 module.
FC0013 module is derived from tuner module.
// The following context is implemented for FC0013 source code.
**/
// Definitions
enum FC0013_TRUE_FALSE_STATUS
{
FC0013_FALSE,
FC0013_TRUE,
};
enum FC0013_I2C_STATUS
{
FC0013_I2C_SUCCESS,
FC0013_I2C_ERROR,
};
enum FC0013_FUNCTION_STATUS
{
FC0013_FUNCTION_SUCCESS,
FC0013_FUNCTION_ERROR,
};
// Functions
int FC0013_Read(int pTuner, unsigned char RegAddr, unsigned char *pByte);
int FC0013_Write(int pTuner, unsigned char RegAddr, unsigned char Byte);
int
fc0013_SetRegMaskBits(
int pTuner,
unsigned char RegAddr,
unsigned char Msb,
unsigned char Lsb,
const unsigned char WritingValue
);
int
fc0013_GetRegMaskBits(
int pTuner,
unsigned char RegAddr,
unsigned char Msb,
unsigned char Lsb,
unsigned char *pReadingValue
);
int FC0013_Open();
int FC0013_SetFrequency(unsigned long Frequency, unsigned short Bandwidth);
// Set VHF Track depends on input frequency
int FC0013_SetVhfTrack(int pTuner, unsigned long Frequency);
// The following context is FC0013 tuner API source code
// Definitions
// Bandwidth mode
enum FC0013_BANDWIDTH_MODE
{
FC0013_BANDWIDTH_6000000HZ = 6,
FC0013_BANDWIDTH_7000000HZ = 7,
FC0013_BANDWIDTH_8000000HZ = 8,
};
// Default for initialing
#define FC0013_RF_FREQ_HZ_DEFAULT 50000000
#define FC0013_BANDWIDTH_MODE_DEFAULT FC0013_BANDWIDTH_8000000HZ
// Tuner LNA
enum FC0013_LNA_GAIN_VALUE
{
FC0013_LNA_GAIN_LOW = 0x00, // -6.3dB
FC0013_LNA_GAIN_MIDDLE = 0x08, // 7.1dB
FC0013_LNA_GAIN_HIGH_17 = 0x11, // 19.1dB
FC0013_LNA_GAIN_HIGH_19 = 0x10, // 19.7dB
};
// Manipulaing functions
void
fc0013_GetTunerType(
int pTuner,
int *pTunerType
);
void
fc0013_GetDeviceAddr(
int pTuner,
unsigned char *pDeviceAddr
);
int
fc0013_Initialize(
int pTuner
);
int
fc0013_SetRfFreqHz(
int pTuner,
unsigned long RfFreqHz
);
int
fc0013_GetRfFreqHz(
int pTuner,
unsigned long *pRfFreqHz
);
// Extra manipulaing functions
int
fc0013_SetBandwidthMode(
int pTuner,
int BandwidthMode
);
int
fc0013_GetBandwidthMode(
int pTuner,
int *pBandwidthMode
);
int
fc0013_RcCalReset(
int pTuner
);
int
fc0013_RcCalAdd(
int pTuner,
int RcValue
);
#endif