2015-06-05 01:46:25 +00:00
|
|
|
/** esp_iomux.h
|
|
|
|
*
|
|
|
|
* GPIO functions.
|
|
|
|
*
|
|
|
|
* Part of esp-open-rtos
|
|
|
|
* Copyright (C) 2015 Superhouse Automation Pty Ltd
|
|
|
|
* BSD Licensed as described in the file LICENSE
|
|
|
|
*/
|
|
|
|
#ifndef _ESP_GPIO_H
|
2015-06-17 23:00:02 +00:00
|
|
|
#define _ESP_GPIO_H
|
2015-06-05 01:46:25 +00:00
|
|
|
#include <stdbool.h>
|
2015-08-19 05:46:25 +00:00
|
|
|
#include "esp/gpio_regs.h"
|
2016-08-30 11:22:05 +00:00
|
|
|
#include "esp/rtc_regs.h"
|
2015-06-05 01:46:25 +00:00
|
|
|
#include "esp/iomux.h"
|
2015-09-23 22:58:34 +00:00
|
|
|
#include "esp/interrupts.h"
|
2015-06-05 01:46:25 +00:00
|
|
|
|
2016-03-21 20:51:50 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-06-05 01:46:25 +00:00
|
|
|
typedef enum {
|
2015-06-08 08:09:06 +00:00
|
|
|
GPIO_INPUT,
|
|
|
|
GPIO_OUTPUT, /* "Standard" push-pull output */
|
|
|
|
GPIO_OUT_OPEN_DRAIN, /* Open drain output */
|
2015-06-05 01:46:25 +00:00
|
|
|
} gpio_direction_t;
|
|
|
|
|
2016-08-30 11:22:05 +00:00
|
|
|
/* Enable GPIO on the specified pin, and set it to input or output mode.
|
2015-06-05 01:46:25 +00:00
|
|
|
*/
|
2015-11-28 07:01:03 +00:00
|
|
|
void gpio_enable(const uint8_t gpio_num, const gpio_direction_t direction);
|
2015-06-05 01:46:25 +00:00
|
|
|
|
2016-02-22 02:34:11 +00:00
|
|
|
/* Enable/disable internal pullup resistor for a particular GPIO
|
|
|
|
*
|
|
|
|
* Note: According to Espressif, pullup resistor values are between 30K and
|
|
|
|
* 100K ohms (see http://bbs.espressif.com/viewtopic.php?t=1079#p4097)
|
|
|
|
* However, measured values suggest that the actual value is likely to be close
|
|
|
|
* to 47K in reality.
|
2016-02-22 17:32:12 +00:00
|
|
|
*
|
|
|
|
* NOTE: The enabled_during_sleep setting is currently untested (please send
|
|
|
|
* feedback if you give it a try)
|
2016-02-22 02:34:11 +00:00
|
|
|
*/
|
|
|
|
void gpio_set_pullup(uint8_t gpio_num, bool enabled, bool enabled_during_sleep);
|
|
|
|
|
2015-06-05 01:46:25 +00:00
|
|
|
/* Disable GPIO on the specified pin, and set it Hi-Z.
|
|
|
|
*
|
|
|
|
* If later muxing this pin to a different function, make sure to set
|
2015-08-19 00:38:31 +00:00
|
|
|
* IOMUX_PIN_OUTPUT_ENABLE if necessary to enable the output buffer.
|
2015-06-05 01:46:25 +00:00
|
|
|
*/
|
2015-11-28 07:01:03 +00:00
|
|
|
static inline void gpio_disable(const uint8_t gpio_num)
|
2015-06-05 01:46:25 +00:00
|
|
|
{
|
2015-08-19 05:46:25 +00:00
|
|
|
GPIO.ENABLE_OUT_CLEAR = BIT(gpio_num);
|
2015-08-19 00:38:31 +00:00
|
|
|
*gpio_iomux_reg(gpio_num) &= ~IOMUX_PIN_OUTPUT_ENABLE;
|
2015-06-05 01:46:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:34:11 +00:00
|
|
|
/* Set whether the specified pin continues to drive its output when the ESP8266
|
|
|
|
* goes into sleep mode. Note that this setting is reset to off whenever
|
|
|
|
* gpio_enable is called, so this must be called after calling that function.
|
2016-02-22 17:32:12 +00:00
|
|
|
*
|
|
|
|
* NOTE: This functionality is currently untested (please send feedback if you
|
|
|
|
* give it a try)
|
2016-02-22 02:34:11 +00:00
|
|
|
*/
|
|
|
|
static inline void gpio_set_output_on_sleep(const uint8_t gpio_num, bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled) {
|
|
|
|
IOMUX.PIN[gpio_to_iomux(gpio_num)] |= IOMUX_PIN_OUTPUT_ENABLE_SLEEP;
|
|
|
|
} else {
|
|
|
|
IOMUX.PIN[gpio_to_iomux(gpio_num)] &= ~IOMUX_PIN_OUTPUT_ENABLE_SLEEP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 01:46:25 +00:00
|
|
|
/* Set output of a pin high or low.
|
|
|
|
*
|
2016-02-22 17:32:12 +00:00
|
|
|
* Only works if pin has been set to GPIO_OUTPUT or GPIO_OUT_OPEN_DRAIN via
|
|
|
|
* gpio_enable()
|
|
|
|
*
|
|
|
|
* If the mode is GPIO_OUT_OPEN_DRAIN, setting it low (false) will pull the pin
|
|
|
|
* down to ground, but setting it high (true) will allow it to float. Note
|
|
|
|
* that even in GPIO_OUT_OPEN_DRAIN mode, the input gates are still physically
|
|
|
|
* connected to the pin, and can be damaged if the voltage is not in either the
|
|
|
|
* "low" or "high" range. Make sure there is some sort of pull-up resistor on
|
|
|
|
* the line to avoid floating logic lines!
|
2015-06-05 01:46:25 +00:00
|
|
|
*/
|
2015-11-28 07:01:03 +00:00
|
|
|
static inline void gpio_write(const uint8_t gpio_num, const bool set)
|
2015-06-05 01:46:25 +00:00
|
|
|
{
|
2016-08-30 11:22:05 +00:00
|
|
|
if (gpio_num == 16) {
|
|
|
|
RTC.GPIO_OUT = (RTC.GPIO_OUT & 0xfffffffe) | (set ? 1 : 0);
|
|
|
|
} else if (set)
|
2016-04-20 05:43:08 +00:00
|
|
|
GPIO.OUT_SET = BIT(gpio_num) & GPIO_OUT_PIN_MASK;
|
2015-06-05 01:46:25 +00:00
|
|
|
else
|
2016-04-20 05:43:08 +00:00
|
|
|
GPIO.OUT_CLEAR = BIT(gpio_num) & GPIO_OUT_PIN_MASK;
|
2015-06-05 01:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Toggle output of a pin
|
|
|
|
*
|
2016-02-22 17:32:12 +00:00
|
|
|
* Only works if pin has been set to GPIO_OUTPUT or GPIO_OUT_OPEN_DRAIN via
|
|
|
|
* gpio_enable()
|
|
|
|
*
|
|
|
|
* See notes in gpio_write() about GPIO_OUT_OPEN_DRAIN mode.
|
2015-06-05 01:46:25 +00:00
|
|
|
*/
|
2015-11-28 07:01:03 +00:00
|
|
|
static inline void gpio_toggle(const uint8_t gpio_num)
|
2015-06-05 01:46:25 +00:00
|
|
|
{
|
|
|
|
/* Why implement like this instead of GPIO_OUT_REG ^= xxx?
|
|
|
|
Concurrency. If an interrupt or higher priority task writes to
|
|
|
|
GPIO_OUT between reading and writing, only the gpio_num pin can
|
|
|
|
get an invalid value. Prevents one task from clobbering another
|
|
|
|
task's pins, without needing to disable/enable interrupts.
|
|
|
|
*/
|
2016-08-30 11:22:05 +00:00
|
|
|
if (GPIO.OUT & BIT(gpio_num))
|
2016-04-20 05:43:08 +00:00
|
|
|
GPIO.OUT_CLEAR = BIT(gpio_num) & GPIO_OUT_PIN_MASK;
|
2015-06-05 01:46:25 +00:00
|
|
|
else
|
2016-04-20 05:43:08 +00:00
|
|
|
GPIO.OUT_SET = BIT(gpio_num) & GPIO_OUT_PIN_MASK;
|
2015-06-05 01:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read input value of a GPIO pin.
|
|
|
|
*
|
2016-02-22 17:32:12 +00:00
|
|
|
* If pin is set GPIO_INPUT, this reads the level on the pin.
|
|
|
|
* If pin is set GPIO_OUTPUT, this reads the level at which the pin is
|
|
|
|
* currently being driven (i.e. the last value written).
|
|
|
|
* If pin is set GPIO_OUT_OPEN_DRAIN, when the pin is written low, this will
|
|
|
|
* return low (false), when the pin is written high, this will behave like
|
|
|
|
* GPIO_INPUT.
|
2015-06-05 01:46:25 +00:00
|
|
|
*/
|
2015-11-28 07:01:03 +00:00
|
|
|
static inline bool gpio_read(const uint8_t gpio_num)
|
2015-06-05 01:46:25 +00:00
|
|
|
{
|
2016-08-30 11:22:05 +00:00
|
|
|
if (gpio_num == 16)
|
|
|
|
return RTC.GPIO_IN & 1;
|
|
|
|
else
|
|
|
|
return GPIO.IN & BIT(gpio_num);
|
2015-06-05 01:46:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 08:09:06 +00:00
|
|
|
extern void gpio_interrupt_handler(void);
|
|
|
|
|
|
|
|
/* Set the interrupt type for a given pin
|
2015-06-17 22:59:33 +00:00
|
|
|
*
|
2016-02-22 17:32:12 +00:00
|
|
|
* If int_type is not GPIO_INTTYPE_NONE, the gpio_interrupt_handler will be
|
|
|
|
* attached and unmasked.
|
2015-06-08 08:09:06 +00:00
|
|
|
*/
|
2015-11-28 07:01:03 +00:00
|
|
|
static inline void gpio_set_interrupt(const uint8_t gpio_num, const gpio_inttype_t int_type)
|
2015-06-08 08:09:06 +00:00
|
|
|
{
|
2015-08-19 05:46:25 +00:00
|
|
|
GPIO.CONF[gpio_num] = SET_FIELD(GPIO.CONF[gpio_num], GPIO_CONF_INTTYPE, int_type);
|
|
|
|
if(int_type != GPIO_INTTYPE_NONE) {
|
2015-06-08 23:00:32 +00:00
|
|
|
_xt_isr_attach(INUM_GPIO, gpio_interrupt_handler);
|
|
|
|
_xt_isr_unmask(1<<INUM_GPIO);
|
2015-06-08 08:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-05 01:46:25 +00:00
|
|
|
|
2015-06-17 22:59:33 +00:00
|
|
|
/* Return the interrupt type set for a pin */
|
2015-11-28 07:01:03 +00:00
|
|
|
static inline gpio_inttype_t gpio_get_interrupt(const uint8_t gpio_num)
|
2015-06-17 22:59:33 +00:00
|
|
|
{
|
2015-08-26 00:49:00 +00:00
|
|
|
return (gpio_inttype_t)FIELD2VAL(GPIO_CONF_INTTYPE, GPIO.CONF[gpio_num]);
|
2015-06-17 22:59:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 12:23:45 +00:00
|
|
|
/* Set GPIO I/O Mux function.
|
|
|
|
* The 'func' is an IOMUX_GPIO<n>_FUNC_<function> value, see iomux_regs.h
|
|
|
|
*/
|
|
|
|
inline static void gpio_set_iomux_function(const uint8_t gpio_num, uint32_t func)
|
|
|
|
{
|
|
|
|
uint8_t iomux_num = gpio_to_iomux(gpio_num);
|
|
|
|
uint32_t prev = IOMUX.PIN[iomux_num] & ~IOMUX_PIN_FUNC_MASK;
|
|
|
|
IOMUX.PIN[iomux_num] = func | prev;
|
|
|
|
}
|
|
|
|
|
2016-03-21 20:51:50 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-05 01:46:25 +00:00
|
|
|
#endif
|