nr_ulsch_ue.c 15.3 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"
adk's avatar
adk committed
34
#include "PHY/NR_REFSIG/ptrs_nr.h"
35
#include "PHY/NR_UE_TRANSPORT/nr_transport_ue.h"
36 37
#include "PHY/NR_UE_TRANSPORT/nr_transport_proto_ue.h"
#include "PHY/MODULATION/nr_modulation.h"
38
#include "PHY/MODULATION/modulation_common.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
39
#include "common/utils/assertions.h"
40
#include "common/utils/LOG/vcd_signal_dumper.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
41
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
42
#include "PHY/NR_TRANSPORT/nr_sch_dmrs.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
43
#include "PHY/defs_nr_common.h"
44
#include "PHY/TOOLS/tools_defs.h"
45
#include "executables/nr-softmodem.h"
46
#include "executables/softmodem-common.h"
matzakos's avatar
matzakos committed
47
#include "LAYER2/NR_MAC_UE/mac_proto.h"
48 49

//#define DEBUG_SCFDMA
50
//#define DEBUG_PUSCH_MAPPING
51
//#define DEBUG_MAC_PDU
52

53
//extern int32_t uplink_counter;
54

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

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

  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
77 78 79 80 81 82 83 84 85 86
    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
87 88 89 90 91
    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);
  }

92
}
93

94
void nr_ue_ulsch_procedures(PHY_VARS_NR_UE *UE,
95
                               unsigned char harq_pid,
96
                               uint32_t frame,
97 98
                               uint8_t slot,
                               uint8_t thread_id,
99
                               int gNB_id) {
100

101 102
  LOG_D(PHY,"nr_ue_ulsch_procedures hard_id %d %d.%d\n",harq_pid,frame,slot);

cig's avatar
cig committed
103
  uint32_t available_bits;
cig's avatar
cig committed
104
  uint8_t cwd_index, l;
105 106 107
  uint32_t scrambled_output[NR_MAX_NB_CODEWORDS][NR_MAX_PDSCH_ENCODED_LENGTH>>5];
  int16_t **tx_layers;
  int32_t **txdataF;
108
  int8_t Wf[2], Wt[2], l_prime[2], delta;
cig's avatar
cig committed
109
  uint8_t nb_dmrs_re_per_rb;
110 111 112
  int ap, i;
  int sample_offsetF, N_RE_prime;

113
  NR_DL_FRAME_PARMS *frame_parms = &UE->frame_parms;
114
  NR_UE_PUSCH *pusch_ue = UE->pusch_vars[thread_id][gNB_id];
115
  // ptrs_UplinkConfig_t *ptrs_Uplink_Config = &UE->pusch_config.dmrs_UplinkConfig.ptrs_UplinkConfig;
116

117 118 119 120
  uint8_t  num_of_codewords = 1; // tmp assumption
  int      Nid_cell = 0;
  int      N_PRB_oh = 0; // higher layer (RRC) parameter xOverhead in PUSCH-ServingCellConfig
  uint16_t number_dmrs_symbols = 0;
121

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

124 125
    NR_UE_ULSCH_t *ulsch_ue = UE->ulsch[thread_id][gNB_id][cwd_index];
    NR_UL_UE_HARQ_t *harq_process_ul_ue = ulsch_ue->harq_processes[harq_pid];
cig's avatar
cig committed
126 127 128 129 130 131 132 133 134 135 136 137 138
    nfapi_nr_ue_pusch_pdu_t *pusch_pdu = &harq_process_ul_ue->pusch_pdu;

    int start_symbol          = pusch_pdu->start_symbol_index;
    uint16_t ul_dmrs_symb_pos = pusch_pdu->ul_dmrs_symb_pos;
    uint8_t number_of_symbols = pusch_pdu->nr_of_symbols;
    uint8_t dmrs_type         = pusch_pdu->dmrs_config_type;
    uint16_t start_rb         = pusch_pdu->rb_start;
    uint16_t nb_rb            = pusch_pdu->rb_size;
    uint8_t Nl                = pusch_pdu->nrOfLayers;
    uint8_t mod_order         = pusch_pdu->qam_mod_order;
    uint16_t rnti             = pusch_pdu->rnti;
    uint8_t cdm_grps_no_data  = pusch_pdu->num_dmrs_cdm_grps_no_data;
    uint16_t start_sc         = frame_parms->first_carrier_offset + start_rb*NR_NB_SC_PER_RB;
139

140 141
    if (start_sc >= frame_parms->ofdm_symbol_size)
      start_sc -= frame_parms->ofdm_symbol_size;
142

143
    ulsch_ue->Nid_cell    = Nid_cell;
144

cig's avatar
cig committed
145
    get_num_re_dmrs(pusch_pdu, &nb_dmrs_re_per_rb, &number_dmrs_symbols);
146

cig's avatar
cig committed
147
    // TbD num_of_mod_symbols is set but never used
148 149
    N_RE_prime = NR_NB_SC_PER_RB*number_of_symbols - nb_dmrs_re_per_rb*number_dmrs_symbols - N_PRB_oh;
    harq_process_ul_ue->num_of_mod_symbols = N_RE_prime*nb_rb*num_of_codewords;
150

151 152 153
    /////////////////////////ULSCH coding/////////////////////////
    ///////////

154 155
    unsigned int G = nr_get_G(nb_rb, number_of_symbols,
                              nb_dmrs_re_per_rb, number_dmrs_symbols, mod_order, Nl);
156
    nr_ulsch_encoding(ulsch_ue, frame_parms, harq_pid, G);
157 158 159 160 161 162 163

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

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

164
    available_bits = G;
165 166 167 168 169 170

    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,
171
                                 rnti,
172 173 174 175 176 177 178 179 180 181 182 183 184 185
                                 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);

