From 4ddeb3b9656e9cb260652c35579b3b4a70d9eedd Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Wed, 26 Jun 2024 11:02:16 -0300
Subject: [PATCH 01/11] feat(rmt): allow legacy driver

---
 cores/esp32/esp32-hal-gpio.c    | 4 +++-
 cores/esp32/esp32-hal-rgb-led.c | 3 +++
 cores/esp32/esp32-hal-rgb-led.h | 2 ++
 cores/esp32/esp32-hal-rmt.c     | 2 ++
 cores/esp32/esp32-hal-rmt.h     | 3 +++
 cores/esp32/io_pin_remap.h      | 2 ++
 6 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c
index b433adcc7fc..5270ff79910 100644
--- a/cores/esp32/esp32-hal-gpio.c
+++ b/cores/esp32/esp32-hal-gpio.c
@@ -151,6 +151,7 @@ uint8_t RGB_BUILTIN_storage = 0;
 #endif
 
 extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
+#ifndef NO_NEW_RMT_DRV
 #ifdef RGB_BUILTIN
   if (pin == RGB_BUILTIN) {
     //use RMT to set all channels on/off
@@ -159,7 +160,8 @@ extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
     neopixelWrite(RGB_BUILTIN, comm_val, comm_val, comm_val);
     return;
   }
-#endif
+#endif // RGB_BUILTIN
+#endif // NO_NEW_RMT_DRV
   if (perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) != NULL) {
     gpio_set_level((gpio_num_t)pin, val);
   } else {
diff --git a/cores/esp32/esp32-hal-rgb-led.c b/cores/esp32/esp32-hal-rgb-led.c
index 99c95e1943e..e89c3220376 100644
--- a/cores/esp32/esp32-hal-rgb-led.c
+++ b/cores/esp32/esp32-hal-rgb-led.c
@@ -1,5 +1,7 @@
 #include "soc/soc_caps.h"
 
+#ifndef NO_NEW_RMT_DRV
+
 #include "esp32-hal-rgb-led.h"
 
 void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val) {
@@ -40,3 +42,4 @@ void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue
   log_e("RMT is not supported on " CONFIG_IDF_TARGET);
 #endif /* SOC_RMT_SUPPORTED */
 }
+#endif /* NO_NEW_RMT_DRV */
\ No newline at end of file
diff --git a/cores/esp32/esp32-hal-rgb-led.h b/cores/esp32/esp32-hal-rgb-led.h
index 33f37c849b6..313c5887070 100644
--- a/cores/esp32/esp32-hal-rgb-led.h
+++ b/cores/esp32/esp32-hal-rgb-led.h
@@ -1,6 +1,7 @@
 #ifndef MAIN_ESP32_HAL_RGB_LED_H_
 #define MAIN_ESP32_HAL_RGB_LED_H_
 
+#ifndef NO_NEW_RMT_DRV
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -18,3 +19,4 @@ void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue
 #endif
 
 #endif /* MAIN_ESP32_HAL_RGB_LED_H_ */
+#endif /* NO_NEW_RMT_DRV */
\ No newline at end of file
diff --git a/cores/esp32/esp32-hal-rmt.c b/cores/esp32/esp32-hal-rmt.c
index e3877b5ff48..f77e673b711 100644
--- a/cores/esp32/esp32-hal-rmt.c
+++ b/cores/esp32/esp32-hal-rmt.c
@@ -13,6 +13,7 @@
 // limitations under the License.
 
 #include "soc/soc_caps.h"
+#ifndef NO_NEW_RMT_DRV
 
 #if SOC_RMT_SUPPORTED
 #include "esp32-hal.h"
@@ -628,3 +629,4 @@ bool rmtInit(int pin, rmt_ch_dir_t channel_direction, rmt_reserve_memsize_t mem_
 }
 
 #endif /* SOC_RMT_SUPPORTED */
+#endif /* NO_NEW_RMT_DRV */
\ No newline at end of file
diff --git a/cores/esp32/esp32-hal-rmt.h b/cores/esp32/esp32-hal-rmt.h
index c15eadfbcd1..0aa498c1489 100644
--- a/cores/esp32/esp32-hal-rmt.h
+++ b/cores/esp32/esp32-hal-rmt.h
@@ -16,6 +16,8 @@
 #define MAIN_ESP32_HAL_RMT_H_
 
 #include "soc/soc_caps.h"
+#ifndef NO_NEW_RMT_DRV
+
 #if SOC_RMT_SUPPORTED
 
 #ifdef __cplusplus
@@ -234,3 +236,4 @@ bool rmtDeinit(int pin);
 
 #endif /* SOC_RMT_SUPPORTED */
 #endif /* MAIN_ESP32_HAL_RMT_H_ */
