diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index 4a4a5d69e4..68a79e5ef8 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -21,7 +21,6 @@ #include "Schedule.h" #include "PolledTimeout.h" #include "interrupts.h" -#include "coredecls.h" typedef std::function mSchedFuncT; struct scheduled_fn_t @@ -50,6 +49,8 @@ static recurrent_fn_t* rLast = nullptr; // The target time for scheduling the next timed recurrent function static decltype(micros()) rTarget; +constexpr decltype(micros()) HALF_MAX_MICROS = ~static_cast(0) >> 1; + // Returns a pointer to an unused sched_fn_t, // or if none are available allocates a new one, // or nullptr if limit is reached @@ -106,7 +107,7 @@ bool schedule_function(const std::function& fn) IRAM_ATTR // (not only) called from ISR bool schedule_recurrent_function_us(const std::function& fn, - uint32_t repeat_us, const std::function& alarm) + decltype(micros()) repeat_us, const std::function& alarm) { assert(repeat_us < decltype(recurrent_fn_t::callNow)::neverExpires); //~26800000us (26.8s) @@ -126,7 +127,7 @@ bool schedule_recurrent_function_us(const std::function& fn, const auto now = micros(); const auto itemRemaining = item->callNow.remaining(); const int32_t remaining = rTarget - now; - if (!rFirst || (remaining > 0 && static_cast(remaining) > itemRemaining)) + if (!rFirst || (remaining > 0 && static_cast(remaining) > itemRemaining)) { rTarget = now + itemRemaining; } @@ -144,17 +145,17 @@ bool schedule_recurrent_function_us(const std::function& fn, return true; } -uint32_t get_scheduled_recurrent_delay_us() +decltype(micros()) get_scheduled_recurrent_delay_us() { - if (!rFirst) return ~static_cast(0) >> 1; + if (!rFirst) return HALF_MAX_MICROS; // handle already expired rTarget. const int32_t remaining = rTarget - micros(); - return (remaining > 0) ? static_cast(remaining) : 0; + return (remaining > 0) ? static_cast(remaining) : 0; } -uint32_t get_scheduled_delay_us() +decltype(micros()) get_scheduled_delay_us() { - return sFirst ? 0 : ~static_cast(0) >> 1; + return sFirst ? 0 : HALF_MAX_MICROS; } void run_scheduled_functions() @@ -223,7 +224,7 @@ void run_scheduled_recurrent_functions() // prevent scheduling of new functions during this run stop = rLast; - rTarget = micros() + (~static_cast(0) >> 1); + rTarget = micros() + HALF_MAX_MICROS; do { @@ -262,7 +263,7 @@ void run_scheduled_recurrent_functions() const auto now = micros(); const auto currentRemaining = current->callNow.remaining(); const int32_t remaining = rTarget - now; - if (remaining > 0 && static_cast(remaining) > currentRemaining) + if (remaining > 0 && static_cast(remaining) > currentRemaining) { rTarget = now + currentRemaining; } diff --git a/cores/esp8266/Schedule.h b/cores/esp8266/Schedule.h index 56333d08e2..730e77d513 100644 --- a/cores/esp8266/Schedule.h +++ b/cores/esp8266/Schedule.h @@ -22,6 +22,8 @@ #include #include +#include "coredecls.h" + #define SCHEDULED_FN_MAX_COUNT 32 // The purpose of scheduled functions is to trigger, from SYS stack (like in @@ -42,7 +44,7 @@ // get_scheduled_recurrent_delay_us() is used by delay() to give a chance to // all recurrent functions to run per their timing requirement. -uint32_t get_scheduled_recurrent_delay_us(); +decltype(micros()) get_scheduled_recurrent_delay_us(); // scheduled functions called once: // @@ -65,7 +67,7 @@ uint32_t get_scheduled_recurrent_delay_us(); // values, viz. 0 in case of any pending scheduled functions, or a large delay time if // there is no function in the queue. -uint32_t get_scheduled_delay_us(); +decltype(micros()) get_scheduled_delay_us(); bool schedule_function (const std::function& fn); @@ -93,7 +95,7 @@ void run_scheduled_functions(); // any remaining delay from repeat_us is disregarded, and fn is executed. bool schedule_recurrent_function_us(const std::function& fn, - uint32_t repeat_us, const std::function& alarm = nullptr); + decltype(micros()) repeat_us, const std::function& alarm = nullptr); // Test recurrence and run recurrent scheduled functions. // (internally called at every `yield()` and `loop()`) diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index 67f12e25ae..a0cfb8533c 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -174,7 +174,8 @@ bool esp_try_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uin } // compute greatest delay interval with respect to scheduled recurrent functions - const uint32_t max_delay_ms = std::min(intvl_ms, get_scheduled_recurrent_delay_us() / 1000); + const uint32_t scheduled_recurrent_delay_ms = get_scheduled_recurrent_delay_us() / 1000UL; + const uint32_t max_delay_ms = std::min(intvl_ms, scheduled_recurrent_delay_ms); // recurrent scheduled functions will be called from esp_delay()->esp_suspend() esp_delay(std::min((timeout_ms - expired), max_delay_ms)); diff --git a/libraries/SoftwareSerial b/libraries/SoftwareSerial index 73fe38b788..a5b2bd86b0 160000 --- a/libraries/SoftwareSerial +++ b/libraries/SoftwareSerial @@ -1 +1 @@ -Subproject commit 73fe38b788a2ffc60ae44e5912ece3ee8ff3cc50 +Subproject commit a5b2bd86b0b471f7a31dae4d257e629cafa2eeca