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 <ssl@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/209866
Reviewed-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
Sheng-Liang Song
2014-07-24 11:39:06 -07:00
committed by chrome-internal-fetch
parent f5b9f2d641
commit dcb9bb6026
6 changed files with 41 additions and 106 deletions

View File

@@ -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

View File

@@ -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, &reg, 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;
}

View File

@@ -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, &reg, 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;

View File

@@ -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, &reg, 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 */

39
chip/stm32/i2c.c Normal file
View File

@@ -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, &reg, 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;
}

View File

@@ -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;