summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGwendal Grignou <gwendal@chromium.org>2015-09-15 12:15:49 -0700
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2016-11-12 19:25:07 +0000
commite64dbbc56c1a6b8f59fccf4e49afb7c23e131cc4 (patch)
treea701b30718871e6a0cb96e5a96ca35efd75ea968
parent85335008bd4a34ad00790a2abeb0ab8206a74e3e (diff)
downloadchrome-ec-e64dbbc56c1a6b8f59fccf4e49afb7c23e131cc4.tar.gz
BACKPORT: host: mock i2c_xfer
Instead of mocking i2c_read8/16/32, mock i2c_xfer. We can now test code that call i2c_xfer directly and test common/i2c.c BRANCH=samus, ryu, cyan BUG=chrome-os-partner:45223 TEST=Unit tests pass. Change-Id: Iaa772515c40cf55d2050d0019e2062d63278adc0 Signed-off-by: Gwendal Grignou <gwendal@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/299768 Reviewed-by: Alec Berg <alecaberg@chromium.org> (cherry picked from commit c2c02249a01ec56857a51e1645060325f7558b59) Signed-off-by: Gwendal Grignou <gwendal@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/359411 Reviewed-on: https://chromium-review.googlesource.com/409498 Reviewed-by: Aseda Aboagye <aaboagye@chromium.org>
-rw-r--r--board/host/battery.c46
-rw-r--r--board/host/board.c13
-rw-r--r--chip/host/config_chip.h2
-rw-r--r--chip/host/i2c.c119
-rw-r--r--core/host/host_exe.lds30
-rw-r--r--include/link_defs.h16
-rw-r--r--include/test_util.h51
-rw-r--r--test/test_config.h25
8 files changed, 77 insertions, 225 deletions
diff --git a/board/host/battery.c b/board/host/battery.c
index a5c447f5d7..e2c4b26b85 100644
--- a/board/host/battery.c
+++ b/board/host/battery.c
@@ -14,38 +14,34 @@
static uint16_t mock_smart_battery[SB_MANUFACTURER_DATA + 1];
-int sb_i2c_read16(int port, int slave_addr, int offset, int *data)
+int sb_i2c_xfer(int port, int slave_addr, const uint8_t *out, int out_size,
+ uint8_t *in, int in_size, int flags)
{
- if (port != I2C_PORT_BATTERY || slave_addr != BATTERY_ADDR)
- return EC_ERROR_INVAL;
- if (offset >= ARRAY_SIZE(mock_smart_battery))
- return EC_ERROR_UNIMPLEMENTED;
- if (offset < 0 || data == NULL)
- return EC_ERROR_INVAL;
- *data = mock_smart_battery[offset];
- return EC_SUCCESS;
-}
-DECLARE_TEST_I2C_READ16(sb_i2c_read16);
+ if (out_size == 0 && in_size == 0)
+ return EC_SUCCESS;
-int sb_i2c_write16(int port, int slave_addr, int offset, int data)
-{
if (port != I2C_PORT_BATTERY || slave_addr != BATTERY_ADDR)
return EC_ERROR_INVAL;
- if (offset >= ARRAY_SIZE(mock_smart_battery))
+ if (out[0] >= ARRAY_SIZE(mock_smart_battery))
return EC_ERROR_UNIMPLEMENTED;
- if (offset < 0)
- return EC_ERROR_INVAL;
- mock_smart_battery[offset] = data;
- return EC_SUCCESS;
-}
-DECLARE_TEST_I2C_WRITE16(sb_i2c_write16);
-
-int sb_i2c_read_string(int port, int slave_addr, int offset, uint8_t *data,
- int len)
-{
+ if (out_size == 1) {
+ /* Read */
+ if (in_size != 2)
+ /* We are not doing a read16, assume read string */
+ return EC_SUCCESS;
+ else
+ *(uint16_t *)in = mock_smart_battery[out[0]];
+ } else {
+ /* write */
+ if (out_size != 3)
+ /* We are only expecting write 16 */
+ return EC_ERROR_UNIMPLEMENTED;
+ else
+ mock_smart_battery[out[0]] = (out[2] << 8) | out[1];
+ }
return EC_SUCCESS;
}
-DECLARE_TEST_I2C_READ_STRING(sb_i2c_read_string);
+DECLARE_TEST_I2C_XFER(sb_i2c_xfer);
int battery_time_at_rate(int rate, int *minutes)
{
diff --git a/board/host/board.c b/board/host/board.c
index cd43d7519b..408bfdcf0b 100644
--- a/board/host/board.c
+++ b/board/host/board.c
@@ -8,6 +8,7 @@
#include "extpower.h"
#include "gpio.h"
#include "host_command.h"
+#include "i2c.h"
#include "inductive_charging.h"
#include "lid_switch.h"
#include "motion_sense.h"
@@ -54,3 +55,15 @@ const struct button_config buttons[] = {
BUILD_ASSERT(ARRAY_SIZE(buttons) == CONFIG_BUTTON_COUNT);
#endif
+#ifdef CONFIG_I2C
+/* I2C ports */
+const struct i2c_port_t i2c_ports[] = {
+#ifdef I2C_PORT_BATTERY
+ {"battery", I2C_PORT_BATTERY, 100, 0, 0},
+#elif defined I2C_PORT_LIGHTBAR
+ {"lightbar", I2C_PORT_LIGHTBAR, 100, 0, 0},
+#endif
+};
+
+const unsigned int i2c_ports_used = ARRAY_SIZE(i2c_ports);
+#endif
diff --git a/chip/host/config_chip.h b/chip/host/config_chip.h
index 5df44a9315..80ed605f1c 100644
--- a/chip/host/config_chip.h
+++ b/chip/host/config_chip.h
@@ -39,4 +39,6 @@ extern char __host_flash[CONFIG_FLASH_PHYSICAL_SIZE];
#define GPIO_PIN(port, index) GPIO_##port, (1 << index)
#define GPIO_PIN_MASK(port, mask) GPIO_##port, (mask)
+#define I2C_PORT_COUNT 1
+
#endif /* __CROS_EC_CONFIG_CHIP_H */
diff --git a/chip/host/i2c.c b/chip/host/i2c.c
index ebf3894a43..ad0da261dc 100644
--- a/chip/host/i2c.c
+++ b/chip/host/i2c.c
@@ -75,129 +75,34 @@ static int test_check_detached(int port, int slave_addr)
return 0;
}
-int i2c_read32(int port, int slave_addr, int offset, int *data)
+int chip_i2c_xfer(int port, int slave_addr, const uint8_t *out, int out_size,
+ uint8_t *in, int in_size, int flags)
{
- const struct test_i2c_read_dev *p;
+ const struct test_i2c_xfer *p;
int rv;
if (test_check_detached(port, slave_addr))
return EC_ERROR_UNKNOWN;
- for (p = __test_i2c_read32; p < __test_i2c_read32_end; ++p) {
- rv = p->routine(port, slave_addr, offset, data);
+ for (p = __test_i2c_xfer; p < __test_i2c_xfer_end; ++p) {
+ rv = p->routine(port, slave_addr, out, out_size,
+ in, in_size, flags);
if (rv != EC_ERROR_INVAL)
return rv;
}
return EC_ERROR_UNKNOWN;
}
-int i2c_write32(int port, int slave_addr, int offset, int data)
+int i2c_raw_get_scl(int port)
{
- const struct test_i2c_write_dev *p;
- int rv;
-
- if (test_check_detached(port, slave_addr))
- return EC_ERROR_UNKNOWN;
- for (p = __test_i2c_write32; p < __test_i2c_write32_end; ++p) {
- rv = p->routine(port, slave_addr, offset, data);
- if (rv != EC_ERROR_INVAL)
- return rv;
- }
- return EC_ERROR_UNKNOWN;
-}
-
-int i2c_read16(int port, int slave_addr, int offset, int *data)
-{
- const struct test_i2c_read_dev *p;
- int rv;
-
- if (test_check_detached(port, slave_addr))
- return EC_ERROR_UNKNOWN;
- for (p = __test_i2c_read16; p < __test_i2c_read16_end; ++p) {
- rv = p->routine(port, slave_addr, offset, data);
- if (rv != EC_ERROR_INVAL)
- return rv;
- }
- return EC_ERROR_UNKNOWN;
+ return 1;
}
-int i2c_write16(int port, int slave_addr, int offset, int data)
+int i2c_raw_get_sda(int port)
{
- const struct test_i2c_write_dev *p;
- int rv;
-
- if (test_check_detached(port, slave_addr))
- return EC_ERROR_UNKNOWN;
- for (p = __test_i2c_write16; p < __test_i2c_write16_end; ++p) {
- rv = p->routine(port, slave_addr, offset, data);
- if (rv != EC_ERROR_INVAL)
- return rv;
- }
- return EC_ERROR_UNKNOWN;
+ return 1;
}
-int i2c_read8(int port, int slave_addr, int offset, int *data)
+int i2c_get_line_levels(int port)
{
- const struct test_i2c_read_dev *p;
- int rv;
-
- if (test_check_detached(port, slave_addr))
- return EC_ERROR_UNKNOWN;
- for (p = __test_i2c_read8; p < __test_i2c_read8_end; ++p) {
- rv = p->routine(port, slave_addr, offset, data);
- if (rv != EC_ERROR_INVAL)
- return rv;
- }
- return EC_ERROR_UNKNOWN;
-}
-
-int i2c_write8(int port, int slave_addr, int offset, int data)
-{
- const struct test_i2c_write_dev *p;
- int rv;
-
- if (test_check_detached(port, slave_addr))
- return EC_ERROR_UNKNOWN;
- for (p = __test_i2c_write8; p < __test_i2c_write8_end; ++p) {
- rv = p->routine(port, slave_addr, offset, data);
- if (rv != EC_ERROR_INVAL)
- return rv;
- }
- return EC_ERROR_UNKNOWN;
-}
-
-int i2c_read_string(int port, int slave_addr, int offset, uint8_t *data,
- int len)
-{
- const struct test_i2c_read_string_dev *p;
- int rv;
-
- if (test_check_detached(port, slave_addr))
- return EC_ERROR_UNKNOWN;
- for (p = __test_i2c_read_string; p < __test_i2c_read_string_end; ++p) {
- rv = p->routine(port, slave_addr, offset, data, len);
- if (rv != EC_ERROR_INVAL)
- return rv;
- }
- return EC_ERROR_UNKNOWN;
-}
-
-int smbus_write_word(uint8_t i2c_port, uint8_t slave_addr,
- uint8_t smbus_cmd, uint16_t d16)
-{
- return i2c_write16(i2c_port, slave_addr, smbus_cmd, d16);
-}
-
-int smbus_read_word(uint8_t i2c_port, uint8_t slave_addr,
- uint8_t smbus_cmd, uint16_t *p16)
-{
- int rv, d16 = 0;
- rv = i2c_read16(i2c_port, slave_addr, smbus_cmd, &d16);
- *p16 = d16;
- return rv;
-}
-
-int smbus_read_string(int i2c_port, uint8_t slave_addr, uint8_t smbus_cmd,
- uint8_t *data, int len)
-{
- return i2c_read_string(i2c_port, slave_addr, smbus_cmd, data, len);
+ return 0;
}
diff --git a/core/host/host_exe.lds b/core/host/host_exe.lds
index a30ebb4219..182d86cd6c 100644
--- a/core/host/host_exe.lds
+++ b/core/host/host_exe.lds
@@ -88,33 +88,9 @@ SECTIONS {
*(.rodata.deferred)
__deferred_funcs_end = .;
- __test_i2c_read8 = .;
- *(.rodata.test_i2c.read8)
- __test_i2c_read8_end = .;
-
- __test_i2c_write8 = .;
- *(.rodata.test_i2c.write8)
- __test_i2c_write8_end = .;
-
- __test_i2c_read16 = .;
- *(.rodata.test_i2c.read16)
- __test_i2c_read16_end = .;
-
- __test_i2c_write16 = .;
- *(.rodata.test_i2c.write16)
- __test_i2c_write16_end = .;
-
- __test_i2c_read32 = .;
- *(.rodata.test_i2c.read32)
- __test_i2c_read32_end = .;
-
- __test_i2c_write32 = .;
- *(.rodata.test_i2c.write32)
- __test_i2c_write32_end = .;
-
- __test_i2c_read_string = .;
- *(.rodata.test_i2c.read_string)
- __test_i2c_read_string_end = .;
+ __test_i2c_xfer = .;
+ *(.rodata.test_i2c.xfer)
+ __test_i2c_xfer_end = .;
}
}
INSERT BEFORE .rodata;
diff --git a/include/link_defs.h b/include/link_defs.h
index 8c775affa2..ad6a39f837 100644
--- a/include/link_defs.h
+++ b/include/link_defs.h
@@ -58,20 +58,8 @@ extern const struct deferred_data __deferred_funcs[];
extern const struct deferred_data __deferred_funcs_end[];
/* I2C fake devices for unit testing */
-extern const struct test_i2c_read_dev __test_i2c_read8[];
-extern const struct test_i2c_read_dev __test_i2c_read8_end[];
-extern const struct test_i2c_write_dev __test_i2c_write8[];
-extern const struct test_i2c_write_dev __test_i2c_write8_end[];
-extern const struct test_i2c_read_dev __test_i2c_read16[];
-extern const struct test_i2c_read_dev __test_i2c_read16_end[];
-extern const struct test_i2c_read_dev __test_i2c_read32[];
-extern const struct test_i2c_read_dev __test_i2c_read32_end[];
-extern const struct test_i2c_write_dev __test_i2c_write16[];
-extern const struct test_i2c_write_dev __test_i2c_write16_end[];
-extern const struct test_i2c_write_dev __test_i2c_write32[];
-extern const struct test_i2c_write_dev __test_i2c_write32_end[];
-extern const struct test_i2c_read_string_dev __test_i2c_read_string[];
-extern const struct test_i2c_read_string_dev __test_i2c_read_string_end[];
+extern const struct test_i2c_xfer __test_i2c_xfer[];
+extern const struct test_i2c_xfer __test_i2c_xfer_end[];
/* Host commands */
extern const struct host_command __hcmds[];
diff --git a/include/test_util.h b/include/test_util.h
index b62e965563..e3aa1d8c27 100644
--- a/include/test_util.h
+++ b/include/test_util.h
@@ -214,9 +214,11 @@ struct test_i2c_read_string_dev {
int len);
};
-struct test_i2c_read_dev {
- /* I2C read handler */
- int (*routine)(int port, int slave_addr, int offset, int *data);
+struct test_i2c_xfer {
+ /* I2C xfer handler */
+ int (*routine)(int port, int slave_addr,
+ const uint8_t *out, int out_size,
+ uint8_t *in, int in_size, int flags);
};
struct test_i2c_write_dev {
@@ -231,46 +233,11 @@ struct test_i2c_write_dev {
* mock functionality, or return EC_ERROR_INVAL to indicate it does
* not respond to the specified port and slave address.
*
- * @param routine Function pointer, with the same prototype as i2c_read8()
+ * @param routine Function pointer, with the same prototype as i2c_xfer()
*/
-#define DECLARE_TEST_I2C_READ8(routine) \
- const struct test_i2c_read_dev __test_i2c_read8_##routine \
- __attribute__((section(".rodata.test_i2c.read8"))) \
- = {routine}
-
-/* Register an I2C 8-bit write function. */
-#define DECLARE_TEST_I2C_WRITE8(routine) \
- const struct test_i2c_write_dev __test_i2c_write8_##routine \
- __attribute__((section(".rodata.test_i2c.write8"))) \
- = {routine}
-
-/* Register an I2C 16-bit read function. */
-#define DECLARE_TEST_I2C_READ16(routine) \
- const struct test_i2c_read_dev __test_i2c_read16_##routine \
- __attribute__((section(".rodata.test_i2c.read16"))) \
- = {routine}
-
-/* Register an I2C 16-bit write function. */
-#define DECLARE_TEST_I2C_WRITE16(routine) \
- const struct test_i2c_write_dev __test_i2c_write16_##routine \
- __attribute__((section(".rodata.test_i2c.write16"))) \
- = {routine}
-
-/* Register an I2C 32-bit read function. */
-#define DECLARE_TEST_I2C_READ32(routine) \
- const struct test_i2c_read_dev __test_i2c_read32_##routine \
- __attribute__((section(".rodata.test_i2c.read32"))) \
- = {routine}
-
-/* Register an I2C 32-bit write function. */
-#define DECLARE_TEST_I2C_WRITE32(routine) \
- const struct test_i2c_write_dev __test_i2c_write32_##routine \
- __attribute__((section(".rodata.test_i2c.write32"))) \
- = {routine}
-
-#define DECLARE_TEST_I2C_READ_STRING(routine) \
- const struct test_i2c_read_string_dev __test_i2c_rs_##routine \
- __attribute__((section(".rodata.test_i2c.read_string"))) \
+#define DECLARE_TEST_I2C_XFER(routine) \
+ const struct test_i2c_xfer __test_i2c_xfer_##routine \
+ __attribute__((section(".rodata.test_i2c.xfer"))) \
= {routine}
/*
diff --git a/test/test_config.h b/test/test_config.h
index 75052829cd..42fdcd7442 100644
--- a/test/test_config.h
+++ b/test/test_config.h
@@ -54,10 +54,11 @@
#define CONFIG_CHARGER_INPUT_CURRENT 4032
#define CONFIG_CHARGER_DISCHARGE_ON_AC
#define CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM
+#define CONFIG_I2C
int board_discharge_on_ac(int enabled);
-#define I2C_PORT_MASTER 1
-#define I2C_PORT_BATTERY 1
-#define I2C_PORT_CHARGER 1
+#define I2C_PORT_MASTER 0
+#define I2C_PORT_BATTERY 0
+#define I2C_PORT_CHARGER 0
#endif
#ifdef TEST_SBS_CHARGING_V2
@@ -69,18 +70,20 @@ int board_discharge_on_ac(int enabled);
#define CONFIG_CHARGER_INPUT_CURRENT 4032
#define CONFIG_CHARGER_DISCHARGE_ON_AC
#define CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM
+#define CONFIG_I2C
int board_discharge_on_ac(int enabled);
-#define I2C_PORT_MASTER 1
-#define I2C_PORT_BATTERY 1
-#define I2C_PORT_CHARGER 1
+#define I2C_PORT_MASTER 0
+#define I2C_PORT_BATTERY 0
+#define I2C_PORT_CHARGER 0
#endif
#ifdef TEST_THERMAL
#define CONFIG_CHIPSET_CAN_THROTTLE
#define CONFIG_FANS 1
+#define CONFIG_I2C
#define CONFIG_TEMP_SENSOR
#define CONFIG_THERMISTOR_NCP15WB
-#define I2C_PORT_THERMAL 1
+#define I2C_PORT_THERMAL 0
int ncp15wb_calculate_temp(uint16_t adc);
#endif
@@ -97,13 +100,15 @@ int ncp15wb_calculate_temp(uint16_t adc);
#define CONFIG_BATTERY_MOCK
#define CONFIG_BATTERY_SMART
#define CONFIG_CHARGER_INPUT_CURRENT 4032
-#define I2C_PORT_MASTER 1
-#define I2C_PORT_BATTERY 1
-#define I2C_PORT_CHARGER 1
+#define CONFIG_I2C
+#define I2C_PORT_MASTER 0
+#define I2C_PORT_BATTERY 0
+#define I2C_PORT_CHARGER 0
#endif
#ifdef TEST_LIGHTBAR
#define CONFIG_I2C
+#define CONFIG_I2C_MASTER
#define I2C_PORT_LIGHTBAR 0
#define CONFIG_ALS_LIGHTBAR_DIMMING 0
#endif