Skip to content

Commit

Permalink
add toogle function, fix interrupt pull up & disable, test all interface
Browse files Browse the repository at this point in the history
  • Loading branch information
shaoguoji committed Jan 16, 2018
1 parent 8795f22 commit fd3b054
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions platform/mcu/esp32/hal/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
#undef gpio_dev_t
#undef gpio_config_t

#include <driver/gpio.h>
#include <driver/rtc_io.h>
#include "driver/gpio.h"
#include "driver/rtc_io.h"
#include "soc/soc.h"

#define ESP_INTR_FLAG_DEFAULT 0
#define GPIO_OUT_REG (DR_REG_GPIO_BASE +0x04)

int32_t hal_gpio_init(aos_gpio_dev_t *gpio)
{
int32_t ret = -1;
gpio_config_t io_conf;

/* disable interrupt */
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
/* set mode */
Expand Down Expand Up @@ -51,11 +53,12 @@ int32_t hal_gpio_init(aos_gpio_dev_t *gpio)
rtc_gpio_init(gpio->port);
}
/* bit mask of the pins */
io_conf.pin_bit_mask = (1ULL << (gpio->port));
io_conf.pin_bit_mask = BIT(gpio->port);
/* set pull-down mode */
io_conf.pull_down_en = (gpio->config == INPUT_PULL_DOWN) ? 1 : 0;
/* set pull-up mode */
io_conf.pull_up_en = ((gpio->config == INPUT_PULL_UP) ||
(gpio->config == IRQ_MODE) ||
(gpio->config == OUTPUT_OPEN_DRAIN_PULL_UP)) ? 1 : 0;
/* configure GPIO with the given settings */
ret = gpio_config(&io_conf);
Expand All @@ -66,63 +69,51 @@ int32_t hal_gpio_init(aos_gpio_dev_t *gpio)
int32_t hal_gpio_output_high(aos_gpio_dev_t *gpio)
{
int32_t ret = -1;

ret = gpio_set_level(gpio->port, 1);

return ret;
}

int32_t hal_gpio_output_low(aos_gpio_dev_t *gpio)
{
int32_t ret = -1;

ret = gpio_set_level(gpio->port, 0);

return ret;
}

int32_t hal_gpio_output_toggle(aos_gpio_dev_t *gpio)
{
// TODO

/* toggle gpio by writing GPIO_OUT_REG register*/
REG_WRITE(GPIO_OUT_REG, (REG_READ(GPIO_OUT_REG)^BIT(gpio->port)));
return 0;
}

int32_t hal_gpio_input_get(aos_gpio_dev_t *gpio, uint32_t *value)
{
*value = gpio_get_level(gpio->port);

return 0;
}

int32_t hal_gpio_enable_irq(aos_gpio_dev_t *gpio, gpio_irq_trigger_t trigger,
gpio_irq_handler_t handler, void *arg)
{
int32_t ret = -1;

gpio_set_intr_type(gpio->port, (gpio_int_type_t)trigger);
ret = gpio_isr_handler_add(gpio->port, handler, arg);

return ret;
}

int32_t hal_gpio_disable_irq(aos_gpio_dev_t *gpio)
{
int32_t ret = -1;

ret = gpio_intr_disable(gpio->port);

ret = gpio_set_intr_type(gpio->port, GPIO_INTR_DISABLE);
return ret;
}

int32_t hal_gpio_clear_irq(aos_gpio_dev_t *gpio)
{
int32_t ret = -1;

gpio_intr_disable(gpio->port);
gpio_set_intr_type(gpio->port, GPIO_INTR_DISABLE);
ret = gpio_isr_handler_remove(gpio->port);

return ret;
}

Expand All @@ -136,7 +127,7 @@ int32_t hal_gpio_finalize(aos_gpio_dev_t *gpio)
/* disable output */
io_conf.mode = GPIO_MODE_INPUT;
/* bit mask of the pins */
io_conf.pin_bit_mask = (1ULL << (gpio->port));
io_conf.pin_bit_mask = BIT(gpio->port);
/* disable pull-down mode */
io_conf.pull_down_en = 0;
/* disable pull-up mode */
Expand Down

0 comments on commit fd3b054

Please sign in to comment.