Skip to content

Commit

Permalink
lib: cache i2c repeater
Browse files Browse the repository at this point in the history
  • Loading branch information
keenerd committed Aug 3, 2014
1 parent a0b6131 commit d59b178
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/librtlsdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 */
Expand All @@ -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))
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) ||
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) */
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d59b178

Please sign in to comment.