int iioc_sampling_capture(struct iio_device *adc_dev) { unsigned int i; off_t offset = 0; assert(adc_dev); struct extra_dev_info *dev_info = iio_device_get_data(adc_dev); ssize_t sample_count = dev_info->sample_count; unsigned int nb_channels = dev_info->nb_channels; if (dev_info->buffer == NULL) { dev_info->buffer_size = sample_count; dev_info->buffer = iio_device_create_buffer(adc_dev, sample_count, false); if (!dev_info->buffer) { IIOC_DBG("Error: Unable to create buffer: %s\n", strerror(errno)); return -errno; } } /* Reset the data offset for all channels */ 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); chn_info->offset = 0; } IIOC_DBG("Enter buffer refill loop.\n"); while (true) { ssize_t ret = iio_buffer_refill(dev_info->buffer); if (ret < 0) { IIOC_DBG("Error while reading data: %s\n", strerror(-ret)); return ret; } else { IIOC_DBG("Read %d bytes from buffer.\n", ret); } ret /= iio_buffer_step(dev_info->buffer); if (ret >= sample_count) { iio_buffer_foreach_sample( dev_info->buffer, demux_sample, NULL); if (ret >= sample_count * 2) { printf("Decreasing buffer size\n"); iio_buffer_destroy(dev_info->buffer); dev_info->buffer_size /= 2; dev_info->buffer = iio_device_create_buffer(adc_dev, dev_info->buffer_size, false); } break; } printf("Increasing buffer size\n"); iio_buffer_destroy(dev_info->buffer); dev_info->buffer_size *= 2; dev_info->buffer = iio_device_create_buffer(adc_dev, dev_info->buffer_size, false); } return 0; }
void iioc_dev_close(struct iio_device *dev) { unsigned int j; struct extra_dev_info *dev_info = iio_device_get_data(dev); if (!dev_info) return; unsigned int nb_channels = iio_device_get_channels_count(dev); for (j = 0; j < nb_channels; j++) { struct iio_channel *ch = iio_device_get_channel(dev, j); struct extra_chn_info *chn_info = iio_channel_get_data(ch); if (chn_info->data_ref) { free(chn_info->data_ref); chn_info->data_ref = NULL; } if (chn_info) { free(chn_info); chn_info = NULL; } } if (dev_info->buffer) { iio_buffer_destroy(dev_info->buffer); dev_info->buffer = NULL; dev_info->buffer_size = 0; } free(dev_info); dev_info = NULL; }
static int buffer_close() { iio_buffer_destroy(dac_buff); dac_buff = NULL; return 0; }
/* cleanup and exit */ static void shutdown() { printf("* Destroying buffers\n"); if (rxbuf) { iio_buffer_destroy(rxbuf); } if (txbuf) { iio_buffer_destroy(txbuf); } printf("* Disabling streaming channels\n"); if (rx0_i) { iio_channel_disable(rx0_i); } if (rx0_q) { iio_channel_disable(rx0_q); } if (tx0_i) { iio_channel_disable(tx0_i); } if (tx0_q) { iio_channel_disable(tx0_q); } printf("* Destroying context\n"); if (ctx) { iio_context_destroy(ctx); } exit(0); }
static void context_destroy(const char *ini_fn) { if (dac_buff) { iio_buffer_destroy(dac_buff); dac_buff = NULL; } iio_context_destroy(ctx); iio_context_destroy(thread_ctx); }
/* cleanup and exit */ void shutdowniio(int s) { sec_end=time(NULL); sec_elapsed=sec_end-sec_begin; printf("\n\nTime elapsed: %lus \tBytes recived %llu \tDebit %llu MB/sec \n ", sec_elapsed, count/1024/1024, count/sec_elapsed/1024/1024); printf("\nSocket sended %lld",sendcount); printf("* Destroying buffers\n"); if (rxbuf) { iio_buffer_destroy(rxbuf); } if (txbuf) { iio_buffer_destroy(txbuf); } printf("* Disabling streaming channels\n"); if (rx0_i) { iio_channel_disable(rx0_i); } if (rx0_q) { iio_channel_disable(rx0_q); } if (tx0_i) { iio_channel_disable(tx0_i); } if (tx0_q) { iio_channel_disable(tx0_q); } printf("* Destroying context\n"); if (ctx) { iio_context_destroy(ctx); } exit(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; }
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 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; }
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; }