Arduino-GPIO
General Purpose Input/Output (GPIO) library for Arduino
Benchmark.ino
Go to the documentation of this file.
1 #include "GPIO.h"
2 #include "benchmark.h"
3 
4 #if defined(ARDUINO_attiny)
5 #include "Software/Serial.h"
9 #define LED_PIN 1
10 #define BUTTON_PIN 2
11 #else
14 #define BUTTON_PIN 12
15 #define LED_PIN 13
16 #endif
17 
18 void setup()
19 {
20  Serial.begin(57600);
21  while (!Serial);
22 
23  // Set pin mode; Arduino style
24  pinMode(LED_PIN, OUTPUT);
25  pinMode(BUTTON_PIN, INPUT_PULLUP);
26 
27  // Set pin mode; GPIO style
28  led.output();
29  button.input().pullup();
30 
31  // Calculate baseline
32  BENCHMARK_BASELINE(1000);
33 }
34 
35 void loop()
36 {
37  BENCHMARK("1.1 Arduino core digitalRead", 1000) {
38  bool state = digitalRead(BUTTON_PIN);
39  (void) state;
40  }
41 
42  BENCHMARK("1.2 Arduino core digitalWrite(HIGH)", 1000) {
43  digitalWrite(LED_PIN, HIGH);
44  }
45 
46  BENCHMARK("1.3 Arduino core digitalWrite(LOW)", 1000) {
47  digitalWrite(LED_PIN, LOW);
48  }
49 
50  BENCHMARK("1.4 Arduino core toggle; digitalRead-digitalWrite", 1000) {
51  digitalWrite(LED_PIN, !digitalRead(LED_PIN));
52  }
53 
54  BENCHMARK("1.5 Arduino core toggle; digitalWrite", 1000) {
55  digitalWrite(LED_PIN, HIGH);
56  digitalWrite(LED_PIN, LOW);
57  }
58 
59  BENCHMARK("2.1 GPIO pin value operator", 1000) {
60  bool state = button;
61  (void) state;
62  }
63 
64  BENCHMARK("2.2 GPIO high member function", 1000) {
65  led.high();
66  }
67 
68  BENCHMARK("2.3 GPIO low member function", 1000) {
69  led.low();
70  }
71 
72  BENCHMARK("2.4 GPIO assignment HIGH", 1000) {
73  led = HIGH;
74  }
75 
76  BENCHMARK("2.5 GPIO assignment LOW", 1000) {
77  led = LOW;
78  }
79 
80  BENCHMARK("2.6 GPIO toggle; value and assignment operator", 1000) {
81  led = !led;
82  }
83 
84  BENCHMARK("2.7 GPIO toggle; high and low member functions", 1000) {
85  led.high();
86  led.low();
87  }
88 
89  BENCHMARK("2.8 GPIO toggle; assignment operator", 1000) {
90  led = HIGH;
91  led = LOW;
92  }
93 
94  BENCHMARK("2.9 GPIO toggle member function", 1000) {
95  led.toggle();
96  }
97 
98 #ifdef PORTH
99 
100  // Benchmark#4: GPIO atomic access of io ports with higher address
101  // These benchmarks are for Arduino Mega pins that use ports above
102  // address 0x40 (PORTH, PORTJ, PINK and PINL). See Board.h.
103 
104 #define DIN_PIN 6
105 #define DOUT_PIN 7
106  GPIO<BOARD::D6> din;
107  GPIO<BOARD::D7> dout;
108  din.input();
109  dout.input();
110 
111  BENCHMARK("3.1 Arduino core digitalRead", 1000) {
112  bool state = digitalRead(DIN_PIN);
113  (void) state;
114  }
115 
116  BENCHMARK("3.2 Arduino core digitalWrite(HIGH)", 1000) {
117  digitalWrite(DOUT_PIN, HIGH);
118  }
119 
120  BENCHMARK("3.3 Arduino core digitalWrite(LOW)", 1000) {
121  digitalWrite(DOUT_PIN, LOW);
122  }
123 
124  BENCHMARK("3.4 Arduino core toggle; digitalRead-digitalWrite", 1000) {
125  digitalWrite(DOUT_PIN, !digitalRead(DOUT_PIN));
126  }
127 
128  BENCHMARK("3.5 Arduino core toggle; digitalWrite", 1000) {
129  digitalWrite(DOUT_PIN, HIGH);
130  digitalWrite(DOUT_PIN, LOW);
131  }
132 
133  BENCHMARK("4.1 GPIO pin value operator", 1000) {
134  bool state = din;
135  (void) state;
136  }
137 
138  BENCHMARK("4.2 GPIO high member function", 1000) {
139  dout.high();
140  }
141 
142  BENCHMARK("4.3 GPIO low member function", 1000) {
143  dout.low();
144  }
145 
146  BENCHMARK("4.4 GPIO assignment HIGH", 1000) {
147  dout = HIGH;
148  }
149 
150  BENCHMARK("4.5 GPIO assignment LOW", 1000) {
151  dout = LOW;
152  }
153 
154  BENCHMARK("4.6 GPIO toggle; value and assignment operator", 1000) {
155  dout = !dout;
156  }
157 
158  BENCHMARK("4.7 GPIO toggle; high and low member functions", 1000) {
159  dout.high();
160  dout.low();
161  }
162 
163  BENCHMARK("4.8 GPIO toggle; assignment operator", 1000) {
164  dout = HIGH;
165  dout = LOW;
166  }
167 
168  BENCHMARK("4.9 GPIO toggle member function", 1000) {
169  dout.toggle();
170  }
171 #endif
172 
173  Serial.println();
174  delay(2000);
175 }
void begin(uint32_t baudrate)
Definition: Serial.h:50
#define BUTTON_PIN
Definition: Benchmark.ino:14
void high()
Definition: GPIO.h:104
GPIO< BOARD::D12 > button
Definition: Benchmark.ino:12
GPIO< PIN > & input()
Definition: GPIO.h:36
void loop()
Definition: Benchmark.ino:35
void toggle()
Definition: GPIO.h:113
#define BENCHMARK(msg, scale)
Definition: benchmark.h:50
#define BENCHMARK_BASELINE(scale)
Definition: benchmark.h:33
void output()
Definition: GPIO.h:56
Definition: GPIO.h:31
void setup()
Definition: Benchmark.ino:18
#define LED_PIN
Definition: Benchmark.ino:15
void low()
Definition: GPIO.h:95
GPIO< BOARD::D13 > led
Definition: Benchmark.ino:13