2014-02-03 19:31:04 +01:00
|
|
|
/*
|
2015-06-03 18:14:16 +02:00
|
|
|
* Copyright (C) 2015 Freie Universität Berlin
|
2014-02-03 19:31:04 +01:00
|
|
|
*
|
2014-08-27 18:47:31 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-11-19 15:54:52 +01:00
|
|
|
* @defgroup drivers_periph_gpio GPIO
|
|
|
|
* @ingroup drivers_periph
|
2014-02-04 18:46:02 +01:00
|
|
|
* @brief Low-level GPIO peripheral driver
|
2014-02-03 19:31:04 +01:00
|
|
|
*
|
2014-12-04 10:03:15 +01:00
|
|
|
* @{
|
2014-10-25 15:37:04 +02:00
|
|
|
* @file
|
2014-02-04 18:46:02 +01:00
|
|
|
* @brief Low-level GPIO peripheral driver interface definitions
|
2014-02-03 19:31:04 +01:00
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
*/
|
|
|
|
|
2015-03-23 21:07:50 +01:00
|
|
|
#ifndef GPIO_H
|
|
|
|
#define GPIO_H
|
2014-02-03 19:31:04 +01:00
|
|
|
|
2015-06-03 18:14:16 +02:00
|
|
|
#include "periph_cpu.h"
|
2015-10-27 13:29:37 +01:00
|
|
|
#include "periph_conf.h"
|
|
|
|
/* TODO: remove once all platforms are ported to this interface */
|
|
|
|
#include "periph/dev_enums.h"
|
2014-02-03 19:31:04 +01:00
|
|
|
|
2014-10-13 15:49:17 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-02-03 19:31:04 +01:00
|
|
|
/**
|
2015-06-14 16:12:20 +02:00
|
|
|
* @brief Default GPIO macro maps port-pin tuples to the pin value
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-09-15 11:46:49 +02:00
|
|
|
#ifndef GPIO_PIN
|
|
|
|
#define GPIO_PIN(x,y) ((x & 0) | y)
|
2014-10-01 18:42:34 +02:00
|
|
|
#endif
|
2015-06-03 18:14:16 +02:00
|
|
|
|
2015-06-14 16:12:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Define global value for GPIO not defined
|
|
|
|
*/
|
|
|
|
#ifndef GPIO_UNDEF
|
|
|
|
#define GPIO_UNDEF (-1)
|
|
|
|
#endif
|
|
|
|
|
2015-06-03 18:14:16 +02:00
|
|
|
/**
|
|
|
|
* @brief Define the default GPIO type identifier
|
|
|
|
*/
|
|
|
|
#ifndef HAVE_GPIO_T
|
|
|
|
typedef int gpio_t;
|
2014-10-01 18:42:34 +02:00
|
|
|
#endif
|
2015-06-03 18:14:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Definition of available pin directions
|
|
|
|
*/
|
|
|
|
#ifndef HAVE_GPIO_DIR_T
|
|
|
|
typedef enum {
|
|
|
|
GPIO_DIR_IN = 0, /**< configure pin as input */
|
2015-11-28 12:45:50 +01:00
|
|
|
GPIO_DIR_OUT = 1 /**< configure pin as output */
|
2015-06-03 18:14:16 +02:00
|
|
|
} gpio_dir_t;
|
2014-10-01 18:42:34 +02:00
|
|
|
#endif
|
2014-02-03 19:31:04 +01:00
|
|
|
|
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Definition of pull-up/pull-down modes
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
#ifndef HAVE_GPIO_PP_T
|
2014-02-03 19:31:04 +01:00
|
|
|
typedef enum {
|
2014-04-03 17:44:49 +02:00
|
|
|
GPIO_NOPULL = 0, /**< do not use internal pull resistors */
|
|
|
|
GPIO_PULLUP = 1, /**< enable internal pull-up resistor */
|
|
|
|
GPIO_PULLDOWN = 2 /**< enable internal pull-down resistor */
|
2014-02-03 19:31:04 +01:00
|
|
|
} gpio_pp_t;
|
2015-06-03 18:14:16 +02:00
|
|
|
#endif
|
2014-02-03 19:31:04 +01:00
|
|
|
|
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Definition of possible active flanks for external interrupt mode
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
#ifndef HAVE_GPIO_FLANK_T
|
2014-02-03 19:31:04 +01:00
|
|
|
typedef enum {
|
2014-04-03 17:44:49 +02:00
|
|
|
GPIO_FALLING = 0, /**< emit interrupt on falling flank */
|
|
|
|
GPIO_RISING = 1, /**< emit interrupt on rising flank */
|
|
|
|
GPIO_BOTH = 2 /**< emit interrupt on both flanks */
|
2014-02-03 19:31:04 +01:00
|
|
|
} gpio_flank_t;
|
2015-06-03 18:14:16 +02:00
|
|
|
#endif
|
2014-02-03 19:31:04 +01:00
|
|
|
|
2014-07-28 16:21:30 +02:00
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Signature of event callback functions triggered from interrupts
|
2014-07-28 16:21:30 +02:00
|
|
|
*
|
|
|
|
* @param[in] arg optional context for the callback
|
|
|
|
*/
|
|
|
|
typedef void (*gpio_cb_t)(void *arg);
|
|
|
|
|
2014-02-03 19:31:04 +01:00
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Initialize the given pin as general purpose input or output
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] pin pin to initialize
|
|
|
|
* @param[in] dir direction for the pin, input or output
|
|
|
|
* @param[in] pullup define what pull resistors should be used
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2014-04-03 17:44:49 +02:00
|
|
|
* @return 0 on success
|
2015-06-03 18:14:16 +02:00
|
|
|
* @return -1 on error
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
int gpio_init(gpio_t pin, gpio_dir_t dir, gpio_pp_t pullup);
|
2014-02-03 19:31:04 +01:00
|
|
|
|
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Initialize a GPIO pin for external interrupt usage
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* The registered callback function will be called in interrupt context every
|
|
|
|
* time the defined flank(s) are detected.
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2014-02-03 19:31:04 +01:00
|
|
|
* The interrupt is activated automatically after the initialization.
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] pin pin to initialize
|
|
|
|
* @param[in] pullup define what pull resistors should be enabled
|
2014-04-03 17:44:49 +02:00
|
|
|
* @param[in] flank define the active flank(s)
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] cb callback that is called from interrupt context
|
2014-07-28 16:21:30 +02:00
|
|
|
* @param[in] arg optional argument passed to the callback
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2014-04-03 17:44:49 +02:00
|
|
|
* @return 0 on success
|
2015-06-03 18:14:16 +02:00
|
|
|
* @return -1 on error
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-06-12 19:10:29 +02:00
|
|
|
int gpio_init_int(gpio_t pin, gpio_pp_t pullup, gpio_flank_t flank,
|
2015-06-03 18:14:16 +02:00
|
|
|
gpio_cb_t cb, void *arg);
|
2014-02-03 19:31:04 +01:00
|
|
|
|
2014-07-03 21:51:37 +02:00
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Enable pin's interrupt if configured as interrupt source
|
2014-07-03 21:51:37 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] pin the pin to enable the interrupt for
|
2014-07-03 21:51:37 +02:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
void gpio_irq_enable(gpio_t pin);
|
2014-07-03 21:51:37 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Disable the pin's interrupt if configured as interrupt source
|
2014-07-03 21:51:37 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] pin the pin to disable the interrupt for
|
2014-07-03 21:51:37 +02:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
void gpio_irq_disable(gpio_t pin);
|
2014-07-03 21:51:37 +02:00
|
|
|
|
2014-02-03 19:31:04 +01:00
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Get the current value of the given pin
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] pin the pin to read
|
2014-07-28 16:21:30 +02:00
|
|
|
* @return the current value, 0 for LOW, != 0 for HIGH
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2014-04-03 17:44:49 +02:00
|
|
|
* @return 0 when pin is LOW
|
|
|
|
* @return greater 0 for HIGH
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
int gpio_read(gpio_t pin);
|
2014-02-03 19:31:04 +01:00
|
|
|
|
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Set the given pin to HIGH
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] pin the pin to set
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
void gpio_set(gpio_t pin);
|
2014-02-03 19:31:04 +01:00
|
|
|
|
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Set the given pin to LOW
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] pin the pin to clear
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
void gpio_clear(gpio_t pin);
|
2014-02-03 19:31:04 +01:00
|
|
|
|
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Toggle the value of the given pin
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] pin the pin to toggle
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
void gpio_toggle(gpio_t pin);
|
2014-02-03 19:31:04 +01:00
|
|
|
|
|
|
|
/**
|
2015-06-03 18:14:16 +02:00
|
|
|
* @brief Set the given pin to the given value
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-06-03 18:14:16 +02:00
|
|
|
* @param[in] pin the pin to set
|
2014-04-03 17:44:49 +02:00
|
|
|
* @param[in] value value to set the pin to, 0 for LOW, HIGH otherwise
|
2014-02-03 19:31:04 +01:00
|
|
|
*/
|
2015-06-03 18:14:16 +02:00
|
|
|
void gpio_write(gpio_t pin, int value);
|
2014-02-03 19:31:04 +01:00
|
|
|
|
2014-10-13 15:49:17 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-23 21:07:50 +01:00
|
|
|
#endif /* GPIO_H */
|
2014-02-03 19:31:04 +01:00
|
|
|
/** @} */
|