nr_ulsch_ue.c 16.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"
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
#include "PHY/TOOLS/tools_defs.h"
44
#include "executables/nr-softmodem.h"
45 46

//#define DEBUG_SCFDMA
47
//#define DEBUG_PUSCH_MAPPING
48
#define DEBUG_MAC_PDU
49

50 51
//extern int32_t uplink_counter;

Khalid Ahmed's avatar
Khalid Ahmed committed
52
void nr_pusch_codeword_scrambling(uint8_t *in,
53
                         uint32_t size,
Khalid Ahmed's avatar
Khalid Ahmed committed
54 55 56 57 58
                         uint32_t Nid,
                         uint32_t n_RNTI,
                         uint32_t* out) {

  uint8_t reset, b_idx;
Khalid Ahmed's avatar
Khalid Ahmed committed
59
  uint32_t x1, x2, s=0, temp_out;
Khalid Ahmed's avatar
Khalid Ahmed committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73

  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
74 75 76 77 78 79 80 81 82 83
    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
84 85 86 87 88
    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);
  }

89
}
90

91
uint8_t nr_ue_ulsch_procedures(PHY_VARS_NR_UE *UE,
92
                               unsigned char harq_pid,
93
                               uint8_t frame,
94 95
                               uint8_t slot,
                               uint8_t thread_id,
96
                               int gNB_id) {
97

98
  uint32_t available_bits;
99
  uint8_t mod_order, cwd_index, num_of_codewords, l;
100 101 102 103 104
  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;
105
  int8_t Wf[2], Wt[2], l_prime[2], delta;
106 107
  uint16_t n_dmrs, code_rate, number_dmrs_symbols;
  uint8_t dmrs_type;
108
  uint8_t mapping_type;
109 110 111
  int ap, start_symbol, Nid_cell, i;
  int sample_offsetF, N_RE_prime, N_PRB_oh;
  uint16_t n_rnti;
112
  uint8_t data_existing =0;
113 114 115 116

  NR_UE_ULSCH_t *ulsch_ue;
  NR_UL_UE_HARQ_t *harq_process_ul_ue;
  NR_DL_FRAME_PARMS *frame_parms = &UE->frame_parms;
117
  NR_UE_PUSCH *pusch_ue = UE->pusch_vars[thread_id][gNB_id];
118
  uint8_t ulsch_input_buffer[MAX_ULSCH_PAYLOAD_BYTES];
119 120

  num_of_codewords = 1; // tmp assumption
121 122 123
  n_rnti = 0x1234;
  Nid_cell = 0;
  N_PRB_oh = 0; // higher layer (RRC) parameter xOverhead in PUSCH-ServingCellConfig
124 125 126
  number_dmrs_symbols = 0;

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

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

130
    ulsch_ue = UE->ulsch[thread_id][gNB_id][cwd_index];
131 132
    harq_process_ul_ue = ulsch_ue->harq_processes[harq_pid];

133 134 135
    start_symbol = harq_process_ul_ue->start_symbol;

    for (i = start_symbol; i < start_symbol + harq_process_ul_ue->number_of_symbols; i++)
136 137 138 139 140 141 142 143 144 145 146 147
      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;
148 149
    ulsch_ue->rnti        = n_rnti;
    ulsch_ue->Nid_cell    = Nid_cell;
150
    ulsch_ue->nb_re_dmrs  = ((UE->dmrs_UplinkConfig.pusch_dmrs_type == pusch_dmrs_type1)?6:4)*number_dmrs_symbols;
151 152 153 154 155

    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;

156 157 158 159 160 161 162 163 164
    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,
165
                                             harq_process_ul_ue->Nl);
166

167 168
    uint8_t access_mode = SCHEDULED_ACCESS;

169 170
    //-----------------------------------------------------//
    // to be removed later when MAC is ready
171

172
    if (harq_process_ul_ue != NULL){
173 174 175 176 177 178 179

    	if (IS_SOFTMODEM_NOS1){
    		data_existing = nr_ue_get_sdu(UE->Mod_id, UE->CC_id, frame,
    				slot, 0, ulsch_input_buffer, harq_process_ul_ue->TBS/8, &access_mode);
    		if(data_existing){
    			//harq_process_ul_ue->a = (unsigned char*)calloc(harq_process_ul_ue->TBS/8, sizeof(unsigned char));
    			memcpy(harq_process_ul_ue->a, ulsch_input_buffer, harq_process_ul_ue->TBS/8);
180 181 182 183 184 185 186 187 188 189 190 191 192 193

    			#ifdef DEBUG_MAC_PDU
    				LOG_I(PHY, "Printing MAC PDU to be encoded: \n");
    				for (i = 0; i < harq_process_ul_ue->TBS / 8; i++) {
    					printf("0x%02x",harq_process_ul_ue->a[i]);
    				}
    				printf("\n");
				#endif
    		}
    		else{
    			//Use different rnti for the random (non-IP traffic) in noS1 mode, in order to use it as a filter
    			//to block this traffic from being forwarded to the MAC layer of the gNB
    			ulsch_ue->rnti        = 0x1111;
    			LOG_E(PHY, "Random data to be tranmsitted: \n");
194
    			for (i = 0; i < harq_process_ul_ue->TBS / 8; i++) {
195 196 197 198
    				harq_process_ul_ue->a[i] = (unsigned char) rand();
    				//printf(" input encoder a[%d]=0x%02x\n",i,harq_process_ul_ue->a[i]);
    				}
    			data_existing = 1;
199 200 201 202 203 204 205 206 207 208 209 210
    		}
    	}
        //else if(uplink_counter == 0){ //if(!IS_SOFTMODEM_NOS1){
    	else{
        	LOG_E(PHY, "Random data to be tranmsitted: \n");
        	for (i = 0; i < harq_process_ul_ue->TBS / 8; i++) {
        		harq_process_ul_ue->a[i] = (unsigned char) rand();
        		//printf(" input encoder a[%d]=0x%02x\n",i,harq_process_ul_ue->a[i]);
        	}
        	data_existing = 1;
        	//uplink_counter++;
        }
211 212
    } else {
      LOG_E(PHY, "[phy_procedures_nrUE_TX] harq_process_ul_ue is NULL !!\n");
213
      return 0;
214 215 216
    }

    //-----------------------------------------------------//
217

218 219 220
    /////////////////////////ULSCH coding/////////////////////////
    ///////////

221 222

    //if(data_existing){
223 224 225 226 227 228 229 230
    nr_ulsch_encoding(ulsch_ue, frame_parms, harq_pid);

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

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

231
    mod_order      = nr_get_Qm_ul(harq_process_ul_ue->mcs, 0);
232 233

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

260

261
    // pusch_transform_precoding(ulsch_ue, frame_parms, harq_pid);
262 263 264 265 266

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


267
  //}
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  }

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

284
  nr_ue_layer_mapping(UE->ulsch[thread_id][gNB_id],
285 286 287 288
                   harq_process_ul_ue->Nl,
                   available_bits/mod_order,
                   tx_layers);

289 290 291 292 293 294 295 296 297 298 299 300 301
  ///////////
  ////////////////////////////////////////////////////////////////////////


  //////////////////////// 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
302
  for (l = start_symbol; l < start_symbol + harq_process_ul_ue->number_of_symbols; l++) {
303

304 305 306 307 308 309 310 311 312 313 314 315 316
    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;
317 318 319 320 321 322 323 324 325 326 327 328
    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
329 330 331 332

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

333 334


335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  /////////////////////////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);
352
    
353

354
    uint8_t k_prime=0;
Khalid Ahmed's avatar
Khalid Ahmed committed
355
    uint8_t is_dmrs;
356 357
    uint16_t m=0, n=0, dmrs_idx=0, k=0;

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

      k = start_sc;
Khalid Ahmed's avatar
Khalid Ahmed committed
361
      n = 0;
362
      dmrs_idx = 0;
363 364 365 366 367

      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
368 369 370 371 372 373 374 375 376 377 378 379 380 381
        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) {
382

383
          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
384 385 386 387 388

          ((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
389
            printf("dmrs_idx %d\t l %d \t k %d \t k_prime %d \t n %d \t dmrs: %d %d\n",
390 391 392 393
            dmrs_idx, l, k, k_prime, n, ((int16_t*)txdataF[ap])[(sample_offsetF)<<1],
            ((int16_t*)txdataF[ap])[((sample_offsetF)<<1) + 1]);
          #endif

394

395 396 397 398 399 400 401 402
          dmrs_idx++;
          k_prime++;
          k_prime&=1;
          n+=(k_prime)?0:1;
        }

        else {

403 404
          ((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];
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

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

421 422
  //}

423 424 425
  ///////////
  ////////////////////////////////////////////////////////////////////////

426 427
  LOG_I(PHY, "Is data existing ?: %d \n", data_existing);
  return data_existing;
428 429 430 431
}


uint8_t nr_ue_pusch_common_procedures(PHY_VARS_NR_UE *UE,
432
                                      uint8_t harq_pid,
433
                                      uint8_t slot,
434 435
                                      uint8_t thread_id,
                                      uint8_t gNB_id,
436 437 438 439 440
                                      NR_DL_FRAME_PARMS *frame_parms) {

  int tx_offset, ap;
  int32_t **txdata;
  int32_t **txdataF;
441
  int timing_advance;
442
  uint8_t Nl = UE->ulsch[thread_id][gNB_id][0]->harq_processes[harq_pid]->Nl; // cw 0
443 444 445 446

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

447 448 449 450 451 452 453
#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;
454 455 456 457

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

458 459 460 461 462 463 464
  // clear the transmit data array for the current subframe
  /*for (int aa=0; aa<UE->frame_parms.nb_antennas_tx; aa++) {
	  memset(&UE->common_vars.txdata[aa][tx_offset],0,UE->frame_parms.samples_per_slot*sizeof(int32_t));
	  //memset(&UE->common_vars.txdataF[aa][tx_offset],0,UE->frame_parms.samples_per_slot*sizeof(int32_t));
  }*/


465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
  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;
}