Page 1 of 1

ESP32 timer and hw_timer_t timer structure

Posted: Tue Jul 04, 2023 1:06 pm
by fanfanlatulipe26
A classical way to setup a timer is for eample

Code: Select all

hw_timer_t *My_timer = NULL;

My_timer = timerBegin(0, 80, true);
timerAttachInterrupt(My_timer, &onTimer, true);
timerAlarmWrite(My_timer, 1000000, true);
timerAlarmEnable(My_timer);
If this code is in a small init function, do we need to declare My_timer as a global variable or can it be local.
I have a running code where it is local but I am not confortable with that.
I can't find the answer in the doc.

Re: ESP32 timer and hw_timer_t timer structure

Posted: Wed Jul 05, 2023 2:18 am
by ESP_Sprite
It depends. My_timer is only a variable that holds a pointer to the actual timer object. C doesn't do garbage collection, so if that variable goes out of scope, nothing will happen to the timer object itself; there's just no way you can reference it anymore in the rest of your code, but it'll keep on working. That can be acceptable if you never need to modify the timer afterwards anymore.

Re: ESP32 timer and hw_timer_t timer structure

Posted: Fri Jul 07, 2023 3:26 pm
by fanfanlatulipe26
Perfectly clear. Thank You for the explanation.