From f2b38db50344ce5a9c240b352435614f676bc6b1 Mon Sep 17 00:00:00 2001 From: Segwon Date: Thu, 14 Dec 2017 20:05:12 +0900 Subject: [PATCH] gpio: set initially edge_mode and direction in handle Change-Id: Ie9c50a3d680562c9f573ee006b816a3960096c46 Signed-off-by: Segwon --- include/interface/peripheral_interface_gpio.h | 5 ++ include/peripheral_handle.h | 2 + src/interface/peripheral_interface_gpio.c | 53 +++++++++++++++++++ src/peripheral_gpio.c | 16 +++++- 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/include/interface/peripheral_interface_gpio.h b/include/interface/peripheral_interface_gpio.h index 140edea..d4ca75d 100644 --- a/include/interface/peripheral_interface_gpio.h +++ b/include/interface/peripheral_interface_gpio.h @@ -22,8 +22,13 @@ #define GPIO_BUFFER_MAX 64 void peripheral_interface_gpio_close(peripheral_gpio_h gpio); + +int peripheral_interface_gpio_set_initial_edge_into_handle(peripheral_gpio_h gpio); int peripheral_interface_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge); + +int peripheral_interface_gpio_set_initial_direction_into_handle(peripheral_gpio_h gpio); int peripheral_interface_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction); + int peripheral_interface_gpio_write(peripheral_gpio_h gpio, uint32_t value); int peripheral_interface_gpio_read(peripheral_gpio_h gpio, uint32_t *value); diff --git a/include/peripheral_handle.h b/include/peripheral_handle.h index 0fc1b25..b834d06 100644 --- a/include/peripheral_handle.h +++ b/include/peripheral_handle.h @@ -25,6 +25,8 @@ struct _peripheral_gpio_s { int fd_direction; int fd_edge; int fd_value; + peripheral_gpio_direction_e direction; + peripheral_gpio_edge_e edge; }; /** diff --git a/src/interface/peripheral_interface_gpio.c b/src/interface/peripheral_interface_gpio.c index 00b911b..4ca79e4 100644 --- a/src/interface/peripheral_interface_gpio.c +++ b/src/interface/peripheral_interface_gpio.c @@ -16,6 +16,30 @@ #include "peripheral_interface_gpio.h" +int peripheral_interface_gpio_set_initial_direction_into_handle(peripheral_gpio_h gpio) +{ + static predefined_type_s types[2] = { + {"in", 2}, + {"out", 3}, + }; + + int index; + char gpio_buf[GPIO_BUFFER_MAX] = {0, }; + + int ret = read(gpio->fd_direction, &gpio_buf, GPIO_BUFFER_MAX); + CHECK_ERROR(ret <= 0); + + for (index = 0; index < 2; index++) { + if (!strncmp(gpio_buf, types[index].type, types[index].len)) { + // PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_HIGH and PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW : out type + gpio->direction = (peripheral_gpio_direction_e)index; + return PERIPHERAL_ERROR_NONE; + } + } + + return PERIPHERAL_ERROR_IO_ERROR; +} + int peripheral_interface_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction) { static predefined_type_s types[3] = { @@ -27,9 +51,36 @@ int peripheral_interface_gpio_set_direction(peripheral_gpio_h gpio, peripheral_g int ret = write(gpio->fd_direction, types[direction].type, types[direction].len); CHECK_ERROR(ret != types[direction].len); + gpio->direction = direction; + return PERIPHERAL_ERROR_NONE; } +int peripheral_interface_gpio_set_initial_edge_into_handle(peripheral_gpio_h gpio) +{ + static predefined_type_s types[4] = { + {"none", 4}, + {"rising", 6}, + {"falling", 7}, + {"both", 4} + }; + + int index; + char gpio_buf[GPIO_BUFFER_MAX] = {0, }; + + int ret = read(gpio->fd_edge, &gpio_buf, GPIO_BUFFER_MAX); + CHECK_ERROR(ret <= 0); + + for (index = 0; index < 4; index++) { + if (!strncmp(gpio_buf, types[index].type, types[index].len)) { + gpio->edge = (peripheral_gpio_edge_e)index; + return PERIPHERAL_ERROR_NONE; + } + } + + return PERIPHERAL_ERROR_IO_ERROR; +} + int peripheral_interface_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge) { static predefined_type_s types[4] = { @@ -42,6 +93,8 @@ int peripheral_interface_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_g int ret = write(gpio->fd_edge, types[edge].type, types[edge].len); CHECK_ERROR(ret != types[edge].len); + gpio->edge = edge; + return PERIPHERAL_ERROR_NONE; } diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 4d0579b..4cd0290 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -71,9 +71,23 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) handle = NULL; } + ret = peripheral_interface_gpio_set_initial_direction_into_handle(handle); + if (ret != PERIPHERAL_ERROR_NONE) { + _E("Failed to peripheral_interface_gpio_set_initial_direction_into_handle()"); + peripheral_gpio_close(handle); + return ret; + } + + ret = peripheral_interface_gpio_set_initial_edge_into_handle(handle); + if (ret != PERIPHERAL_ERROR_NONE) { + _E("Failed to peripheral_interface_gpio_set_initial_edge_into_handle()"); + peripheral_gpio_close(handle); + return ret; + } + *gpio = handle; - return ret; + return PERIPHERAL_ERROR_NONE; } /** -- 2.34.1