Skip to content

Commit 6cb1699

Browse files
authored
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
1 parent e5f4514 commit 6cb1699

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

Diff for: cores/esp8266/Esp.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@ uint8_t EspClass::getBootMode(void)
264264
return system_get_boot_mode();
265265
}
266266

267+
#ifndef F_CPU
267268
uint8_t EspClass::getCpuFreqMHz(void)
268269
{
269270
return system_get_cpu_freq();
270271
}
271-
272+
#endif
272273

273274
uint32_t EspClass::getFlashChipId(void)
274275
{

Diff for: cores/esp8266/Esp.h

+8
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ class EspClass {
157157
uint8_t getBootVersion();
158158
uint8_t getBootMode();
159159

160+
#if defined(F_CPU) || defined(CORE_MOCK)
161+
constexpr uint8_t getCpuFreqMHz() const
162+
{
163+
return clockCyclesPerMicrosecond();
164+
}
165+
#else
160166
uint8_t getCpuFreqMHz();
167+
#endif
161168

162169
uint32_t getFlashChipId();
163170
uint8_t getFlashChipVendorId();
@@ -201,6 +208,7 @@ class EspClass {
201208
};
202209

203210
#ifndef CORE_MOCK
211+
204212
uint32_t EspClass::getCycleCount()
205213
{
206214
return esp_get_cycle_count();

Diff for: cores/esp8266/PolledTimeout.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct TimeSourceCycles
7676

7777
using timeType = decltype(ESP.getCycleCount());
7878
static timeType time() {return ESP.getCycleCount();}
79-
static constexpr timeType ticksPerSecond = F_CPU; // 80'000'000 or 160'000'000 Hz
79+
static constexpr timeType ticksPerSecond = ESP.getCpuFreqMHz() * 1000000UL; // 80'000'000 or 160'000'000 Hz
8080
static constexpr timeType ticksPerSecondMax = 160000000; // 160MHz
8181
};
8282

Diff for: cores/esp8266/core_esp8266_main.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,25 @@ void initVariant() __attribute__((weak));
8181
void initVariant() {
8282
}
8383

84-
void preloop_update_frequency() __attribute__((weak));
85-
void preloop_update_frequency() {
84+
extern "C" void __preloop_update_frequency() {
8685
#if defined(F_CPU) && (F_CPU == 160000000L)
87-
REG_SET_BIT(0x3ff00014, BIT(0));
8886
ets_update_cpu_frequency(160);
87+
CPU2X |= 1UL;
88+
#elif defined(F_CPU)
89+
ets_update_cpu_frequency(80);
90+
CPU2X &= ~1UL;
91+
#elif !defined(F_CPU)
92+
if (system_get_cpu_freq() == 160) {
93+
CPU2X |= 1UL;
94+
}
95+
else {
96+
CPU2X &= ~1UL;
97+
}
8998
#endif
9099
}
91100

101+
extern "C" void preloop_update_frequency() __attribute__((weak, alias("__preloop_update_frequency")));
102+
92103
extern "C" bool can_yield() {
93104
return cont_can_yield(g_pcont);
94105
}

Diff for: tests/host/common/MockEsp.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ uint32_t EspClass::getFreeSketchSpace()
118118
return 4 * 1024 * 1024;
119119
}
120120

121-
uint8_t EspClass::getCpuFreqMHz()
122-
{
123-
return F_CPU / 1000000;
124-
}
125-
126121
const char *EspClass::getSdkVersion()
127122
{
128123
return "2.5.0";

0 commit comments

Comments
 (0)