Skip to content

Commit

Permalink
fix tx
Browse files Browse the repository at this point in the history
  • Loading branch information
paulh002 committed May 28, 2022
1 parent b0087c0 commit a60b790
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 328 deletions.
17 changes: 6 additions & 11 deletions SoapyRadioberry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <memory>
#include <string.h>
#include <mutex>
#include <cstring>
#include "radioberry_ioctl.h"
#include "i2c.h"


#define TX_MAX 4800
#define TX_MAX_BUFFER (TX_MAX * 8)
Expand All @@ -30,9 +31,12 @@ class sdr_stream
direction = dir;
}
int get_direction() { return direction; }
void set_stream_format(radioberrysdrStreamFormat sf) { streamFormat = sf; };
radioberrysdrStreamFormat get_stream_format() { return streamFormat; };

private:
int direction;
radioberrysdrStreamFormat streamFormat;
};


Expand Down Expand Up @@ -138,23 +142,14 @@ class SoapyRadioberry : public SoapySDR::Device

void controlRadioberry(uint32_t command, uint32_t command_data);

/*******************************************************************
* I2C API
******************************************************************/

std::string readI2C(const int addr, const size_t numBytes);
void writeI2C(const int addr, const std::string &data);

private:

int fd_rb;
double 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;
radioberrysdrStreamFormat streamFormat;
std::mutex send_command;
std::vector<sdr_stream *> streams;
bool mox;
};
2 changes: 0 additions & 2 deletions SoapyRadioberry.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="i2c.cpp" />
<ClCompile Include="SoapyRadioberry.cpp" />
<ClCompile Include="SoapyRadioberrySettings.cpp" />
<ClCompile Include="SoapyRadioberryStreaming.cpp" />
Expand All @@ -67,7 +66,6 @@
<None Include="SoapyRadioberry-Release.vgdbsettings" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="i2c.h" />
<ClInclude Include="radioberry_ioctl.h" />
<ClInclude Include="SoapyRadioberry.hpp" />
</ItemGroup>
Expand Down
6 changes: 0 additions & 6 deletions SoapyRadioberry.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
<ClCompile Include="SoapyRadioberryStreaming.cpp">
<Filter>Source files</Filter>
</ClCompile>
<ClCompile Include="i2c.cpp">
<Filter>Source files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="SoapyRadioberry-Debug.vgdbsettings">
Expand All @@ -47,8 +44,5 @@
<ClInclude Include="radioberry_ioctl.h">
<Filter>Header files</Filter>
</ClInclude>
<ClInclude Include="i2c.h">
<Filter>Header files</Filter>
</ClInclude>
</ItemGroup>
</Project>
79 changes: 25 additions & 54 deletions SoapyRadioberrySettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,9 @@ SoapyRadioberry::SoapyRadioberry( const SoapySDR::Kwargs &args ){

SoapySDR_setLogLevel(SOAPY_SDR_INFO);
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::SoapyRadioberry constructor called");

mox = false;
no_channels = 1;
fd_rb = open("/dev/radioberry", O_RDWR);
try
{
i2c_ptr = std::make_unique<rpihw::driver::i2c> (rpihw::driver::i2c("/dev/i2c-1"));
i2c_available = true;
}
catch (std::string s)
{
printf("I2c not found %s", s.c_str());
i2c_available = false;
}
}

SoapyRadioberry::~SoapyRadioberry(void)
Expand Down Expand Up @@ -232,17 +222,24 @@ void SoapyRadioberry::setGain( const int direction, const size_t channel, const

if (direction == SOAPY_SDR_RX)
{
command = 0x14;
if (mox)
command = 0x15;
else
command = 0x14;
command_data = (0x40 | (((uint32_t)value + 12) & 0x3F));
}

if(direction==SOAPY_SDR_TX)
{ // 0 -7 TX RF gain
{ // 0 -7 TX RF gain
if (!mox)
return;

uint32_t z = (uint32_t)value;
if (value > 15) z = 15;
if (value < 0.0) z = 0;
z = z << 28;
command = 0x13;
command_data = z;
command_data = z;
}

this->SoapyRadioberry::controlRadioberry(command, command_data);
Expand All @@ -256,52 +253,26 @@ void SoapyRadioberry::setFrequency( const int direction, const size_t channel,
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::setFrequency called");

uint32_t command = 0;

if(direction==SOAPY_SDR_RX) command = 4;
if(direction==SOAPY_SDR_TX) command = 3;

uint32_t command_data = (uint32_t) frequency;

this->SoapyRadioberry::controlRadioberry(command, command_data);
}

void SoapyRadioberry::writeI2C(const int addr, const std::string &data)
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::writeI2C called");

if (!i2c_available)
return;
i2c_ptr->addr(addr);
try
if (direction == SOAPY_SDR_RX)
{
i2c_ptr->write((uint8_t *)data.c_str(), data.size());
if (mox)
command = 5;
else
command = 4;
}
catch (std::string s)
{
printf("%s", s.c_str());
}
}

std::string SoapyRadioberry::readI2C(const int addr, const size_t numBytes)
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::readI2C called");

std::string data;

if (!i2c_available)
return std::string("");
i2c_ptr->addr(addr);
data.reserve(numBytes);
try
if (direction == SOAPY_SDR_TX)
{
i2c_ptr->read((uint8_t *)data.c_str(), numBytes);
data.resize(numBytes);
if (!mox)
return;
command = 3;
}
catch (std::string s)
{
printf("%s", s.c_str());
}
return data;

uint32_t command_data = (uint32_t) frequency;

this->SoapyRadioberry::controlRadioberry(command, command_data);
}

// end of source.

38 changes: 25 additions & 13 deletions SoapyRadioberryStreaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::setSampleRate called");

int irate = floor(rate);
uint32_t ucom =0x00000004;
uint32_t ucom =0x4;
uint32_t command = 0;

if (direction == SOAPY_SDR_TX)
{
command = 0x1;
if (!mox)
return;
}
if (direction == SOAPY_SDR_RX)
{
sample_rate = rate;
if (mox)
command = 1;
}

// incase of transmit still the receive samplerate need to be send
if (sample_rate < 48001.0)
Expand Down Expand Up @@ -99,23 +107,25 @@ SoapySDR::Stream *SoapyRadioberry::setupStream(
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::setupStream called");
startTime = std::chrono::high_resolution_clock::now();
//check the format
sdr_stream *ptr;
ptr = new sdr_stream(direction);

if (format == SOAPY_SDR_CF32) {
SoapySDR_log(SOAPY_SDR_INFO, "Using format CF32.");
streamFormat = RADIOBERRY_SDR_CF32;
ptr->set_stream_format(RADIOBERRY_SDR_CF32);
}
else if(format == SOAPY_SDR_CS16 && direction == SOAPY_SDR_TX)
{
SoapySDR_log(SOAPY_SDR_INFO, "Using format CS16.");
streamFormat = RADIOBERRY_SDR_CS16;
ptr->set_stream_format(RADIOBERRY_SDR_CS16);
mox = true;
}
else
{
throw std::runtime_error(
"setupStream invalid format '" + format + "' -- Only CF32 is supported by SoapyRadioberrySDR module.");
}

sdr_stream *ptr;
ptr = new sdr_stream(direction);
streams.push_back(ptr);
return (SoapySDR::Stream *)ptr;
}
Expand All @@ -129,6 +139,7 @@ void SoapyRadioberry::closeStream(SoapySDR::Stream *stream)
{
if (((sdr_stream *)stream)->get_direction() == SOAPY_SDR_TX)
{ // switch off TX stream
mox = false;
setSampleRate(SOAPY_SDR_RX, 0, sample_rate);
}
delete ((sdr_stream *)stream);
Expand All @@ -153,14 +164,15 @@ int SoapyRadioberry::readStream(
int16_t *itarget_buffer = (int16_t *) buff_base;
int32_t left_sample;
int32_t right_sample;

sdr_stream *ptr = (sdr_stream *)handle;

char rx_buffer[512];
for(int ii = 0 ; ii < npackages ; ii++)
{
no_bytes = read(fd_rb, rx_buffer, sizeof(rx_buffer));
nr_samples = no_bytes / 6;
//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]);
if(streamFormat == RADIOBERRY_SDR_CF32)
if (ptr->get_stream_format() == RADIOBERRY_SDR_CF32)
{
for (int i = 0; i < no_bytes; i += 6) {
left_sample = (int32_t)((signed char) rx_buffer[i]) << 16;
Expand All @@ -175,7 +187,7 @@ int SoapyRadioberry::readStream(
target_buffer[iq++] = (float)right_sample / 8388608.0; // 24 bit sample
}
}
if (streamFormat == RADIOBERRY_SDR_CS16)
if (ptr->get_stream_format() == RADIOBERRY_SDR_CS16)
{
for (int i = 0; i < no_bytes; i += 6) {
left_sample = (int32_t)((signed char) rx_buffer[i]) << 16;
Expand All @@ -201,8 +213,6 @@ union uTxBuffer
unsigned char i8TxBuffer[4];
};

uTxBuffer tx;

int SoapyRadioberry::writeStream(SoapySDR::Stream *stream, const void * const *buffs, const size_t numElems, int &flags, const long long timeNs, const long timeoutUs)
{
int iq = 0;
Expand All @@ -212,8 +222,10 @@ int SoapyRadioberry::writeStream(SoapySDR::Stream *stream, const void * const *b
void const *buff_base = buffs[0];
float *target_buffer = (float *) buff_base;
int16_t *itarget_buffer = (int16_t *) buff_base;
uTxBuffer tx;
sdr_stream *ptr = (sdr_stream *)stream;

if (streamFormat == RADIOBERRY_SDR_CF32)
if (ptr->get_stream_format() == RADIOBERRY_SDR_CF32)
{
for (int ii = 0; ii < numElems; ii++)
{
Expand All @@ -222,7 +234,7 @@ int SoapyRadioberry::writeStream(SoapySDR::Stream *stream, const void * const *b
ret = write(fd_rb, &tx, 4 * sizeof(uint8_t));
}
}
if (streamFormat == RADIOBERRY_SDR_CS16)
if (ptr->get_stream_format() == RADIOBERRY_SDR_CS16)
{
int j = 0;

Expand All @@ -233,7 +245,7 @@ int SoapyRadioberry::writeStream(SoapySDR::Stream *stream, const void * const *b
tx.i8TxBuffer[2] = (unsigned char)(((-1 * itarget_buffer[j + 1]) & 0xff00) >> 8);
tx.i8TxBuffer[3] = (unsigned char)((-1 * itarget_buffer[j + 1]) & 0xff);

ret = write(fd_rb, &tx, sizeof(uint32_t));
ret = 4; //write(fd_rb, &tx, sizeof(uint32_t));
j += 2;
}
}
Expand Down
Loading

0 comments on commit a60b790

Please sign in to comment.