Loading...
Searching...
No Matches
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
9#pragma once
10
76
77#include <limits.h>
78#include <stdbool.h>
79
80#include "periph_cpu.h"
81#include "periph_conf.h"
82
83#ifdef __cplusplus
84extern "C" {
85#endif
86
87#ifndef HAVE_GPIO_T
91typedef unsigned int gpio_t;
92#endif
93
94#ifndef GPIO_PIN
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
106#define GPIO_UNDEF ((gpio_t)(UINT_MAX))
107#endif
108
117#ifndef HAVE_GPIO_MODE_T
118typedef enum {
120 GPIO_IN_PD,
121 GPIO_IN_PU,
123 GPIO_OD,
125 GPIO_OD_PU
128#endif
129
133#ifndef HAVE_GPIO_FLANK_T
134typedef enum {
135 GPIO_FALLING = 0,
136 GPIO_RISING = 1,
137 GPIO_BOTH = 2
139#endif
140
146typedef void (*gpio_cb_t)(void *arg);
147
151#ifndef HAVE_GPIO_ISR_CTX_T
152typedef struct {
154 void *arg;
156#endif
157
171int gpio_init(gpio_t pin, gpio_mode_t mode);
172
173#if defined(MODULE_PERIPH_GPIO_IRQ) || defined(DOXYGEN)
196int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
197 gpio_cb_t cb, void *arg);
198
210void gpio_irq_enable(gpio_t pin);
211
220void gpio_irq_disable(gpio_t pin);
221
222#endif /* defined(MODULE_PERIPH_GPIO_IRQ) || defined(DOXYGEN) */
223
232bool gpio_read(gpio_t pin);
233
239void gpio_set(gpio_t pin);
240
246void gpio_clear(gpio_t pin);
247
253void gpio_toggle(gpio_t pin);
254
261void gpio_write(gpio_t pin, bool value);
262
269static inline int gpio_is_equal(gpio_t gpio1, gpio_t gpio2)
270{
271 return (gpio1 == gpio2);
272}
273
279static inline int gpio_is_valid(gpio_t gpio)
280{
281 return (gpio != GPIO_UNDEF);
282}
283
284#ifdef __cplusplus
285}
286#endif
287
#define GPIO_UNDEF
Definition of a fitting UNDEF value.
gpio_flank_t
Definition periph_cpu.h:179
@ GPIO_OUT
select GPIO MASK as output
Definition periph_cpu.h:164
@ GPIO_IN
select GPIO MASK as input
Definition periph_cpu.h:163
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:146
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:279
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:269
Default interrupt context for GPIO pins.
Definition gpio.h:152
void * arg
optional argument
Definition gpio.h:154
gpio_cb_t cb
interrupt callback
Definition gpio.h:153