186

187
    // pusch_transform_precoding(ulsch_ue, frame_parms, harq_pid);
188 189 190 191 192 193

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

  /////////////////////////DMRS Modulation/////////////////////////
  ///////////
194 195
  uint32_t ***pusch_dmrs = UE->nr_gold_pusch_dmrs[slot];
  uint16_t n_dmrs = (start_rb+nb_rb)*((dmrs_type == pusch_dmrs_type1) ? 6:4);
196
  int16_t mod_dmrs[n_dmrs<<1] __attribute((aligned(16)));
197 198 199
  ///////////
  ////////////////////////////////////////////////////////////////////////

adk's avatar
adk committed
200 201 202 203

  /////////////////////////PTRS parameters' initialization/////////////////////////
  ///////////

204
  int16_t mod_ptrs[nb_rb] __attribute((aligned(16))); // assume maximum number of PTRS per pusch allocation
205 206
  uint8_t L_ptrs, K_ptrs = 0;
  uint16_t beta_ptrs = 1; // temp value until power control is implemented
adk's avatar
adk committed
207

cig's avatar
cig committed
208
  if (pusch_pdu->pdu_bit_map & PUSCH_PDU_BITMAP_PUSCH_PTRS) {
adk's avatar
adk committed
209

210
    K_ptrs = harq_process_ul_ue->pusch_pdu.pusch_ptrs.ptrs_freq_density;
211
    L_ptrs = 1<<harq_process_ul_ue->pusch_pdu.pusch_ptrs.ptrs_time_density;
adk's avatar
adk committed
212 213 214 215 216 217

    beta_ptrs = 1; // temp value until power control is implemented

    ulsch_ue->ptrs_symbols = 0;

    set_ptrs_symb_idx(&ulsch_ue->ptrs_symbols,
218
                      number_of_symbols,
adk's avatar
adk committed
219 220
                      start_symbol,
                      L_ptrs,
221
                      ul_dmrs_symb_pos);
adk's avatar
adk committed
222 223 224 225 226
  }

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

227 228 229 230 231
  /////////////////////////ULSCH layer mapping/////////////////////////
  ///////////

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

232
  nr_ue_layer_mapping(UE->ulsch[thread_id][gNB_id],
233
                      Nl,
234 235
                      available_bits/mod_order,
                      tx_layers);
236

237 238 239 240 241 242 243 244 245 246 247 248 249
  ///////////
  ////////////////////////////////////////////////////////////////////////


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

