Cleaned up ESP32 integration, reverted ESP32 ifdefs on irreceive examples.

- fixed indenting on existing code in a few places for consistency
- introduced IR_TIMER_USE_ESP32 for ifdefs within the code as per
  request
- added comments explaining what's missing for irsend support on ESP32
- IRrecvDemo.ino gets a warning before and after interrupt is enabled in
  case it causes a crash

TESTED=IoTuz ESP32 board and original 328p arduino to make sure current
code did not break.
This commit is contained in:
Marc MERLIN
2017-03-31 21:52:52 -07:00
parent 1b56da6cc7
commit eae9de4307
7 changed files with 42 additions and 29 deletions

View File

@@ -18,16 +18,17 @@
// Whynter A/C ARC-110WD added by Francesco Meschia // Whynter A/C ARC-110WD added by Francesco Meschia
//****************************************************************************** //******************************************************************************
#ifndef ESP32
#include <avr/interrupt.h>
#endif
// Defining IR_GLOBAL here allows us to declare the instantiation of global variables // Defining IR_GLOBAL here allows us to declare the instantiation of global variables
#define IR_GLOBAL #define IR_GLOBAL
# include "IRremote.h" # include "IRremote.h"
# include "IRremoteInt.h" # include "IRremoteInt.h"
#undef IR_GLOBAL #undef IR_GLOBAL
#ifndef IR_TIMER_USE_ESP32
#include <avr/interrupt.h>
#endif
//+============================================================================= //+=============================================================================
// The match functions were (apparently) originally MACROs to improve code speed // The match functions were (apparently) originally MACROs to improve code speed
// (although this would have bloated the code) hence the names being CAPS // (although this would have bloated the code) hence the names being CAPS
@@ -122,7 +123,7 @@ int MATCH_SPACE (int measured_ticks, int desired_us)
// As soon as first MARK arrives: // As soon as first MARK arrives:
// Gap width is recorded; Ready is cleared; New logging starts // Gap width is recorded; Ready is cleared; New logging starts
// //
#ifdef ESP32 #ifdef IR_TIMER_USE_ESP32
void IRTimer() void IRTimer()
#else #else
ISR (TIMER_INTR_NAME) ISR (TIMER_INTR_NAME)

View File

@@ -39,13 +39,14 @@
# define BLINKLED_ON() (PORTD |= B00000001) # define BLINKLED_ON() (PORTD |= B00000001)
# define BLINKLED_OFF() (PORTD &= B11111110) # define BLINKLED_OFF() (PORTD &= B11111110)
// No system LED on ESP32, disable blinking
#elif defined(ESP32) #elif defined(ESP32)
# define BLINKLED 255 # define BLINKLED 255
# define BLINKLED_ON() 1 # define BLINKLED_ON() 1
# define BLINKLED_OFF() 1 # define BLINKLED_OFF() 1
#else #else
# define BLINKLED 13 # define BLINKLED 13
#define BLINKLED_ON() (PORTB |= B00100000) # define BLINKLED_ON() (PORTB |= B00100000)
# define BLINKLED_OFF() (PORTB &= B11011111) # define BLINKLED_OFF() (PORTB &= B11011111)
#endif #endif
@@ -129,14 +130,16 @@
// ATtiny84 // ATtiny84
#elif defined(__AVR_ATtiny84__) #elif defined(__AVR_ATtiny84__)
#define IR_USE_TIMER1 // tx = pin 6 #define IR_USE_TIMER1 // tx = pin 6
//ATtiny85 //ATtiny85
#elif defined(__AVR_ATtiny85__) #elif defined(__AVR_ATtiny85__)
#define IR_USE_TIMER_TINY0 // tx = pin 1 #define IR_USE_TIMER_TINY0 // tx = pin 1
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc // Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
// ATmega48, ATmega88, ATmega168, ATmega328 // ATmega48, ATmega88, ATmega168, ATmega328
#elif defined(ESP32)
#define IR_TIMER_USE_ESP32
#else #else
//#define IR_USE_TIMER1 // tx = pin 9 //#define IR_USE_TIMER1 // tx = pin 9
#define IR_USE_TIMER2 // tx = pin 3 #define IR_USE_TIMER2 // tx = pin 3
@@ -151,21 +154,12 @@
// //
#if defined(IR_USE_TIMER2) #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_RESET
#define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1)) #define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))
#define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1))) #define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1)))
#define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A)) #define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A))
#define TIMER_DISABLE_INTR (TIMSK2 = 0) #define TIMER_DISABLE_INTR (TIMSK2 = 0)
#define TIMER_INTR_NAME TIMER2_COMPA_vect #define TIMER_INTR_NAME TIMER2_COMPA_vect
#endif
#define TIMER_CONFIG_KHZ(val) ({ \ #define TIMER_CONFIG_KHZ(val) ({ \
const uint8_t pwmval = SYSCLOCK / 2000 / (val); \ const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
@@ -551,6 +545,28 @@
#define TIMER_PWM_PIN 1 /* ATtiny85 */ #define TIMER_PWM_PIN 1 /* ATtiny85 */
//---------------------------------------------------------
// ESP32 (ESP8266 should likely be added here too)
//
// ESP32 has it own timer API and does not use these macros, but to avoid ifdef'ing
// them out in the common code, they are defined to no-op. This allows the code to compile
// (which it wouldn't otherwise) but irsend will not work until ESP32 specific code is written
// for that -- merlin
// As a warning, sending timing specific code from an ESP32 can be challenging if you need 100%
// reliability because the arduino code may be interrupted and cause your sent waveform to be the
// wrong length. This is specifically an issue for neopixels which require 800Khz resolution.
// IR may just work as is with the common code since it's lower frequency, but if not, the other
// 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 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
//--------------------------------------------------------- //---------------------------------------------------------
// Unknown Timer // Unknown Timer
// //

