From aa8f7b31fe16ccb8e26baa5e887126bfca4e24ed Mon Sep 17 00:00:00 2001 From: Marc MERLIN Date: Sun, 19 Mar 2017 20:27:56 -0700 Subject: [PATCH] Added ESP32 IR receive support (IRsend not implemented yet). - disable a lot of defines not relevant to ESP32, set them to 1 (no-op) - change default IR pin to 35 from 11 - changed serial speed to 115200 (9600 is too slow to keep up with IR input) - irSend disables code that will not compile on ESP32. It won't work, but it won't break compilation either. --- IRremote.cpp | 6 ++++++ boarddefs.h | 13 +++++++++++++ examples/IRrecvDemo/IRrecvDemo.ino | 8 +++++++- examples/IRrecvDump/IRrecvDump.ino | 8 ++++++-- examples/IRrecvDumpV2/IRrecvDumpV2.ino | 4 ++-- irRecv.cpp | 15 +++++++++++++++ irSend.cpp | 3 +++ 7 files changed, 52 insertions(+), 5 deletions(-) diff --git a/IRremote.cpp b/IRremote.cpp index c9907eb..9613bea 100644 --- a/IRremote.cpp +++ b/IRremote.cpp @@ -18,7 +18,9 @@ // Whynter A/C ARC-110WD added by Francesco Meschia //****************************************************************************** +#ifndef ESP32 #include +#endif // Defining IR_GLOBAL here allows us to declare the instantiation of global variables #define IR_GLOBAL @@ -120,7 +122,11 @@ 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 ESP32 +void onTimer() +#else ISR (TIMER_INTR_NAME) +#endif { TIMER_RESET; diff --git a/boarddefs.h b/boarddefs.h index 62b6be4..ce8a4f9 100644 --- a/boarddefs.h +++ b/boarddefs.h @@ -39,6 +39,10 @@ # define BLINKLED_ON() (PORTD |= B00000001) # define BLINKLED_OFF() (PORTD &= B11111110) +#elif defined(ESP32) +# define BLINKLED 255 +# define BLINKLED_ON() 1 +# define BLINKLED_OFF() 1 #else # define BLINKLED 13 #define BLINKLED_ON() (PORTB |= B00100000) @@ -147,12 +151,21 @@ // #if defined(IR_USE_TIMER2) +#ifdef ESP32 // Used in irSend, not implemented yet (FIXME) +#define TIMER_RESET 1 +#define TIMER_ENABLE_PWM 1 +#define TIMER_DISABLE_PWM Serial.println("IRsend not implemented for ESP32 yet"); +#define TIMER_ENABLE_INTR 1 +#define TIMER_DISABLE_INTR 1 +#define TIMER_INTR_NAME 1 +#else #define TIMER_RESET #define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1)) #define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1))) #define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A)) #define TIMER_DISABLE_INTR (TIMSK2 = 0) #define TIMER_INTR_NAME TIMER2_COMPA_vect +#endif #define TIMER_CONFIG_KHZ(val) ({ \ const uint8_t pwmval = SYSCLOCK / 2000 / (val); \ diff --git a/examples/IRrecvDemo/IRrecvDemo.ino b/examples/IRrecvDemo/IRrecvDemo.ino index 3a49832..4635b2c 100644 --- a/examples/IRrecvDemo/IRrecvDemo.ino +++ b/examples/IRrecvDemo/IRrecvDemo.ino @@ -8,7 +8,11 @@ #include +#ifdef ESP32 +int RECV_PIN = 35; +#else int RECV_PIN = 11; +#endif IRrecv irrecv(RECV_PIN); @@ -16,8 +20,10 @@ decode_results results; void setup() { - Serial.begin(9600); + Serial.begin(115200); + Serial.println("Enabling IRin"); irrecv.enableIRIn(); // Start the receiver + Serial.println("Enabled IRin"); } void loop() { diff --git a/examples/IRrecvDump/IRrecvDump.ino b/examples/IRrecvDump/IRrecvDump.ino index 90b47ba..9a21b56 100644 --- a/examples/IRrecvDump/IRrecvDump.ino +++ b/examples/IRrecvDump/IRrecvDump.ino @@ -15,7 +15,11 @@ * You can change this to another available Arduino Pin. * Your IR receiver should be connected to the pin defined here */ -int RECV_PIN = 11; +#ifdef ESP32 +int RECV_PIN = 35; +#else +int RECV_PIN = 11; +#endif IRrecv irrecv(RECV_PIN); @@ -23,7 +27,7 @@ decode_results results; void setup() { - Serial.begin(9600); + Serial.begin(115200); irrecv.enableIRIn(); // Start the receiver } diff --git a/examples/IRrecvDumpV2/IRrecvDumpV2.ino b/examples/IRrecvDumpV2/IRrecvDumpV2.ino index 7256127..570baba 100644 --- a/examples/IRrecvDumpV2/IRrecvDumpV2.ino +++ b/examples/IRrecvDumpV2/IRrecvDumpV2.ino @@ -6,7 +6,7 @@ //------------------------------------------------------------------------------ // Tell IRremote which Arduino pin is connected to the IR Receiver (TSOP4838) // -int recvPin = 11; +int recvPin = 35; IRrecv irrecv(recvPin); //+============================================================================= @@ -14,7 +14,7 @@ IRrecv irrecv(recvPin); // void setup ( ) { - Serial.begin(9600); // Status message will be sent to PC at 9600 baud + Serial.begin(115200); // Status message will be sent to PC at 9600 baud irrecv.enableIRIn(); // Start the receiver } diff --git a/irRecv.cpp b/irRecv.cpp index 6343856..2f510c4 100644 --- a/irRecv.cpp +++ b/irRecv.cpp @@ -1,5 +1,10 @@ #include "IRremote.h" #include "IRremoteInt.h" + +#ifdef ESP32 +hw_timer_t *timer; +void onTimer(); // defined in IRremote.cpp +#endif //+============================================================================= // Decodes the received IR message @@ -117,6 +122,15 @@ IRrecv::IRrecv (int recvpin, int blinkpin) // void IRrecv::enableIRIn ( ) { +// Interrupt Service Routine - Fires every 50uS +#ifdef ESP32 + // 3 timers, choose #1, 80 divider nanosecond precision, 1 to count up + timer = timerBegin(1, 80, 1); + timerAttachInterrupt(timer, &onTimer, 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) @@ -130,6 +144,7 @@ void IRrecv::enableIRIn ( ) TIMER_RESET; sei(); // enable interrupts +#endif // Initialize state machine variables irparams.rcvstate = STATE_IDLE; diff --git a/irSend.cpp b/irSend.cpp index 253c941..6191aa6 100644 --- a/irSend.cpp +++ b/irSend.cpp @@ -54,6 +54,8 @@ void IRsend::space (unsigned int time) // void IRsend::enableIROut (int khz) { +// FIXME: implement ESP32 support +#ifndef ESP32 // Disable the Timer2 Interrupt (which is used for receiving IR) TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt @@ -66,6 +68,7 @@ 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 } //+=============================================================================