iris_lib.cpp 19.1 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

/** iris_lib.cpp
 *
 * \author: Rahman Doost-Mohammady : doost@rice.edu
 */

#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <SoapySDR/Device.hpp>
#include <SoapySDR/Formats.hpp>
#include <SoapySDR/Time.hpp>
#include <boost/format.hpp>
#include <iostream>
#include <complex>
#include <fstream>
#include <cmath>
#include <time.h>
#include <limits>
#include "UTIL/LOG/log_extern.h"
#include "common_lib.h"

24
#define SAMPLE_RATE_DOWN 4
25

rubuntun's avatar
rubuntun committed
26
/*! \brief Iris Configuration */
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
typedef struct
{

  // --------------------------------
  // variables for Iris configuration
  // --------------------------------
  //! Iris device pointer
  SoapySDR::Device *iris;

  //create a send streamer and a receive streamer
  //! Iris TX Stream
  SoapySDR::Stream *txStream;
  //! Iris RX Stream
  SoapySDR::Stream *rxStream;

  //! Sampling rate
  double sample_rate;

  //! time offset between transmiter timestamp and receiver timestamp;
  double tdiff;

rubuntun's avatar
rubuntun committed
48
  //! TX forward samples.
49 50 51 52 53 54 55 56 57 58
  int tx_forward_nsamps; //166 for 20Mhz


  // --------------------------------
  // Debug and output control
  // --------------------------------
  //! Number of underflows
  int num_underflows;
  //! Number of overflows
  int num_overflows;
rubuntun's avatar
rubuntun committed
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  //! Number of sequential errors
  int num_seq_errors;
  //! tx count
  int64_t tx_count;
  //! rx count
  int64_t rx_count;
  //! timestamp of RX packet
  openair0_timestamp rx_timestamp;

} iris_state_t;

/*! \brief Called to start the Iris lime transceiver. Return 0 if OK, < 0 if error
    @param device pointer to the device structure specific to the RF hardware target
*/
static int trx_iris_start(openair0_device *device)
{
	iris_state_t *s = (iris_state_t*)device->priv;
rdoost's avatar
rdoost committed
77

78 79
	long long timeNs = s->iris->getHardwareTime("") + 500000;
	int flags = 0;
rdoost's avatar
rdoost committed
80
	//flags |= SOAPY_SDR_HAS_TIME;
81 82 83 84 85 86 87 88 89 90 91
	int ret = s->iris->activateStream(s->rxStream, flags, timeNs, 0);
	int ret2 = s->iris->activateStream(s->txStream);
	if (ret < 0 | ret2 < 0)
		return - 1;
	return 0;
}
/*! \brief Terminate operation of the Iris lime transceiver -- free all associated resources 
 * \param device the hardware to use
 */
static void trx_iris_end(openair0_device *device)
{
92
	LOG_I(HW,"Closing Iris device.\n");
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
	iris_state_t *s = (iris_state_t*)device->priv;
	s->iris->closeStream(s->txStream);
	s->iris->closeStream(s->rxStream);
	SoapySDR::Device::unmake(s->iris);
}

