diff --git a/host/common/include/devcfg.h b/host/common/include/devcfg.h new file mode 100644 index 00000000..877916a8 --- /dev/null +++ b/host/common/include/devcfg.h @@ -0,0 +1,142 @@ +/* + * This file is part of the bladeRF project: + * http://www.github.com/nuand/bladeRF + * + * Copyright (C) 2014-2015 Nuand LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef DEVCFG_H_ +#define DEVCFG_H_ + +#include +#include + +#include "rel_assert.h" +#include "host_config.h" + +#define DEVCFG_OPTIONS_BASE "hd:l:v:s:b:f:" + +/** + * Device configuration parameters + */ +struct devcfg { + char *device_specifier; + + unsigned int tx_frequency; + unsigned int tx_bandwidth; + unsigned int tx_samplerate; + int txvga1; + int txvga2; + + unsigned int rx_frequency; + unsigned int rx_bandwidth; + unsigned int rx_samplerate; + bladerf_lna_gain lnagain; + int rxvga1; + int rxvga2; + + bladerf_loopback loopback; + + bladerf_log_level verbosity; + + bool enable_xb200; + + unsigned int samples_per_buffer; + unsigned int num_buffers; + unsigned int num_transfers; + unsigned int stream_timeout_ms; + unsigned int sync_timeout_ms; +}; + +/** + * Initialize the specified device_config structure to default values + * + * @param c Configuration to initialize + */ +void devcfg_init(struct devcfg *c); + +/** + * Deinitialize any items allocated in the provided device configuration + * + * @param c Configuration to deinitialize + */ +void devcfg_deinit(struct devcfg *c); + +/** + * Creates an option array with the app-specific options appended to the common + * devcfg options. The app_options should be 0-terminated. + * + * The caller is responsible for freeing the resturned structure. + * + * @return Heap-allocated array on success, NULL on failure + */ +struct option *devcfg_get_long_options(const struct option *app_options); + +/** + * Handle command line arguments and update the specified device configuration + * accordingly. + * + * @param argc # of arguments in argv + * @param argv Array of arguments + * + * @param option_str Options string to pass to getopt_long(). This should + * include DEVCFG_OPTIONS_BASE, with any + * application-handled options appended. + * + * @param long_options List of long options created by caller + * + * @param c Device configuration to update. It should have + * already been initialized via devcfg_init() + * + * @return 0 on success, -1 on failure, 1 if help is requested + */ +int devcfg_handle_args(int argc, char **argv, + const char *options, const struct option *long_options, + struct devcfg *c); + +/** + * Wrapper around bladerf_sync_config, using parameters in the specified + * devcfg. Optionally, enable the associated module. + */ +int devcfg_perform_sync_config(struct bladerf *dev, + bladerf_module module, + bladerf_format format, + struct devcfg *config, + bool enable_module); + + +/** + * Apply the provided device_config to the specified device + * + * @param dev Device to configure + * @param c Configuration parameters + * + * @return 0 on succes, -1 on failure + */ +int devcfg_apply(struct bladerf *dev, const struct devcfg *c); + +/** + * Print help info about arguments handled by devcfg_handle_args() + * + * @param title Optional title to print before usage information + */ +void devcfg_print_common_help(const char *title); + +#endif diff --git a/host/common/src/devcfg.c b/host/common/src/devcfg.c new file mode 100644 index 00000000..072b26cf --- /dev/null +++ b/host/common/src/devcfg.c @@ -0,0 +1,631 @@ +/* + * This file is part of the bladeRF project: + * http://www.github.com/nuand/bladeRF + * + * Copyright (C) 2014-2015 Nuand LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include +#include +#include +#include +#include +#include +#include + +#include "devcfg.h" +#include "conversions.h" + +#define DEVCFG_DFLT_TX_FREQ 1000000000 +#define DEVCFG_DFLT_TXVGA1 (-14) +#define DEVCFG_DFLT_TXVGA2 0 + +#define DEVCFG_DFLT_RX_FREQ 1000000000 +#define DEVCFG_DFLT_LNAGAIN BLADERF_LNA_GAIN_MAX +#define DEVCFG_DFLT_RXVGA1 30 +#define DEVCFG_DFLT_RXVGA2 0 + +#define DEVCFG_DFLT_SAMPLERATE 2000000 +#define DEVCFG_DFLT_BANDWIDTH 1500000 + +#define DEVCFG_DFLT_SAMPLES_PER_BUF 8192 +#define DEVCFG_DFLT_NUM_BUFFERS 64 +#define DEVCFG_DFLT_NUM_TRANSFERS 16 +#define DEVCFG_DFLT_STREAM_TIMEMOUT_MS 5000 +#define DEVCFG_DFLT_SYNC_TIMEOUT_MS (2 * DEVCFG_DFLT_STREAM_TIMEMOUT_MS) + +#define OPTION_HELP 'h' +#define OPTION_DEVICE 'd' +#define OPTION_LOOPBACK 'l' +#define OPTION_VERBOSITY 'v' +#define OPTION_SAMPLERATE 's' +#define OPTION_RX_SAMPLERATE 0x80 +#define OPTION_TX_SAMPLERATE 0x81 +#define OPTION_BANDWIDTH 'b' +#define OPTION_RX_BANDWIDTH 0x82 +#define OPTION_TX_BANDWIDTH 0x83 +#define OPTION_FREQUENCY 'f' +#define OPTION_RX_FREQUENCY 0x84 +#define OPTION_TX_FREQUENCY 0x85 +#define OPTION_LNAGAIN 0x86 +#define OPTION_RXVGA1 0x87 +#define OPTION_RXVGA2 0x88 +#define OPTION_TXVGA1 0x89 +#define OPTION_TXVGA2 0x8a +#define OPTION_SAMPLES_PER_BUF 0x91 +#define OPTION_NUM_BUFFERS 0x92 +#define OPTION_NUM_TRANSFERS 0x93 +#define OPTION_STREAM_TIMEOUT 0x94 +#define OPTION_SYNC_TIMEOUT 0x95 +#define OPTION_ENABLE_XB200 0x96 + +static const numeric_suffix hz_suffixes[] = { + { "K", 1000 }, + { "KHz", 1000 }, + { "M", 1000000 }, + { "MHz", 1000000 }, + { "G", 1000000000 }, + { "GHz", 1000000000 }, +}; + +static const struct option devcfg_long_options[] = +{ + { "help", no_argument, 0, OPTION_HELP }, + { "device", required_argument, 0, OPTION_DEVICE }, + { "verbosity", required_argument, 0, OPTION_VERBOSITY }, + + { "samplerate", required_argument, 0, OPTION_SAMPLERATE }, + { "rx-samplerate", required_argument, 0, OPTION_RX_SAMPLERATE }, + { "tx-samplerate", required_argument, 0, OPTION_TX_SAMPLERATE }, + + { "bandwidth", required_argument, 0, OPTION_BANDWIDTH }, + { "rx-bandwidth", required_argument, 0, OPTION_RX_BANDWIDTH }, + { "tx-bandwidth", required_argument, 0, OPTION_TX_BANDWIDTH }, + + { "frequency", required_argument, 0, OPTION_FREQUENCY }, + { "rx-frequency", required_argument, 0, OPTION_RX_FREQUENCY }, + { "tx-frequency", required_argument, 0, OPTION_TX_FREQUENCY }, + + { "lna-gain", required_argument, 0, OPTION_LNAGAIN }, + { "rxvga1-gain", required_argument, 0, OPTION_RXVGA1 }, + { "rxvga2-gain", required_argument, 0, OPTION_RXVGA2 }, + + { "txvga1-gain", required_argument, 0, OPTION_TXVGA1 }, + { "txvga2-gain", required_argument, 0, OPTION_TXVGA2 }, + + { "loopback", required_argument, 0, OPTION_LOOPBACK }, + + { "xb200", no_argument, 0, OPTION_ENABLE_XB200 }, + + { "samples-per-buffer", required_argument, 0, OPTION_SAMPLES_PER_BUF }, + { "num-buffers", required_argument, 0, OPTION_NUM_BUFFERS }, + { "num-transfers", required_argument, 0, OPTION_NUM_TRANSFERS }, + { "stream-timeout", required_argument, 0, OPTION_STREAM_TIMEOUT }, + { "sync-timeout", required_argument, 0, OPTION_SYNC_TIMEOUT }, +}; + +void devcfg_init(struct devcfg *c) +{ + c->device_specifier = NULL; + + c->tx_frequency = DEVCFG_DFLT_TX_FREQ; + c->tx_bandwidth = DEVCFG_DFLT_BANDWIDTH; + c->tx_samplerate = DEVCFG_DFLT_SAMPLERATE; + c->txvga1 = DEVCFG_DFLT_TXVGA1; + c->txvga2 = DEVCFG_DFLT_TXVGA2; + + c->rx_frequency = DEVCFG_DFLT_TX_FREQ; + c->rx_bandwidth = DEVCFG_DFLT_BANDWIDTH; + c->rx_samplerate = DEVCFG_DFLT_SAMPLERATE; + c->lnagain = DEVCFG_DFLT_LNAGAIN; + c->rxvga1 = DEVCFG_DFLT_RXVGA1; + c->rxvga2 = DEVCFG_DFLT_RXVGA2; + + c->loopback = BLADERF_LB_NONE; + + c->verbosity = BLADERF_LOG_LEVEL_INFO; + + c->enable_xb200 = false; + + c->samples_per_buffer = DEVCFG_DFLT_SAMPLES_PER_BUF; + c->num_buffers = DEVCFG_DFLT_NUM_BUFFERS; + c->num_transfers = DEVCFG_DFLT_NUM_TRANSFERS; + c->stream_timeout_ms = DEVCFG_DFLT_STREAM_TIMEMOUT_MS; + c->sync_timeout_ms = DEVCFG_DFLT_SYNC_TIMEOUT_MS; +} + +void devcfg_deinit(struct devcfg *c) +{ + free(c->device_specifier); +} + +void devcfg_print_common_help(const char *title) +{ + if (title != NULL) { + printf("%s", title); + } + + printf(" -h, --help Show this help text.\n"); + printf(" -d, --device Open the specified device.\n"); + printf(" -v, --verbosity Set the libbladeRF verbosity level.\n"); + printf(" --xb200 Enable the XB-200. This will remain enabled\n"); + printf(" until the device is power-cycled.\n"); + printf("\n"); + printf(" -f, --frequency Set RX and TX to the specified frequency.\n"); + printf(" --rx-frequency Set RX to the specified frequency.\n"); + printf(" --tx-frequency Set TX to the specified frequency.\n"); + printf("\n"); + printf(" -s, --samplerate Set RX and TX to the specified sample rate.\n"); + printf(" --rx-samplerate Set RX to the specified sample rate.\n"); + printf(" --tx-samplerate Set RX to the specified sample rate.\n"); + printf("\n"); + printf(" -b, --bandwidth Set RX and TX to the specified bandwidth.\n"); + printf(" --rx-bandwidth Set RX to the specified bandwidth.\n"); + printf(" --tx-bandwidth Set TX to the specified bandwidth.\n"); + printf("\n"); + printf(" --lna-gain Set the RX LNA to the specified gain.\n"); + printf(" Options are: bypass, mid, max\n"); + printf(" --rxvga1-gain Set RX VGA1 to the specified gain.\n"); + printf(" --rxvga2-gain Set RX VGA2 to the specified gain.\n"); + printf(" --txvga1-gain Set TX VGA1 to the specified gain.\n"); + printf(" --txvga2-gain Set TX VGA2 to the specified gain.\n"); + printf("\n"); + printf(" --num-buffers Allocate sample buffers.\n"); + printf(" --samples-per-buffer Allocate samples in each sample buffer.\n"); + printf(" --num-transfers Utilize up to simultaneous USB transfers.\n"); + printf(" --stream-timeout Set stream timeout to milliseconds.\n"); + printf(" --sync-timeout Set sync function timeout to milliseconds.\n"); + +} + +struct option * devcfg_get_long_options(const struct option *app_options) +{ + struct option *ret; + size_t app_size = 0; + const size_t devcfg_size = sizeof(devcfg_long_options); + + while (app_options[app_size].name != NULL) { + app_size++; + } + + /* Account for 0-entry */ + app_size = (app_size + 1) * sizeof(app_options[0]); + + ret = malloc(devcfg_size + app_size); + + if (ret != NULL) { + memcpy(ret, &devcfg_long_options, devcfg_size); + memcpy(ret + ARRAY_SIZE(devcfg_long_options) , app_options, app_size); + } + + return ret; +} + +int devcfg_handle_args(int argc, char **argv, const char *option_str, + const struct option *long_options, struct devcfg *config) +{ + int c; + bool ok; + int status; + unsigned int freq_min; + + while ((c = getopt_long(argc, argv, option_str, long_options, NULL)) >= 0) { + + if (config->enable_xb200) { + freq_min = BLADERF_FREQUENCY_MIN_XB200; + } else { + freq_min = BLADERF_FREQUENCY_MIN; + } + + switch (c) { + + case OPTION_HELP: + return 1; + break; + + case OPTION_DEVICE: + if (config->device_specifier == NULL) { + config->device_specifier = strdup(optarg); + if (config->device_specifier == NULL) { + perror("strdup"); + return -1; + } + } + break; + + case OPTION_LOOPBACK: + status = str2loopback(optarg, &config->loopback); + if (status != 0) { + fprintf(stderr, "Invalid loopback mode: %s\n", optarg); + return -1; + } + break; + + case OPTION_VERBOSITY: + config->verbosity = str2loglevel(optarg, &ok); + if (!ok) { + fprintf(stderr, "Invalid verbosity level: %s\n", optarg); + return -1; + } + break; + + case OPTION_ENABLE_XB200: + config->enable_xb200 = true; + break; + + case OPTION_FREQUENCY: + config->rx_frequency = + str2uint_suffix(optarg, + freq_min, + BLADERF_FREQUENCY_MAX, + hz_suffixes, ARRAY_SIZE(hz_suffixes), + &ok); + + if (!ok) { + fprintf(stderr, "Invalid frequency: %s\n", optarg); + return -1; + } else { + config->tx_frequency = config->rx_frequency; + } + break; + + case OPTION_RX_FREQUENCY: + config->rx_frequency = + str2uint_suffix(optarg, + freq_min, + BLADERF_FREQUENCY_MAX, + hz_suffixes, ARRAY_SIZE(hz_suffixes), + &ok); + + if (!ok) { + fprintf(stderr, "Invalid RX frequency: %s\n", optarg); + return -1; + } + break; + + case OPTION_TX_FREQUENCY: + config->tx_frequency = + str2uint_suffix(optarg, + freq_min, + BLADERF_FREQUENCY_MAX, + hz_suffixes, ARRAY_SIZE(hz_suffixes), + &ok); + + if (!ok) { + fprintf(stderr, "Invalid TX frequency: %s\n", optarg); + return -1; + } + break; + + case OPTION_SAMPLERATE: + config->rx_samplerate = + str2uint_suffix(optarg, + BLADERF_SAMPLERATE_MIN, + BLADERF_SAMPLERATE_REC_MAX, + hz_suffixes, ARRAY_SIZE(hz_suffixes), + &ok); + if (!ok) { + fprintf(stderr, "Invalid sample rate: %s\n", optarg); + return -1; + } else { + config->tx_samplerate = config->rx_samplerate; + } + break; + + case OPTION_RX_SAMPLERATE: + config->rx_samplerate = + str2uint_suffix(optarg, + BLADERF_SAMPLERATE_MIN, + BLADERF_SAMPLERATE_REC_MAX, + hz_suffixes, ARRAY_SIZE(hz_suffixes), + &ok); + if (!ok) { + fprintf(stderr, "Invalid RX sample rate: %s\n", optarg); + return -1; + } + break; + + case OPTION_TX_SAMPLERATE: + config->tx_samplerate = + str2uint_suffix(optarg, + BLADERF_SAMPLERATE_MIN, + BLADERF_SAMPLERATE_REC_MAX, + hz_suffixes, ARRAY_SIZE(hz_suffixes), + &ok); + if (!ok) { + fprintf(stderr, "Invalid TX sample rate: %s\n", optarg); + return -1; + } + break; + + case OPTION_BANDWIDTH: + config->rx_bandwidth = + str2uint_suffix(optarg, + BLADERF_BANDWIDTH_MIN, + BLADERF_BANDWIDTH_MAX, + hz_suffixes, ARRAY_SIZE(hz_suffixes), + &ok); + if (!ok) { + fprintf(stderr, "Invalid bandwidth: %s\n", optarg); + return -1; + } else { + config->tx_bandwidth = config->rx_bandwidth; + } + break; + + case OPTION_RX_BANDWIDTH: + config->rx_bandwidth = + str2uint_suffix(optarg, + BLADERF_BANDWIDTH_MIN, + BLADERF_BANDWIDTH_MAX, + hz_suffixes, ARRAY_SIZE(hz_suffixes), + &ok); + if (!ok) { + fprintf(stderr, "Invalid RX bandwidth: %s\n", optarg); + return -1; + } + break; + + case OPTION_TX_BANDWIDTH: + config->tx_bandwidth = + str2uint_suffix(optarg, + BLADERF_BANDWIDTH_MIN, + BLADERF_BANDWIDTH_MAX, + hz_suffixes, ARRAY_SIZE(hz_suffixes), + &ok); + if (!ok) { + fprintf(stderr, "Invalid TX bandwidth: %s\n", optarg); + return -1; + } + break; + + case OPTION_LNAGAIN: + status = str2lnagain(optarg, &config->lnagain); + if (status != 0) { + fprintf(stderr, "Invalid RX LNA gain: %s\n", optarg); + return -1; + } + break; + + case OPTION_RXVGA1: + config->rxvga1 = str2int(optarg, + BLADERF_RXVGA1_GAIN_MIN, + BLADERF_RXVGA1_GAIN_MAX, + &ok); + + if (!ok) { + fprintf(stderr, "Invalid RXVGA1 gain: %s\n", optarg); + return -1; + } + break; + + case OPTION_RXVGA2: + config->rxvga2 = str2int(optarg, + BLADERF_RXVGA2_GAIN_MIN, + BLADERF_RXVGA2_GAIN_MAX, + &ok); + + if (!ok) { + fprintf(stderr, "Invalid RXVGA1 gain: %s\n", optarg); + return -1; + } + break; + + case OPTION_TXVGA1: + config->txvga1 = str2int(optarg, + BLADERF_TXVGA1_GAIN_MIN, + BLADERF_TXVGA1_GAIN_MAX, + &ok); + + if (!ok) { + fprintf(stderr, "Invalid TXVGA1 gain: %s\n", optarg); + return -1; + } + break; + + case OPTION_TXVGA2: + config->txvga2 = str2int(optarg, + BLADERF_TXVGA2_GAIN_MIN, + BLADERF_TXVGA2_GAIN_MAX, + &ok); + + if (!ok) { + fprintf(stderr, "Invalid TXVGA2 gain: %s\n", optarg); + return -1; + } + break; + + case OPTION_NUM_BUFFERS: + config->num_buffers = str2uint(optarg, 1, UINT_MAX, &ok); + if (!ok) { + fprintf(stderr, "Invalid buffer count: %s\n", optarg); + return -1; + } + break; + + case OPTION_SAMPLES_PER_BUF: + config->samples_per_buffer = str2uint(optarg, + 1024, UINT_MAX, &ok); + if (!ok) { + fprintf(stderr, "Invalid buffer size (in samples): %s\n", + optarg); + return -1; + } + break; + + case OPTION_NUM_TRANSFERS: + config->num_transfers = str2uint(optarg, 1, UINT_MAX, &ok); + + if (!ok) { + fprintf(stderr, "Invalid transfer count: %s\n", optarg); + return -1; + } + break; + + case OPTION_STREAM_TIMEOUT: + config->stream_timeout_ms = str2uint(optarg, 0, UINT_MAX, &ok); + if (!ok) { + fprintf(stderr, "Invalid stream timeout: %s\n", optarg); + return -1; + } + break; + + case OPTION_SYNC_TIMEOUT: + config->sync_timeout_ms = str2uint(optarg, 0, UINT_MAX, &ok); + if (!ok) { + fprintf(stderr, "Invalid sync function timeout: %s\n", optarg); + return -1; + } + break; + } + } + + return 0; +} + +int devcfg_apply(struct bladerf *dev, const struct devcfg *c) +{ + int status; + + bladerf_log_set_verbosity(c->verbosity); + + status = bladerf_set_loopback(dev, c->loopback); + if (status != 0) { + fprintf(stderr, "Failed to set loopback: %s\n", + bladerf_strerror(status)); + return -1; + } + + if (c->enable_xb200) { + status = bladerf_expansion_attach(dev, BLADERF_XB_200); + if (status != 0) { + fprintf(stderr, "Failed to attach XB-200.\n"); + return -1; + } + } + + status = bladerf_set_frequency(dev, BLADERF_MODULE_RX, c->rx_frequency); + if (status != 0) { + fprintf(stderr, "Failed to set RX frequency: %s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_frequency(dev, BLADERF_MODULE_TX, c->tx_frequency); + if (status != 0) { + fprintf(stderr, "Failed to set TX frequency: %s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_sample_rate(dev, BLADERF_MODULE_RX, c->rx_samplerate, NULL); + if (status != 0) { + fprintf(stderr, "Failed to set RX sample rate:%s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_sample_rate(dev, BLADERF_MODULE_TX, c->tx_samplerate, NULL); + if (status != 0) { + fprintf(stderr, "Failed to set TX sample rate: %s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_bandwidth(dev, BLADERF_MODULE_RX, c->rx_bandwidth, NULL); + if (status != 0) { + fprintf(stderr, "Failed to set RX bandwidth: %s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_bandwidth(dev, BLADERF_MODULE_TX, c->tx_bandwidth, NULL); + if (status != 0) { + fprintf(stderr, "Failed to set RX bandwidth: %s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_lna_gain(dev, c->lnagain); + if (status != 0) { + fprintf(stderr, "Failed to set RX LNA gain: %s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_rxvga1(dev, c->rxvga1); + if (status != 0) { + fprintf(stderr, "Failed to set RX VGA1 gain: %s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_rxvga2(dev, c->rxvga2); + if (status != 0) { + fprintf(stderr, "Failed to set RX VGA2 gain: %s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_txvga1(dev, c->txvga1); + if (status != 0) { + fprintf(stderr, "Failed to set TX VGA1 gain: %s\n", + bladerf_strerror(status)); + return -1; + } + + status = bladerf_set_txvga2(dev, c->txvga2); + if (status != 0) { + fprintf(stderr, "Failed to set TX VGA2 gain: %s\n", + bladerf_strerror(status)); + return -1; + } + + return 0; +} + +int devcfg_perform_sync_config(struct bladerf *dev, bladerf_module module, + bladerf_format format, struct devcfg *config, + bool enable_module) +{ + int status = bladerf_sync_config(dev, module, format, + config->num_buffers, + config->samples_per_buffer, + config->num_transfers, + config->stream_timeout_ms); + + if (status != 0) { + fprintf(stderr, "Failed to configure %s: %s\n", + module == BLADERF_MODULE_RX ? "RX" : "TX", + bladerf_strerror(status)); + return -1; + } + + if (enable_module) { + status = bladerf_enable_module(dev, module, true); + if (status != 0) { + fprintf(stderr, "Failed to enable %s module: %s\n", + module == BLADERF_MODULE_RX ? "RX" : "TX", + bladerf_strerror(status)); + } + } + + return 0; +} diff --git a/host/libraries/libbladeRF_test/common/include/test_common.h b/host/libraries/libbladeRF_test/common/include/test_common.h index f6db7c2f..62a55976 100644 --- a/host/libraries/libbladeRF_test/common/include/test_common.h +++ b/host/libraries/libbladeRF_test/common/include/test_common.h @@ -1,155 +1,14 @@ -/* - * This file is part of the bladeRF project: - * http://www.github.com/nuand/bladeRF - * - * Copyright (C) 2014 Nuand LLC - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ #ifndef TEST_COMMON_H_ #define TEST_COMMON_H_ #include -#include - -#include "rel_assert.h" #include "host_config.h" +#include "rel_assert.h" #if BLADERF_OS_WINDOWS -# include "clock_gettime.h" +# include "clocK_gettime.h" #endif -#define TEST_OPTIONS_BASE "hd:l:v:s:b:f:" - -/** - * Device configuration to use in the test program - */ -struct device_config { - char *device_specifier; - - unsigned int tx_frequency; - unsigned int tx_bandwidth; - unsigned int tx_samplerate; - int txvga1; - int txvga2; - - unsigned int rx_frequency; - unsigned int rx_bandwidth; - unsigned int rx_samplerate; - bladerf_lna_gain lnagain; - int rxvga1; - int rxvga2; - - bladerf_loopback loopback; - - bladerf_log_level verbosity; - - bool enable_xb200; - - unsigned int samples_per_buffer; - unsigned int num_buffers; - unsigned int num_transfers; - unsigned int stream_timeout_ms; - unsigned int sync_timeout_ms; -}; - -/** - * Initialize the specified device_config structure to default values - * - * @param c Configuration to initialize - */ -void test_init_device_config(struct device_config *c); - -/** - * Deinitialize any items allocated in the provided device configuration - * - * @param c Configuration to deinitialize - */ -void test_deinit_device_config(struct device_config *c); - -/** - * Creates an option array with the app-specific options appended to the common - * test options. The app_options should be 0-terminated. - * - * The caller is responsible for freeing the resturned structure. - * - * @return Heap-allocated array on success, NULL on failure - */ -struct option *test_get_long_options(const struct option *app_options); - -/** - * Handle command line arguments and update the specified device configuration - * accordingly. - * - * When adding new tests, it is preffered to use this for standard configuration - * items, and handle test-specific items afterwards - * - * @param argc # of arguments in argv - * @param argv Array of arguments - * - * @param option_str Options string to pass to getopt_long(). This should - * include TEST_OPTIONS_BASE, with any - * application-handled options appended. - * - * @param long_options List of long options created byt - * - * @param c Device configuration to update. It should have - * already been initialized via - * test_init_device_config(). - * - * @return 0 on success, -1 on failure, 1 if help is requested - */ -int test_handle_args(int argc, char **argv, - const char *options, const struct option *long_options, - struct device_config *c); - -/** - * Wrapper around bladerf_sync_config, using parameters in the specified - * device_config. Optionally, enable the associated module. - */ -int test_perform_sync_config(struct bladerf *dev, - bladerf_module module, - bladerf_format format, - struct device_config *config, - bool enable_module); - - -/** - * Apply the provided device_config to the specified device - * - * @param dev Device to configure - * @param c Configuration parameters - * - * @return 0 on succes, -1 on failure - */ -int test_apply_device_config(struct bladerf *dev, struct device_config *c); - -/** - * Print help info about arguments handled by test_handle_args() - */ -void test_print_common_help(void); - -/** - * Print the current device config - */ -void test_print_device_config(struct device_config *c); - /** * Initialize a seed for use with randval_update * diff --git a/host/libraries/libbladeRF_test/common/src/test_common.c b/host/libraries/libbladeRF_test/common/src/test_common.c index ec286b14..79e10a02 100644 --- a/host/libraries/libbladeRF_test/common/src/test_common.c +++ b/host/libraries/libbladeRF_test/common/src/test_common.c @@ -2,7 +2,7 @@ * This file is part of the bladeRF project: * http://www.github.com/nuand/bladeRF * - * Copyright (C) 2014 Nuand LLC + * Copyright (C) 2014-2015 Nuand LLC * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,613 +22,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + #include -#include #include -#include -#include -#include -#include - -#include "conversions.h" +#include #include "test_common.h" -#define TEST_DEFAULT_TX_FREQ 1000000000 -#define TEST_DEFAULT_TXVGA1 (-14) -#define TEST_DEFAULT_TXVGA2 0 - -#define TEST_DEFAULT_RX_FREQ 1000000000 -#define TEST_DEFAULT_LNAGAIN BLADERF_LNA_GAIN_MAX -#define TEST_DEFAULT_RXVGA1 30 -#define TEST_DEFAULT_RXVGA2 3 - -#define TEST_DEFAULT_SAMPLERATE 1500000 -#define TEST_DEFAULT_BANDWIDTH TEST_DEFAULT_SAMPLERATE - -#define TEST_DEFAULT_SAMPLES_PER_BUF 4096 -#define TEST_DEFAULT_NUM_BUFFERS 64 -#define TEST_DEFAULT_NUM_TRANSFERS 16 -#define TEST_DEFAULT_STREAM_TIMEMOUT_MS 5000 -#define TEST_DEFAULT_SYNC_TIMEOUT_MS (2 * TEST_DEFAULT_STREAM_TIMEMOUT_MS) - -#define OPTION_HELP 'h' -#define OPTION_DEVICE 'd' -#define OPTION_LOOPBACK 'l' -#define OPTION_VERBOSITY 'v' -#define OPTION_SAMPLERATE 's' -#define OPTION_RX_SAMPLERATE 0x80 -#define OPTION_TX_SAMPLERATE 0x81 -#define OPTION_BANDWIDTH 'b' -#define OPTION_RX_BANDWIDTH 0x82 -#define OPTION_TX_BANDWIDTH 0x83 -#define OPTION_FREQUENCY 'f' -#define OPTION_RX_FREQUENCY 0x84 -#define OPTION_TX_FREQUENCY 0x85 -#define OPTION_LNAGAIN 0x86 -#define OPTION_RXVGA1 0x87 -#define OPTION_RXVGA2 0x88 -#define OPTION_TXVGA1 0x89 -#define OPTION_TXVGA2 0x8a -#define OPTION_SAMPLES_PER_BUF 0x91 -#define OPTION_NUM_BUFFERS 0x92 -#define OPTION_NUM_TRANSFERS 0x93 -#define OPTION_STREAM_TIMEOUT 0x94 -#define OPTION_SYNC_TIMEOUT 0x95 -#define OPTION_ENABLE_XB200 0x96 - - -static const numeric_suffix hz_suffixes[] = { - { "K", 1000 }, - { "KHz", 1000 }, - { "M", 1000000 }, - { "MHz", 1000000 }, - { "G", 1000000000 }, - { "GHz", 1000000000 }, -}; - -static const struct option test_long_options[] = -{ - { "help", no_argument, 0, OPTION_HELP }, - { "device", required_argument, 0, OPTION_DEVICE }, - { "verbosity", required_argument, 0, OPTION_VERBOSITY }, - - { "samplerate", required_argument, 0, OPTION_SAMPLERATE }, - { "rx-samplerate", required_argument, 0, OPTION_RX_SAMPLERATE }, - { "tx-samplerate", required_argument, 0, OPTION_TX_SAMPLERATE }, - - { "bandwidth", required_argument, 0, OPTION_BANDWIDTH }, - { "rx-bandwidth", required_argument, 0, OPTION_RX_BANDWIDTH }, - { "tx-bandwidth", required_argument, 0, OPTION_TX_BANDWIDTH }, - - { "frequency", required_argument, 0, OPTION_FREQUENCY }, - { "rx-frequency", required_argument, 0, OPTION_RX_FREQUENCY }, - { "tx-frequency", required_argument, 0, OPTION_TX_FREQUENCY }, - - { "lna-gain", required_argument, 0, OPTION_LNAGAIN }, - { "rxvga1-gain", required_argument, 0, OPTION_RXVGA1 }, - { "rxvga2-gain", required_argument, 0, OPTION_RXVGA2 }, - - { "txvga1-gain", required_argument, 0, OPTION_TXVGA1 }, - { "txvga2-gain", required_argument, 0, OPTION_TXVGA2 }, - - { "loopback", required_argument, 0, OPTION_LOOPBACK }, - - { "xb200", no_argument, 0, OPTION_ENABLE_XB200 }, - - { "samples-per-buffer", required_argument, 0, OPTION_SAMPLES_PER_BUF }, - { "num-buffers", required_argument, 0, OPTION_NUM_BUFFERS }, - { "num-transfers", required_argument, 0, OPTION_NUM_TRANSFERS }, - { "stream-timeout", required_argument, 0, OPTION_STREAM_TIMEOUT }, - { "sync-timeout", required_argument, 0, OPTION_SYNC_TIMEOUT }, -}; - -void test_init_device_config(struct device_config *c) -{ - c->device_specifier = NULL; - - c->tx_frequency = TEST_DEFAULT_TX_FREQ; - c->tx_bandwidth = TEST_DEFAULT_BANDWIDTH; - c->tx_samplerate = TEST_DEFAULT_SAMPLERATE; - c->txvga1 = TEST_DEFAULT_TXVGA1; - c->txvga2 = TEST_DEFAULT_TXVGA2; - - c->rx_frequency = TEST_DEFAULT_TX_FREQ; - c->rx_bandwidth = TEST_DEFAULT_BANDWIDTH; - c->rx_samplerate = TEST_DEFAULT_SAMPLERATE; - c->lnagain = TEST_DEFAULT_LNAGAIN; - c->rxvga1 = TEST_DEFAULT_RXVGA1; - c->rxvga2 = TEST_DEFAULT_RXVGA2; - - c->loopback = BLADERF_LB_NONE; - - c->verbosity = BLADERF_LOG_LEVEL_INFO; - - c->enable_xb200 = false; - - c->samples_per_buffer = TEST_DEFAULT_SAMPLES_PER_BUF; - c->num_buffers = TEST_DEFAULT_NUM_BUFFERS; - c->num_transfers = TEST_DEFAULT_NUM_TRANSFERS; - c->stream_timeout_ms = TEST_DEFAULT_STREAM_TIMEMOUT_MS; - c->sync_timeout_ms = TEST_DEFAULT_SYNC_TIMEOUT_MS; -} - -void test_deinit_device_config(struct device_config *c) -{ - free(c->device_specifier); -} - -void test_print_common_help() -{ - printf("Common libbladeRF test options:\n"); - printf(" -h, --help Show this help text.\n"); - printf(" -d, --device Open the specified device.\n"); - printf(" -v, --verbosity Set the libbladeRF verbosity level.\n"); - printf(" --xb200 Enable the XB-200. This will remain enabled\n"); - printf(" until the device is power-cycled.\n"); - printf("\n"); - printf(" -f, --frequency Set RX and TX to the specified frequency.\n"); - printf(" --rx-frequency Set RX to the specified frequency.\n"); - printf(" --tx-frequency Set TX to the specified frequency.\n"); - printf("\n"); - printf(" -s, --samplerate Set RX and TX to the specified sample rate.\n"); - printf(" --rx-samplerate Set RX to the specified sample rate.\n"); - printf(" --tx-samplerate Set RX to the specified sample rate.\n"); - printf("\n"); - printf(" -b, --bandwidth Set RX and TX to the specified bandwidth.\n"); - printf(" --rx-bandwidth Set RX to the specified bandwidth.\n"); - printf(" --tx-bandwidth Set TX to the specified bandwidth.\n"); - printf("\n"); - printf(" --lna-gain Set the RX LNA to the specified gain.\n"); - printf(" Options are: bypass, mid, max\n"); - printf(" --rxvga1-gain Set RX VGA1 to the specified gain.\n"); - printf(" --rxvga2-gain Set RX VGA2 to the specified gain.\n"); - printf(" --txvga1-gain Set TX VGA1 to the specified gain.\n"); - printf(" --txvga2-gain Set TX VGA2 to the specified gain.\n"); - printf("\n"); - printf(" --num-buffers Allocate sample buffers.\n"); - printf(" --samples-per-buffer Allocate samples in each sample buffer.\n"); - printf(" --num-transfers Utilize up to simultaneous USB transfers.\n"); - printf(" --stream-timeout Set stream timeout to milliseconds.\n"); - printf(" --sync-timeout Set sync function timeout to milliseconds.\n"); - -} - -struct option * test_get_long_options(const struct option *app_options) -{ - struct option *ret; - size_t app_size = 0; - const size_t test_size = sizeof(test_long_options); - - while (app_options[app_size].name != NULL) { - app_size++; - } - - /* Account for 0-entry */ - app_size = (app_size + 1) * sizeof(app_options[0]); - - ret = malloc(test_size + app_size); - - if (ret != NULL) { - memcpy(ret, &test_long_options, test_size); - memcpy(ret + ARRAY_SIZE(test_long_options) , app_options, app_size); - } - - return ret; -} - -int test_handle_args(int argc, char **argv, - const char *option_str, const struct option *long_options, - struct device_config *config) -{ - int c; - bool ok; - int status; - unsigned int freq_min; - - while ((c = getopt_long(argc, argv, option_str, long_options, NULL)) >= 0) { - - if (config->enable_xb200) { - freq_min = BLADERF_FREQUENCY_MIN_XB200; - } else { - freq_min = BLADERF_FREQUENCY_MIN; - } - - switch (c) { - - case OPTION_HELP: - return 1; - break; - - case OPTION_DEVICE: - if (config->device_specifier == NULL) { - config->device_specifier = strdup(optarg); - if (config->device_specifier == NULL) { - perror("strdup"); - return -1; - } - } - break; - - case OPTION_LOOPBACK: - status = str2loopback(optarg, &config->loopback); - if (status != 0) { - fprintf(stderr, "Invalid loopback mode: %s\n", optarg); - return -1; - } - break; - - case OPTION_VERBOSITY: - config->verbosity = str2loglevel(optarg, &ok); - if (!ok) { - fprintf(stderr, "Invalid verbosity level: %s\n", optarg); - return -1; - } - break; - - case OPTION_ENABLE_XB200: - config->enable_xb200 = true; - break; - - case OPTION_FREQUENCY: - config->rx_frequency = - str2uint_suffix(optarg, - freq_min, - BLADERF_FREQUENCY_MAX, - hz_suffixes, ARRAY_SIZE(hz_suffixes), - &ok); - - if (!ok) { - fprintf(stderr, "Invalid frequency: %s\n", optarg); - return -1; - } else { - config->tx_frequency = config->rx_frequency; - } - break; - - case OPTION_RX_FREQUENCY: - config->rx_frequency = - str2uint_suffix(optarg, - freq_min, - BLADERF_FREQUENCY_MAX, - hz_suffixes, ARRAY_SIZE(hz_suffixes), - &ok); - - if (!ok) { - fprintf(stderr, "Invalid RX frequency: %s\n", optarg); - return -1; - } - break; - - case OPTION_TX_FREQUENCY: - config->tx_frequency = - str2uint_suffix(optarg, - freq_min, - BLADERF_FREQUENCY_MAX, - hz_suffixes, ARRAY_SIZE(hz_suffixes), - &ok); - - if (!ok) { - fprintf(stderr, "Invalid TX frequency: %s\n", optarg); - return -1; - } - break; - - case OPTION_SAMPLERATE: - config->rx_samplerate = - str2uint_suffix(optarg, - BLADERF_SAMPLERATE_MIN, - BLADERF_SAMPLERATE_REC_MAX, - hz_suffixes, ARRAY_SIZE(hz_suffixes), - &ok); - if (!ok) { - fprintf(stderr, "Invalid sample rate: %s\n", optarg); - return -1; - } else { - config->tx_samplerate = config->rx_samplerate; - } - break; - - case OPTION_RX_SAMPLERATE: - config->rx_samplerate = - str2uint_suffix(optarg, - BLADERF_SAMPLERATE_MIN, - BLADERF_SAMPLERATE_REC_MAX, - hz_suffixes, ARRAY_SIZE(hz_suffixes), - &ok); - if (!ok) { - fprintf(stderr, "Invalid RX sample rate: %s\n", optarg); - return -1; - } - break; - - case OPTION_TX_SAMPLERATE: - config->tx_samplerate = - str2uint_suffix(optarg, - BLADERF_SAMPLERATE_MIN, - BLADERF_SAMPLERATE_REC_MAX, - hz_suffixes, ARRAY_SIZE(hz_suffixes), - &ok); - if (!ok) { - fprintf(stderr, "Invalid TX sample rate: %s\n", optarg); - return -1; - } - break; - - case OPTION_BANDWIDTH: - config->rx_bandwidth = - str2uint_suffix(optarg, - BLADERF_BANDWIDTH_MIN, - BLADERF_BANDWIDTH_MAX, - hz_suffixes, ARRAY_SIZE(hz_suffixes), - &ok); - if (!ok) { - fprintf(stderr, "Invalid bandwidth: %s\n", optarg); - return -1; - } else { - config->tx_bandwidth = config->rx_bandwidth; - } - break; - - case OPTION_RX_BANDWIDTH: - config->rx_bandwidth = - str2uint_suffix(optarg, - BLADERF_BANDWIDTH_MIN, - BLADERF_BANDWIDTH_MAX, - hz_suffixes, ARRAY_SIZE(hz_suffixes), - &ok); - if (!ok) { - fprintf(stderr, "Invalid RX bandwidth: %s\n", optarg); - return -1; - } - break; - - case OPTION_TX_BANDWIDTH: - config->tx_bandwidth = - str2uint_suffix(optarg, - BLADERF_BANDWIDTH_MIN, - BLADERF_BANDWIDTH_MAX, - hz_suffixes, ARRAY_SIZE(hz_suffixes), - &ok); - if (!ok) { - fprintf(stderr, "Invalid TX bandwidth: %s\n", optarg); - return -1; - } - break; - - case OPTION_LNAGAIN: - status = str2lnagain(optarg, &config->lnagain); - if (status != 0) { - fprintf(stderr, "Invalid RX LNA gain: %s\n", optarg); - return -1; - } - break; - - case OPTION_RXVGA1: - config->rxvga1 = str2int(optarg, - BLADERF_RXVGA1_GAIN_MIN, - BLADERF_RXVGA1_GAIN_MAX, - &ok); - - if (!ok) { - fprintf(stderr, "Invalid RXVGA1 gain: %s\n", optarg); - return -1; - } - break; - - case OPTION_RXVGA2: - config->rxvga2 = str2int(optarg, - BLADERF_RXVGA2_GAIN_MIN, - BLADERF_RXVGA2_GAIN_MAX, - &ok); - - if (!ok) { - fprintf(stderr, "Invalid RXVGA1 gain: %s\n", optarg); - return -1; - } - break; - - case OPTION_TXVGA1: - config->txvga1 = str2int(optarg, - BLADERF_TXVGA1_GAIN_MIN, - BLADERF_TXVGA1_GAIN_MAX, - &ok); - - if (!ok) { - fprintf(stderr, "Invalid TXVGA1 gain: %s\n", optarg); - return -1; - } - break; - - case OPTION_TXVGA2: - config->txvga2 = str2int(optarg, - BLADERF_TXVGA2_GAIN_MIN, - BLADERF_TXVGA2_GAIN_MAX, - &ok); - - if (!ok) { - fprintf(stderr, "Invalid TXVGA2 gain: %s\n", optarg); - return -1; - } - break; - - case OPTION_NUM_BUFFERS: - config->num_buffers = str2uint(optarg, 1, UINT_MAX, &ok); - if (!ok) { - fprintf(stderr, "Invalid buffer count: %s\n", optarg); - return -1; - } - break; - - case OPTION_SAMPLES_PER_BUF: - config->samples_per_buffer = str2uint(optarg, - 1024, UINT_MAX, &ok); - if (!ok) { - fprintf(stderr, "Invalid buffer size (in samples): %s\n", - optarg); - return -1; - } - break; - - case OPTION_NUM_TRANSFERS: - config->num_transfers = str2uint(optarg, 1, UINT_MAX, &ok); - - if (!ok) { - fprintf(stderr, "Invalid transfer count: %s\n", optarg); - return -1; - } - break; - - case OPTION_STREAM_TIMEOUT: - config->stream_timeout_ms = str2uint(optarg, 0, UINT_MAX, &ok); - if (!ok) { - fprintf(stderr, "Invalid stream timeout: %s\n", optarg); - return -1; - } - break; - - case OPTION_SYNC_TIMEOUT: - config->sync_timeout_ms = str2uint(optarg, 0, UINT_MAX, &ok); - if (!ok) { - fprintf(stderr, "Invalid sync function timeout: %s\n", optarg); - return -1; - } - break; - } - } - - return 0; -} - -int test_apply_device_config(struct bladerf *dev, struct device_config *c) -{ - int status; - - bladerf_log_set_verbosity(c->verbosity); - - status = bladerf_set_loopback(dev, c->loopback); - if (status != 0) { - fprintf(stderr, "Failed to set loopback: %s\n", - bladerf_strerror(status)); - return -1; - } - - if (c->enable_xb200) { - status = bladerf_expansion_attach(dev, BLADERF_XB_200); - if (status != 0) { - fprintf(stderr, "Failed to attach XB-200.\n"); - return -1; - } - } - - status = bladerf_set_frequency(dev, BLADERF_MODULE_RX, c->rx_frequency); - if (status != 0) { - fprintf(stderr, "Failed to set RX frequency: %s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_frequency(dev, BLADERF_MODULE_TX, c->tx_frequency); - if (status != 0) { - fprintf(stderr, "Failed to set TX frequency: %s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_sample_rate(dev, BLADERF_MODULE_RX, c->rx_samplerate, NULL); - if (status != 0) { - fprintf(stderr, "Failed to set RX sample rate:%s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_sample_rate(dev, BLADERF_MODULE_TX, c->tx_samplerate, NULL); - if (status != 0) { - fprintf(stderr, "Failed to set TX sample rate: %s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_bandwidth(dev, BLADERF_MODULE_RX, c->rx_bandwidth, NULL); - if (status != 0) { - fprintf(stderr, "Failed to set RX bandwidth: %s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_bandwidth(dev, BLADERF_MODULE_TX, c->tx_bandwidth, NULL); - if (status != 0) { - fprintf(stderr, "Failed to set RX bandwidth: %s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_lna_gain(dev, c->lnagain); - if (status != 0) { - fprintf(stderr, "Failed to set RX LNA gain: %s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_rxvga1(dev, c->rxvga1); - if (status != 0) { - fprintf(stderr, "Failed to set RX VGA1 gain: %s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_rxvga2(dev, c->rxvga2); - if (status != 0) { - fprintf(stderr, "Failed to set RX VGA2 gain: %s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_txvga1(dev, c->txvga1); - if (status != 0) { - fprintf(stderr, "Failed to set TX VGA1 gain: %s\n", - bladerf_strerror(status)); - return -1; - } - - status = bladerf_set_txvga2(dev, c->txvga2); - if (status != 0) { - fprintf(stderr, "Failed to set TX VGA2 gain: %s\n", - bladerf_strerror(status)); - return -1; - } - - return 0; -} - -int test_perform_sync_config(struct bladerf *dev, bladerf_module module, - bladerf_format format, - struct device_config *config, bool enable_module) -{ - int status = bladerf_sync_config(dev, module, format, - config->num_buffers, - config->samples_per_buffer, - config->num_transfers, - config->stream_timeout_ms); - - if (status != 0) { - fprintf(stderr, "Failed to configure %s: %s\n", - module == BLADERF_MODULE_RX ? "RX" : "TX", - bladerf_strerror(status)); - return -1; - } - - if (enable_module) { - status = bladerf_enable_module(dev, module, true); - if (status != 0) { - fprintf(stderr, "Failed to enable %s module: %s\n", - module == BLADERF_MODULE_RX ? "RX" : "TX", - bladerf_strerror(status)); - } - } - - return 0; -} - double calc_avg_duration(const struct timespec *start, const struct timespec *end, double iterations) diff --git a/host/libraries/libbladeRF_test/test_freq_hop/CMakeLists.txt b/host/libraries/libbladeRF_test/test_freq_hop/CMakeLists.txt index 7c19e654..09f5f954 100644 --- a/host/libraries/libbladeRF_test/test_freq_hop/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_freq_hop/CMakeLists.txt @@ -18,8 +18,9 @@ endif() set(SRC main.c - ../common/src/test_common.c + ${CMAKE_CURRENT_SOURCE_DIR}/../common/src/test_common.c ${BLADERF_HOST_COMMON_SOURCE_DIR}/conversions.c + ${BLADERF_HOST_COMMON_SOURCE_DIR}/devcfg.c ) if(MSVC) diff --git a/host/libraries/libbladeRF_test/test_freq_hop/main.c b/host/libraries/libbladeRF_test/test_freq_hop/main.c index 6fd7c158..f14f2c27 100644 --- a/host/libraries/libbladeRF_test/test_freq_hop/main.c +++ b/host/libraries/libbladeRF_test/test_freq_hop/main.c @@ -31,13 +31,15 @@ #include #include #include + #include "conversions.h" #include "test_common.h" +#include "devcfg.h" -#define TEST_OPTIONS_STR TEST_OPTIONS_BASE"i:S:" +#define TEST_OPTIONS_STR DEVCFG_OPTIONS_BASE"i:S:" struct app_params { - struct device_config dev_config; + struct devcfg dev_config; bool rx; bool tx; uint64_t iterations; @@ -106,7 +108,7 @@ void print_usage(const char *argv0) printf(" --tx Enable bladerf_sync_tx() calls.\n"); printf(" Requires device to be in loopback mode.\n"); printf("\n"); - test_print_common_help(); + devcfg_print_common_help("Device configuration arguments\n"); printf("\n"); } @@ -126,7 +128,7 @@ int run_test(struct bladerf *dev, struct app_params *p) goto out; } - status = test_perform_sync_config(dev, BLADERF_MODULE_RX, + status = devcfg_perform_sync_config(dev, BLADERF_MODULE_RX, BLADERF_FORMAT_SC16_Q11, &p->dev_config, true); if (status != 0) { @@ -142,7 +144,7 @@ int run_test(struct bladerf *dev, struct app_params *p) goto out; } - status = test_perform_sync_config(dev, BLADERF_MODULE_RX, + status = devcfg_perform_sync_config(dev, BLADERF_MODULE_RX, BLADERF_FORMAT_SC16_Q11, &p->dev_config, true); if (status != 0) { @@ -258,21 +260,21 @@ int main(int argc, char *argv[]) struct app_params params; struct option *options = NULL; - test_init_device_config(¶ms.dev_config); + devcfg_init(¶ms.dev_config); params.iterations = 5000; params.randval_seed = 1; params.rx = false; params.tx = false; - options = test_get_long_options(app_long_options); + options = devcfg_get_long_options(app_long_options); if (options == NULL) { status = -1; goto error_no_dev; } - status = test_handle_args(argc, argv, - TEST_OPTIONS_STR, options, - ¶ms.dev_config); + status = devcfg_handle_args(argc, argv, + TEST_OPTIONS_STR, options, + ¶ms.dev_config); if (status < 0) { status = -1; goto error_no_dev; @@ -304,7 +306,7 @@ int main(int argc, char *argv[]) goto error_no_dev; } - status = test_apply_device_config(dev, ¶ms.dev_config); + status = devcfg_apply(dev, ¶ms.dev_config); if (status == 0) { status = run_test(dev, ¶ms); } @@ -313,7 +315,7 @@ int main(int argc, char *argv[]) bladerf_close(dev); error_no_dev: - test_deinit_device_config(¶ms.dev_config); + devcfg_deinit(¶ms.dev_config); free(options); return status; } diff --git a/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt b/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt index a12cca18..9a18e543 100644 --- a/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt @@ -27,6 +27,7 @@ set(SRC src/main.c src/hop_set.c ../common/src/test_common.c + ${BLADERF_HOST_COMMON_SOURCE_DIR}/devcfg.c ${BLADERF_HOST_COMMON_SOURCE_DIR}/conversions.c ) diff --git a/host/libraries/libbladeRF_test/test_scheduled_retune/src/main.c b/host/libraries/libbladeRF_test/test_scheduled_retune/src/main.c index beb5b059..28e44645 100644 --- a/host/libraries/libbladeRF_test/test_scheduled_retune/src/main.c +++ b/host/libraries/libbladeRF_test/test_scheduled_retune/src/main.c @@ -45,6 +45,7 @@ #include #include "test_common.h" +#include "devcfg.h" #include "hop_set.h" #define TIMEOUT_MS 2500 @@ -225,7 +226,7 @@ int main(int argc, char *argv[]) const char *mode = NULL; const char *module_str; struct hop_set *hops; - struct device_config config; + struct devcfg config; bladerf_module module; bool quick_tune; size_t i; @@ -277,7 +278,7 @@ int main(int argc, char *argv[]) goto out; } - test_init_device_config(&config); + devcfg_init(&config); config.tx_samplerate = SAMPLE_RATE; config.tx_bandwidth = 1500000; @@ -286,7 +287,7 @@ int main(int argc, char *argv[]) config.num_buffers = 16; config.num_transfers = 8; - status = test_apply_device_config(dev, &config); + status = devcfg_apply(dev, &config); if (status != 0) { fprintf(stderr, "Failed to configure device.\n"); goto out; @@ -315,9 +316,9 @@ int main(int argc, char *argv[]) hops->idx = 0; printf("\n"); - status = test_perform_sync_config(dev, module, - BLADERF_FORMAT_SC16_Q11_META, - &config, true); + status = devcfg_perform_sync_config(dev, module, + BLADERF_FORMAT_SC16_Q11_META, + &config, true); if (status != 0) { goto out; }