Skip to content

Use F_CPU if (?) CPU frequency switch is compile-time only #6833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 15, 2020
3 changes: 2 additions & 1 deletion cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 8 additions & 0 deletions cores/esp8266/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -201,6 +208,7 @@ class EspClass {
};

#ifndef CORE_MOCK

uint32_t EspClass::getCycleCount()
{
return esp_get_cycle_count();
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/PolledTimeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
17 changes: 14 additions & 3 deletions cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 0 additions & 5 deletions tests/host/common/MockEsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down