Skip to content

Commit

Permalink
upgate tx
Browse files Browse the repository at this point in the history
  • Loading branch information
paulh002 committed May 1, 2022
1 parent f1e002d commit 1a2e807
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 63 deletions.
4 changes: 3 additions & 1 deletion SoapyRadioberry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include <unistd.h>
#include <sys/ioctl.h>
#include <memory>
#include <string.h>
#include <string.h>
#include <mutex>
#include "radioberry_ioctl.h"
#include "i2c.h"

Expand Down Expand Up @@ -145,4 +146,5 @@ class SoapyRadioberry : public SoapySDR::Device{
std::unique_ptr<rpihw::driver::i2c> i2c_ptr;
bool i2c_available = false;
radioberrysdrStreamFormat streamFormat;
std::mutex send_command;
};
33 changes: 22 additions & 11 deletions SoapyRadioberrySettings.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "SoapyRadioberry.hpp"
#include <chrono>
#include <thread>
#include <string>

#define RADIOBERRY_BUFFER_SIZE 4096

Expand All @@ -8,7 +11,7 @@

SoapyRadioberry::SoapyRadioberry( const SoapySDR::Kwargs &args ){

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

no_channels = 1;
Expand All @@ -22,7 +25,7 @@ SoapyRadioberry::SoapyRadioberry( const SoapySDR::Kwargs &args ){
{
printf("I2c not found %s", s.c_str());
i2c_available = false;
}
}
}

SoapyRadioberry::~SoapyRadioberry(void){
Expand All @@ -33,8 +36,9 @@ SoapyRadioberry::~SoapyRadioberry(void){
}

void SoapyRadioberry::controlRadioberry(uint32_t command, uint32_t command_data) {

//SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::controlRadioberry called");

std::unique_lock<std::mutex> soapy_lock(send_command);
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::controlRadioberry called");

uint32_t CWX =0;
uint32_t running = 1;
Expand All @@ -43,11 +47,11 @@ void SoapyRadioberry::controlRadioberry(uint32_t command, uint32_t command_data)
rb_control.command = command;
rb_control.command_data = command_data;

//fprintf(stderr, "RB-Command = %02X Command = %02X command_data = %08X Mox %d\n", rb_control.rb_command, command >> 1, command_data, command & 0x01);
fprintf(stderr, "RB-Command = %02X Command = %02X command_data = %08X Mox %d\n", rb_control.rb_command, command >> 1, command_data, command & 0x01);

if (ioctl(fd_rb, RADIOBERRY_IOC_COMMAND, &rb_control) == -1) {
SoapySDR_log(SOAPY_SDR_INFO, "Could not sent command to radioberry device.");
} //else SoapySDR_log(SOAPY_SDR_INFO, "Command sent succesfull to radioberry device.");
} else SoapySDR_log(SOAPY_SDR_INFO, "Command sent succesfull to radioberry device.");
}

std::string SoapyRadioberry::getDriverKey( void ) const
Expand All @@ -70,13 +74,20 @@ SoapySDR::Kwargs SoapyRadioberry::getHardwareInfo( void ) const
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::getHardwareInfo called");

SoapySDR::Kwargs info;

int count = 0;
struct rb_info_arg_t rb_info;

if (ioctl(fd_rb, RADIOBERRY_IOC_COMMAND, &rb_info) == -1) {
rb_info.major = 0;
rb_info.minor = 0;
}
do
{
std::memset(&rb_info, 0, sizeof(rb_info));
if (ioctl(fd_rb, RADIOBERRY_IOC_COMMAND, &rb_info) == -1)
{
rb_info.major = 0;
rb_info.minor = 0;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
count++;
} while ((rb_info.major == 0 || rb_info.major > 128) && count < 20);

unsigned int major, minor;
major = rb_info.major;
Expand Down
85 changes: 35 additions & 50 deletions SoapyRadioberryStreaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@
#include "SoapyRadioberry.hpp"

void SoapyRadioberry::setSampleRate( const int direction, const size_t channel, const double rate ) {
//SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::setSampleRate called");
SoapySDR_log(SOAPY_SDR_INFO, "SoapyRadioberry::setSampleRate called");

int irate = floor(rate);
uint32_t ucom =0x00000004;
uint32_t command = 0;
if (direction == SOAPY_SDR_TX)
command = 0x1;
if (direction == SOAPY_SDR_RX)
sample_rate = rate;

// incase of transmit still the receive samplerate need to be send
if (rate < 48001.0)
ucom = 0x00000004;
if (rate > 48000.0 && rate < 96001.0)
ucom = 0x01000004;
if (rate > 96000.0 && rate < 192001.0)
ucom = 0x02000004;
if (rate > 192000.0)
ucom = 0x03000004;
this->SoapyRadioberry::controlRadioberry(command, ucom);

if (direction == SOAPY_SDR_TX)
command = 0x1;
if (direction == SOAPY_SDR_RX)
sample_rate = rate;

// incase of transmit still the receive samplerate need to be send
if (sample_rate < 48001.0)
ucom = 0x00000004;
if (sample_rate > 48000.0 && sample_rate < 96001.0)
ucom = 0x01000004;
if (sample_rate > 96000.0 && sample_rate < 192001.0)
ucom = 0x02000004;
if (sample_rate > 192000.0)
ucom = 0x03000004;
this->SoapyRadioberry::controlRadioberry(command, ucom);
}

SoapySDR::RangeList SoapyRadioberry::getSampleRateRange(const int direction, const size_t channel) const
Expand Down Expand Up @@ -126,26 +126,23 @@ int SoapyRadioberry::readStream(
long long &timeNs,
const long timeoutUs )
{
int i;
int iq = 0;
int nr_samples;
int nr_samples, no_bytes, iq=0;

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

char rx_buffer[512];
//setSampleRate(SOAPY_SDR_RX, 0, sample_rate);
for(int ii = 0 ; ii < npackages ; ii++)
{
nr_samples = read(fd_rb, rx_buffer, sizeof(rx_buffer));
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)
{
int32_t left_sample;
int32_t right_sample;

for (i = 0; i < nr_samples; i += 6) {
for (int i = 0; i < no_bytes; i += 6) {
left_sample = (int32_t)((signed char) rx_buffer[i]) << 16;
left_sample |= (int32_t)((((unsigned char)rx_buffer[i + 1]) << 8) & 0xFF00);
left_sample |= (int32_t)((unsigned char)rx_buffer[i + 2] & 0xFF);
Expand All @@ -157,14 +154,10 @@ int SoapyRadioberry::readStream(
target_buffer[iq++] = (float)left_sample / 8388608.0; // 24 bit sample
target_buffer[iq++] = (float)right_sample / 8388608.0; // 24 bit sample
}
//printf("nr_samples %d sample: %d %d \n", nr_samples, left_sample, right_sample);
}
if (streamFormat == RADIOBERRY_SDR_CS16)
{
int32_t left_sample;
int32_t right_sample;

for (i = 0; i < nr_samples; i += 6) {
for (int i = 0; i < no_bytes; i += 6) {
left_sample = (int32_t)((signed char) rx_buffer[i]) << 16;
left_sample |= (int32_t)((((unsigned char)rx_buffer[i + 1]) << 8) & 0xFF00);
left_sample |= (int32_t)((unsigned char)rx_buffer[i + 2] & 0xFF);
Expand All @@ -177,62 +170,54 @@ int SoapyRadioberry::readStream(
itarget_buffer[iq++] = (int16_t)(right_sample >> 8); // 16 bit sample
}
}
//printf("nr_samples %d sample: %d %d \n", nr_samples, left_sample, right_sample);
}
return (npackages * nr_samples / 6); //return the number of IQ samples
return (npackages * nr_samples); //return the number of IQ samples
}

union uTxBuffer
{
std::uint16_t i16TxBuffer[2];
unsigned char i8TxBuffer[4];
std::uint16_t i16TxBuffer[2];
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;
size_t ret;
int left_sample;
int right_sample;
int nr_samples;

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


uTxBuffer tx;

//setSampleRate(SOAPY_SDR_TX, 0, sample_rate);
if (streamFormat == RADIOBERRY_SDR_CF32)
{
for (int ii = 0; ii < numElems; ii++)
{
float i, q;
int16_t di, dq;

tx.i16TxBuffer[0] = (int16_t)(target_buffer[iq++] * 16384.0f);
tx.i16TxBuffer[1] = (int16_t)(target_buffer[iq++] * -16384.0f);
tx.i16TxBuffer[1] = (int16_t)(target_buffer[iq++] * 16384.0f);
ret = write(fd_rb, &tx, 4 * sizeof(uint8_t));
}
}
if (streamFormat == RADIOBERRY_SDR_CS16)
{
int j = 0;

//printf("SoapySDR send %d elements \n", numElems);
for (int ii = 0; ii < numElems; ii++)
{
//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[0] = (unsigned char)((itarget_buffer[j] & 0xff00) >> 8);
tx.i8TxBuffer[1] = (unsigned char)(itarget_buffer[j] & 0xff);
tx.i8TxBuffer[2] = (unsigned char)(((-1 * itarget_buffer[j + 1]) & 0xff00) >> 8);
tx.i8TxBuffer[3] = (unsigned char)((-1 * itarget_buffer[j + 1]) & 0xff);
tx.i8TxBuffer[3] = (unsigned char)((-1 * itarget_buffer[j + 1]) & 0xff);

ret = write(fd_rb, &tx, sizeof(uint32_t));
j += 2;
}

}
if (ret < 0)
SoapySDR_log(SOAPY_SDR_ERROR, "Write error to radioberry driver");
return numElems;
}
2 changes: 1 addition & 1 deletion driver/radioberry_firmware.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void read_iq_sample(int lnrx, int iqs, unsigned char iqdata[]){
iqdata[i] |= (((value >> 13) & 1) << 2);
iqdata[i] |= (((value >> 12) & 1) << 1);
iqdata[i] |= (((value >> 5) & 1));
}
}
}

int write_iq_sample(unsigned char tx_iqdata[]) {
Expand Down

0 comments on commit 1a2e807

Please sign in to comment.