/*! \brief Called to send samples to the Iris RF target
      @param device pointer to the device structure specific to the RF hardware target
      @param timestamp The timestamp at whicch the first sample MUST be sent 
      @param buff Buffer which holds the samples
      @param nsamps number of samples to be sent
      @param antenna_id index of the antenna if the device has multiple anteannas
      @param flags flags must be set to TRUE if timestamp parameter needs to be applied
*/ 
static int trx_iris_write(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps, int cc, int flags)
{
	static long long int loop=0;
	static long time_min=0, time_max=0, time_avg=0;
	struct timespec tp_start, tp_end;
	long time_diff;

	int ret=0, ret_i=0;
	int flag = 0;
	iris_state_t *s = (iris_state_t*)device->priv;

	clock_gettime(CLOCK_MONOTONIC_RAW, &tp_start);
	if (flags)
		flag |= SOAPY_SDR_HAS_TIME;

122
	long long timeNs = SoapySDR::ticksToTimeNs(timestamp, s->sample_rate/SAMPLE_RATE_DOWN);
123 124 125 126 127 128 129 130 131 132 133 134
	int samples_sent = 0;
	uint32_t **samps = (uint32_t **)buff;
	while (samples_sent < nsamps)
	{
		ret = s->iris->writeStream(s->txStream, (void **)samps, (size_t)(nsamps - samples_sent), flag, timeNs, 100000);
		if (ret < 0) {
			printf("Unable to write stream!\n");
			break;
		}
		samples_sent += ret;
		samps[0] += ret;
		if (cc > 1)
rubuntun's avatar
rubuntun committed
135
			samps[1] += ret;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	}

	if (samples_sent != nsamps) {
		printf("[xmit] tx samples %d != %d\n",samples_sent,nsamps);
	}
	/*
	flag = 0;
	size_t channel = 0;
	ret = s->iris->readStreamStatus(s->txStream, channel, flag, timeNs, 0);
	if (ret == SOAPY_SDR_TIME_ERROR)
		printf("[xmit] Time Error in tx stream!\n");
	else if (ret == SOAPY_SDR_UNDERFLOW)
		printf("[xmit] Underflow occured!\n");
	else if (ret == SOAPY_SDR_TIMEOUT)
		printf("[xmit] Timeout occured!\n");
	else if (ret == SOAPY_SDR_STREAM_ERROR)
		printf("[xmit] Stream (tx) error occured!\n");
153
	*/	
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	return nsamps;
}

/*! \brief Receive samples from hardware.
 * Read \ref nsamps samples from each channel to buffers. buff[0] is the array for
 * the first channel. *ptimestamp is the time at which the first sample
 * was received.
 * \param device the hardware to use
 * \param[out] ptimestamp the time at which the first sample was received.
 * \param[out] buff An array of pointers to buffers for received samples. The buffers must be large enough to hold the number of samples \ref nsamps.
 * \param nsamps Number of samples. One sample is 2 byte I + 2 byte Q => 4 byte.
 * \param antenna_id Index of antenna for which to receive samples
 * \returns the number of sample read
*/
static int trx_iris_read(openair0_device *device, openair0_timestamp *ptimestamp, void **buff, int nsamps, int cc)
{
	int ret = 0;
rubuntun's avatar
rubuntun committed
171
	static long long nextTime;
172 173
	static bool nextTimeValid = false;
	iris_state_t *s = (iris_state_t*)device->priv;
rubuntun's avatar
rubuntun committed
174
	bool time_set = false;
175 176 177
	long long timeNs = 0;
	int flags = 0;
	int samples_received = 0;
178 179 180
	uint32_t *samps[2] = {(uint32_t *)buff[0], (uint32_t *)buff[1]}; //cws: it seems another thread can clobber these, so we need to save them locally.
	//printf("Reading %d samples from Iris...\n", nsamps);
	//fflush(stdout);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	while (samples_received < nsamps)
	{
		flags = 0;
		ret = s->iris->readStream(s->rxStream, (void **)samps, (size_t)(nsamps-samples_received), flags, timeNs, 100000);
		if (ret < 0)
		{
			if (ret == SOAPY_SDR_TIME_ERROR)
				printf("[recv] Time Error in tx stream!\n");
			else if (ret == SOAPY_SDR_OVERFLOW | (flags & SOAPY_SDR_END_ABRUPT))
				printf("[recv] Overflow occured!\n");
			else if (ret == SOAPY_SDR_TIMEOUT)
				printf("[recv] Timeout occured!\n");
			else if (ret == SOAPY_SDR_STREAM_ERROR)
				printf("[recv] Stream (tx) error occured!\n");
			else if (ret == SOAPY_SDR_CORRUPTION)
				printf("[recv] Bad packet occured!\n");
			break;
		}

		samples_received += ret;
		samps[0] += ret;
		if (cc > 1)
			samps[1] += ret;
rubuntun's avatar
rubuntun committed
204

205 206 207 208
		if (samples_received == ret) // first batch
		{
			if (flags & SOAPY_SDR_HAS_TIME)
			{
209
				s->rx_timestamp = SoapySDR::timeNsToTicks(timeNs, s->sample_rate/SAMPLE_RATE_DOWN);
rubuntun's avatar
rubuntun committed
210
				*ptimestamp = s->rx_timestamp;
211 212
				nextTime = timeNs;
				nextTimeValid = true;
rubuntun's avatar
rubuntun committed
213 214
				time_set = true;
				//printf("1) time set %llu \n", *ptimestamp);
215
			}
rubuntun's avatar
rubuntun committed
216
		}
217 218
	}

rubuntun's avatar
rubuntun committed
219
	if (samples_received < nsamps)
220 221
		printf("[recv] received %d samples out of %d\n",samples_received,nsamps);

rubuntun's avatar
rubuntun committed
222 223
	s->rx_count += samples_received;

224 225
	if (s->sample_rate != 0 && nextTimeValid)
	{
rubuntun's avatar
rubuntun committed
226 227
		if (!time_set)
		{
228
			s->rx_timestamp = SoapySDR::timeNsToTicks(nextTime, s->sample_rate/SAMPLE_RATE_DOWN);
rubuntun's avatar
rubuntun committed
229 230 231
			*ptimestamp = s->rx_timestamp;
			//printf("2) time set %llu, nextTime will be %llu \n", *ptimestamp, nextTime);
		}
232
		nextTime += SoapySDR::ticksToTimeNs(samples_received, s->sample_rate/SAMPLE_RATE_DOWN);
233 234 235 236 237 238 239 240
	}

	return samples_received;
}

