PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

boarddefs.h 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. //******************************************************************************
  2. // IRremote
  3. // Version 2.0.1 June, 2015
  4. // Copyright 2009 Ken Shirriff
  5. // For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
  6. // This file contains all board specific information. It was previously contained within
  7. // IRremoteInt.h
  8. // Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
  9. //
  10. // Interrupt code based on NECIRrcv by Joe Knapp
  11. // http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
  12. // Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
  13. //
  14. // JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
  15. // Whynter A/C ARC-110WD added by Francesco Meschia
  16. // Sparkfun Pro Micro support by Alastair McCormack
  17. //******************************************************************************
  18. #ifndef boarddefs_h
  19. #define boarddefs_h
  20. //------------------------------------------------------------------------------
  21. // Defines for blinking the LED
  22. //
  23. #if defined(CORE_LED0_PIN)
  24. # define BLINKLED CORE_LED0_PIN
  25. # define BLINKLED_ON() (digitalWrite(CORE_LED0_PIN, HIGH))
  26. # define BLINKLED_OFF() (digitalWrite(CORE_LED0_PIN, LOW))
  27. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  28. # define BLINKLED 13
  29. # define BLINKLED_ON() (PORTB |= B10000000)
  30. # define BLINKLED_OFF() (PORTB &= B01111111)
  31. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  32. # define BLINKLED 0
  33. # define BLINKLED_ON() (PORTD |= B00000001)
  34. # define BLINKLED_OFF() (PORTD &= B11111110)
  35. // No system LED on ESP32, disable blinking
  36. #elif defined(ESP32)
  37. # define BLINKLED 255
  38. # define BLINKLED_ON() 1
  39. # define BLINKLED_OFF() 1
  40. #else
  41. # define BLINKLED 13
  42. # define BLINKLED_ON() (PORTB |= B00100000)
  43. # define BLINKLED_OFF() (PORTB &= B11011111)
  44. #endif
  45. //------------------------------------------------------------------------------
  46. // CPU Frequency
  47. //
  48. #ifdef F_CPU
  49. # define SYSCLOCK F_CPU // main Arduino clock
  50. #else
  51. # define SYSCLOCK 16000000 // main Arduino clock
  52. #endif
  53. // microseconds per clock interrupt tick
  54. #define USECPERTICK 50
  55. //------------------------------------------------------------------------------
  56. // Define which timer to use
  57. //
  58. // Uncomment the timer you wish to use on your board.
  59. // If you are using another library which uses timer2, you have options to
  60. // switch IRremote to use a different timer.
  61. //
  62. // Sparkfun Pro Micro
  63. #if defined(ARDUINO_AVR_PROMICRO)
  64. //#define IR_USE_TIMER1 // tx = pin 9
  65. #define IR_USE_TIMER3 // tx = pin 5
  66. //#define IR_USE_TIMER4_HS // tx = pin 5
  67. // Arduino Mega
  68. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  69. //#define IR_USE_TIMER1 // tx = pin 11
  70. #define IR_USE_TIMER2 // tx = pin 9
  71. //#define IR_USE_TIMER3 // tx = pin 5
  72. //#define IR_USE_TIMER4 // tx = pin 6
  73. //#define IR_USE_TIMER5 // tx = pin 46
  74. // Teensy 1.0
  75. #elif defined(__AVR_AT90USB162__)
  76. #define IR_USE_TIMER1 // tx = pin 17
  77. // Teensy 2.0
  78. #elif defined(__AVR_ATmega32U4__)
  79. //#define IR_USE_TIMER1 // tx = pin 14
  80. //#define IR_USE_TIMER3 // tx = pin 9
  81. #define IR_USE_TIMER4_HS // tx = pin 10
  82. // Teensy 3.0 / Teensy 3.1
  83. #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  84. #define IR_USE_TIMER_CMT // tx = pin 5
  85. // Teensy-LC
  86. #elif defined(__MKL26Z64__)
  87. #define IR_USE_TIMER_TPM1 // tx = pin 16
  88. // Teensy 4
  89. #elif defined(__IMXRT1062__)
  90. #define IR_USE_TIMER_FLEXPWM1 // tx = pin 8
  91. // Teensy++ 1.0 & 2.0
  92. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  93. //#define IR_USE_TIMER1 // tx = pin 25
  94. #define IR_USE_TIMER2 // tx = pin 1
  95. //#define IR_USE_TIMER3 // tx = pin 16
  96. // MightyCore - ATmega1284
  97. #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
  98. //#define IR_USE_TIMER1 // tx = pin 13
  99. #define IR_USE_TIMER2 // tx = pin 14
  100. //#define IR_USE_TIMER3 // tx = pin 6
  101. // MightyCore - ATmega164, ATmega324, ATmega644
  102. #elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \
  103. || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \
  104. || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \
  105. || defined(__AVR_ATmega164P__)
  106. //#define IR_USE_TIMER1 // tx = pin 13
  107. #define IR_USE_TIMER2 // tx = pin 14
  108. //MegaCore - ATmega64, ATmega128
  109. #elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
  110. #define IR_USE_TIMER1 // tx = pin 13
  111. // MightyCore - ATmega8535, ATmega16, ATmega32
  112. #elif defined(__AVR_ATmega8535__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__)
  113. #define IR_USE_TIMER1 // tx = pin 13
  114. // Atmega8
  115. #elif defined(__AVR_ATmega8__)
  116. #define IR_USE_TIMER1 // tx = pin 9
  117. // ATtiny84
  118. #elif defined(__AVR_ATtiny84__)
  119. #define IR_USE_TIMER1 // tx = pin 6
  120. //ATtiny85
  121. #elif defined(__AVR_ATtiny85__)
  122. #define IR_USE_TIMER_TINY0 // tx = pin 1
  123. #elif defined(ESP32)
  124. #define IR_TIMER_USE_ESP32
  125. #else
  126. // Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
  127. // ATmega48, ATmega88, ATmega168, ATmega328
  128. //#define IR_USE_TIMER1 // tx = pin 9
  129. #define IR_USE_TIMER2 // tx = pin 3
  130. #endif
  131. //------------------------------------------------------------------------------
  132. // Defines for Timer
  133. //---------------------------------------------------------
  134. // Timer2 (8 bits)
  135. //
  136. #if defined(IR_USE_TIMER2)
  137. #define TIMER_RESET
  138. #define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))
  139. #define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1)))
  140. #define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A))
  141. #define TIMER_DISABLE_INTR (TIMSK2 = 0)
  142. #define TIMER_INTR_NAME TIMER2_COMPA_vect
  143. #define TIMER_CONFIG_KHZ(val) ({ \
  144. const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
  145. TCCR2A = _BV(WGM20); \
  146. TCCR2B = _BV(WGM22) | _BV(CS20); \
  147. OCR2A = pwmval; \
  148. OCR2B = pwmval / 3; \
  149. })
  150. #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000)
  151. //-----------------
  152. #if (TIMER_COUNT_TOP < 256)
  153. # define TIMER_CONFIG_NORMAL() ({ \
  154. TCCR2A = _BV(WGM21); \
  155. TCCR2B = _BV(CS20); \
  156. OCR2A = TIMER_COUNT_TOP; \
  157. TCNT2 = 0; \
  158. })
  159. #else
  160. # define TIMER_CONFIG_NORMAL() ({ \
  161. TCCR2A = _BV(WGM21); \
  162. TCCR2B = _BV(CS21); \
  163. OCR2A = TIMER_COUNT_TOP / 8; \
  164. TCNT2 = 0; \
  165. })
  166. #endif
  167. //-----------------
  168. #if defined(CORE_OC2B_PIN)
  169. # define TIMER_PWM_PIN CORE_OC2B_PIN // Teensy
  170. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  171. # define TIMER_PWM_PIN 9 // Arduino Mega
  172. #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) \
  173. || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \
  174. || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \
  175. || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \
  176. || defined(__AVR_ATmega164P__)
  177. # define TIMER_PWM_PIN 14 // MightyCore
  178. #else
  179. # define TIMER_PWM_PIN 3 // Arduino Duemilanove, Diecimila, LilyPad, etc
  180. #endif // ATmega48, ATmega88, ATmega168, ATmega328
  181. //---------------------------------------------------------
  182. // Timer1 (16 bits)
  183. //
  184. #elif defined(IR_USE_TIMER1)
  185. #define TIMER_RESET
  186. #define TIMER_ENABLE_PWM (TCCR1A |= _BV(COM1A1))
  187. #define TIMER_DISABLE_PWM (TCCR1A &= ~(_BV(COM1A1)))
  188. //-----------------
  189. #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega8535__) \
  190. || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \
  191. || defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
  192. # define TIMER_ENABLE_INTR (TIMSK |= _BV(OCIE1A))
  193. # define TIMER_DISABLE_INTR (TIMSK &= ~_BV(OCIE1A))
  194. #else
  195. # define TIMER_ENABLE_INTR (TIMSK1 = _BV(OCIE1A))
  196. # define TIMER_DISABLE_INTR (TIMSK1 = 0)
  197. #endif
  198. //-----------------
  199. #define TIMER_INTR_NAME TIMER1_COMPA_vect
  200. #define TIMER_CONFIG_KHZ(val) ({ \
  201. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  202. TCCR1A = _BV(WGM11); \
  203. TCCR1B = _BV(WGM13) | _BV(CS10); \
  204. ICR1 = pwmval; \
  205. OCR1A = pwmval / 3; \
  206. })
  207. #define TIMER_CONFIG_NORMAL() ({ \
  208. TCCR1A = 0; \
  209. TCCR1B = _BV(WGM12) | _BV(CS10); \
  210. OCR1A = SYSCLOCK * USECPERTICK / 1000000; \
  211. TCNT1 = 0; \
  212. })
  213. //-----------------
  214. #if defined(CORE_OC1A_PIN)
  215. # define TIMER_PWM_PIN CORE_OC1A_PIN // Teensy
  216. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  217. # define TIMER_PWM_PIN 11 // Arduino Mega
  218. #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) \
  219. || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \
  220. || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \
  221. || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \
  222. || defined(__AVR_ATmega164P__) || defined(__AVR_ATmega32__) \
  223. || defined(__AVR_ATmega16__) || defined(__AVR_ATmega8535__) \
  224. || defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
  225. # define TIMER_PWM_PIN 13 // MightyCore, MegaCore
  226. #elif defined(__AVR_ATtiny84__)
  227. # define TIMER_PWM_PIN 6
  228. #else
  229. # define TIMER_PWM_PIN 9 // Arduino Duemilanove, Diecimila, LilyPad, Sparkfun Pro Micro etc
  230. #endif // ATmega48, ATmega88, ATmega168, ATmega328
  231. //---------------------------------------------------------
  232. // Timer3 (16 bits)
  233. //
  234. #elif defined(IR_USE_TIMER3)
  235. #define TIMER_RESET
  236. #define TIMER_ENABLE_PWM (TCCR3A |= _BV(COM3A1))
  237. #define TIMER_DISABLE_PWM (TCCR3A &= ~(_BV(COM3A1)))
  238. #define TIMER_ENABLE_INTR (TIMSK3 = _BV(OCIE3A))
  239. #define TIMER_DISABLE_INTR (TIMSK3 = 0)
  240. #define TIMER_INTR_NAME TIMER3_COMPA_vect
  241. #define TIMER_CONFIG_KHZ(val) ({ \
  242. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  243. TCCR3A = _BV(WGM31); \
  244. TCCR3B = _BV(WGM33) | _BV(CS30); \
  245. ICR3 = pwmval; \
  246. OCR3A = pwmval / 3; \
  247. })
  248. #define TIMER_CONFIG_NORMAL() ({ \
  249. TCCR3A = 0; \
  250. TCCR3B = _BV(WGM32) | _BV(CS30); \
  251. OCR3A = SYSCLOCK * USECPERTICK / 1000000; \
  252. TCNT3 = 0; \
  253. })
  254. //-----------------
  255. #if defined(CORE_OC3A_PIN)
  256. # define TIMER_PWM_PIN CORE_OC3A_PIN // Teensy
  257. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ARDUINO_AVR_PROMICRO)
  258. # define TIMER_PWM_PIN 5 // Arduino Mega, Sparkfun Pro Micro
  259. #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
  260. # define TIMER_PWM_PIN 6 // MightyCore
  261. #else
  262. # error "Please add OC3A pin number here\n"
  263. #endif
  264. //---------------------------------------------------------
  265. // Timer4 (10 bits, high speed option)
  266. //
  267. #elif defined(IR_USE_TIMER4_HS)
  268. #define TIMER_RESET
  269. #if defined(ARDUINO_AVR_PROMICRO) // Sparkfun Pro Micro
  270. #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A0)) // Use complimentary O̅C̅4̅A̅ output on pin 5
  271. #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A0))) // (Pro Micro does not map PC7 (32/ICP3/CLK0/OC4A)
  272. // of ATmega32U4 )
  273. #else
  274. #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1))
  275. #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1)))
  276. #endif
  277. #define TIMER_ENABLE_INTR (TIMSK4 = _BV(TOIE4))
  278. #define TIMER_DISABLE_INTR (TIMSK4 = 0)
  279. #define TIMER_INTR_NAME TIMER4_OVF_vect
  280. #define TIMER_CONFIG_KHZ(val) ({ \
  281. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  282. TCCR4A = (1<<PWM4A); \
  283. TCCR4B = _BV(CS40); \
  284. TCCR4C = 0; \
  285. TCCR4D = (1<<WGM40); \
  286. TCCR4E = 0; \
  287. TC4H = pwmval >> 8; \
  288. OCR4C = pwmval; \
  289. TC4H = (pwmval / 3) >> 8; \
  290. OCR4A = (pwmval / 3) & 255; \
  291. })
  292. #define TIMER_CONFIG_NORMAL() ({ \
  293. TCCR4A = 0; \
  294. TCCR4B = _BV(CS40); \
  295. TCCR4C = 0; \
  296. TCCR4D = 0; \
  297. TCCR4E = 0; \
  298. TC4H = (SYSCLOCK * USECPERTICK / 1000000) >> 8; \
  299. OCR4C = (SYSCLOCK * USECPERTICK / 1000000) & 255; \
  300. TC4H = 0; \
  301. TCNT4 = 0; \
  302. })
  303. //-----------------
  304. #if defined(CORE_OC4A_PIN)
  305. # define TIMER_PWM_PIN CORE_OC4A_PIN // Teensy
  306. #elif defined(ARDUINO_AVR_PROMICRO)
  307. # define TIMER_PWM_PIN 5 // Sparkfun Pro Micro
  308. #elif defined(__AVR_ATmega32U4__)
  309. # define TIMER_PWM_PIN 13 // Leonardo
  310. #else
  311. # error "Please add OC4A pin number here\n"
  312. #endif
  313. //---------------------------------------------------------
  314. // Timer4 (16 bits)
  315. //
  316. #elif defined(IR_USE_TIMER4)
  317. #define TIMER_RESET
  318. #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1))
  319. #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1)))
  320. #define TIMER_ENABLE_INTR (TIMSK4 = _BV(OCIE4A))
  321. #define TIMER_DISABLE_INTR (TIMSK4 = 0)
  322. #define TIMER_INTR_NAME TIMER4_COMPA_vect
  323. #define TIMER_CONFIG_KHZ(val) ({ \
  324. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  325. TCCR4A = _BV(WGM41); \
  326. TCCR4B = _BV(WGM43) | _BV(CS40); \
  327. ICR4 = pwmval; \
  328. OCR4A = pwmval / 3; \
  329. })
  330. #define TIMER_CONFIG_NORMAL() ({ \
  331. TCCR4A = 0; \
  332. TCCR4B = _BV(WGM42) | _BV(CS40); \
  333. OCR4A = SYSCLOCK * USECPERTICK / 1000000; \
  334. TCNT4 = 0; \
  335. })
  336. //-----------------
  337. #if defined(CORE_OC4A_PIN)
  338. # define TIMER_PWM_PIN CORE_OC4A_PIN
  339. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  340. # define TIMER_PWM_PIN 6 // Arduino Mega
  341. #else
  342. # error "Please add OC4A pin number here\n"
  343. #endif
  344. //---------------------------------------------------------
  345. // Timer5 (16 bits)
  346. //
  347. #elif defined(IR_USE_TIMER5)
  348. #define TIMER_RESET
  349. #define TIMER_ENABLE_PWM (TCCR5A |= _BV(COM5A1))
  350. #define TIMER_DISABLE_PWM (TCCR5A &= ~(_BV(COM5A1)))
  351. #define TIMER_ENABLE_INTR (TIMSK5 = _BV(OCIE5A))
  352. #define TIMER_DISABLE_INTR (TIMSK5 = 0)
  353. #define TIMER_INTR_NAME TIMER5_COMPA_vect
  354. #define TIMER_CONFIG_KHZ(val) ({ \
  355. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  356. TCCR5A = _BV(WGM51); \
  357. TCCR5B = _BV(WGM53) | _BV(CS50); \
  358. ICR5 = pwmval; \
  359. OCR5A = pwmval / 3; \
  360. })
  361. #define TIMER_CONFIG_NORMAL() ({ \
  362. TCCR5A = 0; \
  363. TCCR5B = _BV(WGM52) | _BV(CS50); \
  364. OCR5A = SYSCLOCK * USECPERTICK / 1000000; \
  365. TCNT5 = 0; \
  366. })
  367. //-----------------
  368. #if defined(CORE_OC5A_PIN)
  369. # define TIMER_PWM_PIN CORE_OC5A_PIN
  370. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  371. # define TIMER_PWM_PIN 46 // Arduino Mega
  372. #else
  373. # error "Please add OC5A pin number here\n"
  374. #endif
  375. //---------------------------------------------------------
  376. // Special carrier modulator timer
  377. //
  378. #elif defined(IR_USE_TIMER_CMT)
  379. #define TIMER_RESET ({ \
  380. uint8_t tmp __attribute__((unused)) = CMT_MSC; \
  381. CMT_CMD2 = 30; \
  382. })
  383. #define TIMER_ENABLE_PWM do { \
  384. CORE_PIN5_CONFIG = PORT_PCR_MUX(2) | PORT_PCR_DSE | PORT_PCR_SRE; \
  385. } while(0)
  386. #define TIMER_DISABLE_PWM do { \
  387. CORE_PIN5_CONFIG = PORT_PCR_MUX(1) | PORT_PCR_DSE | PORT_PCR_SRE; \
  388. } while(0)
  389. #define TIMER_ENABLE_INTR NVIC_ENABLE_IRQ(IRQ_CMT)
  390. #define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_CMT)
  391. #define TIMER_INTR_NAME cmt_isr
  392. //-----------------
  393. #ifdef ISR
  394. # undef ISR
  395. #endif
  396. #define ISR(f) void f(void)
  397. //-----------------
  398. #define CMT_PPS_DIV ((F_BUS + 7999999) / 8000000)
  399. #if F_BUS < 8000000
  400. #error IRremote requires at least 8 MHz on Teensy 3.x
  401. #endif
  402. //-----------------
  403. #define TIMER_CONFIG_KHZ(val) ({ \
  404. SIM_SCGC4 |= SIM_SCGC4_CMT; \
  405. SIM_SOPT2 |= SIM_SOPT2_PTD7PAD; \
  406. CMT_PPS = CMT_PPS_DIV - 1; \
  407. CMT_CGH1 = ((F_BUS / CMT_PPS_DIV / 3000) + ((val)/2)) / (val); \
  408. CMT_CGL1 = ((F_BUS / CMT_PPS_DIV / 1500) + ((val)/2)) / (val); \
  409. CMT_CMD1 = 0; \
  410. CMT_CMD2 = 30; \
  411. CMT_CMD3 = 0; \
  412. CMT_CMD4 = 0; \
  413. CMT_OC = 0x60; \
  414. CMT_MSC = 0x01; \
  415. })
  416. #define TIMER_CONFIG_NORMAL() ({ \
  417. SIM_SCGC4 |= SIM_SCGC4_CMT; \
  418. CMT_PPS = CMT_PPS_DIV - 1; \
  419. CMT_CGH1 = 1; \
  420. CMT_CGL1 = 1; \
  421. CMT_CMD1 = 0; \
  422. CMT_CMD2 = 30; \
  423. CMT_CMD3 = 0; \
  424. CMT_CMD4 = (F_BUS / 160000 + CMT_PPS_DIV / 2) / CMT_PPS_DIV - 31; \
  425. CMT_OC = 0; \
  426. CMT_MSC = 0x03; \
  427. })
  428. #define TIMER_PWM_PIN 5
  429. // defines for TPM1 timer on Teensy-LC
  430. #elif defined(IR_USE_TIMER_TPM1)
  431. #define TIMER_RESET FTM1_SC |= FTM_SC_TOF;
  432. #define TIMER_ENABLE_PWM CORE_PIN16_CONFIG = PORT_PCR_MUX(3)|PORT_PCR_DSE|PORT_PCR_SRE
  433. #define TIMER_DISABLE_PWM CORE_PIN16_CONFIG = PORT_PCR_MUX(1)|PORT_PCR_SRE
  434. #define TIMER_ENABLE_INTR NVIC_ENABLE_IRQ(IRQ_FTM1)
  435. #define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_FTM1)
  436. #define TIMER_INTR_NAME ftm1_isr
  437. #ifdef ISR
  438. #undef ISR
  439. #endif
  440. #define ISR(f) void f(void)
  441. #define TIMER_CONFIG_KHZ(val) ({ \
  442. SIM_SCGC6 |= SIM_SCGC6_TPM1; \
  443. FTM1_SC = 0; \
  444. FTM1_CNT = 0; \
  445. FTM1_MOD = (F_PLL/2000) / val - 1; \
  446. FTM1_C0V = (F_PLL/6000) / val - 1; \
  447. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0); \
  448. })
  449. #define TIMER_CONFIG_NORMAL() ({ \
  450. SIM_SCGC6 |= SIM_SCGC6_TPM1; \
  451. FTM1_SC = 0; \
  452. FTM1_CNT = 0; \
  453. FTM1_MOD = (F_PLL/40000) - 1; \
  454. FTM1_C0V = 0; \
  455. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0) | FTM_SC_TOF | FTM_SC_TOIE; \
  456. })
  457. #define TIMER_PWM_PIN 16
  458. // defines for FlexPWM1 timer on Teensy 4
  459. #elif defined(IR_USE_TIMER_FLEXPWM1)
  460. #define TIMER_RESET FLEXPWM1_SM3STS = FLEXPWM_SMSTS_RF;
  461. #define TIMER_ENABLE_PWM FLEXPWM1_OUTEN |= FLEXPWM_OUTEN_PWMA_EN(8), \
  462. IOMUXC_SW_MUX_CTL_PAD_GPIO_B1_00 = 6
  463. #define TIMER_DISABLE_PWM IOMUXC_SW_MUX_CTL_PAD_GPIO_B1_00 = 5, \
  464. FLEXPWM1_OUTEN &= ~FLEXPWM_OUTEN_PWMA_EN(8)
  465. #define TIMER_ENABLE_INTR attachInterruptVector(IRQ_FLEXPWM1_3, pwm1_3_isr),\
  466. FLEXPWM1_SM3STS = FLEXPWM_SMSTS_RF, \
  467. FLEXPWM1_SM3INTEN = FLEXPWM_SMINTEN_RIE, \
  468. NVIC_ENABLE_IRQ(IRQ_FLEXPWM1_3)
  469. #define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_FLEXPWM1_3)
  470. #define TIMER_INTR_NAME pwm1_3_isr
  471. #define TIMER_INT_DECLARE void pwm1_3_isr(void);
  472. #ifdef ISR
  473. #undef ISR
  474. #endif
  475. #define ISR(f) void f(void)
  476. #define TIMER_CONFIG_KHZ(val) ({ \
  477. uint32_t period = (float)F_BUS_ACTUAL / (float)((val) * 2000); \
  478. uint32_t prescale = 0; \
  479. while (period > 32767) { \
  480. period = period >> 1; \
  481. if (prescale < 7) prescale++; \
  482. } \
  483. FLEXPWM1_FCTRL0 |= FLEXPWM_FCTRL0_FLVL(8); \
  484. FLEXPWM1_FSTS0 = 0x0008; \
  485. FLEXPWM1_MCTRL |= FLEXPWM_MCTRL_CLDOK(8); \
  486. FLEXPWM1_SM3CTRL2 = FLEXPWM_SMCTRL2_INDEP; \
  487. FLEXPWM1_SM3CTRL = FLEXPWM_SMCTRL_HALF | FLEXPWM_SMCTRL_PRSC(prescale); \
  488. FLEXPWM1_SM3INIT = -period; \
  489. FLEXPWM1_SM3VAL0 = 0; \
  490. FLEXPWM1_SM3VAL1 = period; \
  491. FLEXPWM1_SM3VAL2 = -(period / 3); \
  492. FLEXPWM1_SM3VAL3 = period / 3; \
  493. FLEXPWM1_SM3VAL4 = 0; \
  494. FLEXPWM1_SM3VAL5 = 0; \
  495. FLEXPWM1_MCTRL |= FLEXPWM_MCTRL_LDOK(8) | FLEXPWM_MCTRL_RUN(8); \
  496. })
  497. #define TIMER_CONFIG_NORMAL() ({ \
  498. uint32_t period = (float)F_BUS_ACTUAL * (float)USECPERTICK * 0.0000005f; \
  499. uint32_t prescale = 0; \
  500. while (period > 32767) { \
  501. period = period >> 1; \
  502. if (prescale < 7) prescale++; \
  503. } \
  504. FLEXPWM1_FCTRL0 |= FLEXPWM_FCTRL0_FLVL(8); \
  505. FLEXPWM1_FSTS0 = 0x0008; \
  506. FLEXPWM1_MCTRL |= FLEXPWM_MCTRL_CLDOK(8); \
  507. FLEXPWM1_SM3CTRL2 = FLEXPWM_SMCTRL2_INDEP; \
  508. FLEXPWM1_SM3CTRL = FLEXPWM_SMCTRL_HALF | FLEXPWM_SMCTRL_PRSC(prescale); \
  509. FLEXPWM1_SM3INIT = -period; \
  510. FLEXPWM1_SM3VAL0 = 0; \
  511. FLEXPWM1_SM3VAL1 = period; \
  512. FLEXPWM1_SM3VAL2 = 0; \
  513. FLEXPWM1_SM3VAL3 = 0; \
  514. FLEXPWM1_SM3VAL4 = 0; \
  515. FLEXPWM1_SM3VAL5 = 0; \
  516. FLEXPWM1_MCTRL |= FLEXPWM_MCTRL_LDOK(8) | FLEXPWM_MCTRL_RUN(8); \
  517. })
  518. #define TIMER_PWM_PIN 7
  519. // defines for timer_tiny0 (8 bits)
  520. #elif defined(IR_USE_TIMER_TINY0)
  521. #define TIMER_RESET
  522. #define TIMER_ENABLE_PWM (TCCR0A |= _BV(COM0B1))
  523. #define TIMER_DISABLE_PWM (TCCR0A &= ~(_BV(COM0B1)))
  524. #define TIMER_ENABLE_INTR (TIMSK |= _BV(OCIE0A))
  525. #define TIMER_DISABLE_INTR (TIMSK &= ~(_BV(OCIE0A)))
  526. #define TIMER_INTR_NAME TIMER0_COMPA_vect
  527. #define TIMER_CONFIG_KHZ(val) ({ \
  528. const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
  529. TCCR0A = _BV(WGM00); \
  530. TCCR0B = _BV(WGM02) | _BV(CS00); \
  531. OCR0A = pwmval; \
  532. OCR0B = pwmval / 3; \
  533. })
  534. #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000)
  535. #if (TIMER_COUNT_TOP < 256)
  536. #define TIMER_CONFIG_NORMAL() ({ \
  537. TCCR0A = _BV(WGM01); \
  538. TCCR0B = _BV(CS00); \
  539. OCR0A = TIMER_COUNT_TOP; \
  540. TCNT0 = 0; \
  541. })
  542. #else
  543. #define TIMER_CONFIG_NORMAL() ({ \
  544. TCCR0A = _BV(WGM01); \
  545. TCCR0B = _BV(CS01); \
  546. OCR0A = TIMER_COUNT_TOP / 8; \
  547. TCNT0 = 0; \
  548. })
  549. #endif
  550. #define TIMER_PWM_PIN 1 /* ATtiny85 */
  551. //---------------------------------------------------------
  552. // ESP32 (ESP8266 should likely be added here too)
  553. //
  554. // ESP32 has it own timer API and does not use these macros, but to avoid ifdef'ing
  555. // them out in the common code, they are defined to no-op. This allows the code to compile
  556. // (which it wouldn't otherwise) but irsend will not work until ESP32 specific code is written
  557. // for that -- merlin
  558. // As a warning, sending timing specific code from an ESP32 can be challenging if you need 100%
  559. // reliability because the arduino code may be interrupted and cause your sent waveform to be the
  560. // wrong length. This is specifically an issue for neopixels which require 800Khz resolution.
  561. // IR may just work as is with the common code since it's lower frequency, but if not, the other
  562. // way to do this on ESP32 is using the RMT built in driver like in this incomplete library below
  563. // https://github.com/ExploreEmbedded/ESP32_RMT
  564. #elif defined(IR_TIMER_USE_ESP32)
  565. #define TIMER_RESET
  566. #define TIMER_ENABLE_PWM
  567. #define TIMER_DISABLE_PWM Serial.println("IRsend not implemented for ESP32 yet");
  568. #define TIMER_ENABLE_INTR
  569. #define TIMER_DISABLE_INTR
  570. #define TIMER_INTR_NAME
  571. //---------------------------------------------------------
  572. // Unknown Timer
  573. //
  574. #else
  575. # error "Internal code configuration error, no known IR_USE_TIMER# defined\n"
  576. #endif
  577. #endif // ! boarddefs_h