ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
tick_overflow_guard.cpp
Go to the documentation of this file.
2#ifdef USE_ESP32_PM_TICK_OVERFLOW_GUARD
3
4#include <freertos/FreeRTOS.h>
5#include <freertos/task.h>
6
8
9// IDF drops tickless idle on chips without light sleep support (ESP32-H4 and -H21 on 5.5), and then
10// vApplicationSleep() does not exist
11#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
12
13// Backport of IDF 6.0.1 CONFIG_PM_LIGHTSLEEP_TICK_OVERFLOW_PROTECTION: clamp a small tick overshoot
14// after light sleep so vTaskStepTick() does not assert.
15// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming,cppcoreguidelines-avoid-non-const-global-variables)
16extern "C" {
17
18void __real_vApplicationSleep(TickType_t expected_idle_time);
19void __real_vTaskStepTick(TickType_t ticks_to_jump);
20
21// Same default as IDF's CONFIG_PM_LIGHTSLEEP_TICK_OVERFLOW_TOLERANCE; a larger overshoot still asserts
22static constexpr TickType_t TICK_OVERFLOW_TOLERANCE = 2;
23static TickType_t expected_idle_time[portNUM_PROCESSORS];
24
25void __wrap_vApplicationSleep(TickType_t expected) {
26 expected_idle_time[xPortGetCoreID()] = expected;
28}
29
30// Only pm_impl.c calls vTaskStepTick(), from inside vApplicationSleep() on the same core
31void __wrap_vTaskStepTick(TickType_t ticks) {
32 const TickType_t expected = expected_idle_time[xPortGetCoreID()];
33 if (ticks > expected && ticks <= expected + TICK_OVERFLOW_TOLERANCE)
34 ticks = expected;
36}
37
38} // extern "C"
39// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming,cppcoreguidelines-avoid-non-const-global-variables)
40
41#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
42#endif // USE_ESP32_PM_TICK_OVERFLOW_GUARD
void __real_vApplicationSleep(TickType_t expected_idle_time)
void __real_vTaskStepTick(TickType_t ticks_to_jump)
void __wrap_vTaskStepTick(TickType_t ticks)
void __wrap_vApplicationSleep(TickType_t expected)