/*! \brief Get current timestamp of Iris
 * \param device the hardware to use
*/
rubuntun's avatar
rubuntun committed
241
openair0_timestamp get_iris_time(openair0_device *device)
242 243 244
{
	iris_state_t *s = (iris_state_t*)device->priv;
	return SoapySDR::timeNsToTicks(s->iris->getHardwareTime(""), s->sample_rate);
rubuntun's avatar
rubuntun committed
245
}
246 247 248 249 250 251 252 253 254 255

/*! \brief Compares two variables within precision
 * \param a first variable
 * \param b second variable
*/
static bool is_equal(double a, double b)
{
	return std::fabs(a-b) < std::numeric_limits<double>::epsilon();
}

rubuntun's avatar
rubuntun committed
256
void *set_freq_thread(void *arg) {
257

rubuntun's avatar
rubuntun committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271
    openair0_device *device=(openair0_device *)arg;
    iris_state_t *s = (iris_state_t*)device->priv;
    int i;
    printf("Setting Iris TX Freq %f, RX Freq %f\n",device->openair0_cfg[0].tx_freq[0],device->openair0_cfg[0].rx_freq[0]);
    // add check for the number of channels in the cfg
    for(i=0; i < s->iris->getNumChannels(SOAPY_SDR_RX); i++) {
	    if (i < device->openair0_cfg[0].rx_num_channels)
            s->iris->setFrequency(SOAPY_SDR_RX, i, "RF", device->openair0_cfg[0].rx_freq[i]);
    }
    for(i=0; i < s->iris->getNumChannels(SOAPY_SDR_TX); i++) {
	    if (i < device->openair0_cfg[0].tx_num_channels)
            s->iris->setFrequency(SOAPY_SDR_TX, i, "RF", device->openair0_cfg[0].tx_freq[i]);
    }
}
272 273 274 275
/*! \brief Set frequencies (TX/RX)
 * \param device the hardware to use
 * \param openair0_cfg RF frontend parameters set by application
 * \param dummy dummy variable not used
rubuntun's avatar
rubuntun committed
276
 * \returns 0 in success
277
 */
