From e92e631811002adba98eb1e8e0ce167755e0864f Mon Sep 17 00:00:00 2001 From: Angel Nunez Mencias Date: Wed, 23 Jul 2025 07:11:11 +0200 Subject: [PATCH 1/4] feat: Allow calls to timer functions within ISR --- cores/esp32/esp32-hal-timer.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/cores/esp32/esp32-hal-timer.c b/cores/esp32/esp32-hal-timer.c index ec6c507358e..508d510508e 100644 --- a/cores/esp32/esp32-hal-timer.c +++ b/cores/esp32/esp32-hal-timer.c @@ -36,9 +36,8 @@ struct timer_struct_t { bool timer_started; }; -inline uint64_t timerRead(hw_timer_t *timer) { +inline IRAM_ATTR uint64_t timerRead(hw_timer_t *timer) { if (timer == NULL) { - log_e("Timer handle is NULL"); return 0; } uint64_t value; @@ -46,15 +45,14 @@ inline uint64_t timerRead(hw_timer_t *timer) { return value; } -void timerWrite(hw_timer_t *timer, uint64_t val) { +void IRAM_ATTR timerWrite(hw_timer_t *timer, uint64_t val) { if (timer == NULL) { - log_e("Timer handle is NULL"); return; } gptimer_set_raw_count(timer->timer_handle, val); } -void timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count) { +void IRAM_ATTR timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count) { if (timer == NULL) { log_e("Timer handle is NULL"); return; @@ -67,7 +65,7 @@ void timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64 }; err = gptimer_set_alarm_action(timer->timer_handle, &alarm_cfg); if (err != ESP_OK) { - log_e("Timer Alarm Write failed, error num=%d", err); + ; // Ignore } } @@ -80,27 +78,24 @@ uint32_t timerGetFrequency(hw_timer_t *timer) { return frequency; } -void timerStart(hw_timer_t *timer) { +void IRAM_ATTR timerStart(hw_timer_t *timer) { if (timer == NULL) { - log_e("Timer handle is NULL"); return; } gptimer_start(timer->timer_handle); timer->timer_started = true; } -void timerStop(hw_timer_t *timer) { +void IRAM_ATTR timerStop(hw_timer_t *timer) { if (timer == NULL) { - log_e("Timer handle is NULL"); return; } gptimer_stop(timer->timer_handle); timer->timer_started = false; } -void timerRestart(hw_timer_t *timer) { +void IRAM_ATTR timerRestart(hw_timer_t *timer) { if (timer == NULL) { - log_e("Timer handle is NULL"); return; } gptimer_set_raw_count(timer->timer_handle, 0); From 67c59a202196abe0029b868c34e57faf4032f0c5 Mon Sep 17 00:00:00 2001 From: Angel Nunez Mencias Date: Wed, 23 Jul 2025 07:26:03 +0200 Subject: [PATCH 2/4] fix: remove log from IRAM function --- cores/esp32/esp32-hal-timer.c | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/esp32/esp32-hal-timer.c b/cores/esp32/esp32-hal-timer.c index 508d510508e..6875b5b2d8f 100644 --- a/cores/esp32/esp32-hal-timer.c +++ b/cores/esp32/esp32-hal-timer.c @@ -54,7 +54,6 @@ void IRAM_ATTR timerWrite(hw_timer_t *timer, uint64_t val) { void IRAM_ATTR timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count) { if (timer == NULL) { - log_e("Timer handle is NULL"); return; } esp_err_t err = ESP_OK; From 126039663f83b3d6f5868a30f7dc2093812ee7b4 Mon Sep 17 00:00:00 2001 From: Angel Nunez Mencias Date: Thu, 24 Jul 2025 00:09:17 +0200 Subject: [PATCH 3/4] fix: conditional logs when not ISR --- cores/esp32/esp32-hal-timer.c | 40 +++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/cores/esp32/esp32-hal-timer.c b/cores/esp32/esp32-hal-timer.c index 6875b5b2d8f..38575fc9799 100644 --- a/cores/esp32/esp32-hal-timer.c +++ b/cores/esp32/esp32-hal-timer.c @@ -22,6 +22,12 @@ #include "esp_clk_tree.h" #endif +#if CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM +#define TIMER_IRAM IRAM_ATTR +#else +#define TIMER_IRAM +#endif + typedef void (*voidFuncPtr)(void); typedef void (*voidFuncPtrArg)(void *); @@ -36,8 +42,11 @@ struct timer_struct_t { bool timer_started; }; -inline IRAM_ATTR uint64_t timerRead(hw_timer_t *timer) { +inline TIMER_IRAM uint64_t timerRead(hw_timer_t *timer) { if (timer == NULL) { + #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM + log_e("Timer handle is NULL"); + #endif return 0; } uint64_t value; @@ -45,15 +54,21 @@ inline IRAM_ATTR uint64_t timerRead(hw_timer_t *timer) { return value; } -void IRAM_ATTR timerWrite(hw_timer_t *timer, uint64_t val) { +void TIMER_IRAM timerWrite(hw_timer_t *timer, uint64_t val) { if (timer == NULL) { + #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM + log_e("Timer handle is NULL"); + #endif return; } gptimer_set_raw_count(timer->timer_handle, val); } -void IRAM_ATTR timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count) { +void TIMER_IRAM timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count) { if (timer == NULL) { + #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM + log_e("Timer handle is NULL"); + #endif return; } esp_err_t err = ESP_OK; @@ -64,7 +79,9 @@ void IRAM_ATTR timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autorelo }; err = gptimer_set_alarm_action(timer->timer_handle, &alarm_cfg); if (err != ESP_OK) { - ; // Ignore + #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM + log_e("Timer Alarm Write failed, error num=%d", err); + #endif } } @@ -77,24 +94,33 @@ uint32_t timerGetFrequency(hw_timer_t *timer) { return frequency; } -void IRAM_ATTR timerStart(hw_timer_t *timer) { +void TIMER_IRAM timerStart(hw_timer_t *timer) { if (timer == NULL) { + #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM + log_e("Timer handle is NULL"); + #endif return; } gptimer_start(timer->timer_handle); timer->timer_started = true; } -void IRAM_ATTR timerStop(hw_timer_t *timer) { +void TIMER_IRAM timerStop(hw_timer_t *timer) { if (timer == NULL) { + #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM + log_e("Timer handle is NULL"); + #endif return; } gptimer_stop(timer->timer_handle); timer->timer_started = false; } -void IRAM_ATTR timerRestart(hw_timer_t *timer) { +void TIMER_IRAM timerRestart(hw_timer_t *timer) { if (timer == NULL) { + #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM + log_e("Timer handle is NULL"); + #endif return; } gptimer_set_raw_count(timer->timer_handle, 0); From e3a0a3a6c58eca709dacf6f1d2e7cd169f522e86 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Thu, 24 Jul 2025 11:22:13 +0000 Subject: [PATCH 4/4] ci(pre-commit): Apply automatic fixes --- cores/esp32/esp32-hal-timer.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/cores/esp32/esp32-hal-timer.c b/cores/esp32/esp32-hal-timer.c index 38575fc9799..85e007143bd 100644 --- a/cores/esp32/esp32-hal-timer.c +++ b/cores/esp32/esp32-hal-timer.c @@ -44,9 +44,9 @@ struct timer_struct_t { inline TIMER_IRAM uint64_t timerRead(hw_timer_t *timer) { if (timer == NULL) { - #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM +#ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM log_e("Timer handle is NULL"); - #endif +#endif return 0; } uint64_t value; @@ -56,9 +56,9 @@ inline TIMER_IRAM uint64_t timerRead(hw_timer_t *timer) { void TIMER_IRAM timerWrite(hw_timer_t *timer, uint64_t val) { if (timer == NULL) { - #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM +#ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM log_e("Timer handle is NULL"); - #endif +#endif return; } gptimer_set_raw_count(timer->timer_handle, val); @@ -66,9 +66,9 @@ void TIMER_IRAM timerWrite(hw_timer_t *timer, uint64_t val) { void TIMER_IRAM timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count) { if (timer == NULL) { - #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM +#ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM log_e("Timer handle is NULL"); - #endif +#endif return; } esp_err_t err = ESP_OK; @@ -79,9 +79,9 @@ void TIMER_IRAM timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autorel }; err = gptimer_set_alarm_action(timer->timer_handle, &alarm_cfg); if (err != ESP_OK) { - #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM +#ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM log_e("Timer Alarm Write failed, error num=%d", err); - #endif +#endif } } @@ -96,9 +96,9 @@ uint32_t timerGetFrequency(hw_timer_t *timer) { void TIMER_IRAM timerStart(hw_timer_t *timer) { if (timer == NULL) { - #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM +#ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM log_e("Timer handle is NULL"); - #endif +#endif return; } gptimer_start(timer->timer_handle); @@ -107,9 +107,9 @@ void TIMER_IRAM timerStart(hw_timer_t *timer) { void TIMER_IRAM timerStop(hw_timer_t *timer) { if (timer == NULL) { - #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM +#ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM log_e("Timer handle is NULL"); - #endif +#endif return; } gptimer_stop(timer->timer_handle); @@ -118,9 +118,9 @@ void TIMER_IRAM timerStop(hw_timer_t *timer) { void TIMER_IRAM timerRestart(hw_timer_t *timer) { if (timer == NULL) { - #ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM +#ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM log_e("Timer handle is NULL"); - #endif +#endif return; } gptimer_set_raw_count(timer->timer_handle, 0);