Skip to content

Allow calls to timer functions within ISR #11629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions cores/esp32/esp32-hal-timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);

Expand All @@ -36,27 +42,33 @@ struct timer_struct_t {
bool timer_started;
};

inline 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;
gptimer_get_raw_count(timer->timer_handle, &value);
return value;
}

void 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 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;
Expand All @@ -67,7 +79,9 @@ 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) {
#ifndef CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM
log_e("Timer Alarm Write failed, error num=%d", err);
#endif
}
}

Expand All @@ -80,27 +94,33 @@ uint32_t timerGetFrequency(hw_timer_t *timer) {
return frequency;
}

void 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 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 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);
Expand Down
Loading