From e3e824998708ce7d8ef85b4456f8d224e227dbfa Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 29 Mar 2021 05:59:00 +0200 Subject: [PATCH] HackRF: generalize hardware LO correction to output plugin --- devices/hackrf/devicehackrf.cpp | 91 ++++++++++++++++++ devices/hackrf/devicehackrf.h | 1 + .../samplesink/hackrfoutput/hackrfoutput.cpp | 22 ++--- .../samplesink/hackrfoutput/hackrfoutput.h | 2 +- .../samplesource/hackrfinput/hackrfinput.cpp | 92 +------------------ .../samplesource/hackrfinput/hackrfinput.h | 1 - 6 files changed, 105 insertions(+), 104 deletions(-) diff --git a/devices/hackrf/devicehackrf.cpp b/devices/hackrf/devicehackrf.cpp index 28dce0481..6b86fe05e 100644 --- a/devices/hackrf/devicehackrf.cpp +++ b/devices/hackrf/devicehackrf.cpp @@ -17,6 +17,8 @@ #include #include +#include + #include "devicehackrf.h" DeviceHackRF::DeviceHackRF() @@ -153,3 +155,92 @@ void DeviceHackRF::enumOriginDevices(const QString& hardwareId, PluginInterface: hackrf_device_list_free(hackrf_devices); } +void DeviceHackRF::setDevicePPMCorrection(hackrf_device *dev, qint32 loPPMTenths) +{ + if (!dev) { + return; + } + + hackrf_error rc = HACKRF_SUCCESS; + const uint32_t msnaRegBase = 26; // Multisynth NA config register base + const int32_t msnaFreq = 800000000; // Multisynth NA target frequency + int32_t xo = 25000000; //Crystal frequency + int32_t a; // Multisynth NA XTAL multiplier integer 32 * 25mhz = 800mhz + int32_t b; // Multisynth NA XTAL multiplier fractional numerator 0 to 1048575 + int32_t c; // Multisynth NA XTAL multiplier fractional denominator 1048575 max resolution + int64_t rem; + int32_t p1, p2, p3; // raw register values + + xo = xo - xo/1000000*loPPMTenths/10; //adjust crystal freq by ppm error + a = msnaFreq / xo; //multiplier integer + rem = msnaFreq % xo; // multiplier remainder + + if (rem) + { //fraction mode + b = ((rem * 10485750)/xo +5) /10; //multiplier fractional numerator with rounding + c = 1048575; //multiplier fractional divisor + rc = (hackrf_error) hackrf_si5351c_write(dev, 22, 128); // MSNA set fractional mode + qDebug() << "DeviceHackRF::setDevicePPMCorrection: si5351c MSNA set to fraction mode."; + } + else + { //integer mode + b = 0; + c = 1; + rc = (hackrf_error) hackrf_si5351c_write(dev, 22, 0); // MSNA set integer mode + qDebug() << "DeviceHackRF::setDevicePPMCorrection: si5351c MSNA set to integer mode."; + } + + qDebug() << "DeviceHackRF::setDevicePPMCorrection: si5351c MSNA rem" << rem; + qDebug() << "DeviceHackRF::setDevicePPMCorrection: si5351c MSNA xoppm" << loPPMTenths / 10.0f; + qDebug() << "DeviceHackRF::setDevicePPMCorrection: si5351c MSNA xo" << xo; + qDebug() << "DeviceHackRF::setDevicePPMCorrection: si5351c MSNA a" << a; + qDebug() << "DeviceHackRF::setDevicePPMCorrection: si5351c MSNA b" << b; + qDebug() << "DeviceHackRF::setDevicePPMCorrection: si5351c MSNA c" << c; + + p1 = 128*a + (128 * b/c) - 512; + p2 = (128*b) % c; + p3 = c; + + if (rc == HACKRF_SUCCESS) { + rc = (hackrf_error) hackrf_si5351c_write(dev,msnaRegBase, (p3 >> 8) & 0xFF); // reg 26 MSNA_P3[15:8] + } + if (rc == HACKRF_SUCCESS) { + rc = (hackrf_error) hackrf_si5351c_write(dev, msnaRegBase + 1, p3 & 0xFF); // reg 27 MSNA_P3[7:0] + } + if (rc == HACKRF_SUCCESS) { + rc = (hackrf_error) hackrf_si5351c_write(dev, msnaRegBase + 2, (p1 >> 16) & 0x3); // reg28 bits 1:0 MSNA_P1[17:16] + } + if (rc == HACKRF_SUCCESS) { + rc = (hackrf_error) hackrf_si5351c_write(dev, msnaRegBase + 3, (p1 >> 8) & 0xFF); // reg 29 MSNA_P1[15:8] + } + if (rc == HACKRF_SUCCESS) { + rc = (hackrf_error) hackrf_si5351c_write(dev, msnaRegBase + 4, p1 & 0xFF); // reg 30 MSNA_P1[7:0] + } + if (rc == HACKRF_SUCCESS) { + rc = (hackrf_error) hackrf_si5351c_write(dev, msnaRegBase + 5, ((p3 & 0xF0000) >> 12) | ((p2 >> 16) & 0xF)); // bits 7:4 MSNA_P3[19:16], reg31 bits 3:0 MSNA_P2[19:16] + } + if (rc == HACKRF_SUCCESS) { + rc = (hackrf_error) hackrf_si5351c_write(dev, msnaRegBase + 6, (p2 >> 8) & 0xFF); // reg 32 MSNA_P2[15:8] + } + if (rc == HACKRF_SUCCESS) { + rc = (hackrf_error) hackrf_si5351c_write(dev, msnaRegBase + 7, p2 & 0xFF); // reg 33 MSNA_P2[7:0] + } + + if (rc != HACKRF_SUCCESS) + { + qDebug("DeviceHackRF::setDevicePPMCorrection: XTAL error adjust failed: %s", hackrf_error_name(rc)); + } + else + { + qDebug() << "DeviceHackRF::setDevicePPMCorrection: si5351c MSNA registers" + << msnaRegBase << "<-" << ((p3 >> 8) & 0xFF) + << (msnaRegBase + 1) << "<-" << (p3 & 0xFF) + << (msnaRegBase + 2) << "<-" << ((p1 >> 16) & 0x3) + << (msnaRegBase + 3) << "<-" << ((p1 >> 8) & 0xFF) + << (msnaRegBase + 4) << "<-" << (p1 & 0xFF) + << (msnaRegBase + 5) << "<-" << (((p3 & 0xF0000) >> 12) | ((p2 >> 16) & 0xF)) + << (msnaRegBase + 6) << "<-" << ((p2 >> 8) & 0xFF) + << (msnaRegBase + 7) << "<-" << (p2 & 0xFF); + qDebug() << "DeviceHackRF::setDevicePPMCorrection: XTAL error adjusted by" << (loPPMTenths / 10.0f) << "PPM."; + } +} diff --git a/devices/hackrf/devicehackrf.h b/devices/hackrf/devicehackrf.h index cd6de7464..b05e96c21 100644 --- a/devices/hackrf/devicehackrf.h +++ b/devices/hackrf/devicehackrf.h @@ -30,6 +30,7 @@ public: static hackrf_device *open_hackrf(int sequence); static hackrf_device *open_hackrf(const char * const serial); static void enumOriginDevices(const QString& hardwareId, PluginInterface::OriginDevices& originDevices); + static void setDevicePPMCorrection(hackrf_device *dev, qint32 loPPMTenths); protected: DeviceHackRF(); DeviceHackRF(const DeviceHackRF&) {} diff --git a/plugins/samplesink/hackrfoutput/hackrfoutput.cpp b/plugins/samplesink/hackrfoutput/hackrfoutput.cpp index 51cbe19bd..f227659a3 100644 --- a/plugins/samplesink/hackrfoutput/hackrfoutput.cpp +++ b/plugins/samplesink/hackrfoutput/hackrfoutput.cpp @@ -306,20 +306,19 @@ bool HackRFOutput::handleMessage(const Message& message) } } -void HackRFOutput::setDeviceCenterFrequency(quint64 freq_hz, qint32 LOppmTenths) +void HackRFOutput::setDeviceCenterFrequency(quint64 freq_hz) { if (!m_dev) { return; } - qint64 df = ((qint64)freq_hz * LOppmTenths) / 10000000LL; - hackrf_error rc = (hackrf_error) hackrf_set_freq(m_dev, static_cast(freq_hz + df)); + hackrf_error rc = (hackrf_error) hackrf_set_freq(m_dev, static_cast(freq_hz)); - if (rc != HACKRF_SUCCESS) { - qWarning("HackRFOutput::setDeviceCenterFrequency: could not frequency to %llu Hz", freq_hz + df); - } else { - qDebug("HackRFOutput::setDeviceCenterFrequency: frequency set to %llu Hz", freq_hz + df); - } + if (rc != HACKRF_SUCCESS) { + qWarning("HackRFInput::setDeviceCenterFrequency: could not frequency to %llu Hz", freq_hz); + } else { + qDebug("HackRFInput::setDeviceCenterFrequency: frequency set to %llu Hz", freq_hz); + } } bool HackRFOutput::applySettings(const HackRFOutputSettings& settings, bool force) @@ -413,8 +412,10 @@ bool HackRFOutput::applySettings(const HackRFOutputSettings& settings, bool forc if ((m_settings.m_centerFrequency != settings.m_centerFrequency) || force) { reverseAPIKeys.append("centerFrequency"); } - if ((m_settings.m_LOppmTenths != settings.m_LOppmTenths) || force) { + if ((m_settings.m_LOppmTenths != settings.m_LOppmTenths) || force) + { reverseAPIKeys.append("LOppmTenths"); + DeviceHackRF::setDevicePPMCorrection(m_dev, settings.m_LOppmTenths); } if ((m_settings.m_fcPos != settings.m_fcPos) || force) { reverseAPIKeys.append("fcPos"); @@ -428,7 +429,6 @@ bool HackRFOutput::applySettings(const HackRFOutputSettings& settings, bool forc if ((m_settings.m_centerFrequency != settings.m_centerFrequency) || (m_settings.m_devSampleRate != settings.m_devSampleRate) || - (m_settings.m_LOppmTenths != settings.m_LOppmTenths) || (m_settings.m_log2Interp != settings.m_log2Interp) || (m_settings.m_fcPos != settings.m_fcPos) || (m_settings.m_transverterMode != settings.m_transverterMode) || @@ -441,7 +441,7 @@ bool HackRFOutput::applySettings(const HackRFOutputSettings& settings, bool forc (DeviceSampleSink::fcPos_t) settings.m_fcPos, settings.m_devSampleRate, settings.m_transverterMode); - setDeviceCenterFrequency(deviceCenterFrequency, settings.m_LOppmTenths); + setDeviceCenterFrequency(deviceCenterFrequency); if (m_deviceAPI->getSourceBuddies().size() > 0) { diff --git a/plugins/samplesink/hackrfoutput/hackrfoutput.h b/plugins/samplesink/hackrfoutput/hackrfoutput.h index 48e751ab8..7ee60d44f 100644 --- a/plugins/samplesink/hackrfoutput/hackrfoutput.h +++ b/plugins/samplesink/hackrfoutput/hackrfoutput.h @@ -159,7 +159,7 @@ private: void closeDevice(); bool applySettings(const HackRFOutputSettings& settings, bool force); // hackrf_device *open_hackrf_from_sequence(int sequence); - void setDeviceCenterFrequency(quint64 freq_hz, qint32 LOppmTenths); + void setDeviceCenterFrequency(quint64 freq_hz); void webapiReverseSendSettings(QList& deviceSettingsKeys, const HackRFOutputSettings& settings, bool force); void webapiReverseSendStartStop(bool start); diff --git a/plugins/samplesource/hackrfinput/hackrfinput.cpp b/plugins/samplesource/hackrfinput/hackrfinput.cpp index 500ea8ba0..df75dd955 100644 --- a/plugins/samplesource/hackrfinput/hackrfinput.cpp +++ b/plugins/samplesource/hackrfinput/hackrfinput.cpp @@ -331,96 +331,6 @@ void HackRFInput::setDeviceCenterFrequency(quint64 freq_hz) } } -void HackRFInput::setDevicePPMCorrection(qint32 loPPMTenths) -{ - if (!m_dev) { - return; - } - - hackrf_error rc = HACKRF_SUCCESS; - const uint32_t msnaRegBase = 26; // Multisynth NA config register base - const int32_t msnaFreq = 800000000; // Multisynth NA target frequency - int32_t xo = 25000000; //Crystal frequency - int32_t a; // Multisynth NA XTAL multiplier integer 32 * 25mhz = 800mhz - int32_t b; // Multisynth NA XTAL multiplier fractional numerator 0 to 1048575 - int32_t c; // Multisynth NA XTAL multiplier fractional denominator 1048575 max resolution - int64_t rem; - int32_t p1, p2, p3; // raw register values - - xo = xo - xo/1000000*loPPMTenths/10; //adjust crystal freq by ppm error - a = msnaFreq / xo; //multiplier integer - rem = msnaFreq % xo; // multiplier remainder - - if (rem) - { //fraction mode - b = ((rem * 10485750)/xo +5) /10; //multiplier fractional numerator with rounding - c = 1048575; //multiplier fractional divisor - rc = (hackrf_error) hackrf_si5351c_write(m_dev, 22, 128); // MSNA set fractional mode - qDebug() << "HackRFInput::setDevicePPMCorrection: si5351c MSNA set to fraction mode."; - } - else - { //integer mode - b = 0; - c = 1; - rc = (hackrf_error) hackrf_si5351c_write(m_dev, 22, 0); // MSNA set integer mode - qDebug() << "HackRFInput::setDevicePPMCorrection: si5351c MSNA set to integer mode."; - } - - qDebug() << "HackRFInput::setDevicePPMCorrection: si5351c MSNA rem" << rem; - qDebug() << "HackRFInput::setDevicePPMCorrection: si5351c MSNA xoppm" << loPPMTenths / 10.0f; - qDebug() << "HackRFInput::setDevicePPMCorrection: si5351c MSNA xo" << xo; - qDebug() << "HackRFInput::setDevicePPMCorrection: si5351c MSNA a" << a; - qDebug() << "HackRFInput::setDevicePPMCorrection: si5351c MSNA b" << b; - qDebug() << "HackRFInput::setDevicePPMCorrection: si5351c MSNA c" << c; - - p1 = 128*a + (128 * b/c) - 512; - p2 = (128*b) % c; - p3 = c; - - if (rc == HACKRF_SUCCESS) { - rc = (hackrf_error) hackrf_si5351c_write(m_dev,msnaRegBase, (p3 >> 8) & 0xFF); // reg 26 MSNA_P3[15:8] - } - if (rc == HACKRF_SUCCESS) { - rc = (hackrf_error) hackrf_si5351c_write(m_dev, msnaRegBase + 1, p3 & 0xFF); // reg 27 MSNA_P3[7:0] - } - if (rc == HACKRF_SUCCESS) { - rc = (hackrf_error) hackrf_si5351c_write(m_dev, msnaRegBase + 2, (p1 >> 16) & 0x3); // reg28 bits 1:0 MSNA_P1[17:16] - } - if (rc == HACKRF_SUCCESS) { - rc = (hackrf_error) hackrf_si5351c_write(m_dev, msnaRegBase + 3, (p1 >> 8) & 0xFF); // reg 29 MSNA_P1[15:8] - } - if (rc == HACKRF_SUCCESS) { - rc = (hackrf_error) hackrf_si5351c_write(m_dev, msnaRegBase + 4, p1 & 0xFF); // reg 30 MSNA_P1[7:0] - } - if (rc == HACKRF_SUCCESS) { - rc = (hackrf_error) hackrf_si5351c_write(m_dev, msnaRegBase + 5, ((p3 & 0xF0000) >> 12) | ((p2 >> 16) & 0xF)); // bits 7:4 MSNA_P3[19:16], reg31 bits 3:0 MSNA_P2[19:16] - } - if (rc == HACKRF_SUCCESS) { - rc = (hackrf_error) hackrf_si5351c_write(m_dev, msnaRegBase + 6, (p2 >> 8) & 0xFF); // reg 32 MSNA_P2[15:8] - } - if (rc == HACKRF_SUCCESS) { - rc = (hackrf_error) hackrf_si5351c_write(m_dev, msnaRegBase + 7, p2 & 0xFF); // reg 33 MSNA_P2[7:0] - } - - if (rc != HACKRF_SUCCESS) - { - qDebug("HackRFInput::applySettings: XTAL error adjust failed: %s", hackrf_error_name(rc)); - } - else - { - qDebug() << "HackRFInput::applySettings: si5351c MSNA registers" - << msnaRegBase << "<-" << ((p3 >> 8) & 0xFF) - << (msnaRegBase + 1) << "<-" << (p3 & 0xFF) - << (msnaRegBase + 2) << "<-" << ((p1 >> 16) & 0x3) - << (msnaRegBase + 3) << "<-" << ((p1 >> 8) & 0xFF) - << (msnaRegBase + 4) << "<-" << (p1 & 0xFF) - << (msnaRegBase + 5) << "<-" << (((p3 & 0xF0000) >> 12) | ((p2 >> 16) & 0xF)) - << (msnaRegBase + 6) << "<-" << ((p2 >> 8) & 0xFF) - << (msnaRegBase + 7) << "<-" << (p2 & 0xFF); - qDebug() << "HackRFInput::applySettings: XTAL error adjusted by" << (loPPMTenths / 10.0f) << "PPM."; - } -} - bool HackRFInput::applySettings(const HackRFInputSettings& settings, bool force) { // QMutexLocker mutexLocker(&m_mutex); @@ -502,7 +412,7 @@ bool HackRFInput::applySettings(const HackRFInputSettings& settings, bool force) if ((m_settings.m_LOppmTenths != settings.m_LOppmTenths) || force) { reverseAPIKeys.append("LOppmTenths"); - setDevicePPMCorrection(settings.m_LOppmTenths); + DeviceHackRF::setDevicePPMCorrection(m_dev, settings.m_LOppmTenths); } if ((m_settings.m_fcPos != settings.m_fcPos) || force) { diff --git a/plugins/samplesource/hackrfinput/hackrfinput.h b/plugins/samplesource/hackrfinput/hackrfinput.h index ffebcf078..bd7619828 100644 --- a/plugins/samplesource/hackrfinput/hackrfinput.h +++ b/plugins/samplesource/hackrfinput/hackrfinput.h @@ -160,7 +160,6 @@ private: void closeDevice(); bool applySettings(const HackRFInputSettings& settings, bool force); void setDeviceCenterFrequency(quint64 freq); - void setDevicePPMCorrection(qint32 loPPMTenths); void webapiReverseSendSettings(QList& deviceSettingsKeys, const HackRFInputSettings& settings, bool force); void webapiReverseSendStartStop(bool start);