@@ -226,7 +226,7 @@ static void USE_IRAM_ATTR gpio_intr() {
226
226
// Reset the timeout.
227
227
//
228
228
#if _ESP32_IRRECV_TIMER_HACK
229
- // The following three lines of code are the equiv of:
229
+ // The following three lines of code are the equivalent of:
230
230
// `timerWrite(timer, 0);`
231
231
// We can't call that routine safely from inside an ISR as that procedure
232
232
// is not stored in IRAM. Hence, we do it manually so that it's covered by
@@ -242,8 +242,17 @@ static void USE_IRAM_ATTR gpio_intr() {
242
242
// @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178
243
243
timer->dev ->config .alarm_en = 1 ;
244
244
#else // _ESP32_IRRECV_TIMER_HACK
245
- timerWrite (timer, 0 );
246
- timerAlarmEnable (timer);
245
+ // Check the ESP32 core version
246
+ #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
247
+ // For ESP32 core version 3.x, replace `timerAlarmEnable`
248
+ timerWrite (timer, 0 );
249
+ timerAlarm (timer, 0 , true , 0 ); // Use the updated function with all arguments
250
+ #else
251
+ // For ESP32 core version 2.x, keep using `timerAlarmEnable`
252
+ timerWrite (timer, 0 );
253
+ timerAlarmEnable (timer);
254
+ #endif
255
+
247
256
#endif // _ESP32_IRRECV_TIMER_HACK
248
257
#endif // ESP32
249
258
}
@@ -356,23 +365,39 @@ void IRrecv::enableIRIn(const bool pullup) {
356
365
pinMode (params.recvpin , INPUT);
357
366
#endif // UNIT_TEST
358
367
}
368
+
359
369
#if defined(ESP32)
360
370
// Initialise the ESP32 timer.
361
371
// 80MHz / 80 = 1 uSec granularity.
362
- timer = timerBegin (_timer_num, 80 , true );
372
+ // Check for ESP32 core version and handle timerBegin differently for each version
373
+ #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
374
+ // For ESP32 core version 3.x (three arguments)
375
+ timer = timerBegin (_timer_num);
376
+ #else
377
+ // For ESP32 core version 2.0.x (one argument)
378
+ timer = timerBegin (_timer_num, 80 , true );
379
+ #endif
380
+
363
381
#ifdef DEBUG
364
382
if (timer == NULL ) {
365
383
DPRINT (" FATAL: Unable enable system timer: " );
366
384
DPRINTLN ((uint16_t )_timer_num);
367
385
}
368
386
#endif // DEBUG
369
387
assert (timer != NULL ); // Check we actually got the timer.
370
- // Set the timer so it only fires once, and set it's trigger in uSeconds.
371
- timerAlarmWrite (timer, MS_TO_USEC (params.timeout ), ONCE);
388
+
389
+ // Set the timer so it only fires once, and set its trigger in microseconds.
390
+ #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
391
+ // For ESP32 core version 3.x (use timerWrite)
392
+ timerWrite (timer, 0 ); // Reset the timer
393
+ timerAttachInterrupt (timer, &read_timeout);
394
+ #else
395
+ timerAlarmWrite (timer, MS_TO_USEC (params.timeout ), ONCE);
396
+ timerAttachInterrupt (timer, &read_timeout, false );
397
+ #endif
372
398
// Note: Interrupt needs to be attached before it can be enabled or disabled.
373
399
// Note: EDGE (true) is not supported, use LEVEL (false). Ref: #1713
374
- // See: https://github.com/espressif/arduino-esp32/blob/caef4006af491130136b219c1205bdcf8f08bf2b/cores/esp32/esp32-hal-timer.c#L224-L227
375
- timerAttachInterrupt (timer, &read_timeout, false );
400
+
376
401
#endif // ESP32
377
402
378
403
// Initialise state machine variables
@@ -398,9 +423,18 @@ void IRrecv::disableIRIn(void) {
398
423
os_timer_disarm (&timer);
399
424
#endif // ESP8266
400
425
#if defined(ESP32)
401
- timerAlarmDisable (timer);
402
- timerDetachInterrupt (timer);
403
- timerEnd (timer);
426
+ // Check for ESP32 core version and handle timer functions differently
427
+ #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
428
+ // For ESP32 core version 3.x
429
+ timerWrite (timer, 0 ); // Reset the timer
430
+ timerDetachInterrupt (timer); // Detach the interrupt
431
+ timerEnd (timer); // End the timer
432
+ #else
433
+ // For ESP32 core version 2.x
434
+ timerAlarmDisable (timer); // Disable the alarm
435
+ timerDetachInterrupt (timer); // Detach the interrupt
436
+ timerEnd (timer); // End the timer
437
+ #endif
404
438
#endif // ESP32
405
439
detachInterrupt (params.recvpin );
406
440
#endif // UNIT_TEST
@@ -426,7 +460,16 @@ void IRrecv::resume(void) {
426
460
params.rawlen = 0 ;
427
461
params.overflow = false ;
428
462
#if defined(ESP32)
429
- timerAlarmDisable (timer);
463
+ // Check for ESP32 core version and handle timer functions differently
464
+ #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
465
+ // For ESP32 core version 3.x
466
+ timerWrite (timer, 0 ); // Reset the timer (no need for timerAlarmDisable)
467
+ #else
468
+ // For ESP32 core version 2.x
469
+ timerAlarmDisable (timer); // Disable the alarm
470
+ #endif
471
+
472
+ // Re-enable GPIO interrupt in both versions
430
473
gpio_intr_enable ((gpio_num_t )params.recvpin );
431
474
#endif // ESP32
432
475
}
0 commit comments