lte-enb.c 34.8 KB
Newer Older
knopp's avatar
   
knopp 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
/*******************************************************************************

  Eurecom OpenAirInterface
  Copyright(c) 1999 - 2011 Eurecom

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information
  Openair Admin: openair_admin@eurecom.fr
  Openair Tech : openair_tech@eurecom.fr
  Forums       : http://forums.eurecom.fsr/openairinterface
  Address      : Eurecom, 2229, route des crêtes, 06560 Valbonne Sophia Antipolis, France

*******************************************************************************/

/*! \file lte-softmodem.c
 * \brief main program to control HW and scheduling
 * \author R. Knopp, F. Kaltenberger
 * \date 2012
 * \version 0.1
 * \company Eurecom
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr
 * \note
 * \warning
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <execinfo.h>
#include <getopt.h>

#include "rt_wrapper.h"

#ifdef EMOS
#include <gps.h>
#endif

#include "PHY/types.h"
#include "PHY/defs.h"
#include "openair0_lib.h"

#include "PHY/vars.h"
#include "MAC_INTERFACE/vars.h"
#include "SCHED/vars.h"
#include "LAYER2/MAC/vars.h"

#include "../../SIMU/USER/init_lte.h"

#ifdef EMOS
#include "SCHED/phy_procedures_emos.h"
#endif

#ifdef OPENAIR2
#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/vars.h"
#ifndef CELLULAR
#include "RRC/LITE/vars.h"
#endif
#include "PHY_INTERFACE/vars.h"
#endif

#include "UTIL/LOG/log_extern.h"
#include "UTIL/OTG/otg.h"
#include "UTIL/OTG/otg_vars.h"
#include "UTIL/MATH/oml.h"
#include "UTIL/LOG/vcd_signal_dumper.h"

90
91
92
93
94
95
96
97
#if defined(ENABLE_ITTI)
# include "intertask_interface_init.h"
# include "create_tasks.h"
# if defined(ENABLE_USE_MME)
#   include "s1ap_eNB.h"
# endif
#endif

knopp's avatar
   
knopp committed
98
99
100
101
#ifdef XFORMS
#include "PHY/TOOLS/lte_phy_scope.h"
#include "stats.h"
// current status is that every UE has a DL scope for a SINGLE eNB (eNB_id=0)
102
// at eNB 0, an UL scope for every UE
knopp's avatar
   
knopp committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
FD_lte_phy_scope_ue  *form_ue[NUMBER_OF_UE_MAX];
FD_lte_phy_scope_enb *form_enb[NUMBER_OF_UE_MAX];
FD_stats_form *form_stats=NULL;
char title[255];
int UE_id;
unsigned char scope_enb_num_ue = 1;
#endif //XFORMS
unsigned char UE_flag=0;

char *mme_ip;
#define FRAME_PERIOD 100000000ULL
#define DAQ_PERIOD 66667ULL

#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all

#ifdef RTAI
static SEM *mutex;
//static CND *cond;

static int thread0;
static int thread1;
//static int sync_thread;
#else
pthread_t thread0;
pthread_t thread1;
pthread_attr_t attr_dlsch_threads;
struct sched_param sched_param_dlsch;
#endif

pthread_t  thread2;
pthread_t  thread3;
/*
  static int instance_cnt=-1; //0 means worker is busy, -1 means its free
  int instance_cnt_ptr_kern,*instance_cnt_ptr_user;
  int pci_interface_ptr_kern;
*/
//extern unsigned int bigphys_top;
//extern unsigned int mem_base;

int card = 0;
exmimo_config_t *p_exmimo_config;
exmimo_id_t     *p_exmimo_id;
volatile unsigned int *DAQ_MBOX;

147
148
149
150
#if defined(ENABLE_ITTI)
volatile int start_eNB = 0;
#endif
volatile int oai_exit = 0;
knopp's avatar
   
knopp committed
151
152
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
int oai_flag = 0;

//int time_offset[4] = {-138,-138,-138,-138};
//int time_offset[4] = {-145,-145,-145,-145};
int time_offset[4] = {0,0,0,0};

u8 eNB_id=0;

u32 carrier_freq_fdd[4]= {2680e6,0,0,0};
u32 carrier_freq_tdd[4]= {2590e6-4000,0,0,0};
u32 carrier_freq[4];

struct timing_info_t {
  //unsigned int frame, hw_slot, last_slot, next_slot;
  RTIME time_min, time_max, time_avg, time_last, time_now;
  //unsigned int mbox0, mbox1, mbox2, mbox_target;
  unsigned int n_samples;
} timing_info;

runmode_t mode;
int rx_input_level_dBm;
#ifdef XFORMS
extern int otg_enabled;
#else
int otg_enabled;
#endif
int number_of_cards = 1;

//int mbox_bounds[20] = {8,16,24,30,38,46,54,60,68,76,84,90,98,106,114,120,128,136,144, 0}; ///boundaries of slots in terms ob mbox counter rounded up to even numbers
//int mbox_bounds[10] = {15, 30, 45, 60, 75, 90, 105, 120, 135, 0}; // mbox boundaries of subframes
int mbox_bounds[10] = {14, 28, 44, 58, 74, 88, 104, 118, 134, 148}; // mbox boundaries of subframes

int init_dlsch_threads(void);
void cleanup_dlsch_threads(void);
s32 init_rx_pdsch_thread(void);
void cleanup_rx_pdsch_thread(void);
int init_ulsch_threads(void);
void cleanup_ulsch_threads(void);

LTE_DL_FRAME_PARMS *frame_parms;

192
193
void setup_eNB_buffers(PHY_VARS_eNB *phy_vars_eNB,
                       LTE_DL_FRAME_PARMS *frame_parms, int carrier);
knopp's avatar
   
knopp committed
194
195
196
197
198
199
200
201
202

void signal_handler(int sig)
{
  void *array[10];
  size_t size;

  if (sig==SIGSEGV) {
    // get void*'s for all entries on the stack
    size = backtrace(array, 10);
203

knopp's avatar
   
knopp committed
204
205
206
207
    // print out all the frames to stderr
    fprintf(stderr, "Error: signal %d:\n", sig);
    backtrace_symbols_fd(array, size, 2);
    exit(-1);
208
  } else {
knopp's avatar
   
knopp committed
209
210
211
212
    oai_exit=1;
  }
}

213
void exit_fun(const char *s)
knopp's avatar
   
