From ab50557d890bd2bc86faaaaa8ab2b29253d6a82f Mon Sep 17 00:00:00 2001 From: hayati ayguen Date: Tue, 30 Jul 2019 23:59:12 +0200 Subject: [PATCH] added agc_variant = -2 for rtlsdr_set_tuner_agc_mode() Signed-off-by: hayati ayguen --- include/rtl-sdr.h | 3 ++- src/tuner_r82xx.c | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/include/rtl-sdr.h b/include/rtl-sdr.h index 6adb993..b08cb11 100755 --- a/include/rtl-sdr.h +++ b/include/rtl-sdr.h @@ -308,7 +308,8 @@ RTLSDR_API int rtlsdr_set_tuner_gain_mode(rtlsdr_dev_t *dev, int manual); * Automatic gain mode must be enabled for the gain setter function to work. * * \param dev the device handle given by rtlsdr_open() - * \param agc variant: 0 for automatic LNA/Mixer; fixed VGA + * \param agc variant: 0 for automatic LNA/Mixer and fixed VGA (=default) + * -2 for automatic LNA/Mixer and automatic VGA * -1 for LNA/Mixer = last value from prev rtlsdr_set_tuner_gain; VGA = auto * >0: gain := agc_variant for rtlsdr_set_tuner_gain() for LNA/Mixer; VGA = auto * \return 0 on success diff --git a/src/tuner_r82xx.c b/src/tuner_r82xx.c index 015084e..c77c37c 100644 --- a/src/tuner_r82xx.c +++ b/src/tuner_r82xx.c @@ -1074,6 +1074,13 @@ int r82xx_set_gain(struct r82xx_priv *priv, int set_manual_gain, int gain, vga_gain = priv->last_VGA_value | 0x10; /* should activate AGC */ extended_mode = 1; } + else if ( priv->agc_mode == -2 ) { + /* LNA/Mixer = Auto; VGA = auto */ + lna_gain = priv->last_LNA_value; + mixer_gain = priv->last_Mixer_value; + vga_gain = priv->last_VGA_value | 0x10; /* should activate AGC */ + extended_mode = 0; + } else if ( priv->agc_mode > 0 ) { /* LNA/Mixer = from rtlsdr_set_tuner_gain(tunerAgcMode); VGA = auto */ r82xx_get_gain_index(priv->agc_mode, &lna_gain, &mixer_gain); @@ -1138,16 +1145,27 @@ int r82xx_set_gain(struct r82xx_priv *priv, int set_manual_gain, int gain, if (rc < 0) return rc; - /* set fixed VGA gain for now (26.5 dB == -12 dB + 0x0b * 3.5 dB) */ - /* rc = r82xx_write_reg_mask(priv, 0x0c, 0x0b, 0x9f); */ - /* set VGA gain auto */ - rc = r82xx_write_reg_mask(priv, 0x0c, 0x1b, 0x9f); - if (rc < 0) - return rc; - - priv->last_AGC_state = new_agc_state; - if ( rtl_vga_control ) - *rtl_vga_control = 1; + if ( priv->agc_mode == -2 ) { + /* Set VGA = auto */ + rc = r82xx_write_reg_mask(priv, 0x0c, vga_gain, 0x9f); + if (rc < 0) + return rc; + priv->last_VGA_value = vga_gain & 0x0f; + priv->last_AGC_state = new_agc_state; + if ( (vga_gain & 0x10) && rtl_vga_control ) + *rtl_vga_control = 1; + } else { + /* set fixed VGA gain for now (26.5 dB == -12 dB + 0x0b * 3.5 dB) */ + /* rc = r82xx_write_reg_mask(priv, 0x0c, 0x0b, 0x9f); */ + /* set VGA gain auto */ + rc = r82xx_write_reg_mask(priv, 0x0c, 0x1b, 0x9f); + if (rc < 0) + return rc; + + priv->last_AGC_state = new_agc_state; + if ( rtl_vga_control ) + *rtl_vga_control = 0; + } } return 0;