+#endif /* NO_NEW_RMT_DRV */
\ No newline at end of file
diff --git a/cores/esp32/io_pin_remap.h b/cores/esp32/io_pin_remap.h
index 4e422632ea4..c768f2e1936 100644
--- a/cores/esp32/io_pin_remap.h
+++ b/cores/esp32/io_pin_remap.h
@@ -79,6 +79,7 @@ int8_t gpioNumberToDigitalPin(int8_t gpioNumber);
 // cores/esp32/esp32-hal-rgb-led.h
 #define neopixelWrite(pin, red_val, green_val, blue_val) neopixelWrite(digitalPinToGPIONumber(pin), red_val, green_val, blue_val)
 
+#ifndef NO_NEW_RMT_DRV
 // cores/esp32/esp32-hal-rmt.h
 #define rmtInit(pin, channel_direction, memsize, frequency_Hz) rmtInit(digitalPinToGPIONumber(pin), channel_direction, memsize, frequency_Hz)
 #define rmtSetEOT(pin, EOT_Level)                              rmtSetEOT(digitalPinToGPIONumber(pin), EOT_Level)
@@ -94,6 +95,7 @@ int8_t gpioNumberToDigitalPin(int8_t gpioNumber);
   rmtSetCarrier(digitalPinToGPIONumber(pin), carrier_en, carrier_level, frequency_Hz, duty_percent)
 #define rmtSetRxMinThreshold(pin, filter_pulse_ticks) rmtSetRxMinThreshold(digitalPinToGPIONumber(pin), filter_pulse_ticks)
 #define rmtDeinit(pin)                                rmtDeinit(digitalPinToGPIONumber(pin))
+#endif // NO_NEW_RMT_DRV
 
 // cores/esp32/esp32-hal-sigmadelta.h
 #define sigmaDeltaAttach(pin, freq) sigmaDeltaAttach(digitalPinToGPIONumber(pin), freq)

From b295f9c61a2db79fe4e21e51e347062f1a0ae64f Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Wed, 26 Jun 2024 12:36:42 -0300
Subject: [PATCH 02/11] feat(rmt): legacy driver example

---
 .../Legacy_RMT_Driver_Compatible.ino          | 39 +++++++++++++++++++
 .../Legacy_RMT_Driver_Compatible/build_opt.h  |  1 +
 2 files changed, 40 insertions(+)
 create mode 100644 libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
 create mode 100644 libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h

diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
new file mode 100644
index 00000000000..c222f89905c
--- /dev/null
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
@@ -0,0 +1,39 @@
+#ifndef NO_NEW_RMT_DRV
+// add the file "build_opt.h" to your Arduino project folder with "-DNO_NEW_RMT_DRV" to use the RMT Legacy driver
+#warning "NO_NEW_RMT_DRV is not defined, using new RMT driver"
+
+#define RMT_PIN 4  // Valid GPIO for ESP32, S2, S3, C3, C6 and H2
+bool installed = false;
+
+void setup() {
+  Serial.begin(115200);
+  Serial.println("This sketch uses the new RMT driver.");
+  installed = rmtInit(RMT_PIN, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000);
+}
+
+void loop() {
+  Serial.println("RMT New driver is installed: " + installed ? String("Yes.") : String("No."));
+  delay(5000);
+}
+
+#else
+
+// add the file "build_opt.h" to your Arduino project folder with "-DNO_NEW_RMT_DRV" to use the RMT Legacy driver
+// neoPixelWrite() is a function that writes to the RGB LED and it won't be available here
+#include "driver/rmt.h"
+
+bool installed = false;
+
+void setup() {
+  Serial.begin(115200);
+  Serial.println("This sketch is using the RMT Legacy driver.");
+  installed = rmt_driver_install(RMT_CHANNEL_0, 0, 0) == ESP_OK;
+}
+
+void loop() {
+  Serial.println("RMT Legacy driver is installed: " + installed ? String("Yes.") : String("No."));
+  delay(5000);
+}
+
+
+#endif
\ No newline at end of file
diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
new file mode 100644
index 00000000000..446cd08f071
--- /dev/null
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
@@ -0,0 +1 @@
+-DNO_NEW_RMT_DRV
\ No newline at end of file

From 362990521b3ae0f26bd6d41425d1dcd2af942e83 Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Wed, 26 Jun 2024 12:59:58 -0300
Subject: [PATCH 03/11] fix(rmt): legacy driver example

---
 .../Legacy_RMT_Driver_Compatible.ino          | 21 +++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
index c222f89905c..1166dae618c 100644
--- a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
@@ -1,4 +1,14 @@
+/*
+ * This example demonstrates how to use the file build_opt.h to disable the new RMT Driver
+ * Note that this file shall be added the Arduino project 
+ * 
+ * If the content of this file changes, it is necessary to delete the compiled Arduino IDE cache
+ * It can be done by changing, for instance, the "Core Debug Level" option, forcing the system to rebuild the Arduino Core
+ * 
+ */
+
 #ifndef NO_NEW_RMT_DRV
+
 // add the file "build_opt.h" to your Arduino project folder with "-DNO_NEW_RMT_DRV" to use the RMT Legacy driver
 #warning "NO_NEW_RMT_DRV is not defined, using new RMT driver"
 
@@ -12,7 +22,9 @@ void setup() {
 }
 
 void loop() {
-  Serial.println("RMT New driver is installed: " + installed ? String("Yes.") : String("No."));
+  String msg = "RMT New driver is installed: ";
+  msg += (char*)(installed ? "Yes." : "No.");
+  Serial.println(msg);
   delay(5000);
 }
 
@@ -31,9 +43,10 @@ void setup() {
 }
 
 void loop() {
-  Serial.println("RMT Legacy driver is installed: " + installed ? String("Yes.") : String("No."));
+  String msg = "RMT Legacy driver is installed: ";
+  msg += (char*)(installed ? "Yes." : "No.");
+  Serial.println(msg);
   delay(5000);
 }
 
-
-#endif
\ No newline at end of file
+#endif // NO_NEW_RMT_DRV
\ No newline at end of file

From a11b47320f63bc83516ee67093eb6aed58f8078a Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Wed, 26 Jun 2024 14:09:28 -0300
Subject: [PATCH 04/11] fix(rmt): ESP32_ARDUINO_NEW_RMT_DRV_OFF

---
 .../Legacy_RMT_Driver_Compatible.ino                   | 10 +++++-----
 .../RMT/Legacy_RMT_Driver_Compatible/build_opt.h       |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
index 1166dae618c..a7b62f1e05b 100644
--- a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
@@ -7,10 +7,10 @@
  * 
  */
 
-#ifndef NO_NEW_RMT_DRV
+#ifndef ESP32_ARDUIESP32_ARDUINO_NEW_RMT_DRV_OFF_OFF
 
-// add the file "build_opt.h" to your Arduino project folder with "-DNO_NEW_RMT_DRV" to use the RMT Legacy driver
-#warning "NO_NEW_RMT_DRV is not defined, using new RMT driver"
+// add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NEW_RMT_DRV_OFF" to use the RMT Legacy driver
+#warning "ESP32_ARDUINO_NEW_RMT_DRV_OFF is not defined, using new RMT driver"
 
 #define RMT_PIN 4  // Valid GPIO for ESP32, S2, S3, C3, C6 and H2
 bool installed = false;
@@ -30,7 +30,7 @@ void loop() {
 
 #else
 
-// add the file "build_opt.h" to your Arduino project folder with "-DNO_NEW_RMT_DRV" to use the RMT Legacy driver
+// add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NEW_RMT_DRV_OFF" to use the RMT Legacy driver
 // neoPixelWrite() is a function that writes to the RGB LED and it won't be available here
 #include "driver/rmt.h"
 
@@ -49,4 +49,4 @@ void loop() {
   delay(5000);
 }
 
-#endif // NO_NEW_RMT_DRV
\ No newline at end of file
+#endif // ESP32_ARDUINO_NEW_RMT_DRV_OFF
\ No newline at end of file
diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
index 446cd08f071..64238ad6cf1 100644
--- a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
@@ -1 +1 @@
--DNO_NEW_RMT_DRV
\ No newline at end of file
+-DESP32_ARDUINO_NEW_RMT_DRV_OFF
\ No newline at end of file

From 930681cf8aa1f1257af562cc8d3fe369889199a0 Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Wed, 26 Jun 2024 14:09:57 -0300
Subject: [PATCH 05/11] fix(rmt): ESP32_ARDUINO_NEW_RMT_DRV_OFF

---
 cores/esp32/esp32-hal-gpio.c    | 4 ++--
 cores/esp32/esp32-hal-rgb-led.c | 4 ++--
 cores/esp32/esp32-hal-rgb-led.h | 4 ++--
 cores/esp32/esp32-hal-rmt.c     | 4 ++--
 cores/esp32/esp32-hal-rmt.h     | 4 ++--
 cores/esp32/io_pin_remap.h      | 4 ++--
 6 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c
index 5270ff79910..3ccfdf9c1e5 100644
--- a/cores/esp32/esp32-hal-gpio.c
+++ b/cores/esp32/esp32-hal-gpio.c
@@ -151,7 +151,7 @@ uint8_t RGB_BUILTIN_storage = 0;
 #endif
 
 extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
-#ifndef NO_NEW_RMT_DRV
+#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 #ifdef RGB_BUILTIN
   if (pin == RGB_BUILTIN) {
     //use RMT to set all channels on/off
@@ -161,7 +161,7 @@ extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
     return;
   }
 #endif // RGB_BUILTIN
-#endif // NO_NEW_RMT_DRV
+#endif // ESP32_ARDUINO_NEW_RMT_DRV_OFF
   if (perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) != NULL) {
     gpio_set_level((gpio_num_t)pin, val);
   } else {
diff --git a/cores/esp32/esp32-hal-rgb-led.c b/cores/esp32/esp32-hal-rgb-led.c
index e89c3220376..befc204b651 100644
--- a/cores/esp32/esp32-hal-rgb-led.c
+++ b/cores/esp32/esp32-hal-rgb-led.c
@@ -1,6 +1,6 @@
 #include "soc/soc_caps.h"
 
-#ifndef NO_NEW_RMT_DRV
+#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 
 #include "esp32-hal-rgb-led.h"
 
@@ -42,4 +42,4 @@ void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue
   log_e("RMT is not supported on " CONFIG_IDF_TARGET);
 #endif /* SOC_RMT_SUPPORTED */
 }
-#endif /* NO_NEW_RMT_DRV */
\ No newline at end of file
+#endif /* ESP32_ARDUINO_NEW_RMT_DRV_OFF */
\ No newline at end of file
diff --git a/cores/esp32/esp32-hal-rgb-led.h b/cores/esp32/esp32-hal-rgb-led.h
index 313c5887070..4d8cf31d182 100644
--- a/cores/esp32/esp32-hal-rgb-led.h
+++ b/cores/esp32/esp32-hal-rgb-led.h
@@ -1,7 +1,7 @@
 #ifndef MAIN_ESP32_HAL_RGB_LED_H_
 #define MAIN_ESP32_HAL_RGB_LED_H_
 
-#ifndef NO_NEW_RMT_DRV
+#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -19,4 +19,4 @@ void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue
 #endif
 
 #endif /* MAIN_ESP32_HAL_RGB_LED_H_ */
-#endif /* NO_NEW_RMT_DRV */
\ No newline at end of file
+#endif /* ESP32_ARDUINO_NEW_RMT_DRV_OFF */
\ No newline at end of file
diff --git a/cores/esp32/esp32-hal-rmt.c b/cores/esp32/esp32-hal-rmt.c
index f77e673b711..058b4e639a8 100644
--- a/cores/esp32/esp32-hal-rmt.c
+++ b/cores/esp32/esp32-hal-rmt.c
@@ -13,7 +13,7 @@
 // limitations under the License.
 
 #include "soc/soc_caps.h"
-#ifndef NO_NEW_RMT_DRV
+#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 
 #if SOC_RMT_SUPPORTED
 #include "esp32-hal.h"
@@ -629,4 +629,4 @@ bool rmtInit(int pin, rmt_ch_dir_t channel_direction, rmt_reserve_memsize_t mem_
 }
 
 #endif /* SOC_RMT_SUPPORTED */
-#endif /* NO_NEW_RMT_DRV */
\ No newline at end of file
+#endif /* ESP32_ARDUINO_NEW_RMT_DRV_OFF */
\ No newline at end of file
diff --git a/cores/esp32/esp32-hal-rmt.h b/cores/esp32/esp32-hal-rmt.h
index 0aa498c1489..a598a33f989 100644
--- a/cores/esp32/esp32-hal-rmt.h
+++ b/cores/esp32/esp32-hal-rmt.h
@@ -16,7 +16,7 @@
 #define MAIN_ESP32_HAL_RMT_H_
 
 #include "soc/soc_caps.h"
-#ifndef NO_NEW_RMT_DRV
+#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 
 #if SOC_RMT_SUPPORTED
 
@@ -236,4 +236,4 @@ bool rmtDeinit(int pin);
 
 #endif /* SOC_RMT_SUPPORTED */
 #endif /* MAIN_ESP32_HAL_RMT_H_ */
-#endif /* NO_NEW_RMT_DRV */
\ No newline at end of file
+#endif /* ESP32_ARDUINO_NEW_RMT_DRV_OFF */
\ No newline at end of file
diff --git a/cores/esp32/io_pin_remap.h b/cores/esp32/io_pin_remap.h
index c768f2e1936..e962e033e2e 100644
--- a/cores/esp32/io_pin_remap.h
+++ b/cores/esp32/io_pin_remap.h
@@ -79,7 +79,7 @@ int8_t gpioNumberToDigitalPin(int8_t gpioNumber);
 // cores/esp32/esp32-hal-rgb-led.h
 #define neopixelWrite(pin, red_val, green_val, blue_val) neopixelWrite(digitalPinToGPIONumber(pin), red_val, green_val, blue_val)
 
-#ifndef NO_NEW_RMT_DRV
+#ifndef ESP32_ARDUIESP32_ARDUINO_NEW_RMT_DRV_OFF_OFF
 // cores/esp32/esp32-hal-rmt.h
 #define rmtInit(pin, channel_direction, memsize, frequency_Hz) rmtInit(digitalPinToGPIONumber(pin), channel_direction, memsize, frequency_Hz)
 #define rmtSetEOT(pin, EOT_Level)                              rmtSetEOT(digitalPinToGPIONumber(pin), EOT_Level)
@@ -95,7 +95,7 @@ int8_t gpioNumberToDigitalPin(int8_t gpioNumber);
   rmtSetCarrier(digitalPinToGPIONumber(pin), carrier_en, carrier_level, frequency_Hz, duty_percent)
 #define rmtSetRxMinThreshold(pin, filter_pulse_ticks) rmtSetRxMinThreshold(digitalPinToGPIONumber(pin), filter_pulse_ticks)
 #define rmtDeinit(pin)                                rmtDeinit(digitalPinToGPIONumber(pin))
-#endif // NO_NEW_RMT_DRV
+#endif // ESP32_ARDUIESP32_ARDUINO_NEW_RMT_DRV_OFF_OFF
 
 // cores/esp32/esp32-hal-sigmadelta.h
 #define sigmaDeltaAttach(pin, freq) sigmaDeltaAttach(digitalPinToGPIONumber(pin), freq)

From 2fdbe44925751dc8f7662a010fc5d2004127f7d9 Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Wed, 26 Jun 2024 14:13:47 -0300
Subject: [PATCH 06/11] fix(rmt): ESP32_ARDUINO_NEW_RMT_DRV_OFF

---
 cores/esp32/io_pin_remap.h                                    | 4 ++--
 .../Legacy_RMT_Driver_Compatible.ino                          | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/cores/esp32/io_pin_remap.h b/cores/esp32/io_pin_remap.h
index e962e033e2e..8889ba6ed34 100644
--- a/cores/esp32/io_pin_remap.h
+++ b/cores/esp32/io_pin_remap.h
@@ -79,7 +79,7 @@ int8_t gpioNumberToDigitalPin(int8_t gpioNumber);
 // cores/esp32/esp32-hal-rgb-led.h
 #define neopixelWrite(pin, red_val, green_val, blue_val) neopixelWrite(digitalPinToGPIONumber(pin), red_val, green_val, blue_val)
 
-#ifndef ESP32_ARDUIESP32_ARDUINO_NEW_RMT_DRV_OFF_OFF
+#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 // cores/esp32/esp32-hal-rmt.h
 #define rmtInit(pin, channel_direction, memsize, frequency_Hz) rmtInit(digitalPinToGPIONumber(pin), channel_direction, memsize, frequency_Hz)
 #define rmtSetEOT(pin, EOT_Level)                              rmtSetEOT(digitalPinToGPIONumber(pin), EOT_Level)
@@ -95,7 +95,7 @@ int8_t gpioNumberToDigitalPin(int8_t gpioNumber);
   rmtSetCarrier(digitalPinToGPIONumber(pin), carrier_en, carrier_level, frequency_Hz, duty_percent)
 #define rmtSetRxMinThreshold(pin, filter_pulse_ticks) rmtSetRxMinThreshold(digitalPinToGPIONumber(pin), filter_pulse_ticks)
 #define rmtDeinit(pin)                                rmtDeinit(digitalPinToGPIONumber(pin))
-#endif // ESP32_ARDUIESP32_ARDUINO_NEW_RMT_DRV_OFF_OFF
+#endif // ESP32_ARDUINO_NEW_RMT_DRV_OFF
 
 // cores/esp32/esp32-hal-sigmadelta.h
 #define sigmaDeltaAttach(pin, freq) sigmaDeltaAttach(digitalPinToGPIONumber(pin), freq)
diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
index a7b62f1e05b..59afd66a885 100644
--- a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
@@ -7,7 +7,7 @@
  * 
  */
 
-#ifndef ESP32_ARDUIESP32_ARDUINO_NEW_RMT_DRV_OFF_OFF
+#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 
 // add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NEW_RMT_DRV_OFF" to use the RMT Legacy driver
 #warning "ESP32_ARDUINO_NEW_RMT_DRV_OFF is not defined, using new RMT driver"

From a1f228b0ac99fda9123181e48814688e05e034ee Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Wed, 26 Jun 2024 16:21:41 -0300
Subject: [PATCH 07/11] fix(rmt): GPIO HAL only

---
 cores/esp32/esp32-hal-gpio.c                  | 12 ++++++++--
 cores/esp32/esp32-hal-rgb-led.c               |  3 ---
 cores/esp32/esp32-hal-rgb-led.h               |  2 --
 cores/esp32/esp32-hal-rmt.c                   |  2 --
 cores/esp32/esp32-hal-rmt.h                   |  2 --
 cores/esp32/io_pin_remap.h                    |  2 --
 .../Legacy_RMT_Driver_Compatible.ino          | 22 ++++++-------------
 .../Legacy_RMT_Driver_Compatible/build_opt.h  |  2 +-
 8 files changed, 18 insertions(+), 29 deletions(-)

diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c
index 3ccfdf9c1e5..d6a1c3df2db 100644
--- a/cores/esp32/esp32-hal-gpio.c
+++ b/cores/esp32/esp32-hal-gpio.c
@@ -17,6 +17,16 @@
 #include "hal/gpio_hal.h"
 #include "soc/soc_caps.h"
 
+// RGB_BUILTIN is defined in pins_arduino.h
+// If RGB_BUILTIN is defined, it will be used as a pin number for the RGB LED
+// If RGB_BUILTIN has a side effect that prevents using RMT Legacy driver in IDF 5.1
+// Define ESP32_ARDUINO_NO_RGB_BUILTIN in build_opt.h or through CLI to disable RGB_BUILTIN
+#ifdef ESP32_ARDUINO_NO_RGB_BUILTIN
+#ifdef RGB_BUILTIN
+#undef RGB_BUILTIN
+#endif
+#endif
+
 // It fixes lack of pin definition for S3 and for any future SoC
 // this function works for ESP32, ESP32-S2 and ESP32-S3 - including the C3, it will return -1 for any pin
 #if SOC_TOUCH_SENSOR_NUM > 0
@@ -151,7 +161,6 @@ uint8_t RGB_BUILTIN_storage = 0;
 #endif
 
 extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
-#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 #ifdef RGB_BUILTIN
   if (pin == RGB_BUILTIN) {
     //use RMT to set all channels on/off
@@ -161,7 +170,6 @@ extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
     return;
   }
 #endif // RGB_BUILTIN
-#endif // ESP32_ARDUINO_NEW_RMT_DRV_OFF
   if (perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) != NULL) {
     gpio_set_level((gpio_num_t)pin, val);
   } else {
diff --git a/cores/esp32/esp32-hal-rgb-led.c b/cores/esp32/esp32-hal-rgb-led.c
index befc204b651..99c95e1943e 100644
--- a/cores/esp32/esp32-hal-rgb-led.c
+++ b/cores/esp32/esp32-hal-rgb-led.c
@@ -1,7 +1,5 @@
 #include "soc/soc_caps.h"
 
-#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
-
 #include "esp32-hal-rgb-led.h"
 
 void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val) {
@@ -42,4 +40,3 @@ void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue
   log_e("RMT is not supported on " CONFIG_IDF_TARGET);
 #endif /* SOC_RMT_SUPPORTED */
 }
-#endif /* ESP32_ARDUINO_NEW_RMT_DRV_OFF */
\ No newline at end of file
diff --git a/cores/esp32/esp32-hal-rgb-led.h b/cores/esp32/esp32-hal-rgb-led.h
index 4d8cf31d182..33f37c849b6 100644
--- a/cores/esp32/esp32-hal-rgb-led.h
+++ b/cores/esp32/esp32-hal-rgb-led.h
@@ -1,7 +1,6 @@
 #ifndef MAIN_ESP32_HAL_RGB_LED_H_
 #define MAIN_ESP32_HAL_RGB_LED_H_
 
-#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -19,4 +18,3 @@ void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue
 #endif
 
 #endif /* MAIN_ESP32_HAL_RGB_LED_H_ */
-#endif /* ESP32_ARDUINO_NEW_RMT_DRV_OFF */
\ No newline at end of file
diff --git a/cores/esp32/esp32-hal-rmt.c b/cores/esp32/esp32-hal-rmt.c
index 058b4e639a8..e3877b5ff48 100644
--- a/cores/esp32/esp32-hal-rmt.c
+++ b/cores/esp32/esp32-hal-rmt.c
@@ -13,7 +13,6 @@
 // limitations under the License.
 
 #include "soc/soc_caps.h"
-#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 
 #if SOC_RMT_SUPPORTED
 #include "esp32-hal.h"
@@ -629,4 +628,3 @@ bool rmtInit(int pin, rmt_ch_dir_t channel_direction, rmt_reserve_memsize_t mem_
 }
 
 #endif /* SOC_RMT_SUPPORTED */
-#endif /* ESP32_ARDUINO_NEW_RMT_DRV_OFF */
\ No newline at end of file
diff --git a/cores/esp32/esp32-hal-rmt.h b/cores/esp32/esp32-hal-rmt.h
index a598a33f989..7848ab0a390 100644
--- a/cores/esp32/esp32-hal-rmt.h
+++ b/cores/esp32/esp32-hal-rmt.h
@@ -16,7 +16,6 @@
 #define MAIN_ESP32_HAL_RMT_H_
 
 #include "soc/soc_caps.h"
-#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 
 #if SOC_RMT_SUPPORTED
 
@@ -236,4 +235,3 @@ bool rmtDeinit(int pin);
 
 #endif /* SOC_RMT_SUPPORTED */
 #endif /* MAIN_ESP32_HAL_RMT_H_ */
-#endif /* ESP32_ARDUINO_NEW_RMT_DRV_OFF */
\ No newline at end of file
diff --git a/cores/esp32/io_pin_remap.h b/cores/esp32/io_pin_remap.h
index 8889ba6ed34..4e422632ea4 100644
--- a/cores/esp32/io_pin_remap.h
+++ b/cores/esp32/io_pin_remap.h
@@ -79,7 +79,6 @@ int8_t gpioNumberToDigitalPin(int8_t gpioNumber);
 // cores/esp32/esp32-hal-rgb-led.h
 #define neopixelWrite(pin, red_val, green_val, blue_val) neopixelWrite(digitalPinToGPIONumber(pin), red_val, green_val, blue_val)
 
-#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
 // cores/esp32/esp32-hal-rmt.h
 #define rmtInit(pin, channel_direction, memsize, frequency_Hz) rmtInit(digitalPinToGPIONumber(pin), channel_direction, memsize, frequency_Hz)
 #define rmtSetEOT(pin, EOT_Level)                              rmtSetEOT(digitalPinToGPIONumber(pin), EOT_Level)
@@ -95,7 +94,6 @@ int8_t gpioNumberToDigitalPin(int8_t gpioNumber);
   rmtSetCarrier(digitalPinToGPIONumber(pin), carrier_en, carrier_level, frequency_Hz, duty_percent)
 #define rmtSetRxMinThreshold(pin, filter_pulse_ticks) rmtSetRxMinThreshold(digitalPinToGPIONumber(pin), filter_pulse_ticks)
 #define rmtDeinit(pin)                                rmtDeinit(digitalPinToGPIONumber(pin))
-#endif // ESP32_ARDUINO_NEW_RMT_DRV_OFF
 
 // cores/esp32/esp32-hal-sigmadelta.h
 #define sigmaDeltaAttach(pin, freq) sigmaDeltaAttach(digitalPinToGPIONumber(pin), freq)
diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
index 59afd66a885..11750e142dc 100644
--- a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
@@ -7,30 +7,22 @@
  * 
  */
 
-#ifndef ESP32_ARDUINO_NEW_RMT_DRV_OFF
+#ifndef ESP32_ARDUINO_NO_RGB_BUILTIN
 
-// add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NEW_RMT_DRV_OFF" to use the RMT Legacy driver
-#warning "ESP32_ARDUINO_NEW_RMT_DRV_OFF is not defined, using new RMT driver"
-
-#define RMT_PIN 4  // Valid GPIO for ESP32, S2, S3, C3, C6 and H2
-bool installed = false;
+// add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NO_RGB_BUILTIN" to use the RMT Legacy driver
+#warning "ESP32_ARDUINO_NO_RGB_BUILTIN is not defined, this example is intended to demonstrate the RMT Legacy driver.
+#warning "Please add the file 'build_opt.h' with '-DESP32_ARDUINO_NO_RGB_BUILTIN' to your Arduino project folder."
+#warning "Another way to disable the RGB_BUILTIN is to define it in the platformio.ini file, for instance: '-D ESP32_ARDUINO_NO_RGB_BUILTIN'"
 
 void setup() {
-  Serial.begin(115200);
-  Serial.println("This sketch uses the new RMT driver.");
-  installed = rmtInit(RMT_PIN, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000);
 }
 
 void loop() {
-  String msg = "RMT New driver is installed: ";
-  msg += (char*)(installed ? "Yes." : "No.");
-  Serial.println(msg);
-  delay(5000);
 }
 
 #else
 
-// add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NEW_RMT_DRV_OFF" to use the RMT Legacy driver
+// add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NO_RGB_BUILTIN" to use the RMT Legacy driver
 // neoPixelWrite() is a function that writes to the RGB LED and it won't be available here
 #include "driver/rmt.h"
 
@@ -49,4 +41,4 @@ void loop() {
   delay(5000);
 }
 
-#endif // ESP32_ARDUINO_NEW_RMT_DRV_OFF
\ No newline at end of file
+#endif // ESP32_ARDUINO_NO_RGB_BUILTIN
\ No newline at end of file
diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
index 64238ad6cf1..94085f8768e 100644
--- a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
@@ -1 +1 @@
--DESP32_ARDUINO_NEW_RMT_DRV_OFF
\ No newline at end of file
+-DESP32_ARDUINO_NO_RGB_BUILTIN
\ No newline at end of file

From 4320650f476d984f454db6ad72de13c7db9dccff Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Wed, 26 Jun 2024 18:36:08 -0300
Subject: [PATCH 08/11] fix(rmt): error case

---
 .../Legacy_RMT_Driver_Compatible.ino                 | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
index 11750e142dc..5d2ef0ba7e4 100644
--- a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
@@ -10,15 +10,9 @@
 #ifndef ESP32_ARDUINO_NO_RGB_BUILTIN
 
 // add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NO_RGB_BUILTIN" to use the RMT Legacy driver
-#warning "ESP32_ARDUINO_NO_RGB_BUILTIN is not defined, this example is intended to demonstrate the RMT Legacy driver.
-#warning "Please add the file 'build_opt.h' with '-DESP32_ARDUINO_NO_RGB_BUILTIN' to your Arduino project folder."
-#warning "Another way to disable the RGB_BUILTIN is to define it in the platformio.ini file, for instance: '-D ESP32_ARDUINO_NO_RGB_BUILTIN'"
-
-void setup() {
-}
-
-void loop() {
-}
+#error "ESP32_ARDUINO_NO_RGB_BUILTIN is not defined, this example is intended to demonstrate the RMT Legacy driver.
+#error "Please add the file 'build_opt.h' with '-DESP32_ARDUINO_NO_RGB_BUILTIN' to your Arduino project folder."
+#error "Another way to disable the RGB_BUILTIN is to define it in the platformio.ini file, for instance: '-D ESP32_ARDUINO_NO_RGB_BUILTIN'"
 
 #else
 

From fe50369cef0d39f17215622ac87cc560c719c529 Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Wed, 26 Jun 2024 21:51:07 -0300
Subject: [PATCH 09/11] fix(rmt): not necessary change

---
 cores/esp32/esp32-hal-rmt.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/cores/esp32/esp32-hal-rmt.h b/cores/esp32/esp32-hal-rmt.h
index 7848ab0a390..c15eadfbcd1 100644
--- a/cores/esp32/esp32-hal-rmt.h
+++ b/cores/esp32/esp32-hal-rmt.h
@@ -16,7 +16,6 @@
 #define MAIN_ESP32_HAL_RMT_H_
 
 #include "soc/soc_caps.h"
-
 #if SOC_RMT_SUPPORTED
 
 #ifdef __cplusplus

From 1a14a6d2dba39c8da35f150d03cd162fc7c511ba Mon Sep 17 00:00:00 2001
From: "pre-commit-ci-lite[bot]"
 <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Date: Thu, 27 Jun 2024 11:31:24 +0000
Subject: [PATCH 10/11] ci(pre-commit): Apply automatic fixes

---
 cores/esp32/esp32-hal-gpio.c                           |  2 +-
 .../Legacy_RMT_Driver_Compatible.ino                   | 10 +++++-----
 .../RMT/Legacy_RMT_Driver_Compatible/build_opt.h       |  2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c
index d6a1c3df2db..98dc26599be 100644
--- a/cores/esp32/esp32-hal-gpio.c
+++ b/cores/esp32/esp32-hal-gpio.c
@@ -169,7 +169,7 @@ extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
     neopixelWrite(RGB_BUILTIN, comm_val, comm_val, comm_val);
     return;
   }
-#endif // RGB_BUILTIN
+#endif  // RGB_BUILTIN
   if (perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) != NULL) {
     gpio_set_level((gpio_num_t)pin, val);
   } else {
diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
index 5d2ef0ba7e4..74578f755dc 100644
--- a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
@@ -1,10 +1,10 @@
 /*
  * This example demonstrates how to use the file build_opt.h to disable the new RMT Driver
- * Note that this file shall be added the Arduino project 
- * 
+ * Note that this file shall be added the Arduino project
+ *
  * If the content of this file changes, it is necessary to delete the compiled Arduino IDE cache
  * It can be done by changing, for instance, the "Core Debug Level" option, forcing the system to rebuild the Arduino Core
- * 
+ *
  */
 
 #ifndef ESP32_ARDUINO_NO_RGB_BUILTIN
@@ -30,9 +30,9 @@ void setup() {
 
 void loop() {
   String msg = "RMT Legacy driver is installed: ";
-  msg += (char*)(installed ? "Yes." : "No.");
+  msg += (char *)(installed ? "Yes." : "No.");
   Serial.println(msg);
   delay(5000);
 }
 
-#endif // ESP32_ARDUINO_NO_RGB_BUILTIN
\ No newline at end of file
+#endif  // ESP32_ARDUINO_NO_RGB_BUILTIN
diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
index 94085f8768e..583b42d9880 100644
--- a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
@@ -1 +1 @@
--DESP32_ARDUINO_NO_RGB_BUILTIN
\ No newline at end of file
+-DESP32_ARDUINO_NO_RGB_BUILTIN

From a64b42859579bf3b6077689d09726727c0ec0317 Mon Sep 17 00:00:00 2001
From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com>
Date: Thu, 27 Jun 2024 12:30:55 -0300
Subject: [PATCH 11/11] ci(pre-commit): Ignore build_opt in clangformat

---
 .pre-commit-config.yaml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index b06629896ed..c92e9ad0dce 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -34,6 +34,7 @@ repos:
     hooks:
       - id: clang-format
         types_or: [c, c++]
+        exclude: ^.*\/build_opt\.h$
   - repo: https://github.com/psf/black-pre-commit-mirror
     rev: "22.10.0"
     hooks: