source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/IRLibHardware.h @ 25

Last change on this file since 25 was 3, checked in by markus, 7 years ago

Setup project structure

File size: 14.9 KB
Line 
1/* IRLibHardware.h
2 * Part of IRLib Library for Arduino receiving, decoding, and sending
3 * infrared signals. See COPYRIGHT.txt and LICENSE.txt for more information.
4 *
5 * This file was formally called IRLibTimer.h but we have renamed it because it more
6 * accurately reflects the fact that it contains all of the hardware specific defines.
7 * It contains mostly definitions for 8-bit AVR processors. More advanced processors such as
8 * SAMD21 definitions will now be defined in a separate processor specific header file.
9 * NOTE: IRLibHardware.cpp is not related to hardware. See the comments in that file
10 * to explain why it was created and what purpose it serves.
11 */
12
13/* This file has been significantly reconfigured for version 1.5 and greater. It now
14 * allows you use a different timer for input versus output. This also allows you to
15 * use no timers whatsoever by using the IRrecvPCI or IRrecvLoop for input and
16 * bit-bang routines for output. Note the various "Teensy" sections have not been tested.
17 */
18#ifndef IRLibHardware_h
19#define IRLibHardware_h
20
21#include "Arduino.h"
22
23//This is a default for AVRs. SAMD21 will override it.
24#define IR_CLEAR_INTERRUPT {} //clear interrupt flag
25
26// defines for setting and clearing register bits
27#ifndef cbi
28#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
29#endif
30#ifndef sbi
31#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
32#endif
33
34/* In this section we attempt to detect your hardware type and give you the choice of
35 * various hardware timers to change the PWM frequency for output. In each hardware
36 * section below you should un-comment only one of the choices. The defines also
37 * specify the output pin number. DO NOT change the pin number unless you're using
38 * non-standard hardware that would require you to do so. If you wish to use the
39 * bit-bang PWM output, you will specify that AFTER this section as an override.
40 */
41
42/* Arduino Mega */
43#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
44        //#define IR_SEND_TIMER1        11
45        #define IR_SEND_TIMER2          9
46        //#define IR_SEND_TIMER3        5
47        //#define IR_SEND_TIMER4        6
48        //#define IR_SEND_TIMER5        46
49
50/* Teensy 1.0 */
51#elif defined(__AVR_AT90USB162__)
52        #define IR_SEND_TIMER1  17
53
54/* Teensy 2.0 versus Leonardo These boards use the same chip but the
55 * pinouts are different.*/
56#elif defined(__AVR_ATmega32U4__)
57        #ifdef CORE_TEENSY
58                // it's Teensy 2.0
59                //#define IR_SEND_TIMER1        14
60                //#define IR_SEND_TIMER3        9
61                #define IR_SEND_TIMER4_HS       10
62        #else
63        /* it's probably Leonardo */
64                #define IR_SEND_TIMER1          9
65                //#define IR_SEND_TIMER3        5
66                //#define IR_SEND_TIMER4_HS     13
67        #endif
68
69/* Teensy++ 1.0 & 2.0 */
70#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
71        //#define IR_SEND_TIMER1        25
72        #define IR_SEND_TIMER2          1
73        //#define IR_SEND_TIMER3        16
74
75/*  Sanguino */
76#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
77        //#define IR_SEND_TIMER1        13
78        #define IR_SEND_TIMER2          14
79 
80/* Arduino Zero, M0, M0 Pro, Feather M0 etc. */
81#elif defined (__SAMD21G18A__)
82// All of the settings can be found in IRLibSAMD21.h
83  #include "IRLibSAMD21.h"
84  /* Pinoccio Scout */
85#elif defined(__AVR_ATmega256RFR2__)
86        #define IR_SEND_TIMER3          3
87
88/* Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc */
89#else
90        //#define IR_SEND_TIMER1        9
91        #define IR_SEND_TIMER2          3
92#endif //end of setting IR_SEND_TIMER based on hardware detection
93
94/* If you want to use bit-bang PWM output, you should un-comment the line below.
95 * The definition must include the pin number for bit-bang output.  This could be any
96 * available digital output pin. It need not be a designated PWM pin.
97 * NOTE: By un-commenting this line, you are forcing the library to ignore
98 * hardware detection and timer specifications above. The bit-bang frequency
99 * code is not as accurate as using a hardware timer but it is more flexible and
100 * less hardware platform dependent.
101 */
102//#define IR_SEND_BIT_BANG  3  //Be sure to set this pin number if you un-comment
103
104/* This is a fudge factor that adjusts bit-bang timing. Feel free to experiment
105 * for best results.*/
106#define IR_BIT_BANG_OVERHEAD 10
107
108/* We are going to presume that you want to use the same hardware timer to control
109 * the 50 microsecond interrupt used by the IRrecv receiver class as was specified
110 * above in the hardware detection section for sending. Even if you specified bit-bang
111 * for sending, the definitions above have selected a default sending timer for you based
112 * on hardware detection. If that is correct, then do nothing below.  However if you do
113 * wish to specify an IR_RECV_TIMER different than the IR_SEND_TIMER selected by the code
114 * above, then you should un-comment the IR_RECV_TIMER_OVERRIDE and also un-comment one
115 * and only one of the following IR_RECV_TIMERx lines below that.
116 * NOTE: You are responsible for ensuring that the timer you are specifying is
117 * available on your hardware. You should only choose timers which are shown as available
118 * for your hardware platform as shown in the defines in the IR_SEND_TIMER section above.
119 * NOTE: This discussion does not apply to SAMD 21 processors.
120 */
121//#define IR_RECV_TIMER_OVERRIDE
122//#define IR_RECV_TIMER1
123//#define IR_RECV_TIMER2
124//#define IR_RECV_TIMER3
125//#define IR_RECV_TIMER4
126//#define IR_RECV_TIMER4_HS
127//#define IR_RECV_TIMER5
128
129// Miscellaneous defines needed for computations below
130#ifdef F_CPU
131#define SYSCLOCK F_CPU     // main Arduino clock
132#else
133#define SYSCLOCK 16000000  // main Arduino clock
134#endif
135
136/* Everything below this point is a computation based on user settings above.
137 * In all likelihood you would never need to modify anything below this point
138 * unless you are adding some completely new feature or seriously modifying
139 * the behavior of existing features. In other words don't try this at home.
140 */
141#if !defined(IR_RECV_TIMER_OVERRIDE)
142        #if defined(IR_SEND_TIMER1)
143                #define IR_RECV_TIMER1
144        #elif defined(IR_SEND_TIMER2)
145                #define IR_RECV_TIMER2
146        #elif defined(IR_SEND_TIMER3)
147                #define IR_RECV_TIMER3
148        #elif defined(IR_SEND_TIMER4)
149                #define IR_RECV_TIMER4
150        #elif defined(IR_SEND_TIMER4_HS)
151                #define IR_RECV_TIMER4_HS
152        #elif defined(IR_SEND_TIMER5)
153                #define IR_RECV_TIMER5
154        #elif defined(__SAMD21G18A__)//handle this one a little differently
155    #define IR_RECV_TC3
156  #else
157                #error "Unable to set IR_RECV_TIMER"
158        #endif
159#endif
160
161#if defined(IR_SEND_BIT_BANG)  //defines for bit-bang output
162        #define IR_SEND_PWM_PIN IR_SEND_BIT_BANG
163        #define IR_SEND_PWM_START   unsigned int jmax=time/iLength;\
164                for(unsigned int j=0;j<jmax;j++) {\
165                  digitalWrite(IR_SEND_BIT_BANG, HIGH);  delayMicroseconds(onTime);\
166                  digitalWrite(IR_SEND_BIT_BANG, LOW);   delayMicroseconds(offTime);}
167        #define IR_SEND_MARK_TIME(time)
168        #define IR_SEND_PWM_STOP
169        #define IR_SEND_CONFIG_KHZ(val)  float Length=1000.0/(float)khz;\
170                iLength=int(Length+0.5); onTime=int(Length/3.0); \
171                offTime=iLength-onTime-IR_BIT_BANG_OVERHEAD-(val<40);
172
173#elif defined(IR_SEND_TIMER1) // defines for timer1 (16 bits)
174        #define IR_SEND_PWM_START     (TCCR1A |= _BV(COM1A1))
175        #define IR_SEND_MARK_TIME(time)  IRLibDelayUSecs(time)
176        #define IR_SEND_PWM_STOP    (TCCR1A &= ~(_BV(COM1A1)))
177        #define IR_SEND_CONFIG_KHZ(val) ({ \
178                const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
179                TCCR1A = _BV(WGM11); TCCR1B = _BV(WGM13) | _BV(CS10); \
180                ICR1 = pwmval; OCR1A = pwmval / 3; })
181        #if defined(CORE_OC1A_PIN)
182                #define IR_SEND_PWM_PIN        CORE_OC1A_PIN  /* Teensy */
183        #else
184                #define IR_SEND_PWM_PIN IR_SEND_TIMER1
185        #endif
186
187#elif defined(IR_SEND_TIMER2)  // defines for timer2 (8 bits)
188        #define IR_SEND_PWM_START     (TCCR2A |= _BV(COM2B1))
189        #define IR_SEND_MARK_TIME(time)  IRLibDelayUSecs(time)
190        #define IR_SEND_PWM_STOP    (TCCR2A &= ~(_BV(COM2B1)))
191        #define IR_SEND_CONFIG_KHZ(val) ({ \
192                const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
193                TCCR2A = _BV(WGM20);  TCCR2B = _BV(WGM22) | _BV(CS20); \
194                OCR2A = pwmval; OCR2B = pwmval / 3; })
195        #if defined(CORE_OC2B_PIN)
196                #define IR_SEND_PWM_PIN        CORE_OC2B_PIN  /* Teensy */
197        #else
198                #define IR_SEND_PWM_PIN IR_SEND_TIMER2
199        #endif
200
201#elif defined(IR_SEND_TIMER3) // defines for timer3 (16 bits)
202        #define IR_SEND_PWM_START     (TCCR3A |= _BV(COM3A1))
203        #define IR_SEND_MARK_TIME(time)  IRLibDelayUSecs(time)
204        #define IR_SEND_PWM_STOP    (TCCR3A &= ~(_BV(COM3A1)))
205        #define IR_SEND_CONFIG_KHZ(val) ({ \
206                const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
207                TCCR3A = _BV(WGM31);   TCCR3B = _BV(WGM33) | _BV(CS30); \
208                ICR3 = pwmval;   OCR3A = pwmval / 3; })
209        #if defined(CORE_OC3A_PIN)
210                #define IR_SEND_PWM_PIN        CORE_OC3A_PIN  /* Teensy */
211        #else
212                #define IR_SEND_PWM_PIN IR_SEND_TIMER3
213        #endif
214
215#elif defined(IR_SEND_TIMER4_HS) // defines for timer4 (10 bits, high speed option)
216        #define IR_SEND_PWM_START     (TCCR4A |= _BV(COM4A1))
217        #define IR_SEND_MARK_TIME(time)  IRLibDelayUSecs(time)
218        #define IR_SEND_PWM_STOP    (TCCR4A &= ~(_BV(COM4A1)))
219        #define IR_SEND_CONFIG_KHZ(val) ({ \
220                const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
221                TCCR4A = (1<<PWM4A);   TCCR4B = _BV(CS40); \
222                TCCR4C = 0;   TCCR4D = (1<<WGM40); \
223                TCCR4E = 0;   TC4H = pwmval >> 8; \
224                OCR4C = pwmval;   TC4H = (pwmval / 3) >> 8;   OCR4A = (pwmval / 3) & 255; })
225        #if defined(CORE_OC4A_PIN)
226                #define IR_SEND_PWM_PIN        CORE_OC4A_PIN  /* Teensy */
227        #else
228                #define IR_SEND_PWM_PIN IR_SEND_TIMER4_HS
229        #endif
230
231#elif defined(IR_SEND_TIMER4) // defines for timer4 (16 bits)
232        #define IR_SEND_PWM_START     (TCCR4A |= _BV(COM4A1))
233        #define IR_SEND_MARK_TIME(time)  IRLibDelayUSecs(time)
234        #define IR_SEND_PWM_STOP    (TCCR4A &= ~(_BV(COM4A1)))
235        #define IR_SEND_CONFIG_KHZ(val) ({ \
236                const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
237                TCCR4A = _BV(WGM41); TCCR4B = _BV(WGM43) | _BV(CS40); \
238                ICR4 = pwmval;   OCR4A = pwmval / 3; })
239        #if defined(CORE_OC4A_PIN)
240                #define IR_SEND_PWM_PIN        CORE_OC4A_PIN
241        #else
242                #define IR_SEND_PWM_PIN IR_SEND_TIMER4
243        #endif
244
245#elif defined(IR_SEND_TIMER5) // defines for timer5 (16 bits)
246        #define IR_SEND_PWM_START     (TCCR5A |= _BV(COM5A1))
247        #define IR_SEND_MARK_TIME(time)  IRLibDelayUSecs(time)
248        #define IR_SEND_PWM_STOP    (TCCR5A &= ~(_BV(COM5A1)))
249        #define IR_SEND_CONFIG_KHZ(val) ({ \
250                const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
251                TCCR5A = _BV(WGM51);   TCCR5B = _BV(WGM53) | _BV(CS50); \
252                ICR5 = pwmval;   OCR5A = pwmval / 3; })
253        #if defined(CORE_OC5A_PIN)
254                #define IR_SEND_PWM_PIN        CORE_OC5A_PIN
255        #else
256                #define IR_SEND_PWM_PIN IR_SEND_TIMER5
257        #endif
258#elif defined(IRLibSAMD21_h) // Used for SAMD 21
259/* All of these definitions have been moved to IRLibSAMD21.h
260        #define IR_SEND_PWM_START
261        #define IR_SEND_MARK_TIME(time)
262        #define IR_SEND_PWM_STOP
263  #define IR_SEND_PWM_PIN       
264        #define IR_SEND_CONFIG_KHZ(val)
265 */
266#else // unknown timer
267        #error "Internal code configuration error, no known IR_SEND_TIMER# defined\n"
268#endif
269
270/* This section sets up the 50 microsecond interval timer used by the
271 * IRrecv receiver class. The various timers have already been selected
272 * earlier in this file.  Theoretically you could change the 50 but it has not been tested.
273 */
274#define USEC_PER_TICK 50  // microseconds per clock interrupt tick
275
276#if defined(IR_RECV_TIMER1)  // defines for timer1 (16 bits)
277        #define IR_RECV_ENABLE_INTR    (TIMSK1 = _BV(OCIE1A))
278        #define IR_RECV_DISABLE_INTR   (TIMSK1 = 0)
279        #define IR_RECV_INTR_NAME      ISR(TIMER1_COMPA_vect,ISR_NOBLOCK)
280        #define IR_RECV_CONFIG_TICKS() ({ \
281                TCCR1A = 0;   TCCR1B = _BV(WGM12) | _BV(CS10); \
282                OCR1A = SYSCLOCK * USEC_PER_TICK / 1000000;   TCNT1 = 0; })
283
284#elif defined(IR_RECV_TIMER2)  // defines for timer2 (8 bits)
285        #define IR_RECV_ENABLE_INTR    (TIMSK2 = _BV(OCIE2A))
286        #define IR_RECV_DISABLE_INTR   (TIMSK2 = 0)
287        #define IR_RECV_INTR_NAME      ISR(TIMER2_COMPA_vect,ISR_NOBLOCK)
288        #define IR_RECV_COUNT_TOP      (SYSCLOCK * USEC_PER_TICK / 1000000)
289        #if (IR_RECV_COUNT_TOP < 256)
290                #define IR_RECV_CONFIG_TICKS() ({ \
291                        TCCR2A = _BV(WGM21);  TCCR2B = _BV(CS20); \
292                        OCR2A = IR_RECV_COUNT_TOP;   TCNT2 = 0; })
293        #else
294                #define IR_RECV_CONFIG_TICKS() ({ \
295                        TCCR2A = _BV(WGM21);   TCCR2B = _BV(CS21); \
296                        OCR2A = IR_RECV_COUNT_TOP / 8;   TCNT2 = 0; })
297        #endif
298
299#elif defined(IR_RECV_TIMER3)  // defines for timer3 (16 bits)
300        #define IR_RECV_ENABLE_INTR    (TIMSK3 = _BV(OCIE3A))
301        #define IR_RECV_DISABLE_INTR   (TIMSK3 = 0)
302        #define IR_RECV_INTR_NAME      ISR(TIMER3_COMPA_vect,ISR_NOBLOCK)
303        #define IR_RECV_CONFIG_TICKS() ({ \
304                TCCR3A = 0;   TCCR3B = _BV(WGM32) | _BV(CS30); \
305                OCR3A = SYSCLOCK * USEC_PER_TICK / 1000000;   TCNT3 = 0; })
306
307#elif defined(IR_RECV_TIMER4_HS)  // defines for timer4 (10 bits, high speed option)
308        #define IR_RECV_ENABLE_INTR    (TIMSK4 = _BV(TOIE4))
309        #define IR_RECV_DISABLE_INTR   (TIMSK4 = 0)
310        #define IR_RECV_INTR_NAME      ISR(TIMER4_OVF_vect,ISR_NOBLOCK)
311        #define IR_RECV_CONFIG_TICKS() ({ \
312                TCCR4A = 0;   TCCR4B = _BV(CS40); \
313                TCCR4C = 0;   TCCR4D = 0; TCCR4E = 0; \
314                TC4H = (SYSCLOCK * USEC_PER_TICK / 1000000) >> 8; \
315                OCR4C = (SYSCLOCK * USEC_PER_TICK / 1000000) & 255; \
316                TC4H = 0;   TCNT4 = 0; })
317
318#elif defined(IR_RECV_TIMER4) // defines for timer4 (16 bits)
319        #define IR_RECV_ENABLE_INTR    (TIMSK4 = _BV(OCIE4A))
320        #define IR_RECV_DISABLE_INTR   (TIMSK4 = 0)
321        #define IR_RECV_INTR_NAME      ISR(TIMER4_COMPA_vect,ISR_NOBLOCK)
322        #define IR_RECV_CONFIG_TICKS() ({ \
323                TCCR4A = 0;   TCCR4B = _BV(WGM42) | _BV(CS40); \
324                OCR4A = SYSCLOCK * USEC_PER_TICK / 1000000;   TCNT4 = 0; })
325
326#elif defined(IR_RECV_TIMER5)  // defines for timer5 (16 bits)
327        #define IR_RECV_ENABLE_INTR    (TIMSK5 = _BV(OCIE5A))
328        #define IR_RECV_DISABLE_INTR   (TIMSK5 = 0)
329        #define IR_RECV_INTR_NAME      ISR(TIMER5_COMPA_vect,ISR_NOBLOCK)
330        #define IR_RECV_CONFIG_TICKS() ({ \
331                TCCR5A = 0;   TCCR5B = _BV(WGM52) | _BV(CS50); \
332                OCR5A = SYSCLOCK * USEC_PER_TICK / 1000000;   TCNT5 = 0; })
333#elif defined(IRLibSAMD21_h)  //for SAMD 21
334/* All of these definitions have been moved to IRLibSAMD21.h
335        #define IR_RECV_ENABLE_INTR
336  #define IR_RECV_DISABLE_INTR
337        #define IR_RECV_INTR_NAME
338        #define IR_RECV_CONFIG_TICKS()
339 */
340#else // unknown timer
341        #error "Internal code configuration error, no known IR_RECV_TIMER# defined\n"
342#endif
343
344//Cannot use blinking LED on 13 if that's the output pin.
345#if (IR_SEND_PWM_PIN==13)
346  #define BLINKLED      -1
347  #define BLINKLED_ON()
348  #define BLINKLED_OFF()
349#endif
350// defines for blinking the LED
351#ifndef BLINKLED
352  #if defined(CORE_LED0_PIN)
353    #define BLINKLED       CORE_LED0_PIN
354    #define BLINKLED_ON()  (digitalWrite(CORE_LED0_PIN, HIGH))
355    #define BLINKLED_OFF() (digitalWrite(CORE_LED0_PIN, LOW))
356  #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
357    #define BLINKLED       13
358    #define BLINKLED_ON()  (PORTB |= B10000000)
359    #define BLINKLED_OFF() (PORTB &= B01111111)
360  #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
361    #define BLINKLED       0
362    #define BLINKLED_ON()  (PORTD |= B00000001)
363    #define BLINKLED_OFF() (PORTD &= B11111110)
364  #elif defined(__AVR_ATmega32u4__) || defined(__AVR_ATmega328__)
365    #define BLINKLED       13
366    #define BLINKLED_ON()  (PORTB |= B00100000)
367    #define BLINKLED_OFF() (PORTB &= B11011111)
368  #else
369    #define BLINKLED       13
370    #define BLINKLED_ON()  (digitalWrite(13, HIGH))
371    #define BLINKLED_OFF() (digitalWrite(13, LOW)) 
372  #endif
373#endif
374
375extern uint8_t IRLib_didIROut;//See the explanation in IRLibHardware.cpp
376#endif //IRLibHardware_h
Note: See TracBrowser for help on using the repository browser.