11/* ***************************************************************************************************************************
22 Argument_None.ino
3- For ESP32 boards
3+ For ESP32, ESP32_S2, ESP32_S3, ESP32_C3 boards with ESP32 core v2.0.2+
44 Written by Khoi Hoang
55
66 Built by Khoi Hoang https://github.com/khoih-prog/ESP32TimerInterrupt
77 Licensed under MIT license
88
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.
9+ The ESP32, ESP32_S2, ESP32_S3, ESP32_C3 have two timer groups, TIMER_GROUP_0 and TIMER_GROUP_1
10+ 1) each group of ESP32, ESP32_S2, ESP32_S3 has two general purpose hardware timers, TIMER_0 and TIMER_1
11+ 2) each group of ESP32_C3 has ony one general purpose hardware timer, TIMER_0
12+
13+ All the timers are based on 64-bit counters (except 54-bit counter for ESP32_S3 counter) and 16 bit prescalers.
14+ The timer counters can be configured to count up or down and support automatic reload and software reload.
15+ They can also generate alarms when they reach a specific value, defined by the software.
16+ The value of the counter can be read by the software program.
1317
1418 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.
19+ unsigned long miliseconds), you just consume only one ESP32-S2 timer and avoid conflicting with other cores' tasks.
1620 The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1721 Therefore, their executions are not blocked by bad-behaving functions / tasks.
1822 This important feature is absolutely necessary for mission-critical tasks.
19-
20- Based on SimpleTimer - A timer library for Arduino.
21- Author: mromani@ottotecnica.com
22- Copyright (c) 2010 OTTOTECNICA Italy
23-
24- Based on BlynkTimer.h
25- Author: Volodymyr Shymanskyy
2623*****************************************************************************************************************************/
2724
2825/*
3734 or the entire sequence of your code which accesses the data.
3835*/
3936
40- #ifndef ESP32
41- #error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting.
42- #elif ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_ESP32S2_THING_PLUS || ARDUINO_MICROS2 || \
43- ARDUINO_METRO_ESP32S2 || ARDUINO_MAGTAG29_ESP32S2 || ARDUINO_FUNHOUSE_ESP32S2 || \
44- ARDUINO_ADAFRUIT_FEATHER_ESP32S2_NOPSRAM )
45- #define USING_ESP32_S2_TIMER_INTERRUPT true
37+ #if !defined( ESP32 )
38+ #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
4639#endif
4740
48- // These define's must be placed at the beginning before #include "TimerInterrupt_Generic .h"
41+ // These define's must be placed at the beginning before #include "_TIMERINTERRUPT_LOGLEVEL_ .h"
4942// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
50- // Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
51- #define TIMER_INTERRUPT_DEBUG 0
5243#define _TIMERINTERRUPT_LOGLEVEL_ 4
5344
45+ // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
5446#include " ESP32TimerInterrupt.h"
5547
5648#ifndef LED_BUILTIN
5749 #define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
5850#endif
5951
6052// Don't use PIN_D1 in core v2.0.0 and v2.0.1. Check https://github.com/espressif/arduino-esp32/issues/5868
61- #define PIN_D1 1 // Pin D1 mapped to pin GPIO1 of ESP32-S2
6253#define PIN_D2 2 // Pin D2 mapped to pin GPIO2/ADC12/TOUCH2/LED_BUILTIN of ESP32
6354#define PIN_D3 3 // Pin D3 mapped to pin GPIO3/RX0 of ESP32
6455
65- #if USING_ESP32_S2_TIMER_INTERRUPT
66- void IRAM_ATTR TimerHandler0 (void * timerNo)
67- #else
68- void IRAM_ATTR TimerHandler0 (void )
69- #endif
56+ // With core v2.0.0+, you can't use Serial.print/println in ISR or crash.
57+ // and you can't use float calculation inside ISR
58+ // Only OK in core v1.0.6-
59+ bool IRAM_ATTR TimerHandler0 (void * timerNo)
7060{
71- #if USING_ESP32_S2_TIMER_INTERRUPT
72- // ///////////////////////////////////////////////////////
73- // Always call this for ESP32-S2 before processing ISR
74- TIMER_ISR_START (timerNo);
75- // ///////////////////////////////////////////////////////
76- #endif
77-
7861 static bool toggle0 = false ;
79-
80- #if (TIMER_INTERRUPT_DEBUG > 0)
81- Serial.print (" ITimer0: millis() = " ); Serial.println (millis ());
82- #endif
62+ static bool started = false ;
8363
8464 // timer interrupt toggles pin LED_BUILTIN
8565 digitalWrite (LED_BUILTIN, toggle0);
8666 toggle0 = !toggle0;
8767
88- #if USING_ESP32_S2_TIMER_INTERRUPT
89- // ///////////////////////////////////////////////////////
90- // Always call this for ESP32-S2 after processing ISR
91- TIMER_ISR_END (timerNo);
92- // ///////////////////////////////////////////////////////
93- #endif
68+ return true ;
9469}
9570
96- #if USING_ESP32_S2_TIMER_INTERRUPT
97- void IRAM_ATTR TimerHandler1 (void * timerNo)
98- #else
99- void IRAM_ATTR TimerHandler1 (void )
100- #endif
71+ // With core v2.0.0+, you can't use Serial.print/println in ISR or crash.
72+ // and you can't use float calculation inside ISR
73+ // Only OK in core v1.0.6-
74+ bool IRAM_ATTR TimerHandler1 (void * timerNo)
10175{
102- #if USING_ESP32_S2_TIMER_INTERRUPT
10376 // ///////////////////////////////////////////////////////
104- // Always call this for ESP32-S2 before processing ISR
105- TIMER_ISR_START (timerNo);
106- // ///////////////////////////////////////////////////////
107- #endif
10877
10978 static bool toggle1 = false ;
110-
111- #if (TIMER_INTERRUPT_DEBUG > 0)
112- Serial.print (" ITimer1: millis() = " ); Serial.println (millis ());
113- #endif
79+ static bool started = false ;
11480
11581 // timer interrupt toggles outputPin
11682 digitalWrite (PIN_D3, toggle1);
11783 toggle1 = !toggle1;
11884
119- #if USING_ESP32_S2_TIMER_INTERRUPT
120- // ///////////////////////////////////////////////////////
121- // Always call this for ESP32-S2 after processing ISR
122- TIMER_ISR_END (timerNo);
123- // ///////////////////////////////////////////////////////
124- #endif
85+ return true ;
12586}
12687
12788#define TIMER0_INTERVAL_MS 1000
12889
12990#define TIMER1_INTERVAL_MS 5000
13091
131- // Init ESP32 timer 0
92+ // Init ESP32 timer 0 and 1
13293ESP32Timer ITimer0 (0 );
13394ESP32Timer ITimer1 (1 );
13495
@@ -140,16 +101,10 @@ void setup()
140101 Serial.begin (115200 );
141102 while (!Serial);
142103
143- delay (100 );
104+ delay (200 );
144105
145106 Serial.print (F (" \n Starting Argument_None on " )); Serial.println (ARDUINO_BOARD);
146-
147- #if USING_ESP32_S2_TIMER_INTERRUPT
148- Serial.println (ESP32_S2_TIMER_INTERRUPT_VERSION);
149- #else
150107 Serial.println (ESP32_TIMER_INTERRUPT_VERSION);
151- #endif
152-
153108 Serial.print (F (" CPU Frequency = " )); Serial.print (F_CPU / 1000000 ); Serial.println (F (" MHz" ));
154109
155110 // Using ESP32 => 80 / 160 / 240MHz CPU clock ,
@@ -158,22 +113,25 @@ void setup()
158113
159114 // Interval in microsecs
160115 if (ITimer0.attachInterruptInterval (TIMER0_INTERVAL_MS * 1000 , TimerHandler0))
116+ // if (ITimer0.attachInterrupt(1, TimerHandler0))
161117 {
162118 Serial.print (F (" Starting ITimer0 OK, millis() = " )); Serial.println (millis ());
163119 }
164120 else
165- Serial.println (F (" Can't set ITimer0. Select another freq. or timer" ));
121+ Serial.println (F (" Can't set ITimer0. Select another Timer, freq. or timer" ));
122+
166123
167124 // Interval in microsecs
168125 if (ITimer1.attachInterruptInterval (TIMER1_INTERVAL_MS * 1000 , TimerHandler1))
126+ // if (ITimer1.attachInterrupt(2, TimerHandler1))
169127 {
170128 Serial.print (F (" Starting ITimer1 OK, millis() = " )); Serial.println (millis ());
171129 }
172130 else
173- Serial.println (F (" Can't set ITimer1. Select another freq. or timer" ));
131+ Serial.println (F (" Can't set ITimer1. Select another Timer, freq. or timer" ));
174132}
175133
176134void loop ()
177135{
178-
136+ delay ( 1 );
179137}
0 commit comments