ssize_t iio_buffer_push_partial(struct iio_buffer *buffer, size_t samples_count) { size_t new_len = samples_count * buffer->dev_sample_size; if (new_len == 0 || new_len > buffer->length) return -EINVAL; buffer->data_length = new_len; return iio_buffer_push(buffer); }
static gboolean fillBuffer(void) { unsigned int i; uint8_t *buf; int ret; while (true) { buf = iio_buffer_start(dac_buff); for (i = 0; i < IIO_BUFFER_SIZE; i++) { buf[i] = soft_buffer_ch0[current_sample]; current_sample++; if (current_sample >= buffer_size) current_sample = 0; } ret = iio_buffer_push(dac_buff); if (ret < 0) printf("Error occured while writing to buffer: %d\n", ret); } return TRUE; }
/* 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
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; }
/* 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; }