From 6cb16997d8cedacc0dc5b1acda9a3e868b7b4395 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" <19971886+dok-net@users.noreply.github.com> Date: Wed, 15 Apr 2020 22:15:35 +0200 Subject: [PATCH] Use F_CPU if (?) CPU frequency switch is compile-time only (#6833) * At least the F_CPU define in host mock.h is needed by host Arduino.h - need to include Arduino.h further down in mock.h for this to work. * Geting the include order right * Prepare for runtime CPU clock rate selection * Fix compile for not defined F_CPU If defined F_CPU, make getCpuFreqMHz() a constexpr * Use defines for register CPU2X instead of hex value * Fix build for host - getCpuFreqMHz there was also in conflict with getCycleCount, using F_CPU: tests/host/common/mock.h:#define F_CPU 80000000 (!) * Asymmetrical includes and defines on host * Support restart switch from 160MHz to 80MHz, e.g for OTA. Fixes #579 --- cores/esp8266/Esp.cpp | 3 ++- cores/esp8266/Esp.h | 8 ++++++++ cores/esp8266/PolledTimeout.h | 2 +- cores/esp8266/core_esp8266_main.cpp | 17 ++++++++++++++--- tests/host/common/MockEsp.cpp | 5 ----- 5 files changed, 25 insertions(+), 10 deletions(-) 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";