@@ -38,56 +38,328 @@ uint8_t const SCK_PIN = SCK;
3838#define Sd2PinMap_h
3939#include <avr/io.h>
4040
41- #include <Arduino.h>
41+ //------------------------------------------------------------------------------
42+ /** struct for mapping digital pins */
43+ struct pin_map_t {
44+ volatile uint8_t * ddr ;
45+ volatile uint8_t * pin ;
46+ volatile uint8_t * port ;
47+ uint8_t bit ;
48+ };
49+ //------------------------------------------------------------------------------
50+ #if defined(__AVR_ATmega1280__ ) || defined(__AVR_ATmega2560__ )
51+ // Mega
4252
4353// Two Wire (aka I2C) ports
44- uint8_t const SDA_PIN = SDA ;
45- uint8_t const SCL_PIN = SCL ;
54+ uint8_t const SDA_PIN = 20 ;
55+ uint8_t const SCL_PIN = 21 ;
4656
4757// SPI port
48- uint8_t const SS_PIN = SS ;
49- uint8_t const MOSI_PIN = MOSI ;
50- uint8_t const MISO_PIN = MISO ;
51- uint8_t const SCK_PIN = SCK ;
58+ uint8_t const SS_PIN = 53 ;
59+ uint8_t const MOSI_PIN = 51 ;
60+ uint8_t const MISO_PIN = 50 ;
61+ uint8_t const SCK_PIN = 52 ;
62+
63+ static const pin_map_t digitalPinMap [] = {
64+ {& DDRE , & PINE , & PORTE , 0 }, // E0 0
65+ {& DDRE , & PINE , & PORTE , 1 }, // E1 1
66+ {& DDRE , & PINE , & PORTE , 4 }, // E4 2
67+ {& DDRE , & PINE , & PORTE , 5 }, // E5 3
68+ {& DDRG , & PING , & PORTG , 5 }, // G5 4
69+ {& DDRE , & PINE , & PORTE , 3 }, // E3 5
70+ {& DDRH , & PINH , & PORTH , 3 }, // H3 6
71+ {& DDRH , & PINH , & PORTH , 4 }, // H4 7
72+ {& DDRH , & PINH , & PORTH , 5 }, // H5 8
73+ {& DDRH , & PINH , & PORTH , 6 }, // H6 9
74+ {& DDRB , & PINB , & PORTB , 4 }, // B4 10
75+ {& DDRB , & PINB , & PORTB , 5 }, // B5 11
76+ {& DDRB , & PINB , & PORTB , 6 }, // B6 12
77+ {& DDRB , & PINB , & PORTB , 7 }, // B7 13
78+ {& DDRJ , & PINJ , & PORTJ , 1 }, // J1 14
79+ {& DDRJ , & PINJ , & PORTJ , 0 }, // J0 15
80+ {& DDRH , & PINH , & PORTH , 1 }, // H1 16
81+ {& DDRH , & PINH , & PORTH , 0 }, // H0 17
82+ {& DDRD , & PIND , & PORTD , 3 }, // D3 18
83+ {& DDRD , & PIND , & PORTD , 2 }, // D2 19
84+ {& DDRD , & PIND , & PORTD , 1 }, // D1 20
85+ {& DDRD , & PIND , & PORTD , 0 }, // D0 21
86+ {& DDRA , & PINA , & PORTA , 0 }, // A0 22
87+ {& DDRA , & PINA , & PORTA , 1 }, // A1 23
88+ {& DDRA , & PINA , & PORTA , 2 }, // A2 24
89+ {& DDRA , & PINA , & PORTA , 3 }, // A3 25
90+ {& DDRA , & PINA , & PORTA , 4 }, // A4 26
91+ {& DDRA , & PINA , & PORTA , 5 }, // A5 27
92+ {& DDRA , & PINA , & PORTA , 6 }, // A6 28
93+ {& DDRA , & PINA , & PORTA , 7 }, // A7 29
94+ {& DDRC , & PINC , & PORTC , 7 }, // C7 30
95+ {& DDRC , & PINC , & PORTC , 6 }, // C6 31
96+ {& DDRC , & PINC , & PORTC , 5 }, // C5 32
97+ {& DDRC , & PINC , & PORTC , 4 }, // C4 33
98+ {& DDRC , & PINC , & PORTC , 3 }, // C3 34
99+ {& DDRC , & PINC , & PORTC , 2 }, // C2 35
100+ {& DDRC , & PINC , & PORTC , 1 }, // C1 36
101+ {& DDRC , & PINC , & PORTC , 0 }, // C0 37
102+ {& DDRD , & PIND , & PORTD , 7 }, // D7 38
103+ {& DDRG , & PING , & PORTG , 2 }, // G2 39
104+ {& DDRG , & PING , & PORTG , 1 }, // G1 40
105+ {& DDRG , & PING , & PORTG , 0 }, // G0 41
106+ {& DDRL , & PINL , & PORTL , 7 }, // L7 42
107+ {& DDRL , & PINL , & PORTL , 6 }, // L6 43
108+ {& DDRL , & PINL , & PORTL , 5 }, // L5 44
109+ {& DDRL , & PINL , & PORTL , 4 }, // L4 45
110+ {& DDRL , & PINL , & PORTL , 3 }, // L3 46
111+ {& DDRL , & PINL , & PORTL , 2 }, // L2 47
112+ {& DDRL , & PINL , & PORTL , 1 }, // L1 48
113+ {& DDRL , & PINL , & PORTL , 0 }, // L0 49
114+ {& DDRB , & PINB , & PORTB , 3 }, // B3 50
115+ {& DDRB , & PINB , & PORTB , 2 }, // B2 51
116+ {& DDRB , & PINB , & PORTB , 1 }, // B1 52
117+ {& DDRB , & PINB , & PORTB , 0 }, // B0 53
118+ {& DDRF , & PINF , & PORTF , 0 }, // F0 54
119+ {& DDRF , & PINF , & PORTF , 1 }, // F1 55
120+ {& DDRF , & PINF , & PORTF , 2 }, // F2 56
121+ {& DDRF , & PINF , & PORTF , 3 }, // F3 57
122+ {& DDRF , & PINF , & PORTF , 4 }, // F4 58
123+ {& DDRF , & PINF , & PORTF , 5 }, // F5 59
124+ {& DDRF , & PINF , & PORTF , 6 }, // F6 60
125+ {& DDRF , & PINF , & PORTF , 7 }, // F7 61
126+ {& DDRK , & PINK , & PORTK , 0 }, // K0 62
127+ {& DDRK , & PINK , & PORTK , 1 }, // K1 63
128+ {& DDRK , & PINK , & PORTK , 2 }, // K2 64
129+ {& DDRK , & PINK , & PORTK , 3 }, // K3 65
130+ {& DDRK , & PINK , & PORTK , 4 }, // K4 66
131+ {& DDRK , & PINK , & PORTK , 5 }, // K5 67
132+ {& DDRK , & PINK , & PORTK , 6 }, // K6 68
133+ {& DDRK , & PINK , & PORTK , 7 } // K7 69
134+ };
135+ //------------------------------------------------------------------------------
136+ #elif defined(__AVR_ATmega644P__ ) || defined(__AVR_ATmega644__ )
137+ // Sanguino
138+
139+ // Two Wire (aka I2C) ports
140+ uint8_t const SDA_PIN = 17 ;
141+ uint8_t const SCL_PIN = 18 ;
142+
143+ // SPI port
144+ uint8_t const SS_PIN = 4 ;
145+ uint8_t const MOSI_PIN = 5 ;
146+ uint8_t const MISO_PIN = 6 ;
147+ uint8_t const SCK_PIN = 7 ;
148+
149+ static const pin_map_t digitalPinMap [] = {
150+ {& DDRB , & PINB , & PORTB , 0 }, // B0 0
151+ {& DDRB , & PINB , & PORTB , 1 }, // B1 1
152+ {& DDRB , & PINB , & PORTB , 2 }, // B2 2
153+ {& DDRB , & PINB , & PORTB , 3 }, // B3 3
154+ {& DDRB , & PINB , & PORTB , 4 }, // B4 4
155+ {& DDRB , & PINB , & PORTB , 5 }, // B5 5
156+ {& DDRB , & PINB , & PORTB , 6 }, // B6 6
157+ {& DDRB , & PINB , & PORTB , 7 }, // B7 7
158+ {& DDRD , & PIND , & PORTD , 0 }, // D0 8
159+ {& DDRD , & PIND , & PORTD , 1 }, // D1 9
160+ {& DDRD , & PIND , & PORTD , 2 }, // D2 10
161+ {& DDRD , & PIND , & PORTD , 3 }, // D3 11
162+ {& DDRD , & PIND , & PORTD , 4 }, // D4 12
163+ {& DDRD , & PIND , & PORTD , 5 }, // D5 13
164+ {& DDRD , & PIND , & PORTD , 6 }, // D6 14
165+ {& DDRD , & PIND , & PORTD , 7 }, // D7 15
166+ {& DDRC , & PINC , & PORTC , 0 }, // C0 16
167+ {& DDRC , & PINC , & PORTC , 1 }, // C1 17
168+ {& DDRC , & PINC , & PORTC , 2 }, // C2 18
169+ {& DDRC , & PINC , & PORTC , 3 }, // C3 19
170+ {& DDRC , & PINC , & PORTC , 4 }, // C4 20
171+ {& DDRC , & PINC , & PORTC , 5 }, // C5 21
172+ {& DDRC , & PINC , & PORTC , 6 }, // C6 22
173+ {& DDRC , & PINC , & PORTC , 7 }, // C7 23
174+ {& DDRA , & PINA , & PORTA , 7 }, // A7 24
175+ {& DDRA , & PINA , & PORTA , 6 }, // A6 25
176+ {& DDRA , & PINA , & PORTA , 5 }, // A5 26
177+ {& DDRA , & PINA , & PORTA , 4 }, // A4 27
178+ {& DDRA , & PINA , & PORTA , 3 }, // A3 28
179+ {& DDRA , & PINA , & PORTA , 2 }, // A2 29
180+ {& DDRA , & PINA , & PORTA , 1 }, // A1 30
181+ {& DDRA , & PINA , & PORTA , 0 } // A0 31
182+ };
183+ //------------------------------------------------------------------------------
184+ #elif defined(__AVR_ATmega32U4__ )
185+ // Leonardo
186+
187+ // Two Wire (aka I2C) ports
188+ uint8_t const SDA_PIN = 2 ;
189+ uint8_t const SCL_PIN = 3 ;
190+
191+ // SPI port
192+ uint8_t const SS_PIN = 17 ;
193+ uint8_t const MOSI_PIN = 16 ;
194+ uint8_t const MISO_PIN = 14 ;
195+ uint8_t const SCK_PIN = 15 ;
196+
197+ static const pin_map_t digitalPinMap [] = {
198+ {& DDRD , & PIND , & PORTD , 2 }, // D2 0
199+ {& DDRD , & PIND , & PORTD , 3 }, // D3 1
200+ {& DDRD , & PIND , & PORTD , 1 }, // D1 2
201+ {& DDRD , & PIND , & PORTD , 0 }, // D0 3
202+ {& DDRD , & PIND , & PORTD , 4 }, // D4 4
203+ {& DDRC , & PINC , & PORTC , 6 }, // C6 5
204+ {& DDRD , & PIND , & PORTD , 7 }, // D7 6
205+ {& DDRE , & PINE , & PORTE , 6 }, // E6 7
206+ {& DDRB , & PINB , & PORTB , 4 }, // B4 8
207+ {& DDRB , & PINB , & PORTB , 5 }, // B5 9
208+ {& DDRB , & PINB , & PORTB , 6 }, // B6 10
209+ {& DDRB , & PINB , & PORTB , 7 }, // B7 11
210+ {& DDRD , & PIND , & PORTD , 6 }, // D6 12
211+ {& DDRC , & PINC , & PORTC , 7 }, // C7 13
212+ {& DDRB , & PINB , & PORTB , 3 }, // B3 14
213+ {& DDRB , & PINB , & PORTB , 1 }, // B1 15
214+ {& DDRB , & PINB , & PORTB , 2 }, // B2 16
215+ {& DDRB , & PINB , & PORTB , 0 }, // B0 17
216+ {& DDRF , & PINF , & PORTF , 7 }, // F7 18
217+ {& DDRF , & PINF , & PORTF , 6 }, // F6 19
218+ {& DDRF , & PINF , & PORTF , 5 }, // F5 20
219+ {& DDRF , & PINF , & PORTF , 4 }, // F4 21
220+ {& DDRF , & PINF , & PORTF , 1 }, // F1 22
221+ {& DDRF , & PINF , & PORTF , 0 }, // F0 23
222+ };
223+ //------------------------------------------------------------------------------
224+ #elif defined(__AVR_AT90USB646__ ) || defined(__AVR_AT90USB1286__ )
225+ // Teensy++ 1.0 & 2.0
226+
227+ // Two Wire (aka I2C) ports
228+ uint8_t const SDA_PIN = 1 ;
229+ uint8_t const SCL_PIN = 0 ;
230+
231+ // SPI port
232+ uint8_t const SS_PIN = 20 ;
233+ uint8_t const MOSI_PIN = 22 ;
234+ uint8_t const MISO_PIN = 23 ;
235+ uint8_t const SCK_PIN = 21 ;
236+
237+ static const pin_map_t digitalPinMap [] = {
238+ {& DDRD , & PIND , & PORTD , 0 }, // D0 0
239+ {& DDRD , & PIND , & PORTD , 1 }, // D1 1
240+ {& DDRD , & PIND , & PORTD , 2 }, // D2 2
241+ {& DDRD , & PIND , & PORTD , 3 }, // D3 3
242+ {& DDRD , & PIND , & PORTD , 4 }, // D4 4
243+ {& DDRD , & PIND , & PORTD , 5 }, // D5 5
244+ {& DDRD , & PIND , & PORTD , 6 }, // D6 6
245+ {& DDRD , & PIND , & PORTD , 7 }, // D7 7
246+ {& DDRE , & PINE , & PORTE , 0 }, // E0 8
247+ {& DDRE , & PINE , & PORTE , 1 }, // E1 9
248+ {& DDRC , & PINC , & PORTC , 0 }, // C0 10
249+ {& DDRC , & PINC , & PORTC , 1 }, // C1 11
250+ {& DDRC , & PINC , & PORTC , 2 }, // C2 12
251+ {& DDRC , & PINC , & PORTC , 3 }, // C3 13
252+ {& DDRC , & PINC , & PORTC , 4 }, // C4 14
253+ {& DDRC , & PINC , & PORTC , 5 }, // C5 15
254+ {& DDRC , & PINC , & PORTC , 6 }, // C6 16
255+ {& DDRC , & PINC , & PORTC , 7 }, // C7 17
256+ {& DDRE , & PINE , & PORTE , 6 }, // E6 18
257+ {& DDRE , & PINE , & PORTE , 7 }, // E7 19
258+ {& DDRB , & PINB , & PORTB , 0 }, // B0 20
259+ {& DDRB , & PINB , & PORTB , 1 }, // B1 21
260+ {& DDRB , & PINB , & PORTB , 2 }, // B2 22
261+ {& DDRB , & PINB , & PORTB , 3 }, // B3 23
262+ {& DDRB , & PINB , & PORTB , 4 }, // B4 24
263+ {& DDRB , & PINB , & PORTB , 5 }, // B5 25
264+ {& DDRB , & PINB , & PORTB , 6 }, // B6 26
265+ {& DDRB , & PINB , & PORTB , 7 }, // B7 27
266+ {& DDRA , & PINA , & PORTA , 0 }, // A0 28
267+ {& DDRA , & PINA , & PORTA , 1 }, // A1 29
268+ {& DDRA , & PINA , & PORTA , 2 }, // A2 30
269+ {& DDRA , & PINA , & PORTA , 3 }, // A3 31
270+ {& DDRA , & PINA , & PORTA , 4 }, // A4 32
271+ {& DDRA , & PINA , & PORTA , 5 }, // A5 33
272+ {& DDRA , & PINA , & PORTA , 6 }, // A6 34
273+ {& DDRA , & PINA , & PORTA , 7 }, // A7 35
274+ {& DDRE , & PINE , & PORTE , 4 }, // E4 36
275+ {& DDRE , & PINE , & PORTE , 5 }, // E5 37
276+ {& DDRF , & PINF , & PORTF , 0 }, // F0 38
277+ {& DDRF , & PINF , & PORTF , 1 }, // F1 39
278+ {& DDRF , & PINF , & PORTF , 2 }, // F2 40
279+ {& DDRF , & PINF , & PORTF , 3 }, // F3 41
280+ {& DDRF , & PINF , & PORTF , 4 }, // F4 42
281+ {& DDRF , & PINF , & PORTF , 5 }, // F5 43
282+ {& DDRF , & PINF , & PORTF , 6 }, // F6 44
283+ {& DDRF , & PINF , & PORTF , 7 } // F7 45
284+ };
285+ //------------------------------------------------------------------------------
286+ #else // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
287+ // 168 and 328 Arduinos
288+
289+ // Two Wire (aka I2C) ports
290+ uint8_t const SDA_PIN = 18 ;
291+ uint8_t const SCL_PIN = 19 ;
292+
293+ // SPI port
294+ uint8_t const SS_PIN = 10 ;
295+ uint8_t const MOSI_PIN = 11 ;
296+ uint8_t const MISO_PIN = 12 ;
297+ uint8_t const SCK_PIN = 13 ;
298+
299+ static const pin_map_t digitalPinMap [] = {
300+ {& DDRD , & PIND , & PORTD , 0 }, // D0 0
301+ {& DDRD , & PIND , & PORTD , 1 }, // D1 1
302+ {& DDRD , & PIND , & PORTD , 2 }, // D2 2
303+ {& DDRD , & PIND , & PORTD , 3 }, // D3 3
304+ {& DDRD , & PIND , & PORTD , 4 }, // D4 4
305+ {& DDRD , & PIND , & PORTD , 5 }, // D5 5
306+ {& DDRD , & PIND , & PORTD , 6 }, // D6 6
307+ {& DDRD , & PIND , & PORTD , 7 }, // D7 7
308+ {& DDRB , & PINB , & PORTB , 0 }, // B0 8
309+ {& DDRB , & PINB , & PORTB , 1 }, // B1 9
310+ {& DDRB , & PINB , & PORTB , 2 }, // B2 10
311+ {& DDRB , & PINB , & PORTB , 3 }, // B3 11
312+ {& DDRB , & PINB , & PORTB , 4 }, // B4 12
313+ {& DDRB , & PINB , & PORTB , 5 }, // B5 13
314+ {& DDRC , & PINC , & PORTC , 0 }, // C0 14
315+ {& DDRC , & PINC , & PORTC , 1 }, // C1 15
316+ {& DDRC , & PINC , & PORTC , 2 }, // C2 16
317+ {& DDRC , & PINC , & PORTC , 3 }, // C3 17
318+ {& DDRC , & PINC , & PORTC , 4 }, // C4 18
319+ {& DDRC , & PINC , & PORTC , 5 } // C5 19
320+ };
321+ #endif // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
322+ //------------------------------------------------------------------------------
323+ static const uint8_t digitalPinCount = sizeof (digitalPinMap )/sizeof (pin_map_t );
52324
53325uint8_t badPinNumber (void )
54326 __attribute__((error ("Pin number is too large or not a constant" )));
55327
56328static inline __attribute__((always_inline ))
57329 uint8_t getPinMode (uint8_t pin ) {
58- if (__builtin_constant_p (pin ) && pin < NUM_DIGITAL_PINS ) {
59- return * portModeRegister ( digitalPinToPort ( pin )) & digitalPinToBitMask ( pin ) ;
330+ if (__builtin_constant_p (pin ) && pin < digitalPinCount ) {
331+ return ( * digitalPinMap [ pin ]. ddr >> digitalPinMap [ pin ]. bit ) & 1 ;
60332 } else {
61333 return badPinNumber ();
62334 }
63335}
64336static inline __attribute__((always_inline ))
65337 void setPinMode (uint8_t pin , uint8_t mode ) {
66- if (__builtin_constant_p (pin ) && pin < NUM_DIGITAL_PINS ) {
338+ if (__builtin_constant_p (pin ) && pin < digitalPinCount ) {
67339 if (mode ) {
68- * portModeRegister ( digitalPinToPort ( pin )) |= digitalPinToBitMask ( pin ) ;
340+ * digitalPinMap [ pin ]. ddr |= 1 << digitalPinMap [ pin ]. bit ;
69341 } else {
70- * portModeRegister ( digitalPinToPort ( pin )) &= ~digitalPinToBitMask ( pin );
342+ * digitalPinMap [ pin ]. ddr &= ~( 1 << digitalPinMap [ pin ]. bit );
71343 }
72344 } else {
73345 badPinNumber ();
74346 }
75347}
76348static inline __attribute__((always_inline ))
77349 uint8_t fastDigitalRead (uint8_t pin ) {
78- if (__builtin_constant_p (pin ) && pin < NUM_DIGITAL_PINS ) {
79- return * portInputRegister ( digitalPinToPort ( pin )) & digitalPinToBitMask ( pin ) ;
350+ if (__builtin_constant_p (pin ) && pin < digitalPinCount ) {
351+ return ( * digitalPinMap [ pin ]. pin >> digitalPinMap [ pin ]. bit ) & 1 ;
80352 } else {
81353 return badPinNumber ();
82354 }
83355}
84356static inline __attribute__((always_inline ))
85357 void fastDigitalWrite (uint8_t pin , uint8_t value ) {
86- if (__builtin_constant_p (pin ) && pin < NUM_DIGITAL_PINS ) {
358+ if (__builtin_constant_p (pin ) && pin < digitalPinCount ) {
87359 if (value ) {
88- * portOutputRegister ( digitalPinToPort ( pin )) |= digitalPinToBitMask ( pin ) ;
360+ * digitalPinMap [ pin ]. port |= 1 << digitalPinMap [ pin ]. bit ;
89361 } else {
90- * portOutputRegister ( digitalPinToPort ( pin )) &= ~digitalPinToBitMask ( pin );
362+ * digitalPinMap [ pin ]. port &= ~( 1 << digitalPinMap [ pin ]. bit );
91363 }
92364 } else {
93365 badPinNumber ();
0 commit comments