-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathIRTimer.hpp
2062 lines (1827 loc) · 72.9 KB
/
IRTimer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file IRTimer.hpp
*
* @brief All timer specific definitions are contained in this file.
* Sets IR_SEND_PIN if required, e.g. if SEND_PWM_BY_TIMER for AVR is defined, which restricts the output to a dedicated pin number
*
* timerConfigForSend(aFrequencyKHz) must set output pin mode and disable receive interrupt if it uses the same resource
*
* This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
*
*************************************************************************************
* MIT License
*
* Copyright (c) 2021-2023 Armin Joachimsmeyer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************************
*/
#ifndef _IR_TIMER_HPP
#define _IR_TIMER_HPP
/** \addtogroup HardwareDependencies CPU / board dependent definitions
* @{
*/
/** \addtogroup Timer Usage of timers for the different CPU / boards
* @{
*/
/*
* Functions declared here
*/
void timerConfigForReceive(); // Initialization of 50 us timer, interrupts are still disabled
void timerEnableReceiveInterrupt(); // Enable interrupts of an initialized timer
void timerDisableReceiveInterrupt(); // Disable interrupts of an initialized timer
void timerResetInterruptPending(); // ISR helper function for some architectures, which require a manual reset
// of the pending interrupt (TIMER_REQUIRES_RESET_INTR_PENDING is defined). Otherwise empty.
void timerConfigForSend(uint16_t aFrequencyKHz); // Initialization of timer hardware generated PWM, if defined(SEND_PWM_BY_TIMER)
void enableSendPWMByTimer(); // Switch on PWM generation
void disableSendPWMByTimer(); // Switch off PWM generation
// SEND_PWM_BY_TIMER for different architectures is enabled / defined at IRremote.hpp line 195.
#if defined(SEND_PWM_BY_TIMER) && ( (defined(ESP32) || defined(ARDUINO_ARCH_RP2040) || defined(PARTICLE)) || defined(ARDUINO_ARCH_MBED) )
#define SEND_PWM_DOES_NOT_USE_RECEIVE_TIMER // Receive timer and send generation timer are independent here.
#endif
#if defined(IR_SEND_PIN) && defined(SEND_PWM_BY_TIMER) && !defined(SEND_PWM_DOES_NOT_USE_RECEIVE_TIMER) // For ESP32 etc. IR_SEND_PIN definition is useful
#undef IR_SEND_PIN // To avoid "warning: "IR_SEND_PIN" redefined". The user warning is done at IRremote.hpp line 202.
#endif
// Macros for enabling timers for development
//#define SEND_PWM_BY_TIMER
//#define IR_USE_AVR_TIMER1
//#define IR_USE_AVR_TIMER2
//#define IR_USE_AVR_TIMER3
//#define IR_USE_AVR_TIMER4
//#define IR_USE_AVR_TIMER4_HS
//#define IR_USE_AVR_TIMER5
//#define IR_USE_AVR_TIMER_TINY0
//#define IR_USE_AVR_TIMER_TINY1
//#define IR_USE_AVR_TIMER_A
//#define IR_USE_AVR_TIMER_B
//#define IR_USE_AVR_TIMER_D
//#define __MK20DX128__
//#define __MKL26Z64__
//#define __IMXRT1062__
//#define ESP8266
//#define ESP32
//#define ARDUINO_ARCH_SAMD
//#define ARDUINO_ARCH_MBED
//#define ARDUINO_ARCH_RP2040
//#define NRF5
//#define __STM32F1__
//#define STM32F1xx
//#define PARTICLE
//#define ARDUINO_ARCH_RENESAS
#if defined (DOXYGEN)
/**
* Hardware / timer dependent pin number for sending IR if SEND_PWM_BY_TIMER is defined. Otherwise used as default for IrSender.sendPin.
*/
#define IR_SEND_PIN
/**
* Configures the timer to be able to generate the receive sample interrupt,
* which consumes a small amount of CPU every 50 (MICROS_PER_TICK) us.
* The actual interrupt generation is controlled by timerEnableReceiveInterrupt() and timerDisableReceiveInterrupt().
* timerConfigForReceive() is used exclusively by IRrecv::start()
*/
void timerConfigForReceive() {
}
/**
* Enables the receive sample timer interrupt, which consumes a small amount of CPU every 50 us.
*/
void timerEnableReceiveInterrupt() {
}
/**
* Disables the receive sample timer interrupt. This must be done before using the timer for e.g. tone().
* Is a synonym for calling end() or stop().
*/
void timerDisableReceiveInterrupt() {
}
/**
* IF PWM should be generated not by software, but by a timer, this function sets output pin mode,
* configures the timer for generating a PWM with duty cycle of IR_SEND_DUTY_CYCLE_PERCENT
* and disables the receive interrupt if it uses the same resource.
* For most architectures, the pin number(s) which can be used for output is determined by the timer used!
* The output of the PWM signal is controlled by enableSendPWMByTimer() and disableSendPWMByTimer().
* timerConfigForSend() is used exclusively by IRsend::enableIROut().
* @param aFrequencyKHz Frequency of the sent PWM signal in kHz. There is no practical reason to have a sub kHz resolution for sending frequency :-).
*/
void timerConfigForSend(uint16_t aFrequencyKHz) {
}
/**
* Enables output of the PWM signal of the timer at the timer pin.
*/
void enableSendPWMByTimer() {
}
/**
* Disables output of the PWM signal of the timer at the timer pin and set it to inactive.
*/
void disableSendPWMByTimer() {
}
#elif defined(__AVR__)
/**********************************************************************************************************************
* Mapping of AVR boards to AVR timers
* For some CPU's you have the option to switch the timer and the hardware send pin
**********************************************************************************************************************/
/***************************************
* Plain AVR CPU's, no boards
***************************************/
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__) || defined(__AVR_ATmega168__) \
|| defined(__AVR_ATmega88P__) || defined(__AVR_ATmega88PB__)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER2)
//#define IR_USE_AVR_TIMER1 // send pin = pin 9
#define IR_USE_AVR_TIMER2 // send pin = pin 3
# endif
// Arduino Mega
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER2) && !defined(IR_USE_AVR_TIMER3) && !defined(IR_USE_AVR_TIMER4) && !defined(IR_USE_AVR_TIMER5)
//#define IR_USE_AVR_TIMER1 // send pin = pin 11
#define IR_USE_AVR_TIMER2 // send pin = pin 9
//#define IR_USE_AVR_TIMER3 // send pin = pin 5
//#define IR_USE_AVR_TIMER4 // send pin = pin 6
//#define IR_USE_AVR_TIMER5 // send pin = pin 46
# endif
// Leonardo
#elif defined(__AVR_ATmega32U4__) && ! defined(TEENSYDUINO) && ! defined(ARDUINO_AVR_PROMICRO)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER3) && !defined(IR_USE_AVR_TIMER4_HS)
//#define IR_USE_AVR_TIMER1 // send pin = pin 9
#define IR_USE_AVR_TIMER3 // send pin = pin 5
//#define IR_USE_AVR_TIMER4_HS // send pin = pin 13
# endif
// Nano Every, Uno WiFi Rev2 and similar
#elif defined(__AVR_ATmega808__) || defined(__AVR_ATmega809__) || defined(__AVR_ATmega3208__) || defined(__AVR_ATmega3209__) \
|| defined(__AVR_ATmega1608__) || defined(__AVR_ATmega1609__) || defined(__AVR_ATmega4808__) || defined(__AVR_ATmega4809__) || defined(__AVR_ATtiny1604__)
# if !defined(IR_USE_AVR_TIMER_B)
#define IR_USE_AVR_TIMER_B // send pin = pin 6 on ATmega4809 1 on ATmega4809
# endif
#elif defined(__AVR_ATtiny816__) || defined(__AVR_ATtiny1614__) || defined(__AVR_ATtiny1616__) || defined(__AVR_ATtiny3216__) || defined(__AVR_ATtiny3217__) // e.g. TinyCore boards
# if !defined(IR_USE_AVR_TIMER_A) && !defined(IR_USE_AVR_TIMER_D)
#define IR_USE_AVR_TIMER_A // use this if you use megaTinyCore, Tone is on TCB and millis() on TCD
//#define IR_USE_AVR_TIMER_D // use this if you use TinyCore
# endif
// ATmega8u2, ATmega16U2, ATmega32U2, ATmega8 - Timer 2 does not work with existing code below
#elif defined(__AVR_ATmega8U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega8__)
# if !defined(IR_USE_AVR_TIMER1)
#define IR_USE_AVR_TIMER1 // send pin = pin C6
# endif
// ATtiny84
#elif defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny88__)
# if !defined(IR_USE_AVR_TIMER1)
#define IR_USE_AVR_TIMER1 // send pin = pin 6
# endif
#elif defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
# if !defined(IR_USE_AVR_TIMER1)
#define IR_USE_AVR_TIMER1 // send pin = pin PB1 / 8
# endif
#define USE_TIMER_CHANNEL_B
//ATtiny85, 45, 25
#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
# if !defined(IR_USE_AVR_TIMER_TINY0) && !defined(IR_USE_AVR_TIMER_TINY1)
# if defined(ARDUINO_AVR_DIGISPARK) // tested with 16 and 8 MHz
#define IR_USE_AVR_TIMER_TINY0 // send pin = pin 1
// standard Digispark settings use timer 1 for millis() and micros()
# else
// standard ATTinyCore settings use timer 0 for millis() and micros()
#define IR_USE_AVR_TIMER_TINY1 // send pin = pin 4
# endif
# endif
/***************************************
* SPARKFUN Pro Micro board
***************************************/
#elif defined(ARDUINO_AVR_PROMICRO)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER3) && !defined(IR_USE_AVR_TIMER4_HS)
//#define IR_USE_AVR_TIMER1 // send pin = pin 9
#define IR_USE_AVR_TIMER3 // send pin = pin 5
//#define IR_USE_AVR_TIMER4_HS // send pin = pin 13
# endif
/***************************************
* TEENSY Boards
***************************************/
// Teensy 1.0
#elif defined(__AVR_AT90USB162__)
# if !defined(IR_USE_AVR_TIMER1)
#define IR_USE_AVR_TIMER1 // send pin = pin 17
# endif
// Teensy++ 1.0 & 2.0
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER2) && !defined(IR_USE_AVR_TIMER3)
//#define IR_USE_AVR_TIMER1 // send pin = pin 25
#define IR_USE_AVR_TIMER2 // send pin = pin 1
//#define IR_USE_AVR_TIMER3 // send pin = pin 16
# endif
// Teensy 2.0
#elif defined(__AVR_ATmega32U4__) && defined(TEENSYDUINO)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER3) && !defined(IR_USE_AVR_TIMER4_HS)
//#define IR_USE_AVR_TIMER1 // send pin = pin 14 (Teensy 2.0 - physical pin: B5)
//#define IR_USE_AVR_TIMER3 // send pin = pin 9 (Teensy 2.0 - physical pin: C6)
#define IR_USE_AVR_TIMER4_HS // send pin = pin 10 (Teensy 2.0 - physical pin: C7)
# endif
/***************************************
* CPU's with MegaCore
***************************************/
// MegaCore - ATmega64, ATmega128
#elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__)
# if !defined(IR_USE_AVR_TIMER1)
#define IR_USE_AVR_TIMER1 // send pin = pin 13
# endif
/***************************************
* CPU's with MajorCore
***************************************/
#elif defined(__AVR_ATmega8515__) || defined(__AVR_ATmega162__)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER3)
#define IR_USE_AVR_TIMER1 // send pin = pin 13
//#define IR_USE_AVR_TIMER3 // send pin = pin 12 - ATmega162 only
# endif
/***************************************
* CPU's with MightyCore
***************************************/
// MightyCore - ATmega1284
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER2) && !defined(IR_USE_AVR_TIMER3)
//#define IR_USE_AVR_TIMER1 // send pin = pin 13
#define IR_USE_AVR_TIMER2 // send pin = pin 14
//#define IR_USE_AVR_TIMER3 // send pin = pin 6
# endif
// MightyCore - ATmega164, ATmega324, ATmega644
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \
|| defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \
|| defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \
|| defined(__AVR_ATmega164P__)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER2)
//#define IR_USE_AVR_TIMER1 // send pin = pin 13
#define IR_USE_AVR_TIMER2 // send pin = pin 14
# endif
// MightyCore - ATmega8535, ATmega16, ATmega32
#elif defined(__AVR_ATmega8535__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__)
# if !defined(IR_USE_AVR_TIMER1)
#define IR_USE_AVR_TIMER1 // send pin = pin 13
# endif
#endif // AVR CPU's
/**********************************************************************************************************************
* End of AVR mapping, start of AVR timers
**********************************************************************************************************************/
/*
* AVR Timer1 (16 bits)
*/
#if defined(IR_USE_AVR_TIMER1)
# if defined(TIMSK1)
#define TIMSK TIMSK1 // use the value of TIMSK1 for the statements below
# endif
void timerEnableReceiveInterrupt() {
TIMSK |= _BV(OCIE1A);
}
void timerDisableReceiveInterrupt() {
TIMSK &= ~_BV(OCIE1A);
}
# if defined(USE_TIMER_CHANNEL_B)
# if defined(TIMER1_COMPB_vect)
#define TIMER_INTR_NAME TIMER1_COMPB_vect
# elif defined(TIM1_COMPB_vect)
#define TIMER_INTR_NAME TIM1_COMPB_vect
# endif
#else
# if defined(TIMER1_COMPA_vect)
#define TIMER_INTR_NAME TIMER1_COMPA_vect
# elif defined(TIM1_COMPA_vect)
#define TIMER_INTR_NAME TIM1_COMPA_vect
# endif
# endif
void timerConfigForReceive() {
TCCR1A = 0;
TCCR1B = _BV(WGM12) | _BV(CS10); // CTC mode, no prescaling
OCR1A = (F_CPU * MICROS_PER_TICK) / MICROS_IN_ONE_SECOND; // 16 * 50 = 800
TCNT1 = 0;
}
# if defined(SEND_PWM_BY_TIMER)
# if defined(CORE_OC1A_PIN)
#define IR_SEND_PIN CORE_OC1A_PIN // Teensy
# elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define IR_SEND_PIN 11 // Arduino Mega
// MightyCore, MegaCore, MajorCore
# elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) \
|| defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \
|| defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \
|| defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \
|| defined(__AVR_ATmega164P__) || defined(__AVR_ATmega32__) \
|| defined(__AVR_ATmega16__) || defined(__AVR_ATmega8535__) \
|| defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) \
|| defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__) \
|| defined(__AVR_ATmega8515__) || defined(__AVR_ATmega162__)
#define IR_SEND_PIN 13
# elif defined(__AVR_ATtiny84__)
#define IR_SEND_PIN 6
# elif defined(__AVR_ATtiny88__)
#define IR_SEND_PIN 8
# elif defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
/*
* !!! IR_SEND_PIN value must correspond to ENABLE_SEND_PWM_BY_TIMER below !!!
*/
# if defined(USE_TIMER_CHANNEL_B)
#define IR_SEND_PIN PIN_PB1 // OC1BU / PB1 / Pin9 at ATTinyCore
//#define IR_SEND_PIN PIN_PB3 // OC1BV / PB3 / Pin11 at ATTinyCore
//#define IR_SEND_PIN PIN_PB5 // OC1BW / PB5 / Pin13 at ATTinyCore
//#define IR_SEND_PIN PIN_PB7 // OC1BX / PB7 / Pin15 at ATTinyCore
# else
#define IR_SEND_PIN PIN_PB0 // OC1AU / PB1 / Pin8 at ATTinyCore
//#define IR_SEND_PIN PIN_PB2 // OC1AV / PB3 / Pin10 at ATTinyCore
//#define IR_SEND_PIN PIN_PB4 // OC1AW / PB5 / Pin12 at ATTinyCore
//#define IR_SEND_PIN PIN_PB6 // OC1AX / PB6 / Pin14 at ATTinyCore
# endif
# else
#define IR_SEND_PIN 9 // OC1A Arduino Duemilanove, Diecimila, LilyPad, Sparkfun Pro Micro, Leonardo, MH-ET Tiny88 etc.
# endif // defined(CORE_OC1A_PIN)
# if defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
// Clear OC1A/OC1B on Compare Match when up-counting. Set OC1A/OC1B on Compare Match when down counting.
# if defined(USE_TIMER_CHANNEL_B)
void enableSendPWMByTimer() {
TCNT1 = 0;
TCCR1A |= _BV(COM1B1);
TCCR1D |= _BV(OC1BU); // + enable OC1BU as output
//TCNT1 = 0; TCCR1A |= _BV(COM1B1); TCCR1D |= _BV(OC1BV); // + enable OC1BV as output
//TCNT1 = 0; TCCR1A |= _BV(COM1B1); TCCR1D |= _BV(OC1BW); // + enable OC1BW as output
//TCNT1 = 0; TCCR1A |= _BV(COM1B1); TCCR1D |= _BV(OC1BX); // + enable OC1BX as output
}
# else
void disableSendPWMByTimer() {
TCNT1 = 0;
TCCR1A |= _BV(COM1A1);
TCCR1D |= _BV(OC1AU); // + enable OC1BU as output
//TCNT1 = 0; TCCR1A |= _BV(COM1A1); TCCR1D |= _BV(OC1AV); // + enable OC1BV as output
//TCNT1 = 0; TCCR1A |= _BV(COM1A1); TCCR1D |= _BV(OC1AW); // + enable OC1BW as output
//TCNT1 = 0; TCCR1A |= _BV(COM1A1); TCCR1D |= _BV(OC1AX); // + enable OC1BX as output
}
# endif
void disableSendPWMByTimer() {
TCCR1D = 0;
}
# else
# if defined(USE_TIMER_CHANNEL_B)
void enableSendPWMByTimer() {
TCNT1 = 0;
TCCR1A |= _BV(COM1B1); // Clear OC1A/OC1B on Compare Match when up-counting. Set OC1A/OC1B on Compare Match when counting down.
}
void disableSendPWMByTimer() {
TCCR1A &= ~(_BV(COM1B1));
}
# else
void enableSendPWMByTimer() {
TCNT1 = 0;
TCCR1A |= _BV(COM1A1); // Clear OC1A/OC1B on Compare Match when up-counting. Set OC1A/OC1B on Compare Match when downcounting.
}
void disableSendPWMByTimer() {
TCCR1A &= ~(_BV(COM1A1));
}
# endif
# endif
/*
* timerConfigForSend() is used exclusively by IRsend::enableIROut()
* Set output pin mode and disable receive interrupt if it uses the same resource
*/
void timerConfigForSend(uint16_t aFrequencyKHz) {
timerDisableReceiveInterrupt();
# if (((F_CPU / 2000) / 38) < 256)
const uint16_t tPWMWrapValue = (F_CPU / 2000) / (aFrequencyKHz); // 210,52 for 38 kHz @16 MHz clock, 2000 instead of 1000 because of Phase Correct PWM
TCCR1A = _BV(WGM11); // PWM, Phase Correct, Top is ICR1
TCCR1B = _BV(WGM13) | _BV(CS10); // CS10 -> no prescaling
ICR1 = tPWMWrapValue - 1;
# if defined(USE_TIMER_CHANNEL_B)
OCR1B = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
# else
OCR1A = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
# endif
TCNT1 = 0; // not really required, since we have an 8 bit counter, but makes the signal more reproducible
# else
const uint16_t tPWMWrapValue = ((F_CPU / 8) / 2000) / (aFrequencyKHz); // 2000 instead of 1000 because of Phase Correct PWM
TCCR1A = _BV(WGM11);// PWM, Phase Correct, Top is ICR1
TCCR1B = _BV(WGM13) | _BV(CS11);// CS11 -> Prescaling by 8
ICR1 = tPWMWrapValue - 1;
# if defined(USE_TIMER_CHANNEL_B)
OCR1A = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
# else
OCR1A = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
# endif
TCNT1 = 0; // not really required, since we have an 8 bit counter, but makes the signal more reproducible
# endif
}
# endif // defined(SEND_PWM_BY_TIMER)
/*
* AVR Timer2 (8 bits) // Tone timer on Uno
*/
#elif defined(IR_USE_AVR_TIMER2)
void timerEnableReceiveInterrupt() {
TIMSK2 = _BV(OCIE2B); // Output Compare Match A Interrupt Enable
}
void timerDisableReceiveInterrupt() {
TIMSK2 = 0;
}
#define TIMER_INTR_NAME TIMER2_COMPB_vect // We use TIMER2_COMPB_vect to be compatible with tone() library
#define TIMER_COUNT_TOP (F_CPU * MICROS_PER_TICK / MICROS_IN_ONE_SECOND)
void timerConfigForReceive() {
# if (TIMER_COUNT_TOP < 256)
TCCR2A = _BV(WGM21);
TCCR2B = _BV(CS20);
OCR2A = TIMER_COUNT_TOP;
OCR2B = TIMER_COUNT_TOP;
TCNT2 = 0;
# else
TCCR2A = _BV(WGM21);
TCCR2B = _BV(CS21);
OCR2A = TIMER_COUNT_TOP / 8;
OCR2B = TIMER_COUNT_TOP / 8;
TCNT2 = 0;
# endif
}
# if defined(SEND_PWM_BY_TIMER)
# if defined(CORE_OC2B_PIN)
#define IR_SEND_PIN CORE_OC2B_PIN // Teensy
# elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define IR_SEND_PIN 9 // Arduino Mega
# elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) \
|| defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \
|| defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \
|| defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \
|| defined(__AVR_ATmega164P__)
#define IR_SEND_PIN 14 // MightyCore, MegaCore
# else
#define IR_SEND_PIN 3 // Arduino Duemilanove, Diecimila, LilyPad, etc
# endif // defined(CORE_OC2B_PIN)
void enableSendPWMByTimer() {
TCNT2 = 0;
TCCR2A |= _BV(COM2B1); // Clear OC2B on Compare Match
}
void disableSendPWMByTimer() {
TCCR2A &= ~(_BV(COM2B1)); // Normal port operation, OC2B disconnected.
}
/*
* timerConfigForSend() is used exclusively by IRsend::enableIROut()
* Set output pin mode and disable receive interrupt if it uses the same resource
*/
void timerConfigForSend(uint16_t aFrequencyKHz) {
timerDisableReceiveInterrupt();
# if (((F_CPU / 2000) / 38) < 256)
/*
* tPWMWrapValue is 210.52 for 38 kHz, 17.58 for 455 kHz @16 MHz clock.
* 210 -> 38.095 kHz, 17 -> 470.588 kHz @16 MHz clock.
* We use 2000 instead of 1000 in the formula, because of Phase Correct PWM.
*/
const uint16_t tPWMWrapValue = (F_CPU / 2000) / (aFrequencyKHz);
TCCR2A = _BV(WGM20); // PWM, Phase Correct, Top is OCR2A
TCCR2B = _BV(WGM22) | _BV(CS20); // CS20 -> no prescaling
OCR2A = tPWMWrapValue - 1; // The top value for the timer. The modulation frequency will be F_CPU / 2 / (OCR2A + 1).
OCR2B = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
TCNT2 = 0; // not really required, since we have an 8 bit counter, but makes the signal more reproducible
# else
const uint16_t tPWMWrapValue = ((F_CPU / 8) / 2000) / (aFrequencyKHz); // 2000 instead of 1000 because of Phase Correct PWM
TCCR2A = _BV(WGM20);// PWM, Phase Correct, Top is OCR2A
TCCR2B = _BV(WGM22) | _BV(CS21);// CS21 -> Prescaling by 8
OCR2A = tPWMWrapValue - 1;
OCR2B = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
TCNT2 = 0;// not really required, since we have an 8 bit counter, but makes the signal more reproducible
# endif
}
# endif // defined(SEND_PWM_BY_TIMER)
/*
* AVR Timer3 (16 bits)
*/
#elif defined(IR_USE_AVR_TIMER3)
void timerEnableReceiveInterrupt() {
TIMSK3 = _BV(OCIE3B);
}
void timerDisableReceiveInterrupt() {
TIMSK3 = 0;
}
#define TIMER_INTR_NAME TIMER3_COMPB_vect
void timerConfigForReceive() {
TCCR3A = 0;
TCCR3B = _BV(WGM32) | _BV(CS30);
OCR3A = F_CPU * MICROS_PER_TICK / MICROS_IN_ONE_SECOND;
OCR3B = F_CPU * MICROS_PER_TICK / MICROS_IN_ONE_SECOND;
TCNT3 = 0;
}
# if defined(SEND_PWM_BY_TIMER)
# if defined(CORE_OC3A_PIN)
#define IR_SEND_PIN CORE_OC3A_PIN // Teensy
# elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) \
|| defined(__AVR_ATmega32U4__) || defined(ARDUINO_AVR_PROMICRO)
#define IR_SEND_PIN 5 // Arduino Mega, Arduino Leonardo, Sparkfun Pro Micro
# elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
#define IR_SEND_PIN 6 // MightyCore, MegaCore
# else
#error Please add OC3A pin number here
# endif
void enableSendPWMByTimer() {
TCNT3 = 0;
TCCR3A |= _BV(COM3A1);
}
void disableSendPWMByTimer() {
TCCR3A &= ~(_BV(COM3A1));
}
/*
* timerConfigForSend() is used exclusively by IRsend::enableIROut()
* Set output pin mode and disable receive interrupt if it uses the same resource
*/
void timerConfigForSend(uint16_t aFrequencyKHz) {
#if F_CPU > 16000000
#error "Creating timer PWM with timer 3 is not supported for F_CPU > 16 MHz"
#endif
timerDisableReceiveInterrupt();
const uint16_t tPWMWrapValue = (F_CPU / 2000) / (aFrequencyKHz); // 210,52 for 38 kHz @16 MHz clock, 2000 instead of 1000 because of Phase Correct PWM
TCCR3A = _BV(WGM31);
TCCR3B = _BV(WGM33) | _BV(CS30); // PWM, Phase Correct, ICRn as TOP, complete period is double of tPWMWrapValue
ICR3 = tPWMWrapValue - 1;
OCR3A = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
TCNT3 = 0; // required, since we have an 16 bit counter
}
# endif // defined(SEND_PWM_BY_TIMER)
/*
* AVR Timer4 (16 bits)
*/
#elif defined(IR_USE_AVR_TIMER4)
void timerEnableReceiveInterrupt() {
TIMSK4 = _BV(OCIE4A);
}
void timerDisableReceiveInterrupt() {
TIMSK4 = 0;
}
#define TIMER_INTR_NAME TIMER4_COMPA_vect
void timerConfigForReceive() {
TCCR4A = 0;
TCCR4B = _BV(WGM42) | _BV(CS40);
OCR4A = F_CPU * MICROS_PER_TICK / MICROS_IN_ONE_SECOND;
TCNT4 = 0;
}
# if defined(SEND_PWM_BY_TIMER)
# if defined(CORE_OC4A_PIN)
#define IR_SEND_PIN CORE_OC4A_PIN
# elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define IR_SEND_PIN 6 // Arduino Mega
# else
#error Please add OC4A pin number here
# endif
void enableSendPWMByTimer() {
TCNT4 = 0;
TCCR4A |= _BV(COM4A1);
}
void disableSendPWMByTimer() {
TCCR4A &= ~(_BV(COM4A1));
}
void timerConfigForSend(uint16_t aFrequencyKHz) {
#if F_CPU > 16000000
#error "Creating timer PWM with timer 4 is not supported for F_CPU > 16 MHz"
#endif
timerDisableReceiveInterrupt();
const uint16_t tPWMWrapValue = (F_CPU / 2000) / (aFrequencyKHz); // 210,52 for 38 kHz @16 MHz clock, 2000 instead of 1000 because of Phase Correct PWM
TCCR4A = _BV(WGM41);
TCCR4B = _BV(WGM43) | _BV(CS40);
ICR4 = tPWMWrapValue - 1;
OCR4A = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
TCNT4 = 0; // required, since we have an 16 bit counter
}
# endif // defined(SEND_PWM_BY_TIMER)
/*
* AVR Timer4 (10 bits, high speed option)
*/
#elif defined(IR_USE_AVR_TIMER4_HS)
void timerEnableReceiveInterrupt() {
TIMSK4 = _BV(TOIE4);
}
void timerDisableReceiveInterrupt() {
TIMSK4 = 0;
}
#define TIMER_INTR_NAME TIMER4_OVF_vect
void timerConfigForReceive() {
TCCR4A = 0;
TCCR4B = _BV(CS40);
TCCR4C = 0;
TCCR4D = 0;
TCCR4E = 0;
TC4H = (F_CPU * MICROS_PER_TICK / MICROS_IN_ONE_SECOND) >> 8;
OCR4C = (F_CPU * MICROS_PER_TICK / MICROS_IN_ONE_SECOND) & 255;
TC4H = 0;
TCNT4 = 0;
}
# if defined(SEND_PWM_BY_TIMER)
# if defined(CORE_OC4A_PIN)
#define IR_SEND_PIN CORE_OC4A_PIN // Teensy 2.0
# elif defined(ARDUINO_AVR_PROMICRO)
#define IR_SEND_PIN 5 // Sparkfun Pro Micro
# elif defined(__AVR_ATmega32U4__)
#define IR_SEND_PIN 13 // Leonardo
# else
#error Please add OC4A pin number here
# endif
# if defined(ARDUINO_AVR_PROMICRO) // Sparkfun Pro Micro
void enableSendPWMByTimer() {
TCNT4 = 0;
TCCR4A |= _BV(COM4A0); // Use complementary OC4A output on pin 5
}
void disableSendPWMByTimer() {
TCCR4A &= ~(_BV(COM4A0)); // (Pro Micro does not map PC7 (32/ICP3/CLK0/OC4A)
}
// of ATmega32U4 )
# else
void enableSendPWMByTimer() {
TCNT4 = 0;
TCCR4A |= _BV(COM4A1);
DDRC |= 1 << 7;
}
void disableSendPWMByTimer() {
TCCR4A &= ~(_BV(COM4A1));
}
# endif
/*
* timerConfigForSend() is used exclusively by IRsend::enableIROut()
* Set output pin mode and disable receive interrupt if it uses the same resource
*/
void timerConfigForSend(uint16_t aFrequencyKHz) {
#if F_CPU > 16000000
#error "Creating timer PWM with timer 4 HS is not supported for F_CPU > 16 MHz"
#endif
timerDisableReceiveInterrupt();
const uint16_t tPWMWrapValue = ((F_CPU / 2000) / (aFrequencyKHz)) - 1; // 210,52 for 38 kHz @16 MHz clock, 2000 instead of 1000 because of Phase Correct PWM
TCCR4A = (1 << PWM4A);
TCCR4B = _BV(CS40);
TCCR4C = 0;
TCCR4D = (1 << WGM40);
TCCR4E = 0;
TC4H = tPWMWrapValue >> 8;
OCR4C = tPWMWrapValue;
TC4H = (tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT / 100) >> 8;
OCR4A = (tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT / 100) & 255;
TCNT4 = 0; // not really required, since we have an 8 bit counter, but makes the signal more reproducible
}
# endif // defined(SEND_PWM_BY_TIMER)
/*
* AVR Timer5 (16 bits)
*/
#elif defined(IR_USE_AVR_TIMER5)
void timerEnableReceiveInterrupt() {
TIMSK5 = _BV(OCIE5A);
}
void timerDisableReceiveInterrupt() {
TIMSK5 = 0;
}
#define TIMER_INTR_NAME TIMER5_COMPA_vect
void timerConfigForReceive() {
TCCR5A = 0;
TCCR5B = _BV(WGM52) | _BV(CS50);
OCR5A = F_CPU * MICROS_PER_TICK / MICROS_IN_ONE_SECOND;
TCNT5 = 0;
}
# if defined(SEND_PWM_BY_TIMER)
# if defined(CORE_OC5A_PIN)
#define IR_SEND_PIN CORE_OC5A_PIN
# elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define IR_SEND_PIN 46 // Arduino Mega
# else
#error Please add OC5A pin number here
# endif
void enableSendPWMByTimer() {
TCNT5 = 0;
TCCR5A |= _BV(COM5A1);
}
void disableSendPWMByTimer() {
TCCR5A &= ~(_BV(COM5A1));
}
/*
* timerConfigForSend() is used exclusively by IRsend::enableIROut()
* Set output pin mode and disable receive interrupt if it uses the same resource
*/
void timerConfigForSend(uint16_t aFrequencyKHz) {
#if F_CPU > 16000000
#error "Creating timer PWM with timer 5 is not supported for F_CPU > 16 MHz"
#endif
timerDisableReceiveInterrupt();
const uint16_t tPWMWrapValue = (F_CPU / 2000) / (aFrequencyKHz); // 210,52 for 38 kHz @16 MHz clock, 2000 instead of 1000 because of Phase Correct PWM
TCCR5A = _BV(WGM51);
TCCR5B = _BV(WGM53) | _BV(CS50);
ICR5 = tPWMWrapValue - 1;
OCR5A = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
TCNT5 = 0; // required, since we have an 16 bit counter
}
# endif // defined(SEND_PWM_BY_TIMER)
/*
* AVR Timer0 for ATtinies (8 bits)
*/
#elif defined(IR_USE_AVR_TIMER_TINY0)
void timerEnableReceiveInterrupt() {
TIMSK |= _BV(OCIE0A);
}
void timerDisableReceiveInterrupt() {
TIMSK &= ~(_BV(OCIE0A));
}
#define TIMER_INTR_NAME TIMER0_COMPA_vect
#define TIMER_COUNT_TOP (F_CPU * MICROS_PER_TICK / MICROS_IN_ONE_SECOND)
void timerConfigForReceive() {
# if (TIMER_COUNT_TOP < 256)
TCCR0A = _BV(WGM01); // CTC, Top is OCR0A
TCCR0B = _BV(CS00);// No prescaling
OCR0A = TIMER_COUNT_TOP;
TCNT0 = 0;
# else
TCCR0A = _BV(WGM01);
TCCR0B = _BV(CS01); // prescaling by 8
OCR0A = TIMER_COUNT_TOP / 8;
TCNT0 = 0;
# endif
}
# if defined(SEND_PWM_BY_TIMER)
#define IR_SEND_PIN 1
void enableSendPWMByTimer() {
TCNT0 = 0;
TCCR0A |= _BV(COM0B1);
}
void disableSendPWMByTimer() {
TCCR0A &= ~(_BV(COM0B1));
}
/*
* timerConfigForSend() is used exclusively by IRsend::enableIROut()
* Set output pin mode and disable receive interrupt if it uses the same resource
*/
void timerConfigForSend(uint16_t aFrequencyKHz) {
#if F_CPU > 16000000
#error "Creating timer PWM with timer TINY0 is not supported for F_CPU > 16 MHz"
#endif
timerDisableReceiveInterrupt();
const uint16_t tPWMWrapValue = (F_CPU / 2000) / (aFrequencyKHz); // 210,52 for 38 kHz @16 MHz clock, 2000 instead of 1000 because of Phase Correct PWM
TCCR0A = _BV(WGM00); // PWM, Phase Correct, Top is OCR0A
TCCR0B = _BV(WGM02) | _BV(CS00); // CS00 -> no prescaling
OCR0A = tPWMWrapValue - 1;
OCR0B = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
TCNT0 = 0; // not really required, since we have an 8 bit counter, but makes the signal more reproducible
}
# endif // defined(SEND_PWM_BY_TIMER)
/*
* AVR Timer1 for ATtinies (8 bits)
*/
#elif defined(IR_USE_AVR_TIMER_TINY1)
void timerEnableReceiveInterrupt() {
TIMSK |= _BV(OCIE1B);
}
void timerDisableReceiveInterrupt() {
TIMSK &= ~(_BV(OCIE1B));
}
#define TIMER_INTR_NAME TIMER1_COMPB_vect
#define TIMER_COUNT_TOP (F_CPU * MICROS_PER_TICK / MICROS_IN_ONE_SECOND)
void timerConfigForReceive() {
# if (TIMER_COUNT_TOP < 256)
TCCR1 = _BV(CTC1) | _BV(CS10); // Clear Timer/Counter on Compare Match, Top is OCR1C, No prescaling
GTCCR = 0;// normal, non-PWM mode
OCR1C = TIMER_COUNT_TOP;
TCNT1 = 0;
# else
TCCR1 = _BV(CTC1) | _BV(CS12); // Clear Timer/Counter on Compare Match, Top is OCR1C, prescaling by 8
GTCCR = 0; // normal, non-PWM mode
OCR1C = TIMER_COUNT_TOP / 8;
TCNT1 = 0;
# endif
}
# if defined(SEND_PWM_BY_TIMER)
#define IR_SEND_PIN 4
void enableSendPWMByTimer() {
TCNT1 = 0;
GTCCR |= _BV(PWM1B) | _BV(COM1B0); // Enable pin 4 PWM output (PB4 - Arduino D4)
}
void disableSendPWMByTimer() {
GTCCR &= ~(_BV(PWM1B) | _BV(COM1B0));
}
/*
* timerConfigForSend() is used exclusively by IRsend::enableIROut()
* Set output pin mode and disable receive interrupt if it uses the same resource
*/
void timerConfigForSend(uint16_t aFrequencyKHz) {
timerDisableReceiveInterrupt();
# if (((F_CPU / 1000) / 38) < 256)
const uint16_t tPWMWrapValue = (F_CPU / 1000) / (aFrequencyKHz); // 421 @16 MHz, 26 @1 MHz and 38 kHz
TCCR1 = _BV(CTC1) | _BV(CS10);// CTC1 = 1: TOP value set to OCR1C, CS10 No Prescaling
OCR1C = tPWMWrapValue - 1;
OCR1B = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
TCNT1 = 0;// not really required, since we have an 8 bit counter, but makes the signal more reproducible
GTCCR = _BV(PWM1B) | _BV(COM1B0);// PWM1B = 1: Enable PWM for OCR1B, COM1B0 Clear on compare match
# else
const uint16_t tPWMWrapValue = ((F_CPU / 2) / 1000) / (aFrequencyKHz); // 210 for 16 MHz and 38 kHz
TCCR1 = _BV(CTC1) | _BV(CS11); // CTC1 = 1: TOP value set to OCR1C, CS11 Prescaling by 2
OCR1C = tPWMWrapValue - 1;
OCR1B = ((tPWMWrapValue * IR_SEND_DUTY_CYCLE_PERCENT) / 100) - 1;
TCNT1 = 0; // not really required, since we have an 8 bit counter, but makes the signal more reproducible
GTCCR = _BV(PWM1B) | _BV(COM1B0); // PWM1B = 1: Enable PWM for OCR1B, COM1B0 Clear on compare match
# endif
}
# endif // defined(SEND_PWM_BY_TIMER)
/*
* AVR TimerA for TinyCore 32 (16 bits)
*/
#elif defined(IR_USE_AVR_TIMER_A)
#define TIMER_REQUIRES_RESET_INTR_PENDING
void timerResetInterruptPending() {
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm;
}
void timerEnableReceiveInterrupt() {
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm;
}
void timerDisableReceiveInterrupt() {
TCA0.SINGLE.INTCTRL &= ~(TCA_SINGLE_OVF_bm);
}
#define TIMER_INTR_NAME TCA0_OVF_vect
// For MegaTinyCore:
// TCB1 is used by Tone()
// TCB2 is used by Servo, but we cannot hijack the ISR, so we must use a dedicated timer for the 20 ms interrupt
// TCB3 is used by millis()
// Must use TCA0, since TCBx have only prescaler %2. Use single (16bit) mode, because it seems to be easier :-)
void timerConfigForReceive() {
TCA0.SINGLE.CTRLD = 0; // Single mode - required at least for MegaTinyCore
TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc; // Normal mode, top = PER
TCA0.SINGLE.PER = (F_CPU / MICROS_IN_ONE_SECOND) * MICROS_PER_TICK; // 800 at 16 MHz
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm; // set prescaler to 1 and enable timer
}
# if defined(SEND_PWM_BY_TIMER)
#error "No support for hardware PWM generation for ATtiny3216/17 etc."
# endif // defined(SEND_PWM_BY_TIMER)
/*
* AVR TimerB (8 bits) for ATmega4809 (Nano Every, Uno WiFi Rev2)
*/
#elif defined(IR_USE_AVR_TIMER_B)
// ATmega4809 TCB0
#define TIMER_REQUIRES_RESET_INTR_PENDING
void timerResetInterruptPending() {
TCB0.INTFLAGS = TCB_CAPT_bm;
}
void timerEnableReceiveInterrupt() {
TCB0.INTCTRL = TCB_CAPT_bm;
}
void timerDisableReceiveInterrupt() {
TCB0.INTCTRL &= ~(TCB_CAPT_bm);
}
#define TIMER_INTR_NAME TCB0_INT_vect
void timerConfigForReceive() {
TCB0.CTRLB = (TCB_CNTMODE_INT_gc); // Periodic interrupt mode
TCB0.CCMP = ((F_CPU * MICROS_PER_TICK) / MICROS_IN_ONE_SECOND);
TCB0.INTFLAGS = TCB_CAPT_bm; // reset interrupt flags
TCB0.CTRLA = (TCB_CLKSEL_CLKDIV1_gc) | (TCB_ENABLE_bm);
}
# if defined(SEND_PWM_BY_TIMER)
# if defined(__AVR_ATmega4808__) || defined(__AVR_ATmega4809__)
#define IR_SEND_PIN 6 // PF4 on ATmega4809 / Nano Every (see pins_arduino.h digital_pin_to_timer)
# else
#error SEND_PWM_BY_TIMER not yet supported for this CPU
# endif
void enableSendPWMByTimer() {
TCB0.CNT = 0;
TCB0.CTRLB |= TCB_CCMPEN_bm; // set Compare/Capture Output Enable
}
void disableSendPWMByTimer() {
TCB0.CTRLB &= ~(TCB_CCMPEN_bm);
}
/*
* timerConfigForSend() is used exclusively by IRsend::enableIROut()
* Set output pin mode and disable receive interrupt if it uses the same resource
*/
void timerConfigForSend(uint16_t aFrequencyKHz) {
#if F_CPU > 16000000