- Notifications
You must be signed in to change notification settings - Fork30
This library enables you to use Interrupt from Hardware Timers on an ESP32-based board. It now supports 16 ISR-based timers, while 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…
License
khoih-prog/ESP32TimerInterrupt
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- Important Breaking Change from v2.0.0
- Why do we need this ESP32TimerInterrupt library
- Changelog
- Prerequisites
- Installation
- Note for Platform IO using ESP32 LittleFS
- HOWTO Fix
Multiple DefinitionsLinker Error - HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE)
- More useful Information
- How to use
- Examples
- Example ISR_16_Timers_Array_Complex
- Debug Terminal Output Samples
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Please have a look atHOWTO FixMultiple Definitions Linker Error
Why do we need thisESP32TimerInterrupt library
This library enables you to use Interrupt from Hardware Timers on an ESP32-based board.
AsHardware Timers are rare, and very precious assets of any board, this library now enables you to use up to16 ISR-based Timers, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs).
Now with these new16 ISR-based timers, the maximum interval ispractically unlimited (limited only by unsigned long milliseconds) whilethe accuracy is nearly perfect compared to software timers.
The most important feature is they're ISR-based timers. Therefore, their executions arenot blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.
TheISR_Timer_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers.
Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet and Blynk services. You can also have many(up to 16) timers to use.
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
You'll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking taskin loop(), using delay() function as an example. The elapsed time then is very unaccurate
Imagine you have a system with amission-critical function, measuring water level and control the sump pump or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function isblocking the loop() or setup().
So your functionmight not be executed, 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 withInterrupt to call your function.
These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much moreprecise (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 isyour function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:
- ESP32 boards, such as
ESP32_DEV, etc. - ESP32_S2-based boards, such as
ESP32S2_DEV,ESP32_S2 Saola, Adafruit QTPY_ESP32S2, ESP32S2 Native USB, UM FeatherS2 Neo, UM TinyS2, UM RMP, microS2, etc. - ESP32_C3-based boards, such as
ESP32C3_DEV, LOLIN_C3_MINI, DFROBOT_BEETLE_ESP32_C3, ADAFRUIT_QTPY_ESP32C3, AirM2M_CORE_ESP32C3, XIAO_ESP32C3, etc.New - ESP32_S3-based boards, such as ESP32S3_DEV, ESP32_S3_BOX, UM TINYS3, UM PROS3, UM FEATHERS3, FEATHER_ESP32S3_NOPSRAM, QTPY_ESP32S3_NOPSRAM, etc.New
Inside the attached function,delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare asvolatile any variables that you modify within the attached function.
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.
Arduino IDE 1.8.19+for Arduino.ESP32 Core 2.0.5+for ESP32-based boards..
SimpleTimer libraryto use with some examples.
The best and easiest way is to useArduino Library Manager. Search forESP32TimerInterrupt, then select / install the latest version.You can also use this link for more detailed instructions.
Another way to install is to:
- Navigate toESP32TimerInterrupt page.
- Download the latest release
ESP32TimerInterrupt-master.zip. - Extract the zip file to
ESP32TimerInterrupt-masterdirectory - Copy whole
ESP32TimerInterrupt-masterfolder to Arduino libraries' directory such as~/Arduino/libraries/.
- InstallVS Code
- InstallPlatformIO
- InstallESP32TimerInterrupt library by usingLibrary Manager. Search forESP32TimerInterrupt inPlatform.io Author's Libraries
- Use includedplatformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples atProject Configuration File
From esp32 core v1.0.6+,LittleFS_esp32 v1.0.6 has been included and this step is not necessary anymore.
In Platform IO, to fix the error when usingLittleFS_esp32 v1.0 for ESP32-based boards with ESP32 core v1.0.4- (ESP-IDF v3.2-), uncomment the following line
from
//#define CONFIG_LITTLEFS_FOR_IDF_3_2 /* For old IDF - like in release 1.0.4 */to
#defineCONFIG_LITTLEFS_FOR_IDF_3_2/* For old IDF - like in release 1.0.4*/
It's advisable to use the latestLittleFS_esp32 v1.0.5+ to avoid the issue.
Thanks toRoshan to report the issue inError esp_littlefs.c 'utime_p'
The current library implementation, usingxyz-Impl.h instead of standardxyz.cpp, possibly creates certainMultiple Definitions Linker error in certain use cases.
You can use
#include<ESP32TimerInterrupt.hpp>//https://github.com/khoih-prog/ESP32TimerInterrupt
in many files. But be sure to use the following#include <ESP32TimerInterrupt.h>in just 1.h,.cpp or.ino file, which mustnot be included in any other file, to avoidMultiple Definitions Linker Error
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error#include<ESP32TimerInterrupt.h>//https://github.com/khoih-prog/ESP32TimerInterrupt
You now don't need to includeESP32_ISR_Timer.h anymore.
Please have a look atESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example to have more detailed description and solution of the issue.
ADC1controls ADC function for pinsGPIO32-GPIO39ADC2controls ADC function for pinsGPIO0, 2, 4, 12-15, 25-27
Look in fileadc_common.c
In
ADC2, there're two locks used for different cases:
lock shared with app and Wi-Fi:ESP32:When Wi-Fi using the
ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed.ESP32S2:The controller's control over the ADC is determined by the arbiter. There is no need to control by lock.lock shared between tasks:when several tasks sharing the
ADC2, we want to guaranteeall the requests will be handled.Since conversions are short (about 31us), app returns the lock very soon,we use a spinlock to stand there waiting to do conversions one by one.adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
- In order to use
ADC2for other functions, we have toacquire complicated firmware locks and very difficult to do - So, it's not advisable to use
ADC2with WiFi/BlueTooth (BT/BLE). - Use
ADC1, and pinsGPIO32-GPIO39 - If somehow it's a must to use those pins serviced by
ADC2(GPIO0, 2, 4, 12, 13, 14, 15, 25, 26 and 27), use thefix mentioned at the end ofESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example to work with ESP32 WiFi/BlueTooth (BT/BLE).
- The ESP32, ESP32_S2 and ESP32_S3 has two timer groups, each one with two general purpose hardware timers.
- The ESP32_C3 has two timer groups, each one with only one general purpose hardware timer.
- All the timers are based on64-bit counters (except 54-bit counter for ESP32_S3 counter) and 16-bit prescalers.
- The timer counters can be configured to count up or down and support automatic reload and software reload.
- They can also generate alarms when they reach a specific value, defined by the software.
- The value of the counter can be read by the software program.
Now with these new16 ISR-based timers (while consuming only1 hardware timer), the maximum interval is practically unlimited (limited only by unsigned long milliseconds). The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers Therefore, their executions are not blocked by bad-behaving functions / tasks.This important feature is absolutely necessary for mission-critical tasks.
TheISR_16_Timers_Array_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers.Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet and Blynk services. You can also have many(up to 16) timers to use.
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
You'll see software-basedSimpleTimer is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking taskin loop(), using delay() function as an example. The elapsed time then is very unaccurate
Before using any Timer, you have to make sure the Timer has not been used by any other purpose.
Timer0, Timer1, Timer2 and Timer3 are supported forESP32,ESP32_S2 andESP32_S3.Timer0, Timer1 are supported forESP32_C3.
- Argument_None
- Change_Interval
- ISR_16_Timers_Array
- ISR_16_Timers_Array_Complex
- RPM_Measure
- SwitchDebounce
- TimerInterruptTest
- multiFileProjectMore complex
- ISR_16_Timers_Array_OneShotNew
- ISR_16_Timers_Array_Complex_OneShotNew
ExampleISR_16_Timers_Array_Complex
ESP32TimerInterrupt/examples/ISR_16_Timers_Array_Complex/ISR_16_Timers_Array_Complex.ino
Lines 44 to 385 ina9f847b
| #if !defined( ESP32 ) | |
| #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. | |
| #endif | |
| // These define's must be placed at the beginning before #include "ESP32TimerInterrupt.h" | |
| #define_TIMERINTERRUPT_LOGLEVEL_4 | |
| // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error | |
| #include"ESP32TimerInterrupt.h" | |
| #include<SimpleTimer.h>// https://github.com/jfturcot/SimpleTimer | |
| // Don't use PIN_D1 in core v2.0.0 and v2.0.1. Check https://github.com/espressif/arduino-esp32/issues/5868 | |
| #ifndef LED_BLUE | |
| #defineLED_BLUE25 | |
| #endif | |
| #ifndef LED_RED | |
| #defineLED_RED27 | |
| #endif | |
| // Don't use PIN_D1 in core v2.0.0 and v2.0.1. Check https://github.com/espressif/arduino-esp32/issues/5868 | |
| // Don't use PIN_D2 with ESP32_C3 (crash) | |
| #definePIN_D1919// Pin D19 mapped to pin GPIO9 of ESP32 | |
| #definePIN_D33// Pin D3 mapped to pin GPIO3/RX0 of ESP32 | |
| #defineHW_TIMER_INTERVAL_US10000L | |
| volatileuint32_t startMillis =0; | |
| // Init ESP32 timer 1 | |
| ESP32TimerITimer(1); | |
| // Init ESP32_ISR_Timer | |
| ESP32_ISR_Timer ISR_Timer; | |
| #defineLED_TOGGLE_INTERVAL_MS2000L | |
| // With core v2.0.0+, you can't use Serial.print/println in ISR or crash. | |
| // and you can't use float calculation inside ISR | |
| // Only OK in core v1.0.6- | |
| bool IRAM_ATTRTimerHandler(void * timerNo) | |
| { | |
| staticbool toggle =false; | |
| staticint timeRun =0; | |
| ISR_Timer.run(); | |
| // Toggle LED every LED_TOGGLE_INTERVAL_MS = 2000ms = 2s | |
| if (++timeRun == ((LED_TOGGLE_INTERVAL_MS *1000) / HW_TIMER_INTERVAL_US) ) | |
| { | |
| timeRun =0; | |
| //timer interrupt toggles pin PIN_D19 | |
| digitalWrite(PIN_D19, toggle); | |
| toggle = !toggle; | |
| } | |
| returntrue; | |
| } | |
| ///////////////////////////////////////////////// | |
| #defineNUMBER_ISR_TIMERS16 | |
| typedefvoid (*irqCallback) (); | |
| ///////////////////////////////////////////////// | |
| #defineUSE_COMPLEX_STRUCTtrue | |
| #if USE_COMPLEX_STRUCT | |
| typedefstruct | |
| { | |
| irqCallback irqCallbackFunc; | |
| uint32_t TimerInterval; | |
| unsignedlong deltaMillis; | |
| unsignedlong previousMillis; | |
| } ISRTimerData; | |
| // In ESP32, avoid doing something fancy in ISR, for example Serial.print() | |
| // The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment | |
| // Or you can get this run-time error / crash | |
| voiddoingSomething(int index); | |
| #else | |
| volatileunsignedlong deltaMillis [NUMBER_ISR_TIMERS] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; | |
| volatileunsignedlong previousMillis [NUMBER_ISR_TIMERS] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; | |
| // You can assign any interval for any timer here, in milliseconds | |
| uint32_t TimerInterval[NUMBER_ISR_TIMERS] = | |
| { | |
| 5000L,10000L,15000L,20000L,25000L,30000L,35000L,40000L, | |
| 45000L,50000L,55000L,60000L,65000L,70000L,75000L,80000L | |
| }; | |
| voiddoingSomething(int index) | |
| { | |
| unsignedlong currentMillis =millis(); | |
| deltaMillis[index] = currentMillis - previousMillis[index]; | |
| previousMillis[index] = currentMillis; | |
| } | |
| #endif | |
| //////////////////////////////////// | |
| // Shared | |
| //////////////////////////////////// | |
| voiddoingSomething0() | |
| { | |
| doingSomething(0); | |
| } | |
| voiddoingSomething1() | |
| { | |
| doingSomething(1); | |
| } | |
| voiddoingSomething2() | |
| { | |
| doingSomething(2); | |
| } | |
| voiddoingSomething3() | |
| { | |
| doingSomething(3); | |
| } | |
| voiddoingSomething4() | |
| { | |
| doingSomething(4); | |
| } | |
| voiddoingSomething5() | |
| { | |
| doingSomething(5); | |
| } | |
| voiddoingSomething6() | |
| { | |
| doingSomething(6); | |
| } | |
| voiddoingSomething7() | |
| { | |
| doingSomething(7); | |
| } | |
| voiddoingSomething8() | |
| { | |
| doingSomething(8); | |
| } | |
| voiddoingSomething9() | |
| { | |
| doingSomething(9); | |
| } | |
| voiddoingSomething10() | |
| { | |
| doingSomething(10); | |
| } | |
| voiddoingSomething11() | |
| { | |
| doingSomething(11); | |
| } | |
| voiddoingSomething12() | |
| { | |
| doingSomething(12); | |
| } | |
| voiddoingSomething13() | |
| { | |
| doingSomething(13); | |
| } | |
| voiddoingSomething14() | |
| { | |
| doingSomething(14); | |
| } | |
| voiddoingSomething15() | |
| { | |
| doingSomething(15); | |
| } | |
| #if USE_COMPLEX_STRUCT | |
| ISRTimerData curISRTimerData[NUMBER_ISR_TIMERS] = | |
| { | |
| //irqCallbackFunc, TimerInterval, deltaMillis, previousMillis | |
| { doingSomething0,5000L,0,0 }, | |
| { doingSomething1,10000L,0,0 }, | |
| { doingSomething2,15000L,0,0 }, | |
| { doingSomething3,20000L,0,0 }, | |
| { doingSomething4,25000L,0,0 }, | |
| { doingSomething5,30000L,0,0 }, | |
| { doingSomething6,35000L,0,0 }, | |
| { doingSomething7,40000L,0,0 }, | |
| { doingSomething8,45000L,0,0 }, | |
| { doingSomething9,50000L,0,0 }, | |
| { doingSomething10,55000L,0,0 }, | |
| { doingSomething11,60000L,0,0 }, | |
| { doingSomething12,65000L,0,0 }, | |
| { doingSomething13,70000L,0,0 }, | |
| { doingSomething14,75000L,0,0 }, | |
| { doingSomething15,80000L,0,0 } | |
| }; | |
| voiddoingSomething(int index) | |
| { | |
| unsignedlong currentMillis =millis(); | |
| curISRTimerData[index].deltaMillis = currentMillis - curISRTimerData[index].previousMillis; | |
| curISRTimerData[index].previousMillis = currentMillis; | |
| } | |
| #else | |
| irqCallback irqCallbackFunc[NUMBER_ISR_TIMERS] = | |
| { | |
| doingSomething0, doingSomething1, doingSomething2, doingSomething3, | |
| doingSomething4, doingSomething5, doingSomething6, doingSomething7, | |
| doingSomething8, doingSomething9, doingSomething10, doingSomething11, | |
| doingSomething12, doingSomething13, doingSomething14, doingSomething15 | |
| }; | |
| #endif | |
| /////////////////////////////////////////// | |
| #defineSIMPLE_TIMER_MS2000L | |
| // Init SimpleTimer | |
| SimpleTimer simpleTimer; | |
| // Here is software Timer, you can do somewhat fancy stuffs without many issues. | |
| // But always avoid | |
| // 1. Long delay() it just doing nothing and pain-without-gain wasting CPU power.Plan and design your code / strategy ahead | |
| // 2. Very long "do", "while", "for" loops without predetermined exit time. | |
| voidsimpleTimerDoingSomething2s() | |
| { | |
| staticunsignedlong previousMillis = startMillis; | |
| unsignedlong currMillis =millis(); | |
| Serial.print(F("SimpleTimer :")); | |
| Serial.print(SIMPLE_TIMER_MS /1000); | |
| Serial.print(F(", ms :")); | |
| Serial.print(currMillis); | |
| Serial.print(F(", Dms :")); | |
| Serial.println(currMillis - previousMillis); | |
| for (uint16_t i =0; i < NUMBER_ISR_TIMERS; i++) | |
| { | |
| #if USE_COMPLEX_STRUCT | |
| Serial.print(F("Timer :")); | |
| Serial.print(i); | |
| Serial.print(F(", programmed :")); | |
| Serial.print(curISRTimerData[i].TimerInterval); | |
| Serial.print(F(", actual :")); | |
| Serial.println(curISRTimerData[i].deltaMillis); | |
| #else | |
| Serial.print(F("Timer :")); | |
| Serial.print(i); | |
| Serial.print(F(", programmed :")); | |
| Serial.print(TimerInterval[i]); | |
| Serial.print(F(", actual :")); | |
| Serial.println(deltaMillis[i]); | |
| #endif | |
| } | |
| previousMillis = currMillis; | |
| } | |
| voidsetup() | |
| { | |
| pinMode(PIN_D19, OUTPUT); | |
| Serial.begin(115200); | |
| while (!Serial &&millis() <5000); | |
| delay(500); | |
| Serial.print(F("\nStarting ISR_16_Timers_Array_Complex on")); | |
| Serial.println(ARDUINO_BOARD); | |
| Serial.println(ESP32_TIMER_INTERRUPT_VERSION); | |
| Serial.print(F("CPU Frequency =")); | |
| Serial.print(F_CPU /1000000); | |
| Serial.println(F(" MHz")); | |
| // Interval in microsecs | |
| if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler)) | |
| { | |
| startMillis =millis(); | |
| Serial.print(F("Starting ITimer OK, millis() =")); | |
| Serial.println(startMillis); | |
| } | |
| else | |
| Serial.println(F("Can't set ITimer. Select another freq. or timer")); | |
| startMillis =millis(); | |
| // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary | |
| // You can use up to 16 timer for each ISR_Timer | |
| for (uint16_t i =0; i < NUMBER_ISR_TIMERS; i++) | |
| { | |
| #if USE_COMPLEX_STRUCT | |
| curISRTimerData[i].previousMillis = startMillis; | |
| ISR_Timer.setInterval(curISRTimerData[i].TimerInterval, curISRTimerData[i].irqCallbackFunc); | |
| #else | |
| previousMillis[i] =millis(); | |
| ISR_Timer.setInterval(TimerInterval[i], irqCallbackFunc[i]); | |
| #endif | |
| } | |
| // You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary. | |
| simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s); | |
| } | |
| #defineBLOCKING_TIME_MS10000L | |
| voidloop() | |
| { | |
| // This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer | |
| // You see the time elapse of ISR_Timer still accurate, whereas very unaccurate for Software Timer | |
| // The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS) | |
| // While that of ISR_Timer is still prefect. | |
| delay(BLOCKING_TIME_MS); | |
| // You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary | |
| // You don't need to and never call ISR_Timer.run() here in the loop(). It's already handled by ISR timer. | |
| simpleTimer.run(); | |
| } |
The following is the sample terminal output when running exampleTimerInterruptTest onESP32C3_DEV to demonstrate how to start/stop Hardware Timers.
Starting TimerInterruptTest on ESP32C3_DEVESP32TimerInterrupt v2.3.0CPU Frequency = 160 MHz[TISR] ESP32_TimerInterrupt: _timerNo = 0 , _fre = 1000000[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80[TISR] _timerIndex = 0 , _timerGroup = 0[TISR] _count = 0 - 1000000[TISR] timer_set_alarm_value = 1000000.00Starting ITimer0 OK, millis() = 314[TISR] ESP32_TimerInterrupt: _timerNo = 1 , _fre = 1000000[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80[TISR] _timerIndex = 0 , _timerGroup = 1[TISR] _count = 0 - 3000000[TISR] timer_set_alarm_value = 3000000.00Starting ITimer1 OK, millis() = 346Stop ITimer0, millis() = 5001Start ITimer0, millis() = 10002Stop ITimer1, millis() = 15001Stop ITimer0, millis() = 15003Start ITimer0, millis() = 20004Stop ITimer0, millis() = 25005Start ITimer1, millis() = 30002Start ITimer0, millis() = 30006Stop ITimer0, millis() = 35007The following is the sample terminal output when running exampleChange_Interval onESP32C3_DEV to demonstrate how to change Timer Interval on-the-fly
Starting Change_Interval on ESP32C3_DEVESP32TimerInterrupt v2.3.0CPU Frequency = 160 MHzStarting ITimer0 OK, millis() = 293Starting ITimer1 OK, millis() = 303Time = 10001, Timer0Count = 5, Timer1Count = 2Time = 20002, Timer0Count = 10, Timer1Count = 4Changing Interval, Timer0 = 4000, Timer1 = 10000Time = 30003, Timer0Count = 12, Timer1Count = 5Time = 40004, Timer0Count = 15, Timer1Count = 6Changing Interval, Timer0 = 2000, Timer1 = 5000Time = 50005, Timer0Count = 20, Timer1Count = 8Time = 60006, Timer0Count = 25, Timer1Count = 10Changing Interval, Timer0 = 4000, Timer1 = 10000Time = 70007, Timer0Count = 27, Timer1Count = 11Time = 80008, Timer0Count = 30, Timer1Count = 12Changing Interval, Timer0 = 2000, Timer1 = 5000Time = 90009, Timer0Count = 35, Timer1Count = 14Time = 100010, Timer0Count = 40, Timer1Count = 16Changing Interval, Timer0 = 4000, Timer1 = 10000Time = 110011, Timer0Count = 42, Timer1Count = 17Time = 120012, Timer0Count = 45, Timer1Count = 18Changing Interval, Timer0 = 2000, Timer1 = 5000Time = 130013, Timer0Count = 50, Timer1Count = 20Time = 140014, Timer0Count = 55, Timer1Count = 22Changing Interval, Timer0 = 4000, Timer1 = 10000The following is the sample terminal output when running exampleArgument_None onESP32S3_DEV
Starting Argument_None on ESP32S3_DEVESP32TimerInterrupt v2.3.0CPU Frequency = 240 MHz[TISR] ESP32_S3_TimerInterrupt: _timerNo = 0 , _fre = 1000000[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80[TISR] _timerIndex = 0 , _timerGroup = 0[TISR] _count = 0 - 1000000[TISR] timer_set_alarm_value = 1000000.00Starting ITimer0 OK, millis() = 317[TISR] ESP32_S3_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.00Starting ITimer1 OK, millis() = 348The following is the sample terminal output when running exampleISR_16_Timers_Array_Complex onESP32S3_DEV to demonstrate of ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!
Starting ISR_16_Timers_Array_Complex on ESP32S3_DEVESP32TimerInterrupt v2.3.0CPU Frequency = 240 MHz[TISR] ESP32_S3_TimerInterrupt: _timerNo = 1 , _fre = 1000000[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80[TISR] _timerIndex = 1 , _timerGroup = 0[TISR] _count = 0 - 10000[TISR] timer_set_alarm_value = 10000.00Starting ITimer OK, millis() = 318SimpleTimer : 2, ms : 10317, Dms : 9999Timer : 0, programmed : 5000, actual : 5009Timer : 1, programmed : 10000, actual : 0Timer : 2, programmed : 15000, actual : 0Timer : 3, programmed : 20000, actual : 0Timer : 4, programmed : 25000, actual : 0Timer : 5, programmed : 30000, actual : 0Timer : 6, programmed : 35000, actual : 0Timer : 7, programmed : 40000, actual : 0Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 20380, Dms : 10063Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15009Timer : 3, programmed : 20000, actual : 20009Timer : 4, programmed : 25000, actual : 0Timer : 5, programmed : 30000, actual : 0Timer : 6, programmed : 35000, actual : 0Timer : 7, programmed : 40000, actual : 0Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 30443, Dms : 10063Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20009Timer : 4, programmed : 25000, actual : 25009Timer : 5, programmed : 30000, actual : 30009Timer : 6, programmed : 35000, actual : 0Timer : 7, programmed : 40000, actual : 0Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 40506, Dms : 10063Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25009Timer : 5, programmed : 30000, actual : 30009Timer : 6, programmed : 35000, actual : 35009Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 50569, Dms : 10063Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30009Timer : 6, programmed : 35000, actual : 35009Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 60632, Dms : 10063Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35009Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 55009Timer : 11, programmed : 60000, actual : 60009Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 70695, Dms : 10063Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 55009Timer : 11, programmed : 60000, actual : 60009Timer : 12, programmed : 65000, actual : 65009Timer : 13, programmed : 70000, actual : 70009Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 80758, Dms : 10063Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40000Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 55009Timer : 11, programmed : 60000, actual : 60009Timer : 12, programmed : 65000, actual : 65009Timer : 13, programmed : 70000, actual : 70009Timer : 14, programmed : 75000, actual : 75009Timer : 15, programmed : 80000, actual : 80009...SimpleTimer : 2, ms : 161262, Dms : 10063Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40000Timer : 8, programmed : 45000, actual : 45000Timer : 9, programmed : 50000, actual : 50000Timer : 10, programmed : 55000, actual : 55000Timer : 11, programmed : 60000, actual : 60000Timer : 12, programmed : 65000, actual : 65000Timer : 13, programmed : 70000, actual : 70000Timer : 14, programmed : 75000, actual : 75000Timer : 15, programmed : 80000, actual : 80000The following is the sample terminal output when running exampleISR_16_Timers_Array onESP32S3_DEV to demonstrate of ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!
Starting ISR_16_Timers_Array on ESP32S3_DEVESP32TimerInterrupt v2.3.0CPU Frequency = 240 MHz[TISR] ESP32_S3_TimerInterrupt: _timerNo = 1 , _fre = 1000000[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80[TISR] _timerIndex = 1 , _timerGroup = 0[TISR] _count = 0 - 1000[TISR] timer_set_alarm_value = 1000.00Starting ITimer OK, millis() = 318simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10001simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000simpleTimerDoingSomething2s: Delta programmed ms = 2000, actual = 10000The following is the sample terminal output when running exampleISR_16_Timers_Array_Complex onESP32C3_DEV to demonstrate of ISR Hardware Timer, especially when system is very busy or blocked. The 16 independent ISR timers are programmed to be activated repetitively after certain intervals, is activated exactly after that programmed interval !!!
Starting ISR_16_Timers_Array_Complex on ESP32C3_DEVESP32TimerInterrupt v2.3.0CPU Frequency = 160 MHz[TISR] ESP32_TimerInterrupt: _timerNo = 1 , _fre = 1000000[TISR] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80[TISR] _timerIndex = 0 , _timerGroup = 1[TISR] _count = 0 - 10000[TISR] timer_set_alarm_value = 10000.00Starting ITimer OK, millis() = 314SimpleTimer : 2, ms : 10314, Dms : 9999Timer : 0, programmed : 5000, actual : 5009Timer : 1, programmed : 10000, actual : 0Timer : 2, programmed : 15000, actual : 0Timer : 3, programmed : 20000, actual : 0Timer : 4, programmed : 25000, actual : 0Timer : 5, programmed : 30000, actual : 0Timer : 6, programmed : 35000, actual : 0Timer : 7, programmed : 40000, actual : 0Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 20381, Dms : 10067Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15009Timer : 3, programmed : 20000, actual : 20009Timer : 4, programmed : 25000, actual : 0Timer : 5, programmed : 30000, actual : 0Timer : 6, programmed : 35000, actual : 0Timer : 7, programmed : 40000, actual : 0Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 30448, Dms : 10067Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20009Timer : 4, programmed : 25000, actual : 25009Timer : 5, programmed : 30000, actual : 30009Timer : 6, programmed : 35000, actual : 0Timer : 7, programmed : 40000, actual : 0Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 40515, Dms : 10067Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25009Timer : 5, programmed : 30000, actual : 30009Timer : 6, programmed : 35000, actual : 35009Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 0Timer : 9, programmed : 50000, actual : 0Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 50582, Dms : 10067Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30009Timer : 6, programmed : 35000, actual : 35009Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 0Timer : 11, programmed : 60000, actual : 0Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 60649, Dms : 10067Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35009Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 55009Timer : 11, programmed : 60000, actual : 60009Timer : 12, programmed : 65000, actual : 0Timer : 13, programmed : 70000, actual : 0Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 70716, Dms : 10067Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40009Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 55009Timer : 11, programmed : 60000, actual : 60009Timer : 12, programmed : 65000, actual : 65009Timer : 13, programmed : 70000, actual : 70009Timer : 14, programmed : 75000, actual : 0Timer : 15, programmed : 80000, actual : 0SimpleTimer : 2, ms : 80783, Dms : 10067Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40000Timer : 8, programmed : 45000, actual : 45009Timer : 9, programmed : 50000, actual : 50009Timer : 10, programmed : 55000, actual : 55009Timer : 11, programmed : 60000, actual : 60009Timer : 12, programmed : 65000, actual : 65009Timer : 13, programmed : 70000, actual : 70009Timer : 14, programmed : 75000, actual : 75009Timer : 15, programmed : 80000, actual : 80009...SimpleTimer : 2, ms : 161319, Dms : 10067Timer : 0, programmed : 5000, actual : 5000Timer : 1, programmed : 10000, actual : 10000Timer : 2, programmed : 15000, actual : 15000Timer : 3, programmed : 20000, actual : 20000Timer : 4, programmed : 25000, actual : 25000Timer : 5, programmed : 30000, actual : 30000Timer : 6, programmed : 35000, actual : 35000Timer : 7, programmed : 40000, actual : 40000Timer : 8, programmed : 45000, actual : 45000Timer : 9, programmed : 50000, actual : 50000Timer : 10, programmed : 55000, actual : 55000Timer : 11, programmed : 60000, actual : 60000Timer : 12, programmed : 65000, actual : 65000Timer : 13, programmed : 70000, actual : 70000Timer : 14, programmed : 75000, actual : 75000Timer : 15, programmed : 80000, actual : 80000Debug is enabled by default on Serial.
You can also change the debugging level (TIMERINTERRUPT_LOGLEVEL) from 0 to 4
// These define's must be placed at the beginning before #include "ESP32TimerInterrupt.h"// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.#defineTIMER_INTERRUPT_DEBUG0#define_TIMERINTERRUPT_LOGLEVEL_0
If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.
Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.
Submit issues to:ESP32TimerInterrupt issues
- Search for bug and improvement.
- Basic hardware timers for ESP32.
- More hardware-initiated software-enabled timers
- Longer time interval
- Similar features for remaining Arduino boards such as SAMD21, SAMD51, SAM-DUE, nRF52, ESP8266, STM32, etc.
- Add support to new ESP32-S2
- Add support to new ESP32 core v1.0.6
- Fix compiler errors due to conflict to some libraries.
- Add complex examples.
- Avoid using D1 in examples due to issue with core v2.0.0 and v2.0.1.
- Fix
multiple-definitionslinker error. Dropsrc_cppandsrc_hdirectories - Restructure library.
- Add support to new ESP32_S3. Now supporting ESP32, ESP32_S2, ESP32_S3 and ESP32_C3
- Optimize library code by using
reference-passinginstead ofvalue-passing - Add examplemultiFileProject to demo for multiple-file project
- Add exampleISR_16_Timers_Array_Complex_OneShot to demo how to use
one-shot ISR-based timerin complex case - Add exampleISR_16_Timers_Array_OneShot to demo how to use
one-shot ISR-based timer - Add support to many more boards, such as
- ESP32_S2 : ESP32S2 Native USB, UM FeatherS2 Neo, UM TinyS2, UM RMP, microS2, LOLIN_S2_MINI, LOLIN_S2_PICO, ADAFRUIT_FEATHER_ESP32S2, ADAFRUIT_FEATHER_ESP32S2_TFT, ATMegaZero ESP32-S2, Deneyap Mini, FRANZININHO_WIFI, FRANZININHO_WIFI_MSC
- ESP32_S3 : UM TinyS3, UM PROS3, UM FeatherS3, ESP32_S3_USB_OTG, ESP32S3_CAM_LCD, DFROBOT_FIREBEETLE_2_ESP32S3, ADAFRUIT_FEATHER_ESP32S3_TFT
- ESP32_C3 : LOLIN_C3_MINI, DFROBOT_BEETLE_ESP32_C3, ADAFRUIT_QTPY_ESP32C3, AirM2M_CORE_ESP32C3, XIAO_ESP32C3
- Use
allman astyleand addutils
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
- Thanks toJelmer to report and make PR inMoved the implementation header file to a separate .cpp file leading to new Version v1.1.0
- Thanks topedrojvs to report the issue inError in the value defined by TIMER0_INTERVAL_MS #28 leading to new Version v2.3.0
![]() Jelmer | ![]() pedrojvs |
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed underMIT
Copyright 2019- Khoi Hoang
About
This library enables you to use Interrupt from Hardware Timers on an ESP32-based board. It now supports 16 ISR-based timers, while 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…
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.


