From dcb9bb6026bc9b600b968307bb228f8766e9591d Mon Sep 17 00:00:00 2001 From: Sheng-Liang Song Date: Thu, 24 Jul 2014 11:39:06 -0700 Subject: EC: clean up i2c_read_string - Removed duplicate (similar) i2c_read_string functions. - Kept one generic (weak) copy in common/i2c.c. - TBD: Need support start/stop flags for STM32 family devices BUG=chrome-os-partner:23569 BRANCH=ToT TEST=Verified with smart battery firmware update application on glimmer. Passed LGC & Simplo Battery. Change-Id: I6d9446c60b6a36aef9a6179242c081084199c8e2 Signed-off-by: Sheng-Liang Song Reviewed-on: https://chromium-review.googlesource.com/209866 Reviewed-by: Randall Spangler --- chip/stm32/build.mk | 2 +- chip/stm32/i2c-stm32f.c | 34 ---------------------------------- chip/stm32/i2c-stm32f0.c | 35 ----------------------------------- chip/stm32/i2c-stm32l.c | 35 ----------------------------------- chip/stm32/i2c.c | 39 +++++++++++++++++++++++++++++++++++++++ driver/battery/smart.c | 2 +- 6 files changed, 41 insertions(+), 106 deletions(-) create mode 100644 chip/stm32/i2c.c diff --git a/chip/stm32/build.mk b/chip/stm32/build.mk index c1ccab2994..d4fe146197 100644 --- a/chip/stm32/build.mk +++ b/chip/stm32/build.mk @@ -29,7 +29,7 @@ chip-$(CONFIG_SPI)+=spi.o chip-$(CONFIG_SPI_MASTER_PORT)+=spi_master.o chip-$(CONFIG_COMMON_GPIO)+=gpio.o gpio-$(CHIP_FAMILY).o chip-$(CONFIG_COMMON_TIMER)+=hwtimer$(TIMER_TYPE).o -chip-$(CONFIG_I2C)+=i2c-$(CHIP_FAMILY).o +chip-$(CONFIG_I2C)+=i2c-$(CHIP_FAMILY).o i2c.o chip-$(CONFIG_WATCHDOG)+=watchdog.o chip-$(HAS_TASK_CONSOLE)+=uart.o chip-$(HAS_TASK_KEYSCAN)+=keyboard_raw.o diff --git a/chip/stm32/i2c-stm32f.c b/chip/stm32/i2c-stm32f.c index 8e1ab99d57..674808277c 100644 --- a/chip/stm32/i2c-stm32f.c +++ b/chip/stm32/i2c-stm32f.c @@ -23,9 +23,6 @@ #define CPRINTF(format, args...) cprintf(CC_I2C, format, ## args) #define CPRINTS(format, args...) cprints(CC_I2C, format, ## args) -/* Maximum transfer of a SMBUS block transfer */ -#define SMBUS_MAX_BLOCK 32 - /* 8-bit I2C slave address */ #define I2C_ADDRESS 0x3c @@ -767,34 +764,3 @@ int i2c_get_line_levels(int port) (i2c_raw_get_scl(port) ? I2C_LINE_SCL_HIGH : 0); } -int i2c_read_string(int port, int slave_addr, int offset, uint8_t *data, - int len) -{ - int rv; - uint8_t reg, block_length; - - /* - * TODO(crosbug.com/p/23569): when i2c_xfer() supports start/stop bits, - * merge this with the LM4 implementation and move to i2c_common.c. - */ - - if ((len <= 0) || (len > SMBUS_MAX_BLOCK)) - return EC_ERROR_INVAL; - - i2c_lock(port, 1); - - /* Read the counted string into the output buffer */ - reg = offset; - rv = i2c_xfer(port, slave_addr, ®, 1, data, len, I2C_XFER_SINGLE); - if (rv == EC_SUCCESS) { - /* Block length is the first byte of the returned buffer */ - block_length = MIN(data[0], len - 1); - - /* Move data down, then null-terminate it */ - memmove(data, data + 1, block_length); - data[block_length] = 0; - } - - i2c_lock(port, 0); - return rv; -} diff --git a/chip/stm32/i2c-stm32f0.c b/chip/stm32/i2c-stm32f0.c index 8db1d97f2d..5dc40fcebc 100644 --- a/chip/stm32/i2c-stm32f0.c +++ b/chip/stm32/i2c-stm32f0.c @@ -20,9 +20,6 @@ #define CPUTS(outstr) cputs(CC_I2C, outstr) #define CPRINTS(format, args...) cprints(CC_I2C, format, ## args) -/* Maximum transfer of a SMBUS block transfer */ -#define SMBUS_MAX_BLOCK 32 - /* Transmit timeout in microseconds */ #define I2C_TX_TIMEOUT_MASTER (10 * MSEC) @@ -410,38 +407,6 @@ int i2c_get_line_levels(int port) (i2c_raw_get_scl(port) ? I2C_LINE_SCL_HIGH : 0); } -int i2c_read_string(int port, int slave_addr, int offset, uint8_t *data, - int len) -{ - int rv; - uint8_t reg, block_length; - - /* - * TODO(crosbug.com/p/23569): when i2c_xfer() supports start/stop bits, - * merge this with the LM4 implementation and move to i2c_common.c. - */ - - if ((len <= 0) || (len > SMBUS_MAX_BLOCK)) - return EC_ERROR_INVAL; - - i2c_lock(port, 1); - - /* Read the counted string into the output buffer */ - reg = offset; - rv = i2c_xfer(port, slave_addr, ®, 1, data, len, I2C_XFER_SINGLE); - if (rv == EC_SUCCESS) { - /* Block length is the first byte of the returned buffer */ - block_length = MIN(data[0], len - 1); - - /* Move data down, then null-terminate it */ - memmove(data, data + 1, block_length); - data[block_length] = 0; - } - - i2c_lock(port, 0); - return rv; -} - static void i2c_init(void) { const struct i2c_port_t *p = i2c_ports; diff --git a/chip/stm32/i2c-stm32l.c b/chip/stm32/i2c-stm32l.c index 34a80c2b3e..e274207cb8 100644 --- a/chip/stm32/i2c-stm32l.c +++ b/chip/stm32/i2c-stm32l.c @@ -22,9 +22,6 @@ #define CPUTS(outstr) cputs(CC_I2C, outstr) #define CPRINTS(format, args...) cprints(CC_I2C, format, ## args) -/* Maximum transfer of a SMBUS block transfer */ -#define SMBUS_MAX_BLOCK 32 - #define I2C_ERROR_FAILED_START EC_ERROR_INTERNAL_FIRST /* @@ -367,38 +364,6 @@ int i2c_get_line_levels(int port) (i2c_raw_get_scl(port) ? I2C_LINE_SCL_HIGH : 0); } -int i2c_read_string(int port, int slave_addr, int offset, uint8_t *data, - int len) -{ - int rv; - uint8_t reg, block_length; - - /* - * TODO(crosbug.com/p/23569): when i2c_xfer() supports start/stop bits, - * merge this with the LM4 implementation and move to i2c_common.c. - */ - - if ((len <= 0) || (len > SMBUS_MAX_BLOCK)) - return EC_ERROR_INVAL; - - i2c_lock(port, 1); - - /* Read the counted string into the output buffer */ - reg = offset; - rv = i2c_xfer(port, slave_addr, ®, 1, data, len, I2C_XFER_SINGLE); - if (rv == EC_SUCCESS) { - /* Block length is the first byte of the returned buffer */ - block_length = MIN(data[0], len - 1); - - /* Move data down, then null-terminate it */ - memmove(data, data + 1, block_length); - data[block_length] = 0; - } - - i2c_lock(port, 0); - return rv; -} - /*****************************************************************************/ /* Hooks */ diff --git a/chip/stm32/i2c.c b/chip/stm32/i2c.c new file mode 100644 index 0000000000..9cea83a619 --- /dev/null +++ b/chip/stm32/i2c.c @@ -0,0 +1,39 @@ +/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* STM32 common I2C code for Chrome EC */ + +#include "i2c.h" +#include "util.h" + +/* Maximum transfer of a SMBUS block transfer */ +#define SMBUS_MAX_BLOCK 32 + +int i2c_read_string(int port, int slave_addr, + int offset, uint8_t *data, int len) +{ + int rv; + uint8_t reg, block_length; + + if ((len <= 0) || (len > SMBUS_MAX_BLOCK)) + return EC_ERROR_INVAL; + + i2c_lock(port, 1); + + /* Read the counted string into the output buffer */ + reg = offset; + rv = i2c_xfer(port, slave_addr, ®, 1, data, len, I2C_XFER_SINGLE); + if (rv == EC_SUCCESS) { + /* Block length is the first byte of the returned buffer */ + block_length = MIN(data[0], len - 1); + + /* Move data down, then null-terminate it */ + memmove(data, data + 1, block_length); + data[block_length] = 0; + } + + i2c_lock(port, 0); + return rv; +} diff --git a/driver/battery/smart.c b/driver/battery/smart.c index 6dcedfd81f..0df0fefbe8 100644 --- a/driver/battery/smart.c +++ b/driver/battery/smart.c @@ -413,7 +413,7 @@ static int host_command_sb_read_block(struct host_cmd_handler_args *args) (p->reg != SB_DEVICE_CHEMISTRY) && (p->reg != SB_MANUFACTURER_DATA)) return EC_RES_INVALID_PARAM; - rv = i2c_read_string(I2C_PORT_BATTERY, BATTERY_ADDR, p->reg, + rv = sb_read_string(I2C_PORT_BATTERY, BATTERY_ADDR, p->reg, r->data, 32); if (rv) return EC_RES_ERROR; -- cgit v1.2.1