aboutsummaryrefslogtreecommitdiff
path: root/tests/libqos
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2019-03-18 15:09:51 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2019-06-03 14:03:01 +0200
commite8ecb706a8a3a75ea45387a3561b6debed9cacc3 (patch)
treee0074f37da7f4dce193e64c8d11192762429e366 /tests/libqos
parent7d8ada6e4d20b47dcf42d22fc62599a9799eac7a (diff)
libqos: move common i2c code to libqos
The functions to read/write 8-bit or 16-bit registers are the same in tmp105 and pca9552 tests, and in fact they are a special case of "read block"/"write block" functionality; read block in turn is used in ds1338-test. Move everything inside libqos-test, removing the duplication. Account for the small differences by adding to tmp105-test.c the "read register after writing" behavior that is specific to it. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'tests/libqos')
-rw-r--r--tests/libqos/i2c.c47
-rw-r--r--tests/libqos/i2c.h11
2 files changed, 58 insertions, 0 deletions
diff --git a/tests/libqos/i2c.c b/tests/libqos/i2c.c
index 23bc2a3eb2..daf9a96617 100644
--- a/tests/libqos/i2c.c
+++ b/tests/libqos/i2c.c
@@ -21,3 +21,50 @@ void i2c_recv(I2CAdapter *i2c, uint8_t addr,
{
i2c->recv(i2c, addr, buf, len);
}
+
+void i2c_read_block(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+ uint8_t *buf, uint16_t len)
+{
+ i2c_send(i2c, addr, &reg, 1);
+ i2c_recv(i2c, addr, buf, len);
+}
+
+void i2c_write_block(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+ const uint8_t *buf, uint16_t len)
+{
+ uint8_t *cmd = g_malloc(len + 1);
+ cmd[0] = reg;
+ memcpy(&cmd[1], buf, len);
+ i2c_send(i2c, addr, cmd, len + 1);
+ g_free(cmd);
+}
+
+uint8_t i2c_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
+{
+ uint8_t resp[1];
+ i2c_read_block(i2c, addr, reg, resp, sizeof(resp));
+ return resp[0];
+}
+
+uint16_t i2c_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
+{
+ uint8_t resp[2];
+ i2c_read_block(i2c, addr, reg, resp, sizeof(resp));
+ return (resp[0] << 8) | resp[1];
+}
+
+void i2c_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+ uint8_t value)
+{
+ i2c_write_block(i2c, addr, reg, &value, 1);
+}
+
+void i2c_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+ uint16_t value)
+{
+ uint8_t data[2];
+
+ data[0] = value >> 8;
+ data[1] = value & 255;
+ i2c_write_block(i2c, addr, reg, data, sizeof(data));
+}
diff --git a/tests/libqos/i2c.h b/tests/libqos/i2c.h
index cc01358a9f..a462114597 100644
--- a/tests/libqos/i2c.h
+++ b/tests/libqos/i2c.h
@@ -28,6 +28,17 @@ void i2c_send(I2CAdapter *i2c, uint8_t addr,
void i2c_recv(I2CAdapter *i2c, uint8_t addr,
uint8_t *buf, uint16_t len);
+void i2c_read_block(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+ uint8_t *buf, uint16_t len);
+void i2c_write_block(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+ const uint8_t *buf, uint16_t len);
+uint8_t i2c_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg);
+uint16_t i2c_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg);
+void i2c_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+ uint8_t value);
+void i2c_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+ uint16_t value);
+
/* libi2c-omap.c */
I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr);