Skip to content

Commit

Permalink
Improved ESP32 timer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ArminJo committed May 29, 2023
1 parent 1a0b513 commit e3cdf40
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/private/IRTimer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1364,15 +1364,15 @@ void timerConfigForReceive() {
#elif defined(ESP32)
// Variables specific to the ESP32.
// the ledc functions behave like hardware timers for us :-), so we do not require our own soft PWM generation code.
hw_timer_t *s50usTimer; // set by timerConfigForReceive()
hw_timer_t *s50usTimer = NULL; // set by timerConfigForReceive()


# if !defined(SEND_AND_RECEIVE_TIMER_LEDC_CHANNEL)
#define SEND_AND_RECEIVE_TIMER_LEDC_CHANNEL 0 // The channel used for PWM 0 to 7 are high speed PWM channels
# endif

void timerEnableReceiveInterrupt() {
timerAlarmEnable (s50usTimer);
timerAlarmEnable(s50usTimer);
}

#if !defined(ESP_ARDUINO_VERSION)
Expand All @@ -1384,14 +1384,14 @@ void timerEnableReceiveInterrupt() {
#if ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(2, 0, 2)
void timerDisableReceiveInterrupt() {
if (s50usTimer != NULL) {
timerEnd(s50usTimer);
timerDetachInterrupt(s50usTimer);
timerEnd(s50usTimer);
}
}
#else
void timerDisableReceiveInterrupt() {
if (s50usTimer != NULL) {
timerAlarmDisable (s50usTimer);
timerAlarmDisable(s50usTimer);
}
}
#endif
Expand All @@ -1405,10 +1405,12 @@ void timerConfigForReceive() {
// ESP32 has a proper API to setup timers, no weird chip macros needed
// simply call the readable API versions :)
// 3 timers, choose #1, 80 divider for microsecond precision @80MHz clock, count_up = true
s50usTimer = timerBegin(1, 80, true);
timerAttachInterrupt(s50usTimer, &IRReceiveTimerInterruptHandler, false); // false -> level interrupt, true -> edge interrupt, but this is not supported :-(
if(s50usTimer == NULL) {
s50usTimer = timerBegin(1, 80, true);
timerAttachInterrupt(s50usTimer, &IRReceiveTimerInterruptHandler, false); // false -> level interrupt, true -> edge interrupt, but this is not supported :-(
timerAlarmWrite(s50usTimer, MICROS_PER_TICK, true);
}
// every 50 us, autoreload = true
timerAlarmWrite(s50usTimer, MICROS_PER_TICK, true);
}

# if defined(SEND_PWM_BY_TIMER)
Expand Down

0 comments on commit e3cdf40

Please sign in to comment.