111 lines
2.8 KiB
C
111 lines
2.8 KiB
C
|
#include <SoapySDR/Device.h>
|
||
|
#include <SoapySDR/Formats.h>
|
||
|
#include <SoapySDR/Logger.h>
|
||
|
|
||
|
#include <string.h>
|
||
|
|
||
|
#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;
|
||
|
}
|