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"
37
#include "PHY/MODULATION/modulation_common.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
38
#include "common/utils/assertions.h"
39
#include "common/utils/LOG/vcd_signal_dumper.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
40
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
41
#include "PHY/NR_TRANSPORT/nr_sch_dmrs.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
42
#include "PHY/defs_nr_common.h"
43 44 45
#include "PHY/TOOLS/tools_defs.h"

//#define DEBUG_SCFDMA
46
//#define DEBUG_PUSCH_MAPPING
47

Khalid Ahmed's avatar
Khalid Ahmed committed
48
void nr_pusch_codeword_scrambling(uint8_t *in,
49
                         uint32_t size,
Khalid Ahmed's avatar
Khalid Ahmed committed
50 51 52 53 54
                         uint32_t Nid,
                         uint32_t n_RNTI,
                         uint32_t* out) {

  uint8_t reset, b_idx;
Khalid Ahmed's avatar
Khalid Ahmed committed
55
  uint32_t x1, x2, s=0, temp_out;
Khalid Ahmed's avatar
Khalid Ahmed committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69

  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
70 71 72 73 74 75 76 77 78 79
    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
80 81 82 83 84
    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);
  }

85
}
86

87
void nr_ue_ulsch_procedures(PHY_VARS_NR_UE *UE,
88 89 90
                               unsigned char harq_pid,
                               uint8_t slot,
                               uint8_t thread_id,
91
                               int gNB_id) {
92

93
  uint32_t available_bits;
94
  uint8_t mod_order, cwd_index, num_of_codewords, l;
95 96 97 98 99
  uint32_t scrambled_output[NR_MAX_NB_CODEWORDS][NR_MAX_PDSCH_ENCODED_LENGTH>>5];
  uint32_t ***pusch_dmrs;
  int16_t **tx_layers;
  int32_t **txdataF;
  uint16_t start_sc, start_rb;
100
  int8_t Wf[2], Wt[2], l_prime[2], delta;
101 102
  uint16_t n_dmrs, code_rate, number_dmrs_symbols;
  uint8_t dmrs_type;
103
  uint8_t mapping_type;
104 105 106
  int ap, start_symbol, Nid_cell, i;
  int sample_offsetF, N_RE_prime, N_PRB_oh;
  uint16_t n_rnti;
107 108 109 110

  NR_UE_ULSCH_t *ulsch_ue;
  NR_UL_UE_HARQ_t *harq_process_ul_ue;
  NR_DL_FRAME_PARMS *frame_parms = &UE->frame_parms;
111
  NR_UE_PUSCH *pusch_ue = UE->pusch_vars[thread_id][gNB_id];
112 113

  num_of_codewords = 1; // tmp assumption
114 115 116
  n_rnti = 0x1234;
  Nid_cell = 0;
  N_PRB_oh = 0; // higher layer (RRC) parameter xOverhead in PUSCH-ServingCellConfig
117 118 119
  number_dmrs_symbols = 0;

  mapping_type = UE->pusch_config.pusch_TimeDomainResourceAllocation[0]->mappingType;
120

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

123
    ulsch_ue = UE->ulsch[thread_id][gNB_id][cwd_index];
124 125
    harq_process_ul_ue = ulsch_ue->harq_processes[harq_pid];

126 127 128 129 130 131 132 133 134 135 136 137 138
    for (i = 0; i < NR_SYMBOLS_PER_SLOT; i++)
      number_dmrs_symbols += is_dmrs_symbol(i,
                                            0,
                                            0,
                                            0,
                                            0,
                                            0,
                                            harq_process_ul_ue->number_of_symbols,
                                            &UE->dmrs_UplinkConfig,
                                            mapping_type,
                                            frame_parms->ofdm_symbol_size);

    ulsch_ue->length_dmrs = UE->dmrs_UplinkConfig.pusch_maxLength;
139 140
    ulsch_ue->rnti        = n_rnti;
    ulsch_ue->Nid_cell    = Nid_cell;
141
    ulsch_ue->nb_re_dmrs  = ((UE->dmrs_UplinkConfig.pusch_dmrs_type == pusch_dmrs_type1)?6:4)*number_dmrs_symbols;
142 143 144 145 146

    N_RE_prime = NR_NB_SC_PER_RB*harq_process_ul_ue->number_of_symbols - ulsch_ue->nb_re_dmrs - N_PRB_oh;

    harq_process_ul_ue->num_of_mod_symbols = N_RE_prime*harq_process_ul_ue->nb_rb*num_of_codewords;

147 148 149 150 151 152 153 154 155
    mod_order      = nr_get_Qm_ul(harq_process_ul_ue->mcs, 0);
    code_rate      = nr_get_code_rate_ul(harq_process_ul_ue->mcs, 0);

    harq_process_ul_ue->TBS = nr_compute_tbs(mod_order, 
                                             code_rate,
                                             harq_process_ul_ue->nb_rb,
                                             harq_process_ul_ue->number_of_symbols,
                                             ulsch_ue->nb_re_dmrs*ulsch_ue->length_dmrs,
                                             0,
156
                                             harq_process_ul_ue->Nl);
157

158 159
    //-----------------------------------------------------//
    // to be removed later when MAC is ready
160

161
    if (harq_process_ul_ue != NULL){
162
      for (i = 0; i < harq_process_ul_ue->TBS / 8; i++) {
163
        harq_process_ul_ue->a[i] = (unsigned char) rand();
164 165
	//printf("input encoder a[%d]=0x%02x\n",i,harq_process_ul_ue->a[i]);
      }
166 167 168 169 170 171
    } else {
      LOG_E(PHY, "[phy_procedures_nrUE_TX] harq_process_ul_ue is NULL !!\n");
      return;
    }

    //-----------------------------------------------------//
172

173 174 175 176 177 178 179 180 181 182 183
    /////////////////////////ULSCH coding/////////////////////////
    ///////////

    nr_ulsch_encoding(ulsch_ue, frame_parms, harq_pid);

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

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

184
    mod_order      = nr_get_Qm_ul(harq_process_ul_ue->mcs, 0);
185 186

    available_bits = nr_get_G(harq_process_ul_ue->nb_rb,
Ahmed Hussein's avatar
Ahmed Hussein committed
187
                              harq_process_ul_ue->number_of_symbols,
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
                              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);

213

214
    // pusch_transform_precoding(ulsch_ue, frame_parms, harq_pid);
215 216 217 218 219 220 221

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


  }

Ahmed Hussein's avatar
Ahmed Hussein committed
222
  start_symbol = 14 - harq_process_ul_ue->number_of_symbols;
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

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

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

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

238
  nr_ue_layer_mapping(UE->ulsch[thread_id][gNB_id],
239 240 241 242
                   harq_process_ul_ue->Nl,
                   available_bits/mod_order,
                   tx_layers);

243 244 245 246 247 248 249 250 251 252 253 254 255
  ///////////
  ////////////////////////////////////////////////////////////////////////


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

  l_prime[0] = 0; // single symbol ap 0

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

Ahmed Hussein's avatar
Ahmed Hussein committed
256
  for (l = start_symbol; l < start_symbol + harq_process_ul_ue->number_of_symbols; l++) {
257

258 259 260 261 262 263 264 265 266 267 268 269 270
    is_dmrs = is_dmrs_symbol(l,
                             0,
                             0,
                             0,
                             0,
                             0,
                             harq_process_ul_ue->number_of_symbols,
                             &UE->dmrs_UplinkConfig,
                             mapping_type,
                             frame_parms->ofdm_symbol_size);

    if (is_dmrs == 1)
      nb_re_dmrs_per_rb = ulsch_ue->nb_re_dmrs;
271 272 273 274 275 276 277 278 279 280 281 282
    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
283 284 285 286

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

287 288


289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  /////////////////////////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);
306
    
307

308
    uint8_t k_prime=0;
Khalid Ahmed's avatar
Khalid Ahmed committed
309
    uint8_t is_dmrs;
310 311
    uint16_t m=0, n=0, dmrs_idx=0, k=0;

Ahmed Hussein's avatar
Ahmed Hussein committed
312
    for (l=start_symbol; l<start_symbol+harq_process_ul_ue->number_of_symbols; l++) {
313 314

      k = start_sc;
Khalid Ahmed's avatar
Khalid Ahmed committed
315
      n = 0;
316
      dmrs_idx = 0;
317 318 319 320 321

      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;

Khalid Ahmed's avatar
Khalid Ahmed committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335
        is_dmrs = 0;

        is_dmrs = is_dmrs_symbol(l,
                                 k,
                                 start_sc,
                                 k_prime,
                                 n,
                                 delta,
                                 harq_process_ul_ue->number_of_symbols,
                                 &UE->dmrs_UplinkConfig,
                                 mapping_type,
                                 frame_parms->ofdm_symbol_size);

        if (is_dmrs == 1) {
336

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

339 340 341 342
          ((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
343
            printf("dmrs_idx %d\t l %d \t k %d \t k_prime %d \t n %d \t dmrs: %d %d\n",
344 345 346 347
            dmrs_idx, l, k, k_prime, n, ((int16_t*)txdataF[ap])[(sample_offsetF)<<1],
            ((int16_t*)txdataF[ap])[((sample_offsetF)<<1) + 1]);
          #endif

348

349 350 351 352 353 354 355 356
          dmrs_idx++;
          k_prime++;
          k_prime&=1;
          n+=(k_prime)?0:1;
        }

        else {

357 358
          ((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];
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

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

375 376 377
  ///////////
  ////////////////////////////////////////////////////////////////////////

378
  return;
379 380 381 382
}


uint8_t nr_ue_pusch_common_procedures(PHY_VARS_NR_UE *UE,
383
                                      uint8_t harq_pid,
384
                                      uint8_t slot,
385 386
                                      uint8_t thread_id,
                                      uint8_t gNB_id,
387 388 389 390 391
                                      NR_DL_FRAME_PARMS *frame_parms) {

  int tx_offset, ap;
  int32_t **txdata;
  int32_t **txdataF;
392
  int timing_advance;
393
  uint8_t Nl = UE->ulsch[thread_id][gNB_id][0]->harq_processes[harq_pid]->Nl; // cw 0
394 395 396 397

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

398 399 400 401 402 403 404
#if defined(EXMIMO) || defined(OAI_USRP) || defined(OAI_BLADERF) || defined(OAI_LMSSDR)  || defined(OAI_ADRV9371_ZC706)
  timing_advance = UE->timing_advance;
#else
  timing_advance = 0;
#endif

  tx_offset = slot*frame_parms->samples_per_slot - timing_advance;
405 406 407 408

  if (tx_offset < 0)
    tx_offset += frame_parms->samples_per_frame;

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  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;
}