示例#1
0
static void *monitor_thread_fn(void *device_name)
{
	struct iio_context *ctx;
	struct iio_device *dev;
	uint32_t val;
	int ret;

	ctx = iio_create_default_context();
	if (!ctx) {
		fprintf(stderr, "Unable to create IIO context\n");
		return (void *)-1;
	}

	dev = get_device(ctx, device_name);
	if (!dev) {
		fprintf(stderr, "Unable to find IIO device\n");
		iio_context_destroy(ctx);
		return (void *)-1;
	}

	/* Give the main thread a moment to start the DMA */
	sleep(1);

	/* Clear all status bits */
	iio_device_reg_write(dev, 0x80000088, 0x6);

	while (app_running) {
		ret = iio_device_reg_read(dev, 0x80000088, &val);
		if (ret) {
			fprintf(stderr, "Failed to read status register: %s\n",
					strerror(-ret));
			continue;
		}

		if (device_is_tx) {
			if (val & 1)
				fprintf(stderr, "Underflow detected\n");
		} else {
			if (val & 4)
				fprintf(stderr, "Overflow detected\n");
		}

		/* Clear bits */
		if (val)
			iio_device_reg_write(dev, 0x80000088, val);
		sleep(1);
	}

	iio_context_destroy(ctx);

	return (void *)0;
}
示例#2
0
int main(int argc, char **argv)
{
	unsigned int i, nb_channels;
	unsigned int buffer_size = SAMPLES_PER_READ;
	int c, option_index = 0, arg_index = 0, ip_index = 0;
	struct iio_device *dev;
	size_t sample_size;

	while ((c = getopt_long(argc, argv, "+hn:t:b:s:",
					options, &option_index)) != -1) {
		switch (c) {
		case 'h':
			usage();
			return EXIT_SUCCESS;
		case 'n':
			arg_index += 2;
			ip_index = arg_index;
			break;
		case 't':
			arg_index += 2;
			trigger_name = argv[arg_index];
			break;
		case 'b':
			arg_index += 2;
			buffer_size = atoi(argv[arg_index]);
			break;
		case 's':
			arg_index += 2;
			num_samples = atoi(argv[arg_index]);
			break;
		case '?':
			return EXIT_FAILURE;
		}
	}

	if (arg_index + 1 >= argc) {
		fprintf(stderr, "Incorrect number of arguments.\n\n");
		usage();
		return EXIT_FAILURE;
	}

	if (ip_index)
		ctx = iio_create_network_context(argv[ip_index]);
	else
		ctx = iio_create_default_context();

	if (!ctx) {
		fprintf(stderr, "Unable to create IIO context\n");
		return EXIT_FAILURE;
	}


#ifndef _WIN32
	set_handler(SIGHUP, &quit_all);
#endif
	set_handler(SIGINT, &quit_all);
	set_handler(SIGSEGV, &quit_all);
	set_handler(SIGTERM, &quit_all);

	dev = get_device(ctx, argv[arg_index + 1]);
	if (!dev) {
		iio_context_destroy(ctx);
		return EXIT_FAILURE;
	}

	if (trigger_name) {
		struct iio_device *trigger = get_device(ctx, trigger_name);
		if (!trigger) {
			iio_context_destroy(ctx);
			return EXIT_FAILURE;
		}

		if (!iio_device_is_trigger(trigger)) {
			fprintf(stderr, "Specified device is not a trigger\n");
			iio_context_destroy(ctx);
			return EXIT_FAILURE;
		}

		/* Fixed rate for now */
		iio_device_attr_write_longlong(trigger, "frequency", 100);
		iio_device_set_trigger(dev, trigger);
	}

	nb_channels = iio_device_get_channels_count(dev);

	if (argc == arg_index + 2) {
		/* Enable all channels */
		for (i = 0; i < nb_channels; i++)
			iio_channel_enable(iio_device_get_channel(dev, i));
	} else {
		for (i = 0; i < nb_channels; i++) {
			unsigned int j;
			struct iio_channel *ch = iio_device_get_channel(dev, i);
			for (j = arg_index + 2; j < argc; j++) {
				const char *n = iio_channel_get_name(ch);
				if (!strcmp(argv[j], iio_channel_get_id(ch)) ||
						(n && !strcmp(n, argv[j])))
					iio_channel_enable(ch);
			}
		}
	}

	sample_size = iio_device_get_sample_size(dev);

	buffer = iio_device_create_buffer(dev, buffer_size, false);
	if (!buffer) {
		fprintf(stderr, "Unable to allocate buffer\n");
		iio_context_destroy(ctx);
		return EXIT_FAILURE;
	}

	while (app_running) {
		int ret = iio_buffer_refill(buffer);
		if (ret < 0) {
			fprintf(stderr, "Unable to refill buffer: %s\n",
					strerror(-ret));
			break;
		}

		/* If there are only the samples we requested, we don't need to
		 * demux */
		if (iio_buffer_step(buffer) == sample_size) {
			void *start = iio_buffer_start(buffer);
			ptrdiff_t len = (intptr_t) iio_buffer_end(buffer) -
				(intptr_t) start;
			size_t read_len;

			if (num_samples && len > num_samples * sample_size)
				len = num_samples * sample_size;

			for (read_len = len; len; ) {
				ssize_t nb = fwrite(start, 1, len, stdout);
				if (nb < 0) {
					fprintf(stderr, "Unable to write data!\n");
					goto err_destroy_buffer;
				}

				len -= nb;
				start = (void *)((intptr_t) start + nb);
			}

			if (num_samples) {
				num_samples -= read_len / sample_size;
				if (!num_samples)
					quit_all(EXIT_SUCCESS);
			}
		} else {
			iio_buffer_foreach_sample(buffer, print_sample, NULL);
		}
	}

err_destroy_buffer:
	iio_buffer_destroy(buffer);
	iio_context_destroy(ctx);
	return exit_code;
}
示例#3
0
/* simple configuration and streaming */
int main (int argc, char **argv){

int child=fork();
if (child==-1) {DieWithError("socket() failed");}
//Father code
else if (child==0){
	pause();
//	sleep(5);	//Child run first
	// Streaming devices
	struct iio_device *rx;
	// Stream configurations
	struct stream_cfg rxcfg;

	// Listen to ctrl+c and assert
	signal(SIGINT, shutdowniio);

	// RX stream config
	rxcfg.bw_hz = MHZ(28);   // 2 MHz rf bandwidth		//200KHz-56MHz Channel bandwidths 
	rxcfg.fs_hz = MHZ(61.44);   // 2 MS/s rx sample rate	//	61.44
	rxcfg.lo_hz = GHZ(2.5); // 2.5 GHz rf frequency		//70MHz -6 GHz Local-oscilator
	rxcfg.rfport = "A_BALANCED"; // port A (select for rf freq.)

	if(verbose==1)	printf("* Acquiring IIO context\n");
	assert((ctx = iio_create_default_context()) && "No context");
	assert(iio_context_get_devices_count(ctx) > 0 && "No devices");
	if(verbose==1) printf("* Acquiring AD9361 streaming devices\n");
	assert(get_ad9361_stream_dev(ctx, RX, &rx) && "No rx dev found");
	if(verbose==1) printf("* Configuring AD9361 for streaming\n");
	assert(cfg_ad9361_streaming_ch(ctx, &rxcfg, RX, 0) && "RX port 0 not found");
	if(verbose==1) printf("* Init AD9361 IIO streaming channels\n");
	assert(get_ad9361_stream_ch(ctx, RX, rx, 0, &rx0_i) && "RX chan i not found");
	assert(get_ad9361_stream_ch(ctx, RX, rx, 1, &rx0_q) && "RX chan q not found");
	if(verbose==1) printf("* Enabling IIO streaming channels\n");
	iio_channel_enable(rx0_i);
	iio_channel_enable(rx0_q);
	if(verbose==1) printf("* Creating non-cyclic buff with 1 MiS\n");
	rxbuf = iio_device_create_buffer(rx, BUFF_SIZE/4, false);

	struct sockaddr_in serv,serv2; 				/* Echo server address */
	printf("* Server conection(192.168.0.229:50705)*");
	/* Construct the server address structure */
	memset(&serv, 0, sizeof(serv));    			/* Zero out structure */
	serv.sin_family = AF_INET;                 		/* Internet addr family */
	serv.sin_addr.s_addr = inet_addr("192.168.0.229");  	/* Server IP address */
	serv.sin_port   = htons(50705);     			/* Server port */

	memset(&serv2, 0, sizeof(serv2));    			/* Zero out structure */
	serv2.sin_family = AF_INET;                 		/* Internet addr family */
	serv2.sin_addr.s_addr = inet_addr("192.168.0.229");  	/* Server IP address */
	serv2.sin_port   = htons(50705);     			/* Server port */
    	/* Create a datagram/UDP socket */
	if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
	DieWithError("socket() failed");
	if ((sock2 = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
	DieWithError("socket2() failed");

	/* Connecting to the server */
	if (connect(sock,(struct sockaddr *)&serv, sizeof(serv)) < 0) DieWithError("socket() failed");
	if (connect(sock2,(struct sockaddr *)&serv2, sizeof(serv2)) < 0) DieWithError("socket() failed"); 
	
	/* Sincronization */
	printf("\n* Sended   AK *");   
	if (sendto(sock, "AK", 2, 0, (struct sockaddr *)&serv, sizeof(serv)) != 2)
		DieWithError("sendto() sent a different number of bytes than expected");
	fromSize = sizeof(fromAddr);
	rsplen = recvfrom(sock, buff_from_n, 1024*32, 0,(struct sockaddr *) &fromAddr, &fromSize);
	if(rsplen!= 2){ DieWithError("recvfrom() failed");}

	if (serv.sin_addr.s_addr != fromAddr.sin_addr.s_addr) {printf("Unknown client.\n"); exit(1);}
	buff_from_n[rsplen] = '\0';	
	printf("\n* Received %s *\n", (char*)buff_from_n);   
	if(verbose==1) printf("* Starting IO streaming ");

	int status;
	ssize_t nbytes_rx=0;
	void *p_dat ,*p_end;
	pthread_t tid,tid2;
	//Fill buffer from antena
	nbytes_rx = iio_buffer_refill(rxbuf); 
	if (nbytes_rx < 0) { printf("Error refilling buf %d\n",(int) nbytes_rx); shutdowniio(0); }
	p_end = iio_buffer_end(rxbuf);
	p_dat = iio_buffer_first(rxbuf, rx0_i);
	memcpy (buff_from_a, p_dat, (&p_end-&p_dat));
	printf("\n* Ready for fast transfer *");

	sec_begin= time(NULL);
	while(sec_elapsed<10)
	{	
		sec_end=time(NULL);
		sec_elapsed=sec_end-sec_begin;	
		
		//Send data to network
		memcpy (buff_from_a, p_dat, (&p_end-&p_dat));
		
		if(pthread_create(&tid, NULL, send_msg1,(int*)sock)!=0){
			printf ("\nPthread_create");exit(1);}
	//	if(pthread_create(&tid2, NULL, send_msg2,(int*)sock)!=0){
	//		printf ("\nPthread_create");exit(1);}

		//Fill buffer from antena
		nbytes_rx = iio_buffer_refill(rxbuf); 
			if (nbytes_rx < 0) { printf("Error refilling buf %d\n",(int) nbytes_rx); shutdowniio(0); }
		p_end = iio_buffer_end(rxbuf);
		p_dat = iio_buffer_first(rxbuf, rx0_i);
		count=count+nbytes_rx;

		//Syncronizing
		if(pthread_join(tid ,(void**)&status)!=0){printf ("Pthread_join");exit(1);}
	//	if(pthread_join(tid2,(void**)&status)!=0){printf ("Pthread_join");exit(1);}
	}
printf("\n\tTime elapsed: %lus \n\tAntena recived  %llu MB\tDebit %llu Mb/sec ",
		sec_elapsed,	count/1024/1024,	count/sec_elapsed/1024/1024);
printf("\n\tSocket sended %lld Mb \n",sendcount/1024/1024);
//printf("\nBytes sendeded %llu ",summ);
	
close(sock);
close(tcpsock);
return 0;
}//End Parent




//Child programm
else if (child!=0){
	//pause();
	// Listen to ctrl+c and assert
	signal(SIGINT, shutdowniio);
	struct iio_device *tx;		// Streaming devices
	struct stream_cfg txcfg;	// Stream configurations
	// TX stream config
	txcfg.bw_hz = MHZ(28); 		// 1.5 MHz rf bandwidth
	txcfg.fs_hz = MHZ(61.44);  	// 2 MS/s tx sample rate
	txcfg.lo_hz = GHZ(2.5); 	// 2.5 GHz rf frequency
	txcfg.rfport = "A"; 		// port A (select for rf freq.)
	
	if(verbose==1) printf("\t\t\t\t\t* Acquiring IIO context\n");
	assert((ctx = iio_create_default_context()) && "No context");
	assert(iio_context_get_devices_count(ctx) > 0 && "No devices");
	if(verbose==1) printf("\t\t\t\t\t* Acquiring AD9361 streaming devices\n");
	assert(get_ad9361_stream_dev(ctx, TX, &tx) && "No tx dev found");
	if(verbose==1) printf("\t\t\t\t\t* Configuring AD9361 for streaming\n");
	assert(cfg_ad9361_streaming_ch(ctx, &txcfg, TX, 0) && "TX port 0 not found");
	if(verbose==1) printf("\t\t\t\t\t* Initializing AD9361 IIO streaming channels\n");
	assert(get_ad9361_stream_ch(ctx, TX, tx, 0, &tx0_i) && "TX chan i not found");
	assert(get_ad9361_stream_ch(ctx, TX, tx, 1, &tx0_q) && "TX chan q not found");
	if(verbose==1) printf("\t\t\t\t\t* Enabling IIO streaming channels\n");
	iio_channel_enable(tx0_i);
	iio_channel_enable(tx0_q);
	if(verbose==1) printf("\t\t\t\t\t* Creating non-cyclic IIO buffers with 1 MiS\n");
	txbuf = iio_device_create_buffer(tx, BUFF_SIZE/4, false);

	printf("\t\t\t\t\t* Server conection*\n");
	struct sockaddr_in serv; 				/* Echo server address */
	/* Construct the server address structure */
	memset(&serv, 0, sizeof(serv));    			/* Zero out structure */
	serv.sin_family = AF_INET;                 		/* Internet addr family */
	serv.sin_addr.s_addr = inet_addr("192.168.0.229");  	/* Server IP address */
	serv.sin_port   = htons(50707);     			/* Server port */
    	/* Create a datagram/UDP socket */
	if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)	DieWithError("socket() failed");
	/* Connecting to the server */
	//if (connect(sock,(struct sockaddr *)&serv, sizeof(serv)) < 0) 	DieWithError("socket() failed");
	//pause();
	/* Sincronization */
	printf("\t\t\t\t\t* Sended   AK *\n");   
	if (sendto(sock, "AK", 2, 0, (struct sockaddr *)&serv, sizeof(serv)) != 2)
		DieWithError("sendto() sent a different number of bytes than expected");
	fromSize = sizeof(fromAddr);
	rsplen = recvfrom(sock, buff_from_n, 1024*32, 0,(struct sockaddr *) &fromAddr, &fromSize);
	if(rsplen!= 2){ DieWithError("recvfrom() failed");}

	if (serv.sin_addr.s_addr != fromAddr.sin_addr.s_addr) {printf("Unknown client.\n"); exit(1);	}
	buff_from_n[rsplen] = '\0';	
	printf("\t\t\t\t\t* Received %s *", (char*)buff_from_n);   

	int status;
	ssize_t nbytes_tx=0;
	void  *t_dat,*t_end;
	pthread_t tid3;
	setpriority(PRIO_PROCESS, 0, -20);
	//Fill buff_from_n from network
	if(pthread_create(&tid3, NULL, recv_msg,(int*)sock)!=0){
		printf ("\nPthread_create\n");exit(1);}
	if(pthread_join(tid3,(void**)&status)!=0){printf ("Pthread_join");exit(1);}
	//Bug push after in the third try	
	nbytes_tx = iio_buffer_push(txbuf);
	nbytes_tx = iio_buffer_push(txbuf);
	printf("\n\t\t\t\t\tReciving........  ");
	while(1)
      	{
		//Fill TXbuffer 
		t_end = iio_buffer_end(txbuf);
		t_dat = iio_buffer_first(txbuf, tx0_i);
		memcpy(t_dat,buff_from_n, (1024*1024));
		nbytes_tx = iio_buffer_push(txbuf);
		//Fill buff_from_n from network
		if(pthread_create(&tid3, NULL, recv_msg,(int*)sock)!=0){printf ("\nPthread_create\n");exit(1);}
      		// Send buffer to antena
		if (nbytes_tx < 0) { printf("Error pushing buf %d\n", (int) nbytes_tx); shutdowniio(0); }
		count2=count2+nbytes_tx;
		if(pthread_join(tid3,(void**)&status)!=0){printf ("Pthread_join");exit(1);}
		if(status==1){ break;	}
	}

	t_end = iio_buffer_end(txbuf);
	t_dat = iio_buffer_first(txbuf, tx0_i);
	memcpy(t_dat,buff_from_n, (&t_end-&t_dat));
	nbytes_tx = iio_buffer_push(txbuf);
	if (nbytes_tx < 0) { printf("Error pushing buf %d\n", (int) nbytes_tx); shutdowniio(0); }
	count2=count2+nbytes_tx;

	printf("\n\t\t\t\t\tRecived from net %llu MB",recvcount/1024/1024);
	printf("\tSended to antena %lld Mb \n",count2/1024/1024);

}

return 0;
}//End of the life
示例#4
0
int main(int argc, char **argv)
{
	char *xml;
	struct iio_context *ctx;
	int c, option_index = 0, arg_index = 0, xml_index = 0, ip_index = 0;
	enum backend backend = LOCAL;

	while ((c = getopt_long(argc, argv, "+hn:x:",
					options, &option_index)) != -1) {
		switch (c) {
		case 'h':
			usage();
			return EXIT_SUCCESS;
		case 'n':
			if (backend != LOCAL) {
				fprintf(stderr, "-x and -n are mutually exclusive\n");
				return EXIT_FAILURE;
			}
			backend = NETWORK;
			arg_index += 2;
			ip_index = arg_index;
			break;
		case 'x':
			if (backend != LOCAL) {
				fprintf(stderr, "-x and -n are mutually exclusive\n");
				return EXIT_FAILURE;
			}
			backend = XML;
			arg_index += 2;
			xml_index = arg_index;
			break;
		case '?':
			return EXIT_FAILURE;
		}
	}

	if (arg_index >= argc) {
		fprintf(stderr, "Incorrect number of arguments.\n\n");
		usage();
		return EXIT_FAILURE;
	}

	if (backend == XML)
		ctx = iio_create_xml_context(argv[xml_index]);
	else if (backend == NETWORK)
		ctx = iio_create_network_context(argv[ip_index]);
	else
		ctx = iio_create_default_context();

	if (!ctx) {
		fprintf(stderr, "Unable to create IIO context\n");
		return EXIT_FAILURE;
	}

	xml = strdup(iio_context_get_xml(ctx));
	if (!xml) {
		iio_context_destroy(ctx);
		return EXIT_FAILURE;
	}

	printf("XML generated:\n\n%s\n\n", xml);

	iio_context_destroy(ctx);

	ctx = iio_create_xml_context_mem(xml, strlen(xml));
	if (!ctx) {
		fprintf(stderr, "Unable to re-generate context\n");
	} else {
		printf("Context re-creation from generated XML suceeded!\n");
		iio_context_destroy(ctx);
	}
	free(xml);
	return EXIT_SUCCESS;
}
示例#5
0
int main(int argc, char **argv)
{
	unsigned int buffer_size = 1024 * 1024;
	int c, option_index = 0, arg_index = 0;
	unsigned int n_tx = 0, n_rx = 0;
	static struct iio_context *ctx;
	unsigned int i, nb_channels;
	struct iio_buffer *buffer;
	pthread_t monitor_thread;
	const char *device_name;
	struct iio_device *dev;
	char unit;
	int ret;

	while ((c = getopt_long(argc, argv, "+hs:",
					options, &option_index)) != -1) {
		switch (c) {
		case 'h':
			usage(argv);
			return EXIT_SUCCESS;
		case 's':
			arg_index += 2;
			ret = sscanf(argv[arg_index], "%u%c", &buffer_size, &unit);
			if (ret == 0)
				return EXIT_FAILURE;
			if (ret == 2) {
				if (unit == 'k')
					buffer_size *= 1024;
				else if (unit == 'M')
					buffer_size *= 1024 * 1024;
			}
			break;
		case '?':
			return EXIT_FAILURE;
		}
	}

	if (arg_index + 1 >= argc) {
		fprintf(stderr, "Incorrect number of arguments.\n\n");
		usage(argv);
		return EXIT_FAILURE;
	}

#ifndef _WIN32
	set_handler(SIGHUP, &quit_all);
#endif
	set_handler(SIGINT, &quit_all);
	set_handler(SIGSEGV, &quit_all);
	set_handler(SIGTERM, &quit_all);

	ctx = iio_create_default_context();
	if (!ctx) {
		fprintf(stderr, "Unable to create IIO context\n");
		return EXIT_FAILURE;
	}

	device_name = argv[arg_index + 1];

	dev = get_device(ctx, device_name);
	if (!dev) {
		iio_context_destroy(ctx);
		return EXIT_FAILURE;
	}

	nb_channels = iio_device_get_channels_count(dev);
	for (i = 0; i < nb_channels; i++) {
		struct iio_channel *ch = iio_device_get_channel(dev, i);
		if (!iio_channel_is_scan_element(ch))
			continue;
		iio_channel_enable(ch);
		if (iio_channel_is_output(ch))
			n_tx++;
		else
			n_rx++;
	}

	if (n_tx >= n_rx)
		device_is_tx = true;
	else
		device_is_tx = false;

	printf("Monitoring %s for underflows/overflows\n",
		iio_device_get_name(dev));

	buffer = iio_device_create_buffer(dev, buffer_size, false);
	if (!buffer) {
		fprintf(stderr, "Unable to allocate buffer\n");
		iio_context_destroy(ctx);
		return EXIT_FAILURE;
	}

	ret = pthread_create(&monitor_thread, NULL, monitor_thread_fn,
		(void *)device_name);
	if (ret) {
		fprintf(stderr, "Failed to create monitor thread: %s\n",
				strerror(-ret));
	}

	while (app_running) {
		if (device_is_tx) {
			ret = iio_buffer_push(buffer);
			if (ret < 0) {
				fprintf(stderr, "Unable to push buffer: %s\n",
						strerror(-ret));
				app_running = false;
				break;
			}
		} else {
			ret = iio_buffer_refill(buffer);
			if (ret < 0) {
				fprintf(stderr, "Unable to refill buffer: %s\n",
						strerror(-ret));
				app_running = false;
				break;
			}
		}
	}

	iio_buffer_destroy(buffer);
	iio_context_destroy(ctx);

	pthread_join(monitor_thread, NULL);

	return 0;
}
示例#6
0
int main(int argc, char **argv)
{
	unsigned int i, nb_channels;
	unsigned int buffer_size = SAMPLES_PER_READ;
	const char *arg_uri = NULL;
	const char *arg_ip = NULL;
	int c, option_index = 0;
	struct iio_device *dev;
	size_t sample_size;
	int timeout = -1;
	bool scan_for_context = false;

	while ((c = getopt_long(argc, argv, "+hn:u:t:b:s:T:a",
					options, &option_index)) != -1) {
		switch (c) {
		case 'h':
			usage();
			return EXIT_SUCCESS;
		case 'n':
			arg_ip = optarg;
			break;
		case 'u':
			arg_uri = optarg;
			break;
		case 'a':
			scan_for_context = true;
			break;
		case 't':
			trigger_name = optarg;
			break;
		case 'b':
			buffer_size = atoi(optarg);
			break;
		case 's':
			num_samples = atoi(optarg);
			break;
		case 'T':
			timeout = atoi(optarg);
			break;
		case '?':
			return EXIT_FAILURE;
		}
	}

	if (argc == optind) {
		fprintf(stderr, "Incorrect number of arguments.\n\n");
		usage();
		return EXIT_FAILURE;
	}

	setup_sig_handler();

	if (scan_for_context)
		ctx = scan();
	else if (arg_uri)
		ctx = iio_create_context_from_uri(arg_uri);
	else if (arg_ip)
		ctx = iio_create_network_context(arg_ip);
	else
		ctx = iio_create_default_context();

	if (!ctx) {
		fprintf(stderr, "Unable to create IIO context\n");
		return EXIT_FAILURE;
	}

	if (timeout >= 0)
		iio_context_set_timeout(ctx, timeout);

	dev = iio_context_find_device(ctx, argv[optind]);
	if (!dev) {
		fprintf(stderr, "Device %s not found\n", argv[optind]);
		iio_context_destroy(ctx);
		return EXIT_FAILURE;
	}

	if (trigger_name) {
		struct iio_device *trigger = iio_context_find_device(
				ctx, trigger_name);
		if (!trigger) {
			fprintf(stderr, "Trigger %s not found\n", trigger_name);
			iio_context_destroy(ctx);
			return EXIT_FAILURE;
		}

		if (!iio_device_is_trigger(trigger)) {
			fprintf(stderr, "Specified device is not a trigger\n");
			iio_context_destroy(ctx);
			return EXIT_FAILURE;
		}

		/*
		 * Fixed rate for now. Try new ABI first,
		 * fail gracefully to remain compatible.
		 */
		if (iio_device_attr_write_longlong(trigger,
				"sampling_frequency", DEFAULT_FREQ_HZ) < 0)
			iio_device_attr_write_longlong(trigger,
				"frequency", DEFAULT_FREQ_HZ);

		iio_device_set_trigger(dev, trigger);
	}

	nb_channels = iio_device_get_channels_count(dev);

	if (argc == optind + 1) {
		/* Enable all channels */
		for (i = 0; i < nb_channels; i++)
			iio_channel_enable(iio_device_get_channel(dev, i));
	} else {
		for (i = 0; i < nb_channels; i++) {
			unsigned int j;
			struct iio_channel *ch = iio_device_get_channel(dev, i);
			for (j = optind + 1; j < (unsigned int) argc; j++) {
				const char *n = iio_channel_get_name(ch);
				if (!strcmp(argv[j], iio_channel_get_id(ch)) ||
						(n && !strcmp(n, argv[j])))
					iio_channel_enable(ch);
			}
		}
	}

	sample_size = iio_device_get_sample_size(dev);

	buffer = iio_device_create_buffer(dev, buffer_size, false);
	if (!buffer) {
		char buf[256];
		iio_strerror(errno, buf, sizeof(buf));
		fprintf(stderr, "Unable to allocate buffer: %s\n", buf);
		iio_context_destroy(ctx);
		return EXIT_FAILURE;
	}

	while (app_running) {
		int ret = iio_buffer_refill(buffer);
		if (ret < 0) {
			if (app_running) {
				char buf[256];
				iio_strerror(-ret, buf, sizeof(buf));
				fprintf(stderr, "Unable to refill buffer: %s\n", buf);
			}
			break;
		}

		/* If there are only the samples we requested, we don't need to
		 * demux */
		if (iio_buffer_step(buffer) == sample_size) {
			void *start = iio_buffer_start(buffer);
			size_t read_len, len = (intptr_t) iio_buffer_end(buffer)
				- (intptr_t) start;

			if (num_samples && len > num_samples * sample_size)
				len = num_samples * sample_size;

			for (read_len = len; len; ) {
				size_t nb = fwrite(start, 1, len, stdout);
				if (!nb)
					goto err_destroy_buffer;

				len -= nb;
				start = (void *)((intptr_t) start + nb);
			}

			if (num_samples) {
				num_samples -= read_len / sample_size;
				if (!num_samples)
					quit_all(EXIT_SUCCESS);
			}
		} else {
			iio_buffer_foreach_sample(buffer, print_sample, NULL);
		}
	}

err_destroy_buffer:
	iio_buffer_destroy(buffer);
	iio_context_destroy(ctx);
	return exit_code;
}
示例#7
0
/* simple configuration and streaming */
int main (int argc, char **argv)
{
	// Streaming devices
	struct iio_device *tx;
	struct iio_device *rx;

	// RX and TX sample counters
	size_t nrx = 0;
	size_t ntx = 0;

	// Stream configurations
	struct stream_cfg rxcfg;
	struct stream_cfg txcfg;

	// Listen to ctrl+c and assert
	signal(SIGINT, handle_sig);

	// RX stream config
	rxcfg.bw_hz = MHZ(2);   // 2 MHz rf bandwidth
	rxcfg.fs_hz = MHZ(2.5);   // 2.5 MS/s rx sample rate
	rxcfg.lo_hz = GHZ(2.5); // 2.5 GHz rf frequency
	rxcfg.rfport = "A_BALANCED"; // port A (select for rf freq.)

	// TX stream config
	txcfg.bw_hz = MHZ(1.5); // 1.5 MHz rf bandwidth
	txcfg.fs_hz = MHZ(2.5);   // 2.5 MS/s tx sample rate
	txcfg.lo_hz = GHZ(2.5); // 2.5 GHz rf frequency
	txcfg.rfport = "A"; // port A (select for rf freq.)

	printf("* Acquiring IIO context\n");
	assert((ctx = iio_create_default_context()) && "No context");
	assert(iio_context_get_devices_count(ctx) > 0 && "No devices");

	printf("* Acquiring AD9361 streaming devices\n");
	assert(get_ad9361_stream_dev(ctx, TX, &tx) && "No tx dev found");
	assert(get_ad9361_stream_dev(ctx, RX, &rx) && "No rx dev found");

	printf("* Configuring AD9361 for streaming\n");
	assert(cfg_ad9361_streaming_ch(ctx, &rxcfg, RX, 0) && "RX port 0 not found");
	assert(cfg_ad9361_streaming_ch(ctx, &txcfg, TX, 0) && "TX port 0 not found");

	printf("* Initializing AD9361 IIO streaming channels\n");
	assert(get_ad9361_stream_ch(ctx, RX, rx, 0, &rx0_i) && "RX chan i not found");
	assert(get_ad9361_stream_ch(ctx, RX, rx, 1, &rx0_q) && "RX chan q not found");
	assert(get_ad9361_stream_ch(ctx, TX, tx, 0, &tx0_i) && "TX chan i not found");
	assert(get_ad9361_stream_ch(ctx, TX, tx, 1, &tx0_q) && "TX chan q not found");

	printf("* Enabling IIO streaming channels\n");
	iio_channel_enable(rx0_i);
	iio_channel_enable(rx0_q);
	iio_channel_enable(tx0_i);
	iio_channel_enable(tx0_q);

	printf("* Creating non-cyclic IIO buffers with 1 MiS\n");
	rxbuf = iio_device_create_buffer(rx, 1024*1024, false);
	if (!rxbuf) {
		perror("Could not create RX buffer");
		shutdown();
	}
	txbuf = iio_device_create_buffer(tx, 1024*1024, false);
	if (!txbuf) {
		perror("Could not create TX buffer");
		shutdown();
	}

	printf("* Starting IO streaming (press CTRL+C to cancel)\n");
	while (!stop)
	{
		ssize_t nbytes_rx, nbytes_tx;
		void *p_dat, *p_end;
		ptrdiff_t p_inc;

		// Schedule TX buffer
		nbytes_tx = iio_buffer_push(txbuf);
		if (nbytes_tx < 0) { printf("Error pushing buf %d\n", (int) nbytes_tx); shutdown(); }

		// Refill RX buffer
		nbytes_rx = iio_buffer_refill(rxbuf);
		if (nbytes_rx < 0) { printf("Error refilling buf %d\n",(int) nbytes_rx); shutdown(); }

		// READ: Get pointers to RX buf and read IQ from RX buf port 0
		p_inc = iio_buffer_step(rxbuf);
		p_end = iio_buffer_end(rxbuf);
		for (p_dat = iio_buffer_first(rxbuf, rx0_i); p_dat < p_end; p_dat += p_inc) {
			// Example: swap I and Q
			const int16_t i = ((int16_t*)p_dat)[0]; // Real (I)
			const int16_t q = ((int16_t*)p_dat)[1]; // Imag (Q)
			((int16_t*)p_dat)[0] = q;
			((int16_t*)p_dat)[1] = i;
		}

		// WRITE: Get pointers to TX buf and write IQ to TX buf port 0
		p_inc = iio_buffer_step(txbuf);
		p_end = iio_buffer_end(txbuf);
		for (p_dat = iio_buffer_first(txbuf, tx0_i); p_dat < p_end; p_dat += p_inc) {
			// Example: fill with zeros
			((int16_t*)p_dat)[0] = 0; // Real (I)
			((int16_t*)p_dat)[1] = 0; // Imag (Q)
		}

		// Sample counter increment and status output
		nrx += nbytes_rx / iio_device_get_sample_size(rx);
		ntx += nbytes_tx / iio_device_get_sample_size(tx);
		printf("\tRX %8.2f MSmp, TX %8.2f MSmp\n", nrx/1e6, ntx/1e6);
	}

	shutdown();

	return 0;
}