gpio.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Freie Universit├Ąt Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
75 #ifndef PERIPH_GPIO_H
76 #define PERIPH_GPIO_H
77 
78 #include <limits.h>
79 
80 #include "periph_cpu.h"
81 #include "periph_conf.h"
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
87 #ifndef HAVE_GPIO_T
88 
91 typedef unsigned int gpio_t;
92 #endif
93 
94 #ifndef GPIO_PIN
95 
98 /* Default GPIO macro maps port-pin tuples to the pin value */
99 #define GPIO_PIN(x,y) ((gpio_t)((x & 0) | y))
100 #endif
101 
102 #ifndef GPIO_UNDEF
103 
106 #define GPIO_UNDEF ((gpio_t)(UINT_MAX))
107 #endif
108 
117 #ifndef HAVE_GPIO_MODE_T
118 typedef enum {
127 } gpio_mode_t;
128 #endif
129 
133 #ifndef HAVE_GPIO_FLANK_T
134 typedef enum {
138 } gpio_flank_t;
139 #endif
140 
146 typedef void (*gpio_cb_t)(void *arg);
147 
151 #ifndef HAVE_GPIO_ISR_CTX_T
152 typedef struct {
154  void *arg;
156 #endif
157 
171 int gpio_init(gpio_t pin, gpio_mode_t mode);
172 
173 #if defined(MODULE_PERIPH_GPIO_IRQ) || defined(DOXYGEN)
174 
194 int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
195  gpio_cb_t cb, void *arg);
196 
208 void gpio_irq_enable(gpio_t pin);
209 
218 void gpio_irq_disable(gpio_t pin);
219 
220 #endif /* defined(MODULE_PERIPH_GPIO_IRQ) || defined(DOXYGEN) */
221 
230 int gpio_read(gpio_t pin);
231 
237 void gpio_set(gpio_t pin);
238 
244 void gpio_clear(gpio_t pin);
245 
251 void gpio_toggle(gpio_t pin);
252 
259 void gpio_write(gpio_t pin, int value);
260 
267 static inline int gpio_is_equal(gpio_t gpio1, gpio_t gpio2)
268 {
269  return (gpio1 == gpio2);
270 }
271 
277 static inline int gpio_is_valid(gpio_t gpio)
278 {
279  return (gpio != GPIO_UNDEF);
280 }
281 
282 #ifdef __cplusplus
283 }
284 #endif
285 
286 #endif /* PERIPH_GPIO_H */
287 
configure as output in push-pull mode
Definition: gpio.h:122
emit interrupt on rising flank
Definition: gpio.h:136
void gpio_toggle(gpio_t pin)
Toggle the value of the given pin.
void * arg
optional argument
Definition: gpio.h:154
gpio_flank_t
Definition: periph_cpu.h:80
emit interrupt on both flanks
Definition: gpio.h:137
void gpio_irq_enable(gpio_t pin)
Enable pin interrupt if configured as interrupt source.
gpio_flank_t
Definition of possible active flanks for external interrupt mode.
Definition: gpio.h:134
Default interrupt context for GPIO pins.
Definition: gpio.h:152
int gpio_init(gpio_t pin, gpio_mode_t mode)
Initialize the given pin as general purpose input or output.
int gpio_read(gpio_t pin)
Get the current value of the given pin.
unsigned int gpio_t
GPIO type identifier.
Definition: gpio.h:91
gpio_cb_t cb
interrupt callback
Definition: gpio.h:153
#define GPIO_UNDEF
GPIO pin not defined.
Definition: gpio.h:106
void gpio_set(gpio_t pin)
Set the given pin to HIGH.
emit interrupt on falling flank
Definition: gpio.h:135
static int gpio_is_valid(gpio_t gpio)
Test if a GPIO pin is a valid pin and not declared as undefined.
Definition: gpio.h:277
configure as input with pull-up resistor
Definition: gpio.h:121
static int gpio_is_equal(gpio_t gpio1, gpio_t gpio2)
Test if a GPIO pin is equal to another GPIO pin.
Definition: gpio.h:267
void gpio_irq_disable(gpio_t pin)
Disable the pin interrupt if configured as interrupt source.
gpio_mode_t
Available pin modes.
Definition: gpio.h:118
configure as input without pull resistor
Definition: gpio.h:119
void gpio_write(gpio_t pin, int value)
Set the given pin to the given value.
configure as output in open-drain mode without pull resistor
Definition: gpio.h:123
configure as input with pull-down resistor
Definition: gpio.h:120
configure as output in open-drain mode with pull resistor enabled
Definition: gpio.h:125
int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg)
Initialize a GPIO pin for external interrupt usage.
void(* gpio_cb_t)(void *arg)
Signature of event callback functions triggered from interrupts.
Definition: gpio.h:146
void gpio_clear(gpio_t pin)
Set the given pin to LOW.