Windows port of plutoplayer

This commit is contained in:
OSQZSS 2022-08-21 15:30:46 +09:00
parent b0affd85e6
commit 783a885c21
6 changed files with 2755 additions and 0 deletions

View File

@ -0,0 +1,227 @@
/*
* Copyright (C) 2015 Analog Devices, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
/** @file ad9361.h
* @brief Public interface */
#ifndef __AD9361_H__
#define __AD9361_H__
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup FLAGS MCS Flags
*
* @{
*/
/** Flag for ad9361_multichip_sync which verifies interface timing between
master and all slaves is identical
*/
#define FIXUP_INTERFACE_TIMING 1
/** Flag for ad9361_multichip_sync which checks if master and associated slaves
have the same sample rate
*/
#define CHECK_SAMPLE_RATES 2
/** @} */
#ifdef _WIN32
# ifdef LIBAD9361_EXPORTS
# define __api __declspec(dllexport)
# else
# define __api __declspec(dllimport)
# endif
#elif __GNUC__ >= 4 && !defined(MATLAB_MEX_FILE) && !defined(MATLAB_LOADLIBRARY)
# define __api __attribute__((visibility ("default")))
#else
# define __api
#endif
struct iio_context;
struct iio_device;
/**
* @struct filter_design_parameters
* @brief Custom Filter Design Parameters
*
* A structure for custom filter designs for the AD936X programmable FIR
* in both TX and RX paths.
*/
struct filter_design_parameters {
double Rdata; /**< Data rate of digital interface */
double Fpass; /**< Stop edge frequency in hertz of passband */
double Fstop; /**< Start edge frequency in hertz of stopband */
double caldiv; /**< Baseband analog filter calibration divider setting [1-511] */
double FIR; /**< Decimation/Interpolation setting of FIR [1,2,4] */
double HB1; /**< Decimation/Interpolation setting of HB1 [1,2] */
double DAC_div; /**< Divider enable setting of DAC clock [0,1] */
const char *Type; /**< Designer mode (only Lowpass supported) */
const char *RxTx; /**< Filter path [Tx,Rx] */
double RFbw; /**< 3dB corner of analog filter in hertz */
double converter_rate; /**< Rate of ADC in hertz */
double PLL_rate; /**< Rate of PLL in hertz */
double Fcenter; /**< Center frequency in hertz of bandpass (Unused) */
double wnom; /**< RF bandwidth of analog filter in hertz */
double FIRdBmin; /**< Minimum stop band attentuation of the FIR in dB */
double int_FIR; /**< Enable use of internal FIR filter [0,1] */
double PLL_mult; /**< Ratio of converter to PLL rate */
double Apass; /**< Desired passband ripple in dB */
double Astop; /**< Desired stopband attenuation in dB */
double phEQ; /**< Enable phase equalization [0,1] */
double HB2; /**< Decimation/Interpolation setting of HB2 [1,2] */
double HB3; /**< Decimation/Interpolation setting of HB3 [1,2,3] */
double maxTaps; /**< Maximum allowed FIR taps */
};
/* ---------------------------------------------------------------------------*/
/* ------------------------- Top-level functions -----------------------------*/
/** @defgroup TopLevel Top-level functions
* @{ */
/** @brief Multi-chip synchronization (MCS) management
* @param master A pointer to an iio_device structure
* @param slaves A double pointer to an iio_device structure
* @param num_slaves Number of slave devices associated with the master
* @param flags Control flags for MCS configuration
* @return On success, 0 is returned
* @return On error, a negative errno code is returned */
__api int ad9361_multichip_sync(struct iio_device *master,
struct iio_device **slaves, unsigned int num_slaves,
unsigned int flags);
/** @brief FMComms5 specific MCS management
* @param ctx A pointer to an iio_context structure
* @param flags Control flags for MCS configuration
* @return On success, 0 is returned
* @return On error, a negative errno code is returned */
__api int ad9361_fmcomms5_multichip_sync(
struct iio_context *ctx, unsigned int flags);
/** @brief Baseband rate configuration with generic filter support
* @param dev A pointer to an iio_device structure
* @param rate Rate in samples per second of desired baseband rate
* @return On success, 0 is returned
* @return On error, a negative errno code is returned
*
* <b>NOTE:</b> Three possible filters are loaded based on required rate and
* associated decimation/interpolation. These filters are generally very wide
* band and not waveform specific. */
__api int ad9361_set_bb_rate(struct iio_device *dev, unsigned long rate);
/** @brief Enable or disable transmit and receiver FIRs simultaneously
* @param dev A pointer to an iio_device structure
* @param enable Integer to enable FIRs when 1 or disable when 0
* @return On success, 0 is returned
* @return On error, a negative errno code is returned */
__api int ad9361_set_trx_fir_enable(struct iio_device *dev, int enable);
/** @brief Get current enable value of transmit and receiver FIRs
* @param dev A pointer to an iio_device structure
* @param enable Returned integer value of FIR enabled
* @return On success, 0 is returned
* @return On error, a negative errno code is returned */
__api int ad9361_get_trx_fir_enable(struct iio_device *dev, int *enable);
/** @brief Design custom FIR filter from specific design criteria
* @param parameters A pointer filter designer structure
* @param taps A pointer to taps of designed filter
* @param num_taps A pointer to integer number of taps designed in taps
* @param gain Integer gain for designed filter
* @return On success, 0 is returned
* @return On error, a negative errno code is returned */
__api int ad9361_generate_fir_taps(struct filter_design_parameters *parameters,
short *taps, int *num_taps, int *gain);
/** @brief Calculate the clock path rates for both transmit and receiver paths
* @param tx_sample_rate Sample rate in samples per second of desired baseband rate
* @param rate_gov Rate governor enable setting forcing HB3=3 when enabled
* @param rx_path_clks A pointer to a unsigned long variable where the 6 RX path rates should be stored
* @param tx_path_clks A pointer to a unsigned long variable where the 6 TX path rates should be stored
* @return On success, 0 is returned
* @return On error, a negative errno code is returned */
__api int ad9361_calculate_rf_clock_chain(unsigned long tx_sample_rate,
unsigned long rate_gov,
unsigned long *rx_path_clks,
unsigned long *tx_path_clks);
/** @brief Calculate the clock path rates and default filter settings for both transmit and receiver for a desired baseband rate
* @param fdpTX Filter design parameters structure where TX filter design parameters will be stored
* @param fdpRX Filter design parameters structure where RX filter design parameters will be stored
* @param sample_rate Desired basedband sample rate in samples per second for both RX and TX filter configurations
* @return On success, 0 is returned
* @return On error, a negative errno code is returned */
__api int ad9361_calculate_rf_clock_chain_fdp(struct filter_design_parameters *fdpTX,
struct filter_design_parameters *fdpRX,
unsigned long sample_rate);
/** @brief Baseband rate configuration with custom filter support based on desired baseband sample rate
* @param dev A pointer to an iio_device structure
* @param rate Rate in samples per second of desired baseband rate
* @return On success, 0 is returned
* @return On error, a negative errno code is returned
*
* <b>NOTE:</b> Designed filter will have the following configuration:
* Fpass = rate / 3
* Fstop = Fpass * 1.25
* wnomTX = 1.6 * Fstop
* wnomRX = 1.4 * Fstop */
__api int ad9361_set_bb_rate_custom_filter_auto(struct iio_device *dev,
unsigned long rate);
/** @brief Baseband rate configuration with custom filter support based on desired baseband sample rate and simplified filter configuration
* @param dev A pointer to an iio_device structure
* @param rate Rate in samples per second of desired baseband rate
* @param Fpass Stop edge frequency in hertz of passband
* @param Fstop Start edge frequency in hertz of stopband
* @param wnom_tx TX RF bandwidth of analog filter in hertz
* @param wnom_rx RX RF bandwidth of analog filter in hertz
* @return On success, 0 is returned
* @return On error, a negative errno code is returned */
__api int ad9361_set_bb_rate_custom_filter_manual(struct iio_device *dev,
unsigned long rate, unsigned long Fpass,
unsigned long Fstop, unsigned long wnom_tx,
unsigned long wnom_rx);
/** @brief FMComms5 phase synchronize all TX and RX channels together
* @param ctx A pointer to an iio_context structure
* @param lo Frequency in hertz of LO for TX and RX
* @return On success, 0 is returned
* @return On error, a negative errno code is returned. If -2 is returned calibration failed
*
* <b>NOTES:</b> To perform calibration the following side effects occur:
* - RF bandwidths of both TX and RX are expanded to the current sample rate. It can be changed after calibration without effecting phase synchronization.
* - DDSs are enabled and left on after synchronization. Changing these DDSs or switching to DMA sources will not effect phase synchronization.
* - TX and RX LOs are set to the same frequency based on the input provided. LO changes can invalidate phase synchronization.
* - AGCs are set to manual mode at predetermined hardware gains for TX and RX. Gain changes can invalidate phase synchronization.
*
* Phase synchronization is valid until the LOs are retuned or sample rates change or gains are modified.
*
* <b>External Links:</b>
* - <a href="https://wiki.analog.com/resources/eval/user-guides/ad-fmcomms5-ebz/multi-chip-sync">Detailed information on synchronization process</a>
* - <a href="https://wiki.analog.com/resources/eval/user-guides/ad-fmcomms5-ebz/hardware">Phase synchronization performance can depend on revision of hardware</a>*/
__api int ad9361_fmcomms5_phase_sync(struct iio_context *ctx, long long lo);
/** @} */
#ifdef __cplusplus
}
#endif
#undef __api
#endif /* __AD9361_H__ */

