Added Library DigitalWriteFast
This commit is contained in:
parent
e184a48612
commit
9addc78c04
Binary file not shown.
|
@ -0,0 +1,16 @@
|
|||
#include <digitalWriteFast.h>
|
||||
#define pinNum 9
|
||||
//int pinNum = 9; //do not use variables, macro will revert to the slower pinMode()
|
||||
//const int pinNum = 9; //this is a constant, will use port manipulation (fast)
|
||||
|
||||
void setup() {
|
||||
pinModeFast(pinNum, INPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
bool reading = digitalReadFast(pinNum);
|
||||
|
||||
if (reading == ERROR_SEQUENCE) { //ERROR_SEQUENCE defined as 0b10101010
|
||||
// pinNum is not a const and will always return as HIGH
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
#include <digitalWriteFast.h>
|
||||
#define pinNum 9
|
||||
//int pinNum = 9; //do not use variables, macro will revert to the slower digitalWrite()
|
||||
//const int pinNum = 9; //this is a constant, will use port manipulation (fast)
|
||||
void setup() {
|
||||
pinModeFast(pinNum, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//the pin is toggled multiple time before looping is because it took too long that the pin stayed low for 600ns, while clearing or setting the pin only took 125ns. For 16MHz Arduino Uno.
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#define pinNum 9
|
||||
//int pinNum = 9; //for regular digitalWrite(), does matter constant or variable
|
||||
//const int pinNum = 9; //this is a constant
|
||||
|
||||
void setup() {
|
||||
pinMode(pinNum, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//digitalWrite is so slow that , you don't actually need to toggle multiple times in 1 loop
|
||||
// because digitalWrite takes 6280ns but to return to the start of loop takes only 600ns.
|
||||
// this only makes a big deal when using digitalWriteFast(pinNum, state);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
digitalWrite(pinNum, HIGH);
|
||||
digitalWrite(pinNum, LOW);
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
# Notes
|
||||
|
||||
Notes down the observations, experiences and occorances during the development of the repo.
|
||||
|
||||
Useful for explaining certain decisions.
|
||||
|
||||
|
||||
###### speed of digitalWriteFast:
|
||||
|
||||
On Arduino Uno clone, 16MHz
|
||||
|
||||
digitalWriteFast is so fast that
|
||||
|
||||
```C++
|
||||
void loop() {
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
}
|
||||
```
|
||||
|
||||
The above did not result in 50% duty cycle.
|
||||
![Graph 1](graphs/1.png)
|
||||
|
||||
Rather, the pin stays low for quite some time until the program loops again.
|
||||
|
||||
Took around 600ns for the program to repeat
|
||||
|
||||
|
||||
So, the below was done instead:
|
||||
|
||||
```C++
|
||||
void loop() {
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
// ... and it goes on, multiple times
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
digitalWriteFast(pinNum, HIGH);
|
||||
digitalWriteFast(pinNum, LOW);
|
||||
}
|
||||
```
|
||||
|
||||
It only took 250ns to toggle, meaning it took only 250ns/2= **125ns** to set or clear the pin/port.
|
||||
![Graph 2](graphs/2.png)
|
||||
|
||||
###### speed of digitalWrite:
|
||||
|
||||
On Arduino Uno clone, 16MHz
|
||||
|
||||
It took 12.56us to toggle, meaning it took 12.56us/2= **6280ns** to set or clear the pin/port.
|
||||
|
||||
**50 times slower than direct port manipulation!**
|
||||
|
||||
![Graph 3](graphs/3.png)
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 45 KiB |
Binary file not shown.
After Width: | Height: | Size: 50 KiB |
Binary file not shown.
After Width: | Height: | Size: 45 KiB |
|
@ -0,0 +1,64 @@
|
|||
# digitalWriteFast
|
||||
Arduino library for faster `digitalWrite()` using direct port manipulation and macro for ease in pin assignments.
|
||||
Which actually also does faster `pinMode()` and `digitalRead()`.
|
||||
|
||||
## Usage
|
||||
Include the library:
|
||||
`#include <digitalWriteFast.h>`
|
||||
|
||||
Macro definitions:
|
||||
* `digitalWriteFast(pinNum, state)` (sets or clears pin/port faster)
|
||||
* `pinModeFast(pinNum, mode)` (sets pin/port as input or output faster)
|
||||
* `digitalReadFast(pinNum)`(reads the state of pin/port faster)
|
||||
|
||||
Parameters:
|
||||
* `pinNum` is the number written on the Arduino board.
|
||||
* `state` is weather pin is to be set `HIGH` or `LOW`
|
||||
* `mode` is weather pin is to be set `INPUT` or `OUTPUT`
|
||||
|
||||
In order to toggle fast, all the three parameters above must be constant or defined by the macro for ease in changes during compilation.
|
||||
|
||||
For example:
|
||||
* use '#define pinNum 10' instead of `int pinNum = 10;`
|
||||
* use 'const int pinNum 10' instead of `int pinNum = 10;`
|
||||
|
||||
Setting the parameter as a variable would cause the macro to return an error during compilation.
|
||||
|
||||
This makes sure `digitalWriteFast` that produces faster toggling, and notifies the programmer the specific area where toggling is slow. Otherwise, use normal `digitalWrite`
|
||||
|
||||
This is opposed to the forked library form Watterott, where if a variable is used as the parameter, the macro would revert to use sold `digitalWrite`, and remain undetected.
|
||||
|
||||
|
||||
## Speed
|
||||
|
||||
The regular `digitalWrite()` in Arduino Uno core (16MHz) takes about **6280nS** while `digitalWriteFast()` port manipulation takes **125nS**.
|
||||
> More info in: [/NOTES/NOTES.md](/NOTES/NOTES.md)
|
||||
|
||||
This is a huge difference, especially or timing sensitive applications.
|
||||
|
||||
Direct port manipulation is troublesome where one has to refer to the pin assignment of the package and manipulate specific ports, instead of pin numbers on the Arduino board.
|
||||
|
||||
This library makes it easier by using `digitalWriteFast()` and the macro will replace it will the approritate port manipulation commands.
|
||||
|
||||
## Compatibility
|
||||
* Arduino Due
|
||||
* Arduino Zero
|
||||
* Arduino Mega
|
||||
* Arduino with ATmega644 or Atmega644P chip
|
||||
* Arduino Leonardo
|
||||
* Arduino Uno (I have only tested with uno)
|
||||
|
||||
If not in the list, the macro will revert back to `digitalWrite()`, `pinMode()` or `digitalRead()`
|
||||
|
||||
## Installation
|
||||
1. Download the repo as zip, extract and place into Arduino IDE libraries folder.
|
||||
2. Rename "digitalWriteFast-master" to "digitalWriteFast" Arduino IDE does not accept dash character.
|
||||
|
||||
|
||||
## Reference
|
||||
Fork of Watterott's https://github.com/watterott/Arduino-Libs/tree/master/digitalWriteFast
|
||||
I just forked the whole repo, and delete unrelated files, I tried sparse checkout and gave up.
|
||||
|
||||
Watterott's digitalWriteFast could have used the below links as referrence.
|
||||
* https://code.google.com/archive/p/digitalwritefast/downloads
|
||||
* http://forum.arduino.cc/index.php?topic=46896.0
|
|
@ -0,0 +1,353 @@
|
|||
/*
|
||||
Optimized digital functions for AVR microcontrollers
|
||||
by Watterott electronic (www.watterott.com)
|
||||
based on http://code.google.com/p/digitalwritefast
|
||||
*/
|
||||
|
||||
#ifndef __digitalWriteFast_h_
|
||||
#define __digitalWriteFast_h_ 1
|
||||
|
||||
#define ERROR_SEQUENCE 0b10101010 //digitalReadFast will return this value if pin number is not constant
|
||||
// general macros/defines
|
||||
#ifndef BIT_READ
|
||||
# define BIT_READ(value, bit) ((value) & (1UL << (bit)))
|
||||
#endif
|
||||
#ifndef BIT_SET
|
||||
# define BIT_SET(value, bit) ((value) |= (1UL << (bit)))
|
||||
#endif
|
||||
#ifndef BIT_CLEAR
|
||||
# define BIT_CLEAR(value, bit) ((value) &= ~(1UL << (bit)))
|
||||
#endif
|
||||
#ifndef BIT_WRITE
|
||||
# define BIT_WRITE(value, bit, bitvalue) (bitvalue ? BIT_SET(value, bit) : BIT_CLEAR(value, bit))
|
||||
#endif
|
||||
|
||||
#ifndef SWAP
|
||||
#define SWAP(x,y) do{ (x)=(x)^(y); (y)=(x)^(y); (x)=(x)^(y); }while(0)
|
||||
#endif
|
||||
|
||||
/* //not needed, rather it produces annoying warnings when compiled
|
||||
#ifndef DEC
|
||||
# define DEC (10)
|
||||
#endif
|
||||
#ifndef HEX
|
||||
# define HEX (16)
|
||||
#endif
|
||||
#ifndef OCT
|
||||
# define OCT (8)
|
||||
#endif
|
||||
#ifndef BIN
|
||||
# define BIN (2)
|
||||
#endif
|
||||
*/
|
||||
|
||||
// workarounds for ARM microcontrollers
|
||||
#if (!defined(__AVR__) || defined(ARDUINO_ARCH_SAM))
|
||||
#ifndef PROGMEM
|
||||
# define PROGMEM
|
||||
#endif
|
||||
#ifndef PGM_P
|
||||
# define PGM_P const char *
|
||||
#endif
|
||||
#ifndef PSTR
|
||||
# define PSTR(str) (str)
|
||||
#endif
|
||||
|
||||
#ifndef memcpy_P
|
||||
# define memcpy_P(dest, src, num) memcpy((dest), (src), (num))
|
||||
#endif
|
||||
#ifndef strcpy_P
|
||||
# define strcpy_P(dst, src) strcpy((dst), (src))
|
||||
#endif
|
||||
#ifndef strcat_P
|
||||
# define strcat_P(dst, src) strcat((dst), (src))
|
||||
#endif
|
||||
#ifndef strcmp_P
|
||||
# define strcmp_P(a, b) strcmp((a), (b))
|
||||
#endif
|
||||
#ifndef strcasecmp_P
|
||||
# define strcasecmp_P(a, b) strcasecmp((a), (b))
|
||||
#endif
|
||||
#ifndef strncmp_P
|
||||
# define strncmp_P(a, b, n) strncmp((a), (b), (n))
|
||||
#endif
|
||||
#ifndef strncasecmp_P
|
||||
# define strncasecmp_P(a, b, n) strncasecmp((a), (b), (n))
|
||||
#endif
|
||||
#ifndef strstr_P
|
||||
# define strstr_P(a, b) strstr((a), (b))
|
||||
#endif
|
||||
#ifndef strlen_P
|
||||
# define strlen_P(a) strlen((a))
|
||||
#endif
|
||||
#ifndef sprintf_P
|
||||
# define sprintf_P(s, f, ...) sprintf((s), (f), __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#ifndef pgm_read_byte
|
||||
# define pgm_read_byte(addr) (*(const unsigned char *)(addr))
|
||||
#endif
|
||||
#ifndef pgm_read_word
|
||||
# define pgm_read_word(addr) (*(const unsigned short *)(addr))
|
||||
#endif
|
||||
#ifndef pgm_read_dword
|
||||
# define pgm_read_dword(addr) (*(const unsigned long *)(addr))
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// digital functions
|
||||
//#ifndef digitalPinToPortReg
|
||||
#define SPI_SW_SS_PIN (10) //SS on Uno (for software SPI)
|
||||
#define SPI_SW_MOSI_PIN (11) //MOSI on Uno (for software SPI)
|
||||
#define SPI_SW_MISO_PIN (12) //MISO on Uno (for software SPI)
|
||||
#define SPI_SW_SCK_PIN (13) //SCK on Uno (for software SPI)
|
||||
|
||||
|
||||
// --- Arduino Due ---
|
||||
#if (defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__))
|
||||
|
||||
#define UART_RX_PIN (0)
|
||||
#define UART_TX_PIN (1)
|
||||
|
||||
#define I2C_SDA_PIN (20)
|
||||
#define I2C_SCL_PIN (21)
|
||||
|
||||
#define SPI_HW_SS_PIN (78) //SS0:77, SS1:87, SS2:86, SS3:78
|
||||
#define SPI_HW_MOSI_PIN (75) //75
|
||||
#define SPI_HW_MISO_PIN (74) //74
|
||||
#define SPI_HW_SCK_PIN (76) //76
|
||||
|
||||
|
||||
// --- Arduino Zero ---
|
||||
#elif (defined(ARDUINO_SAM_ZERO) || defined(__SAMD21G18A__))
|
||||
|
||||
#define UART_RX_PIN (0)
|
||||
#define UART_TX_PIN (1)
|
||||
|
||||
#define I2C_SDA_PIN (16)
|
||||
#define I2C_SCL_PIN (17)
|
||||
|
||||
#define SPI_HW_SS_PIN (14) //14
|
||||
#define SPI_HW_MOSI_PIN (21) //21
|
||||
#define SPI_HW_MISO_PIN (18) //18
|
||||
#define SPI_HW_SCK_PIN (20) //20
|
||||
|
||||
|
||||
// --- Arduino Mega ---
|
||||
#elif (defined(ARDUINO_AVR_MEGA) || \
|
||||
defined(ARDUINO_AVR_MEGA1280) || \
|
||||
defined(ARDUINO_AVR_MEGA2560) || \
|
||||
defined(__AVR_ATmega1280__) || \
|
||||
defined(__AVR_ATmega1281__) || \
|
||||
defined(__AVR_ATmega2560__) || \
|
||||
defined(__AVR_ATmega2561__))
|
||||
|
||||
#define UART_RX_PIN (0) //PE0
|
||||
#define UART_TX_PIN (1) //PE1
|
||||
|
||||
#define I2C_SDA_PIN (20)
|
||||
#define I2C_SCL_PIN (21)
|
||||
|
||||
#define SPI_HW_SS_PIN (53) //PB0
|
||||
#define SPI_HW_MOSI_PIN (51) //PB2
|
||||
#define SPI_HW_MISO_PIN (50) //PB3
|
||||
#define SPI_HW_SCK_PIN (52) //PB1
|
||||
|
||||
#define __digitalPinToPortReg(P) \
|
||||
(((P) >= 22 && (P) <= 29) ? &PORTA : \
|
||||
((((P) >= 10 && (P) <= 13) || ((P) >= 50 && (P) <= 53)) ? &PORTB : \
|
||||
(((P) >= 30 && (P) <= 37) ? &PORTC : \
|
||||
((((P) >= 18 && (P) <= 21) || (P) == 38) ? &PORTD : \
|
||||
((((P) >= 0 && (P) <= 3) || (P) == 5) ? &PORTE : \
|
||||
(((P) >= 54 && (P) <= 61) ? &PORTF : \
|
||||
((((P) >= 39 && (P) <= 41) || (P) == 4) ? &PORTG : \
|
||||
((((P) >= 6 && (P) <= 9) || (P) == 16 || (P) == 17) ? &PORTH : \
|
||||
(((P) == 14 || (P) == 15) ? &PORTJ : \
|
||||
(((P) >= 62 && (P) <= 69) ? &PORTK : &PORTL))))))))))
|
||||
|
||||
#define __digitalPinToDDRReg(P) \
|
||||
(((P) >= 22 && (P) <= 29) ? &DDRA : \
|
||||
((((P) >= 10 && (P) <= 13) || ((P) >= 50 && (P) <= 53)) ? &DDRB : \
|
||||
(((P) >= 30 && (P) <= 37) ? &DDRC : \
|
||||
((((P) >= 18 && (P) <= 21) || (P) == 38) ? &DDRD : \
|
||||
((((P) >= 0 && (P) <= 3) || (P) == 5) ? &DDRE : \
|
||||
(((P) >= 54 && (P) <= 61) ? &DDRF : \
|
||||
((((P) >= 39 && (P) <= 41) || (P) == 4) ? &DDRG : \
|
||||
((((P) >= 6 && (P) <= 9) || (P) == 16 || (P) == 17) ? &DDRH : \
|
||||
(((P) == 14 || (P) == 15) ? &DDRJ : \
|
||||
(((P) >= 62 && (P) <= 69) ? &DDRK : &DDRL))))))))))
|
||||
|
||||
#define __digitalPinToPINReg(P) \
|
||||
(((P) >= 22 && (P) <= 29) ? &PINA : \
|
||||
((((P) >= 10 && (P) <= 13) || ((P) >= 50 && (P) <= 53)) ? &PINB : \
|
||||
(((P) >= 30 && (P) <= 37) ? &PINC : \
|
||||
((((P) >= 18 && (P) <= 21) || (P) == 38) ? &PIND : \
|
||||
((((P) >= 0 && (P) <= 3) || (P) == 5) ? &PINE : \
|
||||
(((P) >= 54 && (P) <= 61) ? &PINF : \
|
||||
((((P) >= 39 && (P) <= 41) || (P) == 4) ? &PING : \
|
||||
((((P) >= 6 && (P) <= 9) || (P) == 16 || (P) == 17) ? &PINH : \
|
||||
(((P) == 14 || (P) == 15) ? &PINJ : \
|
||||
(((P) >= 62 && (P) <= 69) ? &PINK : &PINL))))))))))
|
||||
|
||||
#define __digitalPinToBit(P) \
|
||||
(((P) >= 7 && (P) <= 9) ? (P) - 3 : \
|
||||
(((P) >= 10 && (P) <= 13) ? (P) - 6 : \
|
||||
(((P) >= 22 && (P) <= 29) ? (P) - 22 : \
|
||||
(((P) >= 30 && (P) <= 37) ? 37 - (P) : \
|
||||
(((P) >= 39 && (P) <= 41) ? 41 - (P) : \
|
||||
(((P) >= 42 && (P) <= 49) ? 49 - (P) : \
|
||||
(((P) >= 50 && (P) <= 53) ? 53 - (P) : \
|
||||
(((P) >= 54 && (P) <= 61) ? (P) - 54 : \
|
||||
(((P) >= 62 && (P) <= 69) ? (P) - 62 : \
|
||||
(((P) == 0 || (P) == 15 || (P) == 17 || (P) == 21) ? 0 : \
|
||||
(((P) == 1 || (P) == 14 || (P) == 16 || (P) == 20) ? 1 : \
|
||||
(((P) == 19) ? 2 : \
|
||||
(((P) == 5 || (P) == 6 || (P) == 18) ? 3 : \
|
||||
(((P) == 2) ? 4 : \
|
||||
(((P) == 3 || (P) == 4) ? 5 : 7)))))))))))))))
|
||||
|
||||
|
||||
// --- Arduino 644 ---
|
||||
#elif (defined(__AVR_ATmega644__) || \
|
||||
defined(__AVR_ATmega644P__))
|
||||
|
||||
#define UART_RX_PIN (8) //PD0
|
||||
#define UART_TX_PIN (9) //PD1
|
||||
|
||||
#define I2C_SDA_PIN (17) //PC1
|
||||
#define I2C_SCL_PIN (16) //PC0
|
||||
|
||||
#define SPI_HW_SS_PIN (4) //PB4
|
||||
#define SPI_HW_MOSI_PIN (5) //PB5
|
||||
#define SPI_HW_MISO_PIN (6) //PB6
|
||||
#define SPI_HW_SCK_PIN (7) //PB7
|
||||
|
||||
#define __digitalPinToPortReg(P) \
|
||||
(((P) >= 0 && (P) <= 7) ? &PORTB : (((P) >= 8 && (P) <= 15) ? &PORTD : (((P) >= 16 && (P) <= 23) ? &PORTC : &PORTA)))
|
||||
#define __digitalPinToDDRReg(P) \
|
||||
(((P) >= 0 && (P) <= 7) ? &DDRB : (((P) >= 8 && (P) <= 15) ? &DDRD : (((P) >= 8 && (P) <= 15) ? &DDRC : &DDRA)))
|
||||
#define __digitalPinToPINReg(P) \
|
||||
(((P) >= 0 && (P) <= 7) ? &PINB : (((P) >= 8 && (P) <= 15) ? &PIND : (((P) >= 8 && (P) <= 15) ? &PINC : &PINA)))
|
||||
#define __digitalPinToBit(P) \
|
||||
(((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 15) ? (P) - 8 : (((P) >= 16 && (P) <= 23) ? (P) - 16 : (P) - 24)))
|
||||
|
||||
|
||||
// --- Arduino Leonardo ---
|
||||
#elif (defined(ARDUINO_AVR_LEONARDO) || \
|
||||
defined(__AVR_ATmega16U4__) || \
|
||||
defined(__AVR_ATmega32U4__))
|
||||
|
||||
#define UART_RX_PIN (0) //PD2
|
||||
#define UART_TX_PIN (1) //PD3
|
||||
|
||||
#define I2C_SDA_PIN (2) //PD1
|
||||
#define I2C_SCL_PIN (3) //PD0
|
||||
|
||||
#define SPI_HW_SS_PIN (17) //PB0
|
||||
#define SPI_HW_MOSI_PIN (16) //PB2
|
||||
#define SPI_HW_MISO_PIN (14) //PB3
|
||||
#define SPI_HW_SCK_PIN (15) //PB1
|
||||
|
||||
#define __digitalPinToPortReg(P) \
|
||||
((((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)))
|
||||
#define __digitalPinToDDRReg(P) \
|
||||
((((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)))
|
||||
#define __digitalPinToPINReg(P) \
|
||||
((((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)))
|
||||
#define __digitalPinToBit(P) \
|
||||
(((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 )))))))))))))))))))
|
||||
|
||||
|
||||
// --- Arduino Uno ---
|
||||
#elif (defined(ARDUINO_AVR_UNO) || \
|
||||
defined(ARDUINO_AVR_DUEMILANOVE) || \
|
||||
defined(ARDUINO_ARCH_AVR) || \
|
||||
defined(__AVR_ATmega328__) || \
|
||||
defined(__AVR_ATmega328P__) || \
|
||||
defined(__AVR__))
|
||||
|
||||
#define UART_RX_PIN (0) //PD0
|
||||
#define UART_TX_PIN (1) //PD1
|
||||
|
||||
#define I2C_SDA_PIN (18) //A4
|
||||
#define I2C_SCL_PIN (19) //A5
|
||||
|
||||
#define SPI_HW_SS_PIN (10) //PB0
|
||||
#define SPI_HW_MOSI_PIN (11) //PB2
|
||||
#define SPI_HW_MISO_PIN (12) //PB3
|
||||
#define SPI_HW_SCK_PIN (13) //PB1
|
||||
|
||||
#define __digitalPinToPortReg(P) \
|
||||
(((P) >= 0 && (P) <= 7) ? &PORTD : (((P) >= 8 && (P) <= 13) ? &PORTB : &PORTC))
|
||||
#define __digitalPinToDDRReg(P) \
|
||||
(((P) >= 0 && (P) <= 7) ? &DDRD : (((P) >= 8 && (P) <= 13) ? &DDRB : &DDRC))
|
||||
#define __digitalPinToPINReg(P) \
|
||||
(((P) >= 0 && (P) <= 7) ? &PIND : (((P) >= 8 && (P) <= 13) ? &PINB : &PINC))
|
||||
#define __digitalPinToBit(P) \
|
||||
(((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 13) ? (P) - 8 : (P) - 14))
|
||||
|
||||
|
||||
// --- Other ---
|
||||
#else
|
||||
|
||||
#define SPI_HW_SS_PIN SS
|
||||
#define SPI_HW_MOSI_PIN MOSI
|
||||
#define SPI_HW_MISO_PIN MISO
|
||||
#define SPI_HW_SCK_PIN SCK
|
||||
|
||||
|
||||
#endif
|
||||
//#endif //#ifndef digitalPinToPortReg
|
||||
|
||||
|
||||
//ref: http://forum.arduino.cc/index.php?topic=140409.msg1054868#msg1054868
|
||||
//void OutputsErrorIfCalled( void ) __attribute__ (( error( "Line: "__line__ "Variable used for digitalWriteFast") ));
|
||||
void NonConstantUsed( void ) __attribute__ (( error("") ));
|
||||
|
||||
|
||||
#ifndef digitalWriteFast
|
||||
#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
|
||||
#define digitalWriteFast(P, V) \
|
||||
if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
|
||||
BIT_WRITE(*__digitalPinToPortReg(P), __digitalPinToBit(P), (V)); \
|
||||
} else { \
|
||||
NonConstantUsed(); \
|
||||
}
|
||||
#else
|
||||
//#define digitalWriteFast digitalWrite
|
||||
#error Non-AVR device, unsupported.
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef pinModeFast
|
||||
#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
|
||||
#define pinModeFast(P, V) \
|
||||
if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
|
||||
BIT_WRITE(*__digitalPinToDDRReg(P), __digitalPinToBit(P), (V)); \
|
||||
} else { \
|
||||
NonConstantUsed(); \
|
||||
}
|
||||
#else
|
||||
//#define pinModeFast pinMode
|
||||
#error Non-AVR device, unsupported.
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef digitalReadFast
|
||||
#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
|
||||
#define digitalReadFast(P) ( (byte) __digitalReadFast((P)) )
|
||||
#define __digitalReadFast(P ) \
|
||||
(__builtin_constant_p(P) ) ? ( \
|
||||
( BIT_READ(*__digitalPinToPINReg(P), __digitalPinToBit(P))) ? HIGH:LOW ) : \
|
||||
ERROR_SEQUENCE
|
||||
#else
|
||||
//#define digitalReadFast digitalRead
|
||||
#error Non-AVR device, unsupported.
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif //__digitalWriteFast_h_
|
Loading…
Reference in New Issue