Arduino-GPIO
General Purpose Input/Output (GPIO) library for Arduino
GPIO.h
Go to the documentation of this file.
1 
19 #ifndef HARDWARE_AVR_GPIO_H
20 #define HARDWARE_AVR_GPIO_H
21 
22 #include "Hardware/AVR/Board.h"
23 
30 template<BOARD::pin_t PIN>
31 class GPIO {
32 public:
37  __attribute__((always_inline))
38  {
39  GPIO_ATOMIC(SFR()->ddr &= ~MASK);
40  return (*this);
41  }
42 
47  void pullup()
48  __attribute__((always_inline))
49  {
50  high();
51  }
52 
56  void output()
57  __attribute__((always_inline))
58  {
59  GPIO_ATOMIC(SFR()->ddr |= MASK);
60  }
61 
65  void open_drain()
66  __attribute__((always_inline))
67  {
68  input();
69  low();
70  }
71 
76  bool read()
77  __attribute__((always_inline))
78  {
79  return ((SFR()->pin & MASK) != 0);
80  }
81 
86  operator bool()
87  __attribute__((always_inline))
88  {
89  return (read());
90  }
91 
95  void low()
96  __attribute__((always_inline))
97  {
98  GPIO_ATOMIC(SFR()->port &= ~MASK);
99  }
100 
104  void high()
105  __attribute__((always_inline))
106  {
107  GPIO_ATOMIC(SFR()->port |= MASK);
108  }
109 
113  void toggle()
114  __attribute__((always_inline))
115  {
116  GPIO_ATOMIC(SFR()->pin |= MASK);
117  }
118 
124  void write(int value)
125  __attribute__((always_inline))
126  {
127  if (value) high(); else low();
128  }
129 
135  void operator=(int value)
136  __attribute__((always_inline))
137  {
138  write(value);
139  }
140 
146  void pulse(uint16_t width)
147  {
148  if (width == 0) return;
149  uint16_t count = ((width * (F_CPU / 1000000L)) / 4);
150  uint8_t sreg = SREG;
151  __asm__ __volatile__("cli" ::: "memory");
152  SFR()->pin |= MASK;
153  _delay_loop_2(count);
154  SFR()->pin |= MASK;
155  SREG = sreg;
156  __asm__ __volatile__("" ::: "memory");
157  }
158 
163  int pulse()
164  {
165  bool s0 = read();
166  while (read() == s0);
167  uint16_t t0 = micros();
168  while (read() != s0);
169  return (micros() - t0);
170  }
171 
172 protected:
174  struct gpio_reg_t {
175  volatile uint8_t pin;
176  volatile uint8_t ddr;
177  volatile uint8_t port;
178  };
179 
184  gpio_reg_t* SFR()
185  __attribute__((always_inline))
186  {
187  return ((gpio_reg_t*) GPIO_REG(PIN));
188  }
189 
191  static const uint8_t MASK = GPIO_MASK(PIN);
192 };
193 #endif
void pullup()
Definition: GPIO.h:47
volatile uint8_t port
Data Register.
Definition: GPIO.h:177
volatile uint8_t pin
Port Input Pins.
Definition: GPIO.h:175
#define GPIO_ATOMIC(expr)
Definition: Board.h:51
void high()
Definition: GPIO.h:104
GPIO< PIN > & input()
Definition: GPIO.h:36
volatile uint8_t ddr
Data Direction Register.
Definition: GPIO.h:176
void operator=(int value)
Definition: GPIO.h:135
void toggle()
Definition: GPIO.h:113
void open_drain()
Definition: GPIO.h:65
void write(int value)
Definition: GPIO.h:124
#define GPIO_MASK(pin)
Definition: Board.h:40
static const uint8_t MASK
Definition: GPIO.h:191
#define GPIO_REG(pin)
Definition: Board.h:34
int pulse()
Definition: GPIO.h:163
void output()
Definition: GPIO.h:56
bool read()
Definition: GPIO.h:76
Definition: GPIO.h:31
void low()
Definition: GPIO.h:95
gpio_reg_t * SFR()
Definition: GPIO.h:184
void pulse(uint16_t width)
Definition: GPIO.h:146