Can I attach multiple alarms to one Timer?

xbastonix
Posts: 1
Joined: Sat Mar 18, 2023 7:55 am

Can I attach multiple alarms to one Timer?

Postby xbastonix » Sat Mar 18, 2023 8:08 am

The ESP32 features 4 64-Bit timers, each with a 16-Bit prescaler and lots of different options for timer configuration. So far so good.
As I understand it i can only attach one ISR (which is called when a certain alarm value is reached by the timer) to each timer. As i have worked with AVR Microcontrollers (e.g. ATmega328P) before, which provide the possibility for setting 2 seperate "alarm values" for ONE single timer (COMPARE_A and COMPARE_B), I am looking for this option on the ESP32.
Basically, I want to set up a timer that counts to 1.000.000, and i want alarms to trigger an interrupt at counter values 100.000, 350.000 and 750.000. However, it seems that i can only attach one alarm value and thus ISR to each timer.
I currently have one timer running to 100.000, then stopping. A second one running to 350.000, then stopping. Same with the 3rd one (counting to 750.000 then stopping). The 4th timer is used to count to 1.000.000, then resetting and restarting timers 1,2 and 3. This works, but doesn't seem like the best way, can it be possible that i have to use all 4 timers to make my project work?

noweare
Posts: 74
Joined: Tue Jul 02, 2019 11:35 am

Re: Can I attach multiple alarms to one Timer?

Postby noweare » Mon Mar 20, 2023 11:44 pm

The timer only allows one alarm value but if you know the alarm values then set your code for the first alarm at 100000, in the interrupt set the alarm for 350000 when that fires set the alarm value for 750000 then when that fires set it to 10000 and the sequence just repeats itself so you get the alarms as desired. You could set the alarm values in an array.

From Espressif docs:
Dynamic Alarm Update
Alarm value can be updated dynamically inside the ISR handler callback, by changing gptimer_alarm_event_data_t::alarm_value. Then the alarm value will be updated after the callback function returns.

Hope that helps

Who is online

Users browsing this forum: No registered users and 71 guests