Fake I2C device support for emulator

To test drivers, we need a way to fake I2C periphrals. With this CL, a
fake peripheral can be done by declaring its own I2C read/write
functions. The fake I2C peripherals may return EC_ERROR_INVAL to
indicate it's not responding. The emulator I2C read/write call scans
through all registered I2C peripherals and uses the first response.

BUG=chrome-os-partner:19235
TEST=Pass sbs_charging test with the next CL.
BRANCH=None

Change-Id: I9380dc40e147781b42e09eb6979c864bbd9f2ac4
Signed-off-by: Vic Yang <victoryang@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/169511
Reviewed-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
Vic Yang
2013-09-16 14:45:35 +08:00
committed by chrome-internal-fetch
parent cdd5c206cd
commit d5e183f9e5
4 changed files with 128 additions and 0 deletions

View File

@@ -6,29 +6,71 @@
*/
#include "i2c.h"
#include "link_defs.h"
#include "test_util.h"
int i2c_read16(int port, int slave_addr, int offset, int *data)
{
const struct test_i2c_read_dev *p;
int rv;
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;
}
int i2c_write16(int port, int slave_addr, int offset, int data)
{
const struct test_i2c_write_dev *p;
int rv;
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;
}
int i2c_read8(int port, int slave_addr, int offset, int *data)
{
const struct test_i2c_read_dev *p;
int rv;
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;
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;
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;
}

View File

@@ -83,6 +83,26 @@ SECTIONS {
__deferred_funcs = .;
*(.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_read_string = .;
*(.rodata.test_i2c.read_string)
__test_i2c_read_string_end = .;
}
}
INSERT BEFORE .rodata;

View File

@@ -12,6 +12,7 @@
#include "hooks.h"
#include "host_command.h"
#include "task.h"
#include "test_util.h"
/* Console commands */
extern const struct console_command __cmds[];
@@ -53,6 +54,18 @@ extern const struct hook_data __hooks_second_end[];
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_write_dev __test_i2c_write16[];
extern const struct test_i2c_write_dev __test_i2c_write16_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[];
/* Host commands */
extern const struct host_command __hcmds[];
extern const struct host_command __hcmds_end[];

View File

@@ -165,4 +165,57 @@ void test_clean_up(void);
/* Set the next step and reboot */
void test_reboot_to_next_step(enum test_state_t step);
struct test_i2c_read_string_dev {
/* I2C string read handler */
int (*routine)(int port, int slave_addr, int offset, uint8_t *data,
int len);
};
struct test_i2c_read_dev {
/* I2C read handler */
int (*routine)(int port, int slave_addr, int offset, int *data);
};
struct test_i2c_write_dev {
/* I2C write handler */
int (*routine)(int port, int slave_addr, int offset, int data);
};
/**
* Register an I2C 8-bit read function.
*
* When this function is called, it should either perform the desired
* 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()
*/
#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}
#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"))) \
= {routine}
#endif /* __CROS_EC_TEST_UTIL_H */