Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bengtmartensson/a96f311164bf45c166e46adbfc91b161 to your computer and use it in GitHub Desktop.
Save bengtmartensson/a96f311164bf45c166e46adbfc91b161 to your computer and use it in GitHub Desktop.
0001-Clean-up-the-ESP32-port.patch
From e5d49e19f63e24b5c826ad5014a1eac12735f261 Mon Sep 17 00:00:00 2001
From: Bengt Martensson <barf@bengt-martensson.de>
Date: Thu, 6 Apr 2017 09:57:26 +0200
Subject: [PATCH] Clean up the ESP32 port.
---
IRremote.cpp | 8 +++-----
boarddefs.h | 32 ++++++++++++++++++++++----------
esp32.cpp | 34 ++++++++++++++++++++++++++++++++++
irRecv.cpp | 33 ++++++++++++++++-----------------
irSend.cpp | 5 ++---
5 files changed, 77 insertions(+), 35 deletions(-)
create mode 100644 esp32.cpp
diff --git a/IRremote.cpp b/IRremote.cpp
index e811cfc..f41f818 100644
--- a/IRremote.cpp
+++ b/IRremote.cpp
@@ -24,7 +24,7 @@
# include "IRremoteInt.h"
#undef IR_GLOBAL
-#ifndef IR_TIMER_USE_ESP32
+#ifdef HAS_AVR_INTERRUPT_H
#include <avr/interrupt.h>
#endif
@@ -123,11 +123,7 @@ int MATCH_SPACE (int measured_ticks, int desired_us)
// As soon as first MARK arrives:
// Gap width is recorded; Ready is cleared; New logging starts
//
-#ifdef IR_TIMER_USE_ESP32
-void IRTimer()
-#else
ISR (TIMER_INTR_NAME)
-#endif
{
TIMER_RESET;
@@ -189,6 +185,7 @@ ISR (TIMER_INTR_NAME)
break;
}
+#ifdef BLINKLED
// If requested, flash LED while receiving IR data
if (irparams.blinkflag) {
if (irdata == MARK)
@@ -197,4 +194,5 @@ ISR (TIMER_INTR_NAME)
else if (irparams.blinkpin) digitalWrite(irparams.blinkpin, LOW); // Turn user defined pin LED on
else BLINKLED_OFF() ; // if no user defined LED pin, turn default LED pin for the hardware on
}
+#endif // BLINKLED
}
diff --git a/boarddefs.h b/boarddefs.h
index 17e2551..3c4a498 100644
--- a/boarddefs.h
+++ b/boarddefs.h
@@ -20,6 +20,12 @@
#ifndef boarddefs_h
#define boarddefs_h
+// Define some defaults, that some boards may like to override
+// (This is to avoid negative logic, ! DONT_... is just awkward.)
+#define HAS_AVR_INTERRUPT_H
+#define SENDING_SUPPORTED
+#define USE_DEFAULT_ENABLE_IR_IN
+
//------------------------------------------------------------------------------
// Defines for blinking the LED
//
@@ -39,11 +45,18 @@
# define BLINKLED_ON() (PORTD |= B00000001)
# define BLINKLED_OFF() (PORTD &= B11111110)
-// No system LED on ESP32, disable blinking
#elif defined(ESP32)
-# define BLINKLED 255
-# define BLINKLED_ON() 1
-# define BLINKLED_OFF() 1
+ // No system LED on ESP32, disable blinking by NOT defining BLINKLED
+
+ // avr/interrupt.h is not present
+# undef HAS_AVR_INTERRUPT_H
+
+ // Sending not implemented
+# undef SENDING_SUPPORTED#
+
+ // Supply own enbleIRIn
+# undef USE_DEFAULT_ENABLE_IR_IN
+
#else
# define BLINKLED 13
# define BLINKLED_ON() (PORTB |= B00100000)
@@ -560,12 +573,11 @@
// way to do this on ESP32 is using the RMT built in driver like in this incomplete library below
// https://github.com/ExploreEmbedded/ESP32_RMT
#elif defined(IR_TIMER_USE_ESP32)
-#define TIMER_RESET
-#define TIMER_ENABLE_PWM
-#define TIMER_DISABLE_PWM Serial.println("IRsend not implemented for ESP32 yet");
-#define TIMER_ENABLE_INTR
-#define TIMER_DISABLE_INTR
-#define TIMER_INTR_NAME
+
+#ifdef ISR
+# undef ISR
+#endif
+#define ISR(f) void IRTimer()
//---------------------------------------------------------
// Unknown Timer
diff --git a/esp32.cpp b/esp32.cpp
new file mode 100644
index 0000000..5f2e9bb
--- /dev/null
+++ b/esp32.cpp
@@ -0,0 +1,34 @@
+#ifdef ESP32
+
+// This file contains functions specific to the ESP32.
+
+#include "IRremote.h"
+#include "IRremoteInt.h"
+
+hw_timer_t *timer;
+void IRTimer(); // defined in IRremote.cpp, masqueraded as ISR(TIMER_INTR_NAME)
+
+//+=============================================================================
+// initialization
+//
+void IRrecv::enableIRIn ( )
+{
+// Interrupt Service Routine - Fires every 50uS
+ // 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 nanosecond precision, 1 to count up
+ timer = timerBegin(1, 80, 1);
+ timerAttachInterrupt(timer, &IRTimer, 1);
+ // every 50ns, autoreload = true
+ timerAlarmWrite(timer, 50, true);
+ timerAlarmEnable(timer);
+
+ // Initialize state machine variables
+ irparams.rcvstate = STATE_IDLE;
+ irparams.rawlen = 0;
+
+ // Set pin modes
+ pinMode(irparams.recvpin, INPUT);
+}
+
+#endif // ESP32
diff --git a/irRecv.cpp b/irRecv.cpp
index 12b0806..2a68897 100644
--- a/irRecv.cpp
+++ b/irRecv.cpp
@@ -1,10 +1,5 @@
#include "IRremote.h"
#include "IRremoteInt.h"
-
-#ifdef IR_TIMER_USE_ESP32
-hw_timer_t *timer;
-void IRTimer(); // defined in IRremote.cpp
-#endif
//+=============================================================================
// Decodes the received IR message
@@ -120,19 +115,20 @@ IRrecv::IRrecv (int recvpin, int blinkpin)
//+=============================================================================
// initialization
//
+#ifdef USE_DEFAULT_ENABLE_IR_IN
void IRrecv::enableIRIn ( )
{
-// Interrupt Service Routine - Fires every 50uS
-#ifdef ESP32
- // 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 nanosecond precision, 1 to count up
- timer = timerBegin(1, 80, 1);
- timerAttachInterrupt(timer, &IRTimer, 1);
- // every 50ns, autoreload = true
- timerAlarmWrite(timer, 50, true);
- timerAlarmEnable(timer);
-#else
+// Interrupt Service Routine - Fires every 50uS
+#ifdef ESP32
+ // 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 nanosecond precision, 1 to count up
+ timer = timerBegin(1, 80, 1);
+ timerAttachInterrupt(timer, &IRTimer, 1);
+ // every 50ns, autoreload = true
+ timerAlarmWrite(timer, 50, true);
+ timerAlarmEnable(timer);
+#else
cli();
// Setup pulse clock timer interrupt
// Prescale /8 (16M/8 = 0.5 microseconds per tick)
@@ -146,7 +142,7 @@ void IRrecv::enableIRIn ( )
TIMER_RESET;
sei(); // enable interrupts
-#endif
+#endif
// Initialize state machine variables
irparams.rcvstate = STATE_IDLE;
@@ -155,14 +151,17 @@ void IRrecv::enableIRIn ( )
// Set pin modes
pinMode(irparams.recvpin, INPUT);
}
+#endif // USE_DEFAULT_ENABLE_IR_IN
//+=============================================================================
// Enable/disable blinking of pin 13 on IR processing
//
void IRrecv::blink13 (int blinkflag)
{
+#ifdef BLINKLED
irparams.blinkflag = blinkflag;
if (blinkflag) pinMode(BLINKLED, OUTPUT) ;
+#endif
}
//+=============================================================================
diff --git a/irSend.cpp b/irSend.cpp
index c3ef3ff..4ae1e9b 100644
--- a/irSend.cpp
+++ b/irSend.cpp
@@ -1,6 +1,7 @@
#include "IRremote.h"
#include "IRremoteInt.h"
+#ifdef SENDING_SUPPORTED
//+=============================================================================
void IRsend::sendRaw (const unsigned int buf[], unsigned int len, unsigned int hz)
{
@@ -54,8 +55,6 @@ void IRsend::space (unsigned int time)
//
void IRsend::enableIROut (int khz)
{
-// FIXME: implement ESP32 support, see IR_TIMER_USE_ESP32 in boarddefs.h
-#ifndef ESP32
// Disable the Timer2 Interrupt (which is used for receiving IR)
TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt
@@ -68,7 +67,6 @@ void IRsend::enableIROut (int khz)
// CS2 = 000: no prescaling
// The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A.
TIMER_CONFIG_KHZ(khz);
-#endif
}
//+=============================================================================
@@ -88,3 +86,4 @@ void IRsend::custom_delay_usec(unsigned long uSecs) {
//}
}
+#endif // SENDING_SUPPORTED
\ No newline at end of file
--
2.9.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment