From a60b7905209dce5b768e5c94e1297c847a9bfd6a Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 28 May 2022 02:28:20 +0200 Subject: [PATCH] fix tx --- SoapyRadioberry.hpp | 17 ++--- SoapyRadioberry.vcxproj | 2 - SoapyRadioberry.vcxproj.filters | 6 -- SoapyRadioberrySettings.cpp | 79 +++++++------------- SoapyRadioberryStreaming.cpp | 38 ++++++---- i2c.cpp | 119 ------------------------------ i2c.h | 123 -------------------------------- 7 files changed, 56 insertions(+), 328 deletions(-) delete mode 100644 i2c.cpp delete mode 100644 i2c.h diff --git a/SoapyRadioberry.hpp b/SoapyRadioberry.hpp index 564ff2c..d5436a9 100644 --- a/SoapyRadioberry.hpp +++ b/SoapyRadioberry.hpp @@ -10,8 +10,9 @@ #include #include #include +#include #include "radioberry_ioctl.h" -#include "i2c.h" + #define TX_MAX 4800 #define TX_MAX_BUFFER (TX_MAX * 8) @@ -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; }; @@ -138,13 +142,6 @@ 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; @@ -152,9 +149,7 @@ class SoapyRadioberry : public SoapySDR::Device int rx_frequency; int no_channels; struct rb_info_arg_t rb_control; - std::unique_ptr i2c_ptr; - bool i2c_available = false; - radioberrysdrStreamFormat streamFormat; std::mutex send_command; std::vector streams; + bool mox; }; diff --git a/SoapyRadioberry.vcxproj b/SoapyRadioberry.vcxproj index bfe503e..215b984 100644 --- a/SoapyRadioberry.vcxproj +++ b/SoapyRadioberry.vcxproj @@ -57,7 +57,6 @@ - @@ -67,7 +66,6 @@ - diff --git a/SoapyRadioberry.vcxproj.filters b/SoapyRadioberry.vcxproj.filters index cc442b6..4f94e0a 100644 --- a/SoapyRadioberry.vcxproj.filters +++ b/SoapyRadioberry.vcxproj.filters @@ -28,9 +28,6 @@ Source files - - Source files - @@ -47,8 +44,5 @@ Header files - - Header files - \ No newline at end of file diff --git a/SoapyRadioberrySettings.cpp b/SoapyRadioberrySettings.cpp index a4a0f56..ab29c36 100644 --- a/SoapyRadioberrySettings.cpp +++ b/SoapyRadioberrySettings.cpp @@ -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("/dev/i2c-1")); - i2c_available = true; - } - catch (std::string s) - { - printf("I2c not found %s", s.c_str()); - i2c_available = false; - } } SoapyRadioberry::~SoapyRadioberry(void) @@ -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); @@ -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. diff --git a/SoapyRadioberryStreaming.cpp b/SoapyRadioberryStreaming.cpp index 6e46a2b..37e20ba 100644 --- a/SoapyRadioberryStreaming.cpp +++ b/SoapyRadioberryStreaming.cpp @@ -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) @@ -99,14 +107,18 @@ 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 { @@ -114,8 +126,6 @@ SoapySDR::Stream *SoapyRadioberry::setupStream( "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; } @@ -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); @@ -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; @@ -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; @@ -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; @@ -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++) { @@ -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; @@ -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; } } diff --git a/i2c.cpp b/i2c.cpp deleted file mode 100644 index d0a3bf4..0000000 --- a/i2c.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/* - Title --- driver/i2c.cpp - - Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com - - This file is part of Rpi-hw. - - Rpi-hw is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation version 3 of the License. - - Rpi-hw is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with Rpi-hw. If not, see . -*/ - -#include "i2c.h" - -namespace rpihw { // Begin main namespace - -namespace driver { // Begin drivers namespace - -i2c::i2c( const std::string &dev_path) - : m_dev_path ( dev_path ) -{ - - // Open device file - m_dev_fd = open( m_dev_path.c_str(), O_RDWR ); - - if ( m_dev_fd < 0 ) - throw "(Fatal) `i2c`: can't open I2C device \n" ; - -} - -void i2c::addr(uint8_t addr) - { - m_addr = addr; - // Select slave device - if(ioctl(m_dev_fd, I2C_SLAVE, addr) < 0) - throw "(Fatal) `i2c`: can't select slave device \n"; - } - -i2c::~i2c() { - - // Close the device file - close( m_dev_fd ); -} - -void -i2c::write( uint8_t *data, uint8_t size ) { - - if ( ::write( m_dev_fd, data, size ) != size ) - throw "(Fatal) `i2c`: failed to write to the bus\n"; -} - -void -i2c::read( uint8_t *data, uint8_t size ) { - - if ( ::read( m_dev_fd, data, size ) != size ) - throw "(Fatal) `i2c`: failed to read from the bus\n"; -} - -void -i2c::writeReg8( uint8_t reg, uint8_t data ) { - - // Build the buffer to send - m_buffer[0] = reg; - m_buffer[1] = data; - - // Write the data on the device - write( m_buffer, 2 ); -} - -uint8_t -i2c::readReg8( uint8_t reg ) { - - // Select the register on the device - write( ®, 1 ); - - // Read the data from the device - read( m_buffer, 1 ); - - return m_buffer[0]; -} - -void -i2c::writeReg16( uint8_t reg, uint16_t data ) { - - // Build the buffer to send - m_buffer[0] = reg; - m_buffer[1] = data & 0xFF; - m_buffer[2] = (data >> 8) & 0xFF; - - // Write the data on the device - write( m_buffer, 2 ); -} - -uint16_t -i2c::readReg16( uint8_t reg ) { - - // Select the register on the device - write( ®, 1 ); - - // Read the data from the device - read( m_buffer, 2 ); - - // Merge the 16 bit data - return (uint16_t) m_buffer[0] | ( (uint16_t) m_buffer[1] << 8 ); -} - -} // End of drivers namespace - -} // End of main namespace - - diff --git a/i2c.h b/i2c.h deleted file mode 100644 index 3fdca4a..0000000 --- a/i2c.h +++ /dev/null @@ -1,123 +0,0 @@ -/* - Title --- driver/i2c.hpp - - Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com - - This file is part of Rpi-hw. - - Rpi-hw is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation version 3 of the License. - - Rpi-hw is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with Rpi-hw. If not, see . -*/ - - -#ifndef _RPI_HW_DRIVER_I2C_HPP_ -#define _RPI_HW_DRIVER_I2C_HPP_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace rpihw { // Begin main namespace - -namespace driver { // Begin drivers namespace - -/*! - @class i2c - @brief Inter Integrated Circuit. -*/ -class i2c { - -public: - - /*! - @brief Constructor method. - @param[in] dev_path The device path. - @param[in] addr The I2C slave address. - */ - i2c( const std::string &dev_path); - void addr(uint8_t addr); - - //! Destructor method. - virtual ~i2c(); - - /*! - @brief Writes data on the device. - @param[in] data The data to write on the device. - @param[in] size Size of the data to write. - */ - void write( uint8_t *data, uint8_t size ); - - /*! - @brief Reads data from the device. - @param[in] data The buffer to store the data. - @param[in] size Size of the data to read. - */ - void read( uint8_t *data, uint8_t size ); - - /*! - @brief Writes a byte data to a register on the device. - @param[in] reg The device register. - @param[in] data The data to write to the register. - */ - void writeReg8( uint8_t reg, uint8_t data ); - - /*! - @brief Reads a byte from a register on the device. - @param[in] reg The device register. - @return The data read from the register. - */ - uint8_t readReg8( uint8_t reg ); - - /*! - @brief Writes a word data to a register on the device. - @param[in] reg The device register. - @param[in] data The data to write to the register. - */ - void writeReg16( uint8_t reg, uint16_t data ); - - /*! - @brief Reads a word from a register on the device. - @param[in] reg The device register. - @return The data read from the register. - */ - uint16_t readReg16( uint8_t reg ); - -private: - - //! The device path. - std::string m_dev_path; - - //! The I2C slave address. - uint32_t m_addr; - - //! File descriptor of the device. - int m_dev_fd; - - //! Data buffer used for I2C transmission. - uint8_t m_buffer[3]; -}; - -} // End of drivers namespace - -} // End of main namespace - - -// Include inline methods -//#include - -#endif /* _RPI_HW_DRIVER_I2C_HPP_ */