nr_ulsch_ue.c 14.1 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"
Khalid Ahmed's avatar
Khalid Ahmed committed
37
#include "common/utils/assertions.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
38
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
39
#include "PHY/defs_nr_common.h"
40 41 42 43 44 45 46 47 48
#include "PHY/TOOLS/tools_defs.h"

//#define DEBUG_SCFDMA

#ifdef DEBUG_SCFDMA

  FILE *debug_scfdma;

#endif
Khalid Ahmed's avatar
Khalid Ahmed committed
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
int generate_ue_ulsch_params(PHY_VARS_NR_UE *UE,
                             temp_nfapi_nr_ul_config_pdcch_parameters_rel15_t *nr_ul_pdcch_params,
                             uint8_t thread_id,
                             int gNB_id,
                             unsigned char harq_pid,
                             unsigned char *test_input){

  int N_PRB_oh, N_RE_prime, cwd_idx, length_dmrs, Nid_cell;
  uint16_t n_rnti;

  NR_UE_ULSCH_t *ulsch_ue;
  NR_UL_UE_HARQ_t *harq_process_ul_ue;

  length_dmrs = 1;
  n_rnti = 0x1234;
  Nid_cell = 0;

  for (cwd_idx = 0; cwd_idx < nr_ul_pdcch_params->nb_codewords; cwd_idx++) {

    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;
    ulsch_ue->Nsc_pusch   = nr_ul_pdcch_params->nb_rb*NR_NB_SC_PER_RB;
    ulsch_ue->Nsymb_pusch = nr_ul_pdcch_params->n_symb;
    ulsch_ue->nb_re_dmrs  = UE->dmrs_UplinkConfig.pusch_maxLength*(UE->dmrs_UplinkConfig.pusch_dmrs_type == pusch_dmrs_type1)?6:4;


    N_PRB_oh   = 0; // higher layer (RRC) parameter xOverhead in PUSCH-ServingCellConfig
    N_RE_prime = NR_NB_SC_PER_RB*nr_ul_pdcch_params->n_symb - ulsch_ue->nb_re_dmrs - N_PRB_oh;

    if (harq_process_ul_ue) {

      harq_process_ul_ue->mcs                = nr_ul_pdcch_params->Imcs;
      harq_process_ul_ue->Nl                 = nr_ul_pdcch_params->Nl;
      harq_process_ul_ue->nb_rb              = nr_ul_pdcch_params->nb_rb;
      harq_process_ul_ue->first_rb           = nr_ul_pdcch_params->first_rb;
      harq_process_ul_ue->number_of_symbols  = nr_ul_pdcch_params->n_symb;
      harq_process_ul_ue->num_of_mod_symbols = N_RE_prime*nr_ul_pdcch_params->nb_rb*nr_ul_pdcch_params->nb_codewords;
      harq_process_ul_ue->rvidx              = nr_ul_pdcch_params->rvidx;
      harq_process_ul_ue->a                  = test_input;
      harq_process_ul_ue->TBS                = nr_compute_tbs(nr_ul_pdcch_params->Imcs,
                                                              nr_ul_pdcch_params->nb_rb,
                                                              nr_ul_pdcch_params->n_symb,
                                                              ulsch_ue->nb_re_dmrs,
                                                              length_dmrs,
                                                              nr_ul_pdcch_params->Nl);

    }

  }

  return 0;
}

Khalid Ahmed's avatar
Khalid Ahmed committed
107 108 109 110 111 112 113 114 115


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
116
  uint32_t x1, x2, s=0, temp_out;
Khalid Ahmed's avatar
Khalid Ahmed committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130

  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
131 132 133 134 135 136 137 138 139 140
    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
141 142 143 144 145
    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);
  }

146
}
147 148 149 150 151


void pusch_transform_precoding(NR_UE_ULSCH_t *ulsch, NR_DL_FRAME_PARMS *frame_parms, int harq_pid){

  NR_UL_UE_HARQ_t *harq_process;
152
  int x[8192]__attribute__ ((aligned (32))); // 8192 is the maximum number of fft bins
153 154 155 156 157 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
  uint32_t *dmod;
  int sc, pusch_symb, pusch_sc;
  int symb, k, l, num_mod_symb;

  harq_process = ulsch->harq_processes[harq_pid];

#ifdef DEBUG_SCFDMA
  debug_scfdma = fopen("debug_scfdma.txt","w");
#endif

  dmod = ulsch->d_mod;
  pusch_symb = ulsch->Nsymb_pusch;
  pusch_sc = ulsch->Nsc_pusch;
  num_mod_symb = harq_process->num_of_mod_symbols;

  void (*dft)(int16_t *,int16_t *, int);

  switch (frame_parms->ofdm_symbol_size) {
  case 128:
    dft = dft128;
    break;

  case 256:
    dft = dft256;
    break;

  case 512:
    dft = dft512;
    break;

  case 1024:
    dft = dft1024;
    break;

  case 1536:
    dft = dft1536;
    break;

  case 2048:
    dft = dft2048;
    break;

  case 4096:
    dft = dft4096;
    break;

  case 8192:
    dft = dft8192;
    break;

  default:
    dft = dft512;
    break;
  }

  k = 0;
  symb = 0;

  for(l = 0; l < pusch_symb; l++){

    for (sc = 0; sc < pusch_sc; sc++){

215 216
      ((int16_t *)x)[sc*2] = (symb<num_mod_symb)?(AMP*((int16_t *)dmod)[symb*2])>>15:0;
      ((int16_t *)x)[sc*2 + 1] = (symb<num_mod_symb)?(AMP*((int16_t *)dmod)[symb*2 + 1])>>15:0;
217 218

  #ifdef DEBUG_SCFDMA
219 220
      fprintf(debug_scfdma, "x[%d] = %d\n", symb*2, ((int16_t *)x)[sc*2] );
      fprintf(debug_scfdma, "x[%d] = %d\n", symb*2 + 1, ((int16_t *)x)[sc*2 + 1] );
221 222 223 224 225 226 227
  #endif

      symb++;

    }


228
    dft((int16_t *)x, (int16_t *)&ulsch->y[l*pusch_sc], 1);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

  }

#ifdef DEBUG_SCFDMA

  for (symb = 0; symb < num_mod_symb; symb++)
  {
    fprintf(debug_scfdma, "ulsch->y[%d] = %d\n", symb*2, ((int16_t *)ulsch->y)[symb*2] );
    fprintf(debug_scfdma, "ulsch->y[%d] = %d\n", symb*2 + 1, ((int16_t *)ulsch->y)[symb*2 + 1] );
  }

  fclose(debug_scfdma);
#endif

}
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 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 353 354 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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473



uint8_t nr_ue_ulsch_procedures(PHY_VARS_NR_UE *UE,
                               unsigned char harq_pid,
                               uint8_t slot,
                               uint8_t thread_id,
                               int eNB_id) {

  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);

    pusch_transform_precoding(ulsch_ue, frame_parms, harq_pid);

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

    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);


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

  /////////////////////////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);
    l_prime[0] = 0; // single symbol ap 0
    uint8_t dmrs_symbol = l0+l_prime[0]; // Assuming dmrs-AdditionalPosition = 0

    uint8_t k_prime=0, l;
    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 {

          ((int16_t*)txdataF[ap])[(sample_offsetF)<<1] = (AMP * tx_layers[ap][m<<1]) >> 15;
          ((int16_t*)txdataF[ap])[((sample_offsetF)<<1) + 1] = (AMP * tx_layers[ap][(m<<1) + 1]) >> 15;

          #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;
}