static void init_device_list(void) { unsigned int i, num = iio_context_get_devices_count(ctx); GtkTreeIter iter; gtk_list_store_clear(device_list_store); for (i = 0; i < num; i++) { struct iio_device *dev = iio_context_get_device(ctx, i); unsigned int j, nch = iio_device_get_channels_count(dev); const char *id; bool found_valid_chan = false; for (j = 0; !found_valid_chan && j < nch; j++) { struct iio_channel *chn = iio_device_get_channel(dev, j); found_valid_chan = is_valid_dmm_channel(chn); } if (!found_valid_chan) continue; id = iio_device_get_name(dev); if (!id) id = iio_device_get_id(dev); gtk_list_store_append(device_list_store, &iter); gtk_list_store_set(device_list_store, &iter, 0, id, 1, 0, -1); } gtk_tree_sortable_set_sort_column_id( GTK_TREE_SORTABLE(GTK_TREE_MODEL(device_list_store)), 0, GTK_SORT_ASCENDING); }
int iiod_client_get_trigger(struct iiod_client *client, int desc, const struct iio_device *dev, const struct iio_device **trigger) { const struct iio_context *ctx = iio_device_get_context(dev); unsigned int i, nb_devices = iio_context_get_devices_count(ctx); char buf[1024]; unsigned int name_len; int ret; snprintf(buf, sizeof(buf), "GETTRIG %s\r\n", iio_device_get_id(dev)); iio_mutex_lock(client->lock); ret = iiod_client_exec_command(client, desc, buf); if (ret == 0) *trigger = NULL; if (ret <= 0) goto out_unlock; if ((unsigned int) ret > sizeof(buf) - 1) { ret = -EIO; goto out_unlock; } name_len = ret; ret = (int) iiod_client_read_all(client, desc, buf, name_len + 1); if (ret < 0) goto out_unlock; ret = -ENXIO; for (i = 0; i < nb_devices; i++) { struct iio_device *cur = iio_context_get_device(ctx, i); if (iio_device_is_trigger(cur)) { const char *name = iio_device_get_name(cur); if (!name) continue; if (!strncmp(name, buf, name_len)) { *trigger = cur; ret = 0; goto out_unlock; } } } out_unlock: iio_mutex_unlock(client->lock); return ret; }
static struct iio_device * get_device(const char *id) { unsigned int i, nb = iio_context_get_devices_count(ctx); for (i = 0; i < nb; i++) { struct iio_device *dev = iio_context_get_device(ctx, i); const char *name = iio_device_get_name(dev); if (!strcmp(id, iio_device_get_id(dev)) || (name && !strcmp(name, id))) return dev; } return NULL; }
struct extra_ctx_info *iioc_ctx_open(void) { struct extra_ctx_info *ctx_info = (struct extra_ctx_info *)calloc(1, sizeof(*ctx_info)); if (!ctx_info) { IIOC_DBG("Can not calloc memory for struct extra_ctx_info.\n"); return NULL; } ctx_info->ctx = iio_create_local_context(); if (!ctx_info->ctx) { IIOC_DBG("iio_create_local_context error.\n"); return NULL; } ctx_info->nb_devices = iio_context_get_devices_count(ctx_info->ctx); IIOC_DBG("There are %d devices in local.\n", ctx_info->nb_devices); if (ctx_info->nb_devices == 0) { IIOC_DBG("No device found.\n"); return NULL; } return ctx_info; //for (i = 0; i < ctx_info->nb_devices; i++) { // struct iio_device *dev = iio_context_get_device(ctx_info->ctx, i); // char *dev_name = iio_device_get_name(dev); // char *dev_id = iio_device_get_id(dev); // unsigned int nb_channels = iio_device_get_channels_count(dev); // unsigned int nb_attrs = iio_device_get_attrs_count(dev); // // // IIOC_DBG("Device%d: %s - %s, Channels count: %d, attritubes count: %d, is_input_device: %s.\n", i, dev_id, dev_name, nb_channels, nb_attrs, dev_info->input_device ? "yes" : "no"); // for (j = 0; j < nb_channels; j++) { // struct iio_channel *ch = iio_device_get_channel(dev, j); // char *ch_id = iio_channel_get_id(ch); // char *ch_name = iio_channel_get_name(ch); // IIOC_DBG("\tCH%d: %s - %s.\n", j, ch_id, ch_name); // struct extra_chn_info *info = calloc(1, sizeof(*info)); // info->dev = dev; // iio_channel_set_data(ch, info); // } // for (k = 0; k < nb_attrs; k++) { // char *attr_name = iio_device_get_attr(dev, k); // IIOC_DBG("\t>> Attr%d: %s.\n", k, attr_name); // } //} }
static bool dmm_identify(const struct osc_plugin *plugin) { /* Use the OSC's IIO context just to detect the devices */ struct iio_context *osc_ctx = get_context_from_osc(); unsigned int i, num; bool ret = false; num = iio_context_get_devices_count(osc_ctx); for (i = 0; !ret && i < num; i++) { struct iio_device *dev = iio_context_get_device(osc_ctx, i); unsigned int j, nch = iio_device_get_channels_count(dev); for (j = 0; !ret && j < nch; j++) { struct iio_channel *chn = iio_device_get_channel(dev, j); if (is_valid_dmm_channel(chn)) ret = true; } } return ret; }
static struct iio_device * get_device(const struct iio_context *ctx, const char *id) { unsigned int i, nb_devices = iio_context_get_devices_count(ctx); struct iio_device *device; for (i = 0; i < nb_devices; i++) { const char *name; device = iio_context_get_device(ctx, i); name = iio_device_get_name(device); if (name && !strcmp(name, id)) break; if (!strcmp(id, iio_device_get_id(device))) break; } if (i < nb_devices) return device; fprintf(stderr, "Device %s not found\n", id); return NULL; }
/* 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
static bool connect_fillin(Dialogs *data) { GtkTextBuffer *buf; GtkTextIter iter; char text[256]; unsigned int num; size_t i; struct iio_context *ctx; const char *desc; #ifdef FRU_FILES char eprom_names[128]; unsigned char *raw_input_data = NULL; FILE *efp, *fp; struct stat st; /* flushes all open output streams */ fflush(NULL); #if DEBUG fp = popen("find ./ -name \"fru*.bin\"", "r"); #else fp = popen("find /sys -name eeprom 2>/dev/null", "r"); #endif if(fp == NULL) { fprintf(stderr, "can't execute find\n"); return false; } buf = gtk_text_buffer_new(NULL); gtk_text_buffer_get_iter_at_offset(buf, &iter, 0); num = 0; while(fgets(eprom_names, sizeof(eprom_names), fp) != NULL) { num++; /* strip trailing new lines */ if (eprom_names[strlen(eprom_names) - 1] == '\n') eprom_names[strlen(eprom_names) - 1] = '\0'; /* FRU EEPROMS are exactly 256 */ if(stat(eprom_names, &st) !=0) continue; if(st.st_size != 256) { printf("skipping %s (size == %d)\n", eprom_names, (int)st.st_size); continue; } i = 0; if (!is_eeprom_fru(eprom_names, buf, &iter)) { /* Wasn't a FRU file, but is it a blank, writeable EEPROM? */ efp = fopen(eprom_names, "w+"); if (efp) { i = fread(text, 1, 256, efp); if (i == 256) { for (i = 0; i < 256; i++) { if (!(text[i] == 0x00 || ((unsigned char) text[i]) == 0xFF)) { i = 0; break; } } } fclose(efp); /* dump the info into it */ if (i == 256) { if (write_fru(eprom_names)) if(!is_eeprom_fru(eprom_names, buf, &iter)) i = 0; } } else { int errsv = errno; printf("Can't open %s in %s\n%s\n", eprom_names, __func__, strerror(errsv)); } if (i == 0) { sprintf(text, "No FRU information in %s\n", eprom_names); gtk_text_buffer_insert(buf, &iter, text, -1); } } free (raw_input_data); } pclose(fp); if (!num) { sprintf(text, "No eeprom files found in /sys/\n"); gtk_text_buffer_insert(buf, &iter, text, -1); } gtk_text_view_set_buffer(GTK_TEXT_VIEW(data->connect_fru), buf); g_object_unref(buf); #endif ctx = get_context(data); desc = ctx ? iio_context_get_description(ctx) : ""; buf = gtk_text_buffer_new(NULL); gtk_text_buffer_get_iter_at_offset(buf, &iter, 0); gtk_text_buffer_insert(buf, &iter, desc, -1); gtk_text_view_set_buffer(GTK_TEXT_VIEW(data->ctx_info), buf); g_object_unref(buf); buf = gtk_text_buffer_new(NULL); gtk_text_buffer_get_iter_at_offset(buf, &iter, 0); num = ctx ? iio_context_get_devices_count(ctx) : 0; if (num > 0) { for (i = 0; i < num; i++) { struct iio_device *dev = iio_context_get_device(ctx, i); sprintf(text, "%s\n", iio_device_get_name(dev)); gtk_text_buffer_insert(buf, &iter, text, -1); } } else { sprintf(text, "No iio devices found\n"); gtk_text_buffer_insert(buf, &iter, text, -1); } gtk_text_view_set_buffer(GTK_TEXT_VIEW(data->connect_iio), buf); g_object_unref(buf); if (ctx) iio_context_destroy(ctx); return !!ctx; }
int main(int argc, char **argv) { struct iio_context *ctx; int c, option_index = 0, arg_index = 0; enum backend backend = LOCAL; unsigned int major, minor; char git_tag[8]; int ret; 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) { ERROR("-x and -n are mutually exclusive\n"); return EXIT_FAILURE; } backend = NETWORK; arg_index += 2; break; case 'x': if (backend != LOCAL) { ERROR("-x and -n are mutually exclusive\n"); return EXIT_FAILURE; } backend = XML; arg_index += 2; break; case '?': return EXIT_FAILURE; } } if (arg_index >= argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(); return EXIT_FAILURE; } iio_library_get_version(&major, &minor, git_tag); INFO("Library version: %u.%u (git tag: %s)\n", major, minor, git_tag); if (backend == XML) ctx = iio_create_xml_context(argv[arg_index]); else if (backend == NETWORK) ctx = iio_create_network_context(argv[arg_index]); else ctx = iio_create_local_context(); if (!ctx) { ERROR("Unable to create IIO context\n"); return EXIT_FAILURE; } INFO("IIO context created with %s backend.\n", iio_context_get_name(ctx)); ret = iio_context_get_version(ctx, &major, &minor, git_tag); if (!ret) INFO("Backend version: %u.%u (git tag: %s)\n", major, minor, git_tag); else ERROR("Unable to get backend version: %i\n", ret); unsigned int nb_devices = iio_context_get_devices_count(ctx); INFO("IIO context has %u devices:\n", nb_devices); unsigned int i; for (i = 0; i < nb_devices; i++) { const struct iio_device *dev = iio_context_get_device(ctx, i); const char *name = iio_device_get_name(dev); INFO("\t%s: %s\n", iio_device_get_id(dev), name ? name : "" ); unsigned int nb_channels = iio_device_get_channels_count(dev); INFO("\t\t%u channels found:\n", nb_channels); unsigned int j; for (j = 0; j < nb_channels; j++) { struct iio_channel *ch = iio_device_get_channel(dev, j); const char *type_name; if (iio_channel_is_output(ch)) type_name = "output"; else type_name = "input"; name = iio_channel_get_name(ch); INFO("\t\t\t%s: %s (%s)\n", iio_channel_get_id(ch), name ? name : "", type_name); unsigned int nb_attrs = iio_channel_get_attrs_count(ch); if (!nb_attrs) continue; INFO("\t\t\t%u channel-specific attributes found:\n", nb_attrs); unsigned int k; for (k = 0; k < nb_attrs; k++) { const char *attr = iio_channel_get_attr(ch, k); char buf[1024]; ret = (int) iio_channel_attr_read(ch, attr, buf, 1024); if (ret > 0) INFO("\t\t\t\tattr %u: %s" " value: %s\n", k, attr, buf); else if (ret == -ENOSYS) INFO("\t\t\t\tattr %u: %s\n", k, attr); else ERROR("Unable to read attribute %s\n", attr); } } unsigned int nb_attrs = iio_device_get_attrs_count(dev); if (!nb_attrs) continue; INFO("\t\t%u device-specific attributes found:\n", nb_attrs); for (j = 0; j < nb_attrs; j++) { const char *attr = iio_device_get_attr(dev, j); char buf[1024]; ret = (int) iio_device_attr_read(dev, attr, buf, 1024); if (ret > 0) INFO("\t\t\t\tattr %u: %s value: %s\n", j, attr, buf); else if (ret == -ENOSYS) INFO("\t\t\t\tattr %u: %s\n", j, attr); else ERROR("Unable to read attribute: %s\n", attr); } } iio_context_destroy(ctx); return EXIT_SUCCESS; }
/* 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; }