static int network_open(const struct iio_device *dev, size_t samples_count, bool cyclic) { struct iio_context_pdata *pdata = dev->ctx->pdata; struct iio_device_pdata *ppdata = dev->pdata; int fd, ret = -EBUSY; iio_mutex_lock(ppdata->lock); if (ppdata->fd >= 0) goto out_mutex_unlock; ret = create_socket(pdata->addrinfo); if (ret < 0) goto out_mutex_unlock; fd = ret; ret = iiod_client_open_unlocked(pdata->iiod_client, fd, dev, samples_count, cyclic); if (ret < 0) { close(fd); goto out_mutex_unlock; } ppdata->is_tx = iio_device_is_tx(dev); ppdata->is_cyclic = cyclic; ppdata->fd = fd; ppdata->wait_for_err_code = false; #ifdef WITH_NETWORK_GET_BUFFER ppdata->mmap_len = samples_count * iio_device_get_sample_size(dev); #endif out_mutex_unlock: iio_mutex_unlock(ppdata->lock); return ret; }
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; }
struct iio_buffer * iio_device_create_buffer(const struct iio_device *dev, size_t samples_count, bool cyclic) { int ret = -EINVAL; struct iio_buffer *buf; unsigned int sample_size = iio_device_get_sample_size(dev); if (!sample_size) goto err_set_errno; buf = malloc(sizeof(*buf)); if (!buf) { ret = -ENOMEM; goto err_set_errno; } buf->dev_sample_size = sample_size; buf->length = sample_size * samples_count; buf->dev = dev; buf->mask = calloc(dev->words, sizeof(*buf->mask)); if (!buf->mask) { ret = -ENOMEM; goto err_free_buf; } /* Set the default channel mask to the one used by the device. * While input buffers will erase this as soon as the refill function * is used, it is useful for output buffers, as it permits * iio_buffer_foreach_sample to be used. */ memcpy(buf->mask, dev->mask, dev->words * sizeof(*buf->mask)); ret = iio_device_open(dev, samples_count, cyclic); if (ret < 0) goto err_free_mask; buf->dev_is_high_speed = device_is_high_speed(dev); if (buf->dev_is_high_speed) { /* Dequeue the first buffer, so that buf->buffer is correctly * initialized */ buf->buffer = NULL; if (iio_device_is_tx(dev)) { ret = dev->ctx->ops->get_buffer(dev, &buf->buffer, buf->length, buf->mask, dev->words); if (ret < 0) goto err_close_device; } } else { buf->buffer = malloc(buf->length); if (!buf->buffer) { ret = -ENOMEM; goto err_close_device; } } buf->sample_size = iio_device_get_sample_size_mask(dev, buf->mask, dev->words); buf->data_length = buf->length; return buf; err_close_device: iio_device_close(dev); err_free_mask: free(buf->mask); err_free_buf: free(buf); err_set_errno: errno = -ret; return NULL; }
int iioc_sampling_setup(struct iio_device *adc_dev, struct iio_device *trigger_dev, unsigned int sampling_freq, unsigned int sample_count ) { unsigned int i; int ret; unsigned int min_timeout = 2000; if (!adc_dev || !trigger_dev) return -ENODEV; unsigned int nb_channels = iio_device_get_channels_count(adc_dev); if (nb_channels == 0) { IIOC_DBG("There is 0 Channel in adc_device.\n"); return -EIO; } unsigned int sample_size = iio_device_get_sample_size(adc_dev); if (sample_size == 0) { IIOC_DBG("Sample Size is 0.\n"); return -ENODATA; } struct extra_dev_info *dev_info = iio_device_get_data(adc_dev); if (dev_info->input_device == false) { IIOC_DBG("adc_dev is not an input device.\n"); return -EIO; } //°ó¶¨trigger ret = iio_device_set_trigger(adc_dev, trigger_dev); if (ret) { #ifdef _DEBUG const char *trigger_name = iio_device_get_name(trigger_dev); const char *adc_name = iio_device_get_name(adc_dev); IIOC_DBG("Can not bind the %s with %s.\n", trigger_name, adc_name); #endif return -EIO; } //ɾ³ý¾Ébuffer if (dev_info->buffer) iio_buffer_destroy(dev_info->buffer); dev_info->buffer = NULL; //ÉèÖÃsample_count(buffer´óС) dev_info->sample_count = sample_count; //ʹÄÜͨµÀ£¬²¢ÎªÃ¿¸öͨµÀ·ÖÅäÄÚ´æ¿Õ¼ä for (i = 0; i < nb_channels; i++) { struct iio_channel *ch = iio_device_get_channel(adc_dev, i); struct extra_chn_info *chn_info = iio_channel_get_data(ch); if (chn_info->enabled) iio_channel_enable(ch); else iio_channel_disable(ch); if (chn_info->data_ref) free(chn_info->data_ref); chn_info->data_ref = (int16_t *)calloc(dev_info->sample_count, sizeof(int16_t)); if (!chn_info->data_ref) { IIOC_DBG("Can not calloc channel data mem.\n"); goto error_calloc_chn_data_ref; } } dev_info->sampling_freq = sampling_freq; //ÖØаó¶¨Êý¾Ý iio_device_set_data(adc_dev, dev_info); //ÉèÖó¬Ê± if (sampling_freq > 0) { /* 2 x capture time + 2s */ unsigned int timeout = dev_info->sample_count * 1000 / sampling_freq; timeout += 2000; if (timeout > min_timeout) min_timeout = timeout; } if (dev_info->ctx_info->ctx) iio_context_set_timeout(dev_info->ctx_info->ctx, min_timeout); return 0; error_calloc_chn_data_ref: for (i = 0; i < nb_channels; i++) { struct iio_channel *ch = iio_device_get_channel(adc_dev, i); struct extra_chn_info *chn_info = iio_channel_get_data(ch); if (chn_info->data_ref) free(chn_info->data_ref); } return -ENOMEM; }
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; }
/* 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; }