ESP32 Timers & Timer Interrupt
Satyabrata Senapati
Technical Content Writer , Technical Expert , Mentor , Trainer , & Embedded Software Developer
In this article we will study how to use ESP32 internal Timers & generate Timer Interrupt events in Arduino IDE.We’ll discuss how ESP32 Timers work, how to configure ESP32’s Timers, and how to generate periodic interrupts to synchronize the execution of logic within our project.
ESP32 Timers:-
The ESP32 SoCs come with 4 hardware timers, each of which is a general-purpose 64-bit up/down counter with a 16-bit prescaler. Except for ESP32-C3 which has only 2 timers each of which is 54 bits instead. The ESP32 timers have the capability of auto-reloading at the end of the counting period as well.
ESP32 Timers Functional Description:-
Each ESP32 timer uses the APB clock (APB_CLK which is normally 80 MHz in frequency) as a base clock. This clock is then scaled down by a 16-bit prescaler which generates the time-base tick time. Therefore, we’ll be changing the value of the prescaler in order to control the timer tick time.
The 16-Bit prescaler can divide the APB_CLK by a factor from 2 to 65536. When you set the prescaler value to be either 1 or 2, the clock divisor is 2; when you set the prescaler to 0, the clock divisor is 65536. Any other value will cause the clock to be divided by exactly that value that you’ve written to the prescaler register.
ESP32 Timers Alarm Generation:-
ESP32 timers can trigger an alarm (Event) which will cause a timer to reload and/or interrupt to occur, depending on your configuration. The alarm event is triggered when the value you’ve stored in the alarm register matches the current timer value. This is very useful to set periodic interrupts to execute some pieces of logic periodically in your project as we’ll be doing hereafter.
ESP32 Timers Equation:-
In order to generate periodic events with ESP32, we’ll be using the alarm event generation as well as the timer’s prescaler in order to achieve the desired interrupt periodicity. There are 3 special cases for the Timer equation down below, which are when the prescaler value is = 0,1, and 2. When Prescaler=1or2, it’s going to be as follows [ TOUT = TimerTicks x (2/APB_CLK) ]. When Prescaler=0, it’s going to be as follows [ TOUT = TimerTicks x (65536/APB_CLK) ]. Otherwise, we can generally use the equation down below.
Tout = TimerTicks * (Prescaler/APB_CLK)
Some Arduino-ESP32 Timer APIs for our application:-
timerBegin
This function is used to configure the timer. After successful setup the timer will automatically start.
hw_timer_t * timerBegin(uint32_t frequency);
This function will return timer structure if configuration is successful. If NULL is returned, error occurs and the timer was not configured.
timerAttachInterrupt
This function is used to attach interrupt to timer.
void timerAttachInterrupt(hw_timer_t * timer, void (*userFunc)(void));
timerAlarm
This function is used to configure alarm value and autoreload of the timer. Alarm is automaticaly enabled.
void timerAlarm(hw_timer_t * timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count);
ESP32 Timer Example in C code:-
#define LED_BUILTIN 2
hw_timer_t *Timer0_Cfg = NULL;
void IRAM_ATTR Timer0_ISR()
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
//// Set timer frequency to 1Mhz
Timer0_Cfg = timerBegin(1000000);
// Attach onTimer function to our timer.
timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR);
// Set alarm to call Timer0_ISR function every 100 ms (value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(Timer0_Cfg, 100000, true, 0);
}
void loop()
{
// Do Nothing!
}
That's it we will compile and download the executable to see the result on esp32 development board.
Note:- The board is available in amazon.in & the price is nearly about 500 to 600.So buy for learning purpose or any business purpose your wish.
Development Environment- Arduino IDE
Development kit- ESP32 wroom devkit version 1
Programming language - C
Thanks for reading this article & you are welcome.