-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPinChangeInterrupt.h
898 lines (825 loc) · 25.7 KB
/
PinChangeInterrupt.h
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
/*
Copyright (c) 2014-2018 NicoHood
See the readme for credit to other people.
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.
*/
// Include Guard
#pragma once
// Software Version
#define PCINT_VERSION 126
#include <avr/io.h>
#include <avr/interrupt.h>
#ifdef ARDUINO
#include "Arduino.h"
#ifndef ARDUINO_ARCH_AVR
#error This library can only be used with AVR
#endif
#else
#ifndef LOW
#define LOW 0x0
#endif
#ifndef CHANGE
#define CHANGE 0x1
#endif
#ifndef FALLING
#define FALLING 0x2
#endif
#ifndef RISING
#define RISING 0x3
#endif
#endif
//================================================================================
// General Helper Definitions and Mappings
//================================================================================
// Settings and Board definitions are seperated to get an better overview.
// The order and position of the inclusion is important!
#include "PinChangeInterruptSettings.h"
#include "PinChangeInterruptBoards.h"
#include "PinChangeInterruptPins.h"
#if !PCINT_NUM_USED_PORTS
#error Please enable at least one PCINT port and pin!
#endif
// manually include cpp files to save flash if only 1 ISR is present
// it includes all ISR files but only the 1 available ISR will/can be compiled
#if (PCINT_NUM_USED_PORTS == 1)
#ifndef PCINT_COMPILE_ENABLED_ISR
#define PCINT_COMPILE_ENABLED_ISR
#endif
#endif
//================================================================================
// Makro Definitions
//================================================================================
// generates the callback for easier reordering in Settings
#define PCINT_MACRO_BRACKETS ()
#define PCINT_MACRO_TRUE == true)
#define PCINT_CALLBACK(bit, pcint) \
if (PCINT_USE_PCINT ## pcint PCINT_MACRO_TRUE \
if (trigger & (1 << bit)) \
PinChangeInterruptEventPCINT ## pcint PCINT_MACRO_BRACKETS
// definition used by the user to create custom LowLevel PCINT Events
#define PinChangeInterruptEvent_Wrapper(n) PinChangeInterruptEventPCINT ## n
#define PinChangeInterruptEvent(n) PinChangeInterruptEvent_Wrapper(n)
// missing 1.0.6 definition workaround
#ifndef NOT_AN_INTERRUPT
#define NOT_AN_INTERRUPT -1
#endif
// convert a normal pin to its PCINT number (0 - max 23), used by the user
// calculates the pin by the Arduino definitions
#if defined(PCIE0)
#define digitalPinToPinChangeInterrupt(p) (digitalPinToPCICR(p) ? ((8 * (digitalPinToPCICRbit(p) - PCIE0)) + digitalPinToPCMSKbit(p)) : NOT_AN_INTERRUPT)
#elif defined(PCIE)
#define digitalPinToPinChangeInterrupt(p) (digitalPinToPCICR(p) ? ((8 * (digitalPinToPCICRbit(p) - PCIE)) + digitalPinToPCMSKbit(p)) : NOT_AN_INTERRUPT)
#else
#error MCU has no such a register
#endif
// alias for shorter writing
#define PCINTEvent(n) PinChangeInterruptEvent_Wrapper(n)
#define digitalPinToPCINT digitalPinToPinChangeInterrupt
#define attachPCINT attachPinChangeInterrupt
#define enablePCINT enablePinChangeInterrupt
#define detachPCINT detachPinChangeInterrupt
#define disablePCINT disablePinChangeInterrupt
#define getPCINTTrigger getPinChangeInterruptTrigger
//================================================================================
// Function Prototypes + Variables
//================================================================================
// typedef for our callback function pointers
typedef void(*callback)(void);
// useless function for weak implemented/not used functions, extern c needed for the alias
#ifdef __cplusplus
extern "C" {
#endif
void pcint_null_callback(void);
#ifdef __cplusplus
}
#endif
void PinChangeInterruptEventPCINT0(void);
void PinChangeInterruptEventPCINT1(void);
void PinChangeInterruptEventPCINT2(void);
void PinChangeInterruptEventPCINT3(void);
void PinChangeInterruptEventPCINT4(void);
void PinChangeInterruptEventPCINT5(void);
void PinChangeInterruptEventPCINT6(void);
void PinChangeInterruptEventPCINT7(void);
void PinChangeInterruptEventPCINT8(void);
void PinChangeInterruptEventPCINT9(void);
void PinChangeInterruptEventPCINT10(void);
void PinChangeInterruptEventPCINT11(void);
void PinChangeInterruptEventPCINT12(void);
void PinChangeInterruptEventPCINT13(void);
void PinChangeInterruptEventPCINT14(void);
void PinChangeInterruptEventPCINT15(void);
void PinChangeInterruptEventPCINT16(void);
void PinChangeInterruptEventPCINT17(void);
void PinChangeInterruptEventPCINT18(void);
void PinChangeInterruptEventPCINT19(void);
void PinChangeInterruptEventPCINT20(void);
void PinChangeInterruptEventPCINT21(void);
void PinChangeInterruptEventPCINT22(void);
void PinChangeInterruptEventPCINT23(void);
void PinChangeInterruptEventPCINT24(void);
void PinChangeInterruptEventPCINT25(void);
void PinChangeInterruptEventPCINT26(void);
void PinChangeInterruptEventPCINT27(void);
void PinChangeInterruptEventPCINT28(void);
void PinChangeInterruptEventPCINT29(void);
void PinChangeInterruptEventPCINT30(void);
void PinChangeInterruptEventPCINT31(void);
extern uint8_t oldPorts[PCINT_NUM_USED_PORTS];
extern uint8_t fallingPorts[PCINT_NUM_USED_PORTS];
extern uint8_t risingPorts[PCINT_NUM_USED_PORTS];
static inline uint8_t getArrayPosPCINT(uint8_t pcintPort) __attribute__((always_inline));
uint8_t getArrayPosPCINT(uint8_t pcintPort) {
/*
Maps the port to the array.
This is needed since you can deactivate ports
and the array will dynamically resize to save ram.
The function does not need that much flash since the if and else
are known at compile time, so the compiler removes all the complex logic.
The return is is the input if all pins are activated for example.
That's why the function is inline.
*/
if (PCINT_NUM_USED_PORTS == 1) {
// only the first element is used for a single port
return 0;
}
else if (PCINT_NUM_USED_PORTS == PCINT_NUM_PORTS) {
// use all ports and down remap the array position.
return pcintPort;
}
else if (PCINT_NUM_PORTS - PCINT_NUM_USED_PORTS == 1) {
// one port is not used
if (PCINT_USE_PORT0 == 0) {
// first port is not used, decrease all port numbers
return (pcintPort - 1);
}
else if (PCINT_HAS_PORT3 == 0) {
// 3 ports (standard)
if (PCINT_USE_PORT2 == 0) {
// last port not used, no mapping needed
return pcintPort;
}
else {
// worst case, port in the middle not used, remap
return ((pcintPort >> 1) & 0x01);
//if (pcintPort == 0) return 0;
//else return 1;
}
}
else {
// 4 ports (special case for a few AVRs)
if (PCINT_USE_PORT3 == 0) {
// last port not used, no mapping needed
return pcintPort;
}
else {
// worst case, one of two ports in the middle not used, remap
if (PCINT_USE_PORT1 == 0) {
// port1 not used, mapping needed
if (pcintPort == 0)
return 0;
else
return pcintPort - 1;
}
else if (PCINT_USE_PORT2 == 0) {
// port2 not used, mapping needed
if (pcintPort == 3)
return 2;
else
return pcintPort;
}
}
}
// use all ports and down remap the array position.
return pcintPort;
}
else if (PCINT_NUM_PORTS - PCINT_NUM_USED_PORTS == 2) {
if (PCINT_USE_PORT2 == 0 && PCINT_USE_PORT3 == 0) {
// no need for mapping
return pcintPort;
}
else if (PCINT_USE_PORT0 == 0 && PCINT_USE_PORT3 == 0) {
// 1 offset
return (pcintPort - 1);
}
else if (PCINT_USE_PORT0 == 0 && PCINT_USE_PORT1 == 0) {
// 2 offset
return (pcintPort - 2);
}
else if (PCINT_USE_PORT0 == 0 && PCINT_USE_PORT2 == 0) {
// 2 -> 1
return (pcintPort >> 1);
}
else if (PCINT_USE_PORT1 == 0 && PCINT_USE_PORT2 == 0) {
// 3 -> 1
return (pcintPort >> 1);
}
else if (PCINT_USE_PORT1 == 0 && PCINT_USE_PORT3 == 0) {
// 3 -> 1, 1 -> 0
return (pcintPort >> 1);
}
}
// error
return 0;
}
//================================================================================
// Attach Function (partly inlined)
//================================================================================
void enablePinChangeInterruptHelper(const uint8_t pcintPort, const uint8_t pcintMask, const uint8_t arrayPos);
void attachPinChangeInterrupt0(void);
void attachPinChangeInterrupt1(void);
void attachPinChangeInterrupt2(void);
void attachPinChangeInterrupt3(void);
#if defined(PCINT_API)
/*
for (int i = 0; i < 32; i++) {
Serial.print("#if (PCINT_USE_PCINT");
Serial.print(i);
Serial.println(" == true)");
Serial.print("extern volatile callback callbackPCINT");
Serial.print(i);
Serial.println(";");
Serial.println("#endif");
}
*/
#if (PCINT_USE_PCINT0 == true)
extern volatile callback callbackPCINT0;
#endif
#if (PCINT_USE_PCINT1 == true)
extern volatile callback callbackPCINT1;
#endif
#if (PCINT_USE_PCINT2 == true)
extern volatile callback callbackPCINT2;
#endif
#if (PCINT_USE_PCINT3 == true)
extern volatile callback callbackPCINT3;
#endif
#if (PCINT_USE_PCINT4 == true)
extern volatile callback callbackPCINT4;
#endif
#if (PCINT_USE_PCINT5 == true)
extern volatile callback callbackPCINT5;
#endif
#if (PCINT_USE_PCINT6 == true)
extern volatile callback callbackPCINT6;
#endif
#if (PCINT_USE_PCINT7 == true)
extern volatile callback callbackPCINT7;
#endif
#if (PCINT_USE_PCINT8 == true)
extern volatile callback callbackPCINT8;
#endif
#if (PCINT_USE_PCINT9 == true)
extern volatile callback callbackPCINT9;
#endif
#if (PCINT_USE_PCINT10 == true)
extern volatile callback callbackPCINT10;
#endif
#if (PCINT_USE_PCINT11 == true)
extern volatile callback callbackPCINT11;
#endif
#if (PCINT_USE_PCINT12 == true)
extern volatile callback callbackPCINT12;
#endif
#if (PCINT_USE_PCINT13 == true)
extern volatile callback callbackPCINT13;
#endif
#if (PCINT_USE_PCINT14 == true)
extern volatile callback callbackPCINT14;
#endif
#if (PCINT_USE_PCINT15 == true)
extern volatile callback callbackPCINT15;
#endif
#if (PCINT_USE_PCINT16 == true)
extern volatile callback callbackPCINT16;
#endif
#if (PCINT_USE_PCINT17 == true)
extern volatile callback callbackPCINT17;
#endif
#if (PCINT_USE_PCINT18 == true)
extern volatile callback callbackPCINT18;
#endif
#if (PCINT_USE_PCINT19 == true)
extern volatile callback callbackPCINT19;
#endif
#if (PCINT_USE_PCINT20 == true)
extern volatile callback callbackPCINT20;
#endif
#if (PCINT_USE_PCINT21 == true)
extern volatile callback callbackPCINT21;
#endif
#if (PCINT_USE_PCINT22 == true)
extern volatile callback callbackPCINT22;
#endif
#if (PCINT_USE_PCINT23 == true)
extern volatile callback callbackPCINT23;
#endif
#if (PCINT_USE_PCINT24 == true)
extern volatile callback callbackPCINT24;
#endif
#if (PCINT_USE_PCINT25 == true)
extern volatile callback callbackPCINT25;
#endif
#if (PCINT_USE_PCINT26 == true)
extern volatile callback callbackPCINT26;
#endif
#if (PCINT_USE_PCINT27 == true)
extern volatile callback callbackPCINT27;
#endif
#if (PCINT_USE_PCINT28 == true)
extern volatile callback callbackPCINT28;
#endif
#if (PCINT_USE_PCINT29 == true)
extern volatile callback callbackPCINT29;
#endif
#if (PCINT_USE_PCINT30 == true)
extern volatile callback callbackPCINT30;
#endif
#if (PCINT_USE_PCINT31 == true)
extern volatile callback callbackPCINT31;
#endif
/*
for (int i = 0; i < 32; i++) {
Serial.print("#if (PCINT_USE_PCINT");
Serial.print(i);
Serial.println(" == true)");
Serial.print("if (pcintNum == ");
Serial.print(i);
Serial.println(")");
Serial.print("callbackPCINT");
Serial.print(i);
Serial.println(" = userFunc;");
Serial.println("#endif");
}
*/
// API attach function
static inline void attachPinChangeInterrupt(const uint8_t pcintNum, void(*userFunc)(void), const uint8_t mode) __attribute__((always_inline));
void attachPinChangeInterrupt(const uint8_t pcintNum, void(*userFunc)(void), const uint8_t mode) {
#else // no API attach function
static inline void attachPinChangeInterrupt(const uint8_t pcintNum, const uint8_t mode) __attribute__((always_inline));
void attachPinChangeInterrupt(const uint8_t pcintNum, const uint8_t mode) {
#endif // PCINT_API
// check if pcint is a valid pcint, exclude deactivated ports
uint8_t pcintPort = pcintNum / 8;
uint8_t pcintBit = pcintNum % 8;
// port 0
if (pcintPort == 0 && PCINT_USE_PORT0 == true) {
// use fake functions to make the ISRs compile with .a linkage
#if defined(PCINT_ALINKAGE) && !defined(PCINT_COMPILE_ENABLED_ISR)
attachPinChangeInterrupt0();
#endif
// attache the function pointers for the API
#if defined(PCINT_API)
#if (PCINT_USE_PCINT0 == true)
if (pcintNum == 0)
callbackPCINT0 = userFunc;
#endif
#if (PCINT_USE_PCINT1 == true)
if (pcintNum == 1)
callbackPCINT1 = userFunc;
#endif
#if (PCINT_USE_PCINT2 == true)
if (pcintNum == 2)
callbackPCINT2 = userFunc;
#endif
#if (PCINT_USE_PCINT3 == true)
if (pcintNum == 3)
callbackPCINT3 = userFunc;
#endif
#if (PCINT_USE_PCINT4 == true)
if (pcintNum == 4)
callbackPCINT4 = userFunc;
#endif
#if (PCINT_USE_PCINT5 == true)
if (pcintNum == 5)
callbackPCINT5 = userFunc;
#endif
#if (PCINT_USE_PCINT6 == true)
if (pcintNum == 6)
callbackPCINT6 = userFunc;
#endif
#if (PCINT_USE_PCINT7 == true)
if (pcintNum == 7)
callbackPCINT7 = userFunc;
#endif
#endif // PCINT_API
}
// port 1
else if (pcintPort == 1 && PCINT_USE_PORT1 == true) {
// use fake functions to make the ISRs compile with .a linkage
#if defined(PCINT_ALINKAGE) && !defined(PCINT_COMPILE_ENABLED_ISR)
attachPinChangeInterrupt1();
#endif
// attache the function pointers for the API
#if defined(PCINT_API)
#if (PCINT_USE_PCINT8 == true)
if (pcintNum == 8)
callbackPCINT8 = userFunc;
#endif
#if (PCINT_USE_PCINT9 == true)
if (pcintNum == 9)
callbackPCINT9 = userFunc;
#endif
#if (PCINT_USE_PCINT10 == true)
if (pcintNum == 10)
callbackPCINT10 = userFunc;
#endif
#if (PCINT_USE_PCINT11 == true)
if (pcintNum == 11)
callbackPCINT11 = userFunc;
#endif
#if (PCINT_USE_PCINT12 == true)
if (pcintNum == 12)
callbackPCINT12 = userFunc;
#endif
#if (PCINT_USE_PCINT13 == true)
if (pcintNum == 13)
callbackPCINT13 = userFunc;
#endif
#if (PCINT_USE_PCINT14 == true)
if (pcintNum == 14)
callbackPCINT14 = userFunc;
#endif
#if (PCINT_USE_PCINT15 == true)
if (pcintNum == 15)
callbackPCINT15 = userFunc;
#endif
#endif // PCINT_API
}
// port 2
else if (pcintPort == 2 && PCINT_USE_PORT2 == true) {
// use fake functions to make the ISRs compile with .a linkage
#if defined(PCINT_ALINKAGE) && !defined(PCINT_COMPILE_ENABLED_ISR)
attachPinChangeInterrupt2();
#endif
// attache the function pointers for the API
#if defined(PCINT_API)
#if (PCINT_USE_PCINT16 == true)
if (pcintNum == 16)
callbackPCINT16 = userFunc;
#endif
#if (PCINT_USE_PCINT17 == true)
if (pcintNum == 17)
callbackPCINT17 = userFunc;
#endif
#if (PCINT_USE_PCINT18 == true)
if (pcintNum == 18)
callbackPCINT18 = userFunc;
#endif
#if (PCINT_USE_PCINT19 == true)
if (pcintNum == 19)
callbackPCINT19 = userFunc;
#endif
#if (PCINT_USE_PCINT20 == true)
if (pcintNum == 20)
callbackPCINT20 = userFunc;
#endif
#if (PCINT_USE_PCINT21 == true)
if (pcintNum == 21)
callbackPCINT21 = userFunc;
#endif
#if (PCINT_USE_PCINT22 == true)
if (pcintNum == 22)
callbackPCINT22 = userFunc;
#endif
#if (PCINT_USE_PCINT23 == true)
if (pcintNum == 23)
callbackPCINT23 = userFunc;
#endif
#endif // PCINT_API
}
// port 3
else if (pcintPort == 3 && PCINT_USE_PORT3 == true) {
// use fake functions to make the ISRs compile with .a linkage
#if defined(PCINT_ALINKAGE) && !defined(PCINT_COMPILE_ENABLED_ISR)
attachPinChangeInterrupt3();
#endif
// attache the function pointers for the API
#if defined(PCINT_API)
#if (PCINT_USE_PCINT24 == true)
if (pcintNum == 24)
callbackPCINT24 = userFunc;
#endif
#if (PCINT_USE_PCINT25 == true)
if (pcintNum == 25)
callbackPCINT25 = userFunc;
#endif
#if (PCINT_USE_PCINT26 == true)
if (pcintNum == 26)
callbackPCINT26 = userFunc;
#endif
#if (PCINT_USE_PCINT27 == true)
if (pcintNum == 27)
callbackPCINT27 = userFunc;
#endif
#if (PCINT_USE_PCINT28 == true)
if (pcintNum == 28)
callbackPCINT28 = userFunc;
#endif
#if (PCINT_USE_PCINT29 == true)
if (pcintNum == 29)
callbackPCINT29 = userFunc;
#endif
#if (PCINT_USE_PCINT30 == true)
if (pcintNum == 30)
callbackPCINT30 = userFunc;
#endif
#if (PCINT_USE_PCINT31 == true)
if (pcintNum == 31)
callbackPCINT31 = userFunc;
#endif
#endif // PCINT_API
}
else return;
// get bitmask and array position
uint8_t pcintMask = (1 << pcintBit);
uint8_t arrayPos = getArrayPosPCINT(pcintPort);
// save settings related to mode and registers
if (mode == CHANGE || mode == RISING)
risingPorts[arrayPos] |= pcintMask;
if (mode == CHANGE || mode == FALLING)
fallingPorts[arrayPos] |= pcintMask;
// call the actual hardware attach function
enablePinChangeInterruptHelper(pcintPort, pcintMask, arrayPos);
}
// enable interrupt again if temporary disabled
static inline void enablePinChangeInterrupt(const uint8_t pcintNum) __attribute__((always_inline));
void enablePinChangeInterrupt(const uint8_t pcintNum) {
// get PCINT registers
uint8_t pcintPort = pcintNum / 8;
uint8_t pcintBit = pcintNum % 8;
// check if pcint is a valid pcint, exclude deactivated ports
if (pcintPort == 0) {
if (PCINT_USE_PORT0 == false)
return;
}
else if (pcintPort == 1) {
if (PCINT_USE_PORT1 == false)
return;
}
else if (pcintPort == 2) {
if (PCINT_USE_PORT2 == false)
return;
}
else if (pcintPort == 3) {
if (PCINT_USE_PORT3 == false)
return;
}
else return;
// call the actual hardware attach function
uint8_t pcintMask = (1 << pcintBit);
uint8_t arrayPos = getArrayPosPCINT(pcintPort);
enablePinChangeInterruptHelper(pcintPort, pcintMask, arrayPos);
}
//================================================================================
// Detach Function (partly inlined)
//================================================================================
void disablePinChangeInterruptHelper(const uint8_t pcintPort, const uint8_t pcintMask);
static inline void detachPinChangeInterrupt(const uint8_t pcintNum) __attribute__((always_inline));
void detachPinChangeInterrupt(const uint8_t pcintNum) {
// get PCINT registers
uint8_t pcintPort = pcintNum / 8;
uint8_t pcintBit = pcintNum % 8;
// check if pcint is a valid pcint, exclude deactivated ports
// port 0
if (pcintPort == 0 && PCINT_USE_PORT0 == true) {
// attache the function pointers for the API
#if defined(PCINT_API)
#if (PCINT_USE_PCINT0 == true)
if (pcintNum == 0)
callbackPCINT0 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT1 == true)
if (pcintNum == 1)
callbackPCINT1 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT2 == true)
if (pcintNum == 2)
callbackPCINT2 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT3 == true)
if (pcintNum == 3)
callbackPCINT3 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT4 == true)
if (pcintNum == 4)
callbackPCINT4 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT5 == true)
if (pcintNum == 5)
callbackPCINT5 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT6 == true)
if (pcintNum == 6)
callbackPCINT6 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT7 == true)
if (pcintNum == 7)
callbackPCINT7 = pcint_null_callback;
#endif
#endif // PCINT_API
}
// port 1
else if (pcintPort == 1 && PCINT_USE_PORT1 == true) {
// attache the function pointers for the API
#if defined(PCINT_API)
#if (PCINT_USE_PCINT8 == true)
if (pcintNum == 8)
callbackPCINT8 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT9 == true)
if (pcintNum == 9)
callbackPCINT9 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT10 == true)
if (pcintNum == 10)
callbackPCINT10 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT11 == true)
if (pcintNum == 11)
callbackPCINT11 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT12 == true)
if (pcintNum == 12)
callbackPCINT12 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT13 == true)
if (pcintNum == 13)
callbackPCINT13 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT14 == true)
if (pcintNum == 14)
callbackPCINT14 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT15 == true)
if (pcintNum == 15)
callbackPCINT15 = pcint_null_callback;
#endif
#endif // PCINT_API
}
// port 2
else if (pcintPort == 2 && PCINT_USE_PORT2 == true) {
// attache the function pointers for the API
#if defined(PCINT_API)
#if (PCINT_USE_PCINT16 == true)
if (pcintNum == 16)
callbackPCINT16 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT17 == true)
if (pcintNum == 17)
callbackPCINT17 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT18 == true)
if (pcintNum == 18)
callbackPCINT18 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT19 == true)
if (pcintNum == 19)
callbackPCINT19 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT20 == true)
if (pcintNum == 20)
callbackPCINT20 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT21 == true)
if (pcintNum == 21)
callbackPCINT21 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT22 == true)
if (pcintNum == 22)
callbackPCINT22 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT23 == true)
if (pcintNum == 23)
callbackPCINT23 = pcint_null_callback;
#endif
#endif // PCINT_API
}
// port 3
else if (pcintPort == 3 && PCINT_USE_PORT3 == true) {
// attache the function pointers for the API
#if defined(PCINT_API)
#if (PCINT_USE_PCINT24 == true)
if (pcintNum == 24)
callbackPCINT24 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT25 == true)
if (pcintNum == 25)
callbackPCINT25 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT26 == true)
if (pcintNum == 26)
callbackPCINT26 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT27 == true)
if (pcintNum == 27)
callbackPCINT27 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT28 == true)
if (pcintNum == 28)
callbackPCINT28 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT29 == true)
if (pcintNum == 29)
callbackPCINT29 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT30 == true)
if (pcintNum == 30)
callbackPCINT30 = pcint_null_callback;
#endif
#if (PCINT_USE_PCINT31 == true)
if (pcintNum == 31)
callbackPCINT31 = pcint_null_callback;
#endif
#endif // PCINT_API
}
else return;
// get bitmask and array position
uint8_t pcintMask = (1 << pcintBit);
uint8_t arrayPos = getArrayPosPCINT(pcintPort);
// delete setting
risingPorts[arrayPos] &= ~pcintMask;
fallingPorts[arrayPos] &= ~pcintMask;
// call the actual hardware disable function
disablePinChangeInterruptHelper(pcintPort, pcintMask);
}
static inline void disablePinChangeInterrupt(const uint8_t pcintNum) __attribute__((always_inline));
void disablePinChangeInterrupt(const uint8_t pcintNum) {
// get PCINT registers
uint8_t pcintPort = pcintNum / 8;
uint8_t pcintBit = pcintNum % 8;
// check if pcint is a valid pcint, exclude deactivated ports
if (pcintPort == 0) {
if (PCINT_USE_PORT0 == false)
return;
}
else if (pcintPort == 1) {
if (PCINT_USE_PORT1 == false)
return;
}
else if (pcintPort == 2) {
if (PCINT_USE_PORT2 == false)
return;
}
else if (pcintPort == 3) {
if (PCINT_USE_PORT3 == false)
return;
}
else return;
// get bitmask
uint8_t pcintMask = (1 << pcintBit);
// Do not delete mode settings nor detach the user function
// Just turn off interrupts
// call the actual hardware disable function
disablePinChangeInterruptHelper(pcintPort, pcintMask);
}
//================================================================================
// getTrigger Function (inlined)
//================================================================================
static inline uint8_t getPinChangeInterruptTrigger(const uint8_t pcintNum) __attribute__((always_inline));
uint8_t getPinChangeInterruptTrigger(const uint8_t pcintNum) {
// get PCINT registers
uint8_t pcintPort = pcintNum / 8;
uint8_t pcintBit = pcintNum % 8;
// check if pcint is a valid pcint, exclude deactivated ports
if (pcintPort == 0) {
if (PCINT_USE_PORT0 == false)
return CHANGE;
}
else if (pcintPort == 1) {
if (PCINT_USE_PORT1 == false)
return CHANGE;
}
else if (pcintPort == 2) {
if (PCINT_USE_PORT2 == false)
return CHANGE;
}
else if (pcintPort == 3) {
if (PCINT_USE_PORT3 == false)
return CHANGE;
}
else return CHANGE;
uint8_t arrayPos = getArrayPosPCINT(pcintPort);
// Check if no mode was set, return an error
if(!(risingPorts[arrayPos] & (1 << pcintBit)) && !(fallingPorts[arrayPos] & (1 << pcintBit)))
return CHANGE;
// specify the CHANGE mode
if (oldPorts[arrayPos] & (1 << pcintBit))
return RISING;
else
return FALLING;
}