nr_adjust_synch_ue.c 4.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

#include "PHY/types.h"
#include "PHY/defs_nr_UE.h"
#include "PHY/NR_UE_ESTIMATION/nr_estimation.h"
#include "PHY/impl_defs_top.h"

27
#include "executables/softmodem-common.h"
28 29 30 31 32 33 34 35 36
#include "common/utils/LOG/vcd_signal_dumper.h"

#define DEBUG_PHY

// Adjust location synchronization point to account for drift
// The adjustment is performed once per frame based on the
// last channel estimate of the receiver

void nr_adjust_synch_ue(NR_DL_FRAME_PARMS *frame_parms,
37 38 39 40 41 42
                        PHY_VARS_NR_UE *ue,
                        module_id_t gNB_id,
                        uint8_t frame,
                        uint8_t subframe,
                        unsigned char clear,
                        short coef)
43 44 45 46 47 48 49 50
{

  static int max_pos_fil = 0;
  static int count_max_pos_ok = 0;
  static int first_time = 1;
  int temp = 0, i, aa, max_val = 0, max_pos = 0;
  int diff;
  short Re,Im,ncoef;
Hongzhi Wang's avatar
Hongzhi Wang committed
51
  uint8_t sync_offset = 0;
52 53 54 55 56

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_ADJUST_SYNCH, VCD_FUNCTION_IN);

  ncoef = 32767 - coef;

laurent's avatar
laurent committed
57
  LOG_D(PHY,"AbsSubframe %d: rx_offset (before) = %d\n",subframe,ue->rx_offset);
58 59 60 61 62 63 64


  // we only use channel estimates from tx antenna 0 here
  for (i = 0; i < frame_parms->nb_prefix_samples; i++) {
    temp = 0;

    for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
cig's avatar
cig committed
65 66
      Re = ((int16_t*)ue->pbch_vars[gNB_id]->dl_ch_estimates_time[aa])[(i<<1)];
      Im = ((int16_t*)ue->pbch_vars[gNB_id]->dl_ch_estimates_time[aa])[1+(i<<1)];
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      temp += (Re*Re/2) + (Im*Im/2);
    }

    if (temp > max_val) {
      max_pos = i;
      max_val = temp;
    }
  }

  // filter position to reduce jitter
  if (clear == 1)
    max_pos_fil = max_pos;
  else
    max_pos_fil = ((max_pos_fil * coef) + (max_pos * ncoef)) >> 15;

  // do not filter to have proactive timing adjustment
Hongzhi's avatar
Hongzhi committed
83
  //max_pos_fil = max_pos;
84 85 86

      diff = max_pos_fil - (frame_parms->nb_prefix_samples>>3);

Hongzhi Wang's avatar
Hongzhi Wang committed
87 88 89 90 91 92
      if (frame_parms->freq_range==nr_FR2) 
		sync_offset = 2;
      else
		sync_offset = 0;
	
      if ( abs(diff) < (SYNCH_HYST+sync_offset) )
93 94 95 96 97 98 99 100 101
          ue->rx_offset = 0;
      else
          ue->rx_offset = diff;

      if(abs(diff)<5)
          count_max_pos_ok ++;
      else
          count_max_pos_ok = 0;
          
Hongzhi's avatar
Hongzhi committed
102
      //printf("adjust sync count_max_pos_ok = %d\n",count_max_pos_ok);
103 104 105 106 107

      if(count_max_pos_ok > 10 && first_time == 1)
      {
          first_time = 0;
          ue->time_sync_cell = 1;
108
          if (get_softmodem_params()->do_ra) {
109 110
              LOG_I(PHY,"[UE%d] Sending synch status to higher layers\n",ue->Mod_id);
              //mac_resynch();
111
              //dl_phy_sync_success(ue->Mod_id,frame,0,1);//ue->common_vars.eNb_id);
112
              ue->UE_mode[0] = PRACH;
113 114
              ue->prach_resources[gNB_id]->sync_frame = frame;
              ue->prach_resources[gNB_id]->init_msg1 = 0;
115
          } else {
116 117 118 119
              ue->UE_mode[0] = PUSCH;
          }
      }

cig's avatar
cig committed
120 121
      if (ue->rx_offset < 0)
        ue->rx_offset += frame_parms->samples_per_frame;
122

cig's avatar
cig committed
123 124
      if (ue->rx_offset >= frame_parms->samples_per_frame)
        ue->rx_offset -= frame_parms->samples_per_frame;
125 126 127 128



      #ifdef DEBUG_PHY
129
      LOG_D(PHY,"AbsSubframe %d: diff =%i rx_offset (final) = %i : clear %d,max_pos = %d,max_pos_fil = %d (peak %d) max_val %d target_pos %d \n",
130 131 132 133 134 135 136 137 138 139 140
              subframe,
              diff,
              ue->rx_offset,
              clear,
              max_pos,
              max_pos_fil,
              temp,max_val,
              (frame_parms->nb_prefix_samples>>3));
      #endif //DEBUG_PHY

      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_ADJUST_SYNCH, VCD_FUNCTION_OUT);
Hongzhi Wang's avatar
Hongzhi Wang committed
141

142
}