Skip to content

Fix timerAttachInterrupt() and timerDetachInterrupt() in esp32-hal-timer.c #6763

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 5 commits into from
May 27, 2022
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
30 changes: 11 additions & 19 deletions cores/esp32/esp32-hal-timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ typedef union {

#define NUM_OF_TIMERS SOC_TIMER_GROUP_TOTAL_TIMERS

typedef struct {
int timer_group;
int timer_idx;
int alarm_interval;
bool auto_reload;
} timer_info_t;

typedef struct hw_timer_s
{
uint8_t group;
Expand Down Expand Up @@ -194,7 +187,7 @@ static void _on_apb_change(void * arg, apb_change_ev_t ev_type, uint32_t old_apb
hw_timer_t * timerBegin(uint8_t num, uint16_t divider, bool countUp){
if(num >= NUM_OF_TIMERS)
{
log_e("Timer dont have that timer number.");
log_e("Timer number %u exceeds available number of Timers.", num);
return NULL;
}

Expand All @@ -220,24 +213,23 @@ void timerEnd(hw_timer_t *timer){
timer_deinit(timer->group, timer->num);
}

bool IRAM_ATTR timerFnWrapper(void *arg){
void (*fn)(void) = arg;
fn();

// some additional logic or handling may be required here to approriately yield or not
return false;
}

void timerAttachInterrupt(hw_timer_t *timer, void (*fn)(void), bool edge){
if(edge){
log_w("EDGE timer interrupt is not supported! Setting to LEVEL...");
edge = false;
}
timer_enable_intr(timer->group, timer->num);

timer_info_t *timer_info = calloc(1, sizeof(timer_info_t));
timer_info->timer_group = timer->group;
timer_info->timer_idx = timer->num;
timer_info->auto_reload = timerGetAutoReload(timer);
timer_info->alarm_interval = timerAlarmRead(timer);

timer_isr_callback_add(timer->group, timer->num, (timer_isr_t)fn, timer_info, 0);
timer_isr_callback_add(timer->group, timer->num, timerFnWrapper, fn, 0);
}

void timerDetachInterrupt(hw_timer_t *timer){
timerAttachInterrupt(timer, NULL, false);
timer_isr_callback_remove(timer->group, timer->num);
}

uint64_t timerReadMicros(hw_timer_t *timer){
Expand Down