From bd8385464e611906cfc8effbaf43b0345cd3525c Mon Sep 17 00:00:00 2001 From: Mark Thompson <129641948+NotherNgineer@users.noreply.github.com> Date: Sun, 28 Jan 2024 10:02:11 -0600 Subject: [PATCH] Tuner code readability (no functional change) (#1820) --- firmware/application/radio.cpp | 2 ++ firmware/application/rf_path.hpp | 4 +-- firmware/application/tuning.cpp | 43 ++++++++++++++++---------------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/firmware/application/radio.cpp b/firmware/application/radio.cpp index 37c719c26..b6fd614d4 100644 --- a/firmware/application/radio.cpp +++ b/firmware/application/radio.cpp @@ -184,11 +184,13 @@ bool set_tuning_frequency(const rf::Frequency frequency) { if (tuning_config.is_valid()) { first_if.disable(); + // Program first local oscillator frequency (if there is one) into RFFC507x if (tuning_config.first_lo_frequency) { first_if.set_frequency(tuning_config.first_lo_frequency); first_if.enable(); } + // Program second local oscillator frequency into MAX283x const auto result_second_if = second_if->set_frequency(tuning_config.second_lo_frequency); rf_path.set_band(tuning_config.rf_path_band); diff --git a/firmware/application/rf_path.hpp b/firmware/application/rf_path.hpp index d6622f80f..e886624d8 100644 --- a/firmware/application/rf_path.hpp +++ b/firmware/application/rf_path.hpp @@ -39,8 +39,8 @@ enum class Direction { namespace path { -constexpr FrequencyRange band_low{0, 2170000000}; -constexpr FrequencyRange band_high{2740000000, 7250000000}; +constexpr FrequencyRange band_low{0, 2170'000'000}; +constexpr FrequencyRange band_high{2740'000'000, 7250'000'000}; constexpr FrequencyRange band_mid{band_low.maximum, band_high.minimum}; enum class Band { diff --git a/firmware/application/tuning.cpp b/firmware/application/tuning.cpp index 5024e8cb0..f1425a52f 100644 --- a/firmware/application/tuning.cpp +++ b/firmware/application/tuning.cpp @@ -26,44 +26,43 @@ namespace tuning { namespace config { -namespace { - +// Low band <2170 Mhz: constexpr rf::Frequency low_band_second_lo_frequency(const rf::Frequency target_frequency) { - return 2650000000 - (target_frequency / 7); -} - -constexpr rf::Frequency high_band_second_lo_regions_2_and_3(const rf::Frequency target_frequency) { - return (target_frequency < 5100000000) - ? (2350000000 + ((target_frequency - 3600000000) / 5)) - : (2500000000 + ((target_frequency - 5100000000) / 9)); -} - -constexpr rf::Frequency high_band_second_lo_frequency(const rf::Frequency target_frequency) { - return (target_frequency < 3600000000) - ? (2170000000 + (((target_frequency - 2740000000) * 57) / 86)) - : high_band_second_lo_regions_2_and_3(target_frequency); + return 2650'000'000 - (target_frequency / 7); } Config low_band(const rf::Frequency target_frequency) { - const rf::Frequency first_lo_frequency = target_frequency + low_band_second_lo_frequency(target_frequency); - const rf::Frequency second_lo_frequency = first_lo_frequency - target_frequency; + const rf::Frequency second_lo_frequency = low_band_second_lo_frequency(target_frequency); + const rf::Frequency first_lo_frequency = target_frequency + second_lo_frequency; const bool mixer_invert = true; return {first_lo_frequency, second_lo_frequency, rf::path::Band::Low, mixer_invert}; } +// Mid band 2170-2740 Mhz: Config mid_band(const rf::Frequency target_frequency) { - return {0, target_frequency, rf::path::Band::Mid, false}; + const rf::Frequency second_lo_frequency = target_frequency; + const rf::Frequency first_lo_frequency = 0; + const bool mixer_invert = false; + return {first_lo_frequency, second_lo_frequency, rf::path::Band::Mid, mixer_invert}; +} + +// High band >2740 Mhz: +constexpr rf::Frequency high_band_second_lo_frequency(const rf::Frequency target_frequency) { + if (target_frequency < 3600'000'000) + return (2170'000'000 + (((target_frequency - 2740'000'000) * 57) / 86)); + else if (target_frequency < 5100'000'000) + return (2350'000'000 + ((target_frequency - 3600'000'000) / 5)); + else + return (2500'000'000 + ((target_frequency - 5100'000'000) / 9)); } Config high_band(const rf::Frequency target_frequency) { - const rf::Frequency first_lo_frequency = target_frequency - high_band_second_lo_frequency(target_frequency); - const rf::Frequency second_lo_frequency = target_frequency - first_lo_frequency; + const rf::Frequency second_lo_frequency = high_band_second_lo_frequency(target_frequency); + const rf::Frequency first_lo_frequency = target_frequency - second_lo_frequency; const bool mixer_invert = false; return {first_lo_frequency, second_lo_frequency, rf::path::Band::High, mixer_invert}; } -} /* namespace */ - Config create(const rf::Frequency target_frequency) { /* TODO: This is some lame code. */ if (rf::path::band_low.contains(target_frequency)) {