Skip to content

Commit e0fedc5

Browse files
authored
avoid circular #include dependence for PolledTimeout (#7356)
* move features to features.h * fix std:: dependencies * fix emulation on host * api explanation
1 parent 01edc0e commit e0fedc5

File tree

6 files changed

+97
-46
lines changed

6 files changed

+97
-46
lines changed

cores/esp8266/Arduino.h

-5
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,6 @@ void analogWriteFreq(uint32_t freq);
177177
void analogWriteResolution(int res);
178178
void analogWriteRange(uint32_t range);
179179

180-
unsigned long millis(void);
181-
unsigned long micros(void);
182-
uint64_t micros64(void);
183-
void delay(unsigned long);
184-
void delayMicroseconds(unsigned int us);
185180
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
186181
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
187182

cores/esp8266/Esp.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,13 @@ class EspClass {
124124
#if defined(F_CPU) || defined(CORE_MOCK)
125125
constexpr uint8_t getCpuFreqMHz() const
126126
{
127-
return clockCyclesPerMicrosecond();
127+
return esp_get_cpu_freq_mhz();
128128
}
129129
#else
130-
uint8_t getCpuFreqMHz();
130+
uint8_t getCpuFreqMHz() const
131+
{
132+
return esp_get_cpu_freq_mhz();
133+
}
131134
#endif
132135

133136
uint32_t getFlashChipId();

cores/esp8266/PolledTimeout.h

+37-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/*
66
PolledTimeout.h - Encapsulation of a polled Timeout
7-
7+
88
Copyright (c) 2018 Daniel Salazar. All rights reserved.
99
This file is part of the esp8266 core for Arduino environment.
1010
@@ -23,9 +23,10 @@
2323
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2424
*/
2525

26-
#include <limits>
27-
28-
#include <Arduino.h>
26+
#include <c_types.h> // IRAM_ATTR
27+
#include <limits> // std::numeric_limits
28+
#include <type_traits> // std::is_unsigned
29+
#include <core_esp8266_features.h>
2930

3031
namespace esp8266
3132
{
@@ -70,13 +71,13 @@ struct TimeSourceMillis
7071

7172
struct TimeSourceCycles
7273
{
73-
// time policy based on ESP.getCycleCount()
74+
// time policy based on esp_get_cycle_count()
7475
// this particular time measurement is intended to be called very often
7576
// (every loop, every yield)
7677

77-
using timeType = decltype(ESP.getCycleCount());
78-
static timeType time() {return ESP.getCycleCount();}
79-
static constexpr timeType ticksPerSecond = ESP.getCpuFreqMHz() * 1000000UL; // 80'000'000 or 160'000'000 Hz
78+
using timeType = decltype(esp_get_cycle_count());
79+
static timeType time() {return esp_get_cycle_count();}
80+
static constexpr timeType ticksPerSecond = esp_get_cpu_freq_mhz() * 1000000UL; // 80'000'000 or 160'000'000 Hz
8081
static constexpr timeType ticksPerSecondMax = 160000000; // 160MHz
8182
};
8283

@@ -161,13 +162,13 @@ class timeoutTemplate
161162
return expiredRetrigger();
162163
return expiredOneShot();
163164
}
164-
165+
165166
IRAM_ATTR // fast
166167
operator bool()
167168
{
168-
return expired();
169+
return expired();
169170
}
170-
171+
171172
bool canExpire () const
172173
{
173174
return !_neverExpires;
@@ -178,6 +179,7 @@ class timeoutTemplate
178179
return _timeout != alwaysExpired;
179180
}
180181

182+
// Resets, will trigger after this new timeout.
181183
IRAM_ATTR // called from ISR
182184
void reset(const timeType newUserTimeout)
183185
{
@@ -186,12 +188,30 @@ class timeoutTemplate
186188
_neverExpires = (newUserTimeout < 0) || (newUserTimeout > timeMax());
187189
}
188190

191+
// Resets, will trigger after the timeout previously set.
189192
IRAM_ATTR // called from ISR
190193
void reset()
191194
{
192195
_start = TimePolicyT::time();
193196
}
194197

198+
// Resets to just expired so that on next poll the check will immediately trigger for the user,
199+
// also change timeout (after next immediate trigger).
200+
IRAM_ATTR // called from ISR
201+
void resetAndSetExpired (const timeType newUserTimeout)
202+
{
203+
reset(newUserTimeout);
204+
_start -= _timeout;
205+
}
206+
207+
// Resets to just expired so that on next poll the check will immediately trigger for the user.
208+
IRAM_ATTR // called from ISR
209+
void resetAndSetExpired ()
210+
{
211+
reset();
212+
_start -= _timeout;
213+
}
214+
195215
void resetToNeverExpires ()
196216
{
197217
_timeout = alwaysExpired + 1; // because canWait() has precedence
@@ -202,7 +222,7 @@ class timeoutTemplate
202222
{
203223
return TimePolicyT::toUserUnit(_timeout);
204224
}
205-
225+
206226
static constexpr timeType timeMax()
207227
{
208228
return TimePolicyT::timeMax;
@@ -235,14 +255,14 @@ class timeoutTemplate
235255
}
236256
return false;
237257
}
238-
258+
239259
IRAM_ATTR // fast
240260
bool expiredOneShot() const
241261
{
242262
// returns "always expired" or "has expired"
243263
return !canWait() || checkExpired(TimePolicyT::time());
244264
}
245-
265+
246266
timeType _timeout;
247267
timeType _start;
248268
bool _neverExpires;
@@ -259,14 +279,14 @@ using periodic = polledTimeout::timeoutTemplate<true> /*__attribute__((deprecate
259279
using oneShotMs = polledTimeout::timeoutTemplate<false>;
260280
using periodicMs = polledTimeout::timeoutTemplate<true>;
261281

262-
// Time policy based on ESP.getCycleCount(), and intended to be called very often:
282+
// Time policy based on esp_get_cycle_count(), and intended to be called very often:
263283
// "Fast" versions sacrifices time range for improved precision and reduced execution time (by 86%)
264-
// (cpu cycles for ::expired(): 372 (millis()) vs 52 (ESP.getCycleCount()))
284+
// (cpu cycles for ::expired(): 372 (millis()) vs 52 (esp_get_cycle_count()))
265285
// timeMax() values:
266286
// Ms: max is 26843 ms (26.8 s)
267287
// Us: max is 26843545 us (26.8 s)
268288
// Ns: max is 1073741823 ns ( 1.07 s)
269-
// (time policy based on ESP.getCycleCount() is intended to be called very often)
289+
// (time policy based on esp_get_cycle_count() is intended to be called very often)
270290

271291
using oneShotFastMs = polledTimeout::timeoutTemplate<false, YieldPolicy::DoNothing, TimePolicy::TimeFastMillis>;
272292
using periodicFastMs = polledTimeout::timeoutTemplate<true, YieldPolicy::DoNothing, TimePolicy::TimeFastMillis>;

cores/esp8266/core_esp8266_features.h

+23
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,29 @@ extern "C" {
123123
#endif
124124

125125
void precache(void *f, uint32_t bytes);
126+
unsigned long millis(void);
127+
unsigned long micros(void);
128+
uint64_t micros64(void);
129+
void delay(unsigned long);
130+
void delayMicroseconds(unsigned int us);
131+
132+
#if defined(F_CPU) || defined(CORE_MOCK)
133+
#ifdef __cplusplus
134+
constexpr
135+
#else
136+
inline
137+
#endif
138+
int esp_get_cpu_freq_mhz()
139+
{
140+
return F_CPU / 1000000L;
141+
}
142+
#else
143+
inline int esp_get_cpu_freq_mhz()
144+
{
145+
return system_get_cpu_freq();
146+
}
147+
#endif
148+
126149

127150
#ifdef __cplusplus
128151
}

tests/host/common/MockEsp.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ uint32_t EspClass::getFlashChipSize(void)
208208

209209
String EspClass::getFullVersion ()
210210
{
211-
return "host-emulation";
211+
return "emulation-on-host";
212212
}
213213

214214
uint32_t EspClass::getFreeContStack()
@@ -221,6 +221,11 @@ void EspClass::resetFreeContStack()
221221
}
222222

223223
uint32_t EspClass::getCycleCount()
224+
{
225+
return esp_get_cycle_count();
226+
}
227+
228+
uint32_t esp_get_cycle_count()
224229
{
225230
timeval t;
226231
gettimeofday(&t, NULL);

tests/host/common/mock.h

+26-21
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@
3131

3232
#define CORE_MOCK 1
3333

34+
//
35+
36+
#define ARDUINO 267
37+
#define ESP8266 1
38+
#define A0 0
39+
#define LED_BUILTIN 0
40+
#define F_CPU 80000000
41+
#define LWIP_OPEN_SRC
42+
#define TCP_MSS 536
43+
#define LWIP_FEATURES 1
44+
45+
//
46+
47+
#define D0 0
48+
#define D1 1
49+
#define D2 3
50+
#define D3 3
51+
#define D4 4
52+
#define D5 5
53+
#define D6 6
54+
#define D7 7
55+
#define D8 8
56+
3457
// include host's STL before any other include file
3558
// because core definition like max() is in the way
3659

@@ -61,28 +84,10 @@ typedef uint32_t uint32;
6184

6285
//
6386

64-
#define ARDUINO 267
65-
#define ESP8266 1
66-
#define A0 0
67-
#define LED_BUILTIN 0
68-
#define F_CPU 80000000
69-
#define LWIP_OPEN_SRC
70-
#define TCP_MSS 536
71-
#define LWIP_FEATURES 1
72-
73-
//
74-
75-
#define D0 0
76-
#define D1 1
77-
#define D2 3
78-
#define D3 3
79-
#define D4 4
80-
#define D5 5
81-
#define D6 6
82-
#define D7 7
83-
#define D8 8
87+
#include <c_types.h>
88+
#include <core_esp8266_features.h>
8489

85-
//
90+
uint32_t esp_get_cycle_count();
8691

8792
#include <Arduino.h>
8893

0 commit comments

Comments
 (0)