Permalink
Browse files

lib: cache i2c repeater

  • Loading branch information...
keenerd committed Aug 3, 2014
1 parent a0b6131 commit d59b178cc1eb27f0beca802a9cf82f1d6ded3a48
Showing with 22 additions and 5 deletions.
  1. +22 −5 src/librtlsdr.c
View
@@ -125,6 +125,7 @@ struct rtlsdr_dev {
int driver_active;
unsigned int xfer_errors;
int tuner_initialized;
int i2c_repeater_on;
};
void rtlsdr_set_gpio_bit(rtlsdr_dev_t *dev, uint8_t gpio, int val);
@@ -573,6 +574,10 @@ void rtlsdr_set_gpio_output(rtlsdr_dev_t *dev, uint8_t gpio)
void rtlsdr_set_i2c_repeater(rtlsdr_dev_t *dev, int on)
{
if (on == dev->i2c_repeater_on)
return;
on = !!on; /* values +2 to force on */
dev->i2c_repeater_on = on;
rtlsdr_demod_write_reg(dev, 1, 0x01, on ? 0x18 : 0x10, 1);
}
@@ -670,6 +675,8 @@ int rtlsdr_deinit_baseband(rtlsdr_dev_t *dev)
if (!dev)
return -1;
rtlsdr_set_i2c_repeater(dev, 0);
if (dev->tuner && dev->tuner->exit) {
rtlsdr_set_i2c_repeater(dev, 1);
r = dev->tuner->exit(dev); /* deinitialize tuner */
@@ -692,6 +699,7 @@ int rtlsdr_set_if_freq(rtlsdr_dev_t *dev, uint32_t freq)
if (!dev)
return -1;
rtlsdr_set_i2c_repeater(dev, 0);
/* read corrected clock value */
if (rtlsdr_get_xtal_freq(dev, &rtl_xtal, NULL))
@@ -712,7 +720,6 @@ int rtlsdr_set_if_freq(rtlsdr_dev_t *dev, uint32_t freq)
if(dev->tuner_initialized && dev->tuner && dev->tuner->set_if_freq) {
rtlsdr_set_i2c_repeater(dev, 1);
dev->tuner->set_if_freq(dev, freq);
rtlsdr_set_i2c_repeater(dev, 0);
}
return r;
}
@@ -722,6 +729,7 @@ int rtlsdr_set_sample_freq_correction(rtlsdr_dev_t *dev, int ppm)
int r = 0;
uint8_t tmp;
int16_t offs = ppm * (-1) * TWO_POW(24) / 1000000;
rtlsdr_set_i2c_repeater(dev, 0);
tmp = offs & 0xff;
r |= rtlsdr_demod_write_reg(dev, 1, 0x3f, tmp, 1);
@@ -734,6 +742,7 @@ int rtlsdr_set_sample_freq_correction(rtlsdr_dev_t *dev, int ppm)
int rtlsdr_set_xtal_freq(rtlsdr_dev_t *dev, uint32_t rtl_freq, uint32_t tuner_freq)
{
int r = 0;
rtlsdr_set_i2c_repeater(dev, 0);
if (!dev)
return -1;
@@ -831,6 +840,7 @@ int rtlsdr_write_eeprom(rtlsdr_dev_t *dev, uint8_t *data, uint8_t offset, uint16
int r = 0;
int i;
uint8_t cmd[2];
rtlsdr_set_i2c_repeater(dev, 0);
if (!dev)
return -1;
@@ -868,6 +878,7 @@ int rtlsdr_read_eeprom(rtlsdr_dev_t *dev, uint8_t *data, uint8_t offset, uint16_
{
int r = 0;
int i;
rtlsdr_set_i2c_repeater(dev, 0);
if (!dev)
return -1;
@@ -897,11 +908,11 @@ int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq)
return -1;
if (dev->direct_sampling) {
rtlsdr_set_i2c_repeater(dev, 0);
r = rtlsdr_set_if_freq(dev, freq);
} else if (dev->tuner && dev->tuner->set_freq) {
rtlsdr_set_i2c_repeater(dev, 1);
r = dev->tuner->set_freq(dev, freq - dev->offs_freq);
rtlsdr_set_i2c_repeater(dev, 0);
}
if (!r)
@@ -923,6 +934,7 @@ uint32_t rtlsdr_get_center_freq(rtlsdr_dev_t *dev)
int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm)
{
int r = 0;
rtlsdr_set_i2c_repeater(dev, 0);
if (!dev)
return -1;
@@ -1025,7 +1037,6 @@ int rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain)
if (dev->tuner->set_gain) {
rtlsdr_set_i2c_repeater(dev, 1);
r = dev->tuner->set_gain((void *)dev, gain);
rtlsdr_set_i2c_repeater(dev, 0);
}
if (!r)
@@ -1054,7 +1065,6 @@ int rtlsdr_set_tuner_if_gain(rtlsdr_dev_t *dev, int stage, int gain)
if (dev->tuner->set_if_gain) {
rtlsdr_set_i2c_repeater(dev, 1);
r = dev->tuner->set_if_gain(dev, stage, gain);
rtlsdr_set_i2c_repeater(dev, 0);
}
return r;
@@ -1070,7 +1080,6 @@ int rtlsdr_set_tuner_gain_mode(rtlsdr_dev_t *dev, int mode)
if (dev->tuner->set_gain_mode) {
rtlsdr_set_i2c_repeater(dev, 1);
r = dev->tuner->set_gain_mode((void *)dev, mode);
rtlsdr_set_i2c_repeater(dev, 0);
}
return r;
@@ -1085,6 +1094,7 @@ int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate)
if (!dev)
return -1;
rtlsdr_set_i2c_repeater(dev, 0);
/* check if the rate is supported by the resampler */
if ((samp_rate <= 225000) || (samp_rate > 3200000) ||
@@ -1140,6 +1150,7 @@ int rtlsdr_set_testmode(rtlsdr_dev_t *dev, int on)
{
if (!dev)
return -1;
rtlsdr_set_i2c_repeater(dev, 0);
return rtlsdr_demod_write_reg(dev, 0, 0x19, on ? 0x03 : 0x05, 1);
}
@@ -1148,6 +1159,7 @@ int rtlsdr_set_agc_mode(rtlsdr_dev_t *dev, int on)
{
if (!dev)
return -1;
rtlsdr_set_i2c_repeater(dev, 0);
return rtlsdr_demod_write_reg(dev, 0, 0x19, on ? 0x25 : 0x05, 1);
}
@@ -1189,6 +1201,7 @@ int rtlsdr_set_direct_sampling(rtlsdr_dev_t *dev, int on)
rtlsdr_set_i2c_repeater(dev, 0);
}
}
rtlsdr_set_i2c_repeater(dev, 0);
/* common to all direct modes */
if (on) {
@@ -1256,6 +1269,7 @@ int rtlsdr_get_direct_sampling(rtlsdr_dev_t *dev)
int rtlsdr_set_offset_tuning(rtlsdr_dev_t *dev, int on)
{
int r = 0;
rtlsdr_set_i2c_repeater(dev, 0);
if (!dev)
return -1;
@@ -1624,6 +1638,7 @@ int rtlsdr_open(rtlsdr_dev_t **out_dev, uint32_t index)
}
if (dev->tuner->init) {
rtlsdr_set_i2c_repeater(dev, 1);
r = dev->tuner->init(dev);
dev->tuner_initialized = 1;
}
@@ -1648,6 +1663,7 @@ int rtlsdr_close(rtlsdr_dev_t *dev)
{
if (!dev)
return -1;
rtlsdr_set_i2c_repeater(dev, 0);
if(!dev->dev_lost) {
/* block until all async operations have been completed (if any) */
@@ -1686,6 +1702,7 @@ int rtlsdr_reset_buffer(rtlsdr_dev_t *dev)
{
if (!dev)
return -1;
rtlsdr_set_i2c_repeater(dev, 0);
rtlsdr_write_reg(dev, USBB, USB_EPA_CTL, 0x1002, 2);
rtlsdr_write_reg(dev, USBB, USB_EPA_CTL, 0x0000, 2);

0 comments on commit d59b178

Please sign in to comment.