Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit cf001ca

Browse files
authored
v1.1.1
### Releases v1.1.1 1. Add Change_Interval example to show how to change TimerInterval on-the-fly 2. Add Version String and Change_Interval example to show how to change TimerInterval
1 parent 50928e8 commit cf001ca

File tree

23 files changed

+308
-39
lines changed

23 files changed

+308
-39
lines changed

README.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine),
4242
---
4343
---
4444

45+
### Releases v1.1.1
46+
47+
1. Add Change_Interval example to show how to change TimerInterval on-the-fly
48+
2. Add Version String and Change_Interval example to show how to change TimerInterval
49+
4550
### Releases v1.1.0
4651

4752
1. Restore cpp code besides Impl.h code to use if Multiple-Definition linker error.
@@ -62,7 +67,7 @@ The catch is **your function is now part of an ISR (Interrupt Service Routine),
6267
---
6368
---
6469

65-
## Prerequisite
70+
## Prerequisites
6671

6772
1. [`Arduino IDE 1.8.13+` for Arduino](https://www.arduino.cc/en/Main/Software)
6873
2. [`ESP32 Core 1.0.4+`](https://github.com/espressif/arduino-esp32) for ESP32-based boards
@@ -90,7 +95,7 @@ Another way to install is to:
9095

9196
1. Install [VS Code](https://code.visualstudio.com/)
9297
2. Install [PlatformIO](https://platformio.org/platformio-ide)
93-
3. Install [**ESP32TimerInterrupt** library](https://platformio.org/lib/show/6904/ESP32TimerInterrupt) by using [Library Manager](https://platformio.org/lib/show/6904/ESP32TimerInterrupt/installation). Search for **ESP32TimerInterrupt** in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22)
98+
3. Install [**ESP32TimerInterrupt** library](https://platformio.org/lib/show/11384/ESP32TimerInterrupt) or [**ESP32TimerInterrupt** library](https://platformio.org/lib/show/6904/ESP32TimerInterrupt) by using [Library Manager](https://platformio.org/lib/show/11384/ESP32TimerInterrupt/installation). Search for **ESP32TimerInterrupt** in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22)
9499
4. Use included [platformio.ini](platformio/platformio.ini) file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at [Project Configuration File](https://docs.platformio.org/page/projectconf.html)
95100

96101
---
@@ -207,6 +212,7 @@ Before using any Timer, you have to make sure the Timer has not been used by any
207212
8. [RPM_Measure](examples/RPM_Measure)
208213
9. [SwitchDebounce](examples/SwitchDebounce)
209214
10. [TimerInterruptTest](examples/TimerInterruptTest)
215+
11. [Change_Interval](examples/Change_Interval). New.
210216

211217

212218
---
@@ -405,6 +411,8 @@ void setup()
405411
while (!Serial);
406412
407413
Serial.println("\nStarting ISR_Timer_Complex on " + String(ARDUINO_BOARD));
414+
Serial.println(ESP32_TIMER_INTERRUPT_VERSION);
415+
Serial.printf("CPU Frequency = %ld MHz\n", F_CPU / 1000000);
408416
409417
// You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary.
410418
blynkTimer.setInterval(BLYNK_TIMER_MS, blynkDoingSomething2s);
@@ -520,6 +528,8 @@ While software timer, **programmed for 2s, is activated after 3.435s !!!**
520528

521529
```
522530
Starting ISR_Timer_Complex on ESP32_DEV
531+
ESP32TimerInterrupt v1.1.1
532+
CPU Frequency = 240 MHz
523533
ESP32TimerInterrupt: _timerNo = 1, _fre = 1000000.00, _count = 0 - 50000
524534
Starting ITimer OK, millis() = 2140
525535
[2341]
@@ -581,6 +591,8 @@ blynkDoingSomething2s: Delta programmed ms = 2000, actual = 3000
581591

582592
```
583593
Starting TimerInterruptTest on ESP32_DEV
594+
ESP32TimerInterrupt v1.1.1
595+
CPU Frequency = 240 MHz
584596
ESP32TimerInterrupt: _timerNo = 0, _fre = 1000000.00, _count = 0 - 1000000
585597
Starting ITimer0 OK, millis() = 136
586598
ESP32TimerInterrupt: _timerNo = 1, _fre = 1000000.00, _count = 0 - 3000000
@@ -632,9 +644,53 @@ ITimer0: millis() = 45008
632644
Stop ITimer0, millis() = 45009
633645
634646
```
647+
648+
---
649+
650+
2. The following is the sample terminal output when running example [Change_Interval](examples/Change_Interval) to demonstrate how to change Timer Interval on-the-fly
651+
652+
```
653+
Starting Change_Interval on ESP32_DEV
654+
ESP32TimerInterrupt v1.1.1
655+
CPU Frequency = 240 MHz
656+
Starting ITimer0 OK, millis() = 136
657+
Starting ITimer1 OK, millis() = 136
658+
Time = 10001, Timer0Count = 19, , Timer1Count = 9
659+
Time = 20002, Timer0Count = 39, , Timer1Count = 19
660+
Changing Interval, Timer0 = 1000, Timer1 = 2000
661+
Time = 30003, Timer0Count = 49, , Timer1Count = 24
662+
Time = 40004, Timer0Count = 59, , Timer1Count = 29
663+
Changing Interval, Timer0 = 500, Timer1 = 1000
664+
Time = 50005, Timer0Count = 79, , Timer1Count = 39
665+
Time = 60006, Timer0Count = 99, , Timer1Count = 49
666+
Changing Interval, Timer0 = 1000, Timer1 = 2000
667+
Time = 70007, Timer0Count = 109, , Timer1Count = 54
668+
Time = 80008, Timer0Count = 119, , Timer1Count = 59
669+
Changing Interval, Timer0 = 500, Timer1 = 1000
670+
Time = 90009, Timer0Count = 139, , Timer1Count = 69
671+
Time = 100010, Timer0Count = 159, , Timer1Count = 79
672+
Changing Interval, Timer0 = 1000, Timer1 = 2000
673+
Time = 110011, Timer0Count = 169, , Timer1Count = 84
674+
Time = 120012, Timer0Count = 179, , Timer1Count = 89
675+
Changing Interval, Timer0 = 500, Timer1 = 1000
676+
Time = 130013, Timer0Count = 199, , Timer1Count = 99
677+
Time = 140014, Timer0Count = 219, , Timer1Count = 109
678+
Changing Interval, Timer0 = 1000, Timer1 = 2000
679+
Time = 150015, Timer0Count = 229, , Timer1Count = 114
680+
Time = 160016, Timer0Count = 239, , Timer1Count = 119
681+
Changing Interval, Timer0 = 500, Timer1 = 1000
682+
Time = 170017, Timer0Count = 259, , Timer1Count = 129
683+
Time = 180018, Timer0Count = 279, , Timer1Count = 139
684+
```
685+
635686
---
636687
---
637688

689+
### Releases v1.1.1
690+
691+
1. Add Change_Interval example to show how to change TimerInterval on-the-fly
692+
2. Add Version String and Change_Interval example to show how to change TimerInterval
693+
638694
### Releases v1.1.0
639695

640696
1. Restore cpp code besides Impl.h code to use if Multiple-Definition linker error.
@@ -668,7 +724,7 @@ Submit issues to: [ESP32TimerInterrupt issues](https://github.com/khoih-prog/ESP
668724
## TO DO
669725

670726
1. Search for bug and improvement.
671-
2. Similar features for remaining Arduino boards suh as SAMD21, SAMD51, SAM-DUE, nRF52
727+
2. Similar features for remaining Arduino boards such as SAMD21, SAMD51, SAM-DUE, nRF52
672728

673729

674730
## DONE

examples/Argument_None/Argument_None.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
Based on BlynkTimer.h
2525
Author: Volodymyr Shymanskyy
2626
27-
Version: 1.1.0
27+
Version: 1.1.1
2828
2929
Version Modified By Date Comments
3030
------- ----------- ---------- -----------
@@ -33,6 +33,7 @@
3333
1.0.2 K.Hoang 03/12/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
3434
1.0.3 K.Hoang 17/05/2020 Restructure code. Add examples. Enhance README.
3535
1.1.0 K.Hoang 27/10/2020 Restore cpp code besides Impl.h code to use if Multiple-Definition linker error.
36+
1.1.1 K.Hoang 06/12/2020 Add Version String and Change_Interval example to show how to change TimerInterval
3637
*****************************************************************************************************************************/
3738

3839
/*
@@ -119,6 +120,8 @@ void setup()
119120
delay(100);
120121

121122
Serial.println("\nStarting Argument_None on " + String(ARDUINO_BOARD));
123+
Serial.println(ESP32_TIMER_INTERRUPT_VERSION);
124+
Serial.printf("CPU Frequency = %ld MHz\n", F_CPU / 1000000);
122125

123126
// Using ESP32 => 80 / 160 / 240MHz CPU clock ,
124127
// For 64-bit timer counter
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/****************************************************************************************************************************
2+
Change_Interval.ino
3+
For ESP32 boards
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/ESP32TimerInterrupt
7+
Licensed under MIT license
8+
9+
The ESP32 has two timer groups, each one with two general purpose hardware timers. All the timers are based on 64 bits
10+
counters and 16 bit prescalers. The timer counters can be configured to count up or down and support automatic reload
11+
and software reload. They can also generate alarms when they reach a specific value, defined by the software. The value
12+
of the counter can be read by the software program.
13+
14+
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
15+
unsigned long miliseconds), you just consume only one ESP32 timer and avoid conflicting with other cores' tasks.
16+
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
17+
Therefore, their executions are not blocked by bad-behaving functions / tasks.
18+
This important feature is absolutely necessary for mission-critical tasks.
19+
20+
Based on SimpleTimer - A timer library for Arduino.
21+
22+
Copyright (c) 2010 OTTOTECNICA Italy
23+
24+
Based on BlynkTimer.h
25+
Author: Volodymyr Shymanskyy
26+
27+
Version: 1.1.1
28+
29+
Version Modified By Date Comments
30+
------- ----------- ---------- -----------
31+
1.0.0 K Hoang 23/11/2019 Initial coding
32+
1.0.1 K Hoang 27/11/2019 No v1.0.1. Bump up to 1.0.2 to match ESP8266_ISR_TimerInterupt library
33+
1.0.2 K.Hoang 03/12/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
34+
1.0.3 K.Hoang 17/05/2020 Restructure code. Add examples. Enhance README.
35+
1.1.0 K.Hoang 27/10/2020 Restore cpp code besides Impl.h code to use if Multiple-Definition linker error.
36+
1.1.1 K.Hoang 06/12/2020 Add Version String and Change_Interval example to show how to change TimerInterval
37+
*****************************************************************************************************************************/
38+
39+
/*
40+
Notes:
41+
Special design is necessary to share data between interrupt code and the rest of your program.
42+
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
43+
variable can not spontaneously change. Because your function may change variables while your program is using them,
44+
the compiler needs this hint. But volatile alone is often not enough.
45+
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
46+
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
47+
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
48+
or the entire sequence of your code which accesses the data.
49+
*/
50+
51+
#ifndef ESP32
52+
#error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting.
53+
#endif
54+
55+
// These define's must be placed at the beginning before #include "ESP32TimerInterrupt.h"
56+
// Don't define TIMER_INTERRUPT_DEBUG > 0. Only for special ISR debugging only. Can hang the system.
57+
#define TIMER_INTERRUPT_DEBUG 0
58+
59+
#include "ESP32TimerInterrupt.h"
60+
61+
#ifndef LED_BUILTIN
62+
#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
63+
#endif
64+
65+
#define PIN_D23 23 // Pin D23 mapped to pin GPIO23/VSPI_MOSI of ESP32
66+
67+
volatile uint32_t Timer0Count = 0;
68+
volatile uint32_t Timer1Count = 0;
69+
70+
void IRAM_ATTR TimerHandler0(void)
71+
{
72+
static bool toggle0 = false;
73+
74+
// Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR
75+
Timer0Count++;
76+
77+
#if (TIMER_INTERRUPT_DEBUG > 0)
78+
Serial.println("ITimer0: millis() = " + String(millis()));
79+
#endif
80+
81+
//timer interrupt toggles pin LED_BUILTIN
82+
digitalWrite(LED_BUILTIN, toggle0);
83+
toggle0 = !toggle0;
84+
}
85+
86+
void IRAM_ATTR TimerHandler1(void)
87+
{
88+
static bool toggle1 = false;
89+
90+
// Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR
91+
Timer1Count++;
92+
93+
#if (TIMER_INTERRUPT_DEBUG > 0)
94+
Serial.println("ITimer1: millis() = " + String(millis()));
95+
#endif
96+
97+
//timer interrupt toggles PIN_D23
98+
digitalWrite(PIN_D23, toggle1);
99+
toggle1 = !toggle1;
100+
}
101+
102+
void printResult(uint32_t currTime)
103+
{
104+
Serial.printf("Time = %ld, Timer0Count = %lu, , Timer1Count = %lu\n", currTime, Timer0Count, Timer1Count);
105+
}
106+
107+
#define TIMER0_INTERVAL_MS 500
108+
109+
#define TIMER1_INTERVAL_MS 1000
110+
111+
// Init ESP32 timer 0
112+
ESP32Timer ITimer0(0);
113+
ESP32Timer ITimer1(1);
114+
115+
void setup()
116+
{
117+
pinMode(LED_BUILTIN, OUTPUT);
118+
pinMode(PIN_D23, OUTPUT);
119+
120+
Serial.begin(115200);
121+
while (!Serial);
122+
123+
delay(100);
124+
125+
Serial.println("\nStarting Change_Interval on " + String(ARDUINO_BOARD));
126+
Serial.println(ESP32_TIMER_INTERRUPT_VERSION);
127+
Serial.printf("CPU Frequency = %ld MHz\n", F_CPU / 1000000);
128+
129+
// Interval in microsecs
130+
if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerHandler0))
131+
{
132+
Serial.printf("Starting ITimer0 OK, millis() = %ld\n", millis());
133+
}
134+
else
135+
Serial.println("Can't set ITimer0. Select another freq. or timer");
136+
137+
// Interval in microsecs
138+
if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000, TimerHandler1))
139+
{
140+
Serial.printf("Starting ITimer1 OK, millis() = %ld\n", millis());
141+
}
142+
else
143+
Serial.println("Can't set ITimer1. Select another freq. or timer");
144+
}
145+
146+
#define CHECK_INTERVAL_MS 10000L
147+
#define CHANGE_INTERVAL_MS 20000L
148+
149+
void loop()
150+
{
151+
static uint32_t lastTime = 0;
152+
static uint32_t lastChangeTime = 0;
153+
static uint32_t currTime;
154+
static uint32_t multFactor = 0;
155+
156+
currTime = millis();
157+
158+
if (currTime - lastTime > CHECK_INTERVAL_MS)
159+
{
160+
printResult(currTime);
161+
lastTime = currTime;
162+
163+
if (currTime - lastChangeTime > CHANGE_INTERVAL_MS)
164+
{
165+
//setInterval(unsigned long interval, timerCallback callback)
166+
multFactor = (multFactor + 1) % 2;
167+
168+
ITimer0.setInterval(TIMER0_INTERVAL_MS * 1000 * (multFactor + 1), TimerHandler0);
169+
ITimer1.setInterval(TIMER1_INTERVAL_MS * 1000 * (multFactor + 1), TimerHandler1);
170+
171+
Serial.printf("Changing Interval, Timer0 = %lu, Timer1 = %lu\n", TIMER0_INTERVAL_MS * (multFactor + 1), TIMER1_INTERVAL_MS * (multFactor + 1));
172+
173+
lastChangeTime = currTime;
174+
}
175+
}
176+
}

examples/ISR_RPM_Measure/ISR_RPM_Measure.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
Based on BlynkTimer.h
2525
Author: Volodymyr Shymanskyy
2626
27-
Version: 1.1.0
27+
Version: 1.1.1
2828
2929
Version Modified By Date Comments
3030
------- ----------- ---------- -----------
@@ -33,6 +33,7 @@
3333
1.0.2 K.Hoang 03/12/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
3434
1.0.3 K.Hoang 17/05/2020 Restructure code. Add examples. Enhance README.
3535
1.1.0 K.Hoang 27/10/2020 Restore cpp code besides Impl.h code to use if Multiple-Definition linker error.
36+
1.1.1 K.Hoang 06/12/2020 Add Version String and Change_Interval example to show how to change TimerInterval
3637
*****************************************************************************************************************************/
3738
/*
3839
Notes:
@@ -142,6 +143,8 @@ void setup()
142143
while (!Serial);
143144

144145
Serial.println("\nStarting ISR_RPM_Measure on " + String(ARDUINO_BOARD));
146+
Serial.println(ESP32_TIMER_INTERRUPT_VERSION);
147+
Serial.printf("CPU Frequency = %ld MHz\n", F_CPU / 1000000);
145148

146149
// Using ESP32 => 80 / 160 / 240MHz CPU clock ,
147150
// For 64-bit timer counter

examples/ISR_Switch/ISR_Switch.ino

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
Based on BlynkTimer.h
2525
Author: Volodymyr Shymanskyy
2626
27-
Version: 1.1.0
27+
Version: 1.1.1
2828
2929
Version Modified By Date Comments
3030
------- ----------- ---------- -----------
@@ -33,6 +33,7 @@
3333
1.0.2 K.Hoang 03/12/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
3434
1.0.3 K.Hoang 17/05/2020 Restructure code. Add examples. Enhance README.
3535
1.1.0 K.Hoang 27/10/2020 Restore cpp code besides Impl.h code to use if Multiple-Definition linker error.
36+
1.1.1 K.Hoang 06/12/2020 Add Version String and Change_Interval example to show how to change TimerInterval
3637
*****************************************************************************************************************************/
3738
/* Notes:
3839
Special design is necessary to share data between interrupt code and the rest of your program.
@@ -61,7 +62,9 @@
6162

6263
// These define's must be placed at the beginning before #include "ESP32TimerInterrupt.h"
6364
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
64-
#define TIMER_INTERRUPT_DEBUG 1
65+
#define TIMER_INTERRUPT_DEBUG 0
66+
67+
#include "ESP32TimerInterrupt.h"
6568

6669
#define BLYNK_PRINT Serial
6770
//#define BLYNK_DEBUG true
@@ -251,6 +254,8 @@ void setup()
251254
while (!Serial);
252255

253256
Serial.println("\nStarting ISR_Switch on " + String(ARDUINO_BOARD));
257+
Serial.println(ESP32_TIMER_INTERRUPT_VERSION);
258+
Serial.printf("CPU Frequency = %ld MHz\n", F_CPU / 1000000);
254259

255260
LampState = false;
256261

0 commit comments

Comments
 (0)