Skip to content

Commit c528611

Browse files
BorisKofmanBorisKofman
BorisKofman
authored and
BorisKofman
committed
Update IRrecv.cpp
1 parent 5406737 commit c528611

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

src/IRrecv.cpp

+55-12
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static void USE_IRAM_ATTR gpio_intr() {
226226
// Reset the timeout.
227227
//
228228
#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:
230230
// `timerWrite(timer, 0);`
231231
// We can't call that routine safely from inside an ISR as that procedure
232232
// 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() {
242242
// @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178
243243
timer->dev->config.alarm_en = 1;
244244
#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+
247256
#endif // _ESP32_IRRECV_TIMER_HACK
248257
#endif // ESP32
249258
}
@@ -356,23 +365,39 @@ void IRrecv::enableIRIn(const bool pullup) {
356365
pinMode(params.recvpin, INPUT);
357366
#endif // UNIT_TEST
358367
}
368+
359369
#if defined(ESP32)
360370
// Initialise the ESP32 timer.
361371
// 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+
363381
#ifdef DEBUG
364382
if (timer == NULL) {
365383
DPRINT("FATAL: Unable enable system timer: ");
366384
DPRINTLN((uint16_t)_timer_num);
367385
}
368386
#endif // DEBUG
369387
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
372398
// Note: Interrupt needs to be attached before it can be enabled or disabled.
373399
// 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+
376401
#endif // ESP32
377402

378403
// Initialise state machine variables
@@ -398,9 +423,18 @@ void IRrecv::disableIRIn(void) {
398423
os_timer_disarm(&timer);
399424
#endif // ESP8266
400425
#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
404438
#endif // ESP32
405439
detachInterrupt(params.recvpin);
406440
#endif // UNIT_TEST
@@ -426,7 +460,16 @@ void IRrecv::resume(void) {
426460
params.rawlen = 0;
427461
params.overflow = false;
428462
#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
430473
gpio_intr_enable((gpio_num_t)params.recvpin);
431474
#endif // ESP32
432475
}

0 commit comments

Comments
 (0)