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 #include <stdbool.h>
80 
81 #include "periph_cpu.h"
82 #include "periph_conf.h"
83 
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87 
88 #ifndef HAVE_GPIO_T
92 typedef unsigned int gpio_t;
93 #endif
94 
95 #ifndef GPIO_PIN
99 /* Default GPIO macro maps port-pin tuples to the pin value */
100 #define GPIO_PIN(x,y) ((gpio_t)((x & 0) | y))
101 #endif
102 
103 #ifndef GPIO_UNDEF
107 #define GPIO_UNDEF ((gpio_t)(UINT_MAX))
108 #endif
109 
118 #ifndef HAVE_GPIO_MODE_T
119 typedef enum {
124  GPIO_OD,
126  GPIO_OD_PU
129 #endif
130 
134 #ifndef HAVE_GPIO_FLANK_T
135 typedef enum {
138  GPIO_BOTH = 2
140 #endif
141 
147 typedef void (*gpio_cb_t)(void *arg);
148 
152 #ifndef HAVE_GPIO_ISR_CTX_T
153 typedef struct {
155  void *arg;
157 #endif
158 
172 int gpio_init(gpio_t pin, gpio_mode_t mode);
173 
174 #if defined(MODULE_PERIPH_GPIO_IRQ) || defined(DOXYGEN)
197 int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
198  gpio_cb_t cb, void *arg);
199 
211 void gpio_irq_enable(gpio_t pin);
212 
221 void gpio_irq_disable(gpio_t pin);
222 
223 #endif /* defined(MODULE_PERIPH_GPIO_IRQ) || defined(DOXYGEN) */
224 
233 bool gpio_read(gpio_t pin);
234 
240 void gpio_set(gpio_t pin);
241 
247 void gpio_clear(gpio_t pin);
248 
254 void gpio_toggle(gpio_t pin);
255 
262 void gpio_write(gpio_t pin, bool value);
263 
270 static inline int gpio_is_equal(gpio_t gpio1, gpio_t gpio2)
271 {
272  return (gpio1 == gpio2);
273 }
274 
280 static inline int gpio_is_valid(gpio_t gpio)
281 {
282  return (gpio != GPIO_UNDEF);
283 }
284 
285 #ifdef __cplusplus
286 }
287 #endif
288 
289 #endif /* PERIPH_GPIO_H */
gpio_flank_t
Definition: periph_cpu.h:180
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:91
void gpio_toggle(gpio_t pin)
Toggle the value of the given pin.
void(* gpio_cb_t)(void *arg)
Signature of event callback functions triggered from interrupts.
Definition: gpio.h:147
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:280
gpio_flank_t
Definition of possible active flanks for external interrupt mode.
Definition: gpio.h:135
void gpio_clear(gpio_t pin)
Set the given pin to LOW.
void gpio_write(gpio_t pin, bool value)
Set the given pin to the given value.
#define GPIO_UNDEF
GPIO pin not defined.
Definition: gpio.h:107
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.
gpio_mode_t
Available pin modes.
Definition: gpio.h:119
void gpio_set(gpio_t pin)
Set the given pin to HIGH.
void gpio_irq_disable(gpio_t pin)
Disable the pin interrupt if configured as interrupt source.
void gpio_irq_enable(gpio_t pin)
Enable pin interrupt if configured as interrupt source.
bool gpio_read(gpio_t pin)
Get the current value of the given pin.
int gpio_init(gpio_t pin, gpio_mode_t mode)
Initialize the given pin as general purpose input or output.
unsigned int gpio_t
GPIO type identifier.
Definition: gpio.h:92
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:270
@ GPIO_FALLING
emit interrupt on falling flank
Definition: gpio.h:136
@ GPIO_RISING
emit interrupt on rising flank
Definition: gpio.h:137
@ GPIO_BOTH
emit interrupt on both flanks
Definition: gpio.h:138
@ GPIO_OUT
configure as output in push-pull mode
Definition: gpio.h:123
@ GPIO_IN
configure as input without pull resistor
Definition: gpio.h:120
@ GPIO_OD
configure as output in open-drain mode without pull resistor
Definition: gpio.h:124
@ GPIO_IN_PU
configure as input with pull-up resistor
Definition: gpio.h:122
@ GPIO_OD_PU
configure as output in open-drain mode with pull resistor enabled
Definition: gpio.h:126
@ GPIO_IN_PD
configure as input with pull-down resistor
Definition: gpio.h:121
Default interrupt context for GPIO pins.
Definition: gpio.h:153
void * arg
optional argument
Definition: gpio.h:155
gpio_cb_t cb
interrupt callback
Definition: gpio.h:154