From e2a5bafd94274c32d0c6fdd5e043161ce124ba19 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Fri, 12 May 2017 15:47:40 +0900 Subject: [PATCH 01/16] Test function for uart API The function reads data stream from uart port. Change-Id: Ic85657939aaa8da4ed3914c9fa4e50388b81627b Signed-off-by: jino.cho --- test/peripheral-io-test.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index dae9170..a787e10 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -296,6 +296,65 @@ int pwm_test_motor(void) return 0; } + +int uart_test_accelerometer(void) +{ + peripheral_uart_h uart; + int ret; + int port; + int loop = 100; + unsigned char buf[1024]; + + printf("<<< uart_test_accelerometer >>>\n"); + printf("artik710 : 4 \n"); + printf(">> PORT NUMBER : "); + + if (scanf("%d", &port) < 0) + return 0; + + ret = peripheral_uart_open(port, &uart); + if (ret < 0) + goto err_open; + ret = peripheral_uart_set_baudrate(uart, PERIPHERAL_UART_BAUDRATE_4800); + if (ret < 0) + goto out; + ret = peripheral_uart_set_mode(uart, + PERIPHERAL_UART_BYTESIZE_8BIT, + PERIPHERAL_UART_PARITY_NONE, + PERIPHERAL_UART_STOPBITS_1BIT); + if (ret < 0) + goto out; + ret = peripheral_uart_set_flowcontrol(uart, true, false); + if (ret < 0) + goto out; + + sleep(1); + ret = peripheral_uart_flush(uart); + if (ret < 0) + goto out; + + while (loop--) { + ret = peripheral_uart_read(uart, buf, 13); + if (ret < 0) { + if (ret == PERIPHERAL_ERROR_NO_DATA) + printf("No data to read (%d)\n", ret); + else + printf("Failed to read (%d)\n", ret); + continue; + } + buf[ret] = 0; + printf("%s", buf); + usleep(100000); + } + +out: + peripheral_uart_close(uart); + +err_open: + printf(">> ret = %d\n", ret); + return 0; +} + int main(int argc, char **argv) { int num = 1; @@ -308,6 +367,7 @@ int main(int argc, char **argv) printf(" 2. I2C Test\n"); printf(" 3. pwm led test\n"); printf(" 4. pwm motor test\n"); + printf(" 5. uart accelerometer test\n"); printf(" 11. GPIO Interrupt Test\n"); printf(" 12. H/W IF I2C Test\n"); @@ -330,6 +390,9 @@ int main(int argc, char **argv) case 4: ret = pwm_test_motor(); break; + case 5: + ret = uart_test_accelerometer(); + break; case 11: ret = gpio_irq_test(); break; -- 2.7.4 From 7de767ffe6e464f2c2914a5c881c42f9877f5c15 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 14:04:57 +0900 Subject: [PATCH 02/16] Remove unused defines Change-Id: Ic330c835875625d782db9cee8e9b6b957443dff9 Signed-off-by: jino.cho --- src/peripheral_pwm.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 07c4001..2b9c1a9 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -24,9 +24,6 @@ #include "peripheral_common.h" #include "peripheral_internal.h" -#define PWM_ENABLE 1 -#define PWM_DISABLE 0 - int peripheral_pwm_open(int device, int channel, peripheral_pwm_h* pwm) { peripheral_pwm_h handle; -- 2.7.4 From 628bec3eec313f4980b038e2541d3fc394559784 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 14:59:20 +0900 Subject: [PATCH 03/16] Check validation of pwm handle in the peripheral_pwm_close() Change-Id: I0b8ae08011188087819365a2dc5cd40bb6b14f39 Signed-off-by: jino.cho --- src/peripheral_pwm.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 2b9c1a9..ecb9b4a 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -58,6 +58,8 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) { int ret = PERIPHERAL_ERROR_NONE; + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + ret = peripheral_gdbus_pwm_close(pwm); pwm_proxy_deinit(); -- 2.7.4 From da06ae69ee8437ec55f1fc9da472980cf178204d Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 15:28:19 +0900 Subject: [PATCH 04/16] Fix gdbus error log Change-Id: Ib2831b01f1080dba1204940c43f23bd02278d98f Signed-off-by: jino.cho --- src/peripheral_gdbus_pwm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index cd4f26e..8b3268c 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -65,7 +65,7 @@ int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -87,7 +87,7 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -110,7 +110,7 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -133,7 +133,7 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -156,7 +156,7 @@ int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -179,7 +179,7 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -202,7 +202,7 @@ int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("%s", error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } -- 2.7.4 From e963f85fcbaee117416c6f9d1671a2e354e388b8 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 15:38:42 +0900 Subject: [PATCH 05/16] Modify peripheral_gdbus_pwm.c - Change return type to peripheral_error_e - Remove TODO comment Change-Id: Iec750952c3d2ee827ef4a830b3bac81c9373068b Signed-off-by: jino.cho --- src/peripheral_gdbus_pwm.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 8b3268c..4d1e396 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -52,11 +52,10 @@ void pwm_proxy_deinit() int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_open_sync( pwm_proxy, device, @@ -76,11 +75,10 @@ int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int device, int channel) int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_close_sync( pwm_proxy, pwm->handle, @@ -98,11 +96,10 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_set_period_sync( pwm_proxy, pwm->handle, @@ -121,11 +118,10 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period) int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_get_period_sync( pwm_proxy, pwm->handle, @@ -144,11 +140,10 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period) int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync( pwm_proxy, pwm->handle, @@ -167,11 +162,10 @@ int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync( pwm_proxy, pwm->handle, @@ -190,11 +184,10 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { GError *error = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - /* TODO: Need to reorganize arguments */ if (peripheral_io_gdbus_pwm_call_set_enable_sync( pwm_proxy, pwm->handle, -- 2.7.4 From dedd27d82ae980ad39cbce270ac40d7ada4979a3 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 15:47:53 +0900 Subject: [PATCH 06/16] Add pwm set_polarity & get_polarity functions Change-Id: Ia68fab71850f2ef0b68e6c3f22310dbb13484e8f Signed-off-by: jino.cho --- include/peripheral_gdbus_pwm.h | 2 ++ include/peripheral_io.h | 12 ++++++++++++ src/peripheral_gdbus_pwm.c | 44 ++++++++++++++++++++++++++++++++++++++++++ src/peripheral_io.xml | 10 ++++++++++ src/peripheral_pwm.c | 14 ++++++++++++++ 5 files changed, 82 insertions(+) diff --git a/include/peripheral_gdbus_pwm.h b/include/peripheral_gdbus_pwm.h index 59f9d38..3fc3cab 100644 --- a/include/peripheral_gdbus_pwm.h +++ b/include/peripheral_gdbus_pwm.h @@ -26,6 +26,8 @@ int peripheral_gdbus_pwm_set_period(peripheral_pwm_h pwm, int period); int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period); int peripheral_gdbus_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); +int peripheral_gdbus_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity); +int peripheral_gdbus_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity); int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable); int peripheral_gdbus_pwm_get_enable(peripheral_pwm_h pwm, bool *enable); diff --git a/include/peripheral_io.h b/include/peripheral_io.h index f5bb401..1000993 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -375,6 +375,14 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); */ typedef struct _peripheral_pwm_s *peripheral_pwm_h; +/** + * @brief Enumeration for Polarity. + */ +typedef enum { + PERIPHERAL_PWM_POLARITY_NORMAL = 0, + PERIPHERAL_PWM_POLARITY_INVERSED, +} peripheral_pwm_polarity_e; + int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm); int peripheral_pwm_close(peripheral_pwm_h pwm); @@ -388,6 +396,10 @@ int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); +int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity); + +int peripheral_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity); + int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable); int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable); diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 4d1e396..1467c8f 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -181,6 +181,50 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) return ret; } +int peripheral_gdbus_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_pwm_call_set_polarity_sync( + pwm_proxy, + pwm->handle, + polarity, + &ret, + NULL, + &error) == FALSE) { + _E("%s", error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_gdbus_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_pwm_call_get_polarity_sync( + pwm_proxy, + pwm->handle, + (gint*)polarity, + &ret, + NULL, + &error) == FALSE) { + _E("%s", error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + int peripheral_gdbus_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { GError *error = NULL; diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index 57ad78d..abc3679 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -113,6 +113,16 @@ + + + + + + + + + + diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index ecb9b4a..354ad80 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -99,6 +99,20 @@ int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) return peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle); } +int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity) +{ + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_pwm_set_polarity(pwm, polarity); +} + +int peripheral_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity) +{ + if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_pwm_get_polarity(pwm, polarity); +} + int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; -- 2.7.4 From fe3a9f4a5a465ac3097d2afd7170d25d2acae913 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 18:36:20 +0900 Subject: [PATCH 07/16] Replace checking validation of the pwm device and channel Change-Id: I13a2b3b14dec162e6abc140def5bb3c4000f9622 Signed-off-by: jino.cho --- src/peripheral_pwm.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 354ad80..607c8f8 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -24,13 +24,12 @@ #include "peripheral_common.h" #include "peripheral_internal.h" -int peripheral_pwm_open(int device, int channel, peripheral_pwm_h* pwm) +int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm) { peripheral_pwm_h handle; int ret = PERIPHERAL_ERROR_NONE; - assert(device >= 0); - assert(channel >= 0); + if (device < 0 || channel < 0) return PERIPHERAL_ERROR_INVALID_PARAMETER; /* Initialize */ handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s)); -- 2.7.4 From beba91c7725e702664b95039532771ee894ee7ca Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Fri, 19 May 2017 19:22:11 +0900 Subject: [PATCH 08/16] Fix possible memory leak in peripheral_pwm_close Free pwm handle regardless of return value of peripheral_gdbus_pwm_close(). Change-Id: Iaaa85b757f96851eac186c11efbdd8fa8e32bb98 Signed-off-by: Hyeongsik Min --- src/peripheral_pwm.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 607c8f8..7e98766 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -59,13 +59,11 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_gdbus_pwm_close(pwm); - pwm_proxy_deinit(); + if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0) + _E("Failed to close PWM device, continuing anyway"); - if (ret == PERIPHERAL_ERROR_NONE) { - free(pwm); - pwm = NULL; - } + pwm_proxy_deinit(); + free(pwm); return ret; } -- 2.7.4 From 7f8f05b74cd0f367233b0d0917fd91482e614680 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 22 May 2017 20:10:43 +0900 Subject: [PATCH 09/16] Fix reference counting bug Change-Id: If8be15c2815a11cc63d477e0af7fa79729a62966 Signed-off-by: Hyeongsik Min --- src/peripheral_gdbus_gpio.c | 5 +++-- src/peripheral_gdbus_i2c.c | 5 +++-- src/peripheral_gdbus_pwm.c | 5 +++-- src/peripheral_gdbus_uart.c | 5 +++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/peripheral_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c index 2027f3d..1c0e90f 100644 --- a/src/peripheral_gdbus_gpio.c +++ b/src/peripheral_gdbus_gpio.c @@ -32,8 +32,10 @@ void gpio_proxy_init(void) { GError *error = NULL; - if (gpio_proxy != NULL) + if (gpio_proxy != NULL) { + g_object_ref(gpio_proxy); return; + } gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync( G_BUS_TYPE_SYSTEM, @@ -58,7 +60,6 @@ void gpio_proxy_deinit() { if (gpio_proxy) { g_object_unref(gpio_proxy); - gpio_proxy = NULL; } } diff --git a/src/peripheral_gdbus_i2c.c b/src/peripheral_gdbus_i2c.c index ce42ce5..2e176ef 100644 --- a/src/peripheral_gdbus_i2c.c +++ b/src/peripheral_gdbus_i2c.c @@ -29,8 +29,10 @@ void i2c_proxy_init(void) { GError *error = NULL; - if (i2c_proxy != NULL) + if (i2c_proxy != NULL) { + g_object_ref(i2c_proxy); return; + } i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync( G_BUS_TYPE_SYSTEM, @@ -45,7 +47,6 @@ void i2c_proxy_deinit() { if (i2c_proxy) { g_object_unref(i2c_proxy); - i2c_proxy = NULL; } } diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 1467c8f..31b2a78 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -29,8 +29,10 @@ void pwm_proxy_init(void) { GError *error = NULL; - if (pwm_proxy != NULL) + if (pwm_proxy != NULL) { + g_object_ref(pwm_proxy); return; + } pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync( G_BUS_TYPE_SYSTEM, @@ -45,7 +47,6 @@ void pwm_proxy_deinit() { if (pwm_proxy) { g_object_unref(pwm_proxy); - pwm_proxy = NULL; } } diff --git a/src/peripheral_gdbus_uart.c b/src/peripheral_gdbus_uart.c index fc94a89..cf51b22 100644 --- a/src/peripheral_gdbus_uart.c +++ b/src/peripheral_gdbus_uart.c @@ -29,8 +29,10 @@ void uart_proxy_init(void) { GError *error = NULL; - if (uart_proxy != NULL) + if (uart_proxy != NULL) { + g_object_ref(uart_proxy); return; + } uart_proxy = peripheral_io_gdbus_uart_proxy_new_for_bus_sync( G_BUS_TYPE_SYSTEM, @@ -45,7 +47,6 @@ void uart_proxy_deinit() { if (uart_proxy) { g_object_unref(uart_proxy); - uart_proxy = NULL; } } -- 2.7.4 From 4f70ec201719542d860af4cd100774a05c6ad7c2 Mon Sep 17 00:00:00 2001 From: Sungguk Na Date: Thu, 25 May 2017 14:14:22 +0900 Subject: [PATCH 10/16] Additional fix to handle reference count of proxy Proxy object should be NULL after proxy deinit, but g_object_unref just make free the object and do not make it NULL Change-Id: I5ca2c14606527f5610181d6470d099fcf681c8cd Signed-off-by: Sungguk Na --- src/peripheral_gdbus_gpio.c | 2 ++ src/peripheral_gdbus_i2c.c | 2 ++ src/peripheral_gdbus_pwm.c | 2 ++ src/peripheral_gdbus_uart.c | 2 ++ 4 files changed, 8 insertions(+) diff --git a/src/peripheral_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c index 1c0e90f..0bbe4f7 100644 --- a/src/peripheral_gdbus_gpio.c +++ b/src/peripheral_gdbus_gpio.c @@ -60,6 +60,8 @@ void gpio_proxy_deinit() { if (gpio_proxy) { g_object_unref(gpio_proxy); + if (!G_IS_OBJECT(gpio_proxy)) + gpio_proxy = NULL; } } diff --git a/src/peripheral_gdbus_i2c.c b/src/peripheral_gdbus_i2c.c index 2e176ef..c30399b 100644 --- a/src/peripheral_gdbus_i2c.c +++ b/src/peripheral_gdbus_i2c.c @@ -47,6 +47,8 @@ void i2c_proxy_deinit() { if (i2c_proxy) { g_object_unref(i2c_proxy); + if (!G_IS_OBJECT(i2c_proxy)) + i2c_proxy = NULL; } } diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index 31b2a78..a3b8b51 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -47,6 +47,8 @@ void pwm_proxy_deinit() { if (pwm_proxy) { g_object_unref(pwm_proxy); + if (!G_IS_OBJECT(pwm_proxy)) + pwm_proxy = NULL; } } diff --git a/src/peripheral_gdbus_uart.c b/src/peripheral_gdbus_uart.c index cf51b22..0a80e72 100644 --- a/src/peripheral_gdbus_uart.c +++ b/src/peripheral_gdbus_uart.c @@ -47,6 +47,8 @@ void uart_proxy_deinit() { if (uart_proxy) { g_object_unref(uart_proxy); + if (!G_IS_OBJECT(uart_proxy)) + uart_proxy = NULL; } } -- 2.7.4 From 46b2e09e69cce2257a90efdf5b67a5c3ce3c6f41 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 24 May 2017 16:31:03 +0900 Subject: [PATCH 11/16] Add GPIO unit test This patch also implements menu for each tc module and utilizes glib IO channel to receive user input. Change-Id: Iedce7feab1887c822b172c30aba58b0f0eab959c Signed-off-by: Hyeongsik Min --- test/peripheral-io-test.c | 696 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 548 insertions(+), 148 deletions(-) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index a787e10..163ffa5 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * Copyright (c) 2017 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,28 +14,59 @@ * limitations under the License. */ -#include "peripheral_io.h" - #include #include #include +#include #include -extern int gpio_test(); -extern int i2c_test(); -extern int adc_test(); +#include "peripheral_io.h" + +#define BUFFER_LEN 32 -GMainLoop *loop; +typedef struct { + const char *tc_name; + int tc_code; + int (*tc_func)(void); +} tc_table_t; + +tc_table_t *tc_table; + +GMainLoop *main_loop; +GList *gpio_list; +GList *i2c_list; +GList *pwm_list; +GList *adc_list; +GList *uart_list; +GList *spi_list; + +int read_int_input(int *input) +{ + char buf[BUFFER_LEN]; + int rv; + + rv = read(0, buf, BUFFER_LEN); + + /* Ignore Enter without value */ + if (*buf == '\n' || *buf == '\r') { + printf("No input value\n"); + return -1; + } + + if (rv < 0) return -1; + *input = atoi(buf); + + return 0; +} -int gpio_test(void) +int gpio_led_test(void) { int num; int cnt = 0; peripheral_gpio_h handle = NULL; - printf("artik5 : 135 \n"); - printf("artik10 : 22 \n"); - printf(">> PIN NUMBER : "); + printf(" %s()\n", __func__); + printf("Enter GPIO pin number "); if (scanf("%d", &num) < 0) return 0; @@ -52,13 +83,13 @@ int gpio_test(void) } while (cnt++ < 5) { - printf("write~\n"); + printf("Writing..\n"); peripheral_gpio_write(handle, 1); sleep(1); peripheral_gpio_write(handle, 0); sleep(1); } - printf("write finish\n"); + printf("Write finish\n"); peripheral_gpio_close(handle); return 1; @@ -74,70 +105,66 @@ void gpio_irq_test_isr(void *user_data) peripheral_gpio_get_pin(gpio, &pin); - printf("gpio_irq_test_isr: GPIO %d interrupt occurs.\n", pin); + printf("%s: GPIO %d interrupt occurs.\n", __func__, pin); } -void *gpio_irq_test_thread(void *data) +int gpio_irq_register(void) { - peripheral_gpio_h gpio = data; - int num; - - printf(">> Press any key to exit GPIO IRQ Test : \n"); - if (scanf("%d", &num) < 0) - return 0; - - peripheral_gpio_unregister_cb(gpio); - peripheral_gpio_close(gpio); - - g_main_loop_quit(loop); - return 0; -} - -int gpio_irq_test(void) -{ - GThread *test_thread; - int num; peripheral_gpio_h gpio = NULL; - peripheral_gpio_edge_e edge = PERIPHERAL_GPIO_EDGE_NONE; + int pin, ret; - printf("artik710 : 27 \n"); - printf(">> PIN NUMBER : "); + printf(" %s()\n", __func__); + printf("Enter gpio pin number\n"); - if (scanf("%d", &num) < 0) - return 0; + if (read_int_input(&pin) < 0) + return -1; - if (peripheral_gpio_open(num, &gpio) != PERIPHERAL_ERROR_NONE) { - printf("test dev is null\n"); - return 0; + if ((ret = peripheral_gpio_open(pin, &gpio)) < 0) { + printf(">>>>> Failed to open GPIO pin, ret : %d\n", ret); + return -1; } + gpio_list = g_list_append(gpio_list, gpio); - if (peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN) != 0) { - printf("test set direction error!!!"); + if ((ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN)) < 0) { + printf(">>>>> Failed to set direction, ret : %d\n", ret); goto error; } + peripheral_gpio_set_edge_mode(gpio, PERIPHERAL_GPIO_EDGE_BOTH); + peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio); - printf(">> Select Edge Mode (0 = None, 1 = Falling, 2 = Rising, 3 = Both) : "); - if (scanf("%d", &num) < 0) - return 0; + return 0; - if (num >= 0 && num <= 3) - edge = num; +error: + gpio_list = g_list_remove(gpio_list, gpio); + peripheral_gpio_close(gpio); + return -1; +} - peripheral_gpio_set_edge_mode( gpio, edge); - peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio); +int gpio_irq_unregister(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + int gpio_test_get_handle_by_pin(int pin, peripheral_gpio_h *gpio); - test_thread = g_thread_new("key input thread", &gpio_irq_test_thread, gpio); - loop = g_main_loop_new(NULL, FALSE); - g_main_loop_run(loop); + printf(" %s()\n", __func__); + printf("Enter gpio pin number\n"); - g_thread_join(test_thread); - if (loop != NULL) - g_main_loop_unref(loop); + if (read_int_input(&pin) < 0) + return -1; - return 0; + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> cannot find handle. please open the gpio pin.\n"); + return -1; + } -error: + if ((ret = peripheral_gpio_unregister_cb(gpio)) < 0) { + printf(">>>>> failed to unregister callback function, ret : %d\n", ret); + return -1; + } + + gpio_list = g_list_remove(gpio_list, gpio); peripheral_gpio_close(gpio); + return 0; } @@ -149,66 +176,45 @@ error: #define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2) -int i2c_test(void) +int i2c_gy30_test(void) { int cnt = 0; - int bus_num; + int bus_num, ret; unsigned char buf[10]; peripheral_i2c_h i2c; - printf(">> I2C bus number : "); + printf(" %s()\n", __func__); + printf("Enter I2C bus number : "); if (scanf("%d", &bus_num) < 0) - return 0; + return -1; - if ((peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) != 0) { - printf("Failed to open I2C communication\n"); - return 0; + if ((ret = peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) < 0) { + printf("Failed to open I2C communication, ret : %d\n", ret); + return -1; } buf[0] = GY30_CONT_HIGH_RES_MODE; - if (peripheral_i2c_write(i2c, buf, 1) != 0) { - printf("Failed to write\n"); + if ((ret = peripheral_i2c_write(i2c, buf, 1)) < 0) { + printf("Failed to write, ret : %d\n", ret); goto error; } while (cnt++ < 15) { int result; sleep(1); - peripheral_i2c_read(i2c, buf, 2); + ret = peripheral_i2c_read(i2c, buf, 2); + if (ret < 0) + printf("Failed to read, ret : %d\n", ret); result = GY30_READ_INTENSITY(buf); - printf("Result [%d]\n", result); + printf("Light intensity : %d\n", result); } peripheral_i2c_close(i2c); - return 1; + return 0; error: peripheral_i2c_close(i2c); - return 0; -} - -int adc_test(void) -{ -#if 0 - int channel = 0; - int data = 0; - adc_context_h dev = NULL; - - printf(">>channel :"); - scanf("%d", &channel); - - dev = peripheral_adc_open(channel); - - if (!dev) { - printf("open error!\n"); - return 1; - } - - peripheral_adc_read(dev, &data); - - peripheral_adc_close(dev); -#endif - return 1; + return -1; } int pwm_test_led(void) @@ -222,7 +228,7 @@ int pwm_test_led(void) int get_period, get_duty_cycle; peripheral_pwm_h dev; - printf("<<< pwm_test >>>\n"); + printf(" %s()\n", __func__); peripheral_pwm_open(device, channel, &dev); peripheral_pwm_set_period(dev, period); /* period: nanosecond */ @@ -263,7 +269,7 @@ int pwm_test_motor(void) int degree[3] = {0, 45, 90}; peripheral_pwm_h dev; - printf("<<< pwm_test_motor >>>\n"); + printf(" %s()\n", __func__); peripheral_pwm_open(device, channel, &dev); for (cnt = 0; cnt < 5; cnt++) { @@ -305,25 +311,27 @@ int uart_test_accelerometer(void) int loop = 100; unsigned char buf[1024]; - printf("<<< uart_test_accelerometer >>>\n"); - printf("artik710 : 4 \n"); - printf(">> PORT NUMBER : "); + printf(" %s()\n", __func__); + printf("Enter port number"); if (scanf("%d", &port) < 0) - return 0; + return -1; ret = peripheral_uart_open(port, &uart); if (ret < 0) goto err_open; + ret = peripheral_uart_set_baudrate(uart, PERIPHERAL_UART_BAUDRATE_4800); if (ret < 0) goto out; + ret = peripheral_uart_set_mode(uart, PERIPHERAL_UART_BYTESIZE_8BIT, PERIPHERAL_UART_PARITY_NONE, PERIPHERAL_UART_STOPBITS_1BIT); if (ret < 0) goto out; + ret = peripheral_uart_set_flowcontrol(uart, true, false); if (ret < 0) goto out; @@ -347,65 +355,457 @@ int uart_test_accelerometer(void) usleep(100000); } + peripheral_uart_close(uart); + return 0; + out: peripheral_uart_close(uart); err_open: - printf(">> ret = %d\n", ret); + return -1; +} + +int gpio_test_get_handle_by_pin(int pin, peripheral_gpio_h *gpio) +{ + peripheral_gpio_h handle; + GList *cursor; + int cur_pin; + + cursor = gpio_list; + while (cursor) { + handle = (peripheral_gpio_h)cursor->data; + peripheral_gpio_get_pin(handle, &cur_pin); + if (pin == cur_pin) + break; + cursor = g_list_next(cursor); + } + if (!cursor) return -1; + + *gpio = handle; + + return 0; +} + +int print_gpio_handle(void) +{ + peripheral_gpio_h gpio; + GList *cursor; + peripheral_gpio_direction_e direction; + peripheral_gpio_edge_e edge; + int pin, value; + char *dir_str, *edge_str; + + printf("--- GPIO handle info. -------------------------------------------\n"); + printf(" No Pin Direction Value Edge mode\n"); + + cursor = gpio_list; + while (cursor) { + gpio = (peripheral_gpio_h)cursor->data; + + if (peripheral_gpio_get_pin(gpio, &pin) < 0) + continue; + if (peripheral_gpio_get_direction(gpio, &direction) < 0) + continue; + if (peripheral_gpio_get_edge_mode(gpio, &edge) < 0) + continue; + + if (direction == PERIPHERAL_GPIO_DIRECTION_IN) + dir_str = "IN"; + else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_LOW) + dir_str = "OUT_LOW"; + else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) + dir_str = "OUT_HIGH"; + else + dir_str = "UNKNOWN"; + + if (edge == PERIPHERAL_GPIO_EDGE_NONE) + edge_str = "NONE"; + else if (edge == PERIPHERAL_GPIO_EDGE_RISING) + edge_str = "RISING"; + else if (edge == PERIPHERAL_GPIO_EDGE_FALLING) + edge_str = "FALLING"; + else if (edge == PERIPHERAL_GPIO_EDGE_BOTH) + edge_str = "BOTH"; + else + edge_str = "UNKNOWN"; + + if (direction == PERIPHERAL_GPIO_DIRECTION_IN) { + if (peripheral_gpio_read(gpio, &value) < 0) + continue; + printf("%8d%8d%12s%8d%12s\n", g_list_position(gpio_list, cursor), + pin, dir_str, value, edge_str); + } else + printf("%8d%8d%12s%20s\n", g_list_position(gpio_list, cursor), + pin, dir_str, edge_str); + + cursor = g_list_next(cursor); + } + printf("-----------------------------------------------------------------\n"); + + return 0; +} + +int gpio_test_open(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + + printf("%s\n", __func__); + printf("Enter GPIO pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if ((ret = peripheral_gpio_open(pin, &gpio)) < 0) { + printf(">>>>> GPIO open failed, ret : %d\n", ret); + return -1; + } + gpio_list = g_list_append(gpio_list, gpio); + print_gpio_handle(); + + return 0; +} + +int gpio_test_close(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + + printf("%s\n", __func__); + printf("Enter GPIO pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + gpio_list = g_list_remove(gpio_list, gpio); + + if ((ret = peripheral_gpio_close(gpio)) < 0) { + printf(">>>>> GPIO close failed, ret : %d\n", ret); + return -1; + } + print_gpio_handle(); + + return 0; +} + +int gpio_test_set_direction(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + int direction; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + printf("Enter direction (0:IN, 1:OUT_LOW, 2:OUT_HIGH)\n"); + if (read_int_input(&direction) < 0) + return -1; + + if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH || + direction < PERIPHERAL_GPIO_DIRECTION_IN) { + printf(">>>>> Wrong input value\n"); + return -1; + } + + if ((ret = peripheral_gpio_set_direction(gpio, (peripheral_gpio_direction_e)direction)) < 0) { + printf(">>>>> Failed to set direction, ret : %d\n", ret); + return -1; + } + + return 0; +} + +int gpio_test_write(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + int value; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + printf("Enter value (0:LOW, 1:HIGH)\n"); + if (read_int_input(&value) < 0) + return -1; + + if (value < 0 || value > 1) { + printf(">>>>> Wrong input value\n"); + return -1; + } + + if ((ret = peripheral_gpio_write(gpio, value)) < 0) { + printf(">>>>> Failed to write value, ret : %d\n", ret); + return -1; + } + + return 0; +} + +int gpio_test_set_edge_mode(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + int edge; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + printf("Enter edge mode (0:NONE, 1:RISING, 2:FALLING, 3:BOTH)\n"); + if (read_int_input(&edge) < 0) + return -1; + + if (edge < PERIPHERAL_GPIO_EDGE_NONE || edge > PERIPHERAL_GPIO_EDGE_BOTH) { + printf(">>>>> Wrong input value\n"); + return -1; + } + + if ((ret = peripheral_gpio_set_edge_mode(gpio, (peripheral_gpio_edge_e)edge)) < 0) { + printf(">>>>> Failed to set edge mode, ret : %d\n", ret); + return -1; + } + + return 0; +} + +int gpio_test_set_register_cb(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + if ((ret = peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio)) < 0) { + printf(">>>>> Failed to register callback function, ret : %d\n", ret); + return -1; + } + return 0; +} + +int gpio_test_set_unregister_cb(void) +{ + peripheral_gpio_h gpio; + int pin, ret; + + printf("%s\n", __func__); + printf("Enter gpio pin number\n"); + + if (read_int_input(&pin) < 0) + return -1; + + if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) { + printf(">>>>> Cannot find handle. Please open the GPIO pin\n"); + return -1; + } + + if ((ret = peripheral_gpio_unregister_cb(gpio)) < 0) { + printf(">>>>> failed to unregister callback function, ret : %d\n", ret); + return -1; + } + return 0; } +int enter_main(void); + +tc_table_t gpio_tc_table[] = { + {"Open GPIO pin", 1, gpio_test_open}, + {"Close GPIO pin", 2, gpio_test_close}, + {"Set direction GPIO pin", 3, gpio_test_set_direction}, + {"Write value to GPIO pin", 4, gpio_test_write}, + {"Set edge mode", 5, gpio_test_set_edge_mode}, + {"Register callback", 6, gpio_test_set_register_cb}, + {"Unregister callback", 7, gpio_test_set_unregister_cb}, + {"Print GPIO handle", 9, print_gpio_handle}, + {"Go back to main", 0, enter_main}, + {NULL, 0, NULL}, +}; + +int enter_gpio_test(void) +{ + tc_table = gpio_tc_table; + print_gpio_handle(); + + return 0; +} + +int enter_i2c_test(void) +{ + return 0; +} + +int enter_pwm_test(void) +{ + return 0; +} + +int enter_adc_test(void) +{ + return 0; +} + +int enter_uart_test(void) +{ + return 0; +} + +int enter_spi_test(void) +{ + return 0; +} + +int terminate_test(void) +{ + int ret = 0; + + printf("Terminate test\n"); + g_main_loop_quit(main_loop); + + exit(1); + + return ret; +} + +tc_table_t main_tc_table[] = { + {"GPIO Test Menu", 1, enter_gpio_test}, + {"I2C Test Menu", 2, enter_i2c_test}, + {"PWM Test Menu", 3, enter_pwm_test}, + {"ADC Test Menu", 4, enter_adc_test}, + {"UART Test Menu", 5, enter_uart_test}, + {"SPI Test Menu", 6, enter_spi_test}, + {"[Preset Test] GPIO LED", 11, gpio_led_test}, + {"[Preset Test] I2C GY30 Light sensor", 12, i2c_gy30_test}, + {"[Preset Test] PWM LED", 14, pwm_test_led}, + {"[Preset Test] PWM Motor", 15, pwm_test_motor}, + {"[Preset Test] Uart Accelerometer", 16, uart_test_accelerometer}, + {"[Preset Test] GPIO IRQ register", 17, gpio_irq_register}, + {"[Preset Test] GPIO IRQ unregister", 18, gpio_irq_unregister}, + {"Exit Test", 0, terminate_test}, + {NULL, 0, NULL}, +}; + +int enter_main(void) +{ + tc_table = main_tc_table; + + return 0; +} + +static int test_input_callback(void *data) +{ + long test_id = (long)data; + int ret = PERIPHERAL_ERROR_NONE; + int i = 0; + + while (tc_table[i].tc_name) { + if (tc_table[i].tc_code == test_id) { + ret = tc_table[i].tc_func(); + + if (ret != PERIPHERAL_ERROR_NONE) + printf(">>>>> Test Error Returned !!! : %d\n", ret); + + break; + } + i++; + } + if (!tc_table[i].tc_name) { + printf(">>>>> Wrong input value!\n"); + return -1; + } + + return 0; +} + +static void print_tc_usage(void) +{ + int i = 0; + + printf("===========================================\n"); + while (tc_table[i].tc_name) { + printf(" %2d : %s\n", tc_table[i].tc_code, tc_table[i].tc_name); + i++; + } + printf("===========================================\n"); + printf("Enter TC number\n"); +} + +static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data) +{ + char buf[BUFFER_LEN] = {0,}; + long test_id; + int rv = 0; + + memset(buf, 0, sizeof(buf)); + + rv = read(0, buf, BUFFER_LEN); + + if (*buf == '\n' || *buf == '\r') { + print_tc_usage(); + return TRUE; + } + + if (rv < 0) return FALSE; + + test_id = atoi(buf); + + test_input_callback((void *)test_id); + print_tc_usage(); + + return TRUE; +} + int main(int argc, char **argv) { - int num = 1; - int ret; + GIOChannel *key_io; - printf("===================\n"); - printf(" test Menu\n"); - printf("===================\n"); - printf(" 1. GPIO Test\n"); - printf(" 2. I2C Test\n"); - printf(" 3. pwm led test\n"); - printf(" 4. pwm motor test\n"); - printf(" 5. uart accelerometer test\n"); + main_loop = g_main_loop_new(NULL, FALSE); + key_io = g_io_channel_unix_new(0); - printf(" 11. GPIO Interrupt Test\n"); - printf(" 12. H/W IF I2C Test\n"); - printf(" 13. H/W IF PWM Test\n"); - printf(" 14. H/W IF SPI Test\n"); + tc_table = main_tc_table; - if (scanf("%d", &num) < 0) - return 0; + print_tc_usage(); + g_io_add_watch(key_io, (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL), key_event_cb, NULL); - switch (num) { - case 1: - ret = gpio_test(); - break; - case 2: - ret = i2c_test(); - break; - case 3: - ret = pwm_test_led(); - break; - case 4: - ret = pwm_test_motor(); - break; - case 5: - ret = uart_test_accelerometer(); - break; - case 11: - ret = gpio_irq_test(); - break; - case 12: - ret = i2c_test(); - break; - case 14: - ret = adc_test(); - break; - default: - printf("Not support \n"); - } - printf(" return : %d\n", ret); + g_main_loop_run(main_loop); - return 1; + g_io_channel_unref(key_io); + g_main_loop_unref(main_loop); + + return 0; } -- 2.7.4 From 26272320cf0cf2d34c7272d72f62e95c18907fa9 Mon Sep 17 00:00:00 2001 From: Sungguk Na Date: Thu, 25 May 2017 19:34:13 +0900 Subject: [PATCH 12/16] Change argument of gpio gdbus method - The gpio methods will pass handle instead of device informations. - Some arguments of gpio handle are removed. Change-Id: I6a0910cbd5e03878c17e5c6426850dbd2865ada4 Signed-off-by: Sungguk Na --- include/peripheral_internal.h | 3 +-- src/peripheral_gdbus_gpio.c | 25 ++++++++++--------------- src/peripheral_gpio.c | 8 -------- src/peripheral_io.xml | 21 ++++++++++----------- 4 files changed, 21 insertions(+), 36 deletions(-) diff --git a/include/peripheral_internal.h b/include/peripheral_internal.h index 85263aa..34f09b0 100644 --- a/include/peripheral_internal.h +++ b/include/peripheral_internal.h @@ -22,8 +22,7 @@ */ struct _peripheral_gpio_s { int pin; - peripheral_gpio_direction_e direction; - peripheral_gpio_edge_e edge; + uint handle; }; /** diff --git a/src/peripheral_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c index 0bbe4f7..721b3d4 100644 --- a/src/peripheral_gdbus_gpio.c +++ b/src/peripheral_gdbus_gpio.c @@ -89,8 +89,7 @@ int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio) if (peripheral_io_gdbus_gpio_call_open_sync( gpio_proxy, gpio->pin, - (gint*)&gpio->edge, - (gint*)&gpio->direction, + &gpio->handle, &ret, NULL, &error) == FALSE) { @@ -111,7 +110,7 @@ int peripheral_gdbus_gpio_close(peripheral_gpio_h gpio) if (peripheral_io_gdbus_gpio_call_close_sync( gpio_proxy, - gpio->pin, + gpio->handle, &ret, NULL, &error) == FALSE) { @@ -132,7 +131,7 @@ int peripheral_gdbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_ if (peripheral_io_gdbus_gpio_call_get_direction_sync( gpio_proxy, - gpio->pin, + gpio->handle, (gint*)direction, &ret, NULL, @@ -141,7 +140,6 @@ int peripheral_gdbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_ g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - gpio->direction = *direction; return ret; } @@ -155,7 +153,7 @@ int peripheral_gdbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_ if (peripheral_io_gdbus_gpio_call_set_direction_sync( gpio_proxy, - gpio->pin, + gpio->handle, direction, &ret, NULL, @@ -164,7 +162,6 @@ int peripheral_gdbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_ g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - gpio->direction = direction; return ret; } @@ -178,7 +175,7 @@ int peripheral_gdbus_gpio_read(peripheral_gpio_h gpio, int *value) if (peripheral_io_gdbus_gpio_call_read_sync( gpio_proxy, - gpio->pin, + gpio->handle, value, &ret, NULL, @@ -200,7 +197,7 @@ int peripheral_gdbus_gpio_write(peripheral_gpio_h gpio, int value) if (peripheral_io_gdbus_gpio_call_write_sync( gpio_proxy, - gpio->pin, + gpio->handle, value, &ret, NULL, @@ -222,7 +219,7 @@ int peripheral_gdbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ if (peripheral_io_gdbus_gpio_call_get_edge_mode_sync( gpio_proxy, - gpio->pin, + gpio->handle, (int*)edge, &ret, NULL, @@ -231,7 +228,6 @@ int peripheral_gdbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - gpio->edge = *edge; return ret; } @@ -245,7 +241,7 @@ int peripheral_gdbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ if (peripheral_io_gdbus_gpio_call_set_edge_mode_sync( gpio_proxy, - gpio->pin, + gpio->handle, edge, &ret, NULL, @@ -254,7 +250,6 @@ int peripheral_gdbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - gpio->edge = edge; return ret; } @@ -268,7 +263,7 @@ int peripheral_gdbus_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callba if (peripheral_io_gdbus_gpio_call_register_irq_sync( gpio_proxy, - gpio->pin, + gpio->handle, &ret, NULL, &error) == FALSE) { @@ -289,7 +284,7 @@ int peripheral_gdbus_gpio_unregister_cb(peripheral_gpio_h gpio) if (peripheral_io_gdbus_gpio_call_unregister_irq_sync( gpio_proxy, - gpio->pin, + gpio->handle, &ret, NULL, &error) == FALSE) { diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 2b3bd20..e49477c 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -176,8 +176,6 @@ int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direct return PERIPHERAL_ERROR_INVALID_PARAMETER; ret = peripheral_gdbus_gpio_get_direction(gpio, direction); - if (ret == PERIPHERAL_ERROR_NONE) - gpio->direction = (*direction); return ret; } @@ -199,8 +197,6 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct /* call gpio_set_direction */ ret = peripheral_gdbus_gpio_set_direction(gpio, direction); - if (ret == PERIPHERAL_ERROR_NONE) - gpio->direction = direction; return ret; } @@ -251,8 +247,6 @@ int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e return PERIPHERAL_ERROR_INVALID_PARAMETER; ret = peripheral_gdbus_gpio_get_edge_mode(gpio, edge); - if (ret == PERIPHERAL_ERROR_NONE) - gpio->edge = (*edge); return ret; } @@ -273,8 +267,6 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e /* call gpio_set_edge_mode */ ret = peripheral_gdbus_gpio_set_edge_mode(gpio, edge); - if (ret == PERIPHERAL_ERROR_NONE) - gpio->edge = edge; return ret; } diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index abc3679..2480f92 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -3,50 +3,49 @@ - - + - + - + - + - + - + - + - + - + - + -- 2.7.4 From 1afae1b2e619a3cfca9697930daac0a16443efc1 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 18 May 2017 19:25:15 +0900 Subject: [PATCH 13/16] Add description for pwm APIs This patch Adds description for pwm APIs. Change-Id: I1dc40bf40e4405616c5f719d905680bf31d783e0 Signed-off-by: jino.cho --- include/peripheral_io.h | 157 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 156 insertions(+), 1 deletion(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 1000993..c68ff98 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -383,25 +383,180 @@ typedef enum { PERIPHERAL_PWM_POLARITY_INVERSED, } peripheral_pwm_polarity_e; +/** + * @brief Initializes(export) pwm device and creates pwm handle. + * @since_tizen 4.0 + * + * @param[in] device The pwm chip number + * @param[in] channel The pwm channel number to control + * @param[out] pwm The pwm handle is created on success + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed + * @retval #PERIPHERAL_ERROR_RESOURCE_BUSY Device is in use + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_pwm_close() + */ int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm); +/** + * @brief Destory the pwm handle and finalize(unexport) the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_pwm_open() + */ int peripheral_pwm_close(peripheral_pwm_h pwm); - +/** + * @brief Sets Period of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[in] period The total period of the PWM signal (in nanoseconds) + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period); +/** + * @brief Gets Period of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[out] period The total period of the PWM signal (in nanoseconds) + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period); +/** + * @brief Sets Duty Cycle of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[in] duty_cycle The active time of the pwm signal (in nanoseconds) + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle); +/** + * @brief Gets Duty Cycle of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[out] duty_cycle The active time of the pwm signal (in nanoseconds) + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle); +/** + * @brief Sets Polarity of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[in] polarity The polarity of the pwm signal + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity); +/** + * @brief Gets Polarity of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[out] polarity The polarity of the pwm signal + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity); +/** + * @brief Enable of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[in] enable Enable/disable the pwm signal + * true - enable + * false - disable + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable); +/** + * @brief Gets Enable status of the pwm device. + * @since_tizen 4.0 + * + * @param[in] pwm The handle to the pwm device + * @param[out] enable Enable/disable the pwm signal + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + */ int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable); /** -- 2.7.4 From df9f456867311136429eb4d41cddcf55d5f9feeb Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Thu, 25 May 2017 20:51:33 +0900 Subject: [PATCH 14/16] Add preset test for MMA7455 accel. sensor Change-Id: I51ed963015963f48d8b78db80a4ff6c4ac0bfc6f Signed-off-by: Hyeongsik Min --- test/peripheral-io-test.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 163ffa5..fb90703 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -217,6 +217,76 @@ error: return -1; } +#define MMA_7455_ADDRESS 0x1D //I2C Address for the sensor +#define MMA_7455_MODE_CONTROL 0x16 //Call the sensors Mode Control + +#define MMA_7455_2G_MODE 0x04 //Set Sensitivity to 2g +#define MMA_7455_4G_MODE 0x08 //Set Sensitivity to 4g +#define MMA_7455_8G_MODE 0x00 //Set Sensitivity to 8g + +#define MMA_7455_STANDBY_MODE 0x0 +#define MMA_7455_MEASUREMENT_MODE 0x1 +#define MMA_7455_LEVEL_DETECTION_MODE 0x2 +#define MMA_7455_PULSE_DETECTION_MODE 0x3 + +#define X_OUT 0x06 //Register for reading the X-Axis +#define Y_OUT 0x07 //Register for reading the Y-Axis +#define Z_OUT 0x08 //Register for reading the Z-Axis + +int i2c_mma7455_test(void) +{ + int cnt = 0; + int bus_num, ret; + unsigned char buf[10]; + peripheral_i2c_h i2c; + + printf(" %s()\n", __func__); + printf("Enter I2C bus number"); + if (scanf("%d", &bus_num) < 0) + return -1; + + if ((ret = peripheral_i2c_open(bus_num, MMA_7455_ADDRESS, &i2c)) < 0) { + printf(">>>>> Failed to open I2C communication, ret : %d \n", ret); + return -1; + } + + buf[0] = MMA_7455_MODE_CONTROL; + buf[1] = MMA_7455_2G_MODE | MMA_7455_LEVEL_DETECTION_MODE; + if ((ret = peripheral_i2c_write(i2c, buf, 2)) != 0) { + printf(">>>>> Failed to write, ret : %d\n", ret); + goto error; + } + + while (cnt++ < 10) { + int x_pos, y_pos, z_pos; + sleep(1); + + buf[0] = X_OUT; + peripheral_i2c_write(i2c, buf, 1); + peripheral_i2c_read(i2c, buf, 1); + x_pos = (int)buf[0]; + + buf[0] = Y_OUT; + peripheral_i2c_write(i2c, buf, 1); + peripheral_i2c_read(i2c, buf, 1); + y_pos = (int)buf[0]; + + buf[0] = Z_OUT; + peripheral_i2c_write(i2c, buf, 1); + peripheral_i2c_read(i2c, buf, 1); + z_pos = (int)buf[0]; + + printf("Result X : %d, Y : %d, Z : %d\n", x_pos, y_pos, z_pos); + } + + peripheral_i2c_close(i2c); + return 0; + +error: + peripheral_i2c_close(i2c); + return -1; +} + int pwm_test_led(void) { int device = 0, channel = 0; @@ -711,6 +781,7 @@ tc_table_t main_tc_table[] = { {"SPI Test Menu", 6, enter_spi_test}, {"[Preset Test] GPIO LED", 11, gpio_led_test}, {"[Preset Test] I2C GY30 Light sensor", 12, i2c_gy30_test}, + {"[Preset Test] I2C MMA7455 Accel. sensor", 13, i2c_mma7455_test}, {"[Preset Test] PWM LED", 14, pwm_test_led}, {"[Preset Test] PWM Motor", 15, pwm_test_motor}, {"[Preset Test] Uart Accelerometer", 16, uart_test_accelerometer}, -- 2.7.4 From a494140fba1f58594a2d51f9a417b2a99bed43ef Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 29 May 2017 19:27:55 +0900 Subject: [PATCH 15/16] Add sub menu for preset test Change-Id: I5dd21166781377d15df3535be6fb2212f886203c Signed-off-by: Hyeongsik Min --- test/peripheral-io-test.c | 62 ++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index fb90703..fa7e18e 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -61,7 +61,7 @@ int read_int_input(int *input) int gpio_led_test(void) { - int num; + int num, ret; int cnt = 0; peripheral_gpio_h handle = NULL; @@ -69,16 +69,16 @@ int gpio_led_test(void) printf("Enter GPIO pin number "); if (scanf("%d", &num) < 0) - return 0; + return -1; printf("num %d\n", num); - if (peripheral_gpio_open(num, &handle) != PERIPHERAL_ERROR_NONE) { - printf("handle is null\n"); - return 0; + if ((ret = peripheral_gpio_open(num, &handle)) < PERIPHERAL_ERROR_NONE) { + printf("Failed to open\n"); + return ret; } - if (peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT) != PERIPHERAL_ERROR_NONE) { - printf("set direction error!!!"); + if ((ret = peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT)) < PERIPHERAL_ERROR_NONE) { + printf("Failed to set direction!!\n"); goto error; } @@ -90,12 +90,16 @@ int gpio_led_test(void) sleep(1); } printf("Write finish\n"); - peripheral_gpio_close(handle); - return 1; + if ((ret = peripheral_gpio_close(handle)) < PERIPHERAL_ERROR_NONE) { + printf("Failed to close the pin\n"); + return ret; + } + + return 0; error: peripheral_gpio_close(handle); - return 0; + return ret; } void gpio_irq_test_isr(void *user_data) @@ -760,6 +764,25 @@ int enter_spi_test(void) return 0; } +tc_table_t preset_tc_table[] = { + {"[Preset Test] GPIO LED", 1, gpio_led_test}, + {"[Preset Test] I2C GY30 Light sensor", 2, i2c_gy30_test}, + {"[Preset Test] I2C MMA7455 Accel. sensor", 3, i2c_mma7455_test}, + {"[Preset Test] PWM LED", 4, pwm_test_led}, + {"[Preset Test] PWM Motor", 5, pwm_test_motor}, + {"[Preset Test] Uart Accelerometer", 6, uart_test_accelerometer}, + {"[Preset Test] GPIO IRQ register", 7, gpio_irq_register}, + {"[Preset Test] GPIO IRQ unregister", 8, gpio_irq_unregister}, + {"Go back to main", 0, enter_main}, + {NULL, 0, NULL}, +}; + +int enter_preset_test(void) +{ + tc_table = preset_tc_table; + return 0; +} + int terminate_test(void) { int ret = 0; @@ -779,14 +802,7 @@ tc_table_t main_tc_table[] = { {"ADC Test Menu", 4, enter_adc_test}, {"UART Test Menu", 5, enter_uart_test}, {"SPI Test Menu", 6, enter_spi_test}, - {"[Preset Test] GPIO LED", 11, gpio_led_test}, - {"[Preset Test] I2C GY30 Light sensor", 12, i2c_gy30_test}, - {"[Preset Test] I2C MMA7455 Accel. sensor", 13, i2c_mma7455_test}, - {"[Preset Test] PWM LED", 14, pwm_test_led}, - {"[Preset Test] PWM Motor", 15, pwm_test_motor}, - {"[Preset Test] Uart Accelerometer", 16, uart_test_accelerometer}, - {"[Preset Test] GPIO IRQ register", 17, gpio_irq_register}, - {"[Preset Test] GPIO IRQ unregister", 18, gpio_irq_unregister}, + {"Preset Test", 10, enter_preset_test}, {"Exit Test", 0, terminate_test}, {NULL, 0, NULL}, }; @@ -800,14 +816,16 @@ int enter_main(void) static int test_input_callback(void *data) { + tc_table_t *tc; long test_id = (long)data; int ret = PERIPHERAL_ERROR_NONE; int i = 0; - while (tc_table[i].tc_name) { - if (tc_table[i].tc_code == test_id) { - ret = tc_table[i].tc_func(); + tc = tc_table; + while (tc[i].tc_name) { + if (tc[i].tc_code == test_id && tc[i].tc_func) { + ret = tc[i].tc_func(); if (ret != PERIPHERAL_ERROR_NONE) printf(">>>>> Test Error Returned !!! : %d\n", ret); @@ -815,7 +833,7 @@ static int test_input_callback(void *data) } i++; } - if (!tc_table[i].tc_name) { + if (!tc[i].tc_name) { printf(">>>>> Wrong input value!\n"); return -1; } -- 2.7.4 From 6402d036ce0b37a062d9d6506c75081f7d22cdaa Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 30 May 2017 11:25:18 +0900 Subject: [PATCH 16/16] Add i2c_write_byte, i2c_read_byte API In order to provide for single byte communication, this patch adds peripheral_i2c_write_byte() and peripheral_i2c_read_byte() APIs. Change-Id: I0b1404fc1571f6d21ab7bfa5ceb9a76cb8a3e936 Signed-off-by: Hyeongsik Min --- include/peripheral_io.h | 32 ++++++++++++++++++++++++++++++-- src/peripheral_i2c.c | 18 ++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index c68ff98..7517d2a 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -332,7 +332,7 @@ int peripheral_i2c_close(peripheral_i2c_h i2c); * @since_tizen 4.0 * * @param[in] i2c The handle to the i2c device - * @param[in, out] data The address of read buffer + * @param[out] data The address of data buffer to read * @param[in] length The size of data buffer (in bytes) * * @return 0 on success, otherwise a negative error value @@ -348,7 +348,7 @@ int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); * @since_tizen 4.0 * * @param[in] i2c The handle to the i2c device - * @param[in, out] data The address of buffer to write + * @param[in] data The address of data buffer to write * @param[in] length The size of data buffer (in bytes) * * @return 0 on success, otherwise a negative error value @@ -359,6 +359,34 @@ int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); */ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); +/** + * @brief Reads single byte data from the i2c device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[out] data The address of data buffer to read + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ +int peripheral_i2c_read_byte(peripheral_i2c_h i2c, uint8_t *data); +/** + * @brief Write single byte data to the i2c device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[in] data The byte value to write + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + */ +int peripheral_i2c_write_byte(peripheral_i2c_h i2c, uint8_t data); /** * @} diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index fee3a32..969e18b 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -89,3 +89,21 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) return peripheral_gdbus_i2c_write(i2c, data, length); } + +int peripheral_i2c_read_byte(peripheral_i2c_h i2c, uint8_t *data) +{ + int ret = PERIPHERAL_ERROR_NONE; + + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + ret = peripheral_gdbus_i2c_read(i2c, data, 0x1); + + return ret; +} + +int peripheral_i2c_write_byte(peripheral_i2c_h i2c, uint8_t data) +{ + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_i2c_write(i2c, &data, 0x1); +} -- 2.7.4