Skip to content

Commit f0e932d

Browse files
BorisKofmanBorisKofman
authored andcommitted
Update IRrecv.cpp
1 parent 93c84f3 commit f0e932d

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/IRrecv.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ typedef struct hw_timer_s {
134134
#endif // _ESP32_IRRECV_TIMER_HACK / End of Horrible Hack.
135135

136136
namespace _IRrecv {
137-
static hw_timer_t * timer = NULL;
137+
static hw_timer_t *timer = NULL; // Declare ESP32 timer variable
138+
138139
} // namespace _IRrecv
139140
#endif // ESP32
140141
using _IRrecv::timer;
@@ -247,7 +248,9 @@ static void USE_IRAM_ATTR gpio_intr() {
247248
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
248249
// For ESP32 core version 3.x, replace `timerAlarmEnable`
249250
timerWrite(timer, 0);
250-
timerAlarm(timer, 0, true, 0); // Use the updated function
251+
uint64_t alarm_value = 50000; // Example value (50ms)
252+
timerAlarm(timer, alarm_value, false, 0); // Disable auto-reload
253+
251254
#else
252255
// For ESP32 core version 2.x, keep using `timerAlarmEnable`
253256
timerWrite(timer, 0);
@@ -369,17 +372,22 @@ void IRrecv::enableIRIn(const bool pullup) {
369372

370373
#if defined(ESP32)
371374
// Initialise the ESP32 timer.
372-
// 80MHz / 80 = 1 uSec granularity.
373-
// Check for ESP32 core version and handle timerBegin differently
374375
#if defined(ESP_ARDUINO_VERSION) && \
375376
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
376-
// For ESP32 core version 3.x (three arguments)
377-
timer = timerBegin(_timer_num);
378-
#else
379-
// For ESP32 core version 2.0.x (one argument)
380-
timer = timerBegin(_timer_num, 80, true);
381-
#endif
377+
// Use newer timerBegin signature for ESP32 core version 3.x
378+
timer = timerBegin(1000000); // Initialize with 1MHz (1us per tick)
379+
Serial.println("Starting timer initialization...");
380+
Serial.print("Timer number: ");
381+
Serial.println(_timer_num);
382+
Serial.println((uint16_t)_timer_num);
383+
#else
384+
// Initialise the ESP32 timer.
385+
// 80MHz / 80 = 1 uSec granularity.
386+
// Check for ESP32 core version and handle timerBegin differently
387+
timer = timerBegin(_timer_num, 80, true);
388+
#endif
382389

390+
// Ensure the timer is successfully initialized
383391
#ifdef DEBUG
384392
if (timer == NULL) {
385393
DPRINT("FATAL: Unable enable system timer: ");
@@ -390,15 +398,13 @@ void IRrecv::enableIRIn(const bool pullup) {
390398
// Set the timer so it only fires once, and set its trigger in microseconds.
391399
#if defined(ESP_ARDUINO_VERSION) && \
392400
(ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0))
393-
// For ESP32 core version 3.x (use timerWrite)
394-
timerWrite(timer, 0); // Reset the timer
401+
timerWrite(timer, 0); // Reset the timer for ESP32 core version 3.x
395402
timerAttachInterrupt(timer, &read_timeout);
396-
#else
397-
timerAlarmWrite(timer, MS_TO_USEC(params.timeout), ONCE);
403+
#else
404+
// Attach timer interrupt for core version 2.x
405+
timerAlarmWrite(timer, MS_TO_USEC(params.timeout), true);
398406
timerAttachInterrupt(timer, &read_timeout, false);
399-
#endif
400-
// Note: Interrupt needs to be attached before it can be enabled or disabled.
401-
// Note: EDGE (true) is not supported, use LEVEL (false). Ref: #1713
407+
#endif
402408

403409
#endif // ESP32
404410

0 commit comments

Comments
 (0)