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: 85
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

User avatar
CseaTec
Posts: 8
Joined: Thu Jul 07, 2022 12:21 pm

Re: Can I attach multiple alarms to one Timer?

Postby CseaTec » Wed Jan 15, 2025 3:29 pm

xbastonix wrote:
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....

Thanks for your original post about this. I too have worked for over 20 years with the ATmega168 and 328P and relied heavily on the Compare A and B match options in the timers (using Compare A twice and Compare B once for 3 "alarms" within a short externally-driven interval). I was already on my way to writing my ESP32 GPTimer interrupt alarm callback support to have multiple modes using a global bit mask, but then ran into the same issue as you. This prompted me to do a search. Now upon reading this, I'm going to have to add the ability to save multiple alarm times for rearming within the callback. The order of the 3 alarms can sometimes vary, so I'll have to make the callback smart enough to figure that out without wasting too much time in it. The ESP32 has great power in some respects, but it leaves a lot to be desired when it comes to being a good microcontroller.
Matteo Giovanetti
Electrical Engineer
CseaTec LLC
Pompano Beach, FL USA

horace99
Posts: 16
Joined: Mon Oct 14, 2024 10:38 am

Re: Can I attach multiple alarms to one Timer?

Postby horace99 » Sat Jan 18, 2025 3:02 pm

try
  1. // ESP32 timer interrupts -   three pulses 100Usec 350uSec 750uSec
  2.  
  3. #define pin 19
  4.  
  5. hw_timer_t *timer = NULL;
  6.  
  7. // timer ISR invert pin level
  8. volatile int counter = 0;
  9. void ARDUINO_ISR_ATTR onTimer() {
  10.   static int timer1=350;
  11.   counter++;
  12.   // digitalWrite(pin, !digitalRead(pin));
  13.   gpio_set_level((gpio_num_t)pin, HIGH); // HIGH pulse
  14.   // set next interrupt time
  15.   timerAlarm(timer, timer1, true, 0);
  16.   if(timer1==100)timer1=350;
  17.   else
  18.    if(timer1 == 350) timer1=750; else timer1=100;
  19.    gpio_set_level((gpio_num_t)pin, LOW);    // LOW
  20. }
  21.  
  22. void setup() {
  23.   Serial.begin(115200);
  24.   pinMode(pin, OUTPUT);
  25.   timer = timerBegin(1000000);            // Set timer frequency Mhz
  26.   timerAttachInterrupt(timer, &onTimer);  //  Attach onTimer function to our timer.
  27.   // Set alarm to call onTimer function(value in microseconds).
  28.   // Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
  29.   timerAlarm(timer, 150, true, 0);        // start with 150uSec pulse
  30. }
  31.  
  32. void loop() {
  33.   // display counter every second
  34.   static long timer1 = millis();
  35.   if (millis() - timer1 > 1000) {
  36.     timer1 = millis();
  37.     Serial.println(counter);
  38.     counter = 0;
  39.   }
  40. }
oscilloscope displays sequences of three pulses
three_pulses.jpg
three_pulses.jpg (22.03 KiB) Viewed 1732 times

Buldakov
Posts: 5
Joined: Wed Oct 02, 2024 2:46 pm

Re: Can I attach multiple alarms to one Timer?

Postby Buldakov » Tue Jan 21, 2025 3:27 pm

Hi. Here is the program code for operating a timer based on the frequency of a quartz oscillator.
How can I change this code if it is necessary to work not with the reference frequency of the quartz oscillator,
but with the external frequency supplied to the ESP32 input?

For example, I set up a timer trigger when 10 input pulses arrive, will we get 1 output pulse at the timer output?

lbernstone
Posts: 886
Joined: Mon Jul 22, 2019 3:20 pm

Re: Can I attach multiple alarms to one Timer?

Postby lbernstone » Tue Jan 21, 2025 5:32 pm

The ESP32 cannot use an external signal as a timer (well, not an arbitrary signal anyhow). It can monitor an external signal, and use that to trigger an interrupt.
https://docs.espressif.com/projects/esp ... /pcnt.html

Who is online

Users browsing this forum: No registered users and 34 guests