lms_lib.cpp 23.9 KB
Newer Older
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
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that 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 OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.

   Contact Information
   OpenAirInterface Admin: openair_admin@eurecom.fr
   OpenAirInterface Tech : openair_tech@eurecom.fr
   OpenAirInterface Dev  : openair4g-devel@eurecom.fr

   Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE

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


#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <unistd.h>
#include <errno.h>

#include "common_lib.h"
#include "LMS_SDR.h"
#include "LMS7002M.h"
#include "Si5351C.h"
#include "LMS_StreamBoard.h"
#include "LMS7002M_RegistersMap.h"

#include <cmath>


///define for parameter enumeration if prefix might be needed
#define LMS7param(id) id

LMScomms* usbport;
LMScomms* comport;
LMS7002M* lms7;
Si5351C* Si;
LMS_StreamBoard *lmsStream;

#define RXDCLENGTH 4096
#define NUMBUFF 40
int16_t cos_fsover8[8]  = {2047,   1447,      0,  -1448,  -2047,  -1448,     0,   1447};
int16_t cos_3fsover8[8] = {2047,  -1448,      0,   1447,  -2047,   1447,     0,  -1448};

extern "C"
{
int write_output(const char *fname,const char *vname,void *data,int length,int dec,char format);
}

int trx_lms_write(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps, int antenna_id, int flags) {
  
 LMS_TRxWrite((int16_t*)buff[0], nsamps,0, timestamp);

 return nsamps;
}


int trx_lms_read(openair0_device *device, openair0_timestamp *ptimestamp, void **buff, int nsamps, int antenna_id) {
    
  uint64_t timestamp;
  int16_t *dst_ptr = (int16_t*) buff[0];
  int ret;
Aikaterini's avatar
Aikaterini committed
85
  ret = LMS_TRxRead(dst_ptr, nsamps,0,&timestamp, 10);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 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 147 148 149 150 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 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 507 508 509 510 511 512 513 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 572 573
  *ptimestamp=timestamp;

  return ret;   
}
void set_rx_gain_offset(openair0_config_t *openair0_cfg, int chain_index) {

  int i=0;
  // loop through calibration table to find best adjustment factor for RX frequency
  double min_diff = 6e9,diff;

  while (openair0_cfg->rx_gain_calib_table[i].freq>0) {
    diff = fabs(openair0_cfg->rx_freq[chain_index] - openair0_cfg->rx_gain_calib_table[i].freq);
    printf("cal %d: freq %f, offset %f, diff %f\n",
	   i,
	   openair0_cfg->rx_gain_calib_table[i].freq,
	   openair0_cfg->rx_gain_calib_table[i].offset,diff);
    if (min_diff > diff) {
      min_diff = diff;
      openair0_cfg->rx_gain_offset[chain_index] = openair0_cfg->rx_gain_calib_table[i].offset;
    }
    i++;
  }
  
}
/*
void calibrate_rf(openair0_device *device) {

  openair0_timestamp ptimestamp;
  int16_t *calib_buffp,*calib_tx_buffp;
  int16_t calib_buff[2*RXDCLENGTH];
  int16_t calib_tx_buff[2*RXDCLENGTH];
  int i,j;
  int8_t offI,offQ,offIold,offQold,offInew,offQnew,offphase,offphaseold,offphasenew,offgain,offgainold,offgainnew;
  int32_t meanI,meanQ,meanIold,meanQold;
  int cnt=0,loop;
  liblms7_status opStatus;
  int16_t dcoffi;
  int16_t dcoffq;
  int16_t dccorri;
  int16_t dccorrq;
    const int16_t firCoefs[] =
    {
        -2531,
        -517,
        2708,
        188,
        -3059,
        216,
        3569,
        -770,
        -4199,
        1541,
        4886,
        -2577,
        -5552,
        3909,
        6108,
        -5537,
        -6457,
        7440,
        6507,
        -9566,
        -6174,
        11845,
        5391,
        -14179,
        -4110,
        16457,
        2310,
        -18561,
        0,
        20369,
        -2780,
        -21752,
        5963,
        22610,
        -9456,
        -22859,
        13127,
        22444,
        -16854,
        -21319,
        20489,
        19492,
        -23883,
        -17002,
        26881,
        13902,
        -29372,
        -10313,
        31226,
        6345,
        -32380,
        -2141,
        32767,
        -2141,
        -32380,
        6345,
        31226,
        -10313,
        -29372,
        13902,
        26881,
        -17002,
        -23883,
        19492,
        20489,
        -21319,
        -16854,
        22444,
        13127,
        -22859,
        -9456,
        22610,
        5963,
        -21752,
        -2780,
        20369,
        0,
        -18561,
        2310,
        16457,
        -4110,
        -14179,
        5391,
        11845,
        -6174,
        -9566,
        6507,
        7440,
        -6457,
        -5537,
        6108,
        3909,
        -5552,
        -2577,
        4886,
        1541,
        -4199,
        -770,
        3569,
        216,
        -3059,
        188,
        2708,
        -517,
        -2531
    };

  j=0;
  for (i=0;i<RXDCLENGTH;i++) {
    calib_tx_buff[j++] = cos_fsover8[i&7];
    calib_tx_buff[j++] = cos_fsover8[(i+6)&7];  // sin
  }
  calib_buffp = &calib_buff[0];
  calib_tx_buffp = &calib_tx_buff[0];

  lms7->BackupAllRegisters();
  uint8_t ch = (uint8_t)lms7->Get_SPI_Reg_bits(LMS7param(MAC));
  //Stage 1
  uint8_t sel_band1_trf = (uint8_t)lms7->Get_SPI_Reg_bits(LMS7param(SEL_BAND1_TRF));
  uint8_t sel_band2_trf = (uint8_t)lms7->Get_SPI_Reg_bits(LMS7param(SEL_BAND2_TRF));
  {
    uint16_t requiredRegs[] = { 0x0400, 0x040A, 0x010D, 0x040C };
    uint16_t requiredMask[] = { 0x6000, 0x3007, 0x0040, 0x00FF }; //CAPSEL, AGC_MODE, AGC_AVG, EN_DCOFF, Bypasses
    uint16_t requiredValue[] = { 0x0000, 0x1007, 0x0040, 0x00BD };
    
    lms7->Modify_SPI_Reg_mask(requiredRegs, requiredMask, requiredValue, 0, 3);
  }

  //  opStatus = lms7->SetFrequencySX(LMS7002M::Rx, device->openair0_cfg[0].tx_freq[0]/1e6,30.72);
  // put TX on fs/4
  opStatus = lms7->CalibrateRxSetup(device->openair0_cfg[0].sample_rate/1e6);
  if (opStatus != LIBLMS7_SUCCESS) {
    printf("Cannot calibrate for %f MHz\n",device->openair0_cfg[0].sample_rate/1e6);
    exit(-1);
  }
    // fill TX buffer with fs/8 complex sinusoid
  offIold=offQold=64;
  lms7->SetRxDCOFF(offIold,offQold);
  LMS_RxStart();  
  for (i=0;i<NUMBUFF;i++)
    trx_lms_read(device, &ptimestamp, (void **)&calib_buffp, RXDCLENGTH, 0);

  for (meanIold=meanQold=i=j=0;i<RXDCLENGTH;i++) {
    meanIold+=calib_buff[j++];
    meanQold+=calib_buff[j++];
  }
  meanIold/=RXDCLENGTH;
  meanQold/=RXDCLENGTH;
  printf("[LMS] RX DC: (%d,%d) => (%d,%d)\n",offIold,offQold,meanIold,meanQold);

  offI=offQ=-64;
  lms7->SetRxDCOFF(offI,offQ);

  for (i=0;i<NUMBUFF;i++)
    trx_lms_read(device, &ptimestamp, (void **)&calib_buffp, RXDCLENGTH, 0);

  for (meanI=meanQ=i=j=0;i<RXDCLENGTH;i++) {
    meanI+=calib_buff[j++];
    meanQ+=calib_buff[j++];
  }
  meanI/=RXDCLENGTH;
  meanQ/=RXDCLENGTH;
  printf("[LMS] RX DC: (%d,%d) => (%d,%d)\n",offI,offQ,meanI,meanQ);

  while (cnt++ < 7) {

    offInew=(offIold+offI)>>1;
    offQnew=(offQold+offQ)>>1;

    if (meanI*meanI < meanIold*meanIold) {
      meanIold = meanI;
      offIold = offI;
      printf("[LMS] *** RX DC: offI %d => %d\n",offIold,meanI);
    }
    if (meanQ*meanQ < meanQold*meanQold) {
      meanQold = meanQ;
      offQold = offQ;
      printf("[LMS] *** RX DC: offQ %d => %d\n",offQold,meanQ);
    }
    offI = offInew;
    offQ = offQnew;

    lms7->SetRxDCOFF(offI,offQ);

    for (i=0;i<NUMBUFF;i++)
      trx_lms_read(device, &ptimestamp, (void **)&calib_buffp, RXDCLENGTH, 0);

    for (meanI=meanQ=i=j=0;i<RXDCLENGTH;i++) {
      meanI+=calib_buff[j++];
      meanQ+=calib_buff[j++];
    }
    meanI/=RXDCLENGTH;
    meanQ/=RXDCLENGTH;
    printf("[LMS] RX DC: (%d,%d) => (%d,%d)\n",offI,offQ,meanI,meanQ);
  }

  if (meanI*meanI < meanIold*meanIold) {
    meanIold = meanI;
    offIold = offI;
    printf("[LMS] *** RX DC: offI %d => %d\n",offIold,meanI);
  }
  if (meanQ*meanQ < meanQold*meanQold) {
    meanQold = meanQ;
    offQold = offQ;
    printf("[LMS] *** RX DC: offQ %d => %d\n",offQold,meanQ);
  }
  
  printf("[LMS] RX DC: (%d,%d) => (%d,%d)\n",offIold,offQold,meanIold,meanQold);
  
  lms7->SetRxDCOFF(offIold,offQold);

  dcoffi = offIold;
  dcoffq = offQold;

  lms7->Modify_SPI_Reg_bits(LMS7param(MAC), ch);
  lms7->Modify_SPI_Reg_bits(LMS7param(AGC_MODE_RXTSP), 1);
  lms7->Modify_SPI_Reg_bits(LMS7param(CAPSEL), 0);

  // TX LO leakage
  offQold=offIold=127;
  lms7->SPI_write(0x0204,(((int16_t)offIold)<<7)|offQold);

  {
    uint16_t requiredRegs[] = { 0x0400, 0x040A, 0x010D, 0x040C };
    uint16_t requiredMask[] = { 0x6000, 0x3007, 0x0040, 0x00FF }; //CAPSEL, AGC_MODE, AGC_AVG, EN_DCOFF, Bypasses
    uint16_t requiredValue[] = { 0x0000, 0x1007, 0x0040, 0x00BD };
    
    lms7->Modify_SPI_Reg_mask(requiredRegs, requiredMask, requiredValue, 0, 3);
  }
  sel_band1_trf = (uint8_t)lms7->Get_SPI_Reg_bits(LMS7param(SEL_BAND1_TRF));
  sel_band2_trf = (uint8_t)lms7->Get_SPI_Reg_bits(LMS7param(SEL_BAND2_TRF));
  //B
  lms7->Modify_SPI_Reg_bits(0x0100, 0, 0, 1); //EN_G_TRF 1
  if (sel_band1_trf == 1)
    {
      lms7->Modify_SPI_Reg_bits(LMS7param(PD_RLOOPB_1_RFE), 0); //PD_RLOOPB_1_RFE 0
      lms7->Modify_SPI_Reg_bits(LMS7param(EN_INSHSW_LB1_RFE), 0); //EN_INSHSW_LB1 0
    }
  if (sel_band2_trf == 1)
    {
      lms7->Modify_SPI_Reg_bits(LMS7param(PD_RLOOPB_2_RFE), 0); //PD_RLOOPB_2_RFE 0
      lms7->Modify_SPI_Reg_bits(LMS7param(EN_INSHSW_LB2_RFE), 0); // EN_INSHSW_LB2 0
    }
  //  FixRXSaturation();
  
  lms7->Modify_SPI_Reg_bits(LMS7param(GFIR3_BYP_RXTSP), 0); //GFIR3_BYP 0
  lms7->Modify_SPI_Reg_bits(LMS7param(HBD_OVR_RXTSP), 2);
  lms7->Modify_SPI_Reg_bits(LMS7param(GFIR3_L_RXTSP), 7);
  lms7->Modify_SPI_Reg_bits(LMS7param(GFIR3_N_RXTSP), 7);
  
  lms7->SetGFIRCoefficients(LMS7002M::Rx, 2, firCoefs, sizeof(firCoefs) / sizeof(int16_t));
    
  for (i=0;i<NUMBUFF;i++) {
    trx_lms_read(device, &ptimestamp, (void **)&calib_buffp, RXDCLENGTH, 0);
    trx_lms_write(device,ptimestamp+5*RXDCLENGTH, (void **)&calib_tx_buffp, RXDCLENGTH, 0, 0);
  }

  write_output("calibrx.m","rxs",calib_buffp,RXDCLENGTH,1,1);
  exit(-1);
  for (meanIold=meanQold=i=j=0;i<RXDCLENGTH;i++) {
    switch (i&3) {
    case 0:
      meanIold+=calib_buff[j++];
      break;
    case 1:
      meanQold+=calib_buff[j++];
      break;
    case 2:
      meanIold-=calib_buff[j++];
      break;
    case 3:
      meanQold-=calib_buff[j++];
      break;
    }
  }
  //  meanIold/=RXDCLENGTH;
  //  meanQold/=RXDCLENGTH;
  printf("[LMS] TX DC (offI): %d => (%d,%d)\n",offIold,meanIold,meanQold);

  offI=-128;
  lms7->SPI_write(0x0204,(((int16_t)offI)<<7)|offQold);

  for (i=0;i<NUMBUFF;i++) {
    trx_lms_read(device, &ptimestamp, (void **)&calib_buffp, RXDCLENGTH, 0);
    trx_lms_write(device,ptimestamp+5*RXDCLENGTH, (void **)&calib_tx_buffp, RXDCLENGTH, 0, 0);
  }

  for (meanI=meanQ=i=j=0;i<RXDCLENGTH;i++) {
    switch (i&3) {
    case 0:
      meanI+=calib_buff[j++];
      break;
    case 1:
      meanQ+=calib_buff[j++];
      break;
    case 2:
      meanI-=calib_buff[j++];
      break;
    case 3:
      meanQ-=calib_buff[j++];
      break;
    }
  }
  //  meanI/=RXDCLENGTH;
  //  meanQ/=RXDCLENGTH;
  printf("[LMS] TX DC (offI): %d => (%d,%d)\n",offI,meanI,meanQ);
  cnt = 0;
  while (cnt++ < 8) {

    offInew=(offIold+offI)>>1;
    if (meanI*meanI+meanQ*meanQ < meanIold*meanIold +meanQold*meanQold) {
      printf("[LMS] TX DC (offI): ([%d,%d]) => %d : %d\n",offIold,offI,offInew,meanI*meanI+meanQ*meanQ);
      meanIold = meanI;
      meanQold = meanQ;
      offIold = offI;
    }
    offI = offInew;
    lms7->SPI_write(0x0204,(((int16_t)offI)<<7)|offQold);

    for (i=0;i<NUMBUFF;i++) {
      trx_lms_read(device, &ptimestamp, (void **)&calib_buffp, RXDCLENGTH, 0);
      trx_lms_write(device,ptimestamp+5*RXDCLENGTH, (void **)&calib_tx_buffp, RXDCLENGTH, 0, 0);
    }

    for (meanI=meanQ=i=j=0;i<RXDCLENGTH;i++) {
      switch (i&3) {
      case 0:
	meanI+=calib_buff[j++];
	break;
      case 1:
	meanQ+=calib_buff[j++];
	break;
      case 2:
	meanI-=calib_buff[j++];
	break;
      case 3:
	meanQ-=calib_buff[j++];
	break;
      }
    }
    //    meanI/=RXDCLENGTH;
    //   meanQ/=RXDCLENGTH;
    //    printf("[LMS] TX DC (offI): %d => (%d,%d)\n",offI,meanI,meanQ);
  }


  if (meanI*meanI+meanQ*meanQ < meanIold*meanIold +meanQold*meanQold) {
    printf("[LMS] TX DC (offI): ([%d,%d]) => %d : %d\n",offIold,offI,offInew,meanI*meanI+meanQ*meanQ);
    meanIold = meanI;
    meanQold = meanQ;
    offIold = offI;
  }
  offQ=-128;
  lms7->SPI_write(0x0204,(((int16_t)offIold)<<7)|offQ);

  for (i=0;i<NUMBUFF;i++) {
    trx_lms_read(device, &ptimestamp, (void **)&calib_buffp, RXDCLENGTH, 0);
    trx_lms_write(device,ptimestamp+5*RXDCLENGTH, (void **)&calib_tx_buffp, RXDCLENGTH, 0, 0);
  }

  for (meanI=meanQ=i=j=0;i<RXDCLENGTH;i++) {
    switch (i&3) {
    case 0:
      meanI+=calib_buff[j++];
      break;
    case 1:
      meanQ+=calib_buff[j++];
      break;
    case 2:
      meanI-=calib_buff[j++];
      break;
    case 3:
      meanQ-=calib_buff[j++];
      break;
    }
  }
  //  meanI/=RXDCLENGTH;
  //  meanQ/=RXDCLENGTH;
  printf("[LMS] TX DC (offQ): %d => (%d,%d)\n",offQ,meanI,meanQ);

  cnt=0;
  while (cnt++ < 8) {

    offQnew=(offQold+offQ)>>1;
    if (meanI*meanI+meanQ*meanQ < meanIold*meanIold +meanQold*meanQold) {
      printf("[LMS] TX DC (offQ): ([%d,%d]) => %d : %d\n",offQold,offQ,offQnew,meanI*meanI+meanQ*meanQ);

      meanIold = meanI;
      meanQold = meanQ;
      offQold = offQ;
    }
    offQ = offQnew;
    lms7->SPI_write(0x0204,(((int16_t)offIold)<<7)|offQ);


    for (i=0;i<NUMBUFF;i++) {
      trx_lms_read(device, &ptimestamp, (void **)&calib_buffp, RXDCLENGTH, 0);
      trx_lms_write(device,ptimestamp+5*RXDCLENGTH, (void **)&calib_tx_buffp, RXDCLENGTH, 0, 0);
    }

    for (meanI=meanQ=i=j=0;i<RXDCLENGTH;i++) {
      switch (i&3) {
      case 0:
	meanI+=calib_buff[j++];
	break;
      case 1:
	meanQ+=calib_buff[j++];
	break;
      case 2:
	meanI-=calib_buff[j++];
	break;
      case 3:
	meanQ-=calib_buff[j++];
	break;
      }
    }
    //    meanI/=RXDCLENGTH;
    //   meanQ/=RXDCLENGTH;
    //    printf("[LMS] TX DC (offQ): %d => (%d,%d)\n",offQ,meanI,meanQ);
  }

  LMS_RxStop();

  printf("[LMS] TX DC: (%d,%d) => (%d,%d)\n",offIold,offQold,meanIold,meanQold);

  dccorri = offIold;
  dccorrq = offQold;
  
  
  lms7->RestoreAllRegisters();
  lms7->Modify_SPI_Reg_bits(LMS7param(MAC), ch);

  lms7->Modify_SPI_Reg_bits(LMS7param(DCOFFI_RFE), dcoffi);
  lms7->Modify_SPI_Reg_bits(LMS7param(DCOFFQ_RFE), dcoffq);
  lms7->Modify_SPI_Reg_bits(LMS7param(DCCORRI_TXTSP), dccorri);
  lms7->Modify_SPI_Reg_bits(LMS7param(DCCORRQ_TXTSP), dccorrq);
  //  lms7->Modify_SPI_Reg_bits(LMS7param(GCORRI_TXTSP), gcorri);
  //  lms7->Modify_SPI_Reg_bits(LMS7param(GCORRQ_TXTSP), gcorrq);
  //  lms7->Modify_SPI_Reg_bits(LMS7param(IQCORR_TXTSP), iqcorr);
  
  //  lms7->Modify_SPI_Reg_bits(LMS7param(DC_BYP_TXTSP), 0); //DC_BYP
  lms7->Modify_SPI_Reg_bits(0x0208, 1, 0, 0); //GC_BYP PH_BYP
  
}
*/

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
int trx_lms_set_gains(openair0_device* device, openair0_config_t *openair0_cfg) {

  double gv = openair0_cfg[0].rx_gain[0] - openair0_cfg[0].rx_gain_offset[0];

  if (gv > 31) {
    printf("RX Gain 0 too high, reduce by %f dB\n",gv-31);
    gv = 31;
  }
  if (gv < 0) {
    printf("RX Gain 0 too low, increase by %f dB\n",-gv);
    gv = 0;
  }
  printf("[LMS] Setting 7002M G_PGA_RBB to %d\n", (int16_t)gv);
  lms7->Modify_SPI_Reg_bits(LMS7param(G_PGA_RBB),(int16_t)gv);

  return(0);
}

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
int trx_lms_start(openair0_device *device){
 

 LMS_Init(0, 128*1024);   
 usbport = LMS_GetUSBPort();
 //connect data stream port
  LMS_UpdateDeviceList(usbport);
  const char *name = LMS_GetDeviceName(usbport, 0);
  printf("Connecting to device: %s\n",name);



  if (LMS_DeviceOpen(usbport, 0)==0)
  {
    Si = new Si5351C();
    lms7 = new LMS7002M(usbport);
    liblms7_status opStatus;

    printf("Configuring Si5351C\n");
    Si->Initialize(usbport);
    Si->SetPLL(0, 25000000, 0);
    Si->SetPLL(1, 25000000, 0);
    Si->SetClock(0, 27000000, true, false);
    Si->SetClock(1, 27000000, true, false);
    for (int i = 2; i < 8; ++i)
      Si->SetClock(i, 27000000, false, false);
    Si5351C::Status status = Si->ConfigureClocks();
    if (status != Si5351C::SUCCESS)
      {
	printf("Failed to configure Si5351C");
	exit(-1);
      }
    status = Si->UploadConfiguration();
    if (status != Si5351C::SUCCESS)
      printf("Failed to upload Si5351C configuration");
    


    lms7->ResetChip();

    opStatus = lms7->LoadConfig(device->openair0_cfg[0].configFilename);
    
    if (opStatus != LIBLMS7_SUCCESS) {
      printf("Failed to load configuration file %s\n",device->openair0_cfg[0].configFilename);
      exit(-1);
    }
    opStatus = lms7->UploadAll();

    if (opStatus != LIBLMS7_SUCCESS) {
      printf("Failed to upload configuration file\n");
      exit(-1);
    }

    // Set TX filter
    
    printf("Tuning TX filter\n");
648
    opStatus = lms7->TuneTxFilter(LMS7002M::TxFilter::TX_HIGHBAND,device->openair0_cfg[0].tx_bw/1e6);
649 650

    if (opStatus != LIBLMS7_SUCCESS) {
651
      printf("Warning: Could not tune TX filter to %f MHz\n",device->openair0_cfg[0].tx_bw/1e6);
652 653 654
    }
    
    printf("Tuning RX filter\n");
655 656
    
    opStatus = lms7->TuneRxFilter(LMS7002M::RxFilter::RX_LPF_LOWBAND,device->openair0_cfg[0].rx_bw/1e6);
657 658

    if (opStatus != LIBLMS7_SUCCESS) {
659
      printf("Warning: Could not tune RX filter to %f MHz\n",device->openair0_cfg[0].rx_bw/1e6);
660 661
    }

662
    /*    printf("Tuning TIA filter\n");
663 664 665 666
    opStatus = lms7->TuneRxFilter(LMS7002M::RxFilter::RX_TIA,7.0);

    if (opStatus != LIBLMS7_SUCCESS) {
      printf("Warning: Could not tune RX TIA filter\n");
667
      }*/
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

    opStatus = lms7->SetInterfaceFrequency(lms7->GetFrequencyCGEN_MHz(), 
					   lms7->Get_SPI_Reg_bits(HBI_OVR_TXTSP), 
					   lms7->Get_SPI_Reg_bits(HBD_OVR_RXTSP));
    if (opStatus != LIBLMS7_SUCCESS) {
      printf("SetInterfaceFrequency failed: %f,%d,%d\n",
	     lms7->GetFrequencyCGEN_MHz(), 
	     lms7->Get_SPI_Reg_bits(HBI_OVR_TXTSP), 
	     lms7->Get_SPI_Reg_bits(HBD_OVR_RXTSP));
    }
    else {
      printf("SetInterfaceFrequency as %f,%d,%d\n",
	     lms7->GetFrequencyCGEN_MHz(), 
	     lms7->Get_SPI_Reg_bits(HBI_OVR_TXTSP), 
	     lms7->Get_SPI_Reg_bits(HBD_OVR_RXTSP));
    } 
    lmsStream = new LMS_StreamBoard(usbport);    
    LMS_StreamBoard::Status opStreamStatus; 
    // this will configure that sampling rate at output of FPGA
687 688 689
    opStreamStatus = lmsStream->ConfigurePLL(usbport,
					     device->openair0_cfg[0].sample_rate,
					     device->openair0_cfg[0].sample_rate,90);
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    if (opStatus != LIBLMS7_SUCCESS){
      printf("Sample rate programming failed\n");
      exit(-1);
    }

    opStatus = lms7->SetFrequencySX(LMS7002M::Tx, device->openair0_cfg[0].tx_freq[0]/1e6,30.72);

    if (opStatus != LIBLMS7_SUCCESS) {
      printf("Cannot set TX frequency %f MHz\n",device->openair0_cfg[0].tx_freq[0]/1e6);
      exit(-1);
    }
    else {
      printf("Set TX frequency %f MHz\n",device->openair0_cfg[0].tx_freq[0]/1e6);
    }
    opStatus = lms7->SetFrequencySX(LMS7002M::Rx, device->openair0_cfg[0].rx_freq[0]/1e6,30.72);

    if (opStatus != LIBLMS7_SUCCESS) {
      printf("Cannot set RX frequency %f MHz\n",device->openair0_cfg[0].rx_freq[0]/1e6);
      exit(-1);
    }
    else {
      printf("Set RX frequency %f MHz\n",device->openair0_cfg[0].rx_freq[0]/1e6);
    }

714
    trx_lms_set_gains(device, device->openair0_cfg);
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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    // Run calibration procedure
    //    calibrate_rf(device);
    //lms7->CalibrateTx(5.0);
    LMS_RxStart();
  }
  else
  {
   return(-1);
  }
  
 //connect control port 

 /* comport = LMS_GetCOMPort();
  LMS_UpdateDeviceList(comport);
  name = LMS_GetDeviceName(comport, 0);
  if (*name == 0)
      comport = usbport;  //attempt to use data port 
  else
  {
    printf("Connecting to device: %s\n",name);
    if (LMS_DeviceOpen(comport, 0)!=0)
        return (-1);
  }
   lms7 = new LMS7002M(comport);
   if( access( "./config.ini", F_OK ) != -1 ) //load config file
   lms7->LoadConfig("./config.ini");
   //calibration takes too long
   //lms7->CalibrateRx(5.0);  
   //lms7->CalibrateTx(5.0);
 */

  return 0;
}


int trx_lms_stop(int card) {
  /*
  LMS_DeviceClose(usbport);
  LMS_DeviceClose(comport);
  delete lms7;
  return LMS_Destroy();
  */
}

int trx_lms_set_freq(openair0_device* device, openair0_config_t *openair0_cfg,int exmimo_dump_config) {
  //Control port must be connected 
   
  lms7->SetFrequencySX(LMS7002M::Tx,openair0_cfg->tx_freq[0]/1e6,30.72);
  lms7->SetFrequencySX(LMS7002M::Rx,openair0_cfg->rx_freq[0]/1e6,30.72);
  printf ("[LMS] rx frequency:%f;\n",openair0_cfg->rx_freq[0]/1e6);
  set_rx_gain_offset(openair0_cfg,0);
  return(0);
    
}

// 31 = 19 dB => 105 dB total gain @ 2.6 GHz
rx_gain_calib_table_t calib_table_sodera[] = {
  {3500000000.0,70.0},
  {2660000000.0,80.0},
  {2300000000.0,80.0},
775 776
  {1880000000.0,74.0},  // on W PAD
  {816000000.0,76.0},   // on W PAD
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
  {-1,0}};







int trx_lms_get_stats(openair0_device* device) {

  return(0);

}

int trx_lms_reset_stats(openair0_device* device) {

  return(0);

}

int openair0_set_gains(openair0_device* device, 
		       openair0_config_t *openair0_cfg) {

  return(0);
}

int openair0_set_frequencies(openair0_device* device, openair0_config_t *openair0_cfg, int dummy) {

  return(0);
}



void trx_lms_end(openair0_device *device) {


}

815 816 817 818 819 820
extern "C" {
/*! \brief Initialize Openair LMSSDR target. It returns 0 if OK
* \param device the hardware to use
* \param openair0_cfg RF frontend parameters set by application
*/
int device_init(openair0_device *device, openair0_config_t *openair0_cfg){
821

Aikaterini's avatar
Aikaterini committed
822
  device->type=LMSSDR_DEV;
823
  printf("LMSSDR: Initializing openair0_device for %s ...\n", ((device->host_type == BBU_HOST) ? "BBU": "RRH"));
824 825 826 827 828 829 830 831 832 833 834 835 836

  switch ((int)openair0_cfg[0].sample_rate) {
  case 30720000:
    // from usrp_time_offset
    openair0_cfg[0].samples_per_packet    = 2048;
    openair0_cfg[0].tx_sample_advance     = 15;
    openair0_cfg[0].tx_bw                 = 30.72e6;
    openair0_cfg[0].rx_bw                 = 30.72e6;
    openair0_cfg[0].tx_scheduling_advance = 8*openair0_cfg[0].samples_per_packet;
    break;
  case 15360000:
    openair0_cfg[0].samples_per_packet    = 2048;
    openair0_cfg[0].tx_sample_advance     = 45;
837 838 839
    openair0_cfg[0].tx_bw                 = 28e6;
    openair0_cfg[0].rx_bw                 = 10e6;
    openair0_cfg[0].tx_scheduling_advance = 8*openair0_cfg[0].samples_per_packet;
840 841 842 843
    break;
  case 7680000:
    openair0_cfg[0].samples_per_packet    = 1024;
    openair0_cfg[0].tx_sample_advance     = 70;
844 845
    openair0_cfg[0].tx_bw                 = 28e6;
    openair0_cfg[0].rx_bw                 = 5.0e6;
Aikaterini's avatar
Aikaterini committed
846
    openair0_cfg[0].tx_scheduling_advance = 8*openair0_cfg[0].samples_per_packet;
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    break;
  case 1920000:
    openair0_cfg[0].samples_per_packet    = 256;
    openair0_cfg[0].tx_sample_advance     = 50;
    openair0_cfg[0].tx_bw                 = 1.25e6;
    openair0_cfg[0].rx_bw                 = 1.25e6;
    openair0_cfg[0].tx_scheduling_advance = 8*openair0_cfg[0].samples_per_packet;
    break;
  default:
    printf("Error: unknown sampling rate %f\n",openair0_cfg[0].sample_rate);
    exit(-1);
    break;
  }

  openair0_cfg[0].rx_gain_calib_table = calib_table_sodera;
  set_rx_gain_offset(openair0_cfg,0);

  device->Mod_id           = 1;
  device->trx_start_func   = trx_lms_start;
  device->trx_write_func   = trx_lms_write;
  device->trx_read_func    = trx_lms_read;  
  device->trx_get_stats_func   = trx_lms_get_stats;
  device->trx_reset_stats_func = trx_lms_reset_stats;
  device->trx_end_func = trx_lms_end;
  device->trx_stop_func = trx_lms_stop;
  device->trx_set_freq_func = trx_lms_set_freq;
  device->trx_set_gains_func = trx_lms_set_gains;
  
  device->openair0_cfg = openair0_cfg;

  return 0;
}
879
}