1
1
/*
2
- Optimized digital functions for AVR microcontrollers
3
- by Watterott electronic (www.watterott.com)
4
- based on http://code.google.com/p/digitalwritefast
2
+ * digitalWriteFast.h
3
+ *
4
+ * Optimized digital functions for AVR microcontrollers
5
+ * by Watterott electronic (www.watterott.com)
6
+ * based on https://code.google.com/p/digitalwritefast
5
7
*/
6
8
7
9
#ifndef __digitalWriteFast_h_
40
42
# define BIN (2)
41
43
#endif
42
44
43
-
44
45
// workarounds for ARM microcontrollers
45
46
#if (!defined(__AVR__ ) || \
46
47
defined(ARDUINO_ARCH_SAM ) || \
99
100
100
101
#endif
101
102
102
-
103
103
// digital functions
104
104
//#if !defined(digitalPinToPortReg)
105
105
#define SPI_SW_SS_PIN (10) //SS on Uno (for software SPI)
106
106
#define SPI_SW_MOSI_PIN (11) //MOSI on Uno (for software SPI)
107
107
#define SPI_SW_MISO_PIN (12) //MISO on Uno (for software SPI)
108
108
#define SPI_SW_SCK_PIN (13) //SCK on Uno (for software SPI)
109
109
110
-
111
110
// --- Arduino Due and SAM3X8E based boards ---
112
111
#if (defined(ARDUINO_SAM_DUE ) || \
113
112
defined(__SAM3X8E__ ))
165
164
((((P) >= 10 && (P) <= 13) || ((P) >= 50 && (P) <= 53)) ? &PORTB : \
166
165
(((P) >= 30 && (P) <= 37) ? &PORTC : \
167
166
((((P) >= 18 && (P) <= 21) || (P) == 38) ? &PORTD : \
168
- ((((P) >= 0 && (P) <= 3) || (P) == 5) ? &PORTE : \
167
+ ((((P) <= 3) || (P) == 5) ? &PORTE : \
169
168
(((P) >= 54 && (P) <= 61) ? &PORTF : \
170
169
((((P) >= 39 && (P) <= 41) || (P) == 4) ? &PORTG : \
171
170
((((P) >= 6 && (P) <= 9) || (P) == 16 || (P) == 17) ? &PORTH : \
177
176
((((P) >= 10 && (P) <= 13) || ((P) >= 50 && (P) <= 53)) ? &DDRB : \
178
177
(((P) >= 30 && (P) <= 37) ? &DDRC : \
179
178
((((P) >= 18 && (P) <= 21) || (P) == 38) ? &DDRD : \
180
- ((((P) >= 0 && (P) <= 3) || (P) == 5) ? &DDRE : \
179
+ ((((P) <= 3) || (P) == 5) ? &DDRE : \
181
180
(((P) >= 54 && (P) <= 61) ? &DDRF : \
182
181
((((P) >= 39 && (P) <= 41) || (P) == 4) ? &DDRG : \
183
182
((((P) >= 6 && (P) <= 9) || (P) == 16 || (P) == 17) ? &DDRH : \
189
188
((((P) >= 10 && (P) <= 13) || ((P) >= 50 && (P) <= 53)) ? &PINB : \
190
189
(((P) >= 30 && (P) <= 37) ? &PINC : \
191
190
((((P) >= 18 && (P) <= 21) || (P) == 38) ? &PIND : \
192
- ((((P) >= 0 && (P) <= 3) || (P) == 5) ? &PINE : \
191
+ ((((P) <= 3) || (P) == 5) ? &PINE : \
193
192
(((P) >= 54 && (P) <= 61) ? &PINF : \
194
193
((((P) >= 39 && (P) <= 41) || (P) == 4) ? &PING : \
195
194
((((P) >= 6 && (P) <= 9) || (P) == 16 || (P) == 17) ? &PINH : \
244
243
245
244
#if defined(__AVR_ATmega324PB__ )
246
245
#define __digitalPinToPortReg (P ) \
247
- (((P) >= 0 && (P) <= 7) ? &PORTB : (((P) >= 8 && (P) <= 15) ? &PORTD : (((P) >= 16 && (P) <= 23) ? &PORTC : (((P) >= 24 && (P) <= 31) ? &PORTA : &PORTE))))
246
+ (((P) <= 7) ? &PORTB : (((P) >= 8 && (P) <= 15) ? &PORTD : (((P) >= 16 && (P) <= 23) ? &PORTC : (((P) >= 24 && (P) <= 31) ? &PORTA : &PORTE))))
248
247
#define __digitalPinToDDRReg (P ) \
249
- (((P) >= 0 && (P) <= 7) ? &DDRB : (((P) >= 8 && (P) <= 15) ? &DDRD : (((P) >= 16 && (P) <= 23) ? &DDRC : (((P) >= 24 && (P) <= 31) ? &DDRA : &DDRE))))
248
+ (((P) <= 7) ? &DDRB : (((P) >= 8 && (P) <= 15) ? &DDRD : (((P) >= 16 && (P) <= 23) ? &DDRC : (((P) >= 24 && (P) <= 31) ? &DDRA : &DDRE))))
250
249
#define __digitalPinToPINReg (P ) \
251
- (((P) >= 0 && (P) <= 7) ? &PINB : (((P) >= 8 && (P) <= 15) ? &PIND : (((P) >= 16 && (P) <= 23) ? &PINC : (((P) >= 24 && (P) <= 31) ? &PINA : &PINE))))
250
+ (((P) <= 7) ? &PINB : (((P) >= 8 && (P) <= 15) ? &PIND : (((P) >= 16 && (P) <= 23) ? &PINC : (((P) >= 24 && (P) <= 31) ? &PINA : &PINE))))
252
251
# if defined(SANGUINO_PINOUT )
253
252
#define __digitalPinToBit (P ) \
254
- (((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 15) ? (P) - 8 : (((P) >= 16 && (P) <= 23) ? (P) - 16 : (((P) >= 16 && (P) <= 23) ? (7 - ((P) - 24)) : (P) - 32))))
253
+ (((P) <= 7) ? (P) : (((P) >= 8 && (P) <= 15) ? (P) - 8 : (((P) >= 16 && (P) <= 23) ? (P) - 16 : (((P) >= 16 && (P) <= 23) ? (7 - ((P) - 24)) : (P) - 32))))
255
254
# else //MightyCore Pinout
256
255
#define __digitalPinToBit (P ) \
257
- (((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 15) ? (P) - 8 : (((P) >= 16 && (P) <= 23) ? (P) - 16 : (((P) >= 16 && (P) <= 23) ? (P) - 24 : (P) - 32))))
256
+ (((P) <= 7) ? (P) : (((P) >= 8 && (P) <= 15) ? (P) - 8 : (((P) >= 16 && (P) <= 23) ? (P) - 16 : (((P) >= 16 && (P) <= 23) ? (P) - 24 : (P) - 32))))
258
257
# endif
259
258
#else
260
259
#define __digitalPinToPortReg (P ) \
261
- (((P) >= 0 && (P) <= 7) ? &PORTB : (((P) >= 8 && (P) <= 15) ? &PORTD : (((P) >= 16 && (P) <= 23) ? &PORTC : &PORTA)))
260
+ (((P) <= 7) ? &PORTB : (((P) >= 8 && (P) <= 15) ? &PORTD : (((P) >= 16 && (P) <= 23) ? &PORTC : &PORTA)))
262
261
#define __digitalPinToDDRReg (P ) \
263
- (((P) >= 0 && (P) <= 7) ? &DDRB : (((P) >= 8 && (P) <= 15) ? &DDRD : (((P) >= 16 && (P) <= 23) ? &DDRC : &DDRA)))
262
+ (((P) <= 7) ? &DDRB : (((P) >= 8 && (P) <= 15) ? &DDRD : (((P) >= 16 && (P) <= 23) ? &DDRC : &DDRA)))
264
263
#define __digitalPinToPINReg (P ) \
265
- (((P) >= 0 && (P) <= 7) ? &PINB : (((P) >= 8 && (P) <= 15) ? &PIND : (((P) >= 16 && (P) <= 23) ? &PINC : &PINA)))
264
+ (((P) <= 7) ? &PINB : (((P) >= 8 && (P) <= 15) ? &PIND : (((P) >= 16 && (P) <= 23) ? &PINC : &PINA)))
266
265
# if defined(SANGUINO_PINOUT )
267
266
#define __digitalPinToBit (P ) \
268
- (((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 15) ? (P) - 8 : (((P) >= 16 && (P) <= 23) ? (P) - 16 : (7 - ((P) - 24)))))
267
+ (((P) <= 7) ? (P) : (((P) >= 8 && (P) <= 15) ? (P) - 8 : (((P) >= 16 && (P) <= 23) ? (P) - 16 : (7 - ((P) - 24)))))
269
268
# else //MightyCore Pinout
270
269
#define __digitalPinToBit (P ) \
271
- (((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 15) ? (P) - 8 : (((P) >= 16 && (P) <= 23) ? (P) - 16 : (P) - 24)))
270
+ (((P) <= 7) ? (P) : (((P) >= 8 && (P) <= 15) ? (P) - 8 : (((P) >= 16 && (P) <= 23) ? (P) - 16 : (P) - 24)))
272
271
# endif
273
272
#endif
274
273
277
276
#elif (defined(ARDUINO_AVR_LEONARDO ) || \
278
277
defined(__AVR_ATmega16U4__ ) || \
279
278
defined(__AVR_ATmega32U4__ ))
279
+ # if defined(TEENSYDUINO )
280
+ #define UART_RX_PIN (7) //PD2
281
+ #define UART_TX_PIN (8) //PD3
282
+
283
+ #define I2C_SDA_PIN (6) //PD1
284
+ #define I2C_SCL_PIN (5) //PD0
285
+
286
+ #define SPI_HW_SS_PIN (0) //PB0
287
+ #define SPI_HW_MOSI_PIN (2) //PB2
288
+ #define SPI_HW_MISO_PIN (3) //PB3
289
+ #define SPI_HW_SCK_PIN (1) //PB1
290
+
291
+ #define __digitalPinToPortReg (P ) \
292
+ ((((P) <= 4) || ((P) >= 13 && (P) <= 15)) ? &PORTB : (((P) == 9 || (P) == 10) ? &PORTC : (((P) >= 16 && (P) <= 21)) ? &PORTF : &PORTD))
293
+ #define __digitalPinToDDRReg (P ) \
294
+ ((((P) <= 4) || ((P) >= 13 && (P) <= 15)) ? &DDRB : (((P) == 9 || (P) == 10) ? &DDRC : (((P) >= 16 && (P) <= 21)) ? &DDRF : &DDRD))
295
+ #define __digitalPinToPINReg (P ) \
296
+ ((((P) <= 4) || ((P) >= 13 && (P) <= 15)) ? &PINB : (((P) == 9 || (P) == 10) ? &PINC : (((P) >= 16 && (P) <= 21)) ? &PINF : &PIND))
297
+ #define __digitalPinToBit (P ) \
298
+ (((P) <= 3) ? (P) : \
299
+ (((P) == 4 || (P) == 12) ? 7 : \
300
+ (((P) <= 8) ? (P) - 5 : \
301
+ (((P) <= 10) ? (P) - 3 : \
302
+ (((P) == 11) ? 6 : \
303
+ (((P) <= 15) ? (P) - 9 : \
304
+ (((P) <= 19) ? 23 - (P) : \
305
+ (((P) <= 21) ? 21 - (P) : (P) - 18))))))))
306
+ # else
280
307
281
308
#define UART_RX_PIN (0) //PD2
282
309
#define UART_TX_PIN (1) //PD3
290
317
#define SPI_HW_SCK_PIN (15) //PB1
291
318
292
319
#define __digitalPinToPortReg (P ) \
293
- ((((P) >= 0 && (P) <= 4) || (P) == 6 || (P) == 12 || (P) == 24 || (P) == 25 || (P) == 29) ? &PORTD : (((P) == 5 || (P) == 13) ? &PORTC : (((P) >= 18 && (P) <= 23)) ? &PORTF : (((P) == 7) ? &PORTE : &PORTB)))
320
+ ((((P) <= 4) || (P) == 6 || (P) == 12 || (P) == 24 || (P) == 25 || (P) == 29) ? &PORTD : (((P) == 5 || (P) == 13) ? &PORTC : (((P) >= 18 && (P) <= 23)) ? &PORTF : (((P) == 7) ? &PORTE : &PORTB)))
294
321
#define __digitalPinToDDRReg (P ) \
295
- ((((P) >= 0 && (P) <= 4) || (P) == 6 || (P) == 12 || (P) == 24 || (P) == 25 || (P) == 29) ? &DDRD : (((P) == 5 || (P) == 13) ? &DDRC : (((P) >= 18 && (P) <= 23)) ? &DDRF : (((P) == 7) ? &DDRE : &DDRB)))
322
+ ((((P) <= 4) || (P) == 6 || (P) == 12 || (P) == 24 || (P) == 25 || (P) == 29) ? &DDRD : (((P) == 5 || (P) == 13) ? &DDRC : (((P) >= 18 && (P) <= 23)) ? &DDRF : (((P) == 7) ? &DDRE : &DDRB)))
296
323
#define __digitalPinToPINReg (P ) \
297
- ((((P) >= 0 && (P) <= 4) || (P) == 6 || (P) == 12 || (P) == 24 || (P) == 25 || (P) == 29) ? &PIND : (((P) == 5 || (P) == 13) ? &PINC : (((P) >= 18 && (P) <= 23)) ? &PINF : (((P) == 7) ? &PINE : &PINB)))
324
+ ((((P) <= 4) || (P) == 6 || (P) == 12 || (P) == 24 || (P) == 25 || (P) == 29) ? &PIND : (((P) == 5 || (P) == 13) ? &PINC : (((P) >= 18 && (P) <= 23)) ? &PINF : (((P) == 7) ? &PINE : &PINB)))
298
325
#define __digitalPinToBit (P ) \
299
- (((P) >= 8 && (P) <= 11) ? (P) - 4 : (((P) >= 18 && (P) <= 21) ? 25 - (P) : (((P) == 0) ? 2 : (((P) == 1) ? 3 : (((P) == 2) ? 1 : (((P) == 3) ? 0 : (((P) == 4) ? 4 : (((P) == 6) ? 7 : (((P) == 13) ? 7 : (((P) == 14) ? 3 : (((P) == 15) ? 1 : (((P) == 16) ? 2 : (((P) == 17) ? 0 : (((P) == 22) ? 1 : (((P) == 23) ? 0 : (((P) == 24) ? 4 : (((P) == 25) ? 7 : (((P) == 26) ? 4 : (((P) == 27) ? 5 : 6 )))))))))))))))))))
300
-
326
+ (((P) >= 8 && (P) <= 11) ? (P) - 4 : \
327
+ (((P) >= 18 && (P) <= 21) ? 25 - (P) : \
328
+ (((P) == 0) ? 2 : (((P) == 1) ? 3 : (((P) == 2) ? 1 : (((P) == 3) ? 0 : (((P) == 4) ? 4 : (((P) == 6) ? 7 : (((P) == 13) ? 7 : \
329
+ (((P) == 14) ? 3 : (((P) == 15) ? 1 : (((P) == 16) ? 2 : (((P) == 17) ? 0 : (((P) == 22) ? 1 : (((P) == 23) ? 0 : \
330
+ (((P) == 24) ? 4 : (((P) == 25) ? 7 : (((P) == 26) ? 4 : (((P) == 27) ? 5 : 6 )))))))))))))))))))
331
+ # endif
301
332
302
333
// --- Arduino Uno and ATmega168/328 based boards ---
303
334
#elif (defined(ARDUINO_AVR_UNO ) || \
328
359
329
360
#if defined(__AVR_ATmega48PB__ ) || defined(__AVR_ATmega88PB__ ) || defined(__AVR_ATmega168PB__ ) || defined(__AVR_ATmega328PB__ )
330
361
#define __digitalPinToPortReg (P ) \
331
- (((P) >= 0 && (P) <= 7) ? &PORTD : (((P) >= 8 && (P) <= 13) ? &PORTB : (((P) >= 14 && (P) <= 19) ? &PORTC : &PORTE)))
362
+ (((P) <= 7) ? &PORTD : (((P) >= 8 && (P) <= 13) ? &PORTB : (((P) >= 14 && (P) <= 19) ? &PORTC : &PORTE)))
332
363
#define __digitalPinToDDRReg (P ) \
333
- (((P) >= 0 && (P) <= 7) ? &DDRD : (((P) >= 8 && (P) <= 13) ? &DDRB : (((P) >= 14 && (P) <= 19) ? &DDRC : &DDRE)))
364
+ (((P) <= 7) ? &DDRD : (((P) >= 8 && (P) <= 13) ? &DDRB : (((P) >= 14 && (P) <= 19) ? &DDRC : &DDRE)))
334
365
#define __digitalPinToPINReg (P ) \
335
- (((P) >= 0 && (P) <= 7) ? &PIND : (((P) >= 8 && (P) <= 13) ? &PINB : (((P) >= 14 && (P) <= 19) ? &PINC : &PINE)))
366
+ (((P) <= 7) ? &PIND : (((P) >= 8 && (P) <= 13) ? &PINB : (((P) >= 14 && (P) <= 19) ? &PINC : &PINE)))
336
367
#define __digitalPinToBit (P ) \
337
- (((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 13) ? (P) - 8 : (((P) >= 14 && (P) <= 19) ? (P) - 14 : (((P) >= 20 && (P) <= 21) ? (P) - 18 : (P) - 22))))
368
+ (((P) <= 7) ? (P) : (((P) >= 8 && (P) <= 13) ? (P) - 8 : (((P) >= 14 && (P) <= 19) ? (P) - 14 : (((P) >= 20 && (P) <= 21) ? (P) - 18 : (P) - 22))))
338
369
#else
339
370
#define __digitalPinToPortReg (P ) \
340
- (((P) >= 0 && (P) <= 7) ? &PORTD : (((P) >= 8 && (P) <= 13) ? &PORTB : &PORTC))
371
+ (((P) <= 7) ? &PORTD : (((P) >= 8 && (P) <= 13) ? &PORTB : &PORTC))
341
372
#define __digitalPinToDDRReg (P ) \
342
- (((P) >= 0 && (P) <= 7) ? &DDRD : (((P) >= 8 && (P) <= 13) ? &DDRB : &DDRC))
373
+ (((P) <= 7) ? &DDRD : (((P) >= 8 && (P) <= 13) ? &DDRB : &DDRC))
343
374
#define __digitalPinToPINReg (P ) \
344
- (((P) >= 0 && (P) <= 7) ? &PIND : (((P) >= 8 && (P) <= 13) ? &PINB : &PINC))
375
+ (((P) <= 7) ? &PIND : (((P) >= 8 && (P) <= 13) ? &PINB : &PINC))
345
376
#define __digitalPinToBit (P ) \
346
- (((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 13) ? (P) - 8 : (P) - 14))
377
+ (((P) <= 7) ? (P) : (((P) >= 8 && (P) <= 13) ? (P) - 8 : (P) - 14))
347
378
#endif
348
379
349
-
350
380
// --- Arduino Uno WiFi Rev 2, Nano Every ---
351
381
#elif defined(__AVR_ATmega4809__ )
352
382
437
467
#endif
438
468
//#endif //#if !defined(digitalPinToPortReg)
439
469
440
-
441
470
#if !defined(digitalWriteFast )
442
- #if (defined(__AVR__ ) || defined(ARDUINO_ARCH_AVR )) && defined(__digitalPinToPortReg )
471
+ # if (defined(__AVR__ ) || defined(ARDUINO_ARCH_AVR )) && defined(__digitalPinToPortReg )
443
472
#define digitalWriteFast (P , V ) \
444
473
if (__builtin_constant_p(P)) { \
445
474
BIT_WRITE(*__digitalPinToPortReg(P), __digitalPinToBit(P), (V)); \
446
475
} else { \
447
476
digitalWrite((P), (V)); \
448
477
}
449
- #else
478
+ # else
450
479
#define digitalWriteFast digitalWrite
480
+ # endif
451
481
#endif
452
- #endif
453
-
454
482
455
483
#if !defined(pinModeFast )
456
- #if (defined(__AVR__ ) || defined(ARDUINO_ARCH_AVR )) && defined(__digitalPinToPortReg )
484
+ # if (defined(__AVR__ ) || defined(ARDUINO_ARCH_AVR )) && defined(__digitalPinToPortReg )
457
485
#define pinModeFast (P , V ) \
458
486
if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
459
487
if (V == INPUT_PULLUP) {\
@@ -465,37 +493,35 @@ if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
465
493
} else { \
466
494
pinMode((P), (V)); \
467
495
}
468
- #else
496
+ # else
469
497
#define pinModeFast pinMode
470
- #endif
471
- #endif
472
-
498
+ # endif
499
+ #endif // !defined(pinModeFast)
473
500
474
501
#if !defined(digitalReadFast )
475
- #if (defined(__AVR__ ) || defined(ARDUINO_ARCH_AVR ))
502
+ # if (defined(__AVR__ ) || defined(ARDUINO_ARCH_AVR )) && defined( __digitalPinToPINReg )
476
503
#define digitalReadFast (P ) ( (int) __digitalReadFast((P)) )
504
+ // since we have return values, it is easier to implement it by ?:
477
505
#define __digitalReadFast (P ) \
478
506
(__builtin_constant_p(P) ) ? \
479
507
(( BIT_READ(*__digitalPinToPINReg(P), __digitalPinToBit(P))) ? HIGH:LOW ) : \
480
508
digitalRead((P))
481
- #else
509
+ # else
482
510
#define digitalReadFast digitalRead
483
- #endif
484
- #endif
485
-
511
+ # endif
512
+ #endif // !defined(digitalReadFast)
486
513
487
514
#if !defined(digitalToggleFast )
488
- #if (defined(__AVR__ ) || defined(ARDUINO_ARCH_AVR ))
515
+ # if (defined(__AVR__ ) || defined(ARDUINO_ARCH_AVR )) && defined( __digitalPinToPINReg )
489
516
#define digitalToggleFast (P ) \
490
517
if (__builtin_constant_p(P)) { \
491
518
BIT_SET(*__digitalPinToPINReg(P), __digitalPinToBit(P)); \
492
519
} else { \
493
520
digitalWrite(P, ! digitalRead(P)); \
494
521
}
495
- #else
522
+ # else
496
523
#define digitalToggleFast (P ) digitalWrite(P, ! digitalRead(P))
497
- #endif
498
- #endif
499
-
524
+ # endif
525
+ #endif // !defined(digitalToggleFast)
500
526
501
527
#endif //__digitalWriteFast_h_
0 commit comments