View File

@ -0,0 +1,181 @@
/*
* Copyright (C) 2016 Analog Devices, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#define _CRT_SECURE_NO_WARNINGS
#include "ad9361.h"
#include <errno.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#include "iio.h"
#else
#include <iio.h>
#include <unistd.h>
#endif
#ifdef _MSC_BUILD
#define snprintf sprintf_s
#endif
static int16_t fir_128_4[] = {
-15,-27,-23,-6,17,33,31,9,-23,-47,-45,-13,34,69,67,21,-49,-102,-99,-32,69,146,143,48,-96,-204,-200,-69,129,278,275,97,-170,
-372,-371,-135,222,494,497,187,-288,-654,-665,-258,376,875,902,363,-500,-1201,-1265,-530,699,1748,1906,845,-1089,-2922,-3424,
-1697,2326,7714,12821,15921,15921,12821,7714,2326,-1697,-3424,-2922,-1089,845,1906,1748,699,-530,-1265,-1201,-500,363,902,875,
376,-258,-665,-654,-288,187,497,494,222,-135,-371,-372,-170,97,275,278,129,-69,-200,-204,-96,48,143,146,69,-32,-99,-102,-49,21,
67,69,34,-13,-45,-47,-23,9,31,33,17,-6,-23,-27,-15};
static int16_t fir_128_2[] = {
-0,0,1,-0,-2,0,3,-0,-5,0,8,-0,-11,0,17,-0,-24,0,33,-0,-45,0,61,-0,-80,0,104,-0,-134,0,169,-0,
-213,0,264,-0,-327,0,401,-0,-489,0,595,-0,-724,0,880,-0,-1075,0,1323,-0,-1652,0,2114,-0,-2819,0,4056,-0,-6883,0,20837,32767,
20837,0,-6883,-0,4056,0,-2819,-0,2114,0,-1652,-0,1323,0,-1075,-0,880,0,-724,-0,595,0,-489,-0,401,0,-327,-0,264,0,-213,-0,
169,0,-134,-0,104,0,-80,-0,61,0,-45,-0,33,0,-24,-0,17,0,-11,-0,8,0,-5,-0,3,0,-2,-0,1,0,-0, 0 };
static int16_t fir_96_2[] = {
-4,0,8,-0,-14,0,23,-0,-36,0,52,-0,-75,0,104,-0,-140,0,186,-0,-243,0,314,-0,-400,0,505,-0,-634,0,793,-0,
-993,0,1247,-0,-1585,0,2056,-0,-2773,0,4022,-0,-6862,0,20830,32767,20830,0,-6862,-0,4022,0,-2773,-0,2056,0,-1585,-0,1247,0,-993,-0,
793,0,-634,-0,505,0,-400,-0,314,0,-243,-0,186,0,-140,-0,104,0,-75,-0,52,0,-36,-0,23,0,-14,-0,8,0,-4,0};
static int16_t fir_64_2[] = {
-58,0,83,-0,-127,0,185,-0,-262,0,361,-0,-488,0,648,-0,-853,0,1117,-0,-1466,0,1954,-0,-2689,0,3960,-0,-6825,0,20818,32767,
20818,0,-6825,-0,3960,0,-2689,-0,1954,0,-1466,-0,1117,0,-853,-0,648,0,-488,-0,361,0,-262,-0,185,0,-127,-0,83,0,-58,0};
#define FIR_BUF_SIZE 8192
int ad9361_set_trx_fir_enable(struct iio_device *dev, int enable)
{
int ret = iio_device_attr_write_bool(dev,
"in_out_voltage_filter_fir_en", !!enable);
if (ret < 0)
ret = iio_channel_attr_write_bool(iio_device_find_channel(dev, "out", false),
"voltage_filter_fir_en", !!enable);
return ret;
}
int ad9361_get_trx_fir_enable(struct iio_device *dev, int *enable)
{
bool value;
int ret = iio_device_attr_read_bool(dev, "in_out_voltage_filter_fir_en", &value);
if (ret < 0)
ret = iio_channel_attr_read_bool(iio_device_find_channel(dev, "out", false),
"voltage_filter_fir_en", &value);
if (!ret)
*enable = value;
return ret;
}
int ad9361_set_bb_rate(struct iio_device *dev, unsigned long rate)
{
struct iio_channel *chan;
long long current_rate;
int dec, taps, ret, i, enable, len = 0;
int16_t *fir;
char *buf;
if (rate <= 20000000UL) {
dec = 4;
taps = 128;
fir = fir_128_4;
} else if (rate <= 40000000UL) {
dec = 2;
fir = fir_128_2;
taps = 128;
} else if (rate <= 53333333UL) {
dec = 2;
fir = fir_96_2;
taps = 96;
} else {
dec = 2;
fir = fir_64_2;
taps = 64;
}
chan = iio_device_find_channel(dev, "voltage0", true);
if (chan == NULL)
return -ENODEV;
ret = iio_channel_attr_read_longlong(chan, "sampling_frequency", &current_rate);
if (ret < 0)
return ret;
ret = ad9361_get_trx_fir_enable(dev, &enable);
if (ret < 0)
return ret;
if (enable) {
if (current_rate <= (25000000 / 12))
iio_channel_attr_write_longlong(chan, "sampling_frequency", 3000000);
ret = ad9361_set_trx_fir_enable(dev, false);
if (ret < 0)
return ret;
}
buf = malloc(FIR_BUF_SIZE);
if (!buf)
return -ENOMEM;
len += snprintf(buf + len, FIR_BUF_SIZE - len, "RX 3 GAIN -6 DEC %d\n", dec);
len += snprintf(buf + len, FIR_BUF_SIZE - len, "TX 3 GAIN 0 INT %d\n", dec);
for (i = 0; i < taps; i++)
len += snprintf(buf + len, FIR_BUF_SIZE - len, "%d,%d\n", fir[i], fir[i]);
len += snprintf(buf + len, FIR_BUF_SIZE - len, "\n");
ret = iio_device_attr_write_raw(dev, "filter_fir_config", buf, len);
free (buf);
if (ret < 0)
return ret;
if (rate <= (25000000 / 12)) {
int dacrate, txrate, max;
char readbuf[100];
ret = iio_device_attr_read(dev, "tx_path_rates", readbuf, sizeof(readbuf));
if (ret < 0)
return ret;
ret = sscanf(readbuf, "BBPLL:%*d DAC:%d T2:%*d T1:%*d TF:%*d TXSAMP:%d", &dacrate, &txrate);
if (ret != 2)
return -EFAULT;
if (txrate == 0)
return -EINVAL;
max = (dacrate / txrate) * 16;
if (max < taps)
iio_channel_attr_write_longlong(chan, "sampling_frequency", 3000000);
ret = ad9361_set_trx_fir_enable(dev, true);
if (ret < 0)
return ret;
ret = iio_channel_attr_write_longlong(chan, "sampling_frequency", rate);
if (ret < 0)
return ret;
} else {
ret = iio_channel_attr_write_longlong(chan, "sampling_frequency", rate);
if (ret < 0)
return ret;
ret = ad9361_set_trx_fir_enable(dev, true);
if (ret < 0)
return ret;
}
return 0;
}

View File

@ -0,0 +1,110 @@
/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "getopt.h"
#include <string.h>
#include <stdio.h>
int opterr = 1, /* if error message should be printed */
optind = 1, /* index into parent argv vector */
optopt, /* character checked for validity */
optreset; /* reset getopt */
char *optarg; /* argument associated with option */
#define BADCH (int)'?'
#define BADARG (int)':'
#define EMSG ""
/*
* getopt --
* Parse argc/argv argument vector.
*
* Note: Unlike GNU getopt(), after a non-option argument, all further
* arguments are considered also non-options. This is similar to
* the way non-GNU Unix systems work.
*/
int getopt(int nargc, char * const nargv[], const char *ostr)
{
static char *place = EMSG; /* option letter processing */
const char *oli; /* option letter list index */
if (optreset || !*place) { /* update scanning pointer */
optreset = 0;
if (optind >= nargc || *(place = nargv[optind]) != '-') {
place = EMSG;
return (-1);
}
if (place[1] && *++place == '-') { /* found "--" */
++optind;
place = EMSG;
return (-1);
}
} /* option letter okay? */
if ((optopt = (int)*place++) == (int)':' ||
!(oli = strchr(ostr, optopt))) {
/*
* if the user didn't specify '-' as an option,
* assume it means -1.
*/
if (optopt == (int)'-')
return (-1);
if (!*place)
++optind;
if (opterr && *ostr != ':')
(void)printf("illegal option -- %c\n", optopt);
return (BADCH);
}
if (*++oli != ':') { /* don't need argument */
optarg = NULL;
if (!*place)
++optind;
}
else { /* need an argument */
if (*place) /* no white space */
optarg = place;
else if (nargc <= ++optind) { /* no arg */
place = EMSG;
if (*ostr == ':')
return (BADARG);
if (opterr)
(void)printf("option requires an argument -- %c\n", optopt);
return (BADCH);
}
else /* white space */
optarg = nargv[optind];
place = EMSG;
++optind;
}
return (optopt); /* dump back option letter */
}