rubuntun's avatar
rubuntun committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
int trx_iris_set_freq(openair0_device* device, openair0_config_t *openair0_cfg, int dont_block)
{
    iris_state_t *s = (iris_state_t*)device->priv;
    pthread_t f_thread;
    if (dont_block)
        pthread_create(&f_thread, NULL, set_freq_thread, (void*)device);
    else
    {
        int i;
        printf("Setting Iris TX Freq %f, RX Freq %f\n",openair0_cfg[0].tx_freq[0],openair0_cfg[0].rx_freq[0]);
    	for(i=0; i < s->iris->getNumChannels(SOAPY_SDR_RX); i++) {
	    	if (i < openair0_cfg[0].rx_num_channels) {
                s->iris->setFrequency(SOAPY_SDR_RX, i, "RF", openair0_cfg[0].rx_freq[i]);
            }
        }
    	for(i=0; i < s->iris->getNumChannels(SOAPY_SDR_TX); i++) {
	    	if (i < openair0_cfg[0].tx_num_channels) {
                s->iris->setFrequency(SOAPY_SDR_TX, i, "RF", openair0_cfg[0].tx_freq[i]);
            }
        }
	}
    return(0);
300 301 302 303 304 305
}


/*! \brief Set Gains (TX/RX)
 * \param device the hardware to use
 * \param openair0_cfg RF frontend parameters set by application
rubuntun's avatar
rubuntun committed
306
 * \returns 0 in success
307
 */
rubuntun's avatar
rubuntun committed
308
int trx_iris_set_gains(openair0_device* device,
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
		       openair0_config_t *openair0_cfg) {
	iris_state_t *s = (iris_state_t*)device->priv;
	s->iris->setGain(SOAPY_SDR_RX, 0, openair0_cfg[0].rx_gain[0]);
	s->iris->setGain(SOAPY_SDR_TX, 0, openair0_cfg[0].tx_gain[0]);
	s->iris->setGain(SOAPY_SDR_RX, 1, openair0_cfg[0].rx_gain[1]);
	s->iris->setGain(SOAPY_SDR_TX, 1, openair0_cfg[0].tx_gain[1]);
	return(0);
}

/*! \brief Stop Iris
 * \param card refers to the hardware index to use
 */
int trx_iris_stop(openair0_device* device) {
	iris_state_t *s = (iris_state_t*)device->priv;
	s->iris->deactivateStream(s->txStream);
	s->iris->deactivateStream(s->rxStream);
	return(0);
}

/*! \brief Iris RX calibration table */
rx_gain_calib_table_t calib_table_iris[] = {
330
  {3500000000.0,24},
331
  {2660000000.0,70},
332 333 334
  {2300000000.0,8},
  {1880000000.0,11},
  {816000000.0,4},
335 336 337
  {-1,0}};


rubuntun's avatar
rubuntun committed
338
/*! \brief Set RX gain offset
339 340
 * \param openair0_cfg RF frontend parameters set by application
 * \param chain_index RF chain to apply settings to
rubuntun's avatar
rubuntun committed
341
 * \returns 0 in success
342 343 344 345 346 347 348 349
 */
void set_rx_gain_offset(openair0_config_t *openair0_cfg, int chain_index,int bw_gain_adjust) {

  int i=0;
  // loop through calibration table to find best adjustment factor for RX frequency
  double min_diff = 6e9,diff,gain_adj=0.0;
  if (bw_gain_adjust==1) {
    switch ((int)openair0_cfg[0].sample_rate) {
rubuntun's avatar
rubuntun committed
350
    case 30720000:
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
      break;
    case 23040000:
      gain_adj=1.25;
      break;
    case 15360000:
      gain_adj=3.0;
      break;
    case 7680000:
      gain_adj=6.0;
      break;
    case 3840000:
      gain_adj=9.0;
      break;
    case 1920000:
      gain_adj=12.0;
      break;
    default:
      printf("unknown sampling rate %d\n",(int)openair0_cfg[0].sample_rate);
      exit(-1);
      break;
    }
  }
  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+gain_adj;
    }
    i++;
  }