250
  for (l = start_symbol; l < start_symbol + number_of_symbols; l++) {
251

252
    if((ul_dmrs_symb_pos >> l) & 0x01)
253
      nb_re_dmrs_per_rb = nb_dmrs_re_per_rb;
254 255 256
    else
      nb_re_dmrs_per_rb = 0;
    
257
    nb_re_pusch = nb_rb * (NR_NB_SC_PER_RB - nb_re_dmrs_per_rb);
258 259 260 261 262 263 264 265

    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
266 267 268 269

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

270 271


272 273 274 275 276
  /////////////////////////ULSCH RE mapping/////////////////////////
  ///////////

  txdataF = UE->common_vars.txdataF;

277
  for (ap=0; ap< Nl; ap++) {
278

279 280 281
    uint8_t k_prime = 0;
    uint16_t m = 0;

282 283 284 285 286
    // DMRS params for this ap
    get_Wt(Wt, ap, dmrs_type);
    get_Wf(Wf, ap, dmrs_type);
    delta = get_delta(ap, dmrs_type);

287
    for (l=start_symbol; l<start_symbol+number_of_symbols; l++) {
288

289 290 291 292 293
      uint16_t k = start_sc;
      uint16_t n = 0;
      uint8_t is_dmrs_sym = 0;
      uint8_t is_ptrs_sym = 0;
      uint16_t dmrs_idx = 0, ptrs_idx = 0;
294

295 296
      if ((ul_dmrs_symb_pos >> l) & 0x01) {
        is_dmrs_sym = 1;
297

cig's avatar
cig committed
298
        if (pusch_pdu->transform_precoding == 1){ // if transform precoding is disabled
299 300 301 302 303 304 305
          if (dmrs_type == pusch_dmrs_type1)
            dmrs_idx = start_rb*6;
          else
            dmrs_idx = start_rb*4;
        } else {
            dmrs_idx = 0;
        }
306

307
        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
Khalid Ahmed's avatar
Khalid Ahmed committed
308

cig's avatar
cig committed
309
      } else if (pusch_pdu->pdu_bit_map & PUSCH_PDU_BITMAP_PUSCH_PTRS) {
310 311 312 313

        if(is_ptrs_symbol(l, ulsch_ue->ptrs_symbols)) {
          is_ptrs_sym = 1;
          nr_modulation(pusch_dmrs[l][0], nb_rb, DMRS_MOD_ORDER, mod_ptrs);
314
        }
315 316 317
      }

      for (i=0; i< nb_rb*NR_NB_SC_PER_RB; i++) {
Khalid Ahmed's avatar
Khalid Ahmed committed
318

319 320
        uint8_t is_dmrs = 0;
        uint8_t is_ptrs = 0;
321

322 323 324 325 326 327
        sample_offsetF = l*frame_parms->ofdm_symbol_size + k;

        if (is_dmrs_sym) {
          if (k == ((start_sc+get_dmrs_freq_idx_ul(n, k_prime, delta, dmrs_type))%frame_parms->ofdm_symbol_size))
            is_dmrs = 1;
        } else if (is_ptrs_sym) {
328 329 330 331 332 333
            is_ptrs = is_ptrs_subcarrier(k,
                                         rnti,
                                         ap,
                                         dmrs_type,
                                         K_ptrs,
                                         nb_rb,
cig's avatar
cig committed
334
                                         pusch_pdu->pusch_ptrs.ptrs_ports_list[0].ptrs_re_offset,
335 336
                                         start_sc,
                                         frame_parms->ofdm_symbol_size);
adk's avatar
adk committed
337 338
        }

Khalid Ahmed's avatar
Khalid Ahmed committed
339
        if (is_dmrs == 1) {
340 341 342 343 344

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

350

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

adk's avatar
adk committed
356 357 358 359 360 361 362
        }  else if (is_ptrs == 1) {

          ((int16_t*)txdataF[ap])[(sample_offsetF)<<1] = (beta_ptrs*AMP*mod_ptrs[ptrs_idx<<1]) >> 15;
          ((int16_t*)txdataF[ap])[((sample_offsetF)<<1) + 1] = (beta_ptrs*AMP*mod_ptrs[(ptrs_idx<<1) + 1]) >> 15;

          ptrs_idx++;

cig's avatar
cig committed
363
        } else if (!is_dmrs_sym || allowed_xlsch_re_in_dmrs_symbol(k, start_sc, cdm_grps_no_data, dmrs_type)) {
364

365 366
          ((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];
367

368
        #ifdef DEBUG_PUSCH_MAPPING
369 370 371
          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]);
372
        #endif
373

374
          m++;
375 376 377 378 379

        } else {

          ((int16_t*)txdataF[ap])[(sample_offsetF)<<1]       = 0;
          ((int16_t*)txdataF[ap])[((sample_offsetF)<<1) + 1] = 0;
380 381 382 383 384

        }

        if (++k >= frame_parms->ofdm_symbol_size)
          k -= frame_parms->ofdm_symbol_size;
385 386 387
      }
    }
  }
388
  }
389

390 391 392
  NR_UL_UE_HARQ_t *harq_process_ulsch=NULL;
  harq_process_ulsch = UE->ulsch[thread_id][gNB_id][0]->harq_processes[harq_pid];
  harq_process_ulsch->status = SCH_IDLE;
393

394 395 396 397 398 399 400 401
  ///////////
  ////////////////////////////////////////////////////////////////////////

}


uint8_t nr_ue_pusch_common_procedures(PHY_VARS_NR_UE *UE,
                                      uint8_t slot,
402 403
                                      NR_DL_FRAME_PARMS *frame_parms,
                                      uint8_t Nl) {
404 405 406 407 408 409 410 411

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

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

412
  tx_offset = frame_parms->get_samples_slot_timestamp(slot, frame_parms, 0);
413

414 415 416 417 418 419 420
  // 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));
  }*/


421 422 423
  txdata = UE->common_vars.txdata;
  txdataF = UE->common_vars.txdataF;

424 425 426 427 428 429 430 431 432 433 434 435 436
  int symb_offset = (slot%frame_parms->slots_per_subframe)*frame_parms->symbols_per_slot;
  for(ap = 0; ap < Nl; ap++) {
    for (int s=0;s<NR_NUMBER_OF_SYMBOLS_PER_SLOT;s++){
      LOG_D(PHY,"rotating txdataF symbol %d (%d) => (%d.%d)\n",
	    s,s+symb_offset,frame_parms->symbol_rotation[2*(s+symb_offset)],frame_parms->symbol_rotation[1+(2*(s+symb_offset))]);
      rotate_cpx_vector((int16_t *)&txdataF[ap][frame_parms->ofdm_symbol_size*s],
			&frame_parms->symbol_rotation[2*(s+symb_offset)],
			(int16_t *)&txdataF[ap][frame_parms->ofdm_symbol_size*s],
			frame_parms->ofdm_symbol_size,
			15);
    }
  }

437 438 439 440 441 442 443 444 445 446 447 448 449
  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);
450 451 452
    }
  }

453 454 455 456
  ///////////
  ////////////////////////////////////////////////////
  return 0;
}