nr_ulsch_ue.c 13.2 KB
Newer Older
Khalid Ahmed's avatar
Khalid Ahmed committed
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 27 28 29 30 31 32
/*
 * 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
 */

/*! \file PHY/NR_UE_TRANSPORT/nr_ulsch.c
* \brief Top-level routines for transmission of the PUSCH TS 38.211 v 15.4.0
* \author Khalid Ahmed
* \date 2019
* \version 0.1
* \company Fraunhofer IIS
* \email: khalid.ahmed@iis.fraunhofer.de
* \note
* \warning
*/
#include <stdint.h>
33
#include "PHY/NR_REFSIG/dmrs_nr.h"
34
#include "PHY/NR_UE_TRANSPORT/nr_transport_ue.h"
35 36
#include "PHY/NR_UE_TRANSPORT/nr_transport_proto_ue.h"
#include "PHY/MODULATION/nr_modulation.h"
37
#include "PHY/MODULATION/modulation_common.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
38
#include "common/utils/assertions.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
39
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
40
#include "PHY/NR_TRANSPORT/nr_sch_dmrs.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
41
#include "PHY/defs_nr_common.h"
42 43 44 45
#include "PHY/TOOLS/tools_defs.h"

//#define DEBUG_SCFDMA

Khalid Ahmed's avatar
Khalid Ahmed committed
46

47 48 49
int generate_ue_ulsch_params(PHY_VARS_NR_UE *UE,
                             uint8_t thread_id,
                             int gNB_id,
50
                             unsigned char harq_pid){
51 52

  int N_PRB_oh, N_RE_prime, cwd_idx, length_dmrs, Nid_cell;
53
  int nb_rb, Nsymb_pusch, first_rb, nb_codewords,mcs,rvidx;
54 55 56 57 58
  uint16_t n_rnti;

  NR_UE_ULSCH_t *ulsch_ue;
  NR_UL_UE_HARQ_t *harq_process_ul_ue;

Florian Kaltenberger's avatar
Florian Kaltenberger committed
59 60
  LOG_W(PHY,"This function should not be used. Use FAPI interfaces instead\n");
  
61
  //--------------------------Temporary configuration-----------------------------//
62 63 64
  length_dmrs = 1;
  n_rnti = 0x1234;
  Nid_cell = 0;
65
  nb_rb = 50;
Khalid Ahmed's avatar
Khalid Ahmed committed
66
  first_rb = 30;
67
  Nsymb_pusch = 12;
68 69 70
  nb_codewords = 1;
  mcs = 9;
  rvidx = 0;
71
  //------------------------------------------------------------------------------//
72

73
  for (cwd_idx = 0; cwd_idx < nb_codewords; cwd_idx++) {
74 75 76 77 78 79 80

    ulsch_ue = UE->ulsch[thread_id][gNB_id][cwd_idx];
    harq_process_ul_ue = ulsch_ue->harq_processes[harq_pid];

    ulsch_ue->length_dmrs = length_dmrs;
    ulsch_ue->rnti        = n_rnti;
    ulsch_ue->Nid_cell    = Nid_cell;
81 82
    ulsch_ue->Nsc_pusch   = nb_rb*NR_NB_SC_PER_RB;
    ulsch_ue->Nsymb_pusch = Nsymb_pusch;
83 84 85
    ulsch_ue->nb_re_dmrs  = UE->dmrs_UplinkConfig.pusch_maxLength*(UE->dmrs_UplinkConfig.pusch_dmrs_type == pusch_dmrs_type1)?6:4;


86 87
    N_PRB_oh = 0; // higher layer (RRC) parameter xOverhead in PUSCH-ServingCellConfig
    N_RE_prime = NR_NB_SC_PER_RB*Nsymb_pusch - ulsch_ue->nb_re_dmrs - N_PRB_oh;
88 89 90

    if (harq_process_ul_ue) {

91 92
      harq_process_ul_ue->mcs                = mcs;
      harq_process_ul_ue->Nl                 = nb_codewords;
93 94 95 96
      harq_process_ul_ue->nb_rb              = nb_rb;
      harq_process_ul_ue->first_rb           = first_rb;
      harq_process_ul_ue->number_of_symbols  = Nsymb_pusch;
      harq_process_ul_ue->num_of_mod_symbols = N_RE_prime*nb_rb*nb_codewords;
97 98
      harq_process_ul_ue->rvidx              = rvidx;
      harq_process_ul_ue->TBS                = nr_compute_tbs(harq_process_ul_ue->mcs,
99 100
                                                              nb_rb,
                                                              Nsymb_pusch,
101 102
                                                              ulsch_ue->nb_re_dmrs,
                                                              length_dmrs,
103
                                                              harq_process_ul_ue->Nl);
104 105 106 107 108 109 110 111

    }

  }

  return 0;
}