View File

@ -0,0 +1,9 @@
#ifndef GETOPT_H
#define GETOPT_H
extern char *optarg;
extern int optind;
int getopt(int nargc, char * const nargv[], const char *ostr) ;
#endif

1974
player/plutoplayer_win/iio.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,254 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include "getopt.h"
#include "ad9361.h"
#include "iio.h"
#else
#include <getopt.h>
#include <ad9361.h>
#include <iio.h>
#endif
#include <errno.h>
#include <signal.h>
#include <string.h>
#define NOTUSED(V) ((void) V)
#define MHZ(x) ((long long)(x*1000000.0 + .5))
#define GHZ(x) ((long long)(x*1000000000.0 + .5))
#define NUM_SAMPLES 2600000
#define BUFFER_SIZE (NUM_SAMPLES * 2 * sizeof(int16_t))
struct stream_cfg {
long long bw_hz; // Analog banwidth in Hz
long long fs_hz; // Baseband sample rate in Hz
long long lo_hz; // Local oscillator frequency in Hz
const char* rfport; // Port name
double gain_db; // Hardware gain
};
static void usage() {
fprintf(stderr, "Usage: plutoplayer [options]\n"
" -t <filename> Transmit data from file (required)\n"
" -a <attenuation> Set TX attenuation [dB] (default -20.0)\n"
" -b <bw> Set RF bandwidth [MHz] (default 5.0)\n"
" -u <uri> ADALM-Pluto URI\n"
" -n <network> ADALM-Pluto network IP or hostname (default pluto.local)\n");
return;
}
static bool stop = false;
static void handle_sig(int sig)
{
NOTUSED(sig);
stop = true;
}
static char* readable_fs(double size, char* buf, size_t buf_size) {
int i = 0;
const char* units[] = { "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
while (size > 1024) {
size /= 1024;
i++;
}
snprintf(buf, buf_size, "%.*f %s", i, size, units[i]);
return buf;
}
/*
*
*/
int main(int argc, char** argv) {
char buf[1024];
int opt;
const char* path = NULL;
struct stream_cfg txcfg;
FILE* fp = NULL;
const char* uri = NULL;
const char* ip = NULL;
// TX stream default config
txcfg.bw_hz = MHZ(3.0); // 3.0 MHz RF bandwidth
txcfg.fs_hz = MHZ(2.6); // 2.6 MS/s TX sample rate
txcfg.lo_hz = GHZ(1.575420); // 1.57542 GHz RF frequency
txcfg.rfport = "A";
txcfg.gain_db = -20.0;
struct iio_context* ctx = NULL;
struct iio_device* tx = NULL;
struct iio_device* phydev = NULL;
struct iio_channel* tx0_i = NULL;
struct iio_channel* tx0_q = NULL;
struct iio_buffer* tx_buffer = NULL;
while ((opt = getopt(argc, argv, "t:a:b:n:u:")) != EOF) {
switch (opt) {
case 't':
path = optarg;
break;
case 'a':
txcfg.gain_db = atof(optarg);
if (txcfg.gain_db > 0.0) txcfg.gain_db = 0.0;
if (txcfg.gain_db < -80.0) txcfg.gain_db = -80.0;
break;
case 'b':
txcfg.bw_hz = MHZ(atof(optarg));
if (txcfg.bw_hz > MHZ(5.0)) txcfg.bw_hz = MHZ(5.0);
if (txcfg.bw_hz < MHZ(1.0)) txcfg.bw_hz = MHZ(1.0);
break;
case 'u':
uri = optarg;
break;
case 'n':
ip = optarg;
break;
default:
printf("Unknown argument '-%c %s'\n", opt, optarg);
usage();
return EXIT_FAILURE;
}
}
//signal(SIGINT, handle_sig);
if (path == NULL) {
printf("Specify a path to a file to transmit\n");
usage();
return EXIT_FAILURE;
}
fp = fopen(path, "rb");
if (fp == NULL) {
fprintf(stderr, "ERROR: Failed to open TX file: %s\n", path);
return EXIT_FAILURE;
}
fseek(fp, 0L, SEEK_END);
size_t sz = ftell(fp);
fseek(fp, 0L, SEEK_SET);
readable_fs((double)sz, buf, sizeof(buf));
printf("* Transmit file size: %s\n", buf);
printf("* Acquiring IIO context\n");
ctx = iio_create_default_context();
if (ctx == NULL) {
if (ip != NULL) {
ctx = iio_create_network_context(ip);
}
else if (uri != NULL) {
ctx = iio_create_context_from_uri(uri);
}
else {
ctx = iio_create_network_context("pluto.local");
}
}
if (ctx == NULL) {
iio_strerror(errno, buf, sizeof(buf));
fprintf(stderr, "Failed creating IIO context: %s\n", buf);
return false;
}
struct iio_scan_context* scan_ctx;
struct iio_context_info** info;
scan_ctx = iio_create_scan_context(NULL, 0);
if (scan_ctx) {
int info_count = iio_scan_context_get_info_list(scan_ctx, &info);
if (info_count > 0) {
printf("* Found %s\n", iio_context_info_get_description(info[0]));
iio_context_info_list_free(info);
}
iio_scan_context_destroy(scan_ctx);
}
printf("* Acquiring devices\n");
int device_count = iio_context_get_devices_count(ctx);
if (!device_count) {
fprintf(stderr, "No supported PLUTOSDR devices found.\n");
goto error_exit;
}
fprintf(stderr, "* Context has %d device(s).\n", device_count);
printf("* Acquiring TX device\n");
tx = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
if (tx == NULL) {
iio_strerror(errno, buf, sizeof(buf));
fprintf(stderr, "Error opening PLUTOSDR TX device: %s\n", buf);
goto error_exit;
}
iio_device_set_kernel_buffers_count(tx, 8);
phydev = iio_context_find_device(ctx, "ad9361-phy");
struct iio_channel* phy_chn = iio_device_find_channel(phydev, "voltage0", true);
iio_channel_attr_write(phy_chn, "rf_port_select", txcfg.rfport);
iio_channel_attr_write_longlong(phy_chn, "rf_bandwidth", txcfg.bw_hz);
iio_channel_attr_write_longlong(phy_chn, "sampling_frequency", txcfg.fs_hz);
iio_channel_attr_write_double(phy_chn, "hardwaregain", txcfg.gain_db);
iio_channel_attr_write_bool(
iio_device_find_channel(phydev, "altvoltage0", true)
, "powerdown", true); // Turn OFF RX LO
iio_channel_attr_write_longlong(
iio_device_find_channel(phydev, "altvoltage1", true)
, "frequency", txcfg.lo_hz); // Set TX LO frequency
printf("* Initializing streaming channels\n");
tx0_i = iio_device_find_channel(tx, "voltage0", true);
if (!tx0_i)
tx0_i = iio_device_find_channel(tx, "altvoltage0", true);
tx0_q = iio_device_find_channel(tx, "voltage1", true);
if (!tx0_q)
tx0_q = iio_device_find_channel(tx, "altvoltage1", true);
printf("* Enabling IIO streaming channels\n");
iio_channel_enable(tx0_i);
iio_channel_enable(tx0_q);
ad9361_set_bb_rate(iio_context_find_device(ctx, "ad9361-phy"), txcfg.fs_hz);
printf("* Creating TX buffer\n");
tx_buffer = iio_device_create_buffer(tx, NUM_SAMPLES, false);
if (!tx_buffer) {
fprintf(stderr, "Could not create TX buffer.\n");
goto error_exit;
}
iio_channel_attr_write_bool(
iio_device_find_channel(iio_context_find_device(ctx, "ad9361-phy"), "altvoltage1", true)
, "powerdown", false); // Turn ON TX LO
int32_t ntx = 0;
short* ptx_buffer = (short*)iio_buffer_start(tx_buffer);
printf("* Transmit starts...\n");
// Keep writing samples while there is more data to send and no failures have occurred.
while (!feof(fp) && !stop) {
fread(ptx_buffer, sizeof(short), BUFFER_SIZE / sizeof(short), fp);
// Schedule TX buffer
ntx = iio_buffer_push(tx_buffer);
if (ntx < 0) {
printf("Error pushing buf %d\n", (int)ntx);
break;
}
}
printf("Done.\n");
error_exit:
fclose(fp);
iio_channel_attr_write_bool(
iio_device_find_channel(iio_context_find_device(ctx, "ad9361-phy"), "altvoltage1", true)
, "powerdown", true); // Turn OFF TX LO
if (tx_buffer) { iio_buffer_destroy(tx_buffer); }
if (tx0_i) { iio_channel_disable(tx0_i); }
if (tx0_q) { iio_channel_disable(tx0_q); }
if (ctx) { iio_context_destroy(ctx); }
return EXIT_SUCCESS;
}