BrickUp API Service for Docker version.

IRremoteInt.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. //
  7. // Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
  8. //
  9. // Interrupt code based on NECIRrcv by Joe Knapp
  10. // http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
  11. // Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
  12. //
  13. // JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
  14. // Whynter A/C ARC-110WD added by Francesco Meschia
  15. //******************************************************************************
  16. #ifndef IRremoteint_h
  17. #define IRremoteint_h
  18. //------------------------------------------------------------------------------
  19. // Include the right Arduino header
  20. //
  21. #if defined(ARDUINO) && (ARDUINO >= 100)
  22. # include <Arduino.h>
  23. #else
  24. # if !defined(IRPRONTO)
  25. # include <WProgram.h>
  26. # endif
  27. #endif
  28. //------------------------------------------------------------------------------
  29. // This handles definition and access to global variables
  30. //
  31. #ifdef IR_GLOBAL
  32. # define EXTERN
  33. #else
  34. # define EXTERN extern
  35. #endif
  36. //------------------------------------------------------------------------------
  37. // Information for the Interrupt Service Routine
  38. //
  39. #define RAWBUF 101 // Maximum length of raw duration buffer
  40. typedef
  41. struct {
  42. // The fields are ordered to reduce memory over caused by struct-padding
  43. uint8_t rcvstate; // State Machine state
  44. uint8_t recvpin; // Pin connected to IR data from detector
  45. uint8_t blinkpin;
  46. uint8_t blinkflag; // true -> enable blinking of pin on IR processing
  47. uint8_t rawlen; // counter of entries in rawbuf
  48. unsigned int timer; // State timer, counts 50uS ticks.
  49. unsigned int rawbuf[RAWBUF]; // raw data
  50. uint8_t overflow; // Raw buffer overflow occurred
  51. }
  52. irparams_t;
  53. // ISR State-Machine : Receiver States
  54. #define STATE_IDLE 2
  55. #define STATE_MARK 3
  56. #define STATE_SPACE 4
  57. #define STATE_STOP 5
  58. #define STATE_OVERFLOW 6
  59. // Allow all parts of the code access to the ISR data
  60. // NB. The data can be changed by the ISR at any time, even mid-function
  61. // Therefore we declare it as "volatile" to stop the compiler/CPU caching it
  62. EXTERN volatile irparams_t irparams;
  63. //------------------------------------------------------------------------------
  64. // Defines for blinking the LED
  65. //
  66. #if defined(CORE_LED0_PIN)
  67. # define BLINKLED CORE_LED0_PIN
  68. # define BLINKLED_ON() (digitalWrite(CORE_LED0_PIN, HIGH))
  69. # define BLINKLED_OFF() (digitalWrite(CORE_LED0_PIN, LOW))
  70. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  71. # define BLINKLED 13
  72. # define BLINKLED_ON() (PORTB |= B10000000)
  73. # define BLINKLED_OFF() (PORTB &= B01111111)
  74. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  75. # define BLINKLED 0
  76. # define BLINKLED_ON() (PORTD |= B00000001)
  77. # define BLINKLED_OFF() (PORTD &= B11111110)
  78. #else
  79. # define BLINKLED 13
  80. #define BLINKLED_ON() (PORTB |= B00100000)
  81. # define BLINKLED_OFF() (PORTB &= B11011111)
  82. #endif
  83. //------------------------------------------------------------------------------
  84. // CPU Frequency
  85. //
  86. #ifdef F_CPU
  87. # define SYSCLOCK F_CPU // main Arduino clock
  88. #else
  89. # define SYSCLOCK 16000000 // main Arduino clock
  90. #endif
  91. //------------------------------------------------------------------------------
  92. // Defines for setting and clearing register bits
  93. //
  94. #ifndef cbi
  95. # define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  96. #endif
  97. #ifndef sbi
  98. # define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  99. #endif
  100. //------------------------------------------------------------------------------
  101. // Pulse parms are ((X*50)-100) for the Mark and ((X*50)+100) for the Space.
  102. // First MARK is the one after the long gap
  103. // Pulse parameters in uSec
  104. //
  105. // Due to sensor lag, when received, Marks tend to be 100us too long and
  106. // Spaces tend to be 100us too short
  107. #define MARK_EXCESS 100
  108. // microseconds per clock interrupt tick
  109. #define USECPERTICK 50
  110. // Upper and Lower percentage tolerances in measurements
  111. #define TOLERANCE 25
  112. #define LTOL (1.0 - (TOLERANCE/100.))
  113. #define UTOL (1.0 + (TOLERANCE/100.))
  114. // Minimum gap between IR transmissions
  115. #define _GAP 5000
  116. #define GAP_TICKS (_GAP/USECPERTICK)
  117. #define TICKS_LOW(us) ((int)(((us)*LTOL/USECPERTICK)))
  118. #define TICKS_HIGH(us) ((int)(((us)*UTOL/USECPERTICK + 1)))
  119. //------------------------------------------------------------------------------
  120. // IR detector output is active low
  121. //
  122. #define MARK 0
  123. #define SPACE 1
  124. //------------------------------------------------------------------------------
  125. // Define which timer to use
  126. //
  127. // Uncomment the timer you wish to use on your board.
  128. // If you are using another library which uses timer2, you have options to
  129. // switch IRremote to use a different timer.
  130. //
  131. // Arduino Mega
  132. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  133. //#define IR_USE_TIMER1 // tx = pin 11
  134. #define IR_USE_TIMER2 // tx = pin 9
  135. //#define IR_USE_TIMER3 // tx = pin 5
  136. //#define IR_USE_TIMER4 // tx = pin 6
  137. //#define IR_USE_TIMER5 // tx = pin 46
  138. // Teensy 1.0
  139. #elif defined(__AVR_AT90USB162__)
  140. #define IR_USE_TIMER1 // tx = pin 17
  141. // Teensy 2.0
  142. #elif defined(__AVR_ATmega32U4__)
  143. //#define IR_USE_TIMER1 // tx = pin 14
  144. //#define IR_USE_TIMER3 // tx = pin 9
  145. #define IR_USE_TIMER4_HS // tx = pin 10
  146. // Teensy 3.0 / Teensy 3.1
  147. #elif defined(__MK20DX128__) || defined(__MK20DX256__)
  148. #define IR_USE_TIMER_CMT // tx = pin 5
  149. // Teensy-LC
  150. #elif defined(__MKL26Z64__)
  151. #define IR_USE_TIMER_TPM1 // tx = pin 16
  152. // Teensy++ 1.0 & 2.0
  153. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  154. //#define IR_USE_TIMER1 // tx = pin 25
  155. #define IR_USE_TIMER2 // tx = pin 1
  156. //#define IR_USE_TIMER3 // tx = pin 16
  157. // Sanguino
  158. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  159. //#define IR_USE_TIMER1 // tx = pin 13
  160. #define IR_USE_TIMER2 // tx = pin 14
  161. // Atmega8
  162. #elif defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__)
  163. #define IR_USE_TIMER1 // tx = pin 9
  164. // ATtiny84
  165. #elif defined(__AVR_ATtiny84__)
  166. #define IR_USE_TIMER1 // tx = pin 6
  167. //ATtiny85
  168. #elif defined(__AVR_ATtiny85__)
  169. #define IR_USE_TIMER_TINY0 // tx = pin 1
  170. // Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
  171. #else
  172. //#define IR_USE_TIMER1 // tx = pin 9
  173. #define IR_USE_TIMER2 // tx = pin 3
  174. #endif
  175. //------------------------------------------------------------------------------
  176. // Defines for Timer
  177. //---------------------------------------------------------
  178. // Timer2 (8 bits)
  179. //
  180. #if defined(IR_USE_TIMER2)
  181. #define TIMER_RESET
  182. #define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))
  183. #define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1)))
  184. #define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A))
  185. #define TIMER_DISABLE_INTR (TIMSK2 = 0)
  186. #define TIMER_INTR_NAME TIMER2_COMPA_vect
  187. #define TIMER_CONFIG_KHZ(val) ({ \
  188. const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
  189. TCCR2A = _BV(WGM20); \
  190. TCCR2B = _BV(WGM22) | _BV(CS20); \
  191. OCR2A = pwmval; \
  192. OCR2B = pwmval / 3; \
  193. })
  194. #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000)
  195. //-----------------
  196. #if (TIMER_COUNT_TOP < 256)
  197. # define TIMER_CONFIG_NORMAL() ({ \
  198. TCCR2A = _BV(WGM21); \
  199. TCCR2B = _BV(CS20); \
  200. OCR2A = TIMER_COUNT_TOP; \
  201. TCNT2 = 0; \
  202. })
  203. #else
  204. # define TIMER_CONFIG_NORMAL() ({ \
  205. TCCR2A = _BV(WGM21); \
  206. TCCR2B = _BV(CS21); \
  207. OCR2A = TIMER_COUNT_TOP / 8; \
  208. TCNT2 = 0; \
  209. })
  210. #endif
  211. //-----------------
  212. #if defined(CORE_OC2B_PIN)
  213. # define TIMER_PWM_PIN CORE_OC2B_PIN // Teensy
  214. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  215. # define TIMER_PWM_PIN 9 // Arduino Mega
  216. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  217. # define TIMER_PWM_PIN 14 // Sanguino
  218. #else
  219. # define TIMER_PWM_PIN 3 // Arduino Duemilanove, Diecimila, LilyPad, etc
  220. #endif
  221. //---------------------------------------------------------
  222. // Timer1 (16 bits)
  223. //
  224. #elif defined(IR_USE_TIMER1)
  225. #define TIMER_RESET
  226. #define TIMER_ENABLE_PWM (TCCR1A |= _BV(COM1A1))
  227. #define TIMER_DISABLE_PWM (TCCR1A &= ~(_BV(COM1A1)))
  228. //-----------------
  229. #if defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__)
  230. # define TIMER_ENABLE_INTR (TIMSK |= _BV(OCIE1A))
  231. # define TIMER_DISABLE_INTR (TIMSK &= ~_BV(OCIE1A))
  232. #else
  233. # define TIMER_ENABLE_INTR (TIMSK1 = _BV(OCIE1A))
  234. # define TIMER_DISABLE_INTR (TIMSK1 = 0)
  235. #endif
  236. //-----------------
  237. #define TIMER_INTR_NAME TIMER1_COMPA_vect
  238. #define TIMER_CONFIG_KHZ(val) ({ \
  239. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  240. TCCR1A = _BV(WGM11); \
  241. TCCR1B = _BV(WGM13) | _BV(CS10); \
  242. ICR1 = pwmval; \
  243. OCR1A = pwmval / 3; \
  244. })
  245. #define TIMER_CONFIG_NORMAL() ({ \
  246. TCCR1A = 0; \
  247. TCCR1B = _BV(WGM12) | _BV(CS10); \
  248. OCR1A = SYSCLOCK * USECPERTICK / 1000000; \
  249. TCNT1 = 0; \
  250. })
  251. //-----------------
  252. #if defined(CORE_OC1A_PIN)
  253. # define TIMER_PWM_PIN CORE_OC1A_PIN // Teensy
  254. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  255. # define TIMER_PWM_PIN 11 // Arduino Mega
  256. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  257. # define TIMER_PWM_PIN 13 // Sanguino
  258. #elif defined(__AVR_ATtiny84__)
  259. # define TIMER_PWM_PIN 6
  260. #else
  261. # define TIMER_PWM_PIN 9 // Arduino Duemilanove, Diecimila, LilyPad, etc
  262. #endif
  263. //---------------------------------------------------------
  264. // Timer3 (16 bits)
  265. //
  266. #elif defined(IR_USE_TIMER3)
  267. #define TIMER_RESET
  268. #define TIMER_ENABLE_PWM (TCCR3A |= _BV(COM3A1))
  269. #define TIMER_DISABLE_PWM (TCCR3A &= ~(_BV(COM3A1)))
  270. #define TIMER_ENABLE_INTR (TIMSK3 = _BV(OCIE3A))
  271. #define TIMER_DISABLE_INTR (TIMSK3 = 0)
  272. #define TIMER_INTR_NAME TIMER3_COMPA_vect
  273. #define TIMER_CONFIG_KHZ(val) ({ \
  274. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  275. TCCR3A = _BV(WGM31); \
  276. TCCR3B = _BV(WGM33) | _BV(CS30); \
  277. ICR3 = pwmval; \
  278. OCR3A = pwmval / 3; \
  279. })
  280. #define TIMER_CONFIG_NORMAL() ({ \
  281. TCCR3A = 0; \
  282. TCCR3B = _BV(WGM32) | _BV(CS30); \
  283. OCR3A = SYSCLOCK * USECPERTICK / 1000000; \
  284. TCNT3 = 0; \
  285. })
  286. //-----------------
  287. #if defined(CORE_OC3A_PIN)
  288. # define TIMER_PWM_PIN CORE_OC3A_PIN // Teensy
  289. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  290. # define TIMER_PWM_PIN 5 // Arduino Mega
  291. #else
  292. # error "Please add OC3A pin number here\n"
  293. #endif
  294. //---------------------------------------------------------
  295. // Timer4 (10 bits, high speed option)
  296. //
  297. #elif defined(IR_USE_TIMER4_HS)
  298. #define TIMER_RESET
  299. #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1))
  300. #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1)))
  301. #define TIMER_ENABLE_INTR (TIMSK4 = _BV(TOIE4))
  302. #define TIMER_DISABLE_INTR (TIMSK4 = 0)
  303. #define TIMER_INTR_NAME TIMER4_OVF_vect
  304. #define TIMER_CONFIG_KHZ(val) ({ \
  305. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  306. TCCR4A = (1<<PWM4A); \
  307. TCCR4B = _BV(CS40); \
  308. TCCR4C = 0; \
  309. TCCR4D = (1<<WGM40); \
  310. TCCR4E = 0; \
  311. TC4H = pwmval >> 8; \
  312. OCR4C = pwmval; \
  313. TC4H = (pwmval / 3) >> 8; \
  314. OCR4A = (pwmval / 3) & 255; \
  315. })
  316. #define TIMER_CONFIG_NORMAL() ({ \
  317. TCCR4A = 0; \
  318. TCCR4B = _BV(CS40); \
  319. TCCR4C = 0; \
  320. TCCR4D = 0; \
  321. TCCR4E = 0; \
  322. TC4H = (SYSCLOCK * USECPERTICK / 1000000) >> 8; \
  323. OCR4C = (SYSCLOCK * USECPERTICK / 1000000) & 255; \
  324. TC4H = 0; \
  325. TCNT4 = 0; \
  326. })
  327. //-----------------
  328. #if defined(CORE_OC4A_PIN)
  329. # define TIMER_PWM_PIN CORE_OC4A_PIN // Teensy
  330. #elif defined(__AVR_ATmega32U4__)
  331. # define TIMER_PWM_PIN 13 // Leonardo
  332. #else
  333. # error "Please add OC4A pin number here\n"
  334. #endif
  335. //---------------------------------------------------------
  336. // Timer4 (16 bits)
  337. //
  338. #elif defined(IR_USE_TIMER4)
  339. #define TIMER_RESET
  340. #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1))
  341. #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1)))
  342. #define TIMER_ENABLE_INTR (TIMSK4 = _BV(OCIE4A))
  343. #define TIMER_DISABLE_INTR (TIMSK4 = 0)
  344. #define TIMER_INTR_NAME TIMER4_COMPA_vect
  345. #define TIMER_CONFIG_KHZ(val) ({ \
  346. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  347. TCCR4A = _BV(WGM41); \
  348. TCCR4B = _BV(WGM43) | _BV(CS40); \
  349. ICR4 = pwmval; \
  350. OCR4A = pwmval / 3; \
  351. })
  352. #define TIMER_CONFIG_NORMAL() ({ \
  353. TCCR4A = 0; \
  354. TCCR4B = _BV(WGM42) | _BV(CS40); \
  355. OCR4A = SYSCLOCK * USECPERTICK / 1000000; \
  356. TCNT4 = 0; \
  357. })
  358. //-----------------
  359. #if defined(CORE_OC4A_PIN)
  360. # define TIMER_PWM_PIN CORE_OC4A_PIN
  361. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  362. # define TIMER_PWM_PIN 6 // Arduino Mega
  363. #else
  364. # error "Please add OC4A pin number here\n"
  365. #endif
  366. //---------------------------------------------------------
  367. // Timer5 (16 bits)
  368. //
  369. #elif defined(IR_USE_TIMER5)
  370. #define TIMER_RESET
  371. #define TIMER_ENABLE_PWM (TCCR5A |= _BV(COM5A1))
  372. #define TIMER_DISABLE_PWM (TCCR5A &= ~(_BV(COM5A1)))
  373. #define TIMER_ENABLE_INTR (TIMSK5 = _BV(OCIE5A))
  374. #define TIMER_DISABLE_INTR (TIMSK5 = 0)
  375. #define TIMER_INTR_NAME TIMER5_COMPA_vect
  376. #define TIMER_CONFIG_KHZ(val) ({ \
  377. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  378. TCCR5A = _BV(WGM51); \
  379. TCCR5B = _BV(WGM53) | _BV(CS50); \
  380. ICR5 = pwmval; \
  381. OCR5A = pwmval / 3; \
  382. })
  383. #define TIMER_CONFIG_NORMAL() ({ \
  384. TCCR5A = 0; \
  385. TCCR5B = _BV(WGM52) | _BV(CS50); \
  386. OCR5A = SYSCLOCK * USECPERTICK / 1000000; \
  387. TCNT5 = 0; \
  388. })
  389. //-----------------
  390. #if defined(CORE_OC5A_PIN)
  391. # define TIMER_PWM_PIN CORE_OC5A_PIN
  392. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  393. # define TIMER_PWM_PIN 46 // Arduino Mega
  394. #else
  395. # error "Please add OC5A pin number here\n"
  396. #endif
  397. //---------------------------------------------------------
  398. // Special carrier modulator timer
  399. //
  400. #elif defined(IR_USE_TIMER_CMT)
  401. #define TIMER_RESET ({ \
  402. uint8_t tmp = CMT_MSC; \
  403. CMT_CMD2 = 30; \
  404. })
  405. #define TIMER_ENABLE_PWM do { \
  406. CORE_PIN5_CONFIG = PORT_PCR_MUX(2) | PORT_PCR_DSE | PORT_PCR_SRE; \
  407. } while(0)
  408. #define TIMER_DISABLE_PWM do { \
  409. CORE_PIN5_CONFIG = PORT_PCR_MUX(1) | PORT_PCR_DSE | PORT_PCR_SRE; \
  410. } while(0)
  411. #define TIMER_ENABLE_INTR NVIC_ENABLE_IRQ(IRQ_CMT)
  412. #define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_CMT)
  413. #define TIMER_INTR_NAME cmt_isr
  414. //-----------------
  415. #ifdef ISR
  416. # undef ISR
  417. #endif
  418. #define ISR(f) void f(void)
  419. //-----------------
  420. #if (F_BUS == 48000000)
  421. # define CMT_PPS_VAL 5
  422. #else
  423. # define CMT_PPS_VAL 2
  424. #endif
  425. //-----------------
  426. #define TIMER_CONFIG_KHZ(val) ({ \
  427. SIM_SCGC4 |= SIM_SCGC4_CMT; \
  428. SIM_SOPT2 |= SIM_SOPT2_PTD7PAD; \
  429. CMT_PPS = CMT_PPS_VAL; \
  430. CMT_CGH1 = 2667 / val; \
  431. CMT_CGL1 = 5333 / val; \
  432. CMT_CMD1 = 0; \
  433. CMT_CMD2 = 30; \
  434. CMT_CMD3 = 0; \
  435. CMT_CMD4 = 0; \
  436. CMT_OC = 0x60; \
  437. CMT_MSC = 0x01; \
  438. })
  439. #define TIMER_CONFIG_NORMAL() ({ \
  440. SIM_SCGC4 |= SIM_SCGC4_CMT; \
  441. CMT_PPS = CMT_PPS_VAL; \
  442. CMT_CGH1 = 1; \
  443. CMT_CGL1 = 1; \
  444. CMT_CMD1 = 0; \
  445. CMT_CMD2 = 30 \
  446. CMT_CMD3 = 0; \
  447. CMT_CMD4 = 19; \
  448. CMT_OC = 0; \
  449. CMT_MSC = 0x03; \
  450. })
  451. #define TIMER_PWM_PIN 5
  452. // defines for TPM1 timer on Teensy-LC
  453. #elif defined(IR_USE_TIMER_TPM1)
  454. #define TIMER_RESET FTM1_SC |= FTM_SC_TOF;
  455. #define TIMER_ENABLE_PWM CORE_PIN16_CONFIG = PORT_PCR_MUX(3)|PORT_PCR_DSE|PORT_PCR_SRE
  456. #define TIMER_DISABLE_PWM CORE_PIN16_CONFIG = PORT_PCR_MUX(1)|PORT_PCR_SRE
  457. #define TIMER_ENABLE_INTR NVIC_ENABLE_IRQ(IRQ_FTM1)
  458. #define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_FTM1)
  459. #define TIMER_INTR_NAME ftm1_isr
  460. #ifdef ISR
  461. #undef ISR
  462. #endif
  463. #define ISR(f) void f(void)
  464. #define TIMER_CONFIG_KHZ(val) ({ \
  465. SIM_SCGC6 |= SIM_SCGC6_TPM1; \
  466. FTM1_SC = 0; \
  467. FTM1_CNT = 0; \
  468. FTM1_MOD = (F_PLL/2000) / val - 1; \
  469. FTM1_C0V = (F_PLL/6000) / val - 1; \
  470. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0); \
  471. })
  472. #define TIMER_CONFIG_NORMAL() ({ \
  473. SIM_SCGC6 |= SIM_SCGC6_TPM1; \
  474. FTM1_SC = 0; \
  475. FTM1_CNT = 0; \
  476. FTM1_MOD = (F_PLL/40000) - 1; \
  477. FTM1_C0V = 0; \
  478. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0) | FTM_SC_TOF | FTM_SC_TOIE; \
  479. })
  480. #define TIMER_PWM_PIN 16
  481. // defines for timer_tiny0 (8 bits)
  482. #elif defined(IR_USE_TIMER_TINY0)
  483. #define TIMER_RESET
  484. #define TIMER_ENABLE_PWM (TCCR0A |= _BV(COM0B1))
  485. #define TIMER_DISABLE_PWM (TCCR0A &= ~(_BV(COM0B1)))
  486. #define TIMER_ENABLE_INTR (TIMSK |= _BV(OCIE0A))
  487. #define TIMER_DISABLE_INTR (TIMSK &= ~(_BV(OCIE0A)))
  488. #define TIMER_INTR_NAME TIMER0_COMPA_vect
  489. #define TIMER_CONFIG_KHZ(val) ({ \
  490. const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
  491. TCCR0A = _BV(WGM00); \
  492. TCCR0B = _BV(WGM02) | _BV(CS00); \
  493. OCR0A = pwmval; \
  494. OCR0B = pwmval / 3; \
  495. })
  496. #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000)
  497. #if (TIMER_COUNT_TOP < 256)
  498. #define TIMER_CONFIG_NORMAL() ({ \
  499. TCCR0A = _BV(WGM01); \
  500. TCCR0B = _BV(CS00); \
  501. OCR0A = TIMER_COUNT_TOP; \
  502. TCNT0 = 0; \
  503. })
  504. #else
  505. #define TIMER_CONFIG_NORMAL() ({ \
  506. TCCR0A = _BV(WGM01); \
  507. TCCR0B = _BV(CS01); \
  508. OCR0A = TIMER_COUNT_TOP / 8; \
  509. TCNT0 = 0; \
  510. })
  511. #endif
  512. #define TIMER_PWM_PIN 1 /* ATtiny85 */
  513. //---------------------------------------------------------
  514. // Unknown Timer
  515. //
  516. #else
  517. # error "Internal code configuration error, no known IR_USE_TIMER# defined\n"
  518. #endif
  519. #endif