ESP32_S2_TimerInterrupt Library

khoih-prog
Posts: 53
Joined: Sat Feb 22, 2020 8:16 pm

ESP32_S2_TimerInterrupt Library

Postby khoih-prog » Thu May 06, 2021 5:30 pm

ESP32_S2_TimerInterrupt Library
How To Install Using Arduino Library Manager


This library enables you to use Interrupt from Hardware Timers on ESP32-S2-based boards.

The ESP32-S2 timer Interrupt control is different from that of ESP32, at least with the ESP32-S2 core v1.0.6. While the examples of current ESP32TimerInterrupt Library can be compiled OK, they can't run yet. That's why this new ESP32_S2_TimerInterrupt Library has been created.

As Hardware Timers are rare, and very precious assets of any board, this library now enables you to use up to 16 different ISR-based timers, while actually consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs). The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks.

Why do we need this ESP32_S2_TimerInterrupt Library

Imagine you have a system with a mission-critical function, measuring water level and control the sump pump or doing something much more important. You normally use asoftware timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().

So your function might not be executed on-time or not at all, and the result would be disastrous.

You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).

The correct choice is to use a Hardware Timer with Interrupt to call your function.

These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.

Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.

The catch is your function is now part of an ISR (Interrupt Service Routine), must be lean and mean, and follow certain rules. More to read on:

HOWTO Attach Interrupt


Important Notes:

1. Inside the ISR function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the ISR function may be lost. You should declare as volatile any variables that you modify within the attached function.

2. Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.

Initial Release v1.3.0

1. Initial coding to support ESP32-S2
2. Sync with ESP32TimerInterrupt Library v1.3.0


Currently Supported Boards

- ESP32-S2-based boards, such as ESP32_S2_DEV, ESP32_S2 Saola, etc


Examples:

1. Argument_None
2. Change_Interval
3. ISR_RPM_Measure
4. ISR_Switch
5. ISR_Timer_4_Switches
6. ISR_Timer_Complex
7. ISR_Timer_Switch
8. ISR_Timer_Switches
9. RPM_Measure
10. SwitchDebounce
11. TimerInterruptTest



Example Change_Interval on ESP32_S2_DEV

The following is the sample terminal output when running example Change_Interval to demonstrate how to change Timer Interval on-the-fly

Code: Select all

Starting Change_Interval on ESP32S2_DEV
ESP32_S2_TimerInterrupt v1.3.0
CPU Frequency = 240 MHz
[TISR] ESP32_S2_TimerInterrupt: _timerNo = 0 , _fre = 1000000
[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80
[TISR] _timerIndex = 0 , _timerGroup = 0
[TISR] _count = 0 - 2000000
[TISR] timer_set_alarm_value = 2000000.00
ITimer0: millis() = 720
Starting  ITimer0 OK, millis() = 724
[TISR] ESP32_S2_TimerInterrupt: _timerNo = 1 , _fre = 1000000
[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80
[TISR] _timerIndex = 1 , _timerGroup = 0
[TISR] _count = 0 - 5000000
[TISR] timer_set_alarm_value = 5000000.00
ITimer1: millis() = 746
Starting  ITimer1 OK, millis() = 749
ITimer0: millis() = 2715
ITimer0: millis() = 4715
ITimer1: millis() = 5741
ITimer0: millis() = 6715
ITimer0: millis() = 8715
Time = 10001, Timer0Count = 5, Timer1Count = 2
ITimer0: millis() = 10715
ITimer1: millis() = 10741
ITimer0: millis() = 12715
ITimer0: millis() = 14715
ITimer1: millis() = 15741
ITimer0: millis() = 16715
ITimer0: millis() = 18715
Time = 20002, Timer0Count = 10, Timer1Count = 4
[TISR] ESP32_S2_TimerInterrupt: _timerNo = 0 , _fre = 1000000
[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80
[TISR] _timerIndex = 0 , _timerGroup = 0
[TISR] _count = 0 - 4000000
[TISR] timer_set_alarm_value = 4000000.00
[TISR] ESP32_S2_TimerInterrupt: _timerNo = 1 , _fre = 1000000
[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80
[TISR] _timerIndex = 1 , _timerGroup = 0
[TISR] _count = 0 - 10000000
[TISR] timer_set_alarm_value = 10000000.00
Changing Interval, Timer0 = 4000,  Timer1 = 10000
ITimer0: millis() = 24011
ITimer0: millis() = 28011
Time = 30003, Timer0Count = 12, Timer1Count = 4
ITimer1: millis() = 30031
ITimer0: millis() = 32011
ITimer0: millis() = 36011

Who is online

Users browsing this forum: No registered users and 44 guests