Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Freie Universität Berlin
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
73
74#include <limits.h>
75#include <stdbool.h>
76
77#include "periph_cpu.h"
78#include "periph_conf.h"
79
80#ifdef __cplusplus
81extern "C" {
82#endif
83
84#ifndef HAVE_GPIO_T
88typedef unsigned int gpio_t;
89#endif
90
91#ifndef GPIO_PIN
95/* Default GPIO macro maps port-pin tuples to the pin value */
96#define GPIO_PIN(x,y) ((gpio_t)((x & 0) | y))
97#endif
98
99#ifndef GPIO_UNDEF
103#define GPIO_UNDEF ((gpio_t)(UINT_MAX))
104#endif
105
114#ifndef HAVE_GPIO_MODE_T
115typedef enum {
117 GPIO_IN_PD,
118 GPIO_IN_PU,
120 GPIO_OD,
122 GPIO_OD_PU
125#endif
126
130#ifndef HAVE_GPIO_FLANK_T
131typedef enum {
132 GPIO_FALLING = 0,
133 GPIO_RISING = 1,
134 GPIO_BOTH = 2
136#endif
137
143typedef void (*gpio_cb_t)(void *arg);
144
148#ifndef HAVE_GPIO_ISR_CTX_T
149typedef struct {
151 void *arg;
153#endif
154
168int gpio_init(gpio_t pin, gpio_mode_t mode);
169
170#if defined(MODULE_PERIPH_GPIO_IRQ) || defined(DOXYGEN)
193int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
194 gpio_cb_t cb, void *arg);
195
207void gpio_irq_enable(gpio_t pin);
208
217void gpio_irq_disable(gpio_t pin);
218
219#endif /* defined(MODULE_PERIPH_GPIO_IRQ) || defined(DOXYGEN) */
220
229bool gpio_read(gpio_t pin);
230
236void gpio_set(gpio_t pin);
237
243void gpio_clear(gpio_t pin);
244
250void gpio_toggle(gpio_t pin);
251
258void gpio_write(gpio_t pin, bool value);
259
266static inline int gpio_is_equal(gpio_t gpio1, gpio_t gpio2)
267{
268 return (gpio1 == gpio2);
269}
270
276static inline int gpio_is_valid(gpio_t gpio)
277{
278 return (gpio != GPIO_UNDEF);
279}
280
281#ifdef __cplusplus
282}
283#endif
284
#define GPIO_UNDEF
Definition of a fitting UNDEF value.
gpio_flank_t
Definition periph_cpu.h:176
@ GPIO_OUT
select GPIO MASK as output
Definition periph_cpu.h:161
@ GPIO_IN
select GPIO MASK as input
Definition periph_cpu.h:160
gpio_mode_t
Available pin modes.
Definition periph_cpu.h:96
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:143
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:276
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.
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_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.
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:266
Default interrupt context for GPIO pins.
Definition gpio.h:149
void * arg
optional argument
Definition gpio.h:151
gpio_cb_t cb
interrupt callback
Definition gpio.h:150