From cad0071117298f730788051177f7a6374843cdc1 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Thu, 1 Jun 2017 15:59:17 +0900 Subject: [PATCH 01/16] Update package version to 0.0.4 Change-Id: Id21d404d52630822966c60442266c5d7337f77a8 Signed-off-by: Hyeongsik Min --- packaging/capi-system-peripheral-io.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/capi-system-peripheral-io.spec b/packaging/capi-system-peripheral-io.spec index 91a3634..595430d 100644 --- a/packaging/capi-system-peripheral-io.spec +++ b/packaging/capi-system-peripheral-io.spec @@ -1,6 +1,6 @@ Name: capi-system-peripheral-io Summary: Tizen Peripheral Input & Output library -Version: 0.0.3 +Version: 0.0.4 Release: 0 Group: System & System Tools License: Apache-2.0 -- 2.7.4 From 84abf6350d82076f1abd27e893223224dcfe3353 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 1 Jun 2017 16:18:28 +0900 Subject: [PATCH 02/16] Fix type mismatch issue The type size of bool and gboolean are different. Therefore, the pointer parameter should not be used directly as a argument of gdbus functions. Change-Id: I6ccccab228ca5fa57bee6db02e49bbe44a15eaef Signed-off-by: jino.cho --- src/peripheral_gdbus_gpio.c | 17 ++++++++++++++--- src/peripheral_gdbus_pwm.c | 26 ++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/peripheral_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c index 721b3d4..6550360 100644 --- a/src/peripheral_gdbus_gpio.c +++ b/src/peripheral_gdbus_gpio.c @@ -126,21 +126,26 @@ int peripheral_gdbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_ { GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + gint value = 0; if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; if (peripheral_io_gdbus_gpio_call_get_direction_sync( gpio_proxy, gpio->handle, - (gint*)direction, + &value, &ret, NULL, &error) == FALSE) { _E("Error in %s() : %s\n", __func__, error->message); g_error_free(error); - return PERIPHERAL_ERROR_UNKNOWN; } + if (value >= PERIPHERAL_GPIO_DIRECTION_IN && value <= PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) + *direction = value; + else + return PERIPHERAL_ERROR_UNKNOWN; + return ret; } @@ -214,13 +219,14 @@ int peripheral_gdbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ { GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + gint value = 0; if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; if (peripheral_io_gdbus_gpio_call_get_edge_mode_sync( gpio_proxy, gpio->handle, - (int*)edge, + &value, &ret, NULL, &error) == FALSE) { @@ -229,6 +235,11 @@ int peripheral_gdbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ return PERIPHERAL_ERROR_UNKNOWN; } + if (value >= PERIPHERAL_GPIO_EDGE_NONE && value <= PERIPHERAL_GPIO_EDGE_BOTH) + *edge = value; + else + return PERIPHERAL_ERROR_UNKNOWN; + return ret; } diff --git a/src/peripheral_gdbus_pwm.c b/src/peripheral_gdbus_pwm.c index a3b8b51..b32074e 100644 --- a/src/peripheral_gdbus_pwm.c +++ b/src/peripheral_gdbus_pwm.c @@ -122,13 +122,14 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period) { GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + gint value = 0; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; if (peripheral_io_gdbus_pwm_call_get_period_sync( pwm_proxy, pwm->handle, - (gint*)period, + &value, &ret, NULL, &error) == FALSE) { @@ -137,6 +138,8 @@ int peripheral_gdbus_pwm_get_period(peripheral_pwm_h pwm, int *period) return PERIPHERAL_ERROR_UNKNOWN; } + *period = value; + return ret; } @@ -166,13 +169,14 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) { GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + gint value = 0; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync( pwm_proxy, pwm->handle, - (gint*)duty_cycle, + &value, &ret, NULL, &error) == FALSE) { @@ -181,6 +185,8 @@ int peripheral_gdbus_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) return PERIPHERAL_ERROR_UNKNOWN; } + *duty_cycle = value; + return ret; } @@ -210,13 +216,14 @@ int peripheral_gdbus_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polar { GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + gint value = 0; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; if (peripheral_io_gdbus_pwm_call_get_polarity_sync( pwm_proxy, pwm->handle, - (gint*)polarity, + &value, &ret, NULL, &error) == FALSE) { @@ -225,6 +232,11 @@ int peripheral_gdbus_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polar return PERIPHERAL_ERROR_UNKNOWN; } + if (!value) + *polarity = PERIPHERAL_PWM_POLARITY_NORMAL; + else + *polarity = PERIPHERAL_PWM_POLARITY_INVERSED; + return ret; } @@ -254,13 +266,14 @@ int peripheral_gdbus_pwm_get_enable(peripheral_pwm_h pwm, bool *enable) { GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + gboolean value = 0; if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; if (peripheral_io_gdbus_pwm_call_get_enable_sync( pwm_proxy, pwm->handle, - (gint*)enable, + &value, &ret, NULL, &error) == FALSE) { @@ -269,6 +282,11 @@ int peripheral_gdbus_pwm_get_enable(peripheral_pwm_h pwm, bool *enable) return PERIPHERAL_ERROR_UNKNOWN; } + if (!value) + *enable = false; + else + *enable = true; + return ret; } -- 2.7.4 From 6e53073c2bed4bf3b8af764cb46b093fd5a55464 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Thu, 8 Jun 2017 16:38:07 +0900 Subject: [PATCH 03/16] Replace error code from ENODEV with ENXIO Change error code ENODEV to ENXIO for the case of no such device, because ENODEV is not defined in tizen_error.h and is also suitable for the case. Change-Id: I5e0f6d17bc9af720fd6d15862aaa2fd8da259ad1 Signed-off-by: Hyeongsik Min --- include/peripheral_io.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 5629ae3..baa48e1 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -36,6 +36,7 @@ extern "C" { typedef enum { PERIPHERAL_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ PERIPHERAL_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR, /**< I/O error */ + PERIPHERAL_ERROR_NO_DEVICE = TIZEN_ERROR_NO_SUCH_DEVICE, /**< No such device */ PERIPHERAL_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ PERIPHERAL_ERROR_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */ PERIPHERAL_ERROR_RESOURCE_BUSY = TIZEN_ERROR_RESOURCE_BUSY, /**< Device or resource busy */ @@ -45,7 +46,6 @@ typedef enum { PERIPHERAL_ERROR_TIMED_OUT = TIZEN_ERROR_TIMED_OUT, /**< Time out */ PERIPHERAL_ERROR_NOT_SUPPORTED = TIZEN_ERROR_NOT_SUPPORTED, /**< Not supported */ PERIPHERAL_ERROR_UNKNOWN = TIZEN_ERROR_UNKNOWN, /**< Unknown error */ - PERIPHERAL_ERROR_NO_DEVICE = -ENODEV, /**< No such device */ } peripheral_error_e; /** -- 2.7.4 From 1eaf4739166f64d583d4f18bdf6f9694c8972bbf Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 1 Jun 2017 10:32:26 +0900 Subject: [PATCH 04/16] Add API for the adc device This patch support the adc device. And, it should work properly if the patch for peripheral-bus is applied together. Change-Id: Iac0b0d3421634443ef2de95268b748ab098d5170 Signed-off-by: jino.cho --- CMakeLists.txt | 3 +- include/peripheral_gdbus.h | 1 + include/peripheral_gdbus_adc.h | 25 ++++++++++++++ include/peripheral_io.h | 33 ++++++++---------- src/peripheral_adc.c | 32 +++++------------- src/peripheral_gdbus_adc.c | 76 ++++++++++++++++++++++++++++++++++++++++++ src/peripheral_io.xml | 8 +++++ test/peripheral-io-test.c | 32 ++++++++++++++++++ 8 files changed, 167 insertions(+), 43 deletions(-) create mode 100644 include/peripheral_gdbus_adc.h create mode 100644 src/peripheral_gdbus_adc.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ebacf9e..725fed5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ SET(SOURCES src/peripheral_gpio.c src/peripheral_gdbus_gpio.c src/peripheral_gdbus_i2c.c src/peripheral_gdbus_pwm.c + src/peripheral_gdbus_adc.c src/peripheral_gdbus_uart.c src/peripheral_gdbus_spi.c src/peripheral_io_gdbus.c @@ -85,4 +86,4 @@ CONFIGURE_FILE( ) INSTALL(FILES ${PROJECT_NAME}.pc DESTINATION ${libdir}/pkgconfig) -ADD_SUBDIRECTORY(test) \ No newline at end of file +ADD_SUBDIRECTORY(test) diff --git a/include/peripheral_gdbus.h b/include/peripheral_gdbus.h index ff726de..0e4f15a 100644 --- a/include/peripheral_gdbus.h +++ b/include/peripheral_gdbus.h @@ -22,6 +22,7 @@ #define PERIPHERAL_GDBUS_GPIO_PATH "/Org/Tizen/Peripheral_io/Gpio" #define PERIPHERAL_GDBUS_I2C_PATH "/Org/Tizen/Peripheral_io/I2c" #define PERIPHERAL_GDBUS_PWM_PATH "/Org/Tizen/Peripheral_io/Pwm" +#define PERIPHERAL_GDBUS_ADC_PATH "/Org/Tizen/Peripheral_io/Adc" #define PERIPHERAL_GDBUS_UART_PATH "/Org/Tizen/Peripheral_io/Uart" #define PERIPHERAL_GDBUS_SPI_PATH "/Org/Tizen/Peripheral_io/Spi" #define PERIPHERAL_GDBUS_NAME "org.tizen.peripheral_io" diff --git a/include/peripheral_gdbus_adc.h b/include/peripheral_gdbus_adc.h new file mode 100644 index 0000000..2f90ecf --- /dev/null +++ b/include/peripheral_gdbus_adc.h @@ -0,0 +1,25 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PERIPHERAL_GDBUS_ADC_H__ +#define __PERIPHERAL_GDBUS_ADC_H__ + +void adc_proxy_init(void); +void adc_proxy_deinit(void); + +int peripheral_gdbus_adc_read(unsigned int device, unsigned int channel, int *data); + +#endif /* __PERIPHERAL_GDBUS_ADC_H__ */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index baa48e1..bba13ca 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -662,26 +662,21 @@ int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable); */ /** - * @brief Struct for peripheral_gpio_s - */ - -#define DEVICE_NAME_SIZE 20 - -struct _peripheral_adc_s { - char device_name[DEVICE_NAME_SIZE]; - int channel; -}; - -/** - * @brief Pointer definition to the internal struct peripheral_adc_s + * @brief Reads data from the adc device. + * @since_tizen 4.0 + * + * @param[in] device The device number of the adc device + * @param[in] channel The channel number to read + * @param[out] data The address of 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 + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed */ -typedef struct _peripheral_adc_s* peripheral_adc_context_h; - -peripheral_adc_context_h peripheral_adc_open(int channel); - -int peripheral_adc_read(peripheral_adc_context_h dev, int *data); - -int peripheral_adc_close(peripheral_adc_context_h dev); +int peripheral_adc_read(unsigned int device, unsigned int channel, int *data); /** * @} diff --git a/src/peripheral_adc.c b/src/peripheral_adc.c index 1aa2973..8df1d75 100644 --- a/src/peripheral_adc.c +++ b/src/peripheral_adc.c @@ -15,32 +15,18 @@ */ #include "peripheral_io.h" +#include "peripheral_gdbus_adc.h" +#include "peripheral_common.h" -#include -#include -#include - -peripheral_adc_context_h peripheral_adc_open(int channel) -{ - return NULL; -} - -int peripheral_adc_read(peripheral_adc_context_h dev, int *data) +int peripheral_adc_read(unsigned int device, unsigned int channel, int *data) { - if (dev == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; - if (data == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret = PERIPHERAL_ERROR_NONE; - return PERIPHERAL_ERROR_INVALID_OPERATION; -} - -int peripheral_adc_close(peripheral_adc_context_h dev) -{ - if (dev != NULL) - free(dev); + adc_proxy_init(); - dev = NULL; + ret = peripheral_gdbus_adc_read(device, channel, data); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to read, ret : %d", ret); - return PERIPHERAL_ERROR_INVALID_OPERATION; + return ret; } diff --git a/src/peripheral_gdbus_adc.c b/src/peripheral_gdbus_adc.c new file mode 100644 index 0000000..e9a2513 --- /dev/null +++ b/src/peripheral_gdbus_adc.c @@ -0,0 +1,76 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "peripheral_io.h" +#include "peripheral_gdbus.h" +#include "peripheral_common.h" +#include "peripheral_io_gdbus.h" + +PeripheralIoGdbusAdc *adc_proxy = NULL; + +void adc_proxy_init(void) +{ + GError *error = NULL; + + if (adc_proxy != NULL) + return; + + adc_proxy = peripheral_io_gdbus_adc_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_GDBUS_NAME, + PERIPHERAL_GDBUS_ADC_PATH, + NULL, + &error); +} + +void adc_proxy_deinit() +{ + if (adc_proxy) { + g_object_unref(adc_proxy); + if (!G_IS_OBJECT(adc_proxy)) + adc_proxy = NULL; + } +} + +int peripheral_gdbus_adc_read(unsigned int device, unsigned int channel, int *data) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + gint value; + + if (adc_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_adc_call_read_sync( + adc_proxy, + device, + channel, + &value, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + *data = value; + + return ret; +} diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index ea4b1f9..16b3cd2 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -142,6 +142,14 @@ + + + + + + + + diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 00bb5f7..23a63ae 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -908,8 +908,40 @@ int enter_pwm_test(void) return 0; } +int adc_read_channel(void) +{ + int device, channel, ret; + int value; + + printf("%s\n", __func__); + + printf("Enter adc device number\n"); + if (read_int_input(&device) < 0) + return -1; + + printf("Enter adc channel number\n"); + if (read_int_input(&channel) < 0) + return -1; + + if ((ret = peripheral_adc_read(device, channel, &value)) < 0) { + printf(">>>>> Failed to read adc value, ret : %d\n", ret); + return -1; + } + printf("ADC(%d,%d) Value = %d\n", device, channel, value); + + return 0; +} + +tc_table_t adc_tc_table[] = { + {"Read ADC Channel", 1, adc_read_channel}, + {"Go back to main", 0, enter_main}, + {NULL, 0, NULL}, +}; + int enter_adc_test(void) { + tc_table = adc_tc_table; + return 0; } -- 2.7.4 From f7bb29309bbfc320b6824c92435a7cc0760fe45e Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 13 Jun 2017 13:12:05 +0900 Subject: [PATCH 05/16] Remove unnecessary EOL in log message Change-Id: Ibd4a653e4ef1b77a13c0efbb866ade6249db04ff Signed-off-by: Hyeongsik Min --- src/peripheral_gdbus_adc.c | 2 +- src/peripheral_gdbus_gpio.c | 22 ++++++++++------------ src/peripheral_gdbus_i2c.c | 10 +++++----- src/peripheral_gdbus_uart.c | 16 ++++++++-------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/peripheral_gdbus_adc.c b/src/peripheral_gdbus_adc.c index e9a2513..f52c0b8 100644 --- a/src/peripheral_gdbus_adc.c +++ b/src/peripheral_gdbus_adc.c @@ -65,7 +65,7 @@ int peripheral_gdbus_adc_read(unsigned int device, unsigned int channel, int *da &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } diff --git a/src/peripheral_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c index 6550360..1c1fb1c 100644 --- a/src/peripheral_gdbus_gpio.c +++ b/src/peripheral_gdbus_gpio.c @@ -74,8 +74,6 @@ void handle_gpio_changed( if (!gpio) return; - _D("gpio=%d state=%d", pin, state); - peripheral_gpio_isr_callback(pin); } @@ -93,7 +91,7 @@ int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -114,7 +112,7 @@ int peripheral_gdbus_gpio_close(peripheral_gpio_h gpio) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -137,7 +135,7 @@ int peripheral_gdbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_ &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); } @@ -163,7 +161,7 @@ int peripheral_gdbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_ &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -185,7 +183,7 @@ int peripheral_gdbus_gpio_read(peripheral_gpio_h gpio, int *value) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -207,7 +205,7 @@ int peripheral_gdbus_gpio_write(peripheral_gpio_h gpio, int value) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -230,7 +228,7 @@ int peripheral_gdbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -257,7 +255,7 @@ int peripheral_gdbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_ &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -278,7 +276,7 @@ int peripheral_gdbus_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callba &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -299,7 +297,7 @@ int peripheral_gdbus_gpio_unregister_cb(peripheral_gpio_h gpio) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } diff --git a/src/peripheral_gdbus_i2c.c b/src/peripheral_gdbus_i2c.c index 875df8c..cc3de07 100644 --- a/src/peripheral_gdbus_i2c.c +++ b/src/peripheral_gdbus_i2c.c @@ -67,7 +67,7 @@ int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -88,7 +88,7 @@ int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -115,7 +115,7 @@ int peripheral_gdbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -158,7 +158,7 @@ int peripheral_gdbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -184,7 +184,7 @@ int peripheral_gdbus_i2c_smbus_ioctl(peripheral_i2c_h i2c, uint8_t read_write, u &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } diff --git a/src/peripheral_gdbus_uart.c b/src/peripheral_gdbus_uart.c index a727f1a..81b77c1 100644 --- a/src/peripheral_gdbus_uart.c +++ b/src/peripheral_gdbus_uart.c @@ -66,7 +66,7 @@ int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -87,7 +87,7 @@ int peripheral_gdbus_uart_close(peripheral_uart_h uart) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -108,7 +108,7 @@ int peripheral_gdbus_uart_flush(peripheral_uart_h uart) &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -130,7 +130,7 @@ int peripheral_gdbus_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_b &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -154,7 +154,7 @@ int peripheral_gdbus_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytes &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -177,7 +177,7 @@ int peripheral_gdbus_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -204,7 +204,7 @@ int peripheral_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } @@ -247,7 +247,7 @@ int peripheral_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int lengt &ret, NULL, &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); + _E("Error in %s() : %s", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } -- 2.7.4 From 3b5ad1c0fb64a43d21909b00d84e31eeaf79782d Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 13 Jun 2017 18:57:05 +0900 Subject: [PATCH 06/16] Add log messages to API function - Add log message for failure cases. - Improve parameter validation with log messages. Change-Id: I21f99c8fbbe1f50032da8933dbb92b6b21faad11 Signed-off-by: Hyeongsik Min --- src/peripheral_adc.c | 4 +- src/peripheral_gpio.c | 79 ++++++++++++++++---------------- src/peripheral_i2c.c | 57 ++++++++++++++++-------- src/peripheral_pwm.c | 90 ++++++++++++++++++++++++++++--------- src/peripheral_spi.c | 121 +++++++++++++++++++++++++++++++++++++++----------- src/peripheral_uart.c | 81 +++++++++++++++++++++++++-------- 6 files changed, 306 insertions(+), 126 deletions(-) diff --git a/src/peripheral_adc.c b/src/peripheral_adc.c index 8df1d75..5997ad1 100644 --- a/src/peripheral_adc.c +++ b/src/peripheral_adc.c @@ -20,12 +20,12 @@ int peripheral_adc_read(unsigned int device, unsigned int channel, int *data) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; adc_proxy_init(); ret = peripheral_gdbus_adc_read(device, channel, data); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Failed to read, ret : %d", ret); return ret; diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index e49477c..6c64b98 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -115,7 +115,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) int ret = PERIPHERAL_ERROR_NONE; peripheral_gpio_h handle; - assert(gpio_pin >= 0); + RETVM_IF(gpio_pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin number"); /* Initialize */ handle = (peripheral_gpio_h)calloc(1, sizeof(struct _peripheral_gpio_s)); @@ -131,6 +131,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) ret = peripheral_gdbus_gpio_open(handle); if (ret != PERIPHERAL_ERROR_NONE) { + _E("Failed to open the gpio pin, ret : %d", ret); free(handle); handle = NULL; } @@ -148,14 +149,12 @@ int peripheral_gpio_close(peripheral_gpio_h gpio) { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); /* call gpio_close */ ret = peripheral_gdbus_gpio_close(gpio); - if (ret) - ret = TIZEN_ERROR_IO_ERROR; + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to close the gpio pin, ret : %d", ret); gpio_proxy_deinit(); free(gpio); @@ -171,11 +170,11 @@ int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direct { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); ret = peripheral_gdbus_gpio_get_direction(gpio, direction); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get direction of the gpio pin, ret : %d", ret); return ret; } @@ -188,15 +187,14 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; - - if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); + RETVM_IF(direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid direction input"); /* call gpio_set_direction */ ret = peripheral_gdbus_gpio_set_direction(gpio, direction); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set gpio direction, ret : %d", ret); return ret; } @@ -208,12 +206,12 @@ int peripheral_gpio_read(peripheral_gpio_h gpio, int *value) { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); /* call gpio_read */ ret = peripheral_gdbus_gpio_read(gpio, value); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to read value of the gpio pin, ret : %d", ret); return ret; } @@ -225,12 +223,12 @@ int peripheral_gpio_write(peripheral_gpio_h gpio, int value) { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); /* call gpio_write */ ret = peripheral_gdbus_gpio_write(gpio, value); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to write to the gpio pin, ret : %d", ret); return ret; } @@ -242,11 +240,11 @@ int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); ret = peripheral_gdbus_gpio_get_edge_mode(gpio, edge); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get edge mode of the gpio pin, ret : %d", ret); return ret; } @@ -258,15 +256,14 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; - - if (edge > PERIPHERAL_GPIO_EDGE_BOTH) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); + RETVM_IF(edge > PERIPHERAL_GPIO_EDGE_BOTH, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid edge input"); /* call gpio_set_edge_mode */ ret = peripheral_gdbus_gpio_set_edge_mode(gpio, edge); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set edge mode of the gpio pin, ret : %d", ret); return ret; } @@ -278,16 +275,18 @@ int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, vo { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); ret = peripheral_gdbus_gpio_register_cb(gpio, callback, user_data); - if (ret != PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) { + _E("Failed to register cb, ret : %d", ret); return ret; + } /* set isr */ ret = peripheral_gpio_isr_set(gpio->pin, callback, user_data); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to register gpio isr, ret : %d", ret); return ret; } @@ -299,13 +298,13 @@ int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio) { int ret = PERIPHERAL_ERROR_NONE; - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); ret = peripheral_gdbus_gpio_unregister_cb(gpio); - if (ret != PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) { + _E("Failed to unregister gpio isr, ret : %d", ret); return ret; + } /* clean up isr */ ret = peripheral_gpio_isr_unset(gpio->pin); @@ -318,9 +317,7 @@ int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio) */ int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin) { - /* check validation of gpio context handle */ - if (gpio == NULL) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "gpio handle is NULL"); *gpio_pin = gpio->pin; diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 0af6dc8..4035eb6 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -39,8 +39,7 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) peripheral_i2c_h handle; int ret = PERIPHERAL_ERROR_NONE; - if (bus < 0) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(bus < 0 || address < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); @@ -54,7 +53,7 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) ret = peripheral_gdbus_i2c_open(handle, bus, address); if (ret != PERIPHERAL_ERROR_NONE) { - _E("[PERIPHERAL] I2C init error\n"); + _E("Failed to open i2c communication, ret : %d", ret); free(handle); handle = NULL; } @@ -65,11 +64,13 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) int peripheral_i2c_close(peripheral_i2c_h i2c) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); ret = peripheral_gdbus_i2c_close(i2c); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to close i2c communcation, ret : %d", ret); i2c_proxy_deinit(); free(i2c); @@ -80,31 +81,41 @@ int peripheral_i2c_close(peripheral_i2c_h i2c) int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + RETVM_IF(data == NULL || length < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); ret = peripheral_gdbus_i2c_read(i2c, data, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to read data from device, ret : %d", ret); return ret; } int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) { - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + RETVM_IF(data == NULL || length < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_gdbus_i2c_write(i2c, data, length); + ret = peripheral_gdbus_i2c_write(i2c, data, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to write data to device, ret : %d", ret); + + return ret; } int peripheral_i2c_read_byte(peripheral_i2c_h i2c, uint8_t *data) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; uint16_t w_data, dummy = 0; - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_READ, 0x0, I2C_SMBUS_BYTE, dummy, &w_data); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Failed to read, ret : %d", ret); *data = (uint8_t)w_data; @@ -117,11 +128,11 @@ int peripheral_i2c_write_byte(peripheral_i2c_h i2c, uint8_t data) int ret = PERIPHERAL_ERROR_NONE; uint16_t dummy = 0; - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_WRITE, dummy, I2C_SMBUS_BYTE, (uint16_t)data, &dummy); - if (ret < PERIPHERAL_ERROR_NONE) - _E("Failed to read, ret : %d", ret); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to write, ret : %d", ret); return ret; } @@ -131,8 +142,10 @@ int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t address, uin int ret; uint16_t w_data, dummy = 0; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_READ, address, I2C_SMBUS_BYTE_DATA, dummy, &w_data); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Smbus transaction failed, ret : %d", ret); *data = (uint8_t)w_data; @@ -145,8 +158,10 @@ int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t address, ui int ret; uint16_t dummy = 0; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_WRITE, address, I2C_SMBUS_BYTE_DATA, (uint16_t)data, &dummy); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Smbus transaction failed, ret : %d", ret); return ret; @@ -157,8 +172,10 @@ int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t address, uin int ret; uint16_t dummy = 0, data_out; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_READ, address, I2C_SMBUS_WORD_DATA, dummy, &data_out); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Smbus transaction failed, ret : %d", ret); *data = data_out; @@ -171,8 +188,10 @@ int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t address, ui int ret; uint16_t dummy = 0; + RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); + ret = peripheral_gdbus_i2c_smbus_ioctl(i2c, I2C_SMBUS_WRITE, address, I2C_SMBUS_WORD_DATA, data, &dummy); - if (ret < PERIPHERAL_ERROR_NONE) + if (ret != PERIPHERAL_ERROR_NONE) _E("Smbus transaction failed, ret : %d", ret); return ret; diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 7e98766..e3718ee 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -29,7 +29,7 @@ int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm) peripheral_pwm_h handle; int ret = PERIPHERAL_ERROR_NONE; - if (device < 0 || channel < 0) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(device < 0 || channel < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); /* Initialize */ handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s)); @@ -44,7 +44,7 @@ int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm) ret = peripheral_gdbus_pwm_open(handle, device, channel); if (ret != PERIPHERAL_ERROR_NONE) { - _E("PWM open error (%d, %d)", device, channel); + _E("Failed to open PWM device : %d, channel : %d", device, channel); free(handle); handle = NULL; } @@ -57,10 +57,10 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) { int ret = PERIPHERAL_ERROR_NONE; - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0) - _E("Failed to close PWM device, continuing anyway"); + _E("Failed to close PWM device, continuing anyway, ret : %d", ret); pwm_proxy_deinit(); free(pwm); @@ -70,56 +70,106 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) int peripheral_pwm_set_period(peripheral_pwm_h pwm, int period) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_set_period(pwm, period); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_set_period(pwm, period); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set period, ret : %d", ret); + + return ret; } int peripheral_pwm_get_period(peripheral_pwm_h pwm, int *period) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_get_period(pwm, period); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_get_period(pwm, period); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get period, ret : %d", ret); + + return ret; } int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, duty_cycle); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set duty cycle, ret : %d", ret); + + return ret; } int peripheral_pwm_get_duty_cycle(peripheral_pwm_h pwm, int *duty_cycle) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_get_duty_cycle(pwm, duty_cycle); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get duty cycle, ret : %d", ret); + + return ret; } int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_set_polarity(pwm, polarity); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + RETVM_IF(polarity > PERIPHERAL_PWM_POLARITY_INVERSED, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid polarity parameter"); + + ret = peripheral_gdbus_pwm_set_polarity(pwm, polarity); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set polarity, ret : %d", ret); + + return ret; } int peripheral_pwm_get_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e *polarity) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_get_polarity(pwm, polarity); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_get_polarity(pwm, polarity); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get polarity, ret : %d", ret); + + return ret; } int peripheral_pwm_set_enable(peripheral_pwm_h pwm, bool enable) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_set_enable(pwm, enable); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_set_enable(pwm, enable); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set enable, ret : %d", ret); + + return ret; } int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable) { - if (pwm == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_pwm_get_enable(pwm, enable); + RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL"); + + ret = peripheral_gdbus_pwm_get_enable(pwm, enable); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get enable, ret : %d", ret); + + return ret; } diff --git a/src/peripheral_spi.c b/src/peripheral_spi.c index 661b68a..eff8f23 100644 --- a/src/peripheral_spi.c +++ b/src/peripheral_spi.c @@ -30,7 +30,7 @@ int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi) peripheral_spi_h handle; int ret = PERIPHERAL_ERROR_NONE; - if (bus < 0 || cs < 0) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(bus < 0 || cs < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); /* Initialize */ handle = (peripheral_spi_h)calloc(1, sizeof(struct _peripheral_spi_s)); @@ -58,10 +58,11 @@ int peripheral_spi_close(peripheral_spi_h spi) { int ret = PERIPHERAL_ERROR_NONE; - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - if ((ret = peripheral_gdbus_spi_close(spi)) < 0) - _E("Failed to close SPI device, continuing anyway"); + ret = peripheral_gdbus_spi_close(spi); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to close SPI device, continuing anyway, ret : %d", ret); spi_proxy_deinit(); free(spi); @@ -71,77 +72,145 @@ int peripheral_spi_close(peripheral_spi_h spi) int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_spi_set_mode(spi, mode); + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + RETVM_IF(mode > PERIPHERAL_SPI_MODE_3, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid spi mode parameter"); + + ret = peripheral_gdbus_spi_set_mode(spi, mode); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set mode, ret : %d", ret); + + return ret; } int peripheral_spi_get_mode(peripheral_spi_h spi, peripheral_spi_mode_e *mode) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_get_mode(spi, mode); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get spi mode, ret : %d", ret); - return peripheral_gdbus_spi_get_mode(spi, mode); + return ret; } int peripheral_spi_set_lsb_first(peripheral_spi_h spi, bool lsb) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_set_lsb_first(spi, lsb); + ret = peripheral_gdbus_spi_set_lsb_first(spi, lsb); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set lsb first, ret : %d", ret); + + return ret; } int peripheral_spi_get_lsb_first(peripheral_spi_h spi, bool *lsb) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_spi_get_lsb_first(spi, lsb); + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_get_lsb_first(spi, lsb); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get lsb first, ret : %d", ret); + + return ret; } int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, unsigned char bits) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_set_bits(spi, bits); + ret = peripheral_gdbus_spi_set_bits(spi, bits); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set bits per word, ret : %d", ret); + + return ret; } int peripheral_spi_get_bits_per_word(peripheral_spi_h spi, unsigned char *bits) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_get_bits(spi, bits); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get bits per word, ret : %d", ret); - return peripheral_gdbus_spi_get_bits(spi, bits); + return ret; } int peripheral_spi_set_frequency(peripheral_spi_h spi, unsigned int freq) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_set_frequency(spi, freq); + ret = peripheral_gdbus_spi_set_frequency(spi, freq); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set frequency, ret : %d", ret); + + return ret; } int peripheral_spi_get_frequency(peripheral_spi_h spi, unsigned int *freq) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_spi_get_frequency(spi, freq); + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_get_frequency(spi, freq); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to get spi frequency, ret : %d", ret); + + return ret; } int peripheral_spi_read(peripheral_spi_h spi, unsigned char *data, int length) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_read(spi, data, length); + ret = peripheral_gdbus_spi_read(spi, data, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to read from spi device, ret : %d", ret); + + return ret; } int peripheral_spi_write(peripheral_spi_h spi, unsigned char *data, int length) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); + + ret = peripheral_gdbus_spi_write(spi, data, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to write to spi device, ret : %d", ret); - return peripheral_gdbus_spi_write(spi, data, length); + return ret; } int peripheral_spi_read_write(peripheral_spi_h spi, unsigned char *txdata, unsigned char *rxdata, int length) { - if (spi == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_gdbus_spi_read_write(spi, txdata, rxdata, length); + ret = peripheral_gdbus_spi_read_write(spi, txdata, rxdata, length); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to read and write, ret : %d", ret); + + return ret; } diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c index 66d96f0..1b18c17 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -30,10 +30,9 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) { peripheral_uart_h handle; - int ret = PERIPHERAL_ERROR_NONE; + int ret; - if (port < 0) - return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(port < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid port number"); handle = (peripheral_uart_h)calloc(1, sizeof(struct _peripheral_uart_s)); @@ -47,7 +46,7 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) ret = peripheral_gdbus_uart_open(handle, port); if (ret != PERIPHERAL_ERROR_NONE) { - _E("[PERIPHERAL] UART open error\n"); + _E("Failed to open uart port, ret : %d", ret); free(handle); handle = NULL; } @@ -61,11 +60,13 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) */ int peripheral_uart_close(peripheral_uart_h uart) { - int ret = PERIPHERAL_ERROR_NONE; + int ret; - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); ret = peripheral_gdbus_uart_close(uart); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to close uart communication, continuing anyway, ret : %d", ret); uart_proxy_deinit(); free(uart); @@ -80,9 +81,15 @@ int peripheral_uart_close(peripheral_uart_h uart) */ int peripheral_uart_flush(peripheral_uart_h uart) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_uart_flush(uart); + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + + ret = peripheral_gdbus_uart_flush(uart); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to flush, ret : %d", ret); + + return ret; } /** @@ -90,9 +97,17 @@ int peripheral_uart_flush(peripheral_uart_h uart) */ int peripheral_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + RETVM_IF(baud > PERIPHERAL_UART_BAUDRATE_230400, PERIPHERAL_ERROR_INVALID_PARAMETER, + "Invalid baud input"); + + ret = peripheral_gdbus_uart_set_baudrate(uart, baud); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set baudrate, ret : %d", ret); - return peripheral_gdbus_uart_set_baudrate(uart, baud); + return ret; } /** @@ -100,9 +115,19 @@ int peripheral_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrat */ int peripheral_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + RETVM_IF(bytesize > PERIPHERAL_UART_BYTESIZE_8BIT + || parity > PERIPHERAL_UART_PARITY_ODD + || stopbits > PERIPHERAL_UART_STOPBITS_2BIT, + PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_gdbus_uart_set_mode(uart, bytesize, parity, stopbits); + ret = peripheral_gdbus_uart_set_mode(uart, bytesize, parity, stopbits); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set uart mode, ret : %d", ret); + + return ret; } /** @@ -110,9 +135,15 @@ int peripheral_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e */ int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; - return peripheral_gdbus_uart_set_flowcontrol(uart, xonxoff, rtscts); + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + + ret = peripheral_gdbus_uart_set_flowcontrol(uart, xonxoff, rtscts); + if (ret != PERIPHERAL_ERROR_NONE) + _E("Failed to set flocontrol, ret : %d", ret); + + return ret; } /** @@ -120,9 +151,16 @@ int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool r */ int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, int length) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + RETVM_IF(data == NULL || length < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + + ret = peripheral_gdbus_uart_read(uart, data, length); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to read from uart device, ret : %d", ret); - return peripheral_gdbus_uart_read(uart, data, length); + return ret; } /** @@ -130,7 +168,14 @@ int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, int length) */ int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, int length) { - if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + int ret; + + RETVM_IF(uart == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "uart handle is NULL"); + RETVM_IF(data == NULL || length < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_gdbus_uart_write(uart, data, length); + ret = peripheral_gdbus_uart_write(uart, data, length); + if (ret < PERIPHERAL_ERROR_NONE) + _E("Failed to write to uart device, ret : %d", ret); + + return ret; } -- 2.7.4 From 6f7c431546416acd5d03b0ef34b3390db10d77dc Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 14 Jun 2017 18:47:10 +0900 Subject: [PATCH 07/16] Improve preset test(I2C, PWM) - Get user input for PWM device and channel number. - Read 2 bytes from GY30 light sensor. Change-Id: I2225119d7ae21e8fd7ebe9bd794c04afe0f40dd1 Signed-off-by: Hyeongsik Min --- test/peripheral-io-test.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 23a63ae..4ccbbe1 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -207,7 +207,7 @@ int i2c_gy30_test(void) gettimeofday(&tv_1, NULL); while (cnt++ < 1000) { - ret = peripheral_i2c_read_byte(i2c, buf); + ret = peripheral_i2c_read(i2c, buf, 2); if (ret < 0) printf("Failed to read, ret : %d\n", ret); result = GY30_READ_INTENSITY(buf); @@ -378,7 +378,7 @@ error: int pwm_test_led(void) { - int device = 0, channel = 0; + int device, channel, ret; int period = 1 * 1000; int duty_cycle = 1 * 1000 / 100; int cnt = 0; @@ -388,13 +388,24 @@ int pwm_test_led(void) peripheral_pwm_h dev; printf(" %s()\n", __func__); + printf("Enter PWM device number\n"); + if (read_int_input(&device) < 0) + return -1; - peripheral_pwm_open(device, channel, &dev); + printf("Enter PWM channel number\n"); + if (read_int_input(&channel) < 0) + return -1; + + ret = peripheral_pwm_open(device, channel, &dev); + if (ret != PERIPHERAL_ERROR_NONE) { + printf("Failed to open\n"); + return ret; + } peripheral_pwm_set_period(dev, period); /* period: nanosecond */ peripheral_pwm_set_duty_cycle(dev, duty_cycle); /* duty_cycle: nanosecond */ peripheral_pwm_set_enable(dev, 1); /* 0: disable, 1: enable */ - while (cnt < 5) { + while (cnt < 2) { for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) { /* set duty cycle */ peripheral_pwm_set_duty_cycle(dev, set_duty_cycle); @@ -421,7 +432,7 @@ int pwm_test_led(void) int pwm_test_motor(void) { - int device = 0, channel = 0; + int device, channel, ret; int period = 20000000; int duty_cycle = 1500000; int cnt = 0, idx = 0; @@ -429,8 +440,19 @@ int pwm_test_motor(void) peripheral_pwm_h dev; printf(" %s()\n", __func__); + printf("Enter PWM device number\n"); + if (read_int_input(&device) < 0) + return -1; - peripheral_pwm_open(device, channel, &dev); + printf("Enter PWM channel number\n"); + if (read_int_input(&channel) < 0) + return -1; + + ret = peripheral_pwm_open(device, channel, &dev); + if (ret != PERIPHERAL_ERROR_NONE) { + printf("Failed to open\n"); + return ret; + } for (cnt = 0; cnt < 5; cnt++) { for (idx = 0; idx < 3; idx++) { switch (degree[idx]) { -- 2.7.4 From 021a2a44080fba0ae9b14750b1b7a454d30e0c16 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 11 Jul 2017 20:13:33 +0900 Subject: [PATCH 08/16] Add a struct for gpio interrupt information This patch adds gpio isr callback data deliverd via gpio_isr_cb(). A gpio isr callback data is delivered as a structure, which contains the pin number, the pin value, and the timestamp(ms) when the gpio interrupt occurred. Change-Id: Iee60dc4d33fe856d46fda83c487ed27d8d1fadae Signed-off-by: jino.cho --- include/peripheral_io.h | 16 +++++++++++++++- src/peripheral_gdbus_gpio.c | 15 +++++++++++---- src/peripheral_gpio.c | 8 ++++++-- src/peripheral_io.xml | 3 ++- test/peripheral-io-test.c | 4 ++-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index bba13ca..d697a55 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -74,6 +74,19 @@ typedef enum { } peripheral_gpio_edge_e; /** + * @brief Gpio isr callback data delivered via gpio_isr_cb(). + * @details A gpio isr callback data is delivered as a structure, which contains + * the pin number, the pin value, and the timestamp of the gpio interrupt + * in microseconds. + * @since_tizen 4.0 + */ +typedef struct { + int pin; + int value; + unsigned long long timestamp; +} gpio_isr_cb_s; + +/** * @brief The handle to the gpio pin * @since_tizen 4.0 */ @@ -225,12 +238,13 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e * @brief Called when the gpio interrupt is triggered. * @since_tizen 4.0 * + * @param[in] data The gpio isr callback data * @param[in] user_data The user data passed from the callback registration function * * @see peripheral_gpio_register_cb() * @see peripheral_gpio_unregister_cb() */ -typedef void(*gpio_isr_cb)(void *user_data); +typedef void(*gpio_isr_cb)(gpio_isr_cb_s *data, void *user_data); /** * @brief Registers a callback function to be invoked when the gpio interrupt is triggered. diff --git a/src/peripheral_gdbus_gpio.c b/src/peripheral_gdbus_gpio.c index 1c1fb1c..b476e47 100644 --- a/src/peripheral_gdbus_gpio.c +++ b/src/peripheral_gdbus_gpio.c @@ -23,8 +23,14 @@ #include "peripheral_internal.h" #include "peripheral_io_gdbus.h" -extern int peripheral_gpio_isr_callback(int pin); -void handle_gpio_changed(PeripheralIoGdbusGpio *gpio, gint pin, gint state, gpointer user_data); +extern int peripheral_gpio_isr_callback(int pin, int value, unsigned long long timestamp); + +void handle_gpio_changed( + PeripheralIoGdbusGpio *gpio, + gint pin, + gint value, + guint64 timestamp, + gpointer user_data); PeripheralIoGdbusGpio *gpio_proxy = NULL; @@ -68,13 +74,14 @@ void gpio_proxy_deinit() void handle_gpio_changed( PeripheralIoGdbusGpio *gpio, gint pin, - gint state, + gint value, + guint64 timestamp, gpointer user_data) { if (!gpio) return; - peripheral_gpio_isr_callback(pin); + peripheral_gpio_isr_callback(pin, value, timestamp); } int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio) diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 6c64b98..8a7ccdf 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -33,18 +33,22 @@ typedef struct { static GList *gpio_isr_list = NULL; -int peripheral_gpio_isr_callback(int pin) +int peripheral_gpio_isr_callback(int pin, int value, unsigned long long timestamp) { GList *link; gpio_isr_data_s *isr_data; + gpio_isr_cb_s cb_data; link = gpio_isr_list; while (link) { isr_data = (gpio_isr_data_s*)link->data; if (isr_data->pin == pin) { + cb_data.pin = pin; + cb_data.value = value; + cb_data.timestamp = timestamp; if (isr_data->callback) - isr_data->callback(isr_data->user_data); + isr_data->callback(&cb_data, isr_data->user_data); return PERIPHERAL_ERROR_NONE; } link = g_list_next(link); diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index 16b3cd2..878fb5c 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -50,7 +50,8 @@ - + + diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 4ccbbe1..cb62e2a 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -103,7 +103,7 @@ error: return ret; } -void gpio_irq_test_isr(void *user_data) +void gpio_irq_test_isr(gpio_isr_cb_s *data, void *user_data) { int pin; peripheral_gpio_h gpio = user_data; @@ -249,7 +249,7 @@ error: #define MMA7455_YOUT8 0x07 //Register for reading the Y-Axis #define MMA7455_ZOUT8 0x08 //Register for reading the Z-Axis -static void i2c_mma7455_isr(void *user_data) +static void i2c_mma7455_isr(gpio_isr_cb_s *data, void *user_data) { peripheral_i2c_h i2c = user_data; uint8_t x_pos, y_pos, z_pos; -- 2.7.4 From 5603d222c09324a50dddf5b8785962dd955de02f Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 13 Jul 2017 11:03:18 +0900 Subject: [PATCH 09/16] Add test code for HC-SR04 ultrasonic raging module Change-Id: I132f2b25ed977181dbffd06f9570e0db4510cb81 Signed-off-by: jino.cho --- test/peripheral-io-test.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 2 deletions(-) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index cb62e2a..3fd3d20 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -26,6 +26,12 @@ #define BUFFER_LEN 32 typedef struct { + int cnt; + peripheral_gpio_h gpio_trig; + peripheral_gpio_h gpio_echo; +} gpio_hcsr04_module_s; + +typedef struct { const char *tc_name; int tc_code; int (*tc_func)(void); @@ -620,6 +626,94 @@ error: return -1; } +void gpio_hcsr04_isr(gpio_isr_cb_s *data, void *user_data) +{ + float dist = 0; + static unsigned long long timestamp = 0; + + if (timestamp > 0 && data->value == 0) { + dist = data->timestamp - timestamp; + dist = (dist * 34300) / 2000000; + printf("%s: Measured Distance : %0.2fcm\n", __func__, dist); + } + + timestamp = data->timestamp; +} + +gboolean gpio_hcsr04_timeout_cb(gpointer data) +{ + gpio_hcsr04_module_s *dev = (gpio_hcsr04_module_s*)data; + + if (dev->cnt--) { + peripheral_gpio_write(dev->gpio_trig, 1); + peripheral_gpio_write(dev->gpio_trig, 0); + } else + return FALSE; + + return TRUE; +} + +void gpio_hcsr04_destroy(gpointer data) +{ + gpio_hcsr04_module_s *dev = (gpio_hcsr04_module_s*)data; + + peripheral_gpio_unregister_cb(dev->gpio_echo); + peripheral_gpio_close(dev->gpio_echo); + peripheral_gpio_close(dev->gpio_trig); + free(dev); +} + +int gpio_hcsr04_ultrasonic_ranging_module(void) +{ + gpio_hcsr04_module_s *dev; + int pin_trig, pin_echo, ret; + + printf(" %s()\n", __func__); + printf("Enter triger gpio pin number\n"); + if (read_int_input(&pin_trig) < 0) + return -1; + + printf("Enter echo gpio pin number\n"); + if (read_int_input(&pin_echo) < 0) + return -1; + + dev = calloc(1, sizeof(gpio_hcsr04_module_s)); + if (dev == NULL) { + printf("failed to allocate gpio_hcsr04_module_s\n"); + return -1; + } + + if ((ret = peripheral_gpio_open(pin_trig, &dev->gpio_trig)) < 0) { + printf(">>>>> Failed to open GPIO pin, ret : %d\n", ret); + goto err; + } + + if ((ret = peripheral_gpio_open(pin_echo, &dev->gpio_echo)) < 0) { + printf(">>>>> Failed to open GPIO pin, ret : %d\n", ret); + peripheral_gpio_close(dev->gpio_trig); + goto err; + } + + peripheral_gpio_set_direction(dev->gpio_echo, PERIPHERAL_GPIO_DIRECTION_IN); + peripheral_gpio_set_edge_mode(dev->gpio_echo, PERIPHERAL_GPIO_EDGE_BOTH); + peripheral_gpio_register_cb(dev->gpio_echo, gpio_hcsr04_isr, NULL); + peripheral_gpio_set_direction(dev->gpio_trig, PERIPHERAL_GPIO_DIRECTION_OUT); + + dev->cnt = 20; + + g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, + 1, + gpio_hcsr04_timeout_cb, + dev, + gpio_hcsr04_destroy); + + return 0; + +err: + free(dev); + return -1; +} + int gpio_test_get_handle_by_pin(int pin, peripheral_gpio_h *gpio) { peripheral_gpio_h handle; @@ -985,8 +1079,9 @@ tc_table_t preset_tc_table[] = { {"[Preset Test] PWM Motor", 5, pwm_test_motor}, {"[Preset Test] Uart Accelerometer", 6, uart_test_accelerometer}, {"[Preset Test] SPI MMA7455 Accel. sensor", 7, spi_mma7455_module_test}, - {"[Preset Test] GPIO IRQ register", 8, gpio_irq_register}, - {"[Preset Test] GPIO IRQ unregister", 9, gpio_irq_unregister}, + {"[Preset Test] GPIO HC-SR04 Range sensor", 8, gpio_hcsr04_ultrasonic_ranging_module}, + {"[Preset Test] GPIO IRQ register", 10, gpio_irq_register}, + {"[Preset Test] GPIO IRQ unregister", 11, gpio_irq_unregister}, {"Go back to main", 0, enter_main}, {NULL, 0, NULL}, }; -- 2.7.4 From 1d2af60b069f1f2d5695df81c6f4c220aa041f3d Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 13 Jul 2017 13:26:04 +0900 Subject: [PATCH 10/16] Update package version to 0.0.5 Change-Id: Ic3bf9cde4c19f30ede23d2f713ed349b4f865c3b Signed-off-by: jino.cho --- packaging/capi-system-peripheral-io.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/capi-system-peripheral-io.spec b/packaging/capi-system-peripheral-io.spec index 595430d..cc7b1de 100644 --- a/packaging/capi-system-peripheral-io.spec +++ b/packaging/capi-system-peripheral-io.spec @@ -1,6 +1,6 @@ Name: capi-system-peripheral-io Summary: Tizen Peripheral Input & Output library -Version: 0.0.4 +Version: 0.0.5 Release: 0 Group: System & System Tools License: Apache-2.0 -- 2.7.4 From f0d8372bf037d8932e26497d761d8d37d49cfe51 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 17 Jul 2017 21:48:04 +0900 Subject: [PATCH 11/16] Add MMA7455 sample dirver codes This moves MMA7455 driver codes to sample directory after refactoring it. Change-Id: Idfa9b617f5081f7ea68a236b913ec1b97ff02a7a Signed-off-by: Hyeongsik Min --- test/CMakeLists.txt | 12 +- test/peripheral-io-test.c | 196 +++++------------------ test/samples/include/mma7455.h | 60 +++++++ test/samples/mma7455.c | 351 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 463 insertions(+), 156 deletions(-) create mode 100644 test/samples/include/mma7455.h create mode 100644 test/samples/mma7455.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6f01d42..d6f4dee 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,13 +8,21 @@ FOREACH(flag ${${fw_test}_CFLAGS}) MESSAGE(${flag}) ENDFOREACH() +SET(SAMPLE_INC_DIR ./samples/include) +INCLUDE_DIRECTORIES(${SAMPLE_INC_DIR}) + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall") -aux_source_directory(. sources) +AUX_SOURCE_DIRECTORY(samples sample_sources) +FOREACH(src ${sample_sources}) + SET(sample_src ${src}) +ENDFOREACH() + +AUX_SOURCE_DIRECTORY(. sources) FOREACH(src ${sources}) GET_FILENAME_COMPONENT(src_name ${src} NAME_WE) MESSAGE("${src_name}") - ADD_EXECUTABLE(${src_name} ${src}) + ADD_EXECUTABLE(${src_name} ${src} ${sample_src}) TARGET_LINK_LIBRARIES(${src_name} ${fw_name} ${${fw_test}_LDFLAGS}) ENDFOREACH() diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 3fd3d20..167a7a9 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -23,6 +23,8 @@ #include "peripheral_io.h" +#include "mma7455.h" + #define BUFFER_LEN 32 typedef struct { @@ -231,68 +233,35 @@ error: return -1; } -#define MMA7455_ADDRESS 0x1D //I2C Address for the sensor - -#define MMA7455_MCTL 0x16 // Mode Control - -#define MMA7455_MCTL_STANDBY_MODE 0x0 -#define MMA7455_MCTL_MEASUREMENT_MODE 0x01 -#define MMA7455_MCTL_LEVEL_DETECTION_MODE 0x02 -#define MMA7455_MCTL_PULSE_DETECTION_MODE 0x03 -#define MMA7455_MCTL_2G 0x04 //Set Sensitivity to 2g -#define MMA7455_MCTL_4G 0x08 //Set Sensitivity to 4g -#define MMA7455_MCTL_8G 0x00 //Set Sensitivity to 8g - -#define MMA7455_INTRST 0x17 -#define MMA7455_INTRST_CLRINT 0x03 -#define MMA7445_INTRST_DONOTCLR 0x00 - -#define MMA7455_CONTROL1 0x18 -#define MMA7455_CONTROL1_INTREG 0x06 -#define MMA7455_CONTROL1_DFBW 0x80 - -#define MMA7455_XOUT8 0x06 //Register for reading the X-Axis -#define MMA7455_YOUT8 0x07 //Register for reading the Y-Axis -#define MMA7455_ZOUT8 0x08 //Register for reading the Z-Axis - static void i2c_mma7455_isr(gpio_isr_cb_s *data, void *user_data) { - peripheral_i2c_h i2c = user_data; - uint8_t x_pos, y_pos, z_pos; - - peripheral_i2c_read_register_byte(i2c, MMA7455_XOUT8, &x_pos); - peripheral_i2c_read_register_byte(i2c, MMA7455_YOUT8, &y_pos); - peripheral_i2c_read_register_byte(i2c, MMA7455_ZOUT8, &z_pos); + mma7455_axes result; - printf("Result X : %d, Y : %d, Z : %d\n", x_pos, y_pos, z_pos); + mma7455_i2c_get_measurement_3(&result); + printf("Result X : %d, Y : %d, Z : %d\n", result.x_pos, result.y_pos, result.z_pos); /* Reset interrupt flags */ - peripheral_i2c_write_register_byte(i2c, MMA7455_INTRST, MMA7455_INTRST_CLRINT); - peripheral_i2c_write_register_byte(i2c, MMA7455_INTRST, MMA7445_INTRST_DONOTCLR); + mma7455_i2c_reset_isr(); return; } int i2c_mma7455_test(void) { - static peripheral_i2c_h i2c; - static peripheral_gpio_h isr_gpio; - int bus_num, gpio_num, ret; + static int gpio_num; + static bool enable; + int bus_num; int cnt = 0; printf(" %s()\n", __func__); - if (i2c) { + if (enable) { printf("Disabling the test\n"); - peripheral_i2c_close(i2c); - i2c = NULL; - printf("i2c handle is closed\n"); + mma7455_i2c_unregister_isr(); + mma7455_i2c_close(); + printf("mma7455 is closed\n"); - if (isr_gpio) { - peripheral_gpio_close(isr_gpio); - isr_gpio = NULL; - printf("isr_gpio handle is closed\n"); - } + enable = FALSE; return 0; } @@ -302,84 +271,47 @@ int i2c_mma7455_test(void) if (read_int_input(&bus_num) < 0) return -1; - if ((ret = peripheral_i2c_open(bus_num, MMA7455_ADDRESS, &i2c)) < 0) { - printf(">>>>> Failed to open I2C communication, ret : %d \n", ret); + if (mma7455_i2c_init(bus_num) < 0) return -1; - } - - ret = peripheral_i2c_write_register_byte(i2c, - MMA7455_MCTL, - MMA7455_MCTL_8G | MMA7455_MCTL_PULSE_DETECTION_MODE); - if (ret < PERIPHERAL_ERROR_NONE) { - printf(">>>>> Failed to write, ret : %d\n", ret); - goto error; - } printf("Enter GPIO pin number for Interrupt\n"); if (read_int_input(&gpio_num) < 0) gpio_num = -1; - if ((gpio_num > 0) && (peripheral_gpio_open(gpio_num, &isr_gpio) == 0)) { - ret = peripheral_gpio_set_direction(isr_gpio, PERIPHERAL_GPIO_DIRECTION_IN); - if (ret < 0) - printf(">>>> Failed to set direction of isr_gpio\n"); - - ret = peripheral_gpio_set_edge_mode(isr_gpio, PERIPHERAL_GPIO_EDGE_RISING); - if (ret < 0) - printf(">>>> Failed to set edge mode of isr_gpio\n"); - - ret = peripheral_gpio_register_cb(isr_gpio, i2c_mma7455_isr, (void*)i2c); - if (ret < 0) - printf(">>>> Failed to register gpio callback\n"); + if (gpio_num > 0) { + if (mma7455_i2c_register_isr(gpio_num, i2c_mma7455_isr) != 0) + return -1; /* Reset interrupt flags */ - peripheral_i2c_write_register_byte(i2c, MMA7455_INTRST, MMA7455_INTRST_CLRINT); - peripheral_i2c_write_register_byte(i2c, MMA7455_INTRST, MMA7445_INTRST_DONOTCLR); + mma7455_i2c_reset_isr(); + + enable = TRUE; printf("callback is registered on gpio pin %d\n", gpio_num); printf("i2c(bus = %d address = %d) handle is open\n", bus_num, MMA7455_ADDRESS); } else { while (cnt++ < 10) { - uint8_t x_pos, y_pos, z_pos; - unsigned char buf[4]; + mma7455_axes result; + sleep(1); /* Get measurement data with different APIs */ - buf[0] = MMA7455_XOUT8; - peripheral_i2c_write(i2c, buf, 0x1); - peripheral_i2c_read(i2c, &x_pos, 0x1); - buf[0] = MMA7455_YOUT8; - peripheral_i2c_write(i2c, buf, 0x1); - peripheral_i2c_read(i2c, &y_pos, 0x1); - buf[0] = MMA7455_ZOUT8; - peripheral_i2c_write(i2c, buf, 0x1); - peripheral_i2c_read(i2c, &z_pos, 0x1); - printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read)\n", x_pos, y_pos, z_pos); - - peripheral_i2c_write_byte(i2c, MMA7455_XOUT8); - peripheral_i2c_read_byte(i2c, &x_pos); - peripheral_i2c_write_byte(i2c, MMA7455_YOUT8); - peripheral_i2c_read_byte(i2c, &y_pos); - peripheral_i2c_write_byte(i2c, MMA7455_ZOUT8); - peripheral_i2c_read_byte(i2c, &z_pos); - printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read_byte)\n", x_pos, y_pos, z_pos); - - peripheral_i2c_read_register_byte(i2c, MMA7455_XOUT8, &x_pos); - peripheral_i2c_read_register_byte(i2c, MMA7455_YOUT8, &y_pos); - peripheral_i2c_read_register_byte(i2c, MMA7455_ZOUT8, &z_pos); - printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read_register_byte)\n", x_pos, y_pos, z_pos); + mma7455_i2c_get_measurement_1(&result); + printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read)\n", + result.x_pos, result.y_pos, result.z_pos); + mma7455_i2c_get_measurement_2(&result); + printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read_byte)\n", + result.x_pos, result.y_pos, result.z_pos); + + mma7455_i2c_get_measurement_3(&result); + printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read_register_byte)\n", + result.x_pos, result.y_pos, result.z_pos); } - peripheral_i2c_close(i2c); - i2c = NULL; + mma7455_i2c_close(); printf("i2c(bus = %d address = %d) handle is closed\n", bus_num, MMA7455_ADDRESS); } return 0; - -error: - peripheral_i2c_close(i2c); - i2c = NULL; - return -1; } int pwm_test_led(void) @@ -552,19 +484,11 @@ err_open: return -1; } -#define MMA7455_MCTL_SPI3W 0x20 // SPI is 3 wire mode -#define MMA7455_MCTL_DRPD 0x40 // Data ready status is not output to INT1/DRDY PIN - -#define MMA7455_SPI_REGISTER_WRITE 0x40 - int spi_mma7455_module_test(void) { int cnt = 0; - int bus_num, cs_num, ret; - unsigned char tx_buf[10]; - unsigned char rx_buf[10]; - unsigned int num; - peripheral_spi_h spi; + int bus_num, cs_num; + mma7455_axes result; printf(" %s()\n", __func__); printf("Enter SPI bus number : "); @@ -575,55 +499,19 @@ int spi_mma7455_module_test(void) if (scanf("%d", &cs_num) < 0) return -1; - if ((ret = peripheral_spi_open(bus_num, cs_num, &spi)) < 0) { - printf("Failed to open I2C communication, ret : %d\n", ret); + if (mma7455_spi_init(bus_num, cs_num) < 0) return -1; - } - peripheral_spi_set_mode(spi, PERIPHERAL_SPI_MODE_0); - peripheral_spi_set_lsb_first(spi, false); - peripheral_spi_set_bits_per_word(spi, 8); - peripheral_spi_set_frequency(spi, 100000); - - printf("bus : %d, cs : %d, ", bus_num, cs_num); - peripheral_spi_get_mode(spi, (peripheral_spi_mode_e*)&num); - printf("mode : %d, ", num); - peripheral_spi_get_lsb_first(spi, (bool*)&num); - printf("lsb first : %d, ", (bool)num); - peripheral_spi_get_bits_per_word(spi, (unsigned char*)&num); - printf("bits : %d, ", (unsigned char)num); - peripheral_spi_get_frequency(spi, &num); - printf("max frequency : %d\n", num); - - tx_buf[0] = (MMA7455_MCTL | MMA7455_SPI_REGISTER_WRITE) << 1; - tx_buf[1] = MMA7455_MCTL_DRPD | MMA7455_MCTL_SPI3W | MMA7455_MCTL_2G | MMA7455_MCTL_MEASUREMENT_MODE; - if ((ret = peripheral_spi_write(spi, tx_buf, 2)) < 0) { - printf("Failed to write, ret : %d\n", ret); - goto error; - } while (cnt++ < 15) { - int i; - unsigned char buf[5]; - sleep(1); - for (i = 0; i < 3; i++) { - tx_buf[0] = (MMA7455_XOUT8 + i) << 1; - tx_buf[1] = 0; - ret = peripheral_spi_read_write(spi, tx_buf, rx_buf, 2); - if (ret < 0) - printf("Failed to read, ret : %d\n", ret); - buf[i] = rx_buf[1]; - } - - printf("X = 0x%02X, Y = 0x%02X, Z = 0x%02X\n", buf[0], buf[1], buf[2]); + mma7455_spi_get_measurement(&result); + printf("Result X : %d, Y : %d, Z : %d\n", + result.x_pos, result.y_pos, result.z_pos); } - peripheral_spi_close(spi); - return 0; + mma7455_spi_close(); -error: - peripheral_spi_close(spi); - return -1; + return 0; } void gpio_hcsr04_isr(gpio_isr_cb_s *data, void *user_data) diff --git a/test/samples/include/mma7455.h b/test/samples/include/mma7455.h new file mode 100644 index 0000000..bbf75f0 --- /dev/null +++ b/test/samples/include/mma7455.h @@ -0,0 +1,60 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Userspace driver for MMA7455 Accelerometer */ + +#define MMA7455_ADDRESS 0x1D //I2C Address of MMA7455 + +#define MMA7455_MCTL 0x16 // Mode Control(register) +#define MMA7455_MCTL_STANDBY_MODE 0x0 // [1:0] Standby mode +#define MMA7455_MCTL_MEASUREMENT_MODE 0x01 // [1:0] Measurement mode +#define MMA7455_MCTL_LEVEL_DETECTION_MODE 0x02 // [1:0] Level detection mode +#define MMA7455_MCTL_PULSE_DETECTION_MODE 0x03 // [1:0] Pulse detection mode +#define MMA7455_MCTL_2G 0x04 // [3:2] Set Sensitivity to 2g +#define MMA7455_MCTL_4G 0x08 // [3:2] Set Sensitivity to 4g +#define MMA7455_MCTL_8G 0x00 // [3:2] Set Sensitivity to 8g +#define MMA7455_MCTL_STON 0x10 // Self-test is enabled +#define MMA7455_MCTL_SPI3W 0x20 // SPI is 3 wire mode +#define MMA7455_MCTL_DRPD 0x40 // Data ready status is not output to INT1/DRDY PIN + +#define MMA7455_INTRST 0x17 // Interrupt latch reset(register) +#define MMA7455_INTRST_CLRINT 0x03 +#define MMA7445_INTRST_DONOTCLR 0x00 + +#define MMA7455_XOUT8 0x06 // 8 bits output value X (register) +#define MMA7455_YOUT8 0x07 // 8 bits output value Y (register) +#define MMA7455_ZOUT8 0x08 // 8 bits output value Z (register) + +#define MMA7455_SPI_REGISTER_WRITE 0x80 + +typedef struct { + uint8_t x_pos; + uint8_t y_pos; + uint8_t z_pos; +} mma7455_axes; + +int mma7455_i2c_init(int bus_num); +int mma7455_i2c_close(); +int mma7455_i2c_reset_isr(); +int mma7455_i2c_get_measurement_1(mma7455_axes *result); +int mma7455_i2c_get_measurement_2(mma7455_axes *result); +int mma7455_i2c_get_measurement_3(mma7455_axes *result); +int mma7455_i2c_register_isr(const int gpio_num, gpio_isr_cb cb_func); +int mma7455_i2c_unregister_isr(); + +int mma7455_spi_init(int bus_num, int cs_num); +int mma7455_spi_close(); +int mma7455_spi_get_measurement(mma7455_axes *result); diff --git a/test/samples/mma7455.c b/test/samples/mma7455.c new file mode 100644 index 0000000..31e2bb2 --- /dev/null +++ b/test/samples/mma7455.c @@ -0,0 +1,351 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Userspace driver for MMA7455 Accelerometer */ + +#include + +#include "peripheral_io.h" +#include "mma7455.h" + +static peripheral_i2c_h mma7455_i2c; +static peripheral_gpio_h isr_gpio; +static peripheral_spi_h mma7455_spi; + +#define LOG(...) printf(__VA_ARGS__) + +int mma7455_i2c_init(int bus_num) +{ + int ret; + + /* Return if it's already initialized */ + if (mma7455_i2c) { + LOG("Device was already initialized\n"); + return -1; + } + + /* Open I2c communication */ + ret = peripheral_i2c_open(bus_num, MMA7455_ADDRESS, &mma7455_i2c); + if (ret < PERIPHERAL_ERROR_NONE) { + LOG(">>>>> Failed to open I2C communication, ret : %d \n", ret); + return -1; + } + + /* Set mode control register */ + ret = peripheral_i2c_write_register_byte(mma7455_i2c, MMA7455_MCTL, + MMA7455_MCTL_2G | MMA7455_MCTL_PULSE_DETECTION_MODE); + if (ret < PERIPHERAL_ERROR_NONE) { + LOG(">>>>> Failed to write, ret : %d\n", ret); + peripheral_i2c_close(mma7455_i2c); + mma7455_i2c = NULL; + + return -1; + } + + return 0; +} + +int mma7455_i2c_close() +{ + int ret; + + if (!mma7455_i2c) { + LOG("Device is not initialized\n"); + return -1; + } + + /* Set the device to standby mode */ + peripheral_i2c_write_register_byte(mma7455_i2c, MMA7455_MCTL, MMA7455_MCTL_STANDBY_MODE); + + /* Close I2C communication */ + ret = peripheral_i2c_close(mma7455_i2c); + if (ret < PERIPHERAL_ERROR_NONE) + LOG("Failed to close i2c communication, continue anyway\n"); + + mma7455_i2c = NULL; + + /* If gpio for interrupt was registered, unregister it */ + if (isr_gpio) { + peripheral_gpio_unregister_cb(isr_gpio); + peripheral_gpio_close(isr_gpio); + + isr_gpio = NULL; + } + + return ret; +} + +int mma7455_i2c_reset_isr() +{ + int ret; + + if (!mma7455_i2c) { + LOG("Device is not initialized\n"); + return -1; + } + + /* Reset interrupt flags */ + ret = peripheral_i2c_write_register_byte(mma7455_i2c, MMA7455_INTRST, MMA7455_INTRST_CLRINT); + if (ret < PERIPHERAL_ERROR_NONE) + goto error; + + ret = peripheral_i2c_write_register_byte(mma7455_i2c, MMA7455_INTRST, MMA7445_INTRST_DONOTCLR); + if (ret < PERIPHERAL_ERROR_NONE) + goto error; + + return 0; + +error: + LOG(">>>>> Failed to reset interrupt flags\n"); + return ret; +} + +int mma7455_i2c_get_measurement_1(mma7455_axes *result) +{ + unsigned char buf[4]; + + if (!mma7455_i2c) { + LOG("Device is not initialized\n"); + return -1; + } + + /* Read measurement data from register respectively */ + buf[0] = MMA7455_XOUT8; + peripheral_i2c_write(mma7455_i2c, buf, 0x1); + peripheral_i2c_read(mma7455_i2c, &result->x_pos, 0x1); + + buf[0] = MMA7455_YOUT8; + peripheral_i2c_write(mma7455_i2c, buf, 0x1); + peripheral_i2c_read(mma7455_i2c, &result->y_pos, 0x1); + + buf[0] = MMA7455_ZOUT8; + peripheral_i2c_write(mma7455_i2c, buf, 0x1); + peripheral_i2c_read(mma7455_i2c, &result->z_pos, 0x1); + + return 0; +} + +int mma7455_i2c_get_measurement_2(mma7455_axes *result) +{ + if (!mma7455_i2c) { + LOG("Device is not initialized\n"); + return -1; + } + + /* Read measurement data by using i2c_write_byte and i2c_read_byte (SMBUS ioctl) */ + peripheral_i2c_write_byte(mma7455_i2c, MMA7455_XOUT8); + peripheral_i2c_read_byte(mma7455_i2c, &result->x_pos); + + peripheral_i2c_write_byte(mma7455_i2c, MMA7455_YOUT8); + peripheral_i2c_read_byte(mma7455_i2c, &result->y_pos); + + peripheral_i2c_write_byte(mma7455_i2c, MMA7455_ZOUT8); + peripheral_i2c_read_byte(mma7455_i2c, &result->z_pos); + + return 0; +} + +int mma7455_i2c_get_measurement_3(mma7455_axes *result) +{ + if (!mma7455_i2c) { + LOG("Device is not initialized\n"); + return -1; + } + + /* Read measurement data by using i2c_read_register_byte (SMBUS ioctl) */ + peripheral_i2c_read_register_byte(mma7455_i2c, MMA7455_XOUT8, &result->x_pos); + peripheral_i2c_read_register_byte(mma7455_i2c, MMA7455_YOUT8, &result->y_pos); + peripheral_i2c_read_register_byte(mma7455_i2c, MMA7455_ZOUT8, &result->z_pos); + + return 0; +} + +int mma7455_i2c_register_isr(const int gpio_num, gpio_isr_cb cb_func) +{ + int ret; + + if (!mma7455_i2c) { + LOG("Device is not initialized\n"); + return -1; + } + + LOG("mma7455_i2c_register_isr\n"); + + if (gpio_num < 0) { + LOG(">>>>> Wrong gpio number\n"); + return -EINVAL; + } + + if (isr_gpio != NULL) { + LOG(">>>>> GPIO ISR is already registered\n"); + return -EBUSY; + } + + ret = peripheral_gpio_open(gpio_num, &isr_gpio); + if (ret < PERIPHERAL_ERROR_NONE) { + LOG(">>>> Failed to open the GPIO pin\n"); + return ret; + } + + peripheral_gpio_set_direction(isr_gpio, PERIPHERAL_GPIO_DIRECTION_IN); + peripheral_gpio_set_edge_mode(isr_gpio, PERIPHERAL_GPIO_EDGE_RISING); + + ret = peripheral_gpio_register_cb(isr_gpio, cb_func, NULL); + if (ret < PERIPHERAL_ERROR_NONE) { + LOG(">>>> Failed to register gpio callback\n"); + goto error; + } + + return 0; + +error: + peripheral_gpio_close(isr_gpio); + isr_gpio = NULL; + + return ret; +} + +int mma7455_i2c_unregister_isr() +{ + if (!mma7455_i2c) { + LOG("Device is not initialized\n"); + return -1; + } + + /* Check whether GPIO ISR is registered */ + if (isr_gpio == NULL) { + LOG("GPIO ISR is not registered\n"); + return -1; + } + + /* Unregister callback and close the pin */ + peripheral_gpio_unregister_cb(isr_gpio); + peripheral_gpio_close(isr_gpio); + + isr_gpio = NULL; + + return 0; +} + +static int mma7455_spi_mctl_write_byte(unsigned char value) +{ + unsigned char tx_buf[2]; + int ret; + + if (!mma7455_spi) { + LOG("Device is not initialized\n"); + return PERIPHERAL_ERROR_INVALID_OPERATION; + } + + tx_buf[0] = MMA7455_SPI_REGISTER_WRITE | (MMA7455_MCTL << 1); + tx_buf[1] = value; + + if ((ret = peripheral_spi_write(mma7455_spi, tx_buf, 2)) < 0) + return ret; + + return PERIPHERAL_ERROR_NONE; +} + +int mma7455_spi_init(int bus_num, int cs_num) +{ + unsigned int num; + int ret; + + /* Return if it's already initialized */ + if (mma7455_spi) { + LOG("Device was already initialized\n"); + return -1; + } + + /* Open SPI communication */ + ret = peripheral_spi_open(bus_num, cs_num, &mma7455_spi); + if (ret < PERIPHERAL_ERROR_NONE) { + LOG("Failed to open SPI communication, ret : %d\n", ret); + return -1; + } + + peripheral_spi_set_mode(mma7455_spi, PERIPHERAL_SPI_MODE_0); + peripheral_spi_set_lsb_first(mma7455_spi, false); + peripheral_spi_set_bits_per_word(mma7455_spi, 8); + peripheral_spi_set_frequency(mma7455_spi, 8*1024*1024); + + LOG("bus : %d, cs : %d, ", bus_num, cs_num); + peripheral_spi_get_mode(mma7455_spi, (peripheral_spi_mode_e*)&num); + LOG("mode : %d, ", num); + peripheral_spi_get_lsb_first(mma7455_spi, (bool*)&num); + LOG("lsb first : %d, ", (bool)num); + peripheral_spi_get_bits_per_word(mma7455_spi, (unsigned char*)&num); + LOG("bits : %d, ", (unsigned char)num); + peripheral_spi_get_frequency(mma7455_spi, &num); + LOG("max frequency : %d\n", num); + + /* Set mode control register */ + ret = mma7455_spi_mctl_write_byte(MMA7455_MCTL_SPI3W + | MMA7455_MCTL_2G + | MMA7455_MCTL_MEASUREMENT_MODE); + if (ret < PERIPHERAL_ERROR_NONE) { + LOG("Failed to write, ret : %d\n", ret); + peripheral_spi_close(mma7455_spi); + mma7455_spi = NULL; + + return -1; + } + + return 0; +} + +int mma7455_spi_close() +{ + int ret; + + if (!mma7455_spi) { + LOG("Device is not initialized\n"); + return -1; + } + + /* Set the device to standby mode */ + ret = mma7455_spi_mctl_write_byte(MMA7455_MCTL_STANDBY_MODE); + if (ret < PERIPHERAL_ERROR_NONE) + LOG("Failed to set the device to standby mode\n"); + + ret = peripheral_spi_close(mma7455_spi); + if (ret < PERIPHERAL_ERROR_NONE) + LOG("Failed to close i2c communication, continue anyway\n"); + + mma7455_spi = NULL; + + return 0; +} + +int mma7455_spi_get_measurement(mma7455_axes *result) +{ + unsigned char tx_data; + + if (!mma7455_spi) { + LOG("Device is not initialized\n"); + return -1; + } + + /* Read measurement value */ + tx_data = MMA7455_XOUT8 << 1; + peripheral_spi_read_write(mma7455_spi, &tx_data, &(result->x_pos), 1); + tx_data = MMA7455_YOUT8 << 1; + peripheral_spi_read_write(mma7455_spi, &tx_data, &(result->y_pos), 1); + tx_data = MMA7455_ZOUT8 << 1; + peripheral_spi_read_write(mma7455_spi, &tx_data, &(result->z_pos), 1); + + return 0; +} -- 2.7.4 From 787ee84db92802397fb23c1505f422edf2be5fd9 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 1 Aug 2017 10:19:38 +0900 Subject: [PATCH 12/16] Add awk as a BuildRequries In order to avoid possible build break, add BuildRequies for awk explicitly. Change-Id: I0a8ba8be024af0453f1483bb1468df0070fa657c Signed-off-by: Hyeongsik Min --- packaging/capi-system-peripheral-io.spec | 1 + 1 file changed, 1 insertion(+) diff --git a/packaging/capi-system-peripheral-io.spec b/packaging/capi-system-peripheral-io.spec index cc7b1de..1dbb5d2 100644 --- a/packaging/capi-system-peripheral-io.spec +++ b/packaging/capi-system-peripheral-io.spec @@ -6,6 +6,7 @@ Group: System & System Tools License: Apache-2.0 Source0: %{name}-%{version}.tar.gz Source1: %{name}.manifest +BuildRequires: awk BuildRequires: cmake BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(gio-2.0) -- 2.7.4 From 49f491856e1a87fbd001db25753e7a4019af5208 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 16 Aug 2017 21:49:16 +0900 Subject: [PATCH 13/16] Add PIE option to peripheral-io-test Change-Id: I6e0669b4b0a7751d82ffd411006a1f33a9842877 Signed-off-by: Hyeongsik Min --- test/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d6f4dee..e1ba67c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,7 +11,8 @@ ENDFOREACH() SET(SAMPLE_INC_DIR ./samples/include) INCLUDE_DIRECTORIES(${SAMPLE_INC_DIR}) -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -fPIE") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") AUX_SOURCE_DIRECTORY(samples sample_sources) FOREACH(src ${sample_sources}) -- 2.7.4 From e6371555de643fe0ca4a80c3bd8438b38a803e60 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 12 Sep 2017 16:45:06 +0900 Subject: [PATCH 14/16] Fix Security Svace issue This patch fixes security issue from SVACE. "WID:31524889 Casting 'num' [ unsigned int *] to _Bool * makes a pointer with a different pointee size. It can lead to out-of-bounds memory access when dereferenced." Change-Id: If3620630eaa8607d132da751f5ce2dc485b69ce4 Signed-off-by: jino.cho --- test/samples/mma7455.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/samples/mma7455.c b/test/samples/mma7455.c index 31e2bb2..1930b74 100644 --- a/test/samples/mma7455.c +++ b/test/samples/mma7455.c @@ -261,7 +261,10 @@ static int mma7455_spi_mctl_write_byte(unsigned char value) int mma7455_spi_init(int bus_num, int cs_num) { - unsigned int num; + peripheral_spi_mode_e spi_mode; + bool spi_lsb; + unsigned char spi_bits; + unsigned int spi_freq; int ret; /* Return if it's already initialized */ @@ -282,15 +285,12 @@ int mma7455_spi_init(int bus_num, int cs_num) peripheral_spi_set_bits_per_word(mma7455_spi, 8); peripheral_spi_set_frequency(mma7455_spi, 8*1024*1024); - LOG("bus : %d, cs : %d, ", bus_num, cs_num); - peripheral_spi_get_mode(mma7455_spi, (peripheral_spi_mode_e*)&num); - LOG("mode : %d, ", num); - peripheral_spi_get_lsb_first(mma7455_spi, (bool*)&num); - LOG("lsb first : %d, ", (bool)num); - peripheral_spi_get_bits_per_word(mma7455_spi, (unsigned char*)&num); - LOG("bits : %d, ", (unsigned char)num); - peripheral_spi_get_frequency(mma7455_spi, &num); - LOG("max frequency : %d\n", num); + peripheral_spi_get_mode(mma7455_spi, &spi_mode); + peripheral_spi_get_lsb_first(mma7455_spi, &spi_lsb); + peripheral_spi_get_bits_per_word(mma7455_spi, &spi_bits); + peripheral_spi_get_frequency(mma7455_spi, &spi_freq); + LOG("bus : %d, cs : %d, mode : %d, lsb first : %d, bits : %d, max frequency : %d\n", + bus_num, cs_num, spi_mode, spi_lsb, spi_bits, spi_freq); /* Set mode control register */ ret = mma7455_spi_mctl_write_byte(MMA7455_MCTL_SPI3W -- 2.7.4 From 0609e18e0c1a8ff40829ed691e31ac86d1b2afb1 Mon Sep 17 00:00:00 2001 From: Segwon Date: Thu, 21 Sep 2017 09:24:11 +0900 Subject: [PATCH 15/16] test: remove console local test app. - It will be replace to new something. - The reason is many apis changed. Signed-off-by: Segwon Change-Id: I142c413ff220aca1752a701998117d39dfedf22d --- CMakeLists.txt | 2 - packaging/capi-system-peripheral-io.spec | 1 - test/CMakeLists.txt | 30 - test/peripheral-io-test.c | 1097 ------------------------------ test/samples/include/mma7455.h | 60 -- test/samples/mma7455.c | 351 ---------- 6 files changed, 1541 deletions(-) delete mode 100644 test/CMakeLists.txt delete mode 100644 test/peripheral-io-test.c delete mode 100644 test/samples/include/mma7455.h delete mode 100644 test/samples/mma7455.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 725fed5..9455ddb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,5 +85,3 @@ CONFIGURE_FILE( @ONLY ) INSTALL(FILES ${PROJECT_NAME}.pc DESTINATION ${libdir}/pkgconfig) - -ADD_SUBDIRECTORY(test) diff --git a/packaging/capi-system-peripheral-io.spec b/packaging/capi-system-peripheral-io.spec index 1dbb5d2..130bd54 100644 --- a/packaging/capi-system-peripheral-io.spec +++ b/packaging/capi-system-peripheral-io.spec @@ -46,7 +46,6 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` %manifest %{name}.manifest %defattr(-,root,root,-) %{_libdir}/lib%{name}.so* -%{_bindir}/peripheral-io-test %license LICENSE.APLv2 %files devel diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index e1ba67c..0000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,30 +0,0 @@ -PROJECT(peripheral-io-test C) -SET(fw_test "${fw_name}-test") - -INCLUDE(FindPkgConfig) -pkg_check_modules(${fw_test} REQUIRED dlog) -FOREACH(flag ${${fw_test}_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") - MESSAGE(${flag}) -ENDFOREACH() - -SET(SAMPLE_INC_DIR ./samples/include) -INCLUDE_DIRECTORIES(${SAMPLE_INC_DIR}) - -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -fPIE") -SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") - -AUX_SOURCE_DIRECTORY(samples sample_sources) -FOREACH(src ${sample_sources}) - SET(sample_src ${src}) -ENDFOREACH() - -AUX_SOURCE_DIRECTORY(. sources) -FOREACH(src ${sources}) - GET_FILENAME_COMPONENT(src_name ${src} NAME_WE) - MESSAGE("${src_name}") - ADD_EXECUTABLE(${src_name} ${src} ${sample_src}) - TARGET_LINK_LIBRARIES(${src_name} ${fw_name} ${${fw_test}_LDFLAGS}) -ENDFOREACH() - -INSTALL(TARGETS peripheral-io-test RUNTIME DESTINATION bin/) \ No newline at end of file diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c deleted file mode 100644 index 167a7a9..0000000 --- a/test/peripheral-io-test.c +++ /dev/null @@ -1,1097 +0,0 @@ -/* - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include - -#include "peripheral_io.h" - -#include "mma7455.h" - -#define BUFFER_LEN 32 - -typedef struct { - int cnt; - peripheral_gpio_h gpio_trig; - peripheral_gpio_h gpio_echo; -} gpio_hcsr04_module_s; - -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_led_test(void) -{ - int num, ret; - int cnt = 0; - peripheral_gpio_h handle = NULL; - - printf(" %s()\n", __func__); - printf("Enter GPIO pin number "); - - if (scanf("%d", &num) < 0) - return -1; - printf("num %d\n", num); - - if ((ret = peripheral_gpio_open(num, &handle)) < PERIPHERAL_ERROR_NONE) { - printf("Failed to open\n"); - return ret; - } - - if ((ret = peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT)) < PERIPHERAL_ERROR_NONE) { - printf("Failed to set direction!!\n"); - goto error; - } - - while (cnt++ < 5) { - printf("Writing..\n"); - peripheral_gpio_write(handle, 1); - sleep(1); - peripheral_gpio_write(handle, 0); - sleep(1); - } - printf("Write finish\n"); - 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 ret; -} - -void gpio_irq_test_isr(gpio_isr_cb_s *data, void *user_data) -{ - int pin; - peripheral_gpio_h gpio = user_data; - - peripheral_gpio_get_pin(gpio, &pin); - - printf("%s: GPIO %d interrupt occurs.\n", __func__, pin); -} - -int gpio_irq_register(void) -{ - peripheral_gpio_h gpio = NULL; - 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(">>>>> Failed to open GPIO pin, ret : %d\n", ret); - return -1; - } - gpio_list = g_list_append(gpio_list, gpio); - - 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); - - return 0; - -error: - gpio_list = g_list_remove(gpio_list, gpio); - peripheral_gpio_close(gpio); - return -1; -} - -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); - - 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; - } - - gpio_list = g_list_remove(gpio_list, gpio); - peripheral_gpio_close(gpio); - - return 0; -} - -/* Address of GY30 light sensor */ -#define GY30_ADDR 0x23 - -/* Start measurement at 11x resolution. Measurement time is approx 120ms. */ -#define GY30_CONT_HIGH_RES_MODE 0x10 - -#define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2) - -int i2c_gy30_test(void) -{ - int cnt = 0; - unsigned char buf[10]; - peripheral_i2c_h i2c; - struct timeval tv_1, tv_2; - int bus_num, ret, result, interval; - - printf(" %s()\n", __func__); - printf("Enter I2C bus number\n"); - - if (read_int_input(&bus_num) < 0) - return -1; - - if ((ret = peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) < 0) { - printf("Failed to open I2C communication, ret : %d\n", ret); - return -1; - } - - if ((ret = peripheral_i2c_write_byte(i2c, GY30_CONT_HIGH_RES_MODE)) < 0) { - printf("Failed to write, ret : %d\n", ret); - goto error; - } - - gettimeofday(&tv_1, NULL); - while (cnt++ < 1000) { - ret = peripheral_i2c_read(i2c, buf, 2); - if (ret < 0) - printf("Failed to read, ret : %d\n", ret); - result = GY30_READ_INTENSITY(buf); - printf("Light intensity : %d\n", result); - } - gettimeofday(&tv_2, NULL); - interval = (tv_2.tv_sec - tv_1.tv_sec) * 1000 + (int)(tv_2.tv_usec - tv_1.tv_usec)/1000; - printf("1000 i2c read calls took %d ms\n", interval); - - peripheral_i2c_close(i2c); - return 0; - -error: - peripheral_i2c_close(i2c); - return -1; -} - -static void i2c_mma7455_isr(gpio_isr_cb_s *data, void *user_data) -{ - mma7455_axes result; - - mma7455_i2c_get_measurement_3(&result); - printf("Result X : %d, Y : %d, Z : %d\n", result.x_pos, result.y_pos, result.z_pos); - - /* Reset interrupt flags */ - mma7455_i2c_reset_isr(); - - return; -} - -int i2c_mma7455_test(void) -{ - static int gpio_num; - static bool enable; - int bus_num; - int cnt = 0; - - printf(" %s()\n", __func__); - if (enable) { - printf("Disabling the test\n"); - - mma7455_i2c_unregister_isr(); - mma7455_i2c_close(); - printf("mma7455 is closed\n"); - - enable = FALSE; - - return 0; - } - - printf("Enter I2C bus number\n"); - - if (read_int_input(&bus_num) < 0) - return -1; - - if (mma7455_i2c_init(bus_num) < 0) - return -1; - - printf("Enter GPIO pin number for Interrupt\n"); - if (read_int_input(&gpio_num) < 0) - gpio_num = -1; - - if (gpio_num > 0) { - if (mma7455_i2c_register_isr(gpio_num, i2c_mma7455_isr) != 0) - return -1; - - /* Reset interrupt flags */ - mma7455_i2c_reset_isr(); - - enable = TRUE; - - printf("callback is registered on gpio pin %d\n", gpio_num); - printf("i2c(bus = %d address = %d) handle is open\n", bus_num, MMA7455_ADDRESS); - } else { - while (cnt++ < 10) { - mma7455_axes result; - - sleep(1); - - /* Get measurement data with different APIs */ - mma7455_i2c_get_measurement_1(&result); - printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read)\n", - result.x_pos, result.y_pos, result.z_pos); - - mma7455_i2c_get_measurement_2(&result); - printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read_byte)\n", - result.x_pos, result.y_pos, result.z_pos); - - mma7455_i2c_get_measurement_3(&result); - printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read_register_byte)\n", - result.x_pos, result.y_pos, result.z_pos); - } - mma7455_i2c_close(); - printf("i2c(bus = %d address = %d) handle is closed\n", bus_num, MMA7455_ADDRESS); - } - return 0; -} - -int pwm_test_led(void) -{ - int device, channel, ret; - int period = 1 * 1000; - int duty_cycle = 1 * 1000 / 100; - int cnt = 0; - - int set_duty_cycle; - int get_period, get_duty_cycle; - peripheral_pwm_h dev; - - printf(" %s()\n", __func__); - printf("Enter PWM device number\n"); - if (read_int_input(&device) < 0) - return -1; - - printf("Enter PWM channel number\n"); - if (read_int_input(&channel) < 0) - return -1; - - ret = peripheral_pwm_open(device, channel, &dev); - if (ret != PERIPHERAL_ERROR_NONE) { - printf("Failed to open\n"); - return ret; - } - peripheral_pwm_set_period(dev, period); /* period: nanosecond */ - peripheral_pwm_set_duty_cycle(dev, duty_cycle); /* duty_cycle: nanosecond */ - peripheral_pwm_set_enable(dev, 1); /* 0: disable, 1: enable */ - - while (cnt < 2) { - for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) { - /* set duty cycle */ - peripheral_pwm_set_duty_cycle(dev, set_duty_cycle); - peripheral_pwm_get_period(dev, &get_period); - peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle); - printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle); - usleep(500000); - } - for (set_duty_cycle = 0; set_duty_cycle < period; set_duty_cycle += 50) { - /* set duty cycle */ - peripheral_pwm_set_duty_cycle(dev, set_duty_cycle); - peripheral_pwm_get_period(dev, &get_period); - peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle); - printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle); - usleep(500000); - } - cnt++; - } - peripheral_pwm_set_enable(dev, 0); /* 0: disable, 1: enable */ - peripheral_pwm_close(dev); - - return 0; -} - -int pwm_test_motor(void) -{ - int device, channel, ret; - int period = 20000000; - int duty_cycle = 1500000; - int cnt = 0, idx = 0; - int degree[3] = {0, 45, 90}; - peripheral_pwm_h dev; - - printf(" %s()\n", __func__); - printf("Enter PWM device number\n"); - if (read_int_input(&device) < 0) - return -1; - - printf("Enter PWM channel number\n"); - if (read_int_input(&channel) < 0) - return -1; - - ret = peripheral_pwm_open(device, channel, &dev); - if (ret != PERIPHERAL_ERROR_NONE) { - printf("Failed to open\n"); - return ret; - } - for (cnt = 0; cnt < 5; cnt++) { - for (idx = 0; idx < 3; idx++) { - switch (degree[idx]) { - case 0: - duty_cycle = 1000000; - break; - case 45: - duty_cycle = 1500000; - break; - case 90: - duty_cycle = 2000000; - break; - default: - duty_cycle = 2000000; - break; - } - printf("set degree: %d\n", degree[idx]); - peripheral_pwm_set_period(dev, period); - peripheral_pwm_set_duty_cycle(dev, duty_cycle); - peripheral_pwm_set_enable(dev, 1); /* 0: disable, 1: enable */ - usleep(500000); - } - } - - peripheral_pwm_set_enable(dev, 0); /* 0: disable, 1: enable */ - peripheral_pwm_close(dev); - - return 0; -} - - -int uart_test_accelerometer(void) -{ - peripheral_uart_h uart; - int ret; - int port; - int loop = 100; - unsigned char buf[1024]; - - printf(" %s()\n", __func__); - printf("Enter port number"); - - if (scanf("%d", &port) < 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; - - 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); - } - - peripheral_uart_close(uart); - return 0; - -out: - peripheral_uart_close(uart); - -err_open: - return -1; -} - -int spi_mma7455_module_test(void) -{ - int cnt = 0; - int bus_num, cs_num; - mma7455_axes result; - - printf(" %s()\n", __func__); - printf("Enter SPI bus number : "); - if (scanf("%d", &bus_num) < 0) - return -1; - - printf("Enter SPI cs number : "); - if (scanf("%d", &cs_num) < 0) - return -1; - - if (mma7455_spi_init(bus_num, cs_num) < 0) - return -1; - - while (cnt++ < 15) { - sleep(1); - mma7455_spi_get_measurement(&result); - printf("Result X : %d, Y : %d, Z : %d\n", - result.x_pos, result.y_pos, result.z_pos); - } - - mma7455_spi_close(); - - return 0; -} - -void gpio_hcsr04_isr(gpio_isr_cb_s *data, void *user_data) -{ - float dist = 0; - static unsigned long long timestamp = 0; - - if (timestamp > 0 && data->value == 0) { - dist = data->timestamp - timestamp; - dist = (dist * 34300) / 2000000; - printf("%s: Measured Distance : %0.2fcm\n", __func__, dist); - } - - timestamp = data->timestamp; -} - -gboolean gpio_hcsr04_timeout_cb(gpointer data) -{ - gpio_hcsr04_module_s *dev = (gpio_hcsr04_module_s*)data; - - if (dev->cnt--) { - peripheral_gpio_write(dev->gpio_trig, 1); - peripheral_gpio_write(dev->gpio_trig, 0); - } else - return FALSE; - - return TRUE; -} - -void gpio_hcsr04_destroy(gpointer data) -{ - gpio_hcsr04_module_s *dev = (gpio_hcsr04_module_s*)data; - - peripheral_gpio_unregister_cb(dev->gpio_echo); - peripheral_gpio_close(dev->gpio_echo); - peripheral_gpio_close(dev->gpio_trig); - free(dev); -} - -int gpio_hcsr04_ultrasonic_ranging_module(void) -{ - gpio_hcsr04_module_s *dev; - int pin_trig, pin_echo, ret; - - printf(" %s()\n", __func__); - printf("Enter triger gpio pin number\n"); - if (read_int_input(&pin_trig) < 0) - return -1; - - printf("Enter echo gpio pin number\n"); - if (read_int_input(&pin_echo) < 0) - return -1; - - dev = calloc(1, sizeof(gpio_hcsr04_module_s)); - if (dev == NULL) { - printf("failed to allocate gpio_hcsr04_module_s\n"); - return -1; - } - - if ((ret = peripheral_gpio_open(pin_trig, &dev->gpio_trig)) < 0) { - printf(">>>>> Failed to open GPIO pin, ret : %d\n", ret); - goto err; - } - - if ((ret = peripheral_gpio_open(pin_echo, &dev->gpio_echo)) < 0) { - printf(">>>>> Failed to open GPIO pin, ret : %d\n", ret); - peripheral_gpio_close(dev->gpio_trig); - goto err; - } - - peripheral_gpio_set_direction(dev->gpio_echo, PERIPHERAL_GPIO_DIRECTION_IN); - peripheral_gpio_set_edge_mode(dev->gpio_echo, PERIPHERAL_GPIO_EDGE_BOTH); - peripheral_gpio_register_cb(dev->gpio_echo, gpio_hcsr04_isr, NULL); - peripheral_gpio_set_direction(dev->gpio_trig, PERIPHERAL_GPIO_DIRECTION_OUT); - - dev->cnt = 20; - - g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, - 1, - gpio_hcsr04_timeout_cb, - dev, - gpio_hcsr04_destroy); - - return 0; - -err: - free(dev); - 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 adc_read_channel(void) -{ - int device, channel, ret; - int value; - - printf("%s\n", __func__); - - printf("Enter adc device number\n"); - if (read_int_input(&device) < 0) - return -1; - - printf("Enter adc channel number\n"); - if (read_int_input(&channel) < 0) - return -1; - - if ((ret = peripheral_adc_read(device, channel, &value)) < 0) { - printf(">>>>> Failed to read adc value, ret : %d\n", ret); - return -1; - } - printf("ADC(%d,%d) Value = %d\n", device, channel, value); - - return 0; -} - -tc_table_t adc_tc_table[] = { - {"Read ADC Channel", 1, adc_read_channel}, - {"Go back to main", 0, enter_main}, - {NULL, 0, NULL}, -}; - -int enter_adc_test(void) -{ - tc_table = adc_tc_table; - - return 0; -} - -int enter_uart_test(void) -{ - return 0; -} - -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] SPI MMA7455 Accel. sensor", 7, spi_mma7455_module_test}, - {"[Preset Test] GPIO HC-SR04 Range sensor", 8, gpio_hcsr04_ultrasonic_ranging_module}, - {"[Preset Test] GPIO IRQ register", 10, gpio_irq_register}, - {"[Preset Test] GPIO IRQ unregister", 11, 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; - - 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", 10, enter_preset_test}, - {"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) -{ - tc_table_t *tc; - long test_id = (long)data; - int ret = PERIPHERAL_ERROR_NONE; - int i = 0; - - 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); - - break; - } - i++; - } - if (!tc[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) -{ - GIOChannel *key_io; - - main_loop = g_main_loop_new(NULL, FALSE); - key_io = g_io_channel_unix_new(0); - - tc_table = main_tc_table; - - 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); - - g_main_loop_run(main_loop); - - g_io_channel_unref(key_io); - g_main_loop_unref(main_loop); - - return 0; -} diff --git a/test/samples/include/mma7455.h b/test/samples/include/mma7455.h deleted file mode 100644 index bbf75f0..0000000 --- a/test/samples/include/mma7455.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* Userspace driver for MMA7455 Accelerometer */ - -#define MMA7455_ADDRESS 0x1D //I2C Address of MMA7455 - -#define MMA7455_MCTL 0x16 // Mode Control(register) -#define MMA7455_MCTL_STANDBY_MODE 0x0 // [1:0] Standby mode -#define MMA7455_MCTL_MEASUREMENT_MODE 0x01 // [1:0] Measurement mode -#define MMA7455_MCTL_LEVEL_DETECTION_MODE 0x02 // [1:0] Level detection mode -#define MMA7455_MCTL_PULSE_DETECTION_MODE 0x03 // [1:0] Pulse detection mode -#define MMA7455_MCTL_2G 0x04 // [3:2] Set Sensitivity to 2g -#define MMA7455_MCTL_4G 0x08 // [3:2] Set Sensitivity to 4g -#define MMA7455_MCTL_8G 0x00 // [3:2] Set Sensitivity to 8g -#define MMA7455_MCTL_STON 0x10 // Self-test is enabled -#define MMA7455_MCTL_SPI3W 0x20 // SPI is 3 wire mode -#define MMA7455_MCTL_DRPD 0x40 // Data ready status is not output to INT1/DRDY PIN - -#define MMA7455_INTRST 0x17 // Interrupt latch reset(register) -#define MMA7455_INTRST_CLRINT 0x03 -#define MMA7445_INTRST_DONOTCLR 0x00 - -#define MMA7455_XOUT8 0x06 // 8 bits output value X (register) -#define MMA7455_YOUT8 0x07 // 8 bits output value Y (register) -#define MMA7455_ZOUT8 0x08 // 8 bits output value Z (register) - -#define MMA7455_SPI_REGISTER_WRITE 0x80 - -typedef struct { - uint8_t x_pos; - uint8_t y_pos; - uint8_t z_pos; -} mma7455_axes; - -int mma7455_i2c_init(int bus_num); -int mma7455_i2c_close(); -int mma7455_i2c_reset_isr(); -int mma7455_i2c_get_measurement_1(mma7455_axes *result); -int mma7455_i2c_get_measurement_2(mma7455_axes *result); -int mma7455_i2c_get_measurement_3(mma7455_axes *result); -int mma7455_i2c_register_isr(const int gpio_num, gpio_isr_cb cb_func); -int mma7455_i2c_unregister_isr(); - -int mma7455_spi_init(int bus_num, int cs_num); -int mma7455_spi_close(); -int mma7455_spi_get_measurement(mma7455_axes *result); diff --git a/test/samples/mma7455.c b/test/samples/mma7455.c deleted file mode 100644 index 1930b74..0000000 --- a/test/samples/mma7455.c +++ /dev/null @@ -1,351 +0,0 @@ -/* - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* Userspace driver for MMA7455 Accelerometer */ - -#include - -#include "peripheral_io.h" -#include "mma7455.h" - -static peripheral_i2c_h mma7455_i2c; -static peripheral_gpio_h isr_gpio; -static peripheral_spi_h mma7455_spi; - -#define LOG(...) printf(__VA_ARGS__) - -int mma7455_i2c_init(int bus_num) -{ - int ret; - - /* Return if it's already initialized */ - if (mma7455_i2c) { - LOG("Device was already initialized\n"); - return -1; - } - - /* Open I2c communication */ - ret = peripheral_i2c_open(bus_num, MMA7455_ADDRESS, &mma7455_i2c); - if (ret < PERIPHERAL_ERROR_NONE) { - LOG(">>>>> Failed to open I2C communication, ret : %d \n", ret); - return -1; - } - - /* Set mode control register */ - ret = peripheral_i2c_write_register_byte(mma7455_i2c, MMA7455_MCTL, - MMA7455_MCTL_2G | MMA7455_MCTL_PULSE_DETECTION_MODE); - if (ret < PERIPHERAL_ERROR_NONE) { - LOG(">>>>> Failed to write, ret : %d\n", ret); - peripheral_i2c_close(mma7455_i2c); - mma7455_i2c = NULL; - - return -1; - } - - return 0; -} - -int mma7455_i2c_close() -{ - int ret; - - if (!mma7455_i2c) { - LOG("Device is not initialized\n"); - return -1; - } - - /* Set the device to standby mode */ - peripheral_i2c_write_register_byte(mma7455_i2c, MMA7455_MCTL, MMA7455_MCTL_STANDBY_MODE); - - /* Close I2C communication */ - ret = peripheral_i2c_close(mma7455_i2c); - if (ret < PERIPHERAL_ERROR_NONE) - LOG("Failed to close i2c communication, continue anyway\n"); - - mma7455_i2c = NULL; - - /* If gpio for interrupt was registered, unregister it */ - if (isr_gpio) { - peripheral_gpio_unregister_cb(isr_gpio); - peripheral_gpio_close(isr_gpio); - - isr_gpio = NULL; - } - - return ret; -} - -int mma7455_i2c_reset_isr() -{ - int ret; - - if (!mma7455_i2c) { - LOG("Device is not initialized\n"); - return -1; - } - - /* Reset interrupt flags */ - ret = peripheral_i2c_write_register_byte(mma7455_i2c, MMA7455_INTRST, MMA7455_INTRST_CLRINT); - if (ret < PERIPHERAL_ERROR_NONE) - goto error; - - ret = peripheral_i2c_write_register_byte(mma7455_i2c, MMA7455_INTRST, MMA7445_INTRST_DONOTCLR); - if (ret < PERIPHERAL_ERROR_NONE) - goto error; - - return 0; - -error: - LOG(">>>>> Failed to reset interrupt flags\n"); - return ret; -} - -int mma7455_i2c_get_measurement_1(mma7455_axes *result) -{ - unsigned char buf[4]; - - if (!mma7455_i2c) { - LOG("Device is not initialized\n"); - return -1; - } - - /* Read measurement data from register respectively */ - buf[0] = MMA7455_XOUT8; - peripheral_i2c_write(mma7455_i2c, buf, 0x1); - peripheral_i2c_read(mma7455_i2c, &result->x_pos, 0x1); - - buf[0] = MMA7455_YOUT8; - peripheral_i2c_write(mma7455_i2c, buf, 0x1); - peripheral_i2c_read(mma7455_i2c, &result->y_pos, 0x1); - - buf[0] = MMA7455_ZOUT8; - peripheral_i2c_write(mma7455_i2c, buf, 0x1); - peripheral_i2c_read(mma7455_i2c, &result->z_pos, 0x1); - - return 0; -} - -int mma7455_i2c_get_measurement_2(mma7455_axes *result) -{ - if (!mma7455_i2c) { - LOG("Device is not initialized\n"); - return -1; - } - - /* Read measurement data by using i2c_write_byte and i2c_read_byte (SMBUS ioctl) */ - peripheral_i2c_write_byte(mma7455_i2c, MMA7455_XOUT8); - peripheral_i2c_read_byte(mma7455_i2c, &result->x_pos); - - peripheral_i2c_write_byte(mma7455_i2c, MMA7455_YOUT8); - peripheral_i2c_read_byte(mma7455_i2c, &result->y_pos); - - peripheral_i2c_write_byte(mma7455_i2c, MMA7455_ZOUT8); - peripheral_i2c_read_byte(mma7455_i2c, &result->z_pos); - - return 0; -} - -int mma7455_i2c_get_measurement_3(mma7455_axes *result) -{ - if (!mma7455_i2c) { - LOG("Device is not initialized\n"); - return -1; - } - - /* Read measurement data by using i2c_read_register_byte (SMBUS ioctl) */ - peripheral_i2c_read_register_byte(mma7455_i2c, MMA7455_XOUT8, &result->x_pos); - peripheral_i2c_read_register_byte(mma7455_i2c, MMA7455_YOUT8, &result->y_pos); - peripheral_i2c_read_register_byte(mma7455_i2c, MMA7455_ZOUT8, &result->z_pos); - - return 0; -} - -int mma7455_i2c_register_isr(const int gpio_num, gpio_isr_cb cb_func) -{ - int ret; - - if (!mma7455_i2c) { - LOG("Device is not initialized\n"); - return -1; - } - - LOG("mma7455_i2c_register_isr\n"); - - if (gpio_num < 0) { - LOG(">>>>> Wrong gpio number\n"); - return -EINVAL; - } - - if (isr_gpio != NULL) { - LOG(">>>>> GPIO ISR is already registered\n"); - return -EBUSY; - } - - ret = peripheral_gpio_open(gpio_num, &isr_gpio); - if (ret < PERIPHERAL_ERROR_NONE) { - LOG(">>>> Failed to open the GPIO pin\n"); - return ret; - } - - peripheral_gpio_set_direction(isr_gpio, PERIPHERAL_GPIO_DIRECTION_IN); - peripheral_gpio_set_edge_mode(isr_gpio, PERIPHERAL_GPIO_EDGE_RISING); - - ret = peripheral_gpio_register_cb(isr_gpio, cb_func, NULL); - if (ret < PERIPHERAL_ERROR_NONE) { - LOG(">>>> Failed to register gpio callback\n"); - goto error; - } - - return 0; - -error: - peripheral_gpio_close(isr_gpio); - isr_gpio = NULL; - - return ret; -} - -int mma7455_i2c_unregister_isr() -{ - if (!mma7455_i2c) { - LOG("Device is not initialized\n"); - return -1; - } - - /* Check whether GPIO ISR is registered */ - if (isr_gpio == NULL) { - LOG("GPIO ISR is not registered\n"); - return -1; - } - - /* Unregister callback and close the pin */ - peripheral_gpio_unregister_cb(isr_gpio); - peripheral_gpio_close(isr_gpio); - - isr_gpio = NULL; - - return 0; -} - -static int mma7455_spi_mctl_write_byte(unsigned char value) -{ - unsigned char tx_buf[2]; - int ret; - - if (!mma7455_spi) { - LOG("Device is not initialized\n"); - return PERIPHERAL_ERROR_INVALID_OPERATION; - } - - tx_buf[0] = MMA7455_SPI_REGISTER_WRITE | (MMA7455_MCTL << 1); - tx_buf[1] = value; - - if ((ret = peripheral_spi_write(mma7455_spi, tx_buf, 2)) < 0) - return ret; - - return PERIPHERAL_ERROR_NONE; -} - -int mma7455_spi_init(int bus_num, int cs_num) -{ - peripheral_spi_mode_e spi_mode; - bool spi_lsb; - unsigned char spi_bits; - unsigned int spi_freq; - int ret; - - /* Return if it's already initialized */ - if (mma7455_spi) { - LOG("Device was already initialized\n"); - return -1; - } - - /* Open SPI communication */ - ret = peripheral_spi_open(bus_num, cs_num, &mma7455_spi); - if (ret < PERIPHERAL_ERROR_NONE) { - LOG("Failed to open SPI communication, ret : %d\n", ret); - return -1; - } - - peripheral_spi_set_mode(mma7455_spi, PERIPHERAL_SPI_MODE_0); - peripheral_spi_set_lsb_first(mma7455_spi, false); - peripheral_spi_set_bits_per_word(mma7455_spi, 8); - peripheral_spi_set_frequency(mma7455_spi, 8*1024*1024); - - peripheral_spi_get_mode(mma7455_spi, &spi_mode); - peripheral_spi_get_lsb_first(mma7455_spi, &spi_lsb); - peripheral_spi_get_bits_per_word(mma7455_spi, &spi_bits); - peripheral_spi_get_frequency(mma7455_spi, &spi_freq); - LOG("bus : %d, cs : %d, mode : %d, lsb first : %d, bits : %d, max frequency : %d\n", - bus_num, cs_num, spi_mode, spi_lsb, spi_bits, spi_freq); - - /* Set mode control register */ - ret = mma7455_spi_mctl_write_byte(MMA7455_MCTL_SPI3W - | MMA7455_MCTL_2G - | MMA7455_MCTL_MEASUREMENT_MODE); - if (ret < PERIPHERAL_ERROR_NONE) { - LOG("Failed to write, ret : %d\n", ret); - peripheral_spi_close(mma7455_spi); - mma7455_spi = NULL; - - return -1; - } - - return 0; -} - -int mma7455_spi_close() -{ - int ret; - - if (!mma7455_spi) { - LOG("Device is not initialized\n"); - return -1; - } - - /* Set the device to standby mode */ - ret = mma7455_spi_mctl_write_byte(MMA7455_MCTL_STANDBY_MODE); - if (ret < PERIPHERAL_ERROR_NONE) - LOG("Failed to set the device to standby mode\n"); - - ret = peripheral_spi_close(mma7455_spi); - if (ret < PERIPHERAL_ERROR_NONE) - LOG("Failed to close i2c communication, continue anyway\n"); - - mma7455_spi = NULL; - - return 0; -} - -int mma7455_spi_get_measurement(mma7455_axes *result) -{ - unsigned char tx_data; - - if (!mma7455_spi) { - LOG("Device is not initialized\n"); - return -1; - } - - /* Read measurement value */ - tx_data = MMA7455_XOUT8 << 1; - peripheral_spi_read_write(mma7455_spi, &tx_data, &(result->x_pos), 1); - tx_data = MMA7455_YOUT8 << 1; - peripheral_spi_read_write(mma7455_spi, &tx_data, &(result->y_pos), 1); - tx_data = MMA7455_ZOUT8 << 1; - peripheral_spi_read_write(mma7455_spi, &tx_data, &(result->z_pos), 1); - - return 0; -} -- 2.7.4 From 24831d9548318f4707a4d98a603952287fbeeb70 Mon Sep 17 00:00:00 2001 From: Segwon Date: Thu, 24 Aug 2017 19:39:51 +0900 Subject: [PATCH 16/16] api: modified with new API set. - ADC not supported - Provides get list API to get necessary for open APIs - Remove unnecessary and uncertain APIs - Terminology unification - Add peripheral_io_doc.h for doxygen - Add feature key and privilege level to doxygen Signed-off-by: Segwon Change-Id: Ie104753f270fdcc37df62e538c9ee06d254c2db2 --- doc/peripheral_io_doc.h | 173 ++++++++ include/peripheral_io.h | 1096 +++++++++++++++++++++++++---------------------- 2 files changed, 753 insertions(+), 516 deletions(-) create mode 100644 doc/peripheral_io_doc.h diff --git a/doc/peripheral_io_doc.h b/doc/peripheral_io_doc.h new file mode 100644 index 0000000..8c373bf --- /dev/null +++ b/doc/peripheral_io_doc.h @@ -0,0 +1,173 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @ingroup CAPI_SYSTEM_FRAMEWORK + * @defgroup CAPI_SYSTEM_PERIPHERAL_IO_MODULE Peripheral IO + * @brief The @ref CAPI_SYSTEM_PERIPHERAL_IO_MODULE API provides functions to make use of peripherals in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_MODULE_OVERVIEW Overview + * This @ref CAPI_SYSTEM_PERIPHERAL_IO_MODULE API provides access to the low level device providers, + * including GPIO, I2C, PWM, UART and SPI. + */ + + +/** + * @ingroup CAPI_SYSTEM_PERIPHERAL_IO_MODULE + * @defgroup CAPI_SYSTEM_PERIPHERAL_IO_GPIO_MODULE GPIO + * @brief The @ref CAPI_SYSTEM_PERIPHERAL_IO_GPIO_MODULE API provides functions to control GPIO in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_GPIO_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_GPIO_MODULE_OVERVIEW Overview + * + * This @ref CAPI_SYSTEM_PERIPHERAL_IO_GPIO_MODULE API provides functions to control GPIO in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_GPIO_MODULE_FEATURE Realted Features + * + * This API is related with the following feature:\n + * - http://tizen.org/feature/peripheralio.gpio\n + * + * It is recommended to use features in your application for reliability.\n + * + * You can check if a device supports the related features for this API \n + * by using @ref CAPI_SYSTEM_SYSTEM_INFO_MODULE, and control your application's actions accordingly.\n + * + * To ensure your application is only running on the device with specific features, \n + * please define the features in your manifest file using the manifest editor in the SDK.\n + * + * More details on featuring your application can be found from Feature Element. + */ + + +/** + * @ingroup CAPI_SYSTEM_PERIPHERAL_IO_MODULE + * @defgroup CAPI_SYSTEM_PERIPHERAL_IO_I2C_MODULE I2C + * @brief The @ref CAPI_SYSTEM_PERIPHERAL_IO_I2C_MODULE API provides functions to control I2C in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_I2C_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_I2C_MODULE_OVERVIEW Overview + * + * This @ref CAPI_SYSTEM_PERIPHERAL_IO_I2C_MODULE API provides functions to control I2C in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_I2C_MODULE_FEATURE Realted Features + * + * This API is related with the following feature:\n + * - http://tizen.org/feature/peripheralio.i2c\n + * + * It is recommended to use features in your application for reliability.\n + * + * You can check if a device supports the related features for this API \n + * by using @ref CAPI_SYSTEM_SYSTEM_INFO_MODULE, and control your application's actions accordingly.\n + * + * To ensure your application is only running on the device with specific features, \n + * please define the features in your manifest file using the manifest editor in the SDK.\n + * + * More details on featuring your application can be found from Feature Element. + */ + + +/** + * @ingroup CAPI_SYSTEM_PERIPHERAL_IO_MODULE + * @defgroup CAPI_SYSTEM_PERIPHERAL_IO_SPI_MODULE SPI + * @brief The @ref CAPI_SYSTEM_PERIPHERAL_IO_SPI_MODULE API provides functions to control SPI in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_SPI_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_SPI_MODULE_OVERVIEW Overview + * + * This @ref CAPI_SYSTEM_PERIPHERAL_IO_SPI_MODULE API provides functions to control SPI in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_SPI_MODULE_FEATURE Realted Features + * + * This API is related with the following feature:\n + * - http://tizen.org/feature/peripheralio.spi\n + * + * It is recommended to use features in your application for reliability.\n + * + * You can check if a device supports the related features for this API \n + * by using @ref CAPI_SYSTEM_SYSTEM_INFO_MODULE, and control your application's actions accordingly.\n + * + * To ensure your application is only running on the device with specific features, \n + * please define the features in your manifest file using the manifest editor in the SDK.\n + * + * More details on featuring your application can be found from Feature Element. + */ + + +/** + * @ingroup CAPI_SYSTEM_PERIPHERAL_IO_MODULE + * @defgroup CAPI_SYSTEM_PERIPHERAL_IO_UART_MODULE UART + * @brief The @ref CAPI_SYSTEM_PERIPHERAL_IO_UART_MODULE API provides functions to control UART in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_UART_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_UART_MODULE_OVERVIEW Overview + * + * This @ref CAPI_SYSTEM_PERIPHERAL_IO_UART_MODULE API provides functions to control UART in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_UART_MODULE_FEATURE Realted Features + * + * This API is related with the following feature:\n + * - http://tizen.org/feature/peripheralio.uart\n + * + * It is recommended to use features in your application for reliability.\n + * + * You can check if a device supports the related features for this API \n + * by using @ref CAPI_SYSTEM_SYSTEM_INFO_MODULE, and control your application's actions accordingly.\n + * + * To ensure your application is only running on the device with specific features, \n + * please define the features in your manifest file using the manifest editor in the SDK.\n + * + * More details on featuring your application can be found from Feature Element. + */ + + +/** + * @ingroup CAPI_SYSTEM_PERIPHERAL_IO_MODULE + * @defgroup CAPI_SYSTEM_PERIPHERAL_IO_PWM_MODULE PWM + * @brief The @ref CAPI_SYSTEM_PERIPHERAL_IO_PWM_MODULE API provides functions to control PWM in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_PWM_MODULE_HEADER Required Header + * \#include + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_PWM_MODULE_OVERVIEW Overview + * + * This @ref CAPI_SYSTEM_PERIPHERAL_IO_PWM_MODULE API provides functions to control PWM in the device. + * + * @section CAPI_SYSTEM_PERIPHERAL_IO_PWM_MODULE_FEATURE Realted Features + * + * This API is related with the following feature:\n + * - http://tizen.org/feature/peripheralio.pwm\n + * + * It is recommended to use features in your application for reliability.\n + * + * You can check if a device supports the related features for this API \n + * by using @ref CAPI_SYSTEM_SYSTEM_INFO_MODULE, and control your application's actions accordingly.\n + * + * To ensure your application is only running on the device with specific features, \n + * please define the features in your manifest file using the manifest editor in the SDK.\n + * + * More details on featuring your application can be found from Feature Element. + */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index d697a55..7ff48d4 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __PERIPHERAL_IO_H__ -#define __PERIPHERAL_IO_H__ +#ifndef __TIZEN_SYSTEM_PERIPHERAL_IO_H__ +#define __TIZEN_SYSTEM_PERIPHERAL_IO_H__ #include #include @@ -25,1105 +25,1169 @@ extern "C" { #endif /** + * @addtogroup CAPI_SYSTEM_PERIPHERAL_IO_MODULE + * @{ + */ + +/** * @file peripheral_io.h - * @brief This file contains the peripheral-io API + * @brief This file contains the peripheral-io API. */ /** * @brief Enumeration for peripheral-io error. - * @since_tizen + * @since_tizen 4.0 */ typedef enum { PERIPHERAL_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ PERIPHERAL_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR, /**< I/O error */ PERIPHERAL_ERROR_NO_DEVICE = TIZEN_ERROR_NO_SUCH_DEVICE, /**< No such device */ + PERIPHERAL_ERROR_TRY_AGAIN = TIZEN_ERROR_TRY_AGAIN, /**< Try again */ PERIPHERAL_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ PERIPHERAL_ERROR_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */ PERIPHERAL_ERROR_RESOURCE_BUSY = TIZEN_ERROR_RESOURCE_BUSY, /**< Device or resource busy */ PERIPHERAL_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ - PERIPHERAL_ERROR_NO_DATA = TIZEN_ERROR_NO_DATA, /**< No data available */ - PERIPHERAL_ERROR_INVALID_OPERATION = TIZEN_ERROR_INVALID_OPERATION, /**< Function not implemented */ - PERIPHERAL_ERROR_TIMED_OUT = TIZEN_ERROR_TIMED_OUT, /**< Time out */ PERIPHERAL_ERROR_NOT_SUPPORTED = TIZEN_ERROR_NOT_SUPPORTED, /**< Not supported */ PERIPHERAL_ERROR_UNKNOWN = TIZEN_ERROR_UNKNOWN, /**< Unknown error */ } peripheral_error_e; /** - * @addtogroup CAPI_SYSTEM_PERIPHERAL_GPIO_MODULE +* @} +*/ + +/** + * @addtogroup CAPI_SYSTEM_PERIPHERAL_IO_GPIO_MODULE * @{ */ /** - * @brief Enumeration of gpio direction + * @brief Enumeration of GPIO direction options. + * @since_tizen 4.0 */ typedef enum { - PERIPHERAL_GPIO_DIRECTION_IN = 0, /**< Input Mode */ - PERIPHERAL_GPIO_DIRECTION_OUT, /**< Output mode with low value */ - PERIPHERAL_GPIO_DIRECTION_OUT_LOW = PERIPHERAL_GPIO_DIRECTION_OUT, /**< Same as above */ - PERIPHERAL_GPIO_DIRECTION_OUT_HIGH, /**< Output mode with high value */ + PERIPHERAL_GPIO_DIRECTION_IN = 0, /**< Input Mode */ + PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_HIGH, /**< Output mode with high value */ + PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW, /**< Output mode with low value */ } peripheral_gpio_direction_e; /** - * @brief Enumeration of edge type for gpio interrupt + * @brief Enumeration of edge types for the GPIO interrupt. + * @since_tizen 4.0 */ typedef enum { - PERIPHERAL_GPIO_EDGE_NONE = 0, /**< No interrupt on Gpio */ + PERIPHERAL_GPIO_EDGE_NONE = 0, /**< No interrupt on GPIO */ PERIPHERAL_GPIO_EDGE_RISING, /**< Interrupt on rising only */ PERIPHERAL_GPIO_EDGE_FALLING, /**< Interrupt on falling only */ PERIPHERAL_GPIO_EDGE_BOTH, /**< Interrupt on rising & falling */ } peripheral_gpio_edge_e; /** - * @brief Gpio isr callback data delivered via gpio_isr_cb(). - * @details A gpio isr callback data is delivered as a structure, which contains - * the pin number, the pin value, and the timestamp of the gpio interrupt - * in microseconds. + * @brief The handle of a GPIO pin. * @since_tizen 4.0 */ -typedef struct { - int pin; - int value; - unsigned long long timestamp; -} gpio_isr_cb_s; +typedef struct _peripheral_gpio_s *peripheral_gpio_h; /** - * @brief The handle to the gpio pin - * @since_tizen 4.0 - */ -typedef struct _peripheral_gpio_s* peripheral_gpio_h; - -/** - * @brief Initializes(export) gpio pin and creates gpio handle. + * @platform + * @brief Opens a GPIO pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio + * @remarks @a gpio should be released with peripheral_gpio_close() * - * @param[in] gpio_pin The gpio pin number - * @param[out] gpio The gpio handle is created on success + * @param[in] gpio_pin The GPIO pin number + * @param[out] gpio The GPIO 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_NO_DEVICE Device does not exist or is removed * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied + * @retval #PERIPHERAL_ERROR_RESOURCE_BUSY Device is in use + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * - * @see peripheral_gpio_close() + * @post peripheral_gpio_close() */ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio); /** - * @brief Releases the gpio handle and finalize(unexport) the gpio pin. + * @platform + * @brief Closes a GPIO pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] gpio The handle to the gpio pin to release + * @param[in] gpio The GPIO handle * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * - * @see peripheral_gpio_open() + * @pre peripheral_gpio_open() */ int peripheral_gpio_close(peripheral_gpio_h gpio); /** - * @brief Gets direction of the gpio. + * @platform + * @brief Sets the GPIO direction. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] gpio The handle to the gpio pin - * @param[out] value The direction(value) type of the gpio + * @param[in] gpio The GPIO handle + * @param[in] direction The direction of the GPIO pin * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * - * @see peripheral_gpio_set_direction() - */ -int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction); - -/** - * @brief Sets direction of the gpio pin. - * @since_tizen 4.0 - * - * @param[in] gpio The handle to the gpio pin to set - * @param[in] direction Direction(value) type of the gpio pin - * - * @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 - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * @see peripheral_gpio_direction_e */ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction); /** - * @brief Reads value of the gpio. + * @platform + * @brief Sets the GPIO edge mode. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] gpio The handle to the gpio pin - * @param[out] value The value of the gpio (zero or non-zero) + * @param[in] gpio The GPIO handle + * @param[in] edge The edge mode of the GPIO pin * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * - * @see peripheral_gpio_write() + * @see peripheral_gpio_edge_e */ -int peripheral_gpio_read(peripheral_gpio_h gpio, int *value); +int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge); /** - * @brief Writes value to the gpio. + * @platform + * @brief The GPIO interrupted callback called when the GPIO interrupt is triggered. * @since_tizen 4.0 * - * @param[in] gpio The handle to the gpio pin - * @param[in] value Value to be written to the gpio (muse be zero or non-zero) - * - * @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 - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * @param[in] gpio The GPIO handle + * @param[in] error The GPIO error + * @param[in] user_data The user data passed from the callback registration function * - * @see peripheral_gpio_read() + * @see peripheral_gpio_set_interrupted_cb() */ -int peripheral_gpio_write(peripheral_gpio_h gpio, int value); +typedef void(*peripheral_gpio_interrupted_cb)(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data); /** - * @brief Gets the edge mode of the gpio. + * @platform + * @brief Sets the GPIO interrupted callback to be invoked when the GPIO interrupt is triggered. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] gpio The handle to the gpio pin - * @param[out] gpio_pin The edge mode of the gpio + * @param[in] gpio The GPIO handle + * @param[in] callback The GPIO interrupted callback function to set + * @param[in] user_data The user data to be passed to the callback function * * @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_PERMISSION_DENIED Permission denied * @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 + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * + * @post peripheral_gpio_unset_interrupted_cb() * @see peripheral_gpio_set_edge_mode() */ -int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge); +int peripheral_gpio_set_interrupted_cb(peripheral_gpio_h gpio, peripheral_gpio_interrupted_cb callback, void *user_data); /** - * @brief Sets the edge mode of the gpio pin. + * @platform + * @brief Unsets the GPIO interrupted callback. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] gpio The handle to the gpio pin to set - * @param[in] edge The edge mode of the gpio pin + * @param[in] gpio The GPIO handle * * @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_PERMISSION_DENIED Permission denied * @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_gpio_get_edge_mode() - */ -int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge); - -/** - * @brief Called when the gpio interrupt is triggered. - * @since_tizen 4.0 + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * - * @param[in] data The gpio isr callback data - * @param[in] user_data The user data passed from the callback registration function - * - * @see peripheral_gpio_register_cb() - * @see peripheral_gpio_unregister_cb() + * @pre peripheral_gpio_set_interrupted_cb() */ -typedef void(*gpio_isr_cb)(gpio_isr_cb_s *data, void *user_data); +int peripheral_gpio_unset_interrupted_cb(peripheral_gpio_h gpio); /** - * @brief Registers a callback function to be invoked when the gpio interrupt is triggered. + * @platform + * @brief Gets the current value of the GPIO pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] gpio The handle to the gpio pin to set - * @param[in] edge The edge type of the gpio pin - * @param[in] callback The callback function to register - * @param[in] user_data The user data to be passed to the callback function + * @param[in] gpio The GPIO handle + * @param[out] value The value to get * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error * - * @see peripheral_gpio_set_edge_mode() - * @see peripheral_gpio_unregister_cb() + * @see peripheral_gpio_write() */ -int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data); +int peripheral_gpio_read(peripheral_gpio_h gpio, uint32_t *value); /** - * @brief Unregisters the callback function for the gpio handler. + * @platform + * @brief Sets the value of the GPIO pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] gpio The handle to the gpio pin + * @param[in] gpio The GPIO handle + * @param[in] value The value to set (must be 0 or 1) * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error * - * @see peripheral_gpio_register_cb() - */ -int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio); - -/** - * @brief Gets pin number of the gpio handle. - * @since_tizen 4.0 - * - * @param[in] gpio The handle to the gpio pin - * @param[out] gpio_pin The pin number of the gpio - * - * @return 0 on success, otherwise a negative error value - * @retval #PERIPHERAL_ERROR_NONE Successful - * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @see peripheral_gpio_read() */ -int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin); +int peripheral_gpio_write(peripheral_gpio_h gpio, uint32_t value); /** * @} */ /** - * @addtogroup CAPI_SYSTEM_PERIPHERAL_I2C_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_IO_I2C_MODULE * @{ */ /** - * @brief The handle to the i2c device + * @brief The handle of the I2C slave device. * @since_tizen 4.0 */ typedef struct _peripheral_i2c_s *peripheral_i2c_h; /** - * @brief Initializes i2c communication and creates i2c handle. + * @platform + * @brief Opens an I2C slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio + * @remarks @a i2c should be released with peripheral_i2c_close() * - * @param[in] bus The i2c bus number that the slave device is connected + * @param[in] bus The I2C bus number that the slave device is connected * @param[in] address The address of the slave device - * @param[out] i2c The i2c handle is created on success + * @param[out] i2c The I2C 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_NO_DEVICE Device does not exist or is removed * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied + * @retval #PERIPHERAL_ERROR_RESOURCE_BUSY Device is in use * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * - * @see peripheral_i2c_close() + * @post peripheral_i2c_close() */ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c); /** - * @brief Destory the i2c handle and release the communication. + * @platform + * @brief Closes an I2C slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] i2c The i2c handle + * @param[in] i2c The I2C handle * * @return 0 on success, otherwise a negative error value * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error * - * @see peripheral_i2c_open() + * @pre peripheral_i2c_open() */ int peripheral_i2c_close(peripheral_i2c_h i2c); /** - * @brief Reads data from the i2c slave device. + * @platform + * @brief Reads the bytes data from the I2C slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] i2c The handle to the i2c device - * @param[out] data The address of data buffer to read + * @param[in] i2c The I2C handle + * @param[out] data The data buffer to read * @param[in] length The size of data buffer (in bytes) * * @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_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_i2c_write() */ -int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); +int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, uint32_t length); /** - * @brief Write data to the i2c slave device. + * @platform + * @brief Writes the bytes data to the I2C slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] i2c The handle to the i2c device - * @param[in] data The address of data buffer to write + * @param[in] i2c The I2C handle + * @param[in] data The data buffer to write * @param[in] length The size of data buffer (in bytes) * * @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_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - */ -int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); - -/** - * @brief Reads single byte data from the i2c slave 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 + * @see peripheral_i2c_read() */ -int peripheral_i2c_read_byte(peripheral_i2c_h i2c, uint8_t *data); +int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, uint32_t length); /** - * @brief Write single byte data to the i2c slave device. + * @platform + * @brief Reads single byte data from the register of the I2C slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] i2c The handle to the i2c device - * @param[in] data The byte value to write + * @param[in] i2c The I2C handle + * @param[in] reg The register address of the I2C slave device to read + * @param[out] data The single byte data 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_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - */ -int peripheral_i2c_write_byte(peripheral_i2c_h i2c, uint8_t data); - -/** - * @brief Reads byte data from the register of i2c slave device. - * @since_tizen 4.0 * - * @param[in] i2c The handle to the i2c device - * @param[in] address The register address of the i2c slave device to read - * @param[out] data The byte output of slave device(register value) - * - * @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 + * @see peripheral_i2c_write_register_byte() */ -int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t address, uint8_t *data); +int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data); /** - * @brief Write byte data to the register of i2c slave device. + * @platform + * @brief Writes single byte data to the register of the I2C slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] i2c The handle to the i2c device - * @param[in] address The register address of the i2c slave device to write - * @param[in] data The byte value to write + * @param[in] i2c The I2C handle + * @param[in] reg The register address of the I2C slave device to write + * @param[in] data The single byte data 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_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_i2c_read_register_byte() */ -int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t address, uint8_t data); +int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data); /** - * @brief Reads word data from the register of i2c slave device. + * @platform + * @brief Reads word data from the register of the I2C slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] i2c The handle to the i2c device - * @param[in] address The register address of the i2c slave device to read - * @param[out] data The word output(2 bytes) of slave device(register value) + * @param[in] i2c The I2C handle + * @param[in] reg The register address of the I2C slave device to read + * @param[out] data The word(2 bytes) data 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_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_i2c_write_register_word() */ -int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t address, uint16_t *data); +int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data); /** - * @brief Write byte data to the register of i2c slave device. + * @platform + * @brief Writes word data to the register of the I2C slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] i2c The handle to the i2c device - * @param[in] address The register address of the i2c slave device to write - * @param[in] data The word(2 bytes) value to write + * @param[in] i2c The I2C handle + * @param[in] reg The register address of the I2C slave device to write + * @param[in] data The word(2 bytes) data 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_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_i2c_read_register_word() */ -int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t address, uint16_t data); +int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data); /** * @} */ /** - * @addtogroup CAPI_SYSTEM_PERIPHERAL_PWM_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_IO_PWM_MODULE * @{ */ /** - * @brief The handle to the pwm device + * @brief The handle of the PWM device. * @since_tizen 4.0 */ typedef struct _peripheral_pwm_s *peripheral_pwm_h; /** * @brief Enumeration for Polarity. + * @since_tizen 4.0 */ typedef enum { - PERIPHERAL_PWM_POLARITY_NORMAL = 0, - PERIPHERAL_PWM_POLARITY_INVERSED, + PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH = 0, /**< PWM signal start in the active high state (Normal) */ + PERIPHERAL_PWM_POLARITY_ACTIVE_LOW, /**< PWM signal start in the active low state (Inversed) */ } peripheral_pwm_polarity_e; /** - * @brief Initializes(export) pwm device and creates pwm handle. + * @platform + * @brief Opens the PWM pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio + * @remarks @a pwm should be released with peripheral_pwm_close() * - * @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 + * @param[in] chip The PWM chip number + * @param[in] pin The PWM pin(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_NO_DEVICE Device does not exist or is removed * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_RESOURCE_BUSY Device is in use * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * - * @see peripheral_pwm_close() + * @post peripheral_pwm_close() */ -int peripheral_pwm_open(int device, int channel, peripheral_pwm_h *pwm); +int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm); /** - * @brief Destory the pwm handle and finalize(unexport) the pwm device. + * @platform + * @brief Closes the PWM pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] pwm The handle to the pwm device + * @param[in] pwm The PWM handle * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * - * @see peripheral_pwm_open() + * @pre 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. + * @platform + * @brief Sets period of the PWM pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] pwm The handle to the pwm device - * @param[out] period The total period of the PWM signal (in nanoseconds) + * @param[in] pwm The PWM handle + * @param[in] period_ns The total period of the PWM pin (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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @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); +int peripheral_pwm_set_period(peripheral_pwm_h pwm, uint32_t period_ns); /** - * @brief Sets Duty Cycle of the pwm device. + * @platform + * @brief Sets duty cycle of the PWM pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] pwm The handle to the pwm device - * @param[in] duty_cycle The active time of the pwm signal (in nanoseconds) + * @param[in] pwm The PWM handle + * @param[in] duty_cycle_ns The duty cycle of the PWM pin (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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @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); +int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, uint32_t duty_cycle_ns); /** - * @brief Gets Duty Cycle of the pwm device. + * @platform + * @brief Sets polarity of the PWM pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] pwm The handle to the pwm device - * @param[out] duty_cycle The active time of the pwm signal (in nanoseconds) + * @param[in] pwm The PWM handle + * @param[in] polarity The polarity of the PWM pin * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @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 + * @see peripheral_pwm_polarity_e */ int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity); /** - * @brief Gets Polarity of the pwm device. + * @platform + * @brief Enables the PWM pin. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @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 + * @param[in] pwm The PWM handle + * @param[in] enabled Enable/disable the PWM pin * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #PERIPHERAL_ERROR_INVALID_OPERATION Invalid access + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @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); +int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, bool enabled); /** * @} */ /** - * @addtogroup CAPI_SYSTEM_PERIPHERAL_ADC_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_IO_UART_MODULE * @{ */ /** - * @brief Reads data from the adc device. + * @brief The handle to the UART device. * @since_tizen 4.0 - * - * @param[in] device The device number of the adc device - * @param[in] channel The channel number to read - * @param[out] data The address of 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 - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed */ -int peripheral_adc_read(unsigned int device, unsigned int channel, int *data); - -/** -* @} -*/ +typedef struct _peripheral_uart_s *peripheral_uart_h; /** - * @addtogroup CAPI_SYSTEM_PERIPHERAL_UART_MODULE - * @{ + * @brief Enumeration for baud rate. + * @since_tizen 4.0 */ - -/** - * @brief The handle to the uart device +typedef enum { + PERIPHERAL_UART_BAUD_RATE_0 = 0, /**< The number of signal in one second is 0 */ + PERIPHERAL_UART_BAUD_RATE_50, /**< The number of signal in one second is 50 */ + PERIPHERAL_UART_BAUD_RATE_75, /**< The number of signal in one second is 75 */ + PERIPHERAL_UART_BAUD_RATE_110, /**< The number of signal in one second is 110 */ + PERIPHERAL_UART_BAUD_RATE_134, /**< The number of signal in one second is 134 */ + PERIPHERAL_UART_BAUD_RATE_150, /**< The number of signal in one second is 150 */ + PERIPHERAL_UART_BAUD_RATE_200, /**< The number of signal in one second is 200 */ + PERIPHERAL_UART_BAUD_RATE_300, /**< The number of signal in one second is 300 */ + PERIPHERAL_UART_BAUD_RATE_600, /**< The number of signal in one second is 600 */ + PERIPHERAL_UART_BAUD_RATE_1200, /**< The number of signal in one second is 1200 */ + PERIPHERAL_UART_BAUD_RATE_1800, /**< The number of signal in one second is 1800 */ + PERIPHERAL_UART_BAUD_RATE_2400, /**< The number of signal in one second is 2400 */ + PERIPHERAL_UART_BAUD_RATE_4800, /**< The number of signal in one second is 4800 */ + PERIPHERAL_UART_BAUD_RATE_9600, /**< The number of signal in one second is 9600 */ + PERIPHERAL_UART_BAUD_RATE_19200, /**< The number of signal in one second is 19200 */ + PERIPHERAL_UART_BAUD_RATE_38400, /**< The number of signal in one second is 38400 */ + PERIPHERAL_UART_BAUD_RATE_57600, /**< The number of signal in one second is 57600 */ + PERIPHERAL_UART_BAUD_RATE_115200, /**< The number of signal in one second is 115200 */ + PERIPHERAL_UART_BAUD_RATE_230400, /**< The number of signal in one second is 230400 */ +} peripheral_uart_baud_rate_e; + +/** + * @brief Enumeration for byte size. * @since_tizen 4.0 */ -typedef struct _peripheral_uart_s *peripheral_uart_h; +typedef enum { + PERIPHERAL_UART_BYTE_SIZE_5BIT = 0, /**< 5 data bits */ + PERIPHERAL_UART_BYTE_SIZE_6BIT, /**< 6 data bits */ + PERIPHERAL_UART_BYTE_SIZE_7BIT, /**< 7 data bits */ + PERIPHERAL_UART_BYTE_SIZE_8BIT, /**< 8 data bits */ +} peripheral_uart_byte_size_e; /** - * @brief Enumeration for Baud Rate. + * @brief Enumeration for parity bit. + * @since_tizen 4.0 */ typedef enum { - PERIPHERAL_UART_BAUDRATE_0 = 0, - PERIPHERAL_UART_BAUDRATE_50, - PERIPHERAL_UART_BAUDRATE_75, - PERIPHERAL_UART_BAUDRATE_110, - PERIPHERAL_UART_BAUDRATE_134, - PERIPHERAL_UART_BAUDRATE_150, - PERIPHERAL_UART_BAUDRATE_200, - PERIPHERAL_UART_BAUDRATE_300, - PERIPHERAL_UART_BAUDRATE_600, - PERIPHERAL_UART_BAUDRATE_1200, - PERIPHERAL_UART_BAUDRATE_1800, - PERIPHERAL_UART_BAUDRATE_2400, - PERIPHERAL_UART_BAUDRATE_4800, - PERIPHERAL_UART_BAUDRATE_9600, - PERIPHERAL_UART_BAUDRATE_19200, - PERIPHERAL_UART_BAUDRATE_38400, - PERIPHERAL_UART_BAUDRATE_57600, - PERIPHERAL_UART_BAUDRATE_115200, - PERIPHERAL_UART_BAUDRATE_230400 -} peripheral_uart_baudrate_e; - -/** - * @brief Enumeration for Byte Size. + PERIPHERAL_UART_PARITY_NONE = 0, /**< No parity is used */ + PERIPHERAL_UART_PARITY_EVEN, /**< Even parity is used */ + PERIPHERAL_UART_PARITY_ODD, /**< ODD parity is used */ +} peripheral_uart_parity_e; + +/** + * @brief Enumeration for stop bits. + * @since_tizen 4.0 */ typedef enum { - PERIPHERAL_UART_BYTESIZE_5BIT = 0, - PERIPHERAL_UART_BYTESIZE_6BIT, - PERIPHERAL_UART_BYTESIZE_7BIT, - PERIPHERAL_UART_BYTESIZE_8BIT -} peripheral_uart_bytesize_e; + PERIPHERAL_UART_STOP_BITS_1BIT = 0, /**< One stop bit */ + PERIPHERAL_UART_STOP_BITS_2BIT, /**< Two stop bits */ +} peripheral_uart_stop_bits_e; /** - * @brief Enumeration for Parity Bit. + * @brief Enumeration for hardware flow control. + * @since_tizen 4.0 */ typedef enum { - PERIPHERAL_UART_PARITY_NONE = 0, - PERIPHERAL_UART_PARITY_EVEN, - PERIPHERAL_UART_PARITY_ODD -} peripheral_uart_parity_e; + PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_NONE = 0, /**< No hardware flow control */ + PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS, /**< Automatic RTS/CTS hardware flow control*/ +} peripheral_uart_hardware_flow_control_e; /** - * @brief Enumeration for Stop Bits. + * @brief Enumeration for software flow control. + * @since_tizen 4.0 */ typedef enum { - PERIPHERAL_UART_STOPBITS_1BIT = 0, - PERIPHERAL_UART_STOPBITS_2BIT -} peripheral_uart_stopbits_e; + PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_NONE = 0, /**< No software flow control */ + PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_XONXOFF, /**< XON/XOFF software flow control */ +} peripheral_uart_software_flow_control_e; /** - * @brief Initializes uart communication and creates uart handle. + * @platform + * @brief Opens the UART slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio + * @remarks @a uart should be released with peripheral_uart_close() * - * @param[in] port The uart port number that the slave device is connected - * @param[out] uart The uart handle is created on success + * @param[in] port The UART port number that the slave device is connected + * @param[out] uart The UART 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_NO_DEVICE Device does not exist or is removed * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied + * @retval #PERIPHERAL_ERROR_RESOURCE_BUSY Device is in use * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * - * @see peripheral_uart_close() + * @post peripheral_uart_close() */ int peripheral_uart_open(int port, peripheral_uart_h *uart); /** - * @brief Destory the uart handle and release the communication. + * @platform + * @brief Closes the UART slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] uart The handle to the uart device + * @param[in] uart The UART handle * * @return 0 on success, otherwise a negative error value * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error * - * @see peripheral_uart_open() + * @pre peripheral_uart_open() */ int peripheral_uart_close(peripheral_uart_h uart); /** - * @brief Flush all input that has received but not yet been read by the uart - * device, or all output written but not transmitted to the uart device. + * @platform + * @brief Sets baud rate of the UART slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] uart The uart handle + * @param[in] uart The UART handle + * @param[in] baud Baud rate of the UART 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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_uart_baud_rate_e */ -int peripheral_uart_flush(peripheral_uart_h uart); +int peripheral_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud); /** - * @brief Sets baudrate of the uart device. + * @platform + * @brief Sets byte size of the UART slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] uart The handle to the uart device to set - * @param[in] baud Baudrate of the uart device + * @param[in] uart The UART handle + * @param[in] byte_size Byte size of the UART 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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_uart_byte_size_e */ -int peripheral_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud); +int peripheral_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size); /** - * @brief Sets mode of the uart device. + * @platform + * @brief Sets parity bit of the UART slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] uart The handle to the uart device to set - * @param[in] bytesize Byte size of the uart device - * @param[in] parity Parity bits of the uart device - * @param[in] stopbits Stop bits of the uart device + * @param[in] uart The UART handle + * @param[in] parity Parity bit of the UART 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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_uart_parity_e */ -int peripheral_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits); +int peripheral_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity); /** - * @brief Sets flow control of the uart device. + * @platform + * @brief Sets stop bits of the UART slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] uart The handle to the uart device to set - * @param[in] xonxoff Turns a transmitter on or off - * @param[in] rtscts Turns "Request to Send/Clear to Send" on or off + * @param[in] uart The UART handle + * @param[in] stop_bits Stop bits of the UART 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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_uart_stop_bits_e */ -int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts); +int peripheral_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits); /** - * @brief Reads data from the uart device. + * @platform + * @brief Sets flow control of the UART slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] uart The handle to the uart device - * @param[out] data The address of read buffer - * @param[out] length The size of data buffer (in bytes) + * @param[in] uart The UART handle + * @param[in] sw_flow_control Software flow control (Turns a transmitter on or off) + * @param[in] hw_flow_control Hardware flow control (Turns "Request to Send/Clear to Send" on or off) + * + * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_uart_software_flow_control_e + * @see peripheral_uart_hardware_flow_control_e + */ +int peripheral_uart_set_flow_control(peripheral_uart_h uart, + peripheral_uart_software_flow_control_e sw_flow_control, + peripheral_uart_hardware_flow_control_e hw_flow_control); + +/** + * @platform + * @brief Reads data from the UART slave device. + * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio + * + * @param[in] uart The UART handle + * @param[out] data The buffer to read + * @param[out] length The size of buffer (in bytes) * * @return the number of bytes read on success, otherwise a negative error value * @retval #PERIPHERAL_ERROR_NONE Successful * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_TRY_AGAIN Try again + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_uart_write() */ -int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, int length); +int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, uint32_t length); /** - * @brief Write data to the uart device. + * @platform + * @brief Writes data to the UART slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] uart The handle to the uart device - * @param[in] data The address of buffer to write - * @param[in] length The size of data (in bytes) + * @param[in] uart The UART handle + * @param[in] data The buffer to write + * @param[in] length The size of buffer (in bytes) * * @return the number of bytes write on success, otherwise a negative error value * @retval #PERIPHERAL_ERROR_NONE Successful * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed + * @retval #PERIPHERAL_ERROR_TRY_AGAIN Try again + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_uart_read() */ -int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, int length); +int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, uint32_t length); /** * @} */ /** - * @addtogroup CAPI_SYSTEM_PERIPHERAL_SPI_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_IO_SPI_MODULE * @{ */ /** - * @brief The handle to the spi device + * @brief The handle of a SPI device. * @since_tizen 4.0 */ -typedef struct _peripheral_spi_s* peripheral_spi_h; +typedef struct _peripheral_spi_s *peripheral_spi_h; /** - * @brief Enumeration for SPI mode. + * @brief Enumeration of SPI transfer modes. + * @since_tizen 4.0 */ typedef enum { - PERIPHERAL_SPI_MODE_0 = 0, - PERIPHERAL_SPI_MODE_1, - PERIPHERAL_SPI_MODE_2, - PERIPHERAL_SPI_MODE_3 + PERIPHERAL_SPI_MODE_0 = 0, /**< CPOL = 0, CPHa = 0 Mode */ + PERIPHERAL_SPI_MODE_1, /**< CPOL = 0, CPHa = 1 Mode */ + PERIPHERAL_SPI_MODE_2, /**< CPOL = 1, CPHa = 0 Mode */ + PERIPHERAL_SPI_MODE_3, /**< CPOL = 1, CPHa = 1 Mode */ } peripheral_spi_mode_e; /** - * @brief Initializes spi communication and creates spi handle. + * @brief Enumeration of bit orders. * @since_tizen 4.0 - * - * @param[in] bus The spi bus number that the slave device is connected - * @param[in] cs The spi chip select number that the slave device is connected - * @param[out] spi The spi handle is created on success - * - * @return 0 on success, otherwise a negative error value - * @retval #PERIPHERAL_ERROR_NONE Successful - * - * @see peripheral_spi_close() */ -int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi); +typedef enum { + PERIPHERAL_SPI_BIT_ORDER_MSB = 0, /**< Use most siginificant bit first */ + PERIPHERAL_SPI_BIT_ORDER_LSB, /**< Use least significant bit first */ +} peripheral_spi_bit_order_e; /** - * @brief Destory the spi handle and release the communication. + * @platform + * @brief Opens a SPI slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio + * @remarks @a spi should be released with peripheral_spi_close() * - * @param[in] spi The handle to the spi device + * @param[in] bus The SPI bus number + * @param[in] cs The SPI chip select number + * @param[out] spi The SPI device handle * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied + * @retval #PERIPHERAL_ERROR_RESOURCE_BUSY Device is in use * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error * - * @see peripheral_spi_open() + * @post peripheral_spi_close() */ -int peripheral_spi_close(peripheral_spi_h spi); +int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi); /** - * @brief Sets mode of the spi device. + * @platform + * @brief Closes the SPI slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] spi The handle to the spi device - * @param[in] mode The mode of the spi device + * @param[in] spi The SPI device handle * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed - */ -int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode); - -/** - * @brief Gets mode of the spi device. - * @since_tizen 4.0 - * - * @param[in] spi The handle to the spi device - * @param[out] mode The mode of the spi 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_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * @see peripheral_spi_open() */ -int peripheral_spi_get_mode(peripheral_spi_h spi, peripheral_spi_mode_e *mode); +int peripheral_spi_close(peripheral_spi_h spi); /** - * @brief Sets bits justification of the spi device. + * @platform + * @brief Sets the SPI transfer mode. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] spi The handle to the spi device - * @param[in] lsb The bit position to be transmitted first - * true - LSB first - * false - MSB first + * @param[in] spi The SPI device handle + * @param[in] mode The SPI transfer mode * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_spi_mode_e */ -int peripheral_spi_set_lsb_first(peripheral_spi_h spi, bool lsb); +int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode); /** - * @brief Gets bits justification of the spi device. + * @platform + * @brief Sets the SPI bit order. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] spi The handle to the spi device - * @param[out] lsb The bit position to be transmitted first + * @param[in] spi The SPI device handle + * @param[in] bit_order The transfer bit order * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_spi_bit_order_e */ -int peripheral_spi_get_lsb_first(peripheral_spi_h spi, bool *lsb); +int peripheral_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order); /** - * @brief Sets the number of bits per word of the spi device + * @platform + * @brief Sets the number of bits per word. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] spi The handle to the spi device + * @param[in] spi The SPI device handle * @param[in] bits The number of bits per word (in bits) * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed */ -int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, unsigned char bits); +int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits); /** - * @brief Gets the number of bits per word of the spi device + * @platform + * @brief Sets the frequency of the SPI bus. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio + * @remarks The frequencies supported are board dependent. * - * @param[in] spi The handle to the spi device - * @param[out] bits The number of bits per word (in bits) + * @param[in] spi The SPI device handle + * @param[in] freq_hz Frequency to set (in Hz) * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed */ -int peripheral_spi_get_bits_per_word(peripheral_spi_h spi, unsigned char *bits); +int peripheral_spi_set_frequency(peripheral_spi_h spi, uint32_t freq_hz); /** - * @brief Sets default max speed of the spi device. + * @platform + * @brief Reads the bytes data from the SPI slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] spi The handle to the spi device - * @param[in] freq Max speed (in hz) + * @param[in] spi The SPI device handle + * @param[out] data The data buffer to read + * @param[in] length The size of data buffer (in bytes) * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed - */ -int peripheral_spi_set_frequency(peripheral_spi_h spi, unsigned int freq); - -/** - * @brief Gets default max speed of the spi device. - * @since_tizen 4.0 - * - * @param[in] spi The handle to the spi device - * @param[out] freq Max speed (in hz) * - * @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 - * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * @see peripheral_spi_write() */ - -int peripheral_spi_get_frequency(peripheral_spi_h spi, unsigned int *freq); +int peripheral_spi_read(peripheral_spi_h spi, uint8_t *data, uint32_t length); /** - * @brief Reads data from the slave device. + * @platform + * @brief Writes the bytes data to the SPI slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] spi The handle to the spi device - * @param[out] data The address of buffer to read + * @param[in] spi The SPI device handle + * @param[in] data The data buffer to write * @param[in] length The size of data buffer (in bytes) * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error - */ -int peripheral_spi_read(peripheral_spi_h spi, unsigned char *data, int length); - -/** - * @brief Write data to the slave device. - * @since_tizen 4.0 * - * @param[in] spi The handle to the spi device - * @param[in] data The address of buffer to write - * @param[in] length The size of data (in bytes) - * - * @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 + * @see peripheral_spi_read() */ -int peripheral_spi_write(peripheral_spi_h spi, unsigned char *data, int length); +int peripheral_spi_write(peripheral_spi_h spi, uint8_t *data, uint32_t length); /** - * @brief Exchange data with the slave device. + * @platform + * @brief Exchanges the bytes data to the SPI slave device. * @since_tizen 4.0 + * @privlevel platform + * @privilege http://tizen.org/privilege/peripheralio * - * @param[in] spi The handle to the spi device - * @param[in] txdata The address of buffer to write - * @param[out] rxdata The address of buffer to read - * @param[in] length The size of data (in bytes) + * @param[in] spi The SPI device handle + * @param[in] txdata The data buffer to write + * @param[out] rxdata The data buffer to read + * @param[in] length The size of txdata and rxdata buffer (in bytes) * * @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_NO_DEVICE Device does not exist or is removed + * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_spi_read() + * @see peripheral_spi_write() */ -int peripheral_spi_read_write(peripheral_spi_h spi, unsigned char *txdata, unsigned char *rxdata, int length); +int peripheral_spi_transfer(peripheral_spi_h spi, uint8_t *txdata, uint8_t *rxdata, uint32_t length); /** * @} @@ -1133,4 +1197,4 @@ int peripheral_spi_read_write(peripheral_spi_h spi, unsigned char *txdata, unsig } #endif -#endif /* __PERIPHERAL_IO_H__ */ +#endif /* __TIZEN_SYSTEM_PERIPHERAL_IO_H__ */ -- 2.7.4