diff --git a/impl/test/CMakeLists.txt b/impl/test/CMakeLists.txt index 45900af..0371ce9 100644 --- a/impl/test/CMakeLists.txt +++ b/impl/test/CMakeLists.txt @@ -222,3 +222,24 @@ target_link_libraries( test_interleaver m ) + +#------------------------------------ + +add_executable( + test_sx + ../src/utils.c + ../src/utils.h + ../src/logger.c + ../src/logger.h + test_sx.c +) + +target_link_libraries( + test_sx + liquid + m + rt + fftw3f + fec + SoapySDR +) diff --git a/impl/test/test_sx.c b/impl/test/test_sx.c new file mode 100644 index 0000000..cc42bc3 --- /dev/null +++ b/impl/test/test_sx.c @@ -0,0 +1,110 @@ +#include +#include +#include + +#include + +#include "logger.h" +#include "config.h" +#include "results.h" +#include "utils.h" + + +typedef struct { + SoapySDRDevice *sdr; + + SoapySDRStream *rx_stream; + SoapySDRStream *tx_stream; +} sdr_ctx_t; + + +int main(void) +{ + sdr_ctx_t ctx; + + SoapySDRKwargs args; + memset(&args, 0, sizeof(args)); + SoapySDRKwargs_set(&args, "driver", "sx"); + ctx.sdr = SoapySDRDevice_make(&args); + SoapySDRKwargs_clear(&args); + + if (ctx.sdr == NULL) + { + LOG(LVL_ERR, "SoapySDRDevice_make fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + + //setup streams + ctx.rx_stream = SoapySDRDevice_setupStream(ctx.sdr, SOAPY_SDR_RX, SOAPY_SDR_CF32, NULL, 0, NULL); + + if(ctx.rx_stream == NULL) { + LOG(LVL_ERR, "setupStream fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + + ctx.tx_stream = SoapySDRDevice_setupStream(ctx.sdr, SOAPY_SDR_TX, SOAPY_SDR_CF32, NULL, 0, NULL); + + if(ctx.tx_stream == NULL) { + LOG(LVL_ERR, "setupStream fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + + //apply settings + if (SoapySDRDevice_setSampleRate(ctx.sdr, SOAPY_SDR_RX, 0, SDR_RX_SAMPLING_RATE) != 0) { + LOG(LVL_ERR, "setSampleRate fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + if (SoapySDRDevice_setFrequency(ctx.sdr, SOAPY_SDR_RX, 0, SDR_RX_FREQ - SDR_RX_IF_SHIFT, NULL) != 0) { + LOG(LVL_ERR, "setFrequency fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + + if (SoapySDRDevice_setSampleRate(ctx.sdr, SOAPY_SDR_TX, 0, SDR_TX_SAMPLING_RATE) != 0) { + LOG(LVL_ERR, "setSampleRate fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + if (SoapySDRDevice_setFrequency(ctx.sdr, SOAPY_SDR_TX, 0, SDR_TX_FREQ, NULL) != 0) { + LOG(LVL_ERR, "setFrequency fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + + if(SoapySDRDevice_setGain(ctx.sdr, SOAPY_SDR_TX, 0, SDR_GAIN_TX) != 0) { + LOG(LVL_ERR, "setGain fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + +#define BUFSIZE 300000 + + if(SoapySDRDevice_activateStream(ctx.sdr, ctx.tx_stream, 0, 0, 0) != 0) { + LOG(LVL_ERR, "activateStream fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + + float complex samples[BUFSIZE]; + + float dphi = 6.28 * 20e3 / SDR_TX_SAMPLING_RATE; + float phi = 0.0f; + float twopi = 6.283185307; + + while(true) { + for(size_t i = 0; i < BUFSIZE; i++) { + phi += dphi; + if(phi > twopi) { + phi -= twopi; + } + samples[i] = cexpf(I * phi); + } + + int flags = 0; + void *buffs[] = {(void*)samples}; + size_t timeout_us = 100000; + int ret = SoapySDRDevice_writeStream(ctx.sdr, ctx.tx_stream, (const void* const*)buffs, BUFSIZE, &flags, 0, timeout_us); + + if(ret <= 0) { + LOG(LVL_ERR, "writeStream fail: %s", SoapySDRDevice_lastError()); + return ERR_SOAPY; + } + } + + return OK; +}