test_sx: test stream activation just before TX

This commit is contained in:
Thomas Kolb 2024-10-13 18:40:52 +01:00
parent e4a6e6b300
commit 5aa9eeb18b

View file

@ -3,12 +3,47 @@
#include <SoapySDR/Logger.h>
#include <string.h>
#include <math.h>
#include "logger.h"
#include "config.h"
#include "results.h"
#include "utils.h"
void soapy_log_handler(const SoapySDRLogLevel logLevel, const char *message)
{
int level;
switch(logLevel) {
case SOAPY_SDR_CRITICAL:
case SOAPY_SDR_FATAL:
level = LVL_FATAL;
break;
case SOAPY_SDR_ERROR:
level = LVL_ERR;
break;
case SOAPY_SDR_WARNING:
level = LVL_WARN;
break;
case SOAPY_SDR_INFO:
level = LVL_INFO;
break;
case SOAPY_SDR_DEBUG:
level = LVL_DEBUG;
break;
default:
level = LVL_DUMP;
break;
}
LOG(level, "soapy [%d]: %s", logLevel, message);
}
typedef struct {
SoapySDRDevice *sdr;
@ -22,6 +57,12 @@ int main(void)
{
sdr_ctx_t ctx;
logger_init();
// set up logging
SoapySDR_registerLogHandler(soapy_log_handler);
SoapySDR_setLogLevel(SOAPY_SDR_DEBUG);
SoapySDRKwargs args;
memset(&args, 0, sizeof(args));
SoapySDRKwargs_set(&args, "driver", "sx");
@ -75,19 +116,39 @@ int main(void)
#define BUFSIZE 300000
#if 0
if(SoapySDRDevice_activateStream(ctx.sdr, ctx.tx_stream, 0, 0, 0) != 0) {
LOG(LVL_ERR, "activateStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
#endif
float complex samples[BUFSIZE];
float dphi = 6.28 * 20e3 / SDR_TX_SAMPLING_RATE;
float twopi = 6.283185307f;
float offset_hz = 20e3;
float fm_osc_freq_hz = 1.0f;
float fm_osc_dev_hz = 5e3;
float fm_dphi = twopi * fm_osc_freq_hz / SDR_TX_SAMPLING_RATE;
float fm_phi = 0.0f;
float phi = 0.0f;
float twopi = 6.283185307;
int64_t timeNs = 0;
while(true) {
for(size_t i = 0; i < BUFSIZE; i++) {
fm_phi += fm_dphi;
if(fm_phi > twopi) {
fm_phi -= twopi;
}
float fm_dev_hz = fm_osc_dev_hz * sinf(fm_phi);
float dphi = twopi * (offset_hz + fm_dev_hz) / SDR_TX_SAMPLING_RATE;
phi += dphi;
if(phi > twopi) {
phi -= twopi;
@ -95,15 +156,39 @@ int main(void)
samples[i] = cexpf(I * phi);
}
timeNs += 1000000000LL + 1000000000LL * BUFSIZE / SDR_TX_SAMPLING_RATE;
if(SoapySDRDevice_activateStream(ctx.sdr, ctx.tx_stream, 0, 0, 0) != 0) {
LOG(LVL_ERR, "activateStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
double tstart = get_hires_time();
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);
int ret = SoapySDRDevice_writeStream(ctx.sdr, ctx.tx_stream, (const void* const*)buffs, BUFSIZE, &flags, timeNs, timeout_us);
if(ret <= 0) {
LOG(LVL_ERR, "writeStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
double tend_write = get_hires_time();
if(SoapySDRDevice_deactivateStream(ctx.sdr, ctx.tx_stream, 0, 0) != 0) {
LOG(LVL_ERR, "deactivateStream fail: %s", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
double tend = get_hires_time();
LOG(LVL_INFO, "write duration: %.3f ms", (tend_write - tstart) * 1000);
LOG(LVL_INFO, "total duration: %.3f ms", (tend - tstart) * 1000);
fsleep(1);
}
return OK;