View File

@@ -8,11 +8,7 @@
#include <IRremote.h> #include <IRremote.h>
#ifdef ESP32
int RECV_PIN = 35;
#else
int RECV_PIN = 11; int RECV_PIN = 11;
#endif
IRrecv irrecv(RECV_PIN); IRrecv irrecv(RECV_PIN);
@@ -20,7 +16,9 @@ decode_results results;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(9600);
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
Serial.println("Enabling IRin"); Serial.println("Enabling IRin");
irrecv.enableIRIn(); // Start the receiver irrecv.enableIRIn(); // Start the receiver
Serial.println("Enabled IRin"); Serial.println("Enabled IRin");

View File

@@ -15,11 +15,7 @@
* You can change this to another available Arduino Pin. * You can change this to another available Arduino Pin.
* Your IR receiver should be connected to the pin defined here * Your IR receiver should be connected to the pin defined here
*/ */
#ifdef ESP32
int RECV_PIN = 35;
#else
int RECV_PIN = 11; int RECV_PIN = 11;
#endif
IRrecv irrecv(RECV_PIN); IRrecv irrecv(RECV_PIN);

View File

@@ -6,7 +6,7 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Tell IRremote which Arduino pin is connected to the IR Receiver (TSOP4838) // Tell IRremote which Arduino pin is connected to the IR Receiver (TSOP4838)
// //
int recvPin = 35; int recvPin = 11;
IRrecv irrecv(recvPin); IRrecv irrecv(recvPin);
//+============================================================================= //+=============================================================================
@@ -14,7 +14,7 @@ IRrecv irrecv(recvPin);
// //
void setup ( ) void setup ( )
{ {
Serial.begin(115200); // Status message will be sent to PC at 9600 baud Serial.begin(9600); // Status message will be sent to PC at 9600 baud
irrecv.enableIRIn(); // Start the receiver irrecv.enableIRIn(); // Start the receiver
} }

View File

@@ -124,6 +124,8 @@ void IRrecv::enableIRIn ( )
{ {
// Interrupt Service Routine - Fires every 50uS // Interrupt Service Routine - Fires every 50uS
#ifdef ESP32 #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 // 3 timers, choose #1, 80 divider nanosecond precision, 1 to count up
timer = timerBegin(1, 80, 1); timer = timerBegin(1, 80, 1);
timerAttachInterrupt(timer, &IRTimer, 1); timerAttachInterrupt(timer, &IRTimer, 1);

View File

@@ -54,7 +54,7 @@ void IRsend::space (unsigned int time)
// //
void IRsend::enableIROut (int khz) void IRsend::enableIROut (int khz)
{ {
// FIXME: implement ESP32 support // FIXME: implement ESP32 support, see IR_TIMER_USE_ESP32 in boarddefs.h
#ifndef ESP32 #ifndef ESP32
// Disable the Timer2 Interrupt (which is used for receiving IR) // Disable the Timer2 Interrupt (which is used for receiving IR)
TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt