feat: add frequency conversion functions for RF frequency management

This commit is contained in:
2025-08-08 12:26:30 +08:00
parent fc9b06aec0
commit 59ba0f81d8

View File

@ -139,6 +139,19 @@ namespace details {}
constexpr auto delay_ms = app::utils::delay_ms;
/**
* \brief Set the RF frequency in MHz
* \param freq the frequency to set
* \sa set_frequency
*
* \note \text{value}=\frac{f_{\text{XTAL}}}{2^{25}}\cdot f_{\text{RF}}
*/
inline constexpr uint32_t freq_mhz_to_freq_raw(const freq_t freq) {
uint32_t frf = (freq * (uint32_t(1) << RADIOLIB_SX126X_DIV_EXPONENT)) / RADIOLIB_SX126X_CRYSTAL_FREQ;
return frf;
}
/*!
\brief Sets the module to standby mode.
*/
@ -234,16 +247,21 @@ clear_irq_status(const uint16_t mask = RADIOLIB_SX126X_IRQ_ALL) {
/**
* \brief set RF frequency by writing the raw value to registers
* \param frf the raw value to write to registers
* \note don't use this function directly, use `set_frequency` instead
* \sa set_frequency
* \sa set_frequency_raw
*/
inline Result<Unit, error_t>
set_rf_frequency(const uint32_t frf) {
const uint8_t data[] = {static_cast<uint8_t>((frf >> 24) & 0xFF), static_cast<uint8_t>((frf >> 16) & 0xFF), static_cast<uint8_t>((frf >> 8) & 0xFF), static_cast<uint8_t>(frf & 0xFF)};
set_rf_frequency(const uint32_t freq_raw) {
const uint8_t data[] = {static_cast<uint8_t>((freq_raw >> 24) & 0xFF),
static_cast<uint8_t>((freq_raw >> 16) & 0xFF),
static_cast<uint8_t>((freq_raw >> 8) & 0xFF),
static_cast<uint8_t>(freq_raw & 0xFF)};
return spi::write_stream(RADIOLIB_SX126X_CMD_SET_RF_FREQUENCY, data);
}
inline Result<Unit, error_t>
set_rf_frequency(const freq_t freq_mhz) {
return set_rf_frequency(freq_mhz_to_freq_raw(freq_mhz));
}
inline Result<Unit, error_t>
set_buffer_base_address(const uint8_t txBaseAddress = DEFAULT_TX_BUFFER_ADDRESS, const uint8_t rxBaseAddress = DEFAULT_RX_BUFFER_ADDRESS) {
const uint8_t data[] = {txBaseAddress, rxBaseAddress};
@ -295,18 +313,6 @@ clear_device_errors() {
return {};
}
/**
* \brief Set the RF frequency in MHz
* \param freq the frequency to set
* \sa set_frequency
*
* \note \text{value}=\frac{f_{\text{XTAL}}}{2^{25}}\cdot f_{\text{RF}}
*/
inline constexpr uint32_t frequency_raw(const freq_t freq) {
uint32_t frf = (freq * (uint32_t(1) << RADIOLIB_SX126X_DIV_EXPONENT)) / RADIOLIB_SX126X_CRYSTAL_FREQ;
return frf;
}
/**
* @brief Image calibration is done through the command CalibrateImage(...) for
* a given range of frequencies defined by the parameters freq1