Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Esp32 Core version 3 #2144

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
77 changes: 63 additions & 14 deletions src/IRrecv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -226,7 +227,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
Expand All @@ -242,8 +243,20 @@ 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) && \
BorisKofman marked this conversation as resolved.
Show resolved Hide resolved
(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
// For ESP32 core version 2.x, keep using `timerAlarmEnable`
timerWrite(timer, 0);
timerAlarmEnable(timer);
#endif

#endif // _ESP32_IRRECV_TIMER_HACK
#endif // ESP32
}
Expand Down Expand Up @@ -356,23 +369,39 @@ void IRrecv::enableIRIn(const bool pullup) {
pinMode(params.recvpin, INPUT);
#endif // UNIT_TEST
}

#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)
#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
BorisKofman marked this conversation as resolved.
Show resolved Hide resolved

// Ensure the timer is successfully initialized
#ifdef DEBUG
if (timer == NULL) {
DPRINT("FATAL: Unable enable system timer: ");
DPRINTLN((uint16_t)_timer_num);
}
#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);
// 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);
// 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))
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

#endif // ESP32

// Initialise state machine variables
Expand All @@ -398,9 +427,19 @@ 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
Expand All @@ -426,7 +465,17 @@ 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
}
Expand Down
Loading