Skip to content

Commit 03713a7

Browse files
committed
Fix millis/micros mismatch.
1 parent 3b315d8 commit 03713a7

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

cores/esp8266/Schedule.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
120120
item->alarm = alarm;
121121

122122
// prevent new item overwriting an already expired rTarget.
123-
const int32_t rRemaining = rTarget - millis();
123+
const int32_t rRemaining = rTarget - micros();
124124
if (!rFirst || (rRemaining > 0 && static_cast<uint32_t>(rRemaining) > item->callNow.remaining()))
125125
{
126-
rTarget = millis() + item->callNow.remaining();
126+
rTarget = micros() + item->callNow.remaining();
127127
}
128128

129129
esp8266::InterruptLock lockAllInterruptsInThisScope;
@@ -141,11 +141,11 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
141141
return true;
142142
}
143143

144-
uint32_t get_scheduled_recurrent_delay()
144+
uint32_t get_scheduled_recurrent_delay_us()
145145
{
146146
if (!rFirst) return ~static_cast<uint32_t>(0);
147147
// handle already expired rTarget.
148-
const int32_t rRemaining = rTarget - millis();
148+
const int32_t rRemaining = rTarget - micros();
149149
return (rRemaining > 0) ? static_cast<uint32_t>(rRemaining) : 0;
150150
}
151151

@@ -209,7 +209,7 @@ void run_scheduled_recurrent_functions()
209209
fence = true;
210210
}
211211

212-
rTarget = millis() + current->callNow.remaining();
212+
rTarget = micros() + current->callNow.remaining();
213213
recurrent_fn_t* prev = nullptr;
214214
// prevent scheduling of new functions during this run
215215
auto stop = rLast;
@@ -249,10 +249,10 @@ void run_scheduled_recurrent_functions()
249249
prev = current;
250250
current = current->mNext;
251251
// prevent current item overwriting an already expired rTarget.
252-
const int32_t rRemaining = rTarget - millis();
252+
const int32_t rRemaining = rTarget - micros();
253253
if (rRemaining > 0 && static_cast<uint32_t>(rRemaining) > current->callNow.remaining())
254254
{
255-
rTarget = millis() + current->callNow.remaining();
255+
rTarget = micros() + current->callNow.remaining();
256256
}
257257
}
258258

cores/esp8266/Schedule.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
// scheduled function happen more often: every yield() (vs every loop()),
4040
// and time resolution is microsecond (vs millisecond). Details are below.
4141

42-
// get_scheduled_recurrent_delay() is used by delay() to give a chance to
42+
// get_scheduled_recurrent_delay_us() is used by delay() to give a chance to
4343
// all recurrent functions to run per their timing requirement.
4444

45-
uint32_t get_scheduled_recurrent_delay();
45+
uint32_t get_scheduled_recurrent_delay_us();
4646

4747
// scheduled functions called once:
4848
//

cores/esp8266/core_esp8266_main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ bool esp_try_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uin
173173
}
174174

175175
// compute greatest delay interval with respect to scheduled recurrent functions
176-
const uint32_t max_delay_ms = std::min(intvl_ms, get_scheduled_recurrent_delay());
176+
const uint32_t max_delay_ms = std::min(intvl_ms, get_scheduled_recurrent_delay_us() / 1000);
177177

178178
// recurrent scheduled functions will be called from esp_delay()->esp_suspend()
179179
esp_delay(std::min((timeout_ms - expired), max_delay_ms));

0 commit comments

Comments
 (0)