Prioritize ESP32 Auto CPU speed during dual-profile radio scanning
This commit is contained in:
1 parent
663312c820
commit
1412ee4954
16 files changed
+399
-28
No files matched your search
@@ -170,7 +170,12 @@ public:
|
||||
// preference layout and board vtable. Zero selects the role's Auto policy.
|
||||
virtual uint16_t getCpuFrequencyMHz() const { return 0; }
|
||||
virtual bool supportsCpuFrequencyMHz(uint16_t) const { return false; }
|
||||
virtual void setCpuFrequencyRequest(uint16_t, bool) { }
|
||||
virtual bool applyCpuFrequencyMHz(uint16_t, bool) { return false; }
|
||||
virtual bool prepareRadioProfileCpuClock(bool, bool& changed) {
|
||||
changed = false;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
virtual void reboot() = 0;
|
||||
MESH_REGULATOR_VIRTUAL uint8_t mcuRegulatorModes() const { return 0; }
|
||||
|
||||
@@ -1070,6 +1070,12 @@ void CommonCLI::loadPrefs(FILESYSTEM* fs) {
|
||||
if (loaded) syncOtaConfigFromPrefs(); // persisted OTA policy/keys -> OtaContext (else keep safe defaults)
|
||||
#endif
|
||||
mesh::hilStartupTrace("prefs_apply_begin");
|
||||
#if defined(ESP32_PLATFORM)
|
||||
if (!mesh::esp32::cpuclock::validSelection(_prefs->cpu_freq_mhz)
|
||||
|| (_prefs->cpu_freq_mhz && !_board->supportsCpuFrequencyMHz(_prefs->cpu_freq_mhz)))
|
||||
_prefs->cpu_freq_mhz = 0;
|
||||
_board->setCpuFrequencyRequest(_prefs->cpu_freq_mhz, _prefs->powersaving_enabled != 0);
|
||||
#endif
|
||||
#if defined(NRF52_POWER_MANAGEMENT)
|
||||
// The startup voltage policy owns the relative calibration on these boards.
|
||||
// Its checked record must not be replaced by the legacy infrastructure field.
|
||||
|
||||
@@ -54,6 +54,13 @@ protected:
|
||||
bool ota_started_ap = false;
|
||||
bool ota_started_radio = false;
|
||||
bool companion_cpu_clock_policy = false;
|
||||
bool radio_profile_cpu_clock_policy = false;
|
||||
bool applied_profile_cpu_clock_policy = false;
|
||||
bool applied_cpu_clock_valid = false;
|
||||
uint16_t requested_cpu_clock_selection = 0;
|
||||
bool requested_cpu_clock_powersaving = false;
|
||||
uint16_t sampled_profile_cpu_mhz = 0;
|
||||
uint32_t profile_cpu_clock_retry_at = 0;
|
||||
uint32_t uart_quiet_start_at = 0;
|
||||
uint32_t uart_quiet_restore_at = 0;
|
||||
uint16_t uart_quiet_seconds = 0;
|
||||
@@ -121,6 +128,9 @@ public:
|
||||
void attachDynamicPrefs(KeyValueStore* prefs) { (void)prefs; } // no-op
|
||||
|
||||
void setCompanionCpuClockPolicy(bool enabled) { companion_cpu_clock_policy = enabled; }
|
||||
void setRadioProfileCpuClockPolicy(bool enabled) {
|
||||
radio_profile_cpu_clock_policy = mesh::esp32::cpuclock::profilePerformanceEnabled(enabled);
|
||||
}
|
||||
uint16_t getCpuFrequencyMHz() const override { return ::getCpuFrequencyMhz(); }
|
||||
bool supportsCpuFrequencyMHz(uint16_t mhz) const override {
|
||||
#if CONFIG_IDF_TARGET_ESP32H2
|
||||
@@ -145,15 +155,40 @@ public:
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t resolveCpuFrequencyMHz(uint16_t selected, bool powersaving) const {
|
||||
uint16_t maximum = mesh::esp32::cpuclock::resolveMHz(
|
||||
selected, powersaving, companion_cpu_clock_policy);
|
||||
if (!selected && radio_profile_cpu_clock_policy) {
|
||||
const uint16_t candidates[] = {240, 160, 80};
|
||||
for (uint16_t candidate : candidates) {
|
||||
if (supportsCpuFrequencyMHz(candidate)) return candidate;
|
||||
}
|
||||
}
|
||||
if (!selected && maximum == 240 && !supportsCpuFrequencyMHz(240)
|
||||
&& supportsCpuFrequencyMHz(160)) maximum = 160;
|
||||
return maximum;
|
||||
}
|
||||
|
||||
// Seed the loaded choice before saved profiles can retune the radio. Keep
|
||||
// this request even when the SDK cannot apply it on the first attempt.
|
||||
void setCpuFrequencyRequest(uint16_t selected, bool powersaving) override {
|
||||
if (requested_cpu_clock_selection != selected || requested_cpu_clock_powersaving != powersaving) {
|
||||
requested_cpu_clock_selection = selected;
|
||||
requested_cpu_clock_powersaving = powersaving;
|
||||
applied_cpu_clock_valid = false;
|
||||
profile_cpu_clock_retry_at = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool applyCpuFrequencyMHz(uint16_t selected, bool powersaving) override {
|
||||
if (!mesh::esp32::cpuclock::validSelection(selected)
|
||||
|| (selected && !supportsCpuFrequencyMHz(selected))) return false;
|
||||
uint16_t maximum = mesh::esp32::cpuclock::resolveMHz(
|
||||
selected, powersaving, companion_cpu_clock_policy);
|
||||
if (!selected && maximum == 240 && !supportsCpuFrequencyMHz(240)
|
||||
&& supportsCpuFrequencyMHz(160)) maximum = 160;
|
||||
setCpuFrequencyRequest(selected, powersaving);
|
||||
const uint16_t maximum = resolveCpuFrequencyMHz(selected, powersaving);
|
||||
applied_cpu_clock_valid = false;
|
||||
#if defined(CONFIG_PM_ENABLE) && CONFIG_PM_ENABLE
|
||||
const uint16_t minimum = !selected && companion_cpu_clock_policy && powersaving && maximum > 40
|
||||
const uint16_t minimum = !selected && !radio_profile_cpu_clock_policy
|
||||
&& companion_cpu_clock_policy && powersaving && maximum > 40
|
||||
? 40 : maximum;
|
||||
#if CONFIG_IDF_TARGET_ESP32C3
|
||||
esp_pm_config_esp32c3_t config;
|
||||
@@ -175,7 +210,42 @@ public:
|
||||
#else
|
||||
if (!setCpuFrequencyMhz(maximum)) return false;
|
||||
#endif
|
||||
return !selected || getCpuFrequencyMHz() == selected;
|
||||
if ((selected || radio_profile_cpu_clock_policy) && getCpuFrequencyMHz() != maximum) return false;
|
||||
applied_profile_cpu_clock_policy = radio_profile_cpu_clock_policy;
|
||||
applied_cpu_clock_valid = true;
|
||||
profile_cpu_clock_retry_at = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool prepareRadioProfileCpuClock(bool enabled, bool& changed) override {
|
||||
changed = false;
|
||||
setRadioProfileCpuClockPolicy(enabled);
|
||||
uint16_t previous = 0;
|
||||
const bool fixed_clock = requested_cpu_clock_selection || radio_profile_cpu_clock_policy;
|
||||
const bool clock_drifted = fixed_clock && getCpuFrequencyMHz()
|
||||
!= resolveCpuFrequencyMHz(requested_cpu_clock_selection, requested_cpu_clock_powersaving);
|
||||
if (!applied_cpu_clock_valid || applied_profile_cpu_clock_policy != radio_profile_cpu_clock_policy
|
||||
|| clock_drifted) {
|
||||
const uint32_t now = millis();
|
||||
if (profile_cpu_clock_retry_at && static_cast<int32_t>(now - profile_cpu_clock_retry_at) < 0)
|
||||
return false;
|
||||
previous = getCpuFrequencyMHz();
|
||||
if (!applyCpuFrequencyMHz(requested_cpu_clock_selection, requested_cpu_clock_powersaving)) {
|
||||
profile_cpu_clock_retry_at = now + 5000;
|
||||
if (!profile_cpu_clock_retry_at) profile_cpu_clock_retry_at = 1;
|
||||
return false;
|
||||
}
|
||||
profile_cpu_clock_retry_at = 0;
|
||||
}
|
||||
// Observe explicit clock changes too. Reset only the pair's timing proof,
|
||||
// once per effective transition; hardware failure counters stay intact.
|
||||
if (radio_profile_cpu_clock_policy) {
|
||||
const uint16_t current = getCpuFrequencyMHz();
|
||||
changed = (previous && previous != current)
|
||||
|| (sampled_profile_cpu_mhz && sampled_profile_cpu_mhz != current);
|
||||
sampled_profile_cpu_mhz = current;
|
||||
} else sampled_profile_cpu_mhz = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handleUartQuiet(mesh::esp32::UartQuietAction action, uint16_t seconds,
|
||||
|
||||
@@ -34,6 +34,15 @@ inline uint16_t resolveMHz(uint16_t selected, bool powersaving, bool companion)
|
||||
return companion && powersaving && nominal > 80 ? 80 : nominal;
|
||||
}
|
||||
|
||||
inline bool profilePerformanceEnabled(bool profiles_enabled) {
|
||||
#if defined(SENSECAP_INDICATOR_LORA)
|
||||
(void)profiles_enabled;
|
||||
return false;
|
||||
#else
|
||||
return profiles_enabled;
|
||||
#endif
|
||||
}
|
||||
|
||||
static constexpr size_t FooterSize = 8;
|
||||
|
||||
inline uint16_t footerChecksum(const uint8_t* bytes, size_t length) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "CpuClockPolicy.h"
|
||||
|
||||
namespace mesh {
|
||||
namespace esp32 {
|
||||
@@ -72,14 +73,18 @@ inline uint32_t profileSleepSettleMillis(uint32_t normal) {
|
||||
// choice at boot and after preference changes without reconfiguring each loop.
|
||||
template <typename Board>
|
||||
bool serviceCpuFrequencyPolicy(Board& board, uint16_t selected, bool powersaving,
|
||||
uint32_t now_ms, bool force = false) {
|
||||
uint32_t now_ms, bool force = false, bool profiles_enabled = false) {
|
||||
struct State {
|
||||
uint16_t selected = UINT16_MAX;
|
||||
bool powersaving = false;
|
||||
bool profiles_enabled = false;
|
||||
uint32_t retry_at = 0;
|
||||
};
|
||||
static State state;
|
||||
if (!force && state.selected == selected && state.powersaving == powersaving) return true;
|
||||
profiles_enabled = cpuclock::profilePerformanceEnabled(profiles_enabled);
|
||||
board.setRadioProfileCpuClockPolicy(profiles_enabled);
|
||||
if (!force && state.selected == selected && state.powersaving == powersaving
|
||||
&& state.profiles_enabled == profiles_enabled) return true;
|
||||
if (!force && state.retry_at && static_cast<int32_t>(now_ms - state.retry_at) < 0) return false;
|
||||
if (!board.applyCpuFrequencyMHz(selected, powersaving)) {
|
||||
state.retry_at = now_ms + 5000;
|
||||
@@ -88,6 +93,7 @@ bool serviceCpuFrequencyPolicy(Board& board, uint16_t selected, bool powersaving
|
||||
}
|
||||
state.selected = selected;
|
||||
state.powersaving = powersaving;
|
||||
state.profiles_enabled = profiles_enabled;
|
||||
state.retry_at = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -626,6 +626,12 @@ mesh::RadioParamApplyResult RadioLibWrapper::tuneProfile(uint8_t profile) {
|
||||
#endif
|
||||
if (_cw_active) return mesh::RadioParamApplyResult::BUSY;
|
||||
if (profile > 1 || (profile == 1 && !_profiles.enabled())) return mesh::RadioParamApplyResult::FAILED;
|
||||
#if defined(ESP32_PLATFORM) && !defined(SENSECAP_INDICATOR_LORA)
|
||||
bool clock_changed = false;
|
||||
if (!_board->prepareRadioProfileCpuClock(_profiles.enabled(), clock_changed))
|
||||
return mesh::RadioParamApplyResult::BUSY;
|
||||
if (clock_changed) _profiles.resetSwitchTest();
|
||||
#endif
|
||||
const auto& p = _profiles.params(profile);
|
||||
const uint16_t preamble = profilePreamble(profile);
|
||||
if (!_profile_refresh_required && !_profile_repair_required && _active_profile == profile && _profile_generation == _profiles.generation[profile]
|
||||
|
||||
Reference in new issue
Block a user