gpio: set initially edge_mode and direction in handle 85/163985/6
authorSegwon <segwon.han@samsung.com>
Thu, 14 Dec 2017 11:05:12 +0000 (20:05 +0900)
committerSegwon <segwon.han@samsung.com>
Mon, 18 Dec 2017 03:18:03 +0000 (12:18 +0900)
Change-Id: Ie9c50a3d680562c9f573ee006b816a3960096c46
Signed-off-by: Segwon <segwon.han@samsung.com>
include/interface/peripheral_interface_gpio.h
include/peripheral_handle.h
src/interface/peripheral_interface_gpio.c
src/peripheral_gpio.c

index 140edeafca2a2ff3d92e131a63919422ebf18433..d4ca75dcd138892b23e869872a8122a952555c77 100644 (file)
 #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);
 
index 0fc1b2575f454c20a7454ddf128797bca7c94520..b834d06bc7a9cbe7eb2cd3b6e9a554ede69f6a0e 100644 (file)
@@ -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;
 };
 
 /**
index 00b911b43daf91ae0c260c34668d52cc1abd5fa5..4ca79e4b7d29ce4a88b9ad3882210ff227330d1a 100644 (file)
 
 #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;
 }
 
index 4d0579bc649afe25d8b72efb52ec976788bc4ede..4cd02906500dd55277a715c54524e29f1a0d93fc 100644 (file)
@@ -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;
 }
 
 /**