Khalid Ahmed's avatar
Khalid Ahmed committed
112 113 114 115 116 117 118 119 120


void nr_pusch_codeword_scrambling(uint8_t *in,
                         uint16_t size,
                         uint32_t Nid,
                         uint32_t n_RNTI,
                         uint32_t* out) {

  uint8_t reset, b_idx;
Khalid Ahmed's avatar
Khalid Ahmed committed
121
  uint32_t x1, x2, s=0, temp_out;
Khalid Ahmed's avatar
Khalid Ahmed committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135

  reset = 1;
  x2 = (n_RNTI<<15) + Nid;

  for (int i=0; i<size; i++) {
    b_idx = i&0x1f;
    if (b_idx==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
      if (i)
        out++;
    }
    if (in[i]==NR_PUSCH_x)
      *out ^= 1<<b_idx;
Khalid Ahmed's avatar
Khalid Ahmed committed
136 137 138 139 140 141 142 143 144 145
    else if (in[i]==NR_PUSCH_y){
      if (b_idx!=0)
        *out ^= (*out & (1<<(b_idx-1)))<<1;
      else{

        temp_out = *(out-1);
        *out ^= temp_out>>31;

      }
    }
Khalid Ahmed's avatar
Khalid Ahmed committed
146 147 148 149 150
    else
      *out ^= (((in[i])&1) ^ ((s>>b_idx)&1))<<b_idx;
    //printf("i %d b_idx %d in %d s 0x%08x out 0x%08x\n", i, b_idx, in[i], s, *out);
  }

151
}
152

