From c5286115adf0bb8e3a99ead6ef59dda90db367f5 Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Fri, 6 Sep 2024 17:57:19 +0300 Subject: [PATCH 01/14] Update IRrecv.cpp --- src/IRrecv.cpp | 67 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 316dfc149..832957695 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -226,7 +226,7 @@ static void USE_IRAM_ATTR gpio_intr() { // Reset the timeout. // #if _ESP32_IRRECV_TIMER_HACK - // The following three lines of code are the equiv of: + // The following three lines of code are the equivalent of: // `timerWrite(timer, 0);` // We can't call that routine safely from inside an ISR as that procedure // 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() { // @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178 timer->dev->config.alarm_en = 1; #else // _ESP32_IRRECV_TIMER_HACK - timerWrite(timer, 0); - timerAlarmEnable(timer); + // Check the ESP32 core version + #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) + // For ESP32 core version 3.x, replace `timerAlarmEnable` + timerWrite(timer, 0); + timerAlarm(timer, 0, true, 0); // Use the updated function with all arguments + #else + // For ESP32 core version 2.x, keep using `timerAlarmEnable` + timerWrite(timer, 0); + timerAlarmEnable(timer); + #endif + #endif // _ESP32_IRRECV_TIMER_HACK #endif // ESP32 } @@ -356,10 +365,19 @@ void IRrecv::enableIRIn(const bool pullup) { pinMode(params.recvpin, INPUT); #endif // UNIT_TEST } + #if defined(ESP32) // Initialise the ESP32 timer. // 80MHz / 80 = 1 uSec granularity. - timer = timerBegin(_timer_num, 80, true); + // Check for ESP32 core version and handle timerBegin differently for each version + #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) + // For ESP32 core version 3.x (three arguments) + timer = timerBegin(_timer_num); + #else + // For ESP32 core version 2.0.x (one argument) + timer = timerBegin(_timer_num, 80, true); + #endif + #ifdef DEBUG if (timer == NULL) { DPRINT("FATAL: Unable enable system timer: "); @@ -367,12 +385,19 @@ void IRrecv::enableIRIn(const bool pullup) { } #endif // DEBUG assert(timer != NULL); // Check we actually got the timer. - // Set the timer so it only fires once, and set it's trigger in uSeconds. - timerAlarmWrite(timer, MS_TO_USEC(params.timeout), ONCE); + + // Set the timer so it only fires once, and set its trigger in microseconds. + #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) + // For ESP32 core version 3.x (use timerWrite) + timerWrite(timer, 0); // Reset the timer + timerAttachInterrupt(timer, &read_timeout); + #else + timerAlarmWrite(timer, MS_TO_USEC(params.timeout), ONCE); + timerAttachInterrupt(timer, &read_timeout, false); + #endif // Note: Interrupt needs to be attached before it can be enabled or disabled. // Note: EDGE (true) is not supported, use LEVEL (false). Ref: #1713 - // See: https://github.com/espressif/arduino-esp32/blob/caef4006af491130136b219c1205bdcf8f08bf2b/cores/esp32/esp32-hal-timer.c#L224-L227 - timerAttachInterrupt(timer, &read_timeout, false); + #endif // ESP32 // Initialise state machine variables @@ -398,9 +423,18 @@ void IRrecv::disableIRIn(void) { os_timer_disarm(&timer); #endif // ESP8266 #if defined(ESP32) - timerAlarmDisable(timer); - timerDetachInterrupt(timer); - timerEnd(timer); + // Check for ESP32 core version and handle timer functions differently + #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) + // For ESP32 core version 3.x + timerWrite(timer, 0); // Reset the timer + timerDetachInterrupt(timer); // Detach the interrupt + timerEnd(timer); // End the timer + #else + // For ESP32 core version 2.x + timerAlarmDisable(timer); // Disable the alarm + timerDetachInterrupt(timer); // Detach the interrupt + timerEnd(timer); // End the timer + #endif #endif // ESP32 detachInterrupt(params.recvpin); #endif // UNIT_TEST @@ -426,7 +460,16 @@ void IRrecv::resume(void) { params.rawlen = 0; params.overflow = false; #if defined(ESP32) - timerAlarmDisable(timer); + // Check for ESP32 core version and handle timer functions differently + #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) + // For ESP32 core version 3.x + timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable) + #else + // For ESP32 core version 2.x + timerAlarmDisable(timer); // Disable the alarm + #endif + + // Re-enable GPIO interrupt in both versions gpio_intr_enable((gpio_num_t)params.recvpin); #endif // ESP32 } From b6803d9cb7e806715bfa4f54e66f859a8cef29d4 Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Fri, 6 Sep 2024 18:05:15 +0300 Subject: [PATCH 02/14] fix 80 characters long --- src/IRrecv.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 832957695..27e975c0d 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -243,7 +243,8 @@ static void USE_IRAM_ATTR gpio_intr() { timer->dev->config.alarm_en = 1; #else // _ESP32_IRRECV_TIMER_HACK // Check the ESP32 core version - #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) +#if defined(ESP_ARDUINO_VERSION) && \ + (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // For ESP32 core version 3.x, replace `timerAlarmEnable` timerWrite(timer, 0); timerAlarm(timer, 0, true, 0); // Use the updated function with all arguments @@ -370,7 +371,8 @@ void IRrecv::enableIRIn(const bool pullup) { // Initialise the ESP32 timer. // 80MHz / 80 = 1 uSec granularity. // Check for ESP32 core version and handle timerBegin differently for each version - #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) +#if defined(ESP_ARDUINO_VERSION) && \ + (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // For ESP32 core version 3.x (three arguments) timer = timerBegin(_timer_num); #else @@ -387,7 +389,8 @@ void IRrecv::enableIRIn(const bool pullup) { assert(timer != NULL); // Check we actually got the timer. // Set the timer so it only fires once, and set its trigger in microseconds. - #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) +#if defined(ESP_ARDUINO_VERSION) && \ + (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // For ESP32 core version 3.x (use timerWrite) timerWrite(timer, 0); // Reset the timer timerAttachInterrupt(timer, &read_timeout); @@ -424,7 +427,8 @@ void IRrecv::disableIRIn(void) { #endif // ESP8266 #if defined(ESP32) // Check for ESP32 core version and handle timer functions differently - #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) +#if defined(ESP_ARDUINO_VERSION) && \ + (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // For ESP32 core version 3.x timerWrite(timer, 0); // Reset the timer timerDetachInterrupt(timer); // Detach the interrupt @@ -461,7 +465,8 @@ void IRrecv::resume(void) { params.overflow = false; #if defined(ESP32) // Check for ESP32 core version and handle timer functions differently - #if defined(ESP_ARDUINO_VERSION) && (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) +#if defined(ESP_ARDUINO_VERSION) && \ + (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // For ESP32 core version 3.x timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable) #else From 5bffe277c7b6acf1e44bb4f0de40c24b1bb9ffe5 Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Fri, 6 Sep 2024 18:07:42 +0300 Subject: [PATCH 03/14] fix whitespace --- src/IRrecv.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 27e975c0d..7ba3f46cc 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -375,7 +375,7 @@ void IRrecv::enableIRIn(const bool pullup) { (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // For ESP32 core version 3.x (three arguments) timer = timerBegin(_timer_num); - #else + #else // For ESP32 core version 2.0.x (one argument) timer = timerBegin(_timer_num, 80, true); #endif @@ -387,7 +387,6 @@ void IRrecv::enableIRIn(const bool pullup) { } #endif // DEBUG assert(timer != NULL); // Check we actually got the timer. - // Set the timer so it only fires once, and set its trigger in microseconds. #if defined(ESP_ARDUINO_VERSION) && \ (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) From 93c84f31e28298d2b355bf3dfef1d02348b65c4c Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Fri, 6 Sep 2024 18:14:30 +0300 Subject: [PATCH 04/14] Update IRrecv.cpp --- src/IRrecv.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 7ba3f46cc..999b2caac 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -247,7 +247,7 @@ static void USE_IRAM_ATTR gpio_intr() { (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // For ESP32 core version 3.x, replace `timerAlarmEnable` timerWrite(timer, 0); - timerAlarm(timer, 0, true, 0); // Use the updated function with all arguments + timerAlarm(timer, 0, true, 0); // Use the updated function #else // For ESP32 core version 2.x, keep using `timerAlarmEnable` timerWrite(timer, 0); @@ -370,7 +370,7 @@ void IRrecv::enableIRIn(const bool pullup) { #if defined(ESP32) // Initialise the ESP32 timer. // 80MHz / 80 = 1 uSec granularity. - // Check for ESP32 core version and handle timerBegin differently for each version + // Check for ESP32 core version and handle timerBegin differently #if defined(ESP_ARDUINO_VERSION) && \ (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // For ESP32 core version 3.x (three arguments) From f0e932d06001ebf82945bb67db75aebc64398f50 Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Sun, 8 Sep 2024 16:11:03 +0300 Subject: [PATCH 05/14] Update IRrecv.cpp --- src/IRrecv.cpp | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 999b2caac..4e9389cd4 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -134,7 +134,8 @@ typedef struct hw_timer_s { #endif // _ESP32_IRRECV_TIMER_HACK / End of Horrible Hack. namespace _IRrecv { -static hw_timer_t * timer = NULL; +static hw_timer_t *timer = NULL; // Declare ESP32 timer variable + } // namespace _IRrecv #endif // ESP32 using _IRrecv::timer; @@ -247,7 +248,9 @@ static void USE_IRAM_ATTR gpio_intr() { (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // For ESP32 core version 3.x, replace `timerAlarmEnable` timerWrite(timer, 0); - timerAlarm(timer, 0, true, 0); // Use the updated function + uint64_t alarm_value = 50000; // Example value (50ms) + timerAlarm(timer, alarm_value, false, 0); // Disable auto-reload + #else // For ESP32 core version 2.x, keep using `timerAlarmEnable` timerWrite(timer, 0); @@ -369,17 +372,22 @@ void IRrecv::enableIRIn(const bool pullup) { #if defined(ESP32) // Initialise the ESP32 timer. - // 80MHz / 80 = 1 uSec granularity. - // Check for ESP32 core version and handle timerBegin differently #if defined(ESP_ARDUINO_VERSION) && \ (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) - // For ESP32 core version 3.x (three arguments) - timer = timerBegin(_timer_num); - #else - // For ESP32 core version 2.0.x (one argument) - timer = timerBegin(_timer_num, 80, true); - #endif + // Use newer timerBegin signature for ESP32 core version 3.x + timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick) + Serial.println("Starting timer initialization..."); + Serial.print("Timer number: "); + Serial.println(_timer_num); + Serial.println((uint16_t)_timer_num); +#else + // Initialise the ESP32 timer. + // 80MHz / 80 = 1 uSec granularity. + // Check for ESP32 core version and handle timerBegin differently + timer = timerBegin(_timer_num, 80, true); +#endif + // Ensure the timer is successfully initialized #ifdef DEBUG if (timer == NULL) { DPRINT("FATAL: Unable enable system timer: "); @@ -390,15 +398,13 @@ void IRrecv::enableIRIn(const bool pullup) { // Set the timer so it only fires once, and set its trigger in microseconds. #if defined(ESP_ARDUINO_VERSION) && \ (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) - // For ESP32 core version 3.x (use timerWrite) - timerWrite(timer, 0); // Reset the timer + timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x timerAttachInterrupt(timer, &read_timeout); - #else - timerAlarmWrite(timer, MS_TO_USEC(params.timeout), ONCE); +#else + // Attach timer interrupt for core version 2.x + timerAlarmWrite(timer, MS_TO_USEC(params.timeout), true); timerAttachInterrupt(timer, &read_timeout, false); - #endif - // Note: Interrupt needs to be attached before it can be enabled or disabled. - // Note: EDGE (true) is not supported, use LEVEL (false). Ref: #1713 +#endif #endif // ESP32 From a67832ce449c38f7d55ccd956fd0ed5b8a621dc9 Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Tue, 17 Sep 2024 10:00:59 +0300 Subject: [PATCH 06/14] Remove Serial Debug --- src/IRrecv.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 4e9389cd4..48255e2bc 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -376,10 +376,6 @@ void IRrecv::enableIRIn(const bool pullup) { (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) // Use newer timerBegin signature for ESP32 core version 3.x timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick) - Serial.println("Starting timer initialization..."); - Serial.print("Timer number: "); - Serial.println(_timer_num); - Serial.println((uint16_t)_timer_num); #else // Initialise the ESP32 timer. // 80MHz / 80 = 1 uSec granularity. From 5d9da884a855c6911520eb2fe68e9253880d3444 Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Tue, 17 Sep 2024 13:54:09 +0300 Subject: [PATCH 07/14] Update IRrecv.cpp --- src/IRrecv.cpp | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 48255e2bc..cdf002a7e 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -69,6 +69,14 @@ static ETSTimer timer; #endif // Version check #endif // !defined(_ESP32_IRRECV_TIMER_HACK) +// Define ARDUINO_COREV3 macro +#if defined(ESP_ARDUINO_VERSION) && \ + (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) + #define ARDUINO_COREV3 1 +#else + #define ARDUINO_COREV3 0 +#endif + #if _ESP32_IRRECV_TIMER_HACK // Required structs/types from: // https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L28-L58 @@ -242,21 +250,15 @@ static void USE_IRAM_ATTR gpio_intr() { // @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350 // @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178 timer->dev->config.alarm_en = 1; -#else // _ESP32_IRRECV_TIMER_HACK - // Check the ESP32 core version -#if defined(ESP_ARDUINO_VERSION) && \ - (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) - // For ESP32 core version 3.x, replace `timerAlarmEnable` - timerWrite(timer, 0); - uint64_t alarm_value = 50000; // Example value (50ms) - timerAlarm(timer, alarm_value, false, 0); // Disable auto-reload - - #else +#elif ARDUINO_COREV3 + // For ESP32 core version 3.x, replace `timerAlarmEnable` + timerWrite(timer, 0); + uint64_t alarm_value = 50000; // Example value (50ms) + timerAlarm(timer, alarm_value, false, 0); // Disable auto-reloadmer, alarm_value, false, 0); // Disable auto-reload +#else // For ESP32 core version 2.x, keep using `timerAlarmEnable` timerWrite(timer, 0); timerAlarmEnable(timer); - #endif - #endif // _ESP32_IRRECV_TIMER_HACK #endif // ESP32 } @@ -372,15 +374,12 @@ void IRrecv::enableIRIn(const bool pullup) { #if defined(ESP32) // Initialise the ESP32 timer. -#if defined(ESP_ARDUINO_VERSION) && \ - (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) - // Use newer timerBegin signature for ESP32 core version 3.x - timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick) +#if ARDUINO_COREV3 + // Use newer timerBegin signature for ESP32 core version 3.x + timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick) #else - // Initialise the ESP32 timer. - // 80MHz / 80 = 1 uSec granularity. - // Check for ESP32 core version and handle timerBegin differently - timer = timerBegin(_timer_num, 80, true); + // Fallback for ESP32 core version 2.x or earlier + timer = timerBegin(0, 1000000, true); // Old signature with divider #endif // Ensure the timer is successfully initialized @@ -392,8 +391,7 @@ void IRrecv::enableIRIn(const bool pullup) { #endif // DEBUG assert(timer != NULL); // Check we actually got the timer. // Set the timer so it only fires once, and set its trigger in microseconds. -#if defined(ESP_ARDUINO_VERSION) && \ - (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) +#if ARDUINO_COREV3 timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x timerAttachInterrupt(timer, &read_timeout); #else @@ -428,8 +426,7 @@ void IRrecv::disableIRIn(void) { #endif // ESP8266 #if defined(ESP32) // Check for ESP32 core version and handle timer functions differently -#if defined(ESP_ARDUINO_VERSION) && \ - (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) + #if ARDUINO_COREV3 // For ESP32 core version 3.x timerWrite(timer, 0); // Reset the timer timerDetachInterrupt(timer); // Detach the interrupt From 1b0f35f7bc13c0d8ac59f8e45d7aa8205c7ce637 Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Tue, 17 Sep 2024 14:12:50 +0300 Subject: [PATCH 08/14] Update IRrecv.cpp --- src/IRrecv.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index cdf002a7e..09bacc845 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -254,7 +254,7 @@ static void USE_IRAM_ATTR gpio_intr() { // For ESP32 core version 3.x, replace `timerAlarmEnable` timerWrite(timer, 0); uint64_t alarm_value = 50000; // Example value (50ms) - timerAlarm(timer, alarm_value, false, 0); // Disable auto-reloadmer, alarm_value, false, 0); // Disable auto-reload + timerAlarm(timer, alarm_value, false, 0); #else // For ESP32 core version 2.x, keep using `timerAlarmEnable` timerWrite(timer, 0); From 1aaef348f4a004f4cea17c5fdef73547629b12b8 Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Thu, 19 Sep 2024 08:49:54 +0300 Subject: [PATCH 09/14] Update IRrecv.cpp --- src/IRrecv.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 09bacc845..b88b6ccf7 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -75,7 +75,7 @@ static ETSTimer timer; #define ARDUINO_COREV3 1 #else #define ARDUINO_COREV3 0 -#endif +#endif // defined(ESP_ARDUINO_VERSION) #if _ESP32_IRRECV_TIMER_HACK // Required structs/types from: @@ -380,7 +380,7 @@ void IRrecv::enableIRIn(const bool pullup) { #else // Fallback for ESP32 core version 2.x or earlier timer = timerBegin(0, 1000000, true); // Old signature with divider -#endif +#endif // ARDUINO_COREV3 // Ensure the timer is successfully initialized #ifdef DEBUG @@ -398,7 +398,7 @@ void IRrecv::enableIRIn(const bool pullup) { // Attach timer interrupt for core version 2.x timerAlarmWrite(timer, MS_TO_USEC(params.timeout), true); timerAttachInterrupt(timer, &read_timeout, false); -#endif +#endif // ARDUINO_COREV3 #endif // ESP32 @@ -463,14 +463,13 @@ void IRrecv::resume(void) { params.overflow = false; #if defined(ESP32) // Check for ESP32 core version and handle timer functions differently -#if defined(ESP_ARDUINO_VERSION) && \ - (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) + #if ARDUINO_COREV3 // For ESP32 core version 3.x timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable) #else // For ESP32 core version 2.x timerAlarmDisable(timer); // Disable the alarm - #endif + #endif // ARDUINO_COREV3 // Re-enable GPIO interrupt in both versions gpio_intr_enable((gpio_num_t)params.recvpin); From eac0e4d3dbdc70dc5ba1803a7c0c22f5add952ed Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Thu, 19 Sep 2024 08:52:49 +0300 Subject: [PATCH 10/14] fix whitespace --- src/IRrecv.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index b88b6ccf7..db6609406 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -75,7 +75,7 @@ static ETSTimer timer; #define ARDUINO_COREV3 1 #else #define ARDUINO_COREV3 0 -#endif // defined(ESP_ARDUINO_VERSION) +#endif // defined(ESP_ARDUINO_VERSION) #if _ESP32_IRRECV_TIMER_HACK // Required structs/types from: @@ -380,7 +380,7 @@ void IRrecv::enableIRIn(const bool pullup) { #else // Fallback for ESP32 core version 2.x or earlier timer = timerBegin(0, 1000000, true); // Old signature with divider -#endif // ARDUINO_COREV3 +#endif // ARDUINO_COREV3 // Ensure the timer is successfully initialized #ifdef DEBUG @@ -398,7 +398,7 @@ void IRrecv::enableIRIn(const bool pullup) { // Attach timer interrupt for core version 2.x timerAlarmWrite(timer, MS_TO_USEC(params.timeout), true); timerAttachInterrupt(timer, &read_timeout, false); -#endif // ARDUINO_COREV3 +#endif // ARDUINO_COREV3 #endif // ESP32 @@ -469,7 +469,7 @@ void IRrecv::resume(void) { #else // For ESP32 core version 2.x timerAlarmDisable(timer); // Disable the alarm - #endif // ARDUINO_COREV3 + #endif // ARDUINO_COREV3 // Re-enable GPIO interrupt in both versions gpio_intr_enable((gpio_num_t)params.recvpin); From b542501fb6b15742233296905750ddc297a754ed Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Thu, 19 Sep 2024 10:41:39 +0300 Subject: [PATCH 11/14] Update IRrecv.cpp --- src/IRrecv.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index db6609406..099c4486b 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -379,7 +379,7 @@ void IRrecv::enableIRIn(const bool pullup) { timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick) #else // Fallback for ESP32 core version 2.x or earlier - timer = timerBegin(0, 1000000, true); // Old signature with divider + timer = timerBegin(_timer_num, 80, true); #endif // ARDUINO_COREV3 // Ensure the timer is successfully initialized From f858d28eea9690c176d162a4bc793173b673b36a Mon Sep 17 00:00:00 2001 From: "Christian I. Nilsson" Date: Tue, 17 Sep 2024 23:56:14 +0900 Subject: [PATCH 12/14] Minimize changes against master WIP for #2144 --- src/IRrecv.cpp | 87 +++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 51 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index 099c4486b..b16aaf55b 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -56,6 +56,10 @@ static ETSTimer timer; } // namespace _IRrecv #endif // ESP8266 #if defined(ESP32) +#if ( defined(ESP_ARDUINO_VERSION) && \ + (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) ) +#define _ESP32_ARDUINOV3 +#endif // ESP_ARDUINO_VERSION >= 3 // We need a horrible timer hack for ESP32 Arduino framework < v2.0.0 #if !defined(_ESP32_IRRECV_TIMER_HACK) // Version check @@ -69,14 +73,6 @@ static ETSTimer timer; #endif // Version check #endif // !defined(_ESP32_IRRECV_TIMER_HACK) -// Define ARDUINO_COREV3 macro -#if defined(ESP_ARDUINO_VERSION) && \ - (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) - #define ARDUINO_COREV3 1 -#else - #define ARDUINO_COREV3 0 -#endif // defined(ESP_ARDUINO_VERSION) - #if _ESP32_IRRECV_TIMER_HACK // Required structs/types from: // 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 { #endif // _ESP32_IRRECV_TIMER_HACK / End of Horrible Hack. namespace _IRrecv { -static hw_timer_t *timer = NULL; // Declare ESP32 timer variable - +static hw_timer_t *timer = NULL; } // namespace _IRrecv #endif // ESP32 using _IRrecv::timer; @@ -250,15 +245,14 @@ static void USE_IRAM_ATTR gpio_intr() { // @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350 // @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178 timer->dev->config.alarm_en = 1; -#elif ARDUINO_COREV3 +#elif defined(_ESP32_ARDUINOV3) // For ESP32 core version 3.x, replace `timerAlarmEnable` timerWrite(timer, 0); uint64_t alarm_value = 50000; // Example value (50ms) timerAlarm(timer, alarm_value, false, 0); -#else - // For ESP32 core version 2.x, keep using `timerAlarmEnable` - timerWrite(timer, 0); - timerAlarmEnable(timer); +#else // !_ESP32_ARDUINOV3 + timerWrite(timer, 0); + timerAlarmEnable(timer); #endif // _ESP32_IRRECV_TIMER_HACK #endif // ESP32 } @@ -371,16 +365,15 @@ void IRrecv::enableIRIn(const bool pullup) { pinMode(params.recvpin, INPUT); #endif // UNIT_TEST } - #if defined(ESP32) // Initialise the ESP32 timer. -#if ARDUINO_COREV3 +#if defined(_ESP32_ARDUINOV3) // Use newer timerBegin signature for ESP32 core version 3.x timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick) -#else - // Fallback for ESP32 core version 2.x or earlier +#else // _ESP32_ARDUINOV3 + // 80MHz / 80 = 1 uSec granularity. timer = timerBegin(_timer_num, 80, true); -#endif // ARDUINO_COREV3 +#endif // _ESP32_ARDUINOV3 // Ensure the timer is successfully initialized #ifdef DEBUG @@ -391,15 +384,16 @@ void IRrecv::enableIRIn(const bool pullup) { #endif // DEBUG assert(timer != NULL); // Check we actually got the timer. // Set the timer so it only fires once, and set its trigger in microseconds. -#if ARDUINO_COREV3 - timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x - timerAttachInterrupt(timer, &read_timeout); -#else - // Attach timer interrupt for core version 2.x - timerAlarmWrite(timer, MS_TO_USEC(params.timeout), true); - timerAttachInterrupt(timer, &read_timeout, false); -#endif // ARDUINO_COREV3 - +#if defined(_ESP32_ARDUINOV3) + timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x + timerAttachInterrupt(timer, &read_timeout); +#else // _ESP32_ARDUINOV3 + timerAlarmWrite(timer, MS_TO_USEC(params.timeout), ONCE); + // Note: Interrupt needs to be attached before it can be enabled or disabled. + // Note: EDGE (true) is not supported, use LEVEL (false). Ref: #1713 + // See: https://github.com/espressif/arduino-esp32/blob/caef4006af491130136b219c1205bdcf8f08bf2b/cores/esp32/esp32-hal-timer.c#L224-L227 + timerAttachInterrupt(timer, &read_timeout, false); +#endif // _ESP32_ARDUINOV3 #endif // ESP32 // Initialise state machine variables @@ -423,20 +417,14 @@ void IRrecv::disableIRIn(void) { #ifndef UNIT_TEST #if defined(ESP8266) os_timer_disarm(&timer); -#endif // ESP8266 -#if defined(ESP32) - // Check for ESP32 core version and handle timer functions differently - #if ARDUINO_COREV3 - // For ESP32 core version 3.x - timerWrite(timer, 0); // Reset the timer - timerDetachInterrupt(timer); // Detach the interrupt - timerEnd(timer); // End the timer - #else - // For ESP32 core version 2.x - timerAlarmDisable(timer); // Disable the alarm - timerDetachInterrupt(timer); // Detach the interrupt - timerEnd(timer); // End the timer - #endif +#elif defined(_ESP32_ARDUINOV3) + timerWrite(timer, 0); // Reset the timer + timerDetachInterrupt(timer); + timerEnd(timer); +#elif defined(ESP32) + timerAlarmDisable(timer); + timerDetachInterrupt(timer); + timerEnd(timer); #endif // ESP32 detachInterrupt(params.recvpin); #endif // UNIT_TEST @@ -463,14 +451,11 @@ void IRrecv::resume(void) { params.overflow = false; #if defined(ESP32) // Check for ESP32 core version and handle timer functions differently - #if ARDUINO_COREV3 - // For ESP32 core version 3.x - timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable) - #else - // For ESP32 core version 2.x - timerAlarmDisable(timer); // Disable the alarm - #endif // ARDUINO_COREV3 - +#if defined(_ESP32_ARDUINOV3) + timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable) +#else // _ESP32_ARDUINOV3 + timerAlarmDisable(timer); +#endif // _ESP32_ARDUINOV3 // Re-enable GPIO interrupt in both versions gpio_intr_enable((gpio_num_t)params.recvpin); #endif // ESP32 From 1eb144366d62f6d05125bf4bfa5393e95f5e944b Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Sun, 27 Oct 2024 08:22:52 +0200 Subject: [PATCH 13/14] Arduino naming by version --- src/IRrecv.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index b16aaf55b..c37be944d 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -58,22 +58,22 @@ static ETSTimer timer; #if defined(ESP32) #if ( defined(ESP_ARDUINO_VERSION) && \ (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)) ) -#define _ESP32_ARDUINOV3 +#define _ESP32_ARDUINO_CORE_V3PLUS #endif // ESP_ARDUINO_VERSION >= 3 // We need a horrible timer hack for ESP32 Arduino framework < v2.0.0 -#if !defined(_ESP32_IRRECV_TIMER_HACK) +#if !defined(_ESP32_ARDUINO_CORE_V2PLUS) // Version check #if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) // No need for the hack if we are running version >= 2.0.0 -#define _ESP32_IRRECV_TIMER_HACK false +#define _ESP32_ARDUINO_CORE_V2PLUS false #else // Version check // If no ESP_ARDUINO_VERSION_MAJOR is defined, or less than 2, then we are // using an old ESP32 core, so we need the hack. -#define _ESP32_IRRECV_TIMER_HACK true +#define _ESP32_ARDUINO_CORE_V2PLUS true #endif // Version check -#endif // !defined(_ESP32_IRRECV_TIMER_HACK) +#endif // !defined(_ESP32_ARDUINO_CORE_V2PLUS) -#if _ESP32_IRRECV_TIMER_HACK +#if _ESP32_ARDUINO_CORE_V2PLUS // Required structs/types from: // https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L28-L58 // These are needed to be able to directly manipulate the timer registers from @@ -135,7 +135,7 @@ typedef struct hw_timer_s { uint8_t timer; portMUX_TYPE lock; } hw_timer_t; -#endif // _ESP32_IRRECV_TIMER_HACK / End of Horrible Hack. +#endif // _ESP32_ARDUINO_CORE_V2PLUS / End of Horrible Hack. namespace _IRrecv { static hw_timer_t *timer = NULL; @@ -229,7 +229,7 @@ static void USE_IRAM_ATTR gpio_intr() { #if defined(ESP32) // Reset the timeout. // -#if _ESP32_IRRECV_TIMER_HACK +#if _ESP32_ARDUINO_CORE_V2PLUS // The following three lines of code are the equivalent of: // `timerWrite(timer, 0);` // We can't call that routine safely from inside an ISR as that procedure @@ -245,15 +245,15 @@ static void USE_IRAM_ATTR gpio_intr() { // @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350 // @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L176-L178 timer->dev->config.alarm_en = 1; -#elif defined(_ESP32_ARDUINOV3) +#elif defined(_ESP32_ARDUINO_CORE_V3PLUS) // For ESP32 core version 3.x, replace `timerAlarmEnable` timerWrite(timer, 0); uint64_t alarm_value = 50000; // Example value (50ms) timerAlarm(timer, alarm_value, false, 0); -#else // !_ESP32_ARDUINOV3 +#else // !_ESP32_ARDUINO_CORE_V3PLUS timerWrite(timer, 0); timerAlarmEnable(timer); -#endif // _ESP32_IRRECV_TIMER_HACK +#endif // _ESP32_ARDUINO_CORE_V2PLUS #endif // ESP32 } #endif // UNIT_TEST @@ -367,13 +367,13 @@ void IRrecv::enableIRIn(const bool pullup) { } #if defined(ESP32) // Initialise the ESP32 timer. -#if defined(_ESP32_ARDUINOV3) +#if defined(_ESP32_ARDUINO_CORE_V3PLUS) // Use newer timerBegin signature for ESP32 core version 3.x timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick) -#else // _ESP32_ARDUINOV3 +#else // _ESP32_ARDUINO_CORE_V3PLUS // 80MHz / 80 = 1 uSec granularity. timer = timerBegin(_timer_num, 80, true); -#endif // _ESP32_ARDUINOV3 +#endif // _ESP32_ARDUINO_CORE_V3PLUS // Ensure the timer is successfully initialized #ifdef DEBUG @@ -384,16 +384,16 @@ void IRrecv::enableIRIn(const bool pullup) { #endif // DEBUG assert(timer != NULL); // Check we actually got the timer. // Set the timer so it only fires once, and set its trigger in microseconds. -#if defined(_ESP32_ARDUINOV3) +#if defined(_ESP32_ARDUINO_CORE_V3PLUS) timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x timerAttachInterrupt(timer, &read_timeout); -#else // _ESP32_ARDUINOV3 +#else // _ESP32_ARDUINO_CORE_V3PLUS timerAlarmWrite(timer, MS_TO_USEC(params.timeout), ONCE); // Note: Interrupt needs to be attached before it can be enabled or disabled. // Note: EDGE (true) is not supported, use LEVEL (false). Ref: #1713 // See: https://github.com/espressif/arduino-esp32/blob/caef4006af491130136b219c1205bdcf8f08bf2b/cores/esp32/esp32-hal-timer.c#L224-L227 timerAttachInterrupt(timer, &read_timeout, false); -#endif // _ESP32_ARDUINOV3 +#endif // _ESP32_ARDUINO_CORE_V3PLUS #endif // ESP32 // Initialise state machine variables @@ -417,7 +417,7 @@ void IRrecv::disableIRIn(void) { #ifndef UNIT_TEST #if defined(ESP8266) os_timer_disarm(&timer); -#elif defined(_ESP32_ARDUINOV3) +#elif defined(_ESP32_ARDUINO_CORE_V3PLUS) timerWrite(timer, 0); // Reset the timer timerDetachInterrupt(timer); timerEnd(timer); @@ -451,11 +451,11 @@ void IRrecv::resume(void) { params.overflow = false; #if defined(ESP32) // Check for ESP32 core version and handle timer functions differently -#if defined(_ESP32_ARDUINOV3) +#if defined(_ESP32_ARDUINO_CORE_V3PLUS) timerWrite(timer, 0); // Reset the timer (no need for timerAlarmDisable) -#else // _ESP32_ARDUINOV3 +#else // _ESP32_ARDUINO_CORE_V3PLUS timerAlarmDisable(timer); -#endif // _ESP32_ARDUINOV3 +#endif // _ESP32_ARDUINO_CORE_V3PLUS // Re-enable GPIO interrupt in both versions gpio_intr_enable((gpio_num_t)params.recvpin); #endif // ESP32 From 652921bb12866b838c15278468e9eb88b72a4e7e Mon Sep 17 00:00:00 2001 From: BorisKofman Date: Wed, 20 Nov 2024 10:55:42 +0200 Subject: [PATCH 14/14] Update IRrecv.cpp --- src/IRrecv.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IRrecv.cpp b/src/IRrecv.cpp index c37be944d..535578810 100644 --- a/src/IRrecv.cpp +++ b/src/IRrecv.cpp @@ -237,8 +237,8 @@ static void USE_IRAM_ATTR gpio_intr() { // USE_IRAM_ATTR in this ISR. // @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1350 // @see https://github.com/espressif/arduino-esp32/blob/6b0114366baf986c155e8173ab7c22bc0c5fcedc/cores/esp32/esp32-hal-timer.c#L106-L110 - timer->dev->load_high = (uint32_t) 0; - timer->dev->load_low = (uint32_t) 0; + timer->dev->load_high = static_cast(0); + timer->dev->load_low = static_cast(0); timer->dev->reload = 1; // The next line is the same, but instead replaces: // `timerAlarmEnable(timer);`