Skip to content

Commit

Permalink
update tx
Browse files Browse the repository at this point in the history
  • Loading branch information
paulh002 committed Sep 6, 2021
1 parent 1094188 commit 66e4bfd
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 43 deletions.
17 changes: 12 additions & 5 deletions SoapyRadioberry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#define TX_MAX_BUFFER (TX_MAX * 8)
const int npackages = 4;

typedef enum radioberrysdrStreamFormat {
RADIOBERRY_SDR_CF32,
RADIOBERRY_SDR_CS16
} radioberrysdrStreamFormat;

class SoapyRadioberry : public SoapySDR::Device{

public:
Expand Down Expand Up @@ -131,11 +136,13 @@ class SoapyRadioberry : public SoapySDR::Device{

private:

int fd_rb;
int sample_rate;
int rx_frequency;
int no_channels;
int fd_rb;
int sample_rate;
int rx_frequency;
int no_channels;
struct rb_info_arg_t rb_control;
std::unique_ptr<rpihw::driver::i2c> i2c_ptr;
bool i2c_available = false;
bool i2c_available = false;
radioberrysdrStreamFormat streamFormat;
uint32_t m_count;
};
6 changes: 3 additions & 3 deletions SoapyRadioberrySettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SoapyRadioberry::SoapyRadioberry( const SoapySDR::Kwargs &args ){
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::SoapyRadioberry constructor called");

no_channels = 1;
m_count = 0;
fd_rb = open("/dev/radioberry", O_RDWR);
try
{
Expand Down Expand Up @@ -198,7 +199,7 @@ SoapySDR::Range SoapyRadioberry::getGainRange( const int direction, const size_t

if(direction==SOAPY_SDR_RX)
return(SoapySDR::Range(-12, 48));
return(SoapySDR::Range(0,16));
return(SoapySDR::Range(0,15));
}

void SoapyRadioberry::setGain( const int direction, const size_t channel, const double value ) {
Expand All @@ -216,9 +217,8 @@ void SoapyRadioberry::setGain( const int direction, const size_t channel, const
if(direction==SOAPY_SDR_TX)
{ // 0 -7 TX RF gain
uint32_t z = (uint32_t)value;
if (value > 15) z = 15 << 28;
if (value > 15) z = 15;
if (value < 0.0) z = 0;
printf("value = %f, z: %u\n", value, z);
z = z << 28;
command = 0x13;
command_data = z;
Expand Down
130 changes: 95 additions & 35 deletions SoapyRadioberryStreaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ std::vector<std::string> SoapyRadioberry::getStreamFormats(const int direction,
std::vector<std::string> formats;

formats.push_back(SOAPY_SDR_CF32);
formats.push_back(SOAPY_SDR_CS16);

return formats;
}
Expand Down Expand Up @@ -92,8 +93,16 @@ SoapySDR::Stream *SoapyRadioberry::setupStream(
//check the format
if (format == SOAPY_SDR_CF32) {
SoapySDR_log(SOAPY_SDR_INFO, "Using format CF32.");
streamFormat = RADIOBERRY_SDR_CF32;
}
else {
else if(format == SOAPY_SDR_CS16 && direction == SOAPY_SDR_TX)
{
SoapySDR_log(SOAPY_SDR_INFO, "Using format CS16.");
streamFormat = RADIOBERRY_SDR_CS16;
m_count = 0;
}
else
{
throw std::runtime_error(
"setupStream invalid format '" + format + "' -- Only CF32 is supported by SoapyRadioberrySDR module.");
}
Expand All @@ -112,36 +121,59 @@ int SoapyRadioberry::readStream(
{
int i;
int iq = 0;
int left_sample;
int right_sample;
int16_t left_sample;
int16_t right_sample;
int nr_samples;

void *buff_base = buffs[0];
float *target_buffer = (float *) buff_base;
float *target_buffer = (float *) buff_base;
int16_t *itarget_buffer = (int16_t *) buff_base;

char rx_buffer[512];
for(int ii = 0 ; ii < npackages ; ii++)
{
nr_samples = read(fd_rb, rx_buffer, sizeof(rx_buffer));
//printf("nr_samples %d sample: %d %d %d %d %d %d\n",nr_samples, (int)rx_buffer[0],(int)rx_buffer[1],(int)rx_buffer[2],(int)rx_buffer[3],(int)rx_buffer[4],(int)rx_buffer[5]);
for(i = 0 ; i < nr_samples ; i += 6) {
left_sample = (int)((signed char) rx_buffer[i]) << 16;
left_sample |= (int)((((unsigned char)rx_buffer[i + 1]) << 8) & 0xFF00);
left_sample |= (int)((unsigned char)rx_buffer[i + 2] & 0xFF);
right_sample = (int)((signed char)rx_buffer[i + 3]) << 16;
right_sample |= (int)((((unsigned char)rx_buffer[i + 4]) << 8) & 0xFF00);
right_sample |= (int)((unsigned char)rx_buffer[i + 5] & 0xFF);
right_sample = right_sample * -1;
if(streamFormat == RADIOBERRY_SDR_CF32)
{
for (i = 0; i < nr_samples; i += 6) {
left_sample = (int)((signed char) rx_buffer[i]) << 16;
left_sample |= (int)((((unsigned char)rx_buffer[i + 1]) << 8) & 0xFF00);
left_sample |= (int)((unsigned char)rx_buffer[i + 2] & 0xFF);
right_sample = (int)((signed char)rx_buffer[i + 3]) << 16;
right_sample |= (int)((((unsigned char)rx_buffer[i + 4]) << 8) & 0xFF00);
right_sample |= (int)((unsigned char)rx_buffer[i + 5] & 0xFF);
right_sample = right_sample * -1;

target_buffer[iq++] = (float)left_sample / 2048.0; // 12 bit sample
target_buffer[iq++] = (float)right_sample / 2048.0; // 12 bit sample
//printf("nr_samples %d sample: %d %d \n", nr_samples, left_sample, right_sample);
target_buffer[iq++] = (float)left_sample / 2048.0; // 12 bit sample
target_buffer[iq++] = (float)right_sample / 2048.0; // 12 bit sample
//printf("nr_samples %d sample: %d %d \n", nr_samples, left_sample, right_sample);
}
}
if (streamFormat == RADIOBERRY_SDR_CS16)
{
for (i = 0; i < nr_samples; i += 6) {
left_sample = (int16_t)((signed char) rx_buffer[i]) << 16;
left_sample |= (int16_t)((((unsigned char)rx_buffer[i + 1]) << 8) & 0xFF00);
left_sample |= (int16_t)((unsigned char)rx_buffer[i + 2] & 0xFF);
right_sample = (int16_t)((signed char)rx_buffer[i + 3]) << 16;
right_sample |= (int16_t)((((unsigned char)rx_buffer[i + 4]) << 8) & 0xFF00);
right_sample |= (int16_t)((unsigned char)rx_buffer[i + 5] & 0xFF);
right_sample = right_sample * -1;

itarget_buffer[iq++] = left_sample << 4; // 12 bit sample
itarget_buffer[iq++] = right_sample << 4; // 12 bit sample
}
}
}

return (npackages * nr_samples / 6); //return the number of IQ samples
}

union uTxBuffer
{
std::uint16_t i16TxBuffer[2];
unsigned char i8TxBuffer[4];
};

int SoapyRadioberry::writeStream(SoapySDR::Stream *stream, const void * const *buffs, const size_t numElems, int &flags, const long long timeNs, const long timeoutUs)
{
Expand All @@ -151,30 +183,58 @@ int SoapyRadioberry::writeStream(SoapySDR::Stream *stream, const void * const *b
int right_sample;
int nr_samples;

void const *buff_base = buffs[0];
float *target_buffer = (float *) buff_base;
void const *buff_base = buffs[0];
float *target_buffer = (float *) buff_base;
int16_t *itarget_buffer = (int16_t *) buff_base;


uTxBuffer tx;

unsigned char tx_buffer[4];
for (int ii = 0; ii < numElems; ii++)
if (streamFormat == RADIOBERRY_SDR_CF32)
{
float i, q;
int16_t di, dq;
for (int ii = 0; ii < numElems; ii++)
{
float i, q;
int16_t di, dq;

di = (int16_t)(target_buffer[iq++] * 32767.999f);
dq = (int16_t)(target_buffer[iq++] * 32767.999f);
// i
tx_buffer[0] = (di & 0xFF00) >> 8;
tx_buffer[1] = (di & 0x00FF);
// q
tx_buffer[2] = (dq & 0xFF00) >> 8;
tx_buffer[3] = (dq & 0x00FF);
ret = write(fd_rb, tx_buffer, sizeof(tx_buffer));
if (ret == 0)
tx.i16TxBuffer[0] = (int16_t)(target_buffer[iq++] * 32767.999f);
tx.i16TxBuffer[1] = (int16_t)(target_buffer[iq++] * -32767.999f);
ret = write(fd_rb, &tx, 4 * sizeof(uint8_t));
if (ret == 0)
{
printf("radioberry buffer full\n");
usleep(1000); //50 samples sleep (1/48K about 20usec /sample * 50)
}
}
}
if (streamFormat == RADIOBERRY_SDR_CS16)
{
int j = 0;

for (int ii = 0; ii < numElems; ii++)
{
printf("radioberry buffer full\n");
usleep(1000); //50 samples sleep (1/48K about 20usec /sample * 50)
//printf("%x %x %x %x\n", (itarget_buffer[j] & 0xFF00) >> 8, (itarget_buffer[j] & 0x00FF), (itarget_buffer[j+1] & 0xFF00) >> 8, (itarget_buffer[j+1] & 0x00FF));

tx.i8TxBuffer[2] = (unsigned char)((itarget_buffer[j] & 0xff00) >> 8);
tx.i8TxBuffer[3] = (unsigned char)(itarget_buffer[j] & 0xff);
tx.i8TxBuffer[0] = (unsigned char)((itarget_buffer[j + 1] & 0xff00) >> 8);
tx.i8TxBuffer[1] = (unsigned char)(itarget_buffer[j + 1] & 0xff);

ret = write(fd_rb, &tx, sizeof(uint32_t));

j += 2;
m_count++;
if (ret == 0)
{
printf("radioberry buffer full\n");
usleep(1000); //50 samples sleep (1/48K about 20usec /sample * 50)
}
if(m_count > 2048)
{
m_count -= 1024;
usleep(19200); //64 samples sleep (1/48K about 20usec /sample * 64)
}
}
}
//usleep((useconds_t)((double)numElems * 21.333));
return numElems;
}

0 comments on commit 66e4bfd

Please sign in to comment.