153 154 155 156
uint8_t nr_ue_ulsch_procedures(PHY_VARS_NR_UE *UE,
                               unsigned char harq_pid,
                               uint8_t slot,
                               uint8_t thread_id,
157
                               int eNB_id) {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

  unsigned int available_bits;
  uint8_t mod_order, cwd_index, num_of_codewords;
  uint32_t scrambled_output[NR_MAX_NB_CODEWORDS][NR_MAX_PDSCH_ENCODED_LENGTH>>5];
  int32_t *mod_symbols[MAX_NUM_NR_RE];
  uint32_t ***pusch_dmrs;
  int16_t **tx_layers;
  int32_t **txdataF;
  uint16_t start_sc, start_rb;
  int8_t Wf[2], Wt[2], l0, l_prime[2], delta;
  uint16_t n_dmrs;
  uint8_t dmrs_type;
  uint8_t mapping_type;
  int ap, start_symbol, i;
  int sample_offsetF;

  NR_UE_ULSCH_t *ulsch_ue;
  NR_UL_UE_HARQ_t *harq_process_ul_ue;
  NR_DL_FRAME_PARMS *frame_parms = &UE->frame_parms;
  NR_UE_PUSCH *pusch_ue = UE->pusch_vars[thread_id][eNB_id];

  num_of_codewords = 1; // tmp assumption

  for (cwd_index = 0;cwd_index < num_of_codewords; cwd_index++) {

    ulsch_ue = UE->ulsch[thread_id][eNB_id][cwd_index];
    harq_process_ul_ue = ulsch_ue->harq_processes[harq_pid];

    /////////////////////////ULSCH coding/////////////////////////
    ///////////

    nr_ulsch_encoding(ulsch_ue, frame_parms, harq_pid);

    ///////////
    ////////////////////////////////////////////////////////////////////

    /////////////////////////ULSCH scrambling/////////////////////////
    ///////////

    mod_order      = nr_get_Qm(harq_process_ul_ue->mcs, 1);

    available_bits = nr_get_G(harq_process_ul_ue->nb_rb,
                              ulsch_ue->Nsymb_pusch,
                              ulsch_ue->nb_re_dmrs,
                              ulsch_ue->length_dmrs,
                              mod_order,
                              1);

    memset(scrambled_output[cwd_index], 0, ((available_bits>>5)+1)*sizeof(uint32_t));

    nr_pusch_codeword_scrambling(ulsch_ue->g,
                                 available_bits,
                                 ulsch_ue->Nid_cell,
                                 ulsch_ue->rnti,
                                 scrambled_output[cwd_index]); // assume one codeword for the moment


    /////////////
    //////////////////////////////////////////////////////////////////////////

    /////////////////////////ULSCH modulation/////////////////////////
    ///////////

    nr_modulation(scrambled_output[cwd_index], // assume one codeword for the moment
                  available_bits,
                  mod_order,
                  (int16_t *)ulsch_ue->d_mod);

226
    // pusch_transform_precoding(ulsch_ue, frame_parms, harq_pid);
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

    ///////////
    ////////////////////////////////////////////////////////////////////////

    mod_symbols[cwd_index] = (int32_t *)malloc16((NR_MAX_PUSCH_ENCODED_LENGTH)*sizeof(int32_t*));

    memcpy(mod_symbols[cwd_index],ulsch_ue->d_mod,(available_bits/mod_order)*sizeof(int32_t));

  }

  start_symbol = 14 - ulsch_ue->Nsymb_pusch;

  /////////////////////////DMRS Modulation/////////////////////////
  ///////////

  pusch_dmrs = UE->nr_gold_pusch_dmrs[slot];
  n_dmrs = (harq_process_ul_ue->nb_rb*ulsch_ue->nb_re_dmrs);
  int16_t mod_dmrs[n_dmrs<<1];
  dmrs_type = UE->dmrs_UplinkConfig.pusch_dmrs_type;
  mapping_type = UE->pusch_config.pusch_TimeDomainResourceAllocation[0]->mappingType;

  l0 = get_l0_ul(mapping_type, 2);
  nr_modulation(pusch_dmrs[l0][0], n_dmrs*2, DMRS_MOD_ORDER, mod_dmrs); // currently only codeword 0 is modulated. Qm = 2 as DMRS is QPSK modulated


  ///////////
  ////////////////////////////////////////////////////////////////////////

  /////////////////////////ULSCH layer mapping/////////////////////////
  ///////////

  tx_layers = (int16_t **)pusch_ue->txdataF_layers;

  nr_layer_mapping((int16_t **)mod_symbols,
                   harq_process_ul_ue->Nl,
                   available_bits/mod_order,
                   tx_layers);
264 265 266
  
  for (uint32_t i = 0; i < 2*available_bits/mod_order; i++)
      tx_layers[0][i] = (tx_layers[0][i] * AMP) >> 15;
267

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  ///////////
  ////////////////////////////////////////////////////////////////////////


  //////////////////////// ULSCH transform precoding ////////////////////////
  ///////////

  l_prime[0] = 0; // single symbol ap 0
  uint8_t dmrs_symbol = l0+l_prime[0], l; // Assuming dmrs-AdditionalPosition = 0

#ifdef NR_SC_FDMA
  uint32_t nb_re_pusch, nb_re_dmrs_per_rb;
  uint32_t y_offset = 0;

  for (l = start_symbol; l < start_symbol + ulsch_ue->Nsymb_pusch; l++) {

    if(l == dmrs_symbol)
      nb_re_dmrs_per_rb = ulsch_ue->nb_re_dmrs; // [hna] ulsch_ue->nb_re_dmrs = 6 in this configuration
    else
      nb_re_dmrs_per_rb = 0;
    
    nb_re_pusch = harq_process_ul_ue->nb_rb * (NR_NB_SC_PER_RB - nb_re_dmrs_per_rb);

    nr_dft(&ulsch_ue->y[y_offset], &((int32_t*)tx_layers[0])[y_offset], nb_re_pusch);

    y_offset = y_offset + nb_re_pusch;
  }
#else
  memcpy(ulsch_ue->y, tx_layers[0], (available_bits/mod_order)*sizeof(int32_t));
#endif
298 299 300 301

  ///////////
  ////////////////////////////////////////////////////////////////////////

302 303


304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  /////////////////////////ULSCH RE mapping/////////////////////////
  ///////////

  txdataF = UE->common_vars.txdataF;

  start_rb = harq_process_ul_ue->first_rb;
  start_sc = frame_parms->first_carrier_offset + start_rb*NR_NB_SC_PER_RB;

  if (start_sc >= frame_parms->ofdm_symbol_size)
    start_sc -= frame_parms->ofdm_symbol_size;

  for (ap=0; ap<harq_process_ul_ue->Nl; ap++) {

    // DMRS params for this ap
    get_Wt(Wt, ap, dmrs_type);
    get_Wf(Wf, ap, dmrs_type);
    delta = get_delta(ap, dmrs_type);
321
    
322

323
    uint8_t k_prime=0;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    uint16_t m=0, n=0, dmrs_idx=0, k=0;

    for (l=start_symbol; l<start_symbol+ulsch_ue->Nsymb_pusch; l++) {

      k = start_sc;

      for (i=0; i<harq_process_ul_ue->nb_rb*NR_NB_SC_PER_RB; i++) {

        sample_offsetF = l*frame_parms->ofdm_symbol_size + k;

        if ((l == dmrs_symbol) && (k == ((start_sc+get_dmrs_freq_idx_ul(n, k_prime, delta, dmrs_type))%(frame_parms->ofdm_symbol_size)))) {

          ((int16_t*)txdataF[ap])[(sample_offsetF)<<1] = (Wt[l_prime[0]]*Wf[k_prime]*AMP*mod_dmrs[dmrs_idx<<1]) >> 15;
          ((int16_t*)txdataF[ap])[((sample_offsetF)<<1) + 1] = (Wt[l_prime[0]]*Wf[k_prime]*AMP*mod_dmrs[(dmrs_idx<<1) + 1]) >> 15;

          #ifdef DEBUG_PUSCH_MAPPING
            printf("dmrs_idx %d\t l %d \t k %d \t k_prime %d \t n %d \t txdataF: %d %d\n",
            dmrs_idx, l, k, k_prime, n, ((int16_t*)txdataF[ap])[(sample_offsetF)<<1],
            ((int16_t*)txdataF[ap])[((sample_offsetF)<<1) + 1]);
          #endif

          dmrs_idx++;
          k_prime++;
          k_prime&=1;
          n+=(k_prime)?0:1;
        }

        else {

353 354
          ((int16_t*)txdataF[ap])[(sample_offsetF)<<1]       = ((int16_t *) ulsch_ue->y)[m<<1];
          ((int16_t*)txdataF[ap])[((sample_offsetF)<<1) + 1] = ((int16_t *) ulsch_ue->y)[(m<<1) + 1];
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

          #ifdef DEBUG_PUSCH_MAPPING
            printf("m %d\t l %d \t k %d \t txdataF: %d %d\n",
            m, l, k, ((int16_t*)txdataF[ap])[(sample_offsetF)<<1],
            ((int16_t*)txdataF[ap])[((sample_offsetF)<<1) + 1]);
          #endif

          m++;
        }

        if (++k >= frame_parms->ofdm_symbol_size)
          k -= frame_parms->ofdm_symbol_size;
      }
    }
  }

  ///////////
  ////////////////////////////////////////////////////////////////////////

  return 0;
}


uint8_t nr_ue_pusch_common_procedures(PHY_VARS_NR_UE *UE,
                                      uint8_t slot,
                                      uint8_t Nl,
                                      NR_DL_FRAME_PARMS *frame_parms) {

  int tx_offset, ap;
  int32_t **txdata;
  int32_t **txdataF;

  /////////////////////////IFFT///////////////////////
  ///////////

  tx_offset = slot*frame_parms->samples_per_slot;
  txdata = UE->common_vars.txdata;
  txdataF = UE->common_vars.txdataF;

  for (ap = 0; ap < Nl; ap++) {
      if (frame_parms->Ncp == 1) { // extended cyclic prefix
  PHY_ofdm_mod(txdataF[ap],
         &txdata[ap][tx_offset],
         frame_parms->ofdm_symbol_size,
         12,
         frame_parms->nb_prefix_samples,
         CYCLIC_PREFIX);
      } else { // normal cyclic prefix
  nr_normal_prefix_mod(txdataF[ap],
           &txdata[ap][tx_offset],
           14,
           frame_parms);
      }
    }

  ///////////
  ////////////////////////////////////////////////////
  return 0;
}