From bf061c655ffee2373c90cade51253cf0fe466c8d Mon Sep 17 00:00:00 2001 From: jgromes Date: Thu, 4 Jan 2024 18:30:22 +0100 Subject: [PATCH] [SX126x] Allow custom band calibration --- src/modules/SX126x/SX1262.cpp | 6 +++--- src/modules/SX126x/SX1262.h | 5 ++++- src/modules/SX126x/SX1268.cpp | 6 +++--- src/modules/SX126x/SX1268.h | 7 +++++-- src/modules/SX126x/SX126x.cpp | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/modules/SX126x/SX1262.cpp b/src/modules/SX126x/SX1262.cpp index b6f68cb4..0bbce30d 100644 --- a/src/modules/SX126x/SX1262.cpp +++ b/src/modules/SX126x/SX1262.cpp @@ -51,12 +51,12 @@ int16_t SX1262::setFrequency(float freq) { return(setFrequency(freq, true)); } -int16_t SX1262::setFrequency(float freq, bool calibrate) { +int16_t SX1262::setFrequency(float freq, bool calibrate, float band) { RADIOLIB_CHECK_RANGE(freq, 150.0, 960.0, RADIOLIB_ERR_INVALID_FREQUENCY); - // calibrate image rejection - assume band to be the selected frequency +- 4 MHz + // calibrate image rejection if(calibrate) { - int16_t state = SX126x::calibrateImage(freq - 4, freq + 4); + int16_t state = SX126x::calibrateImage(freq - band, freq + band); RADIOLIB_ASSERT(state); } diff --git a/src/modules/SX126x/SX1262.h b/src/modules/SX126x/SX1262.h index f47b1a81..61f8facc 100644 --- a/src/modules/SX126x/SX1262.h +++ b/src/modules/SX126x/SX1262.h @@ -75,9 +75,12 @@ class SX1262: public SX126x { \brief Sets carrier frequency. Allowed values are in range from 150.0 to 960.0 MHz. \param freq Carrier frequency to be set in MHz. \param calibrate Run image calibration. + \param band Half bandwidth for image calibration. For example, + if carrier is 434 MHz and band is set to 4 MHz, then the image will be calibrate + for band 430 - 438 MHz. Unused if calibrate is set to false, defaults to 4 MHz \returns \ref status_codes */ - int16_t setFrequency(float freq, bool calibrate); + int16_t setFrequency(float freq, bool calibrate, float band = 4); /*! \brief Sets output power. Allowed values are in range from -9 to 22 dBm. diff --git a/src/modules/SX126x/SX1268.cpp b/src/modules/SX126x/SX1268.cpp index 8aad25b7..7f14e9fa 100644 --- a/src/modules/SX126x/SX1268.cpp +++ b/src/modules/SX126x/SX1268.cpp @@ -52,12 +52,12 @@ int16_t SX1268::setFrequency(float freq) { } /// \todo integers only (all modules - frequency, data rate, bandwidth etc.) -int16_t SX1268::setFrequency(float freq, bool calibrate) { +int16_t SX1268::setFrequency(float freq, bool calibrate, float band) { RADIOLIB_CHECK_RANGE(freq, 410.0, 810.0, RADIOLIB_ERR_INVALID_FREQUENCY); - // calibrate image rejection - assume band to be the selected frequency +- 4 MHz + // calibrate image rejection if(calibrate) { - int16_t state = SX126x::calibrateImage(freq - 4, freq + 4); + int16_t state = SX126x::calibrateImage(freq - band, freq + band); RADIOLIB_ASSERT(state); } diff --git a/src/modules/SX126x/SX1268.h b/src/modules/SX126x/SX1268.h index 08b3eb41..f3f61dc8 100644 --- a/src/modules/SX126x/SX1268.h +++ b/src/modules/SX126x/SX1268.h @@ -71,12 +71,15 @@ class SX1268: public SX126x { int16_t setFrequency(float freq); /*! - \brief Sets carrier frequency. Allowed values are in range from 410.0 to 810.0 MHz. + \brief Sets carrier frequency. Allowed values are in range from 150.0 to 960.0 MHz. \param freq Carrier frequency to be set in MHz. \param calibrate Run image calibration. + \param band Half bandwidth for image calibration. For example, + if carrier is 434 MHz and band is set to 4 MHz, then the image will be calibrate + for band 430 - 438 MHz. Unused if calibrate is set to false, defaults to 4 MHz \returns \ref status_codes */ - int16_t setFrequency(float freq, bool calibrate); + int16_t setFrequency(float freq, bool calibrate, float band = 4); /*! \brief Sets output power. Allowed values are in range from -9 to 22 dBm. diff --git a/src/modules/SX126x/SX126x.cpp b/src/modules/SX126x/SX126x.cpp index e2ffdb74..59d4e764 100644 --- a/src/modules/SX126x/SX126x.cpp +++ b/src/modules/SX126x/SX126x.cpp @@ -1836,7 +1836,7 @@ int16_t SX126x::setRfFrequency(uint32_t frf) { } int16_t SX126x::calibrateImage(float freqMin, float freqMax) { - uint8_t data[] = { (uint8_t)floor((freqMin - 1.0f) / 4.0f), (uint8_t)floor((freqMax - 1.0f) / 4.0f) }; + uint8_t data[] = { (uint8_t)floor((freqMin - 1.0f) / 4.0f), (uint8_t)ceil((freqMax + 1.0f) / 4.0f) }; int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CALIBRATE_IMAGE, data, 2); // if something failed, show the device errors