Inline main_task notify helpers in header, simplify wake.h

This commit is contained in:
J. Nick Koston
2026-04-04 11:40:55 -10:00
parent 6dcdefe179
commit 1dc0559e20
4 changed files with 23 additions and 46 deletions
-34
View File
@@ -1,39 +1,5 @@
#include "esphome/core/main_task.h"
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
// IRAM_ATTR is defined by esp_attr.h (included via FreeRTOS headers) on ESP32.
// On LibreTiny it's not reliably available — provide a no-op fallback.
#ifndef IRAM_ATTR
#define IRAM_ATTR
#endif
TaskHandle_t esphome_main_task_handle = NULL;
void esphome_main_task_notify(void) {
TaskHandle_t task = esphome_main_task_handle;
if (task != NULL) {
xTaskNotifyGive(task);
}
}
void IRAM_ATTR esphome_main_task_notify_from_isr(int *px_higher_priority_task_woken) {
TaskHandle_t task = esphome_main_task_handle;
if (task != NULL) {
vTaskNotifyGiveFromISR(task, (BaseType_t *) px_higher_priority_task_woken);
}
}
#ifdef USE_ESP32
void IRAM_ATTR esphome_main_task_notify_any_context(void) {
if (xPortInIsrContext()) {
int px_higher_priority_task_woken = 0;
esphome_main_task_notify_from_isr(&px_higher_priority_task_woken);
portYIELD_FROM_ISR(px_higher_priority_task_woken);
} else {
esphome_main_task_notify();
}
}
#endif
#endif // USE_ESP32 || USE_LIBRETINY
+21 -3
View File
@@ -20,14 +20,32 @@ extern "C" {
extern TaskHandle_t esphome_main_task_handle;
/// Wake the main loop task from another FreeRTOS task. NOT ISR-safe.
void esphome_main_task_notify(void);
static inline void esphome_main_task_notify(void) {
TaskHandle_t task = esphome_main_task_handle;
if (task != NULL) {
xTaskNotifyGive(task);
}
}
/// Wake the main loop task from an ISR. ISR-safe.
void esphome_main_task_notify_from_isr(int *px_higher_priority_task_woken);
static inline void esphome_main_task_notify_from_isr(int *px_higher_priority_task_woken) {
TaskHandle_t task = esphome_main_task_handle;
if (task != NULL) {
vTaskNotifyGiveFromISR(task, (BaseType_t *) px_higher_priority_task_woken);
}
}
#ifdef USE_ESP32
/// Wake the main loop from any context (ISR or task). ESP32-only (needs xPortInIsrContext).
void esphome_main_task_notify_any_context(void);
static inline void esphome_main_task_notify_any_context(void) {
if (xPortInIsrContext()) {
int px_higher_priority_task_woken = 0;
esphome_main_task_notify_from_isr(&px_higher_priority_task_woken);
portYIELD_FROM_ISR(px_higher_priority_task_woken);
} else {
esphome_main_task_notify();
}
}
#endif
#ifdef __cplusplus
+2 -2
View File
@@ -15,9 +15,9 @@ namespace esphome {
// === ESP32 — IRAM_ATTR entry points ===
#ifdef USE_ESP32
void IRAM_ATTR wake_loop_isrsafe(int *px_higher_priority_task_woken) {
wake_loop_isrsafe_inline_(px_higher_priority_task_woken);
esphome_main_task_notify_from_isr(px_higher_priority_task_woken);
}
void IRAM_ATTR wake_loop_any_context() { wake_loop_any_context_inline_(); }
void IRAM_ATTR wake_loop_any_context() { esphome_main_task_notify_any_context(); }
#endif
// === ESP8266 / RP2040 ===
-7
View File
@@ -29,15 +29,8 @@ extern volatile bool g_main_loop_woke;
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
#ifdef USE_ESP32
/// Inline impl — ISR callers inline this into IRAM.
inline void ESPHOME_ALWAYS_INLINE wake_loop_isrsafe_inline_(int *px_higher_priority_task_woken) {
esphome_main_task_notify_from_isr(px_higher_priority_task_woken);
}
/// IRAM_ATTR entry point — defined in wake.cpp.
void wake_loop_isrsafe(int *px_higher_priority_task_woken);
/// Inline impl — ISR callers inline this into IRAM.
inline void ESPHOME_ALWAYS_INLINE wake_loop_any_context_inline_() { esphome_main_task_notify_any_context(); }
/// IRAM_ATTR entry point — defined in wake.cpp.
void wake_loop_any_context();
#else