ESP32 timer and hw_timer_t timer structure

fanfanlatulipe26
Posts: 2
Joined: Tue Jul 04, 2023 12:42 pm

ESP32 timer and hw_timer_t timer structure

Postby fanfanlatulipe26 » Tue Jul 04, 2023 1:06 pm

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.

ESP_Sprite
Posts: 9725
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 timer and hw_timer_t timer structure

Postby ESP_Sprite » Wed Jul 05, 2023 2:18 am

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.

fanfanlatulipe26
Posts: 2
Joined: Tue Jul 04, 2023 12:42 pm

Re: ESP32 timer and hw_timer_t timer structure

Postby fanfanlatulipe26 » Fri Jul 07, 2023 3:26 pm

Perfectly clear. Thank You for the explanation.

Who is online

Users browsing this forum: No registered users and 67 guests