diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index 21ffe880e..db29ec599 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -264,11 +264,12 @@ uint8_t EspClass::getBootMode(void) return system_get_boot_mode(); } +#ifndef F_CPU uint8_t EspClass::getCpuFreqMHz(void) { return system_get_cpu_freq(); } - +#endif uint32_t EspClass::getFlashChipId(void) { diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index f4529c839..01327747e 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -157,7 +157,14 @@ class EspClass { uint8_t getBootVersion(); uint8_t getBootMode(); +#if defined(F_CPU) || defined(CORE_MOCK) + constexpr uint8_t getCpuFreqMHz() const + { + return clockCyclesPerMicrosecond(); + } +#else uint8_t getCpuFreqMHz(); +#endif uint32_t getFlashChipId(); uint8_t getFlashChipVendorId(); @@ -201,6 +208,7 @@ class EspClass { }; #ifndef CORE_MOCK + uint32_t EspClass::getCycleCount() { return esp_get_cycle_count(); diff --git a/cores/esp8266/PolledTimeout.h b/cores/esp8266/PolledTimeout.h index fe9c9ca4b..715564a8e 100644 --- a/cores/esp8266/PolledTimeout.h +++ b/cores/esp8266/PolledTimeout.h @@ -76,7 +76,7 @@ struct TimeSourceCycles using timeType = decltype(ESP.getCycleCount()); static timeType time() {return ESP.getCycleCount();} - static constexpr timeType ticksPerSecond = F_CPU; // 80'000'000 or 160'000'000 Hz + static constexpr timeType ticksPerSecond = ESP.getCpuFreqMHz() * 1000000UL; // 80'000'000 or 160'000'000 Hz static constexpr timeType ticksPerSecondMax = 160000000; // 160MHz }; diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index a95c4b2c1..0ec4f1f3b 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -81,14 +81,25 @@ void initVariant() __attribute__((weak)); void initVariant() { } -void preloop_update_frequency() __attribute__((weak)); -void preloop_update_frequency() { +extern "C" void __preloop_update_frequency() { #if defined(F_CPU) && (F_CPU == 160000000L) - REG_SET_BIT(0x3ff00014, BIT(0)); ets_update_cpu_frequency(160); + CPU2X |= 1UL; +#elif defined(F_CPU) + ets_update_cpu_frequency(80); + CPU2X &= ~1UL; +#elif !defined(F_CPU) + if (system_get_cpu_freq() == 160) { + CPU2X |= 1UL; + } + else { + CPU2X &= ~1UL; + } #endif } +extern "C" void preloop_update_frequency() __attribute__((weak, alias("__preloop_update_frequency"))); + extern "C" bool can_yield() { return cont_can_yield(g_pcont); } diff --git a/tests/host/common/MockEsp.cpp b/tests/host/common/MockEsp.cpp index c5ca502ed..0854bdcd7 100644 --- a/tests/host/common/MockEsp.cpp +++ b/tests/host/common/MockEsp.cpp @@ -118,11 +118,6 @@ uint32_t EspClass::getFreeSketchSpace() return 4 * 1024 * 1024; } -uint8_t EspClass::getCpuFreqMHz() -{ - return F_CPU / 1000000; -} - const char *EspClass::getSdkVersion() { return "2.5.0";