@@ -56,6 +56,10 @@ static ETSTimer timer;
56
56
} // namespace _IRrecv
57
57
#endif // ESP8266
58
58
#if defined(ESP32)
59
+ #if ( defined(ESP_ARDUINO_VERSION) && \
60
+ (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3 , 0 , 0 )) )
61
+ #define _ESP32_ARDUINOV3
62
+ #endif // ESP_ARDUINO_VERSION >= 3
59
63
// We need a horrible timer hack for ESP32 Arduino framework < v2.0.0
60
64
#if !defined(_ESP32_IRRECV_TIMER_HACK)
61
65
// Version check
@@ -69,14 +73,6 @@ static ETSTimer timer;
69
73
#endif // Version check
70
74
#endif // !defined(_ESP32_IRRECV_TIMER_HACK)
71
75
72
- // Define ARDUINO_COREV3 macro
73
- #if defined(ESP_ARDUINO_VERSION) && \
74
- (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3 , 0 , 0 ))
75
- #define ARDUINO_COREV3 1
76
- #else
77
- #define ARDUINO_COREV3 0
78
- #endif // defined(ESP_ARDUINO_VERSION)
79
-
80
76
#if _ESP32_IRRECV_TIMER_HACK
81
77
// Required structs/types from:
82
78
// https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L28-L58
@@ -142,8 +138,7 @@ typedef struct hw_timer_s {
142
138
#endif // _ESP32_IRRECV_TIMER_HACK / End of Horrible Hack.
143
139
144
140
namespace _IRrecv {
145
- static hw_timer_t *timer = NULL ; // Declare ESP32 timer variable
146
-
141
+ static hw_timer_t *timer = NULL ;
147
142
} // namespace _IRrecv
148
143
#endif // ESP32
149
144
using _IRrecv::timer;
@@ -250,15 +245,14 @@ static void USE_IRAM_ATTR gpio_intr() {
250
245
// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350
251
246
// @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178
252
247
timer->dev ->config .alarm_en = 1 ;
253
- #elif ARDUINO_COREV3
248
+ #elif defined(_ESP32_ARDUINOV3)
254
249
// For ESP32 core version 3.x, replace `timerAlarmEnable`
255
250
timerWrite (timer, 0 );
256
251
uint64_t alarm_value = 50000 ; // Example value (50ms)
257
252
timerAlarm (timer, alarm_value, false , 0 );
258
- #else
259
- // For ESP32 core version 2.x, keep using `timerAlarmEnable`
260
- timerWrite (timer, 0 );
261
- timerAlarmEnable (timer);
253
+ #else // !_ESP32_ARDUINOV3
254
+ timerWrite (timer, 0 );
255
+ timerAlarmEnable (timer);
262
256
#endif // _ESP32_IRRECV_TIMER_HACK
263
257
#endif // ESP32
264
258
}
@@ -371,16 +365,15 @@ void IRrecv::enableIRIn(const bool pullup) {
371
365
pinMode (params.recvpin , INPUT);
372
366
#endif // UNIT_TEST
373
367
}
374
-
375
368
#if defined(ESP32)
376
369
// Initialise the ESP32 timer.
377
- #if ARDUINO_COREV3
370
+ #if defined(_ESP32_ARDUINOV3)
378
371
// Use newer timerBegin signature for ESP32 core version 3.x
379
372
timer = timerBegin (1000000 ); // Initialize with 1MHz (1us per tick)
380
- #else
381
- // Fallback for ESP32 core version 2.x or earlier
373
+ #else // _ESP32_ARDUINOV3
374
+ // 80MHz / 80 = 1 uSec granularity.
382
375
timer = timerBegin (_timer_num, 80 , true );
383
- #endif // ARDUINO_COREV3
376
+ #endif // _ESP32_ARDUINOV3
384
377
385
378
// Ensure the timer is successfully initialized
386
379
#ifdef DEBUG
@@ -391,15 +384,16 @@ void IRrecv::enableIRIn(const bool pullup) {
391
384
#endif // DEBUG
392
385
assert (timer != NULL ); // Check we actually got the timer.
393
386
// Set the timer so it only fires once, and set its trigger in microseconds.
394
- #if ARDUINO_COREV3
395
- timerWrite (timer, 0 ); // Reset the timer for ESP32 core version 3.x
396
- timerAttachInterrupt (timer, &read_timeout);
397
- #else
398
- // Attach timer interrupt for core version 2.x
399
- timerAlarmWrite (timer, MS_TO_USEC (params.timeout ), true );
400
- timerAttachInterrupt (timer, &read_timeout, false );
401
- #endif // ARDUINO_COREV3
402
-
387
+ #if defined(_ESP32_ARDUINOV3)
388
+ timerWrite (timer, 0 ); // Reset the timer for ESP32 core version 3.x
389
+ timerAttachInterrupt (timer, &read_timeout);
390
+ #else // _ESP32_ARDUINOV3
391
+ timerAlarmWrite (timer, MS_TO_USEC (params.timeout ), ONCE);
392
+ // Note: Interrupt needs to be attached before it can be enabled or disabled.
393
+ // Note: EDGE (true) is not supported, use LEVEL (false). Ref: #1713
394
+ // See: https://github.com/espressif/arduino-esp32/blob/caef4006af491130136b219c1205bdcf8f08bf2b/cores/esp32/esp32-hal-timer.c#L224-L227
395
+ timerAttachInterrupt (timer, &read_timeout, false );
396
+ #endif // _ESP32_ARDUINOV3
403
397
#endif // ESP32
404
398
405
399
// Initialise state machine variables
@@ -423,20 +417,14 @@ void IRrecv::disableIRIn(void) {
423
417
#ifndef UNIT_TEST
424
418
#if defined(ESP8266)
425
419
os_timer_disarm (&timer);
426
- #endif // ESP8266
427
- #if defined(ESP32)
428
- // Check for ESP32 core version and handle timer functions differently
429
- #if ARDUINO_COREV3
430
- // For ESP32 core version 3.x
431
- timerWrite (timer, 0 ); // Reset the timer
432
- timerDetachInterrupt (timer); // Detach the interrupt
433
- timerEnd (timer); // End the timer
434
- #else
435
- // For ESP32 core version 2.x
436
- timerAlarmDisable (timer); // Disable the alarm
437
- timerDetachInterrupt (timer); // Detach the interrupt
438
- timerEnd (timer); // End the timer
439
- #endif
420
+ #elif defined(_ESP32_ARDUINOV3)
421
+ timerWrite (timer, 0 ); // Reset the timer
422
+ timerDetachInterrupt (timer);
423
+ timerEnd (timer);
424
+ #elif defined(ESP32)
425
+ timerAlarmDisable (timer);
426
+ timerDetachInterrupt (timer);
427
+ timerEnd (timer);
440
428
#endif // ESP32
441
429
detachInterrupt (params.recvpin );
442
430
#endif // UNIT_TEST
@@ -463,14 +451,11 @@ void IRrecv::resume(void) {
463
451
params.overflow = false ;
464
452
#if defined(ESP32)
465
453
// Check for ESP32 core version and handle timer functions differently
466
- #if ARDUINO_COREV3
467
- // For ESP32 core version 3.x
468
- timerWrite (timer, 0 ); // Reset the timer (no need for timerAlarmDisable)
469
- #else
470
- // For ESP32 core version 2.x
471
- timerAlarmDisable (timer); // Disable the alarm
472
- #endif // ARDUINO_COREV3
473
-
454
+ #if defined(_ESP32_ARDUINOV3)
455
+ timerWrite (timer, 0 ); // Reset the timer (no need for timerAlarmDisable)
456
+ #else // _ESP32_ARDUINOV3
457
+ timerAlarmDisable (timer);
458
+ #endif // _ESP32_ARDUINOV3
474
459
// Re-enable GPIO interrupt in both versions
475
460
gpio_intr_enable ((gpio_num_t )params.recvpin );
476
461
#endif // ESP32
0 commit comments