knopp committed
214
{
215
216
217
  if (s != NULL) {
    printf("Exiting: %s\n",s);
  }
knopp's avatar
   
knopp committed
218
219

  oai_exit=1;
220
221

#if defined(ENABLE_ITTI)
222
  itti_terminate_tasks (TASK_UNKNOWN);
223
224
#endif

knopp's avatar
   
knopp committed
225
226
227
228
229
230
  //rt_sleep_ns(FRAME_PERIOD);

  //exit (-1);
}

#ifdef XFORMS
231
232
void *scope_thread(void *arg)
{
knopp's avatar
   
knopp committed
233
234
235
236
237
238
  s16 prach_corr[1024], i;
  char stats_buffer[16384];
  //FILE *UE_stats, *eNB_stats;
  int len=0;

  /*
239
    if (UE_flag==1)
knopp's avatar
   
knopp committed
240
    UE_stats  = fopen("UE_stats.txt", "w");
241
    else
knopp's avatar
   
knopp committed
242
243
244
245
246
247
248
249
    eNB_stats = fopen("eNB_stats.txt", "w");
  */

  while (!oai_exit) {
    len = dump_eNB_stats (PHY_vars_eNB_g[0], stats_buffer, 0);
    fl_set_object_label(form_stats->stats_text, stats_buffer);
    //rewind (eNB_stats);
    //fwrite (stats_buffer, 1, len, eNB_stats);
250
251
252
253
254
    for(UE_id=0; UE_id<scope_enb_num_ue; UE_id++) {
      phy_scope_eNB(form_enb[UE_id],
                    PHY_vars_eNB_g[eNB_id],
                    UE_id);

knopp's avatar
   
knopp committed
255
256
257
258
259
260
261
262
    }
    //printf("doing forms\n");
    sleep(1);
  }

  //fclose (UE_stats);
  //fclose (eNB_stats);

263
  pthread_exit((void *)arg);
knopp's avatar
   
knopp committed
264
265
266
}
#endif

267
268
void do_OFDM_mod(mod_sym_t **txdataF, s32 **txdata, u16 next_slot,
                 LTE_DL_FRAME_PARMS *frame_parms)
knopp's avatar
   
knopp committed
269
270
271
{
  int aa, slot_offset, slot_offset_F;

272
273
  slot_offset_F = (next_slot)*(frame_parms->ofdm_symbol_size)*((
                    frame_parms->Ncp == EXTENDED) ? 6 : 7);
knopp's avatar
   
knopp committed
274
275
276
277
278
279
280
281
282
283
284
285
  slot_offset   = (next_slot)*(frame_parms->samples_per_tti>>1);

  for (aa = 0; aa < frame_parms->nb_antennas_tx; aa++) {
    if (frame_parms->Ncp == EXTENDED) {
      PHY_ofdm_mod(&txdataF[aa][slot_offset_F],
                   &txdata[aa][slot_offset],
                   frame_parms->log2_symbol_size,
                   6,
                   frame_parms->nb_prefix_samples,
                   frame_parms->twiddle_fft,
                   frame_parms->rev,
                   CYCLIC_PREFIX);
286
    } else {
knopp's avatar
   
knopp committed
287
288
289
290
291
292
293
294
295
296
      normal_prefix_mod(&txdataF[aa][slot_offset_F],
                        &txdata[aa][slot_offset],
                        7,
                        frame_parms);
    }
  }
}

int dummy_tx_buffer[3840*4] __attribute__((aligned(16)));

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
#if defined(ENABLE_ITTI)
void *l2l1_task(void *arg)
{
  MessageDef *message_p = NULL;

  itti_set_task_real_time(TASK_L2L1);
  itti_mark_task_ready(TASK_L2L1);

  if (UE_flag == 0) {
    /* Wait for the initialize message */
    do {
      if (message_p != NULL) {
        itti_free (ITTI_MSG_ORIGIN_ID(message_p), message_p);
      }
      itti_receive_msg (TASK_L2L1, &message_p);

      switch (ITTI_MSG_ID(message_p)) {
        case INITIALIZE_MESSAGE:
          /* Start eNB thread */
          start_eNB = 1;
          break;

        case TERMINATE_MESSAGE:
          oai_exit=1;
          itti_exit_task ();
          break;

        default:
          LOG_E(EMU, "Received unexpected message %s\n", ITTI_MSG_NAME(message_p));
          break;
      }
    } while (ITTI_MSG_ID(message_p) != INITIALIZE_MESSAGE);
    itti_free (ITTI_MSG_ORIGIN_ID(message_p), message_p);
  }

  do {
    // Wait for a message
    itti_receive_msg (TASK_L2L1, &message_p);

    switch (ITTI_MSG_ID(message_p)) {
      case TERMINATE_MESSAGE:
        oai_exit=1;
        itti_exit_task ();
        break;

      case MESSAGE_TEST:
        LOG_I(EMU, "Received %s\n", ITTI_MSG_NAME(message_p));
        break;

      default:
        LOG_E(EMU, "Received unexpected message %s\n", ITTI_MSG_NAME(message_p));
        break;
    }

    itti_free (ITTI_MSG_ORIGIN_ID(message_p), message_p);
  } while(1);

  return NULL;
}
#endif

knopp's avatar
   
