-
Notifications
You must be signed in to change notification settings - Fork 73
/
EnableInterrupt.h
2073 lines (1880 loc) · 67.6 KB
/
EnableInterrupt.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
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
// in vim, :set ts=2 sts=2 sw=2 et fdm=marker
// EnableInterrupt, a library by GreyGnome. Copyright 2014-2015 by Michael Anthony Schwager.
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Many definitions in /usr/avr/include/avr/io.h
#ifndef EnableInterrupt_h
#define EnableInterrupt_h
#include <Arduino.h>
// Function Prototypes (comment) /*{{{*/
/* *************************************************************************************
// *************************************************************************************
// Function Prototypes *****************************************************************
// *************************************************************************************
// *************************************************************************************
// *** These are the only functions the end user (programmer) needs to consider. ***
// *** This means you! ***
// *************************************************************************************
*//*}}}*/
// Arduino Due (not Duemilanove) and Zero macros. Easy-peasy.
// Zero uses the __SAMD21G18A__ processor macro (2015-10-13) but we will use this handy macro, to
// avoid breaking the library over tiny changes.
#if defined __SAM3U4E__ || defined __SAM3X8E__ || defined __SAM3X8H__ || defined ARDUINO_SAMD_ZERO || defined __SAMD21G18A__ || defined __SAMD21J18A__ /*{{{*/
#ifdef NEEDFORSPEED
#error Due and Zero are already fast; the NEEDFORSPEED definition does not make sense on it.
#endif
#define enableInterrupt(pin,userFunc,mode) attachInterrupt(pin, userFunc,mode)
#define disableInterrupt(pin) detachInterrupt(pin)/*}}}*/
#else
// Usage: (comment) /*{{{*/
/* enableInterrupt- Sets up an interrupt on a selected Arduino pin.
* or
* enableInterruptFast- When used with the NEEDFORSPEED macro, sets up an interrupt on a selected Arduino pin.
*
* Usage:
* enableInterrupt(uint8_t pinNumber, void (*userFunction)(void), uint8_t mode);
* or
* enableInterrupt(uint8_t interruptDesignator, void (*userFunction)(void), uint8_t mode);
*
* For HiSpeed mode,
* enableInterruptFast(uint8_t pinNumber, uint8_t mode);
* or
* enableInterruptFast(uint8_t interruptDesignator, uint8_t mode);
*
* ---------------------------------------------------------------------------------------
*
* disableInterrupt- Disables interrupt on a selected Arduino pin.
*
* Usage:
*
* disableInterrupt(uint8_t pinNumber);
* or
* disableInterrupt(uint8_t interruptDesignator);
*
* ---------------------------------------------------------------------------------------
*
* interruptDesignator: Essentially this is an Arduino pin, and if that's all you want to give
* the function, it will work just fine. Why is it called an "interruptDesignator", then? Because
* there's a twist: You can perform a bitwise "and" with the pin number and PINCHANGEINTERRUPT
* to specify that you want to use a Pin Change Interrupt type of interrupt on those pins that
* support both Pin Change and External Interrupts. Otherwise, the library will choose whatever
* interrupt type (External, or Pin Change) normally applies to that pin, with priority to
* External Interrupt.
*
* The interruptDesignator is required because on the ATmega328 processor pins 2 and 3 support
* ''either'' pin change or * external interrupts. On 644/1284-based systems, pin change interrupts
* are supported on all pins and external interrupts are supported on pins 2, 10, and 11.
* Otherwise, each pin only supports a single type of interrupt and the
* PINCHANGEINTERRUPT scheme changes nothing. This means you can ignore this whole discussion
* for ATmega2560- or ATmega32U4-based Arduinos. You can probably safely ignore it for
* ATmega328-based Arduinos, too.
*//*}}}*/
void enableInterrupt(uint8_t interruptDesignator, void (*userFunction)(void), uint8_t mode);
void disableInterrupt(uint8_t interruptDesignator);
void bogusFunctionPlaceholder(void);
#ifdef NEEDFORSPEED
#undef enableInterruptFast
// enableInterruptFast(uint8_t interruptDesignator, uint8_t mode);
#define enableInterruptFast(x, y) enableInterrupt(x, bogusFunctionPlaceholder, y)
#endif
// *************************************************************************************
// End Function Prototypes *************************************************************
// *************************************************************************************
#undef PINCHANGEINTERRUPT
#define PINCHANGEINTERRUPT 0x80
#undef attachPinChangeInterrupt
#undef detachPinChangeInterrupt
#define detachPinChangeInterrupt(pin) disableInterrupt(pin)
#define attachPinChangeInterrupt(pin,userFunc,mode) enableInterrupt(pin, userFunc, mode)
#ifdef LIBCALL_ENABLEINTERRUPT // LIBCALL_ENABLEINTERRUPT ****************************************
#ifdef EI_ARDUINO_INTERRUPTED_PIN
extern volatile uint8_t arduinoInterruptedPin;
extern volatile uint8_t arduinoPinState;
#endif
#else
#ifdef EI_ARDUINO_INTERRUPTED_PIN
volatile uint8_t arduinoInterruptedPin=0;
volatile uint8_t arduinoPinState=0;
#endif
#ifdef NEEDFORSPEED
void bogusFunctionPlaceholder(void) {
}
#include "utility/ei_pindefs_speed.h"
#endif
/* Arduino pin to ATmega port translaton is found doing digital_pin_to_port_PGM[] */
/* Arduino pin to PCMSKx bitmask is found by doing digital_pin_to_bit_mask_PGM[] */
/* ...except for PortJ, which is shifted left 1 bit in PCI1 */
volatile uint8_t *pcmsk;
// Arduino.h has these, but the block is surrounded by #ifdef ARDUINO_MAIN
#define PA 1
#define PB 2
#define PC 3
#define PD 4
#define PE 5
#define PF 6
#define PG 7
#define PH 8
#define PJ 10
#define PK 11
#define PL 12
typedef void (*interruptFunctionType)(void);
// ===========================================================================================
// CHIP SPECIFIC DATA STRUCTURES =============================================================
// ===========================================================================================
/* UNO SERIES *************************************************************************/
/* UNO SERIES *************************************************************************/
/* UNO SERIES *************************************************************************/
#if defined __AVR_ATmega168__ || defined __AVR_ATmega168A__ || defined __AVR_ATmega168P__ || \
defined __AVR_ATmega168PA__ || defined __AVR_ATmega328__ || defined __AVR_ATmega328P__
#define ARDUINO_328 /*{{{*/
#define EI_NOTPORTA
#define EI_NOTPORTJ
#define EI_NOTPORTK /*}}}*/
#if defined EI_NOTPINCHANGE /*{{{*/
#ifndef EI_NOTPORTB
#define EI_NOTPORTB
#endif
#ifndef EI_NOTPORTC
#define EI_NOTPORTC
#endif
#ifndef EI_NOTPORTD
#define EI_NOTPORTD
#endif
#endif // defined EI_NOTPINCHANGE
#ifndef NEEDFORSPEED
#define ARDUINO_PIN_B0 8
#define ARDUINO_PIN_B1 9
#define ARDUINO_PIN_B2 10
#define ARDUINO_PIN_B3 11
#define ARDUINO_PIN_B4 12
#define ARDUINO_PIN_B5 13
#define ARDUINO_PIN_C0 14
#define ARDUINO_PIN_C1 15
#define ARDUINO_PIN_C2 16
#define ARDUINO_PIN_C3 17
#define ARDUINO_PIN_C4 18
#define ARDUINO_PIN_C5 19
#define ARDUINO_PIN_D0 0
#define ARDUINO_PIN_D1 1
#define ARDUINO_PIN_D2 2
#define ARDUINO_PIN_D3 3
#define ARDUINO_PIN_D4 4
#define ARDUINO_PIN_D5 5
#define ARDUINO_PIN_D6 6
#define ARDUINO_PIN_D7 7
const uint8_t PROGMEM digital_pin_to_port_bit_number_PGM[] = {
0, // 0 == port D, 0
1,
2,
3,
4,
5,
6,
7,
0, // 8 == port B, 0
1,
2,
3,
4,
5,
0, // 14 == port C, 0
1,
2,
3,
4,
5
};
#if ! defined(EI_NOTEXTERNAL) && ! defined(EI_NOTINT0) && ! defined (EI_NOTINT1)
interruptFunctionType functionPointerArrayEXTERNAL[2];
#endif
#ifndef EI_NOTPORTB
// 2 of the interrupts are unsupported on Arduino UNO.
struct functionPointersPortB {
interruptFunctionType pinZero;
interruptFunctionType pinOne;
interruptFunctionType pinTwo;
interruptFunctionType pinThree;
interruptFunctionType pinFour;
interruptFunctionType pinFive;
};
typedef struct functionPointersPortB functionPointersPortB;
functionPointersPortB portBFunctions = { NULL, NULL, NULL, NULL, NULL, NULL };
#endif // EI_NOTPORTB
#ifndef EI_NOTPORTC
// 1 of the interrupts are used as RESET on Arduino UNO.
struct functionPointersPortC {
interruptFunctionType pinZero;
interruptFunctionType pinOne;
interruptFunctionType pinTwo;
interruptFunctionType pinThree;
interruptFunctionType pinFour;
interruptFunctionType pinFive;
};
typedef struct functionPointersPortC functionPointersPortC;
functionPointersPortC portCFunctions = { NULL, NULL, NULL, NULL, NULL, NULL };
#endif // EI_NOTPORTC
#ifndef EI_NOTPORTD
// 1 of the interrupts are used as RESET on Arduino UNO.
struct functionPointersPortD {
interruptFunctionType pinZero;
interruptFunctionType pinOne;
interruptFunctionType pinTwo;
interruptFunctionType pinThree;
interruptFunctionType pinFour;
interruptFunctionType pinFive;
interruptFunctionType pinSix;
interruptFunctionType pinSeven;
};
typedef struct functionPointersPortD functionPointersPortD;
functionPointersPortD portDFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif // EI_NOTPORTD
#endif // NEEDFORSPEED
// For Pin Change Interrupts; since we're duplicating FALLING and RISING in software,
// we have to know how the ports were defined.
#ifndef EI_NOTPORTB
volatile uint8_t risingPinsPORTB=0;
volatile uint8_t fallingPinsPORTB=0;
// for the saved state of the ports
static volatile uint8_t portSnapshotB;
#endif
#ifndef EI_NOTPORTC
volatile uint8_t risingPinsPORTC=0;
volatile uint8_t fallingPinsPORTC=0;
static volatile uint8_t portSnapshotC;
#endif
#ifndef EI_NOTPORTD
volatile uint8_t risingPinsPORTD=0;
volatile uint8_t fallingPinsPORTD=0;
static volatile uint8_t portSnapshotD;
#endif
// the PCINT?_vect's are defined in the avr.h files, like iom328p.h
#define PORTB_VECT PCINT0_vect
#define PORTC_VECT PCINT1_vect
#define PORTD_VECT PCINT2_vect/*}}}*/
/* MEGA2560/1280/640 SERIES ************************************************************************/
/* MEGA2560/1280/640 SERIES ************************************************************************/
/* MEGA2560/1280/640 SERIES ************************************************************************/
#elif defined __AVR_ATmega640__ || defined __AVR_ATmega2560__ || defined __AVR_ATmega1280__
#define ARDUINO_MEGA /*{{{*/
#define EI_NOTPORTA
#define EI_NOTPORTC
#define EI_NOTPORTD /*}}}*/
#if defined EI_NOTPINCHANGE/*{{{*/
#ifndef EI_NOTPORTB
#define EI_NOTPORTB
#endif
#ifndef EI_NOTPORTJ
#define EI_NOTPORTJ
#endif
#ifndef EI_NOTPORTK
#define EI_NOTPORTK
#endif
#endif
volatile uint8_t portJPCMSK=0; // This is a shifted version of PCMSK for PortJ, so I
// don't have to perform a shift in the IRQ.
#ifndef NEEDFORSPEED
// Pin change interrupts
#define ARDUINO_PIN_B0 53
#define ARDUINO_PIN_B1 52
#define ARDUINO_PIN_B2 51
#define ARDUINO_PIN_B3 50
#define ARDUINO_PIN_B4 10
#define ARDUINO_PIN_B5 11
#define ARDUINO_PIN_B6 12
#define ARDUINO_PIN_B7 13
#define ARDUINO_PIN_J0 15
#define ARDUINO_PIN_J1 14
// "fake" pins
#define ARDUINO_PIN_J2 70
#define ARDUINO_PIN_J3 71
#define ARDUINO_PIN_J4 72
#define ARDUINO_PIN_J5 73
#define ARDUINO_PIN_J6 74
#define ARDUINO_PIN_K0 62
#define ARDUINO_PIN_K1 63
#define ARDUINO_PIN_K2 64
#define ARDUINO_PIN_K3 65
#define ARDUINO_PIN_K4 66
#define ARDUINO_PIN_K5 67
#define ARDUINO_PIN_K6 68
#define ARDUINO_PIN_K7 69
#define ARDUINO_PIN_D0 21
#define ARDUINO_PIN_D1 20
#define ARDUINO_PIN_D2 19
#define ARDUINO_PIN_D3 18
#define ARDUINO_PIN_E4 2
#define ARDUINO_PIN_E5 3
#define ARDUINO_PIN_E6 75
#define ARDUINO_PIN_E7 76
const uint8_t PROGMEM digital_pin_to_port_bit_number_PGM[] = {
0, // PE0 pin: 0
1, // PE1 pin: 1
4, // PE4 pin: 2
5, // PE5 pin: 3
5, // PG5 pin: 4
3, // PE3 pin: 5
3, // PH3 pin: 6
4, // PH4 pin: 7
5, // PH5 pin: 8
6, // PH6 pin: 9
4, // PB4 pin: 10
5, // PB5 pin: 11
6, // PB6 pin: 12
7, // PB7 pin: 13
1, // PJ1 pin: 14
0, // PJ0 pin: 15
1, // PH1 pin: 16
0, // PH0 pin: 17
3, // PD3 pin: 18
2, // PD2 pin: 19
1, // PD1 pin: 20
0, // PD0 pin: 21
0, // PA0 pin: 22
1, // PA1 pin: 23
2, // PA2 pin: 24
3, // PA3 pin: 25
4, // PA4 pin: 26
5, // PA5 pin: 27
6, // PA6 pin: 28
7, // PA7 pin: 29
7, // PC7 pin: 30
6, // PC6 pin: 31
5, // PC5 pin: 32
4, // PC4 pin: 33
3, // PC3 pin: 34
2, // PC2 pin: 35
1, // PC1 pin: 36
0, // PC0 pin: 37
7, // PD7 pin: 38
2, // PG2 pin: 39
1, // PG1 pin: 40
0, // PG0 pin: 41
7, // PL7 pin: 42
6, // PL6 pin: 43
5, // PL5 pin: 44
4, // PL4 pin: 45
3, // PL3 pin: 46
2, // PL2 pin: 47
1, // PL1 pin: 48
0, // PL0 pin: 49
3, // PB3 pin: 50
2, // PB2 pin: 51
1, // PB1 pin: 52
0, // PB0 pin: 53
0, // PF0 pin: 54
1, // PF1 pin: 55
2, // PF2 pin: 56
3, // PF3 pin: 57
4, // PF4 pin: 58
5, // PF5 pin: 59
6, // PF6 pin: 60
7, // PF7 pin: 61
0, // PK0 pin: 62
1, // PK1 pin: 63
2, // PK2 pin: 64
3, // PK3 pin: 65
4, // PK4 pin: 66
5, // PK5 pin: 67
6, // PK6 pin: 68
7, // PK7 pin: 69
2, // PJ2 pin: fake70, trick to allow software interrupts on Port J. PJ2
3, // PJ3 pin: fake71 PJ3
4, // PJ4 pin: fake72 PJ4
5, // PJ5 pin: fake73 PJ5
6, // PJ6 pin: fake74 PJ6
6, // PE6 pin: fake75 PE6
7 // PE7 pin: fake76 PE7
};
#if ! defined(EI_NOTEXTERNAL) && ! defined(EI_NOTINT0) && ! defined(EI_NOTINT1) && ! defined(EI_NOTINT2) && ! defined(EI_NOTINT3) && ! defined(EI_NOTINT4) && ! defined(EI_NOTINT5) && ! defined(EI_NOTINT6) && ! defined(EI_NOTINT7)
interruptFunctionType functionPointerArrayEXTERNAL[8];
#endif
#ifndef EI_NOTPORTB
struct functionPointersPortB {
interruptFunctionType pinZero;
interruptFunctionType pinOne;
interruptFunctionType pinTwo;
interruptFunctionType pinThree;
interruptFunctionType pinFour;
interruptFunctionType pinFive;
interruptFunctionType pinSix;
interruptFunctionType pinSeven;
};
typedef struct functionPointersPortB functionPointersPortB;
functionPointersPortB portBFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif
#ifndef EI_NOTPORTJ
// only 7 pins total of port J are supported as interrupts on the ATmega2560,
// and only PJ0 and 1 are supported on the Arduino MEGA.
// For PCI1 the 0th bit is PE0. PJ2-6 are not exposed on the Arduino pins, but
// we will support them anyway. There are clones that provide them, and users may
// solder in their own connections (...go, Makers!)
struct functionPointersPortJ {
interruptFunctionType pinZero;
interruptFunctionType pinOne;
interruptFunctionType pinTwo;
interruptFunctionType pinThree;
interruptFunctionType pinFour;
interruptFunctionType pinFive;
interruptFunctionType pinSix;
};
typedef struct functionPointersPortJ functionPointersPortJ;
functionPointersPortJ portJFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif
#ifndef EI_NOTPORTK
struct functionPointersPortK {
interruptFunctionType pinZero;
interruptFunctionType pinOne;
interruptFunctionType pinTwo;
interruptFunctionType pinThree;
interruptFunctionType pinFour;
interruptFunctionType pinFive;
interruptFunctionType pinSix;
interruptFunctionType pinSeven;
};
typedef struct functionPointersPortK functionPointersPortK;
functionPointersPortK portKFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif
#endif // ifndef NEEDFORSPEED
// For Pin Change Interrupts; since we're duplicating FALLING and RISING in software,
// we have to know how we were defined.
#ifndef EI_NOTPORTB
volatile uint8_t risingPinsPORTB=0;
volatile uint8_t fallingPinsPORTB=0;
static volatile uint8_t portSnapshotB;
#endif
#ifndef EI_NOTPORTJ
volatile uint8_t risingPinsPORTJ=0;
volatile uint8_t fallingPinsPORTJ=0;
static volatile uint8_t portSnapshotJ;
#endif
#ifndef EI_NOTPORTK
volatile uint8_t risingPinsPORTK=0;
volatile uint8_t fallingPinsPORTK=0;
static volatile uint8_t portSnapshotK;
#endif
#define PORTB_VECT PCINT0_vect
#define PORTJ_VECT PCINT1_vect
#define PORTK_VECT PCINT2_vect
/*}}}*/
/* MEGA2561/1281 SERIES ************************************************************************/
/* MEGA2561/1281 SERIES ************************************************************************/
/* MEGA2561/1281 SERIES ************************************************************************/
#elif defined __AVR_ATmega1281__ || defined __AVR_ATmega2561__
#define ARDUINO_MEGA /*{{{*/
#define EI_NOTPORTA
#define EI_NOTPORTC
#define EI_NOTPORTD
#define EI_NOTPORTJ
#define EI_NOTPORTK /*}}}*/
#if defined EI_NOTPINCHANGE/*{{{*/
#ifndef EI_NOTPORTB
#define EI_NOTPORTB
#endif
#endif
#ifndef NEEDFORSPEED
// Pin change interrupts
#define ARDUINO_PIN_B0 8
#define ARDUINO_PIN_B1 9
#define ARDUINO_PIN_B2 10
#define ARDUINO_PIN_B3 11
#define ARDUINO_PIN_B4 12
#define ARDUINO_PIN_B5 13
#define ARDUINO_PIN_B6 14
#define ARDUINO_PIN_B7 15
const uint8_t PROGMEM digital_pin_to_port_bit_number_PGM[] = {
0, // PE0 pin: 0
1, // PE1 pin: 1
2, // PE2 pin: 2
3, // PE3 pin: 3
4, // PE4 pin: 4
5, // PE5 pin: 5
6, // PE6 pin: 6
7, // PE7 pin: 7
0, // PB0 pin: 8
1, // PB1 pin: 9
2, // PB2 pin: 10
3, // PB3 pin: 11
4, // PB4 pin: 12
5, // PB5 pin: 13
6, // PB6 pin: 14
7, // PB7 pin: 15
3, // PG3 pin: 16
4, // PG4 pin: 17
0, // PD0 pin: 18
1, // PD1 pin: 19
2, // PD2 pin: 20
3, // PD3 pin: 21
4, // PD4 pin: 22
5, // PD5 pin: 23
6, // PD6 pin: 24
7, // PD7 pin: 25
0, // PG0 pin: 26
1, // PG1 pin: 27
0, // PC0 pin: 28
1, // PC1 pin: 29
2, // PC2 pin: 30
3, // PC3 pin: 31
4, // PC4 pin: 32
5, // PC5 pin: 33
6, // PC6 pin: 34
7, // PC7 pin: 35
2, // PG2 pin: 36
7, // PA7 pin: 37
6, // PA6 pin: 38
5, // PA5 pin: 39
4, // PA4 pin: 40
3, // PA3 pin: 41
2, // PA2 pin: 42
1, // PA1 pin: 43
0, // PA0 pin: 44
0, // PF0 pin: 45
1, // PF1 pin: 46
2, // PF2 pin: 47
3, // PF3 pin: 48
4, // PF4 pin: 49
5, // PF5 pin: 50
6, // PF6 pin: 51
7, // PF7 pin: 52
5 // PG5 pin: 53
};
#if ! defined(EI_NOTEXTERNAL) && ! defined(EI_NOTINT0) && ! defined(EI_NOTINT1) && ! defined(EI_NOTINT2) && ! defined(EI_NOTINT3) && ! defined(EI_NOTINT4) && ! defined(EI_NOTINT5) && ! defined(EI_NOTINT6) && ! defined(EI_NOTINT7)
interruptFunctionType functionPointerArrayEXTERNAL[8];
#endif
#ifndef EI_NOTPORTB
struct functionPointersPortB {
interruptFunctionType pinZero;
interruptFunctionType pinOne;
interruptFunctionType pinTwo;
interruptFunctionType pinThree;
interruptFunctionType pinFour;
interruptFunctionType pinFive;
interruptFunctionType pinSix;
interruptFunctionType pinSeven;
};
typedef struct functionPointersPortB functionPointersPortB;
functionPointersPortB portBFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif
#endif // ifndef NEEDFORSPEED
// For Pin Change Interrupts; since we're duplicating FALLING and RISING in software,
// we have to know how we were defined.
#ifndef EI_NOTPORTB
volatile uint8_t risingPinsPORTB=0;
volatile uint8_t fallingPinsPORTB=0;
static volatile uint8_t portSnapshotB;
#endif
#define PORTB_VECT PCINT0_vect
/*}}}*/
/* LEONARDO ***************************************************************************/
/* LEONARDO ***************************************************************************/
/* LEONARDO ***************************************************************************/
#elif defined __AVR_ATmega32U4__ || defined __AVR_ATmega16U4__
#define ARDUINO_LEONARDO /*{{{*/
#define EI_NOTPORTA
#define EI_NOTPORTC
#define EI_NOTPORTD
#define EI_NOTPORTJ
#define EI_NOTPORTK
/*}}}*/
#if defined EI_NOTPINCHANGE/*{{{*/
#ifndef EI_NOTPORTB
#define EI_NOTPORTB
#endif
#endif
#ifndef NEEDFORSPEED
#define ARDUINO_PIN_B0 17
#define ARDUINO_PIN_B1 15
#define ARDUINO_PIN_B2 16
#define ARDUINO_PIN_B3 14
#define ARDUINO_PIN_B4 8
#define ARDUINO_PIN_B5 9
#define ARDUINO_PIN_B6 10
#define ARDUINO_PIN_B7 11
#define ARDUINO_PIN_D0 3
#define ARDUINO_PIN_D1 2
#define ARDUINO_PIN_D2 0
#define ARDUINO_PIN_D3 1
#define ARDUINO_PIN_E6 7
/* To derive this list:
sed -n -e '1,/digital_pin_to_port_PGM/d' -e '/^}/,$d' -e '/P/p' \
/usr/share/arduino/hardware/arduino/variants/leonardo/pins_arduino.h | \
awk '{print " ", $5 ", // " $5 " pin: " $3}'
...then massage the output as necessary to create the below:
*/
const uint8_t PROGMEM digital_pin_to_port_bit_number_PGM[] = {
2, // PD2 pin: D0
3, // PD3 pin: D1
1, // PD1 pin: D2
0, // PD0 pin: D3
4, // PD4 pin: D4
6, // PC6 pin: D5
7, // PD7 pin: D6
6, // PE6 pin: D7
4, // PB4 pin: D8 // we really only care about Port B, but I don't know that
5, // PB5 pin: D9 // shortening this array and doing array index arithmetic
6, // PB6 pin: D10 // would make the code any shorter.
7, // PB7 pin: D11
6, // PD6 pin: D12
7, // PC7 pin: D13
3, // PB3 pin: D14 MISO
1, // PB1 pin: D15 SCK
2, // PB2 pin: D16 MOSI
0 // PB0 pin: D17 SS (RXLED). Available on non-Leonardo 32u4 boards, at least (exposed on the Leonardo??)
// There are no ports we care about after pin 17.
};
#if ! defined(EI_NOTEXTERNAL) && ! defined(EI_NOTINT0) && ! defined(EI_NOTINT1) && ! defined(EI_NOTINT2) && ! defined(EI_NOTINT3) && ! defined(EI_NOTINT6)
interruptFunctionType functionPointerArrayEXTERNAL[5];
#endif
#ifndef EI_NOTPORTB
struct functionPointersPortB {
interruptFunctionType pinZero;
interruptFunctionType pinOne;
interruptFunctionType pinTwo;
interruptFunctionType pinThree;
interruptFunctionType pinFour;
interruptFunctionType pinFive;
interruptFunctionType pinSix;
interruptFunctionType pinSeven;
};
typedef struct functionPointersPortB functionPointersPortB;
functionPointersPortB portBFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif
#endif // NEEDFOR SPEED
#ifndef EI_NOTPORTB
// For Pin Change Interrupts; since we're duplicating FALLING and RISING in software,
// we have to know how we were defined.
volatile uint8_t risingPinsPORTB=0;
volatile uint8_t fallingPinsPORTB=0;
// for the saved state of the ports
static volatile uint8_t portSnapshotB;
#endif
#define PORTB_VECT PCINT0_vect
/*}}}*/
/* 644/1284 ***************************************************************************/
/* 644/1284 ***************************************************************************/
/* 644/1284 ***************************************************************************/
#elif defined __AVR_ATmega1284P__ || defined __AVR_ATmega1284__ || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
#define MIGHTY1284 /*{{{*/
#define EI_NOTPORTJ
#define EI_NOTPORTK /*}}}*/
#if defined EI_NOTPINCHANGE/*{{{*/
#ifndef EI_NOTPORTA
#define EI_NOTPORTA
#endif
#ifndef EI_NOTPORTB
#define EI_NOTPORTB
#endif
#ifndef EI_NOTPORTC
#define EI_NOTPORTC
#endif
#ifndef EI_NOTPORTD
#define EI_NOTPORTD
#endif
#endif
#ifndef INPUT_PULLUP
#define INPUT_PULLUP 0x2
#endif
#ifndef NEEDFORSPEED
#if defined BOBUINO_PINOUT
#define ARDUINO_PIN_A0 21
#define ARDUINO_PIN_A1 20
#define ARDUINO_PIN_A2 19
#define ARDUINO_PIN_A3 18
#define ARDUINO_PIN_A4 17
#define ARDUINO_PIN_A5 16
#define ARDUINO_PIN_A6 15
#define ARDUINO_PIN_A7 14
#define ARDUINO_PIN_B0 4
#define ARDUINO_PIN_B1 5
#define ARDUINO_PIN_B2 6 // INT2
#define ARDUINO_PIN_B3 7
#define ARDUINO_PIN_B4 10
#define ARDUINO_PIN_B5 11
#define ARDUINO_PIN_B6 12
#define ARDUINO_PIN_B7 13
#define ARDUINO_PIN_C0 22
#define ARDUINO_PIN_C1 23
#define ARDUINO_PIN_C2 24
#define ARDUINO_PIN_C3 25
#define ARDUINO_PIN_C4 26
#define ARDUINO_PIN_C5 27
#define ARDUINO_PIN_C6 28
#define ARDUINO_PIN_C7 29
#define ARDUINO_PIN_D0 0
#define ARDUINO_PIN_D1 1
#define ARDUINO_PIN_D2 2 // INT0
#define ARDUINO_PIN_D3 3 // INT1
#define ARDUINO_PIN_D4 30
#define ARDUINO_PIN_D5 8
#define ARDUINO_PIN_D6 9
#define ARDUINO_PIN_D7 31
const uint8_t PROGMEM digital_pin_to_port_bit_number_PGM[] = {
0, // PD0
1, // PD1
2, // PD2
3, // PD3
0, // PB0
1, // PB1
2, // PB2
3, // PB3
5, // PD5
6, // PD6
4, // PB4
5, // PB5
6, // PB6
7, // PB7
7, // PA7
6, // PA6
5, // PA5
4, // PA4
3, // PA3
2, // PA2
1, // PA1
0, // PA0
0, // PC0
1, // PC1
2, // PC2
3, // PC3
4, // PC4
5, // PC5
6, // PC6
7, // PC7
4, // PD4
7, // PD7
};
#else
#define ARDUINO_PIN_A0 24
#define ARDUINO_PIN_A1 25
#define ARDUINO_PIN_A2 26
#define ARDUINO_PIN_A3 27
#define ARDUINO_PIN_A4 28
#define ARDUINO_PIN_A5 29
#define ARDUINO_PIN_A6 30
#define ARDUINO_PIN_A7 31
#define ARDUINO_PIN_C0 16
#define ARDUINO_PIN_C1 17
#define ARDUINO_PIN_C2 18
#define ARDUINO_PIN_C3 19
#define ARDUINO_PIN_C4 20
#define ARDUINO_PIN_C5 21
#define ARDUINO_PIN_C6 22
#define ARDUINO_PIN_C7 23
// For boards with the 44 pin options: vectors B & D are reversed
// Thanks to Sara Damiano for these updates!!!
#if defined ARDUINO_AVR_ENVIRODIY_MAYFLY || defined ARDUINO_AVR_SODAQ_MBILI
#define ARDUINO_PIN_B0 8
#define ARDUINO_PIN_B1 9
#define ARDUINO_PIN_B2 10 // INT2
#define ARDUINO_PIN_B3 11
#define ARDUINO_PIN_B4 12
#define ARDUINO_PIN_B5 13
#define ARDUINO_PIN_B6 14
#define ARDUINO_PIN_B7 15
#define ARDUINO_PIN_D0 0
#define ARDUINO_PIN_D1 1
#define ARDUINO_PIN_D2 2 // INT0
#define ARDUINO_PIN_D3 3 // INT1
#define ARDUINO_PIN_D4 4
#define ARDUINO_PIN_D5 5
#define ARDUINO_PIN_D6 6
#define ARDUINO_PIN_D7 7
#else
#define ARDUINO_PIN_B0 0
#define ARDUINO_PIN_B1 1
#define ARDUINO_PIN_B2 2 // INT2
#define ARDUINO_PIN_B3 3
#define ARDUINO_PIN_B4 4
#define ARDUINO_PIN_B5 5
#define ARDUINO_PIN_B6 6
#define ARDUINO_PIN_B7 7
#define ARDUINO_PIN_D0 8
#define ARDUINO_PIN_D1 9
#define ARDUINO_PIN_D2 10 // INT0
#define ARDUINO_PIN_D3 11 // INT1
#define ARDUINO_PIN_D4 12
#define ARDUINO_PIN_D5 13
#define ARDUINO_PIN_D6 14
#define ARDUINO_PIN_D7 15
#endif
const uint8_t PROGMEM digital_pin_to_port_bit_number_PGM[] = {
0, // 0 == port B, 0
1,
2,
3,
4,
5,
6,
7,
0, // 8 == port D, 0
1,
2,
3,
4,
5,
6,
7,
0, // 16 == port C, 0
1,
2,
3,
4,
5,
6,
7,
0, // 24 == port A, 0
1,
2,
3,
4,
5,
6,
7
};
#endif
#if ! defined(EI_NOTEXTERNAL) && ! defined(EI_NOTINT0) && ! defined(EI_NOTINT1) && ! defined(EI_NOTINT2)
interruptFunctionType functionPointerArrayEXTERNAL[3];
#endif
struct functionPointers {
interruptFunctionType pinZero;
interruptFunctionType pinOne;
interruptFunctionType pinTwo;
interruptFunctionType pinThree;
interruptFunctionType pinFour;
interruptFunctionType pinFive;
interruptFunctionType pinSix;
interruptFunctionType pinSeven;
};
#ifndef EI_NOTPORTA
typedef struct functionPointers functionPointersPortA;
functionPointers portAFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif
#ifndef EI_NOTPORTB
typedef struct functionPointers functionPointersPortB;
functionPointersPortB portBFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif
#ifndef EI_NOTPORTC
typedef struct functionPointers functionPointersPortC;
functionPointersPortC portCFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif
#ifndef EI_NOTPORTD
typedef struct functionPointers functionPointersPortD;
functionPointersPortD portDFunctions = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
#endif
#endif // NEEDFORSPEED
#ifndef EI_NOTPORTA
volatile uint8_t risingPinsPORTA=0;
volatile uint8_t fallingPinsPORTA=0;
// for the saved state of the ports
static volatile uint8_t portSnapshotA;
#endif
#ifndef EI_NOTPORTB
volatile uint8_t risingPinsPORTB=0;
volatile uint8_t fallingPinsPORTB=0;
static volatile uint8_t portSnapshotB;
#endif
#ifndef EI_NOTPORTC
volatile uint8_t risingPinsPORTC=0;
volatile uint8_t fallingPinsPORTC=0;
static volatile uint8_t portSnapshotC;
#endif
#ifndef EI_NOTPORTD
volatile uint8_t risingPinsPORTD=0;
volatile uint8_t fallingPinsPORTD=0;
static volatile uint8_t portSnapshotD;
#endif
// the vectors (eg, "PCINT0_vect") are defined in the avr.h files, like iom1284p.h
#define PORTA_VECT PCINT0_vect