rubuntun's avatar
rubuntun committed
385

386 387
}

rubuntun's avatar
rubuntun committed
388
/*! \brief print the Iris statistics
389 390 391 392 393 394 395 396 397
* \param device the hardware to use
* \returns  0 on success
*/
int trx_iris_get_stats(openair0_device* device) {

  return(0);

}

rubuntun's avatar
rubuntun committed
398
/*! \brief Reset the Iris statistics
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
* \param device the hardware to use
* \returns  0 on success
*/
int trx_iris_reset_stats(openair0_device* device) {

  return(0);

}



extern "C" {
/*! \brief Initialize Openair Iris 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) {
rubuntun's avatar
rubuntun committed
416

417 418 419 420 421 422 423 424
	size_t i;
	int bw_gain_adjust=0;
	openair0_cfg[0].rx_gain_calib_table = calib_table_iris;
	iris_state_t *s = (iris_state_t*)malloc(sizeof(iris_state_t));
	memset(s, 0, sizeof(iris_state_t));

	// Initialize Iris device
	device->openair0_cfg = openair0_cfg;
425
	char* remote_addr = device->openair0_cfg->remote_addr;
426
	LOG_I(HW,"Attempting to open Iris device: %s\n", remote_addr);
427
	std::string args = "driver=remote,serial="+std::string(remote_addr)+",remote:format=CS16";
428 429 430 431

	s->iris = SoapySDR::Device::make(args);
	device->type=IRIS_DEV;

432
	s->iris->setMasterClockRate(8*openair0_cfg[0].sample_rate); // sample*8=clock_rate for Soapy
433
	printf("tx_sample_advance %d\n", openair0_cfg[0].tx_sample_advance);
434 435 436
	switch ((int)openair0_cfg[0].sample_rate) {
	case 30720000:
		//openair0_cfg[0].samples_per_packet    = 1024;
437
		//openair0_cfg[0].tx_sample_advance     = 115;
438 439
		openair0_cfg[0].tx_bw                 = 20e6;
		openair0_cfg[0].rx_bw                 = 20e6;
440 441 442
		break;
	case 23040000:
		//openair0_cfg[0].samples_per_packet    = 1024;
443
		//openair0_cfg[0].tx_sample_advance     = 113;
444 445
		openair0_cfg[0].tx_bw                 = 15e6;
		openair0_cfg[0].rx_bw                 = 15e6;
446 447 448
		break;
	case 15360000:
		//openair0_cfg[0].samples_per_packet    = 1024;
rubuntun's avatar
rubuntun committed
449
		//openair0_cfg[0].tx_sample_advance     = 103;
450 451
		openair0_cfg[0].tx_bw                 = 10e6;
		openair0_cfg[0].rx_bw                 = 10e6;
452 453 454
		break;
	case 7680000:
		//openair0_cfg[0].samples_per_packet    = 1024;
455
		//openair0_cfg[0].tx_sample_advance     = 80;
456 457
		openair0_cfg[0].tx_bw                 = 5e6;
		openair0_cfg[0].rx_bw                 = 5e6;
458 459 460
		break;
	case 1920000:
		//openair0_cfg[0].samples_per_packet    = 1024;
461
		//openair0_cfg[0].tx_sample_advance     = 40;
462 463
		openair0_cfg[0].tx_bw                 = 5e6;
		openair0_cfg[0].rx_bw                 = 5e6;
464 465 466 467 468 469 470
		break;
	default:
		printf("Error: unknown sampling rate %f\n",openair0_cfg[0].sample_rate);
		exit(-1);
		break;
	}

rubuntun's avatar
rubuntun committed
471

472 473
	for(i=0; i < s->iris->getNumChannels(SOAPY_SDR_RX); i++) {
		if (i < openair0_cfg[0].rx_num_channels) {
474
			s->iris->setSampleRate(SOAPY_SDR_RX, i, openair0_cfg[0].sample_rate/SAMPLE_RATE_DOWN);
475 476 477 478
			s->iris->setFrequency(SOAPY_SDR_RX, i, "RF", openair0_cfg[0].rx_freq[i]);
			set_rx_gain_offset(&openair0_cfg[0],i,bw_gain_adjust);

			s->iris->setGain(SOAPY_SDR_RX, i, openair0_cfg[0].rx_gain[i]-openair0_cfg[0].rx_gain_offset[i]);
479 480
			if (openair0_cfg[0].duplex_mode == 1) // TDD
				s->iris->setAntenna(SOAPY_SDR_RX, i, (i==0)?"TRXA":"TRXB");			
481 482 483 484 485
			s->iris->setDCOffsetMode(SOAPY_SDR_RX, i, true); // move somewhere else
		}
	}
	for(i=0; i < s->iris->getNumChannels(SOAPY_SDR_TX); i++) {
		if (i < openair0_cfg[0].tx_num_channels) {
486
			s->iris->setSampleRate(SOAPY_SDR_TX, i, openair0_cfg[0].sample_rate/SAMPLE_RATE_DOWN);
487
			s->iris->setFrequency(SOAPY_SDR_TX, i, "RF", openair0_cfg[0].tx_freq[i]);
488
			s->iris->setGain(SOAPY_SDR_TX, i, openair0_cfg[0].tx_gain[i]);
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
		}
	}


	// display Iris settings
	std::cout << boost::format("Actual master clock: %fMHz...") % (s->iris->getMasterClockRate()/1e6) << std::endl;

	sleep(1);
	int samples=openair0_cfg[0].sample_rate;
	samples/=24000;


	/* Setting TX/RX BW after streamers are created due to iris calibration issue */
	for(i = 0; i < openair0_cfg[0].tx_num_channels; i++) {
		if (i < s->iris->getNumChannels(SOAPY_SDR_TX) ) {
			s->iris->setBandwidth(SOAPY_SDR_TX, i, openair0_cfg[0].tx_bw);
			printf("Setting tx freq/gain on channel %lu/%lu: BW %f (readback %f)\n",i,s->iris->getNumChannels(SOAPY_SDR_TX),openair0_cfg[0].tx_bw/1e6,s->iris->getBandwidth(SOAPY_SDR_TX, i)/1e6);
		}
	}
	for(i = 0; i < openair0_cfg[0].rx_num_channels; i++) {
		if (i < s->iris->getNumChannels(SOAPY_SDR_RX)) {
			s->iris->setBandwidth(SOAPY_SDR_RX, i, openair0_cfg[0].rx_bw);
			printf("Setting rx freq/gain on channel %lu/%lu : BW %f (readback %f)\n",i,s->iris->getNumChannels(SOAPY_SDR_RX),openair0_cfg[0].rx_bw/1e6,s->iris->getBandwidth(SOAPY_SDR_RX, i)/1e6);
		}
	}

rdoost's avatar
rdoost committed
515 516 517 518 519 520 521 522 523 524 525 526 527
	// create tx & rx streamer
	const SoapySDR::Kwargs &arg = SoapySDR::Kwargs();
	std::vector<size_t> channels={};
	for (i = 0; i<openair0_cfg[0].rx_num_channels; i++)
		if (i < s->iris->getNumChannels(SOAPY_SDR_RX))
			channels.push_back(i);
	s->rxStream = s->iris->setupStream(SOAPY_SDR_RX, SOAPY_SDR_CS16, channels, arg);

	std::vector<size_t> tx_channels={};
	for (i = 0; i<openair0_cfg[0].tx_num_channels; i++)
		if (i < s->iris->getNumChannels(SOAPY_SDR_TX))
			tx_channels.push_back(i);
	s->txStream = s->iris->setupStream(SOAPY_SDR_TX, SOAPY_SDR_CS16, tx_channels, arg);
528
	s->iris->setHardwareTime(0, "");
rubuntun's avatar
rubuntun committed
529

530 531 532 533 534 535 536 537 538 539 540 541 542

	for (i = 0; i < openair0_cfg[0].rx_num_channels; i++) {
		if (i < s->iris->getNumChannels(SOAPY_SDR_RX)) {
			printf("RX Channel %lu\n",i);
			std::cout << boost::format("Actual RX sample rate: %fMSps...") % (s->iris->getSampleRate(SOAPY_SDR_RX, i)/1e6) << std::endl;
			std::cout << boost::format("Actual RX frequency: %fGHz...") % (s->iris->getFrequency(SOAPY_SDR_RX, i)/1e9) << std::endl;
			std::cout << boost::format("Actual RX gain: %f...") % (s->iris->getGain(SOAPY_SDR_RX, i)) << std::endl;
			std::cout << boost::format("Actual RX bandwidth: %fM...") % (s->iris->getBandwidth(SOAPY_SDR_RX, i)/1e6) << std::endl;
			std::cout << boost::format("Actual RX antenna: %s...") % (s->iris->getAntenna(SOAPY_SDR_RX, i)) << std::endl;
		}
	}

	for (i=0;i<openair0_cfg[0].tx_num_channels;i++) {
rubuntun's avatar
rubuntun committed
543
		if (i < s->iris->getNumChannels(SOAPY_SDR_TX)) {
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 574 575
			printf("TX Channel %lu\n",i);
			std::cout << std::endl<<boost::format("Actual TX sample rate: %fMSps...") % (s->iris->getSampleRate(SOAPY_SDR_TX, i)/1e6) << std::endl;
			std::cout << boost::format("Actual TX frequency: %fGHz...") % (s->iris->getFrequency(SOAPY_SDR_TX, i)/1e9) << std::endl;
			std::cout << boost::format("Actual TX gain: %f...") % (s->iris->getGain(SOAPY_SDR_TX, i)) << std::endl;
			std::cout << boost::format("Actual TX bandwidth: %fM...") % (s->iris->getBandwidth(SOAPY_SDR_TX, i)/1e6) << std::endl;
			std::cout << boost::format("Actual TX antenna: %s...") % (s->iris->getAntenna(SOAPY_SDR_TX, i)) << std::endl;
		}
	}

	std::cout << boost::format("Device timestamp: %f...") % (s->iris->getHardwareTime()/1e9) << std::endl;

	device->priv = s;
	device->trx_start_func = trx_iris_start;
	device->trx_write_func = trx_iris_write;
	device->trx_read_func  = trx_iris_read;
	device->trx_get_stats_func = trx_iris_get_stats;
	device->trx_reset_stats_func = trx_iris_reset_stats;
	device->trx_end_func   = trx_iris_end;
	device->trx_stop_func  = trx_iris_stop;
	device->trx_set_freq_func = trx_iris_set_freq;
	device->trx_set_gains_func   = trx_iris_set_gains;
	device->openair0_cfg = openair0_cfg;

	s->sample_rate = openair0_cfg[0].sample_rate;
	// TODO:
	// init tx_forward_nsamps based iris_time_offset ex
	if(is_equal(s->sample_rate, (double)30.72e6))
		s->tx_forward_nsamps  = 176;
	if(is_equal(s->sample_rate, (double)15.36e6))
		s->tx_forward_nsamps = 90;
	if(is_equal(s->sample_rate, (double)7.68e6))
		s->tx_forward_nsamps = 50;
576 577

	LOG_I(HW,"Finished initializing Iris device. %d %f \n");
578 579 580 581
	return 0;
  }
}
/*@}*/