diff --git a/firmware/application/receiver_model.cpp b/firmware/application/receiver_model.cpp index 387fa82b0..f54ae86eb 100644 --- a/firmware/application/receiver_model.cpp +++ b/firmware/application/receiver_model.cpp @@ -27,11 +27,11 @@ using namespace portapack; rf::Frequency ReceiverModel::tuning_frequency() const { - return tuning_frequency_; + return persistent_memory::tuned_frequency(); } void ReceiverModel::set_tuning_frequency(rf::Frequency f) { - tuning_frequency_ = f; + persistent_memory::set_tuned_frequency(f); update_tuning_frequency(); } @@ -158,7 +158,7 @@ int32_t ReceiverModel::tuning_offset() { } void ReceiverModel::update_tuning_frequency() { - radio::set_tuning_frequency(tuning_frequency_ + tuning_offset()); + radio::set_tuning_frequency(persistent_memory::tuned_frequency() + tuning_offset()); } void ReceiverModel::update_rf_amp() { diff --git a/firmware/application/receiver_model.hpp b/firmware/application/receiver_model.hpp index 8d6eb3601..a6af119c5 100644 --- a/firmware/application/receiver_model.hpp +++ b/firmware/application/receiver_model.hpp @@ -76,7 +76,6 @@ public: void disable(); private: - rf::Frequency tuning_frequency_ { 858750000 }; rf::Frequency frequency_step_ { 25000 }; bool rf_amp_ { false }; int32_t lna_gain_db_ { 32 }; diff --git a/firmware/common/portapack_persistent_memory.cpp b/firmware/common/portapack_persistent_memory.cpp index bb538d0ef..6c44f0fdc 100644 --- a/firmware/common/portapack_persistent_memory.cpp +++ b/firmware/common/portapack_persistent_memory.cpp @@ -47,12 +47,17 @@ struct range_t { } }; +using tuned_frequency_range_t = range_t; +constexpr tuned_frequency_range_t tuned_frequency_range { rf::tuning_range.min, rf::tuning_range.max }; +constexpr rf::Frequency tuned_frequency_reset_value { 858750000 }; + using ppb_range_t = range_t; constexpr ppb_range_t ppb_range { -99000, 99000 }; constexpr ppb_t ppb_reset_value { 0 }; /* struct must pack the same way on M4 and M0 cores. */ struct data_t { + int64_t tuned_frequency; int32_t correction_ppb; }; @@ -60,6 +65,15 @@ static_assert(sizeof(data_t) <= 0x100, "Persistent memory structure too large fo static data_t* const data = reinterpret_cast(LPC_BACKUP_REG_BASE); +rf::Frequency tuned_frequency() { + tuned_frequency_range.reset_if_outside(data->tuned_frequency, tuned_frequency_reset_value); + return data->tuned_frequency; +} + +void set_tuned_frequency(const rf::Frequency new_value) { + data->tuned_frequency = tuned_frequency_range.clip(new_value); +} + ppb_t correction_ppb() { ppb_range.reset_if_outside(data->correction_ppb, ppb_reset_value); return data->correction_ppb; diff --git a/firmware/common/portapack_persistent_memory.hpp b/firmware/common/portapack_persistent_memory.hpp index 03fd6e6e5..3e1a6c146 100644 --- a/firmware/common/portapack_persistent_memory.hpp +++ b/firmware/common/portapack_persistent_memory.hpp @@ -24,11 +24,16 @@ #include +#include "rf_path.hpp" + namespace portapack { namespace persistent_memory { using ppb_t = int32_t; +rf::Frequency tuned_frequency(); +void set_tuned_frequency(const rf::Frequency new_value); + ppb_t correction_ppb(); void set_correction_ppb(const ppb_t new_value);