adjust_gain.c 6.1 KB
Newer Older
ghaddab's avatar
ghaddab committed
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface 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 3 of the License, or
    (at your option) any later version.


    OpenAirInterface 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
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19 20 21 22 23 24
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
ghaddab's avatar
ghaddab committed
27 28

 *******************************************************************************/
29 30 31 32 33 34 35 36 37 38 39 40
#include "PHY/types.h"
#include "PHY/defs.h"
#include "PHY/extern.h"
#include "MAC_INTERFACE/defs.h"
#include "MAC_INTERFACE/extern.h"

#ifdef EXMIMO
#include "openair0_lib.h"
extern int card;
#endif

void
41 42
phy_adjust_gain (PHY_VARS_UE *phy_vars_ue, uint8_t eNB_id)
{
43

44
  uint16_t rx_power_fil_dB;
45 46
#ifdef EXMIMO
  exmimo_config_t *p_exmimo_config = openair0_exmimo_pci[card].exmimo_config_ptr;
47
  uint16_t i;
48
#endif
49
  int rssi;
50

51
  rssi = dB_fixed(phy_vars_ue->PHY_measurements.rssi);
52

53
  if (rssi>0) rx_power_fil_dB = rssi;
54
  else rx_power_fil_dB = phy_vars_ue->PHY_measurements.rx_power_avg_dB[eNB_id];
55

56
  printf("Gain control: rssi %d (%d,%d)\n",
57 58 59 60
         rssi,
         phy_vars_ue->PHY_measurements.rssi,
         phy_vars_ue->PHY_measurements.rx_power_avg_dB[eNB_id]
        );
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  // Gain control with hysterisis
  // Adjust gain in phy_vars_ue->rx_vars[0].rx_total_gain_dB

  if (rx_power_fil_dB < TARGET_RX_POWER - 5) //&& (phy_vars_ue->rx_total_gain_dB < MAX_RF_GAIN) )
    phy_vars_ue->rx_total_gain_dB+=5;
  else if (rx_power_fil_dB > TARGET_RX_POWER + 5) //&& (phy_vars_ue->rx_total_gain_dB > MIN_RF_GAIN) )
    phy_vars_ue->rx_total_gain_dB-=5;

  if (phy_vars_ue->rx_total_gain_dB>MAX_RF_GAIN) {
    /*
    if ((openair_daq_vars.rx_rf_mode==0) && (openair_daq_vars.mode == openair_NOT_SYNCHED)) {
      openair_daq_vars.rx_rf_mode=1;
      phy_vars_ue->rx_total_gain_dB = max(MIN_RF_GAIN,MAX_RF_GAIN-25);
    }
    else {
    */
    phy_vars_ue->rx_total_gain_dB = MAX_RF_GAIN;
79
  } else if (phy_vars_ue->rx_total_gain_dB<MIN_RF_GAIN) {
80 81 82 83 84 85 86 87 88 89
    /*
    if ((openair_daq_vars.rx_rf_mode==1) && (openair_daq_vars.mode == openair_NOT_SYNCHED)) {
      openair_daq_vars.rx_rf_mode=0;
      phy_vars_ue->rx_total_gain_dB = min(MAX_RF_GAIN,MIN_RF_GAIN+25);
    }
    else {
    */
    phy_vars_ue->rx_total_gain_dB = MIN_RF_GAIN;
  }

90
  LOG_D(PHY,"Gain control: rx_total_gain_dB = %d (max %d,rxpf %d)\n",phy_vars_ue->rx_total_gain_dB,MAX_RF_GAIN,rx_power_fil_dB);
91 92 93

#ifdef EXMIMO

94 95
  if (phy_vars_ue->rx_total_gain_dB>phy_vars_ue->rx_gain_max[0]) {
    phy_vars_ue->rx_total_gain_dB = phy_vars_ue->rx_gain_max[0];
96 97

    for (i=0; i<phy_vars_ue->lte_frame_parms.nb_antennas_rx; i++) {
98 99
      p_exmimo_config->rf.rx_gain[i][0] = 30;
    }
100 101

  } else if (phy_vars_ue->rx_total_gain_dB<(phy_vars_ue->rx_gain_max[0]-30)) {
102 103
    // for the moment we stay in max gain mode
    phy_vars_ue->rx_total_gain_dB = phy_vars_ue->rx_gain_max[0] - 30;
104 105

    for (i=0; i<phy_vars_ue->lte_frame_parms.nb_antennas_rx; i++) {
106 107
      p_exmimo_config->rf.rx_gain[i][0] = 0;
    }
108

109 110 111 112 113
    /*
      phy_vars_ue->rx_gain_mode[0] = byp;
      phy_vars_ue->rx_gain_mode[1] = byp;
      exmimo_pci_interface->rf.rf_mode0 = 22991; //bypass
      exmimo_pci_interface->rf.rf_mode1 = 22991; //bypass
114

115 116 117
      if (phy_vars_ue->rx_total_gain_dB<(phy_vars_ue->rx_gain_byp[0]-50)) {
      exmimo_pci_interface->rf.rx_gain00 = 0;
      exmimo_pci_interface->rf.rx_gain10 = 0;
118
      }
119
    */
120
  } else {
121

122
    for (i=0; i<phy_vars_ue->lte_frame_parms.nb_antennas_rx; i++) {
123 124 125
      p_exmimo_config->rf.rx_gain[i][0] =  30 - phy_vars_ue->rx_gain_max[0] + phy_vars_ue->rx_total_gain_dB;
    }
  }
126 127 128 129 130 131 132 133 134 135 136 137 138 139

  /*
    break;
  case med_gain:
  case byp_gain:
      if (phy_vars_ue->rx_total_gain_dB>phy_vars_ue->rx_gain_byp[0]) {
          phy_vars_ue->rx_gain_mode[0]   = max_gain;
          phy_vars_ue->rx_gain_mode[1]   = max_gain;
          exmimo_pci_interface->rf.rf_mode0 = 55759; //max gain
          exmimo_pci_interface->rf.rf_mode1 = 55759; //max gain

          if (phy_vars_ue->rx_total_gain_dB>phy_vars_ue->rx_gain_max[0]) {
              exmimo_pci_interface->rf.rx_gain00 = 50;
              exmimo_pci_interface->rf.rx_gain10 = 50;
140 141
          }
          else {
142 143
              exmimo_pci_interface->rf.rx_gain00 = 50 - phy_vars_ue->rx_gain_max[0] + phy_vars_ue->rx_total_gain_dB;
              exmimo_pci_interface->rf.rx_gain10 = 50 - phy_vars_ue->rx_gain_max[1] + phy_vars_ue->rx_total_gain_dB;
144 145
          }
      }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
      else if (phy_vars_ue->rx_total_gain_dB<(phy_vars_ue->rx_gain_byp[0]-50)) {
          exmimo_pci_interface->rf.rx_gain00 = 0;
          exmimo_pci_interface->rf.rx_gain10 = 0;
      }
      else {
          exmimo_pci_interface->rf.rx_gain00 = 50 - phy_vars_ue->rx_gain_byp[0] + phy_vars_ue->rx_total_gain_dB;
          exmimo_pci_interface->rf.rx_gain10 = 50 - phy_vars_ue->rx_gain_byp[1] + phy_vars_ue->rx_total_gain_dB;
      }
      break;
  default:
      exmimo_pci_interface->rf.rx_gain00 = 50;
      exmimo_pci_interface->rf.rx_gain10 = 50;
      break;
  }
      */
161 162 163
#endif

#ifdef DEBUG_PHY
164 165 166 167 168
  /*  if ((phy_vars_ue->frame%100==0) || (phy_vars_ue->frame < 10))
  msg("[PHY][ADJUST_GAIN] frame %d,  rx_power = %d, rx_power_fil = %d, rx_power_fil_dB = %d, coef=%d, ncoef=%d, rx_total_gain_dB = %d (%d,%d,%d)\n",
    phy_vars_ue->frame,rx_power,rx_power_fil,rx_power_fil_dB,coef,ncoef,phy_vars_ue->rx_total_gain_dB,
  TARGET_RX_POWER,MAX_RF_GAIN,MIN_RF_GAIN);
  */
169
#endif //DEBUG_PHY
170

171 172 173
}