From f2e9aa169319ef5fe1c8fd351da00bec262d5000 Mon Sep 17 00:00:00 2001 From: Jon Szymaniak Date: Tue, 26 May 2015 18:31:37 -0400 Subject: [PATCH] libbladeRF_test: Added scheduled retune test --- host/libraries/libbladeRF_test/CMakeLists.txt | 1 + .../test_scheduled_retune/CMakeLists.txt | 32 +++ .../test_scheduled_retune/src/hop_set.c | 251 ++++++++++++++++++ .../test_scheduled_retune/src/hop_set.h | 83 ++++++ .../test_scheduled_retune/src/main.c | 250 +++++++++++++++++ 5 files changed, 617 insertions(+) create mode 100644 host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt create mode 100644 host/libraries/libbladeRF_test/test_scheduled_retune/src/hop_set.c create mode 100644 host/libraries/libbladeRF_test/test_scheduled_retune/src/hop_set.h create mode 100644 host/libraries/libbladeRF_test/test_scheduled_retune/src/main.c diff --git a/host/libraries/libbladeRF_test/CMakeLists.txt b/host/libraries/libbladeRF_test/CMakeLists.txt index bdcce5b6..87aa3e4a 100644 --- a/host/libraries/libbladeRF_test/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/CMakeLists.txt @@ -12,6 +12,7 @@ add_subdirectory(test_peripheral_timing) add_subdirectory(test_repeater) add_subdirectory(test_quick_retune) add_subdirectory(test_rx_discont) +add_subdirectory(test_scheduled_retune) add_subdirectory(test_sync) add_subdirectory(test_timestamps) add_subdirectory(test_tune_timing) diff --git a/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt b/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt new file mode 100644 index 00000000..ef1a73e9 --- /dev/null +++ b/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required(VERSION 2.8) +project(libbladeRF_test_scheduled_retune C) + +include_directories(${libbladeRF_SOURCE_DIR}/include) + +set(INCLUDES + ${BLADERF_HOST_COMMON_INCLUDE_DIRS} + ${CMAKE_CURRENT_SOURCE_DIR}/../common/include +) + +if(MSVC) + set(INCLUDES ${INCLUDES} ${MSVC_C99_INCLUDES}) +endif() + +set(LIBS libbladerf_shared) + +set(SRC + src/main.c + src/hop_set.c + ../common/src/test_common.c + ${BLADERF_HOST_COMMON_SOURCE_DIR}/conversions.c +) + +if(MSVC) + set(SRC ${SRC} + ${BLADERF_HOST_COMMON_SOURCE_DIR}/windows/getopt_long.c + ) +endif(MSVC) + +include_directories(${INCLUDES}) +add_executable(libbladeRF_test_scheduled_retune ${SRC}) +target_link_libraries(libbladeRF_test_scheduled_retune ${LIBS}) diff --git a/host/libraries/libbladeRF_test/test_scheduled_retune/src/hop_set.c b/host/libraries/libbladeRF_test/test_scheduled_retune/src/hop_set.c new file mode 100644 index 00000000..aec012cd --- /dev/null +++ b/host/libraries/libbladeRF_test/test_scheduled_retune/src/hop_set.c @@ -0,0 +1,251 @@ +/* + * This file is part of the bladeRF project: + * http://www.github.com/nuand/bladeRF + * + * Copyright (c) 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 "hop_set.h" +#include "conversions.h" + +#define INC 16 + +static const struct numeric_suffix freq_suffix[] = { + { .suffix = "k", .multiplier = 1000 }, + { .suffix = "K", .multiplier = 1000 }, + { .suffix = "m", .multiplier = 1000 * 1000 }, + { .suffix = "M", .multiplier = 1000 * 1000 }, + { .suffix = "g", .multiplier = 1000 * 1000 * 1000 }, + { .suffix = "G", .multiplier = 1000 * 1000 * 1000 }, +}; + +static const size_t freq_suffix_count = + sizeof(freq_suffix) / sizeof(freq_suffix[0]); + +static inline char * strip_chars(char *str) +{ + size_t i; + size_t len = strlen(str); + + for (i = 0; i < len; i++) { + switch (str[i]) { + case '\n': + case '\t': + case ' ': + case ',': + case ':': + case ';': + str[i] = '\0'; + return str; + } + } + + return str; +} + +struct hop_set * hop_set_load(const char *filename) +{ + int status = -1; + struct hop_set *h = NULL; + FILE *in = NULL; + size_t actual_count = INC; + char buf[81]; + bool ok; + + in = fopen(filename, "r"); + if (in == NULL) { + fprintf(stderr, "Failed to open %s: %s\n", + filename, strerror(errno)); + return NULL; + } + + h = malloc(sizeof(h[0])); + if (h == NULL) { + goto out; + } + + h->params = calloc(actual_count, sizeof(h->params[0])); + if (h->params == NULL) { + goto out; + } + + h->count = 0; + h->idx = 0; + + while (fgets(buf, sizeof(buf), in) != NULL) { + if (h->count >= actual_count) { + struct hop_params *tmp = + realloc(h->params, (h->count + INC) * sizeof(h->params[0])); + + if (tmp == NULL) { + perror("realloc"); + goto out; + } + + actual_count += INC; + h->params = tmp; + } + + strip_chars(buf); + if (strlen(buf) > 0) { + h->params[h->count].f = str2uint_suffix(buf, + BLADERF_FREQUENCY_MIN, + BLADERF_FREQUENCY_MAX, + freq_suffix, freq_suffix_count, + &ok); + if (!ok) { + fprintf(stderr, "Invalid frequency value: %s\n", buf); + goto out; + } + + h->count++; + } + } + + status = 0; + +out: + fclose(in); + + if (status != 0) { + hop_set_free(h); + h = NULL; + } + + return h; +} + +int hop_set_load_quick_tunes(struct bladerf *dev, + bladerf_module m, + struct hop_set *h) +{ + int status; + size_t i; + + for (i = 0; i < h->count; i++) { + status = bladerf_set_frequency(dev, m, h->params[i].f); + if (status != 0) { + fprintf(stderr, "Failed to set frequency: %s\n", + bladerf_strerror(status)); + return status; + } + + status = bladerf_get_quick_tune(dev, m, &h->params[i].qt); + if (status != 0) { + fprintf(stderr, "Failed to get quick tune parameters: %s\n", + bladerf_strerror(status)); + return status; + } + } + + return 0; +} + +unsigned int hop_set_next(struct hop_set *h, struct hop_params *p_usr) +{ + struct hop_params *p = &h->params[h->idx]; + + if (p_usr != NULL) { + memcpy(p_usr, p, sizeof(p[0])); + } + + h->idx = (h->idx + 1) % h->count; + + return p->f; +} + +void hop_set_free(struct hop_set *h) +{ + if (h != NULL) { + free(h->params); + free(h); + } +} + +/* Exercises this hop_set interface */ +#ifdef HOP_SET_TEST +int main(int argc, char *argv[]) +{ + int status = 0; + unsigned int i; + struct bladerf *dev = NULL; + struct hop_set *h = NULL; + const char *dev_str = NULL; + bool have_quick_tune = false; + + if (argc < 2) { + fprintf(stderr, "Usage: %s [device]\n", argv[0]); + return 1; + } + + if (argc >= 3) { + dev_str = argv[2]; + } + + h = hop_set_load(argv[1]); + if (h == NULL) { + fprintf(stderr, "Could not load hop set from %s.\n", argv[1]); + return 1; + } + + status = bladerf_open(&dev, dev_str); + if (status != 0) { + if (status == BLADERF_ERR_NODEV) { + printf("No bladeRF found. Not fetching quick tune params.\n"); + } else { + fprintf(stderr, "Failed to open device: %s\n", + bladerf_strerror(status)); + goto out; + } + } else { + status = hop_set_load_quick_tunes(dev, BLADERF_MODULE_RX, h); + if (status != 0) { + goto out; + } else { + have_quick_tune = true; + } + } + + printf("Loaded %zd entries.\n", h->count); + + /* Wrap around once to verify hop_set_next() doesn't roll out of bounds */ + for (i = 0; i < 2 * h->count; i++) { + + if (have_quick_tune) { + struct hop_params p; + hop_set_next(h, &p); + + printf("f=%-10u nint=%-6u nfrac=%-8u flags=0x%02x vcocap=%u\n", + p.f, p.qt.nint, p.qt.nfrac, p.qt.flags, p.qt.vcocap); + } else { + printf("f=%u\n", hop_set_next(h, NULL)); + } + } + +out: + bladerf_close(dev); + hop_set_free(h); + return status; +} +#endif diff --git a/host/libraries/libbladeRF_test/test_scheduled_retune/src/hop_set.h b/host/libraries/libbladeRF_test/test_scheduled_retune/src/hop_set.h new file mode 100644 index 00000000..651decf2 --- /dev/null +++ b/host/libraries/libbladeRF_test/test_scheduled_retune/src/hop_set.h @@ -0,0 +1,83 @@ +/* + * This file is part of the bladeRF project: + * http://www.github.com/nuand/bladeRF + * + * Copyright (c) 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 HOP_SET_H_ +#define HOP_SET_H_ + +#include +#include + +struct hop_params { + unsigned int f; /* 32-bit frequency, in Hz */ + struct bladerf_quick_tune qt; /* Quick tune parameters */ +}; + +struct hop_set { + size_t idx; /* Current index into `hops` */ + size_t count; /* Number of hop entries */ + struct hop_params *params; +}; + +/** + * Load a hop set from the specified file. The file should contain a list + * of frequencies, with one item per line. + * + * @param filename File to load hop set from + * + * @return Hop set on success, NULL on failure + */ +struct hop_set * hop_set_load(const char *filename); + +/** + * Load the quick tune parameters for all the entries in a hop set + * + * @param dev Handle to bladeRF device + * @param m Module to retune in order to find the desired quick tune + * parameters. + * @param h Hop set + * + * @return 0 on success, BLADERF_ERR_* value on failure + */ +int hop_set_load_quick_tunes(struct bladerf *dev, + bladerf_module m, + struct hop_set *h); +/** + * Get the next entry in the hop set table and advance the hop index + * + * @param[in] h Hop set + * @param[out] p If non-NULL, the hop's parameters, including quick tune + * information, is written this address. + * + * @return The frequency (Hz) of the next hop set entry + */ +unsigned int hop_set_next(struct hop_set *h, struct hop_params *p); + +/** + * Deallocate the specified hop set + * + * @param h Hop set + */ +void hop_set_free(struct hop_set *h); + +#endif diff --git a/host/libraries/libbladeRF_test/test_scheduled_retune/src/main.c b/host/libraries/libbladeRF_test/test_scheduled_retune/src/main.c new file mode 100644 index 00000000..62377f0d --- /dev/null +++ b/host/libraries/libbladeRF_test/test_scheduled_retune/src/main.c @@ -0,0 +1,250 @@ +/* + * This file is part of the bladeRF project: + * http://www.github.com/nuand/bladeRF + * + * Copyright (C) 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. + */ + +/* + * This program exercises the scheduled retune functionality added in + * FPGA v0.2.0. + * + * It transmits a tone for 1ms every 10 ms. During the + * "off time" the device is scheduled to retune to a different frequency, + * per the list of frequencies provided to this program. + * + * The "hop set" file should be a newline-delimited list of frequencies. + * The M or G suffixes may be used, as well as scientific notation. + */ + +#include +#include +#include +#include +#include +#include + +#include "test_common.h" +#include "hop_set.h" + +#define TIMEOUT_MS 2500 +#define ITERATIONS 10000 +#define SAMPLE_RATE 2000000 + +#define NUM_SAMPLES 2000 +#define TS_INC 20000 +#define RETUNE_INC (NUM_SAMPLES + (TS_INC - NUM_SAMPLES) / 10) + +int run_test(struct bladerf *dev, struct hop_set *hops, bool quick_tune) +{ + int status = -1; + int16_t *samples = NULL; + struct bladerf_metadata meta; + unsigned int i; + + memset(&meta, 0, sizeof(meta)); + + samples = malloc(2 * NUM_SAMPLES * sizeof(samples[0])); + if (samples == NULL) { + perror("malloc"); + return -1; + } + + /* Set IQ values to ~ sqrt(2)/2 */ + for (i = 0; i < (2 * NUM_SAMPLES); i += 2) { + samples[i] = samples[i+1] = 1448; + } + + status = bladerf_get_timestamp(dev, BLADERF_MODULE_TX, &meta.timestamp); + if (status != 0) { + fprintf(stderr, "Failed to get initial TX timestamp: %s\n", + bladerf_strerror(status)); + goto out; + } + + /* Add some initial startup delay */ + meta.timestamp += (SAMPLE_RATE / 50); + meta.flags = BLADERF_META_FLAG_TX_BURST_START | + BLADERF_META_FLAG_TX_BURST_END; + + /* Set initial frequency */ + status = bladerf_set_frequency(dev, BLADERF_MODULE_TX, + hop_set_next(hops, NULL)); + if (status != 0) { + fprintf(stderr, "Failed to set initial frequency: %s\n", + bladerf_strerror(status)); + goto out; + } + + for (i = 0; i < ITERATIONS; i++) { + status = bladerf_sync_tx(dev, samples, NUM_SAMPLES, &meta, TIMEOUT_MS); + if (status != 0) { + fprintf(stderr, "Failed to TX @ iteration %u: %s\n", + i + 1, bladerf_strerror(status)); + goto out; + } + + /* Schedule retune during off-time */ + if (quick_tune) { + struct hop_params p; + hop_set_next(hops, &p); + + status = bladerf_schedule_retune(dev, BLADERF_MODULE_TX, + meta.timestamp + RETUNE_INC, + 0, &p.qt); + } else { + unsigned int freq = hop_set_next(hops, NULL); + + status = bladerf_schedule_retune(dev, BLADERF_MODULE_TX, + meta.timestamp + RETUNE_INC, + freq, NULL); + } + + if (status != 0) { + fprintf(stderr, "Failed to schedule retune @ %"PRIu64": %s\n", + meta.timestamp + RETUNE_INC, bladerf_strerror(status)); + goto out; + } + + /* Update timestamp for next transmission */ + meta.timestamp += TS_INC; + } + +out: + free(samples); + return status; +} + +int main(int argc, char *argv[]) +{ + int status, disable_status; + struct bladerf *dev = NULL; + const char *dev_str = NULL; + const char *hop_file = NULL; + const char *mode = NULL; + struct hop_set *hops; + struct device_config config; + bool quick_tune; + size_t i; + + if (argc < 3) { + fprintf(stderr, "Usage: %s [device str] \n", + argv[0]); + + return 1; + } else if (argc >= 4) { + dev_str = argv[1]; + hop_file = argv[2]; + mode = argv[3]; + } else { + hop_file = argv[1]; + mode = argv[2]; + } + + if (!strcasecmp(mode, "quick")) { + quick_tune = true; + } else if (!strcasecmp(mode, "normal")) { + quick_tune = false; + } else { + fprintf(stderr, "Invalid tuning mode: %s\n", mode); + return 1; + } + + hops = hop_set_load(hop_file); + if (hops == NULL) { + return 1; + } + + status = bladerf_open(&dev, dev_str); + if (status != 0) { + fprintf(stderr, "Unable to open device: %s\n", + bladerf_strerror(status)); + + goto out; + } + + test_init_device_config(&config); + + config.tx_samplerate = SAMPLE_RATE; + config.tx_bandwidth = 1500000; + + config.samples_per_buffer = 4096; + config.num_buffers = 16; + config.num_transfers = 8; + + status = test_apply_device_config(dev, &config); + if (status != 0) { + fprintf(stderr, "Failed to configure device.\n"); + goto out; + } + + if (quick_tune) { + status = hop_set_load_quick_tunes(dev, BLADERF_MODULE_TX, hops); + if (status != 0) { + goto out; + } + } + + printf("Hop set:\n"); + for (i = 0; i < hops->count; i++) { + if (quick_tune) { + struct hop_params p; + hop_set_next(hops, &p); + + printf(" f=%-10u nint=%-6u nfrac=%-8u flags=0x%02x vcocap=%u\n", + p.f, p.qt.nint, p.qt.nfrac, p.qt.flags, p.qt.vcocap); + } else { + printf(" f=%u\n", hop_set_next(hops, NULL)); + } + } + + hops->idx = 0; + printf("\n"); + + status = test_perform_sync_config(dev, BLADERF_MODULE_TX, + BLADERF_FORMAT_SC16_Q11_META, + &config, true); + if (status != 0) { + goto out; + } + + printf("Running...\n"); + fflush(stdout); + + status = run_test(dev, hops, quick_tune); + + printf("Done.\n"); + fflush(stdout); + + disable_status = bladerf_enable_module(dev, BLADERF_MODULE_TX, false); + if (disable_status != 0) { + fprintf(stderr, "Failed to disable_status TX module: %s\n", + bladerf_strerror(status)); + goto out; + } + + status = (status == 0) ? disable_status : status; + +out: + bladerf_close(dev); + hop_set_free(hops); + return status; +}