From: Adrian Szyndela Date: Fri, 11 Jun 2021 11:09:26 +0000 (+0200) Subject: i2c: flatten structure - remove 'interface' X-Git-Tag: submit/tizen/20210629.011533~19 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d86b14b3419a1701895295a9fae7e84d63aa1978;p=platform%2Fcore%2Fapi%2Fperipheral-io.git i2c: flatten structure - remove 'interface' Merge src/peripheral_i2c.c with src/interface/peripheral_interface_i2c.c, with additions from include/interface/peripheral_interface_i2c.h. Change-Id: I4c0646d7b78b6753c929cb66fe2a696b4eab7ba9 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 8fea6c2..e3c144c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,6 @@ SET(SOURCES src/peripheral_gpio.c src/peripheral_uart.c src/peripheral_spi.c src/interface/peripheral_interface_gpio.c - src/interface/peripheral_interface_i2c.c src/interface/peripheral_interface_pwm.c src/interface/peripheral_interface_adc.c src/interface/peripheral_interface_spi.c diff --git a/include/interface/peripheral_interface_i2c.h b/include/interface/peripheral_interface_i2c.h deleted file mode 100644 index b9a47c6..0000000 --- a/include/interface/peripheral_interface_i2c.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2016-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_INTERFACE_I2C_H__ -#define __PERIPHERAL_INTERFACE_I2C_H__ - -#include "peripheral_interface_common.h" - -#define I2C_BUFFER_MAX 64 - -#define I2C_SLAVE 0x0703 /* Use this slave address */ -#define I2C_SMBUS 0x0720 /* SMBus transfer */ - -/* i2c_smbus_xfer read or write markers */ -#define I2C_SMBUS_READ 1 -#define I2C_SMBUS_WRITE 0 - -/* SMBus transaction types */ -#define I2C_SMBUS_QUICK 0 -#define I2C_SMBUS_BYTE 1 -#define I2C_SMBUS_BYTE_DATA 2 -#define I2C_SMBUS_WORD_DATA 3 - -/* - * Data for SMBus Messages - */ -#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */ - -union i2c_smbus_data { - uint8_t byte; - uint16_t word; - uint8_t block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */ - /* and one more for user-space compatibility */ -}; - -/* This is the structure as used in the I2C_SMBUS ioctl call */ -struct i2c_smbus_ioctl_data { - uint8_t read_write; - uint8_t command; - uint32_t size; - union i2c_smbus_data *data; -}; - -void peripheral_interface_i2c_close(peripheral_i2c_h i2c); -int peripheral_interface_i2c_read(peripheral_i2c_h i2c, uint8_t *data, uint32_t length); -int peripheral_interface_i2c_write(peripheral_i2c_h i2c, uint8_t *data, uint32_t length); -int peripheral_interface_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out); -int peripheral_interface_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in); -int peripheral_interface_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out); -int peripheral_interface_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in); - -#endif /* __PERIPHERAL_INTERFACE_I2C_H__ */ diff --git a/src/interface/peripheral_interface_i2c.c b/src/interface/peripheral_interface_i2c.c deleted file mode 100644 index 7c793e3..0000000 --- a/src/interface/peripheral_interface_i2c.c +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright (c) 2016-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 "peripheral_interface_i2c.h" - -void peripheral_interface_i2c_close(peripheral_i2c_h i2c) -{ - close(i2c->fd); -} - -/* It was developed temporarily because of the I2C Stub. */ -static int peripheral_interface_i2c_read_buffer(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length) -{ - int ret; - - struct i2c_smbus_ioctl_data data_arg; - union i2c_smbus_data data; - - memset(&data, 0x0, sizeof(data.block)); - - data_arg.read_write = I2C_SMBUS_READ; - data_arg.size = I2C_SMBUS_BYTE; - data_arg.data = &data; - data_arg.command = *data_out; - - ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); - CHECK_ERROR(ret != 0); - - *data_out = data.byte; - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_i2c_read(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length) -{ - int ret = read(i2c->fd, data_out, length); - if (ret != length) - return peripheral_interface_i2c_read_buffer(i2c, data_out, length); - - return PERIPHERAL_ERROR_NONE; -} - -/* It was developed temporarily because of the I2C Stub. */ -static int peripheral_interface_i2c_write_buffer(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length) -{ - int ret; - - struct i2c_smbus_ioctl_data data_arg; - union i2c_smbus_data data; - - memset(&data, 0x0, sizeof(data.block)); - - data_arg.read_write = I2C_SMBUS_WRITE; - data_arg.size = I2C_SMBUS_BYTE; - data_arg.data = &data; - data_arg.command = *data_in; - - ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_i2c_write(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length) -{ - int ret = write(i2c->fd, data_in, length); - if (ret != length) - return peripheral_interface_i2c_write_buffer(i2c, data_in, length); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out) -{ - int ret; - - struct i2c_smbus_ioctl_data data_arg; - union i2c_smbus_data data; - - memset(&data, 0x0, sizeof(data.block)); - - data_arg.read_write = I2C_SMBUS_READ; - data_arg.size = I2C_SMBUS_BYTE_DATA; - data_arg.data = &data; - data_arg.command = reg; - - ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); - CHECK_ERROR(ret != 0); - - *data_out = data.byte; - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in) -{ - int ret; - - struct i2c_smbus_ioctl_data data_arg; - union i2c_smbus_data data; - - memset(&data, 0x0, sizeof(data.block)); - - data_arg.read_write = I2C_SMBUS_WRITE; - data_arg.size = I2C_SMBUS_BYTE_DATA; - data_arg.data = &data; - data_arg.command = reg; - - data.byte = data_in; - - ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out) -{ - int ret; - - struct i2c_smbus_ioctl_data data_arg; - union i2c_smbus_data data; - - memset(&data, 0x0, sizeof(data.block)); - - data_arg.read_write = I2C_SMBUS_READ; - data_arg.size = I2C_SMBUS_WORD_DATA; - data_arg.data = &data; - data_arg.command = reg; - - ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); - CHECK_ERROR(ret != 0); - - *data_out = data.word; - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in) -{ - int ret; - - struct i2c_smbus_ioctl_data data_arg; - union i2c_smbus_data data; - - memset(&data, 0x0, sizeof(data.block)); - - data_arg.read_write = I2C_SMBUS_WRITE; - data_arg.size = I2C_SMBUS_WORD_DATA; - data_arg.data = &data; - data_arg.command = reg; - - data.word = data_in; - - ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} \ No newline at end of file diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index b97f4c1..4076094 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -20,8 +20,7 @@ #include #include "peripheral_io.h" -#include "peripheral_handle.h" -#include "peripheral_interface_i2c.h" +#include "peripheral_interface_common.h" #include "peripheral_log.h" #define PERIPHERAL_IO_I2C_FEATURE "http://tizen.org/feature/peripheral_io.i2c" @@ -30,6 +29,9 @@ #define I2C_FEATURE_FALSE 0 #define I2C_FEATURE_TRUE 1 +#define I2C_SLAVE 0x0703 /* Use this slave address */ +#define I2C_SMBUS 0x0720 /* SMBus transfer */ + /* i2c_smbus_xfer read or write markers */ #define I2C_SMBUS_READ 1 #define I2C_SMBUS_WRITE 0 @@ -40,6 +42,26 @@ #define I2C_SMBUS_BYTE_DATA 2 #define I2C_SMBUS_WORD_DATA 3 +/* + * Data for SMBus Messages + */ +#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */ + +union i2c_smbus_data { + uint8_t byte; + uint16_t word; + uint8_t block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */ + /* and one more for user-space compatibility */ +}; + +/* This is the structure as used in the I2C_SMBUS ioctl call */ +struct i2c_smbus_ioctl_data { + uint8_t read_write; + uint8_t command; + uint32_t size; + union i2c_smbus_data *data; +}; + static int i2c_feature = I2C_FEATURE_UNKNOWN; static bool __is_feature_supported(void) @@ -120,67 +142,166 @@ 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; - RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); - peripheral_interface_i2c_close(i2c); - - free(i2c); - i2c = NULL; + cleanup_handlep(&i2c); - return ret; + return PERIPHERAL_ERROR_NONE; } -int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, uint32_t length) +int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data_out, uint32_t length) { RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); - RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + + int ret = read(i2c->fd, data_out, length); + if (ret == length) + return PERIPHERAL_ERROR_NONE; + + struct i2c_smbus_ioctl_data data_arg; + union i2c_smbus_data data; + + memset(&data, 0x0, sizeof(data.block)); + + data_arg.read_write = I2C_SMBUS_READ; + data_arg.size = I2C_SMBUS_BYTE; + data_arg.data = &data; + data_arg.command = *data_out; + + ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); + CHECK_ERROR(ret != 0); - return peripheral_interface_i2c_read(i2c, data, length); + *data_out = data.byte; + + return PERIPHERAL_ERROR_NONE; } -int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, uint32_t length) +int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data_in, uint32_t length) { RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); - RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + RETVM_IF(data_in == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + + int ret = write(i2c->fd, data_in, length); + if (ret == length) + return PERIPHERAL_ERROR_NONE; + + struct i2c_smbus_ioctl_data data_arg; + union i2c_smbus_data data; + + memset(&data, 0x0, sizeof(data.block)); + + data_arg.read_write = I2C_SMBUS_WRITE; + data_arg.size = I2C_SMBUS_BYTE; + data_arg.data = &data; + data_arg.command = *data_in; - return peripheral_interface_i2c_write(i2c, data, length); + ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } -int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data) +int peripheral_i2c_read_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t *data_out) { RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); - RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + + int ret; + + struct i2c_smbus_ioctl_data data_arg; + union i2c_smbus_data data; + + memset(&data, 0x0, sizeof(data.block)); + + data_arg.read_write = I2C_SMBUS_READ; + data_arg.size = I2C_SMBUS_BYTE_DATA; + data_arg.data = &data; + data_arg.command = reg; - return peripheral_interface_i2c_read_register_byte(i2c, reg, data); + ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); + CHECK_ERROR(ret != 0); + + *data_out = data.byte; + + return PERIPHERAL_ERROR_NONE; } -int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data) +int peripheral_i2c_write_register_byte(peripheral_i2c_h i2c, uint8_t reg, uint8_t data_in) { RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); - return peripheral_interface_i2c_write_register_byte(i2c, reg, data); + int ret; + + struct i2c_smbus_ioctl_data data_arg; + union i2c_smbus_data data; + + memset(&data, 0x0, sizeof(data.block)); + + data_arg.read_write = I2C_SMBUS_WRITE; + data_arg.size = I2C_SMBUS_BYTE_DATA; + data_arg.data = &data; + data_arg.command = reg; + + data.byte = data_in; + + ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } -int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data) +int peripheral_i2c_read_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t *data_out) { RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); - RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + RETVM_IF(data_out == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + + int ret; + + struct i2c_smbus_ioctl_data data_arg; + union i2c_smbus_data data; - return peripheral_interface_i2c_read_register_word(i2c, reg, data); + memset(&data, 0x0, sizeof(data.block)); + + data_arg.read_write = I2C_SMBUS_READ; + data_arg.size = I2C_SMBUS_WORD_DATA; + data_arg.data = &data; + data_arg.command = reg; + + ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); + CHECK_ERROR(ret != 0); + + *data_out = data.word; + + return PERIPHERAL_ERROR_NONE; } -int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data) +int peripheral_i2c_write_register_word(peripheral_i2c_h i2c, uint8_t reg, uint16_t data_in) { RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "I2C feature is not supported"); RETVM_IF(i2c == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "i2c handle is NULL"); - return peripheral_interface_i2c_write_register_word(i2c, reg, data); + int ret; + + struct i2c_smbus_ioctl_data data_arg; + union i2c_smbus_data data; + + memset(&data, 0x0, sizeof(data.block)); + + data_arg.read_write = I2C_SMBUS_WRITE; + data_arg.size = I2C_SMBUS_WORD_DATA; + data_arg.data = &data; + data_arg.command = reg; + + data.word = data_in; + + ret = ioctl(i2c->fd, I2C_SMBUS, &data_arg); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; }