knopp committed
358
359
360
361
362
363
364
365
366
367
/* This is the main eNB thread. It gets woken up by the kernel driver using the RTAI message mechanism (rt_send and rt_receive). */
static void *eNB_thread(void *arg)
{
#ifdef RTAI
  RT_TASK *task;
  SEM *sem;
#endif
  unsigned char last_slot, next_slot;
  int subframe = 0, hw_subframe;
  int frame=0;
368
  int skip_first=1;
knopp's avatar
   
knopp committed
369
370
371
372
373
374
375
376
377
378
379
  unsigned int msg1;
  unsigned int aa,slot_offset, slot_offset_F;
  int diff;
  int delay_cnt;
  RTIME time_in, time_diff;
  RTIME period;
  int mbox_target=0,mbox_current=0;
  int i,ret;
  int tx_offset;


380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#if defined(ENABLE_ITTI)
  /* Wait for eNB application initialization to be complete (eNB registration to MME) */
  {
    char *indicator[] = {".  ", ".. ", "...", " ..", "  .", "   "};
    int i = 0;

    while ((!oai_exit) && (start_eNB == 0)) {
      LOG_D(HW,"Waiting for eNB application to be ready %s\r", indicator[i]);
      i = (i + 1) % (sizeof(indicator) / sizeof(indicator[0]));
      usleep(200000);
    }
    LOG_D(HW,"\n");
  }
#endif

knopp's avatar
   
knopp committed
395
396
#ifdef RTAI
  task = rt_task_init_schmod(nam2num("TASK0"), 0, 0, 0, SCHED_FIFO, 0xF);
397
  rt_receive(0, (unsigned long *)((void *)&sem));
knopp's avatar
   
knopp committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
  LOG_D(HW,"Started eNB thread (id %p)\n",task);
#endif

#ifdef HARD_RT
  rt_make_hard_real_time();
#endif

  mlockall(MCL_CURRENT | MCL_FUTURE);

  timing_info.time_min = 100000000ULL;
  timing_info.time_max = 0;
  timing_info.time_avg = 0;
  timing_info.n_samples = 0;


  // sync to HW subframe == 0
414
  mbox_current = ((volatile unsigned int *)DAQ_MBOX)[0];
knopp's avatar
   
knopp committed
415
416
417
418
  rt_sleep_ns((165-mbox_current)*DAQ_PERIOD);

  time_in = rt_get_time_ns();

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
  while (!oai_exit) {
    //this is the mbox counter where we should be
    mbox_target = mbox_bounds[subframe];
    //this is the mbox counter where we are
    mbox_current = ((volatile unsigned int *)DAQ_MBOX)[0];
    //this is the time we need to sleep in order to synchronize with the hw (in multiples of DAQ_PERIOD)
    if ((mbox_current>=135) && (mbox_target<15)) { //handle the frame wrap-arround
      diff = 150-mbox_current+mbox_target;
    } else if ((mbox_current<15) && (mbox_target>=135)) {
      diff = -150+mbox_target-mbox_current;
    } else {
      diff = mbox_target - mbox_current;
    }

    if (diff < -15) {
      LOG_D(HW,
            "Time %.3f: Frame %d, missed subframe, proceeding with next one (subframe %d, hw_subframe %d, mbox_currend %d, mbox_target %d,diff %d) processing time %.3f\n",
            (float)(rt_get_time_ns()-time_in)/1e6,
            frame, subframe, hw_subframe, mbox_current, mbox_target, diff,
            (float)(timing_info.time_now-timing_info.time_last)/1e6);

      subframe++;
      if (frame>0) {
        oai_exit=1;
knopp's avatar
   
knopp committed
443
      }
444
445
446
      if (subframe==10) {
        subframe=0;
        frame++;
knopp's avatar
   
knopp committed
447
      }
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
      continue;
    }
    if (diff > 15) {
      LOG_D(HW,
            "Time %.3f: Frame %d, skipped subframe, waiting for hw to catch up (subframe %d, hw_subframe %d, mbox_current %d, mbox_target %d, diff %d), processing time %.3f\n",
            (float)(rt_get_time_ns()-time_in)/1e6,
            frame, subframe, hw_subframe, mbox_current, mbox_target, diff,
            (float)(timing_info.time_now-timing_info.time_last)/1e6);
      //exit(-1);
    }

    delay_cnt = 0;
    vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX,
                                            *((volatile unsigned int *) openair0_exmimo_pci[card].rxcnt_ptr[0]));
    vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DIFF, diff);
knopp's avatar
   
knopp committed
463

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
    while ((diff>0) && (!oai_exit)) {
      time_diff = rt_get_time_ns() - time_in;
      ret = rt_sleep_ns(diff*DAQ_PERIOD);
      if (ret) {
        LOG_D(HW,"eNB Frame %d, time %llu: rt_sleep_ns returned %d\n",frame, time_in);
      }

      delay_cnt++;
      if (delay_cnt == 10) {
        oai_exit = 1;
        LOG_D(HW,"eNB Frame %d: HW stopped ... \n",frame);
      }
      mbox_current = ((volatile unsigned int *)DAQ_MBOX)[0];
      if ((mbox_current>=135) && (mbox_target<15)) { //handle the frame wrap-arround
        diff = 150-mbox_current+mbox_target;
      } else if ((mbox_current<15) && (mbox_target>=135)) {
        diff = -150+mbox_target-mbox_current;
      } else {
        diff = mbox_target - mbox_current;
      }
    }

    hw_subframe = ((((volatile unsigned int *)DAQ_MBOX)[0]+135)%150)/15;
    LOG_D(HW,
          "Time: %.3f: Frame %d, subframe %d, hw_subframe %d (mbox %d) processing time %0.3f\n",
          (float)(rt_get_time_ns()-time_in)/1e6,
          frame,subframe,hw_subframe,((volatile unsigned int *)DAQ_MBOX)[0],
          (float)(timing_info.time_now-timing_info.time_last)/1e6);

    last_slot = (subframe<<1)+1;
    if (last_slot <0) {
      last_slot+=20;
    }
    next_slot = ((subframe<<1)+4)%LTE_SLOTS_PER_FRAME;

    if ((skip_first == 0) || (frame>5))
    {
      skip_first = 0;
      timing_info.time_last = rt_get_time_ns();

      //msg("subframe %d, last_slot %d,next_slot %d\n", subframe,last_slot,next_slot);
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX,
                                              *((volatile unsigned int *) openair0_exmimo_pci[card].rxcnt_ptr[0]));
knopp's avatar
   
knopp committed
507
508
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DIFF, diff);

509
510
511
      phy_procedures_eNB_lte (last_slot, next_slot, PHY_vars_eNB_g[0], 0,0,NULL);
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX,
                                              *((volatile unsigned int *) openair0_exmimo_pci[card].rxcnt_ptr[0]));
knopp's avatar
   
knopp committed
512
513
#ifndef IFFT_FPGA

514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
      if ((subframe_select(&PHY_vars_eNB_g[0]->lte_frame_parms,next_slot>>1)==SF_DL)||
          ((subframe_select(&PHY_vars_eNB_g[0]->lte_frame_parms,next_slot>>1)==SF_S)
           &&((next_slot&1)==0))) {
        //LOG_D(HW,"Frame %d: Generating slot %d\n",frame,next_slot);
        /*
        do_OFDM_mod(PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdataF[0],
        PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdata[0],
        next_slot,
        &PHY_vars_eNB_g[0]->lte_frame_parms);

        do_OFDM_mod(PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdataF[0],
        PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdata[0],
        next_slot+1,
        &PHY_vars_eNB_g[0]->lte_frame_parms);
        */

        vcd_signal_dumper_dump_function_by_name(
          VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_SFGEN,1);
        slot_offset_F = (next_slot)*
                        (PHY_vars_eNB_g[0]->lte_frame_parms.ofdm_symbol_size)*7;
        slot_offset = (next_slot)*
                      (PHY_vars_eNB_g[0]->lte_frame_parms.samples_per_tti>>1);

        normal_prefix_mod(
          &PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdataF[0][0][slot_offset_F],
          dummy_tx_buffer,
          7,
          &(PHY_vars_eNB_g[0]->lte_frame_parms));
        for (i = 0; i < PHY_vars_eNB_g[0]->lte_frame_parms.samples_per_tti/2; i++) {
          tx_offset = slot_offset + time_offset[0] + i;
          ((short *)&PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdata[0][0][tx_offset])[0]
            = ((short *)dummy_tx_buffer)[2*i]<<4;
          ((short *)&PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdata[0][0][tx_offset])[1]
            = ((short *)dummy_tx_buffer)[2*i+1]<<4;
        }

        slot_offset_F = (next_slot+1)*
                        (PHY_vars_eNB_g[0]->lte_frame_parms.ofdm_symbol_size)*7;
        slot_offset = (next_slot+1)*
                      (PHY_vars_eNB_g[0]->lte_frame_parms.samples_per_tti>>1);

        normal_prefix_mod(
          &PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdataF[0][0][slot_offset_F],
          dummy_tx_buffer,
          7,
          &(PHY_vars_eNB_g[0]->lte_frame_parms));
        for (i = 0; i < PHY_vars_eNB_g[0]->lte_frame_parms.samples_per_tti/2; i++) {
          tx_offset = slot_offset + i;
          ((short *)&PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdata[0][0][tx_offset])[0]
            = ((short *)dummy_tx_buffer)[2*i]<<4;
          ((short *)&PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdata[0][0][tx_offset])[1]
            = ((short *)dummy_tx_buffer)[2*i+1]<<4;
        }
      }
      vcd_signal_dumper_dump_function_by_name(
        VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_SFGEN,0);
      timing_info.time_now = rt_get_time_ns();
#endif //IFFT_FPGA
knopp's avatar
   
knopp committed
572
      /*
573
574
        if (frame%100==0)
        LOG_D(HW,"hw_slot %d (after): DAQ_MBOX %d\n",hw_slot,DAQ_MBOX[0]);
knopp's avatar
   
knopp committed
575
      */
576
    }
knopp's avatar
   
knopp committed
577

578
579
580
581
    /*
    if ((slot%2000)<10)
    LOG_D(HW,"fun0: doing very hard work\n");
    */
knopp's avatar
   
knopp committed
582
583


584
585
586
587
588
589
    if (oai_flag == 2) {
      //dump_ulsch(PHY_vars_eNB_g[0], subframe, 0);
      //exit(-1);
      oai_exit=1;
      exit_fun("[HW][eNB] HW stopped");
    }
knopp's avatar
   
knopp committed
590

591
592
    if (oai_flag == 1) {
      oai_flag = 2;
knopp's avatar
   
knopp committed
593
594
    }

595
596
597
598
599
600
601
602
603
604
605
606
607
608
    subframe++;
    if (subframe==10) {
      subframe=0;
      frame++;
    }
    if(frame == 1024) {
      frame = 0;
      time_in = rt_get_time_ns();
    }
#if defined(ENABLE_ITTI)
    itti_update_lte_time(frame, subframe * 2);
#endif
  }

knopp's avatar
   
knopp committed
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
  LOG_D(HW,"eNB_thread: finished, ran %d times.\n",frame);

#ifdef HARD_RT
  rt_make_soft_real_time();
#endif

  // clean task
#ifdef RTAI
  rt_sem_signal(mutex);
  rt_task_delete(task);
#endif
  LOG_D(HW,"Task deleted. returning\n");
  return 0;
}

624
625
int main(int argc, char **argv)
{
knopp's avatar
   
knopp committed
626
627
628
629
630
631
632
633
634
635
636
#ifdef RTAI
  RT_TASK *task;
#endif
  int i,j,aa;
  void *status;

  /*
    u32 rf_mode_max[4]     = {55759,55759,55759,55759};
    u32 rf_mode_med[4]     = {39375,39375,39375,39375};
    u32 rf_mode_byp[4]     = {22991,22991,22991,22991};
  */
637
638
639
640
  u32 my_rf_mode = RXEN + TXEN + TXLPFNORM + TXLPFEN + TXLPF25 + RXLPFNORM +
                   RXLPFEN + RXLPF25 + LNA1ON +LNAMax + RFBBNORM + DMAMODE_RX + DMAMODE_TX;
  u32 rf_mode_base = TXLPFNORM + TXLPFEN + TXLPF25 + RXLPFNORM + RXLPFEN +
                     RXLPF25 + LNA1ON + /*LNAMax Antennas*/ LNAByp + RFBBNORM;
knopp's avatar
   
knopp committed
641
642
643
644
645
646
647
648
649
650
  u32 rf_mode[4]     = {my_rf_mode,0,0,0};
  u32 rf_local[4]    = {8255000,8255000,8255000,8255000}; // UE zepto
  //{8254617, 8254617, 8254617, 8254617}; //eNB khalifa
  //{8255067,8254810,8257340,8257340}; // eNB PETRONAS

  u32 rf_vcocal[4]   = {910,910,910,910};
  u32 rf_vcocal_850[4] = {2015, 2015, 2015, 2015};
  u32 rf_rxdc[4]     = {32896,32896,32896,32896};
  // Gain for antennas connection
  //u32 rxgain[4]      = {25,20,20,20};
651
  //u32 txgain[4]      = {30,25,25,25};
knopp's avatar
   
knopp committed
652
653
654

  // Gain for Cable connection
  u32 rxgain[4]      = {20,20,20,20};
655
  u32 txgain[4]      = {25,25,25,25};
knopp's avatar
   
knopp committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675


  u8 frame_type = FDD;
  u8 tdd_config = 3;
  u8 tdd_config_S = 0;
  u8 extended_prefix_flag = 0;
  u16 Nid_cell = 0;
  u8 N_RB_DL = 25;
  u8  cooperation_flag = 0;
  u8 transmission_mode = 1;
  u8 abstraction_flag = 0;
  u8 nb_antennas_rx = 1;

  u8 beta_ACK=0,beta_RI=0,beta_CQI=2;

  int c;
  char do_forms=0;
  unsigned int fd;

  int amp;
676
  unsigned int rxg_max[4]= {133,133,133,133}, rxg_med[4]= {127,127,127,127}, rxg_byp[4]= {120,120,120,120};
knopp's avatar
   
knopp committed
677
678
679
680
681
  int tx_max_power=0;

  int ret, ant;

  int error_code;
682
  char *itti_dump_file = NULL;
knopp's avatar
   
knopp committed
683
684
685
686
687

  mode = normal_txrx;

  mme_ip = "146.208.175.6";

688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
  while ((c = getopt(argc, argv, "dC:K:O:T:N:R:Vi;")) != -1) {
    switch (c) {
      case 'd':
        do_forms=1;
        printf("Starting XFORMS ...\n");
        break;
      case 'C':
        carrier_freq[0] = atoi(optarg);
        carrier_freq[1] = atoi(optarg);
        carrier_freq[2] = atoi(optarg);
        carrier_freq[3] = atoi(optarg);
        break;
      case 'T':
        frame_type = TDD; // default FDD
        tdd_config = atoi(optarg);
        break;
      case 'K':
#if defined(ENABLE_ITTI)
        itti_dump_file = strdup(optarg);
#else
        printf("-K option is disabled when ENABLE_ITTI is not defined\n");
#endif
        break;
      case 'O':
#if defined(ENABLE_USE_MME)
        EPC_MODE_ENABLED = 1;
        if (optarg == NULL) { /* No IP address provided: use localhost */
          memcpy(&EPC_MODE_MME_ADDRESS[0], "127.0.0.1", 10);
        } else {
          u8 ip_length = strlen(optarg) + 1;
          memcpy(&EPC_MODE_MME_ADDRESS[0], optarg,
                 ip_length > 16 ? 16 : ip_length);
        }
#else
        printf("You enabled mme mode without s1ap compiled...\n");
#endif
        break;
      case 'R':
        N_RB_DL = atoi(optarg);
        if ((N_RB_DL != 6) && (N_RB_DL != 15) && (N_RB_DL != 25) &&
            (N_RB_DL != 50) && (N_RB_DL != 75) && (N_RB_DL != 100)) {
          printf("Illegal N_RB_DL %d (should be one of 6,15,25,50,75,100)\n", N_RB_DL);
          exit(-1);
        }
        break;
      case 'i':
        mme_ip = optarg;
        break;
      case 'V':
        ouput_vcd = 1;
        break;
      default:
        break;
knopp's avatar
   
knopp committed
741
    }
742
  }
knopp's avatar
   
knopp committed
743
744
745
746
747
748
749
750
751
752

  set_taus_seed (0);

  // initialize the log (see log.h for details)
  logInit();

  if (ouput_vcd) {
    vcd_signal_dumper_init("/tmp/openair_dump_eNB.vcd");
  }

753
754
755
756
757
758
#if defined(ENABLE_ITTI)
  log_set_instance_type (LOG_INSTANCE_ENB);

  itti_init(TASK_MAX, THREAD_MAX, MESSAGES_ID_MAX, tasks_info, messages_info,
            messages_definition_xml, itti_dump_file);
#endif
knopp's avatar
   
knopp committed
759
760
761
762
763

#ifdef NAS_NETLINK
  netlink_init();
#endif

764
#if !defined(ENABLE_ITTI)
knopp's avatar
   
knopp committed
765
766
767
  // to make a graceful exit when ctrl-c is pressed
  signal(SIGSEGV, signal_handler);
  signal(SIGINT, signal_handler);
768
#endif
knopp's avatar
   
knopp committed
769
770
771
772
773

#ifndef RTAI
  check_clock();
#endif

774
775
776
777
  init_lte_vars(&frame_parms, frame_type, tdd_config, tdd_config_S,
                extended_prefix_flag, N_RB_DL,
                Nid_cell, cooperation_flag, transmission_mode, abstraction_flag,
                nb_antennas_rx,0);
knopp's avatar
   
knopp committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792

  g_log->log_component[HW].level = LOG_INFO;
  g_log->log_component[HW].flag  = LOG_LOW;
  g_log->log_component[PHY].level = LOG_INFO;
  g_log->log_component[PHY].flag  = LOG_LOW;
  g_log->log_component[MAC].level = LOG_INFO;
  g_log->log_component[MAC].flag  = LOG_LOW;
  g_log->log_component[RLC].level = LOG_INFO;
  g_log->log_component[RLC].flag  = LOG_LOW;
  g_log->log_component[PDCP].level = LOG_INFO;
  g_log->log_component[PDCP].flag  = LOG_LOW;
  g_log->log_component[RRC].level = LOG_INFO;
  g_log->log_component[RRC].flag  = LOG_LOW;
  g_log->log_component[OIP].level = LOG_INFO;
  g_log->log_component[OIP].flag = LOG_HIGH;
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
#if defined(ENABLE_ITTI)
  g_log->log_component[EMU].level = LOG_INFO;
  g_log->log_component[EMU].flag  = LOG_HIGH;
# if defined(ENABLE_USE_MME)
  g_log->log_component[S1AP].level  = LOG_INFO;
  g_log->log_component[S1AP].flag   = LOG_HIGH;
  g_log->log_component[SCTP].level  = LOG_INFO;
  g_log->log_component[SCTP].flag   = LOG_HIGH;
# endif
  g_log->log_component[S1AP].level = LOG_INFO;
  g_log->log_component[S1AP].flag  = LOG_LOW;
#else
#endif
  g_log->log_component[ENB_APP].level = LOG_INFO;
  g_log->log_component[ENB_APP].flag  = LOG_HIGH;
knopp's avatar
   
knopp committed
808

809
810
811
  PHY_vars_eNB_g = malloc(sizeof(PHY_VARS_eNB *));
  PHY_vars_eNB_g[0] = init_lte_eNB(frame_parms,eNB_id,Nid_cell,cooperation_flag,
                                   transmission_mode,abstraction_flag);
knopp's avatar
   
knopp committed
812
813

#ifndef OPENAIR2
814
  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
knopp's avatar
   
knopp committed
815
816
817
818
819
820
821
822
823
824
    PHY_vars_eNB_g[0]->pusch_config_dedicated[i].betaOffset_ACK_Index = beta_ACK;
    PHY_vars_eNB_g[0]->pusch_config_dedicated[i].betaOffset_RI_Index  = beta_RI;
    PHY_vars_eNB_g[0]->pusch_config_dedicated[i].betaOffset_CQI_Index = beta_CQI;

    PHY_vars_eNB_g[0]->scheduling_request_config[i].sr_PUCCH_ResourceIndex = i;
    PHY_vars_eNB_g[0]->scheduling_request_config[i].sr_ConfigIndex = 7+(i%3);
    PHY_vars_eNB_g[0]->scheduling_request_config[i].dsr_TransMax = sr_n4;
  }
#endif

825

knopp's avatar
   
knopp committed
826
827
828
829
830
831
832
833
834
835
836
  NB_eNB_INST=1;
  NB_INST=1;

  openair_daq_vars.ue_dl_rb_alloc=0x1fff;
  openair_daq_vars.target_ue_dl_mcs=16;
  openair_daq_vars.ue_ul_nb_rb=6;
  openair_daq_vars.target_ue_ul_mcs=9;



  // set eNB to max gain
837
838
  PHY_vars_eNB_g[0]->rx_total_gain_eNB_dB =  rxg_max[0] + rxgain[0] -
      30; //was measured at rxgain=30;
knopp's avatar
   
knopp committed
839
840
841
842
843
844
845
846
  for (i=0; i<4; i++) {
    //        frame_parms->rfmode[i] = rf_mode_max[i];
    rf_mode[i] = (rf_mode[i] & (~LNAGAINMASK)) | LNAMax;
  }

  // Initialize card
  ret = openair0_open();
  if ( ret != 0 ) {
847
848
849
850
851
852
853
854
855
    if (ret == -1) {
      printf("Error opening /dev/openair0\n");
    }
    if (ret == -2) {
      printf("Error mapping bigshm\n");
    }
    if (ret == -3) {
      printf("Error mapping RX or TX buffer\n");
    }
knopp's avatar
   
knopp committed
856
857
858
    return(ret);
  }

859
860
861
  printf ("Detected %d number of cards, %d number of antennas.\n",
          openair0_num_detected_cards, openair0_num_antennas[card]);

knopp's avatar
   
knopp committed
862
863
864
  p_exmimo_config = openair0_exmimo_pci[card].exmimo_config_ptr;
  p_exmimo_id     = openair0_exmimo_pci[card].exmimo_id_ptr;

865
866
867
868
869
870
871
872
873
  printf("Card %d: ExpressMIMO %d, HW Rev %d, SW Rev 0x%d\n", card,
         p_exmimo_id->board_exmimoversion, p_exmimo_id->board_hwrev,
         p_exmimo_id->board_swrev);

  if (p_exmimo_id->board_swrev>=BOARD_SWREV_CNTL2) {
    p_exmimo_config->framing.eNB_flag   = 0;
  } else {
    p_exmimo_config->framing.eNB_flag   = 0;  //!UE_flag;
  }
knopp's avatar
   
knopp committed
874
875
  p_exmimo_config->framing.tdd_config = DUPLEXMODE_FDD + TXRXSWITCH_LSB;
  p_exmimo_config->framing.resampling_factor = 2;
876
877
878

  for (ant=0; ant<max(frame_parms->nb_antennas_tx,frame_parms->nb_antennas_rx);
       ant++) {
knopp's avatar
   
knopp committed
879
    p_exmimo_config->rf.rf_mode[ant] = rf_mode_base;
880
881
  }
  for (ant=0; ant<frame_parms->nb_antennas_tx; ant++) {
knopp's avatar
   
knopp committed
882
    p_exmimo_config->rf.rf_mode[ant] += (TXEN + DMAMODE_TX);
883
884
  }
  for (ant=0; ant<frame_parms->nb_antennas_rx; ant++) {
knopp's avatar
   
knopp committed
885
    p_exmimo_config->rf.rf_mode[ant] += (RXEN + DMAMODE_RX);
886
887
888
  }
  for (ant=max(frame_parms->nb_antennas_tx,frame_parms->nb_antennas_rx); ant<4;
       ant++) {
knopp's avatar
   
knopp committed
889
890
891
892
893
    p_exmimo_config->rf.rf_mode[ant] = 0;
    carrier_freq[ant] = 0; //this turns off all other LIMEs
  }

  for (ant = 0; ant < 4; ant++) {
894
    if (frame_type == FDD) {
knopp's avatar
   
knopp committed
895
      carrier_freq[ant] = carrier_freq_fdd[ant];
896
    } else {
knopp's avatar
   
knopp committed
897
      carrier_freq[ant] = carrier_freq_tdd[ant];
898
    }
knopp's avatar
   
knopp committed
899
  }
900
  for (ant = 0; ant<1; ant++) {
knopp's avatar
   
knopp committed
901
    p_exmimo_config->rf.do_autocal[ant] = 1;
902
903
904
905
    if (frame_type == FDD) {
      p_exmimo_config->rf.rf_freq_rx[ant] =
        carrier_freq[ant]-120e6;  // LTE FDD band 1 duplex space
    } else {
knopp's avatar
   
knopp committed
906
      p_exmimo_config->rf.rf_freq_rx[ant] = carrier_freq[ant];
907
    }
knopp's avatar
   
knopp committed
908
909
910
911

    p_exmimo_config->rf.rf_freq_tx[ant] = carrier_freq[ant];
    p_exmimo_config->rf.rx_gain[ant][0] = rxgain[ant];
    p_exmimo_config->rf.tx_gain[ant][0] = txgain[ant];
912

knopp's avatar
   
knopp committed
913
914
915
916
917
    p_exmimo_config->rf.rf_local[ant]   = rf_local[ant];
    p_exmimo_config->rf.rf_rxdc[ant]    = rf_rxdc[ant];

    if ((carrier_freq[ant] >= 850000000) && (carrier_freq[ant] <= 865000000)) {
      p_exmimo_config->rf.rf_vcocal[ant]  = rf_vcocal_850[ant];
918
919
      p_exmimo_config->rf.rffe_band_mode[ant] = DD_TDD;
    } else {
knopp's avatar
   
knopp committed
920
      p_exmimo_config->rf.rf_vcocal[ant]  = rf_vcocal[ant];
921
      p_exmimo_config->rf.rffe_band_mode[ant] = B19G_TDD;
knopp's avatar
   
knopp committed
922
923
924
925
926
927
928
929
930
931
    }

    p_exmimo_config->rf.rffe_gain_txlow[ant] = 63;
    p_exmimo_config->rf.rffe_gain_txhigh[ant] = 63;
    p_exmimo_config->rf.rffe_gain_rxfinal[ant] = 63;
    p_exmimo_config->rf.rffe_gain_rxlow[ant] = 63;
  }


  dump_frame_parms(frame_parms);
932

knopp's avatar
   
knopp committed
933
  mac_xface = malloc(sizeof(MAC_xface));
934

knopp's avatar
   
knopp committed
935
936
937
#ifdef OPENAIR2
  int eMBMS_active=0;
  l2_init(frame_parms,eMBMS_active,
938
939
          0,// cba_group_active
          0); //HO active
knopp's avatar
   
knopp committed
940
941
942
943
944
  mac_xface->mrbch_phy_sync_failure (0, 0, 0);
#endif

  mac_xface->macphy_exit = &exit_fun;

winckel's avatar
winckel committed
945
946
947
948
949
950
#if defined(ENABLE_ITTI)
  if (create_tasks(UE_flag ? 0 : 1, UE_flag ? 1 : 0) < 0) {
    exit(-1); // need a softer mode
  }
  printf("ITTI tasks created\n");
#endif
knopp's avatar
   
knopp committed
951
952

  number_of_cards = openair0_num_detected_cards;
953
  if (p_exmimo_id->board_exmimoversion==1) { //ExpressMIMO1
knopp's avatar
   
knopp committed
954
    openair_daq_vars.timing_advance = 138;
955
  } else { //ExpressMIMO2
knopp's avatar
   
knopp committed
956
    openair_daq_vars.timing_advance = 15;
957
  }
knopp's avatar
   
knopp committed
958
959
960
961
962

  setup_eNB_buffers(PHY_vars_eNB_g[0],frame_parms,0);

  openair0_dump_config(card);

963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
  printf("EXMIMO_CONFIG: rf_mode 0x %x %x %x %x, [0]: TXRXEn %d, TXLPFEn %d, TXLPF %d, RXLPFEn %d, RXLPF %d, RFBB %d, LNA %d, LNAGain %d, RXLPFMode %d, SWITCH %d, rf_rxdc %d, rf_local %d, rf_vcocal %d\n",

         p_exmimo_config->rf.rf_mode[0],
         p_exmimo_config->rf.rf_mode[1],
         p_exmimo_config->rf.rf_mode[2],
         p_exmimo_config->rf.rf_mode[3],
         (p_exmimo_config->rf.rf_mode[0]&3),  // RXen+TXen
         (p_exmimo_config->rf.rf_mode[0]&4)>>2,         //TXLPFen
         (p_exmimo_config->rf.rf_mode[0]&TXLPFMASK)>>3, //TXLPF
         (p_exmimo_config->rf.rf_mode[0]&128)>>7,      //RXLPFen
         (p_exmimo_config->rf.rf_mode[0]&RXLPFMASK)>>8, //TXLPF
         (p_exmimo_config->rf.rf_mode[0]&RFBBMASK)>>16, // RFBB mode
         (p_exmimo_config->rf.rf_mode[0]&LNAMASK)>>12, // RFBB mode
         (p_exmimo_config->rf.rf_mode[0]&LNAGAINMASK)>>14, // RFBB mode
         (p_exmimo_config->rf.rf_mode[0]&RXLPFMODEMASK)>>19, // RXLPF mode
         (p_exmimo_config->framing.tdd_config&TXRXSWITCH_MASK)>>1, // Switch mode
         p_exmimo_config->rf.rf_rxdc[0],
         p_exmimo_config->rf.rf_local[0],
         p_exmimo_config->rf.rf_vcocal[0]);

  for (ant=0; ant<4; ant++) {
knopp's avatar
   
knopp committed
984
    p_exmimo_config->rf.do_autocal[ant] = 0;
985
  }
knopp's avatar
   
knopp committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004


  mlockall(MCL_CURRENT | MCL_FUTURE);

#ifdef RTAI
  // make main thread LXRT soft realtime
  task = rt_task_init_schmod(nam2num("MYTASK"), 9, 0, 0, SCHED_FIFO, 0xF);

  // start realtime timer and scheduler
  //rt_set_oneshot_mode();
  rt_set_periodic_mode();
  start_rt_timer(0);

  //now = rt_get_time() + 10*PERIOD;
  //rt_task_make_periodic(task, now, PERIOD);

  printf("Init mutex\n");
  //mutex = rt_get_adr(nam2num("MUTEX"));
  mutex = rt_sem_init(nam2num("MUTEX"), 0);
1005
1006
1007
1008
  if (mutex==0) {
    printf("Error init mutex\n");
    exit(-1);
  } else {
knopp's avatar
   
knopp committed
1009
    printf("mutex=%p\n",mutex);
1010
  }
knopp's avatar
   
knopp committed
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
#endif

  DAQ_MBOX = (volatile unsigned int *) openair0_exmimo_pci[card].rxcnt_ptr[0];

  // this starts the DMA transfers
  openair0_start_rt_acquisition(card);


#ifdef XFORMS
  if (do_forms==1) {
    printf("Running XFORMS\n");
    fl_initialize (&argc, argv, NULL, 0, 0);
    form_stats = create_form_stats_form();
1024
    for(UE_id=0; UE_id<scope_enb_num_ue; UE_id++) {
knopp's avatar
   
knopp committed
1025
1026
      form_enb[UE_id] = create_lte_phy_scope_enb();
      sprintf (title, "UE%d LTE UL SCOPE eNB",UE_id+1);
1027
1028
      fl_show_form (form_enb[UE_id]->lte_phy_scope_enb, FL_PLACE_HOTSPOT,
                    FL_FULLBORDER, title);
knopp's avatar
   
knopp committed
1029
1030
    }
    fl_show_form (form_stats->stats_form, FL_PLACE_HOTSPOT, FL_FULLBORDER, "stats");
1031
    for (UE_id=0; UE_id<scope_enb_num_ue; UE_id++) {
knopp's avatar
   
knopp committed
1032
      if (otg_enabled) {
1033
1034
1035
1036
1037
        fl_set_button(form_enb[UE_id]->button_0,1);
        fl_set_object_label(form_enb[UE_id]->button_0,"DL Traffic ON");
      } else {
        fl_set_button(form_enb[UE_id]->button_0,0);
        fl_set_object_label(form_enb[UE_id]->button_0,"DL Traffic OFF");
knopp's avatar
   
knopp committed
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
      }
    }

    ret = pthread_create(&thread2, NULL, scope_thread, NULL);
    printf("Scope thread created, ret=%d\n",ret);
  }
#endif

  rt_sleep_ns(10*FRAME_PERIOD);

#ifndef RTAI
  pthread_attr_init (&attr_dlsch_threads);
  pthread_attr_setstacksize(&attr_dlsch_threads,OPENAIR_THREAD_STACK_SIZE);
  //attr_dlsch_threads.priority = 1;
1052
1053
  sched_param_dlsch.sched_priority = sched_get_priority_max(
                                       SCHED_FIFO); //OPENAIR_THREAD_PRIORITY;
knopp's avatar
   
knopp committed
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
  pthread_attr_setschedparam  (&attr_dlsch_threads, &sched_param_dlsch);
  pthread_attr_setschedpolicy (&attr_dlsch_threads, SCHED_FIFO);
#endif

  // start the main thread
#ifdef RTAI
  thread0 = rt_thread_create(eNB_thread, NULL, 10000000);
  rt_sleep_ns(FRAME_PERIOD*10);
  rt_send(rt_get_adr(nam2num("TASK0")), (unsigned long)mutex);
#else
  error_code = pthread_create(&thread0, &attr_dlsch_threads, eNB_thread, NULL);
  if (error_code!= 0) {
1066
1067
    LOG_D(HW,"[lte-softmodem.c] Could not allocate eNB_thread, error %d\n",
          error_code);
knopp's avatar
   
knopp committed
1068
    return(error_code);
1069
  } else {
knopp's avatar
   
knopp committed
1070
1071
1072
1073
    LOG_D(HW,"[lte-softmodem.c] Allocate eNB_thread successful\n");
  }
#endif

1074
1075
1076
// #ifdef OPENAIR2
//   init_pdcp_thread(1);
// #endif
knopp's avatar
   
knopp committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088

#ifdef ULSCH_THREAD
  init_ulsch_threads();
#endif
  printf("eNB threads created\n");


  // wait for end of program
  printf("TYPE <CTRL-C> TO TERMINATE\n");
  getchar();
  //while (oai_exit==0)
  //  rt_sleep_ns(FRAME_PERIOD);
1089
1090

#if defined(ENABLE_ITTI)
winckel's avatar
winckel committed
1091
  printf("Entering ITTI signals handler\n");
1092
1093
  itti_wait_tasks_end();
#else
knopp's avatar
   
knopp committed
1094
  rt_sem_wait(mutex);
1095
#endif
knopp's avatar
   
knopp committed
1096
1097
1098
1099

  // stop threads
#ifdef XFORMS
  printf("waiting for XFORMS thread\n");
1100
1101
1102
1103
1104
1105
1106
  if (do_forms==1) {
    pthread_join(thread2,&status);
    fl_hide_form(form_stats->stats_form);
    fl_free_form(form_stats->stats_form);
    for(UE_id=0; UE_id<scope_enb_num_ue; UE_id++) {
      fl_hide_form(form_enb[UE_id]->lte_phy_scope_enb);
      fl_free_form(form_enb[UE_id]->lte_phy_scope_enb);
knopp's avatar
   
knopp committed
1107
    }
1108
  }
knopp's avatar
   
knopp committed
1109
1110
1111
1112
1113
#endif

  printf("stopping MODEM threads\n");
  // cleanup
#ifdef RTAI
1114
  rt_thread_join(thread0);
knopp's avatar
   
knopp committed
1115
#else
1116
  pthread_join(thread0,&status);
knopp's avatar
   
knopp committed
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
#endif
#ifdef ULSCH_THREAD
  cleanup_ulsch_threads();
#endif

#ifdef RTAI
  rt_sem_delete(mutex);
  stop_rt_timer();
  rt_task_delete(task);
#endif

  printf("stopping card\n");
  openair0_stop(card);
  printf("closing openair0_lib\n");
  openair0_close();



1135
  if (ouput_vcd) {
knopp's avatar
   
knopp committed
1136
    vcd_signal_dumper_close();
1137
  }
knopp's avatar
   
knopp committed
1138
1139
1140
1141
1142
1143

  logClean();

  return 0;
}

1144
1145
1146
void setup_eNB_buffers(PHY_VARS_eNB *phy_vars_eNB,
                       LTE_DL_FRAME_PARMS *frame_parms, int carrier)
{
knopp's avatar
   
knopp committed
1147
1148
1149
1150

  int i,j;
  u16 N_TA_offset = 0;

1151
  if (frame_parms->frame_type == TDD) {
knopp's avatar
   
knopp committed
1152
    N_TA_offset = 624/4;
1153
  }
knopp's avatar
   
knopp committed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164

  if (phy_vars_eNB) {
    if ((frame_parms->nb_antennas_rx>1) && (carrier>0)) {
      printf("RX antennas > 1 and carrier > 0 not possible\n");
      exit(-1);
    }

    if ((frame_parms->nb_antennas_tx>1) && (carrier>0)) {
      printf("TX antennas > 1 and carrier > 0 not possible\n");
      exit(-1);
    }
1165

knopp's avatar
   
knopp committed
1166
    // replace RX signal buffers with mmaped HW versions
1167
    for (i=0; i<frame_parms->nb_antennas_rx; i++) {
knopp's avatar
   
knopp committed
1168
      free(phy_vars_eNB->lte_eNB_common_vars.rxdata[0][i]);
1169
1170
1171
1172
      phy_vars_eNB->lte_eNB_common_vars.rxdata[0][i] = ((s32 *)
          openair0_exmimo_pci[card].adc_head[i+carrier]) -
          N_TA_offset; // N_TA offset for TDD

knopp's avatar
   
knopp committed
1173
1174
1175
      /*
        printf("rxdata[%d] @ %p\n",i,phy_vars_eNB->lte_eNB_common_vars.rxdata[0][i]);
        for (j=0;j<16;j++) {
1176
1177
      printf("rxbuffer %d: %x\n",j,phy_vars_eNB->lte_eNB_common_vars.rxdata[0][i][j]);
      phy_vars_eNB->lte_eNB_common_vars.rxdata[0][i][j] = 16-j;
knopp's avatar
   
knopp committed
1178
1179
1180
        }
      */
    }
1181
    for (i=0; i<frame_parms->nb_antennas_tx; i++) {
knopp's avatar
   
knopp committed
1182
      free(phy_vars_eNB->lte_eNB_common_vars.txdata[0][i]);
1183
1184
1185
      phy_vars_eNB->lte_eNB_common_vars.txdata[0][i] = (s32 *)
          openair0_exmimo_pci[card].dac_head[i+carrier];

knopp's avatar
   
knopp committed
1186
1187
1188
      /*
        printf("txdata[%d] @ %p\n",i,phy_vars_eNB->lte_eNB_common_vars.txdata[0][i]);
        for (j=0;j<16;j++) {
1189
1190
1191
      printf("txbuffer %d: %x\n",j,phy_vars_eNB->lte_eNB_common_vars.txdata[0][i][j]);
      phy_vars_eNB->lte_eNB_common_vars.txdata[0][i][j] = 16-j;
      }
knopp's avatar
   
knopp committed
1192
1193
1194
1195
      */
    }
  }
}