Go to the documentation of this file. 26 #define HAS_AVR_INTERRUPT_H 29 #define SENDING_SUPPORTED 33 #define USE_DEFAULT_ENABLE_IR_IN 40 #define PULSE_CORRECTION 3 52 #if defined(CORE_LED0_PIN) 53 # define BLINKLED CORE_LED0_PIN 54 # define BLINKLED_ON() (digitalWrite(CORE_LED0_PIN, HIGH)) 55 # define BLINKLED_OFF() (digitalWrite(CORE_LED0_PIN, LOW)) 57 #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 59 # define BLINKLED_ON() (PORTB |= B10000000) 60 # define BLINKLED_OFF() (PORTB &= B01111111) 62 #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) 64 # define BLINKLED_ON() (PORTD |= B00000001) 65 # define BLINKLED_OFF() (PORTD &= B11111110) 67 #elif defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD) 68 # define BLINKLED LED_BUILTIN 69 # define BLINKLED_ON() (digitalWrite(LED_BUILTIN, HIGH)) 70 # define BLINKLED_OFF() (digitalWrite(LED_BUILTIN, LOW)) 72 # define USE_SOFT_CARRIER 74 # define USE_SPIN_WAIT 75 # undef USE_DEFAULT_ENABLE_IR_IN 84 # undef HAS_AVR_INTERRUPT_H 87 # undef SENDING_SUPPORTED# 90 # undef USE_DEFAULT_ENABLE_IR_IN 92 #elif defined(ESP8266) 95 # undef HAS_AVR_INTERRUPT_H 97 # define USE_SOFT_CARRIER 100 # undef USE_DEFAULT_ENABLE_IR_IN 103 # define SEND_PIN 4 // pin 4 = D2 on board 107 # define BLINKLED_ON() (PORTB |= B00100000) 108 # define BLINKLED_OFF() (PORTB &= B11011111) 115 # define SYSCLOCK F_CPU // main Arduino clock 117 # define SYSCLOCK 16000000 // main Arduino clock 121 #define USECPERTICK 50 132 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 134 #define IR_USE_TIMER2 // tx = pin 9 140 #elif defined(__AVR_AT90USB162__) 141 #define IR_USE_TIMER1 // tx = pin 17 144 #elif defined(__AVR_ATmega32U4__) 147 #define IR_USE_TIMER4_HS // tx = pin 10 150 #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) 151 #define IR_USE_TIMER_CMT // tx = pin 5 154 #elif defined(__MKL26Z64__) 155 #define IR_USE_TIMER_TPM1 // tx = pin 16 158 #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) 160 #define IR_USE_TIMER2 // tx = pin 1 164 #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) 166 #define IR_USE_TIMER2 // tx = pin 14 170 #elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \ 171 || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \ 172 || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \ 173 || defined(__AVR_ATmega164P__) 175 #define IR_USE_TIMER2 // tx = pin 14 178 #elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) 179 #define IR_USE_TIMER1 // tx = pin 13 182 #elif defined(__AVR_ATmega8535__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) 183 #define IR_USE_TIMER1 // tx = pin 13 186 #elif defined(__AVR_ATmega8__) 187 #define IR_USE_TIMER1 // tx = pin 9 190 #elif defined(__AVR_ATtiny84__) 191 #define IR_USE_TIMER1 // tx = pin 6 194 #elif defined(__AVR_ATtiny85__) 195 #define IR_USE_TIMER_TINY0 // tx = pin 1 198 #define IR_TIMER_USE_ESP32 200 #elif defined(ESP8266) 202 #elif defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD) 203 #define TIMER_PRESCALER_DIV 64 209 #define IR_USE_TIMER2 // tx = pin 3 219 #if defined(IR_USE_TIMER2) 222 #define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1)) 223 #define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1))) 224 #define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A)) 225 #define TIMER_DISABLE_INTR (TIMSK2 = 0) 226 #define TIMER_INTR_NAME TIMER2_COMPA_vect 228 #define TIMER_CONFIG_KHZ(val) ({ \ 229 const uint8_t pwmval = SYSCLOCK / 2000 / (val); \ 230 TCCR2A = _BV(WGM20); \ 231 TCCR2B = _BV(WGM22) | _BV(CS20); \ 233 OCR2B = pwmval / 3; \ 236 #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000) 239 #if (TIMER_COUNT_TOP < 256) 240 # define TIMER_CONFIG_NORMAL() ({ \ 241 TCCR2A = _BV(WGM21); \ 242 TCCR2B = _BV(CS20); \ 243 OCR2A = TIMER_COUNT_TOP; \ 247 # define TIMER_CONFIG_NORMAL() ({ \ 248 TCCR2A = _BV(WGM21); \ 249 TCCR2B = _BV(CS21); \ 250 OCR2A = TIMER_COUNT_TOP / 8; \ 256 #if defined(CORE_OC2B_PIN) 257 # define SEND_PIN CORE_OC2B_PIN // Teensy 258 #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 259 # define SEND_PIN 9 // Arduino Mega 260 #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) \ 261 || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \ 262 || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \ 263 || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \ 264 || defined(__AVR_ATmega164P__) 265 # define SEND_PIN 14 // MightyCore 267 # define SEND_PIN 3 // Arduino Duemilanove, Diecimila, LilyPad, etc 268 #endif // ATmega48, ATmega88, ATmega168, ATmega328 273 #elif defined(IR_USE_TIMER1) 276 #define TIMER_ENABLE_PWM (TCCR1A |= _BV(COM1A1)) 277 #define TIMER_DISABLE_PWM (TCCR1A &= ~(_BV(COM1A1))) 280 #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega8535__) \ 281 || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \ 282 || defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) 283 # define TIMER_ENABLE_INTR (TIMSK |= _BV(OCIE1A)) 284 # define TIMER_DISABLE_INTR (TIMSK &= ~_BV(OCIE1A)) 286 # define TIMER_ENABLE_INTR (TIMSK1 = _BV(OCIE1A)) 287 # define TIMER_DISABLE_INTR (TIMSK1 = 0) 291 #define TIMER_INTR_NAME TIMER1_COMPA_vect 293 #define TIMER_CONFIG_KHZ(val) ({ \ 294 const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ 295 TCCR1A = _BV(WGM11); \ 296 TCCR1B = _BV(WGM13) | _BV(CS10); \ 298 OCR1A = pwmval / 3; \ 301 #define TIMER_CONFIG_NORMAL() ({ \ 303 TCCR1B = _BV(WGM12) | _BV(CS10); \ 304 OCR1A = SYSCLOCK * USECPERTICK / 1000000; \ 309 #if defined(CORE_OC1A_PIN) 310 # define SEND_PIN CORE_OC1A_PIN // Teensy 311 #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 312 # define SEND_PIN 11 // Arduino Mega 313 #elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) 314 # define SEND_PIN 13 // MegaCore 315 #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) \ 316 || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \ 317 || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \ 318 || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \ 319 || defined(__AVR_ATmega164P__) || defined(__AVR_ATmega32__) \ 320 || defined(__AVR_ATmega16__) || defined(__AVR_ATmega8535__) 321 # define SEND_PIN 13 // MightyCore 322 #elif defined(__AVR_ATtiny84__) 325 # define SEND_PIN 9 // Arduino Duemilanove, Diecimila, LilyPad, etc 326 #endif // ATmega48, ATmega88, ATmega168, ATmega328 331 #elif defined(IR_USE_TIMER3) 334 #define TIMER_ENABLE_PWM (TCCR3A |= _BV(COM3A1)) 335 #define TIMER_DISABLE_PWM (TCCR3A &= ~(_BV(COM3A1))) 336 #define TIMER_ENABLE_INTR (TIMSK3 = _BV(OCIE3A)) 337 #define TIMER_DISABLE_INTR (TIMSK3 = 0) 338 #define TIMER_INTR_NAME TIMER3_COMPA_vect 340 #define TIMER_CONFIG_KHZ(val) ({ \ 341 const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ 342 TCCR3A = _BV(WGM31); \ 343 TCCR3B = _BV(WGM33) | _BV(CS30); \ 345 OCR3A = pwmval / 3; \ 348 #define TIMER_CONFIG_NORMAL() ({ \ 350 TCCR3B = _BV(WGM32) | _BV(CS30); \ 351 OCR3A = SYSCLOCK * USECPERTICK / 1000000; \ 356 #if defined(CORE_OC3A_PIN) 357 # define SEND_PIN CORE_OC3A_PIN // Teensy 358 #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 359 # define SEND_PIN 5 // Arduino Mega 360 #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) 361 # define SEND_PIN 6 // MightyCore 363 # error "Please add OC3A pin number here\n" 369 #elif defined(IR_USE_TIMER4_HS) 372 #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1)) 373 #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1))) 374 #define TIMER_ENABLE_INTR (TIMSK4 = _BV(TOIE4)) 375 #define TIMER_DISABLE_INTR (TIMSK4 = 0) 376 #define TIMER_INTR_NAME TIMER4_OVF_vect 378 #define TIMER_CONFIG_KHZ(val) ({ \ 379 const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ 380 TCCR4A = (1<<PWM4A); \ 381 TCCR4B = _BV(CS40); \ 383 TCCR4D = (1<<WGM40); \ 385 TC4H = pwmval >> 8; \ 387 TC4H = (pwmval / 3) >> 8; \ 388 OCR4A = (pwmval / 3) & 255; \ 391 #define TIMER_CONFIG_NORMAL() ({ \ 393 TCCR4B = _BV(CS40); \ 397 TC4H = (SYSCLOCK * USECPERTICK / 1000000) >> 8; \ 398 OCR4C = (SYSCLOCK * USECPERTICK / 1000000) & 255; \ 404 #if defined(CORE_OC4A_PIN) 405 # define SEND_PIN CORE_OC4A_PIN // Teensy 406 #elif defined(__AVR_ATmega32U4__) 407 # define SEND_PIN 13 // Leonardo 409 # error "Please add OC4A pin number here\n" 415 #elif defined(IR_USE_TIMER4) 418 #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1)) 419 #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1))) 420 #define TIMER_ENABLE_INTR (TIMSK4 = _BV(OCIE4A)) 421 #define TIMER_DISABLE_INTR (TIMSK4 = 0) 422 #define TIMER_INTR_NAME TIMER4_COMPA_vect 424 #define TIMER_CONFIG_KHZ(val) ({ \ 425 const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ 426 TCCR4A = _BV(WGM41); \ 427 TCCR4B = _BV(WGM43) | _BV(CS40); \ 429 OCR4A = pwmval / 3; \ 432 #define TIMER_CONFIG_NORMAL() ({ \ 434 TCCR4B = _BV(WGM42) | _BV(CS40); \ 435 OCR4A = SYSCLOCK * USECPERTICK / 1000000; \ 440 #if defined(CORE_OC4A_PIN) 441 # define SEND_PIN CORE_OC4A_PIN 442 #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 443 # define SEND_PIN 6 // Arduino Mega 445 # error "Please add OC4A pin number here\n" 451 #elif defined(IR_USE_TIMER5) 454 #define TIMER_ENABLE_PWM (TCCR5A |= _BV(COM5A1)) 455 #define TIMER_DISABLE_PWM (TCCR5A &= ~(_BV(COM5A1))) 456 #define TIMER_ENABLE_INTR (TIMSK5 = _BV(OCIE5A)) 457 #define TIMER_DISABLE_INTR (TIMSK5 = 0) 458 #define TIMER_INTR_NAME TIMER5_COMPA_vect 460 #define TIMER_CONFIG_KHZ(val) ({ \ 461 const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ 462 TCCR5A = _BV(WGM51); \ 463 TCCR5B = _BV(WGM53) | _BV(CS50); \ 465 OCR5A = pwmval / 3; \ 468 #define TIMER_CONFIG_NORMAL() ({ \ 470 TCCR5B = _BV(WGM52) | _BV(CS50); \ 471 OCR5A = SYSCLOCK * USECPERTICK / 1000000; \ 476 #if defined(CORE_OC5A_PIN) 477 # define SEND_PIN CORE_OC5A_PIN 478 #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 479 # define SEND_PIN 46 // Arduino Mega 481 # error "Please add OC5A pin number here\n" 487 #elif defined(IR_USE_TIMER_CMT) 489 #define TIMER_RESET ({ \ 490 uint8_t tmp __attribute__((unused)) = CMT_MSC; \ 494 #define TIMER_ENABLE_PWM do { \ 495 CORE_PIN5_CONFIG = PORT_PCR_MUX(2) | PORT_PCR_DSE | PORT_PCR_SRE; \ 498 #define TIMER_DISABLE_PWM do { \ 499 CORE_PIN5_CONFIG = PORT_PCR_MUX(1) | PORT_PCR_DSE | PORT_PCR_SRE; \ 502 #define TIMER_ENABLE_INTR NVIC_ENABLE_IRQ(IRQ_CMT) 503 #define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_CMT) 504 #define TIMER_INTR_NAME cmt_isr 510 #define ISR(f) void f(void) 513 #define CMT_PPS_DIV ((F_BUS + 7999999) / 8000000) 515 #error IRremote requires at least 8 MHz on Teensy 3.x 519 #define TIMER_CONFIG_KHZ(val) ({ \ 520 SIM_SCGC4 |= SIM_SCGC4_CMT; \ 521 SIM_SOPT2 |= SIM_SOPT2_PTD7PAD; \ 522 CMT_PPS = CMT_PPS_DIV - 1; \ 523 CMT_CGH1 = ((F_BUS / CMT_PPS_DIV / 3000) + ((val)/2)) / (val); \ 524 CMT_CGL1 = ((F_BUS / CMT_PPS_DIV / 1500) + ((val)/2)) / (val); \ 533 #define TIMER_CONFIG_NORMAL() ({ \ 534 SIM_SCGC4 |= SIM_SCGC4_CMT; \ 535 CMT_PPS = CMT_PPS_DIV - 1; \ 541 CMT_CMD4 = (F_BUS / 160000 + CMT_PPS_DIV / 2) / CMT_PPS_DIV - 31; \ 549 #elif defined(IR_USE_TIMER_TPM1) 550 #define TIMER_RESET FTM1_SC |= FTM_SC_TOF; 551 #define TIMER_ENABLE_PWM CORE_PIN16_CONFIG = PORT_PCR_MUX(3)|PORT_PCR_DSE|PORT_PCR_SRE 552 #define TIMER_DISABLE_PWM CORE_PIN16_CONFIG = PORT_PCR_MUX(1)|PORT_PCR_SRE 553 #define TIMER_ENABLE_INTR NVIC_ENABLE_IRQ(IRQ_FTM1) 554 #define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_FTM1) 555 #define TIMER_INTR_NAME ftm1_isr 559 #define ISR(f) void f(void) 560 #define TIMER_CONFIG_KHZ(val) ({ \ 561 SIM_SCGC6 |= SIM_SCGC6_TPM1; \ 564 FTM1_MOD = (F_PLL/2000) / val - 1; \ 565 FTM1_C0V = (F_PLL/6000) / val - 1; \ 566 FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0); \ 568 #define TIMER_CONFIG_NORMAL() ({ \ 569 SIM_SCGC6 |= SIM_SCGC6_TPM1; \ 572 FTM1_MOD = (F_PLL/40000) - 1; \ 574 FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0) | FTM_SC_TOF | FTM_SC_TOIE; \ 579 #elif defined(IR_USE_TIMER_TINY0) 581 #define TIMER_ENABLE_PWM (TCCR0A |= _BV(COM0B1)) 582 #define TIMER_DISABLE_PWM (TCCR0A &= ~(_BV(COM0B1))) 583 #define TIMER_ENABLE_INTR (TIMSK |= _BV(OCIE0A)) 584 #define TIMER_DISABLE_INTR (TIMSK &= ~(_BV(OCIE0A))) 585 #define TIMER_INTR_NAME TIMER0_COMPA_vect 586 #define TIMER_CONFIG_KHZ(val) ({ \ 587 const uint8_t pwmval = SYSCLOCK / 2000 / (val); \ 588 TCCR0A = _BV(WGM00); \ 589 TCCR0B = _BV(WGM02) | _BV(CS00); \ 591 OCR0B = pwmval / 3; \ 593 #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000) 594 #if (TIMER_COUNT_TOP < 256) 595 #define TIMER_CONFIG_NORMAL() ({ \ 596 TCCR0A = _BV(WGM01); \ 597 TCCR0B = _BV(CS00); \ 598 OCR0A = TIMER_COUNT_TOP; \ 602 #define TIMER_CONFIG_NORMAL() ({ \ 603 TCCR0A = _BV(WGM01); \ 604 TCCR0B = _BV(CS01); \ 605 OCR0A = TIMER_COUNT_TOP / 8; \ 626 #elif defined(IR_TIMER_USE_ESP32) 633 #define ISR(f) void IRTimer() 635 #elif defined(ESP8266) 641 #define TIMER_DISABLE_PWM 642 #define TIMER_DISABLE_INTR 643 #define TIMER_CONFIG_KHZ(khz) 649 #elif defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD) 653 #define TIMER_ENABLE_PWM // Not presently used 654 #define TIMER_DISABLE_PWM 655 #define TIMER_ENABLE_INTR NVIC_EnableIRQ(TC3_IRQn) // Not presently used 656 #define TIMER_DISABLE_INTR NVIC_DisableIRQ(TC3_IRQn) 657 #define TIMER_INTR_NAME TC3_Handler // Not presently used 658 #define TIMER_CONFIG_KHZ(f) 663 #define ISR(f) void irs() 669 # error "Internal code configuration error, no known IR_USE_TIMER# defined\n" 674 #define SENDPIN_ON(pin) digitalWrite(pin, HIGH) 678 #define SENDPIN_OFF(pin) digitalWrite(pin, LOW)