Skip to content

Commit 598361f

Browse files
committed
Substitute micros() by CPU cycles - faster, but large values for interval_us can overflow - 26s at 160MHz CPU clock.
1 parent d6e603f commit 598361f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

cores/esp8266/core_esp8266_main.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ cont_t* g_pcont __attribute__((section(".noinit")));
5858
static os_event_t s_loop_queue[LOOP_QUEUE_SIZE];
5959

6060
/* Used to implement optimistic_yield */
61-
static uint32_t s_micros_since_yield_start;
61+
static uint32_t s_cycles_since_yield_start;
6262

6363
/* For ets_intr_lock_nest / ets_intr_unlock_nest
6464
* Max nesting seen by SDK so far is 2.
@@ -116,7 +116,7 @@ extern "C" void __yield() {
116116
if (can_yield()) {
117117
esp_schedule();
118118
esp_yield_within_cont();
119-
s_micros_since_yield_start = system_get_time();
119+
s_cycles_since_yield_start = ESP.getCycleCount();
120120
}
121121
else {
122122
panic();
@@ -126,8 +126,9 @@ extern "C" void __yield() {
126126
extern "C" void yield(void) __attribute__ ((weak, alias("__yield")));
127127

128128
extern "C" void optimistic_yield(uint32_t interval_us) {
129+
const uint32_t interval_cycles = interval_us * ESP.getCpuFreqMHz();
129130
if (can_yield() &&
130-
(system_get_time() - s_micros_since_yield_start) > interval_us)
131+
(ESP.getCycleCount() - s_cycles_since_yield_start) > interval_cycles)
131132
{
132133
yield();
133134
}
@@ -184,7 +185,7 @@ static void loop_wrapper() {
184185

185186
static void loop_task(os_event_t *events) {
186187
(void) events;
187-
s_micros_since_yield_start = system_get_time();
188+
s_cycles_since_yield_start = ESP.getCycleCount();
188189
cont_run(g_pcont, &loop_wrapper);
189190
if (cont_check(g_pcont) != 0) {
190191
panic();
@@ -212,7 +213,7 @@ extern void __unhandled_exception(const char *str);
212213
static void __unhandled_exception_cpp()
213214
{
214215
#ifndef __EXCEPTIONS
215-
abort();
216+
abort();
216217
#else
217218
static bool terminating;
218219
if (terminating)

0 commit comments

Comments
 (0)