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>
This commit is contained in:
Paolo Bonzini 2019-03-18 15:09:51 +01:00
parent 7d8ada6e4d
commit e8ecb706a8
5 changed files with 88 additions and 95 deletions

View File

@ -35,17 +35,11 @@ static inline uint8_t bcd2bin(uint8_t x)
static void send_and_receive(void) static void send_and_receive(void)
{ {
uint8_t cmd[1];
uint8_t resp[7]; uint8_t resp[7];
time_t now = time(NULL); time_t now = time(NULL);
struct tm *tm_ptr = gmtime(&now); struct tm *tm_ptr = gmtime(&now);
/* reset the index in the RTC memory */ i2c_read_block(i2c, addr, 0, resp, sizeof(resp));
cmd[0] = 0;
i2c_send(i2c, addr, cmd, 1);
/* retrieve the date */
i2c_recv(i2c, addr, resp, 7);
/* check retrieved time againt local time */ /* check retrieved time againt local time */
g_assert_cmpuint(bcd2bin(resp[4]), == , tm_ptr->tm_mday); g_assert_cmpuint(bcd2bin(resp[4]), == , tm_ptr->tm_mday);

View File

@ -21,3 +21,50 @@ void i2c_recv(I2CAdapter *i2c, uint8_t addr,
{ {
i2c->recv(i2c, addr, buf, len); 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));
}

View File

@ -28,6 +28,17 @@ void i2c_send(I2CAdapter *i2c, uint8_t addr,
void i2c_recv(I2CAdapter *i2c, uint8_t addr, void i2c_recv(I2CAdapter *i2c, uint8_t addr,
uint8_t *buf, uint16_t len); 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 */ /* libi2c-omap.c */
I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr); I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr);

View File

@ -18,27 +18,6 @@
static I2CAdapter *i2c; static I2CAdapter *i2c;
static uint8_t pca9552_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
{
uint8_t resp[1];
i2c_send(i2c, addr, &reg, 1);
i2c_recv(i2c, addr, resp, 1);
return resp[0];
}
static void pca9552_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
uint8_t value)
{
uint8_t cmd[2];
uint8_t resp[1];
cmd[0] = reg;
cmd[1] = value;
i2c_send(i2c, addr, cmd, 2);
i2c_recv(i2c, addr, resp, 1);
g_assert_cmphex(resp[0], ==, cmd[1]);
}
static void receive_autoinc(void) static void receive_autoinc(void)
{ {
uint8_t resp; uint8_t resp;
@ -67,26 +46,26 @@ static void send_and_receive(void)
{ {
uint8_t value; uint8_t value;
value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0); value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
g_assert_cmphex(value, ==, 0x55); g_assert_cmphex(value, ==, 0x55);
value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0); value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
g_assert_cmphex(value, ==, 0x0); g_assert_cmphex(value, ==, 0x0);
/* Switch on LED 0 */ /* Switch on LED 0 */
pca9552_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0, 0x54); i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0, 0x54);
value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0); value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
g_assert_cmphex(value, ==, 0x54); g_assert_cmphex(value, ==, 0x54);
value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0); value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
g_assert_cmphex(value, ==, 0x01); g_assert_cmphex(value, ==, 0x01);
/* Switch on LED 12 */ /* Switch on LED 12 */
pca9552_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3, 0x54); i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3, 0x54);
value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3); value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3);
g_assert_cmphex(value, ==, 0x54); g_assert_cmphex(value, ==, 0x54);
value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT1); value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT1);
g_assert_cmphex(value, ==, 0x10); g_assert_cmphex(value, ==, 0x10);
} }

View File

@ -19,50 +19,6 @@
static I2CAdapter *i2c; static I2CAdapter *i2c;
static uint16_t tmp105_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
{
uint8_t resp[1];
i2c_send(i2c, addr, &reg, 1);
i2c_recv(i2c, addr, resp, 1);
return resp[0];
}
static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
{
uint8_t resp[2];
i2c_send(i2c, addr, &reg, 1);
i2c_recv(i2c, addr, resp, 2);
return (resp[0] << 8) | resp[1];
}
static void tmp105_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
uint8_t value)
{
uint8_t cmd[2];
uint8_t resp[1];
cmd[0] = reg;
cmd[1] = value;
i2c_send(i2c, addr, cmd, 2);
i2c_recv(i2c, addr, resp, 1);
g_assert_cmphex(resp[0], ==, cmd[1]);
}
static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
uint16_t value)
{
uint8_t cmd[3];
uint8_t resp[2];
cmd[0] = reg;
cmd[1] = value >> 8;
cmd[2] = value & 255;
i2c_send(i2c, addr, cmd, 3);
i2c_recv(i2c, addr, resp, 2);
g_assert_cmphex(resp[0], ==, cmd[1]);
g_assert_cmphex(resp[1], ==, cmd[2]);
}
static int qmp_tmp105_get_temperature(const char *id) static int qmp_tmp105_get_temperature(const char *id)
{ {
QDict *response; QDict *response;
@ -94,14 +50,14 @@ static void send_and_receive(void)
value = qmp_tmp105_get_temperature(TMP105_TEST_ID); value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
g_assert_cmpuint(value, ==, 0); g_assert_cmpuint(value, ==, 0);
value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); value = i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
g_assert_cmphex(value, ==, 0); g_assert_cmphex(value, ==, 0);
qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000); qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
value = qmp_tmp105_get_temperature(TMP105_TEST_ID); value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
g_assert_cmpuint(value, ==, 20000); g_assert_cmpuint(value, ==, 20000);
value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); value = i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
g_assert_cmphex(value, ==, 0x1400); g_assert_cmphex(value, ==, 0x1400);
qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */ qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */
@ -110,24 +66,27 @@ static void send_and_receive(void)
g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2); g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
/* Set config */ /* Set config */
tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60); i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
value = tmp105_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG); value = i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG);
g_assert_cmphex(value, ==, 0x60); g_assert_cmphex(value, ==, 0x60);
value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); value = i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
g_assert_cmphex(value, ==, 0x14f0); g_assert_cmphex(value, ==, 0x14f0);
/* Set precision to 9, 10, 11 bits. */ /* Set precision to 9, 10, 11 bits. */
tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x00); i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x00);
value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); g_assert_cmphex(i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG), ==, 0x00);
value = i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
g_assert_cmphex(value, ==, 0x1480); g_assert_cmphex(value, ==, 0x1480);
tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x20); i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x20);
value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); g_assert_cmphex(i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG), ==, 0x20);
value = i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
g_assert_cmphex(value, ==, 0x14c0); g_assert_cmphex(value, ==, 0x14c0);
tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x40); i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x40);
value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); g_assert_cmphex(i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG), ==, 0x40);
value = i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
g_assert_cmphex(value, ==, 0x14e0); g_assert_cmphex(value, ==, 0x14e0);
/* stored precision remains the same */ /* stored precision remains the same */
@ -135,12 +94,15 @@ static void send_and_receive(void)
g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2); g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2); g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60); i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); g_assert_cmphex(i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG), ==, 0x60);
value = i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
g_assert_cmphex(value, ==, 0x14f0); g_assert_cmphex(value, ==, 0x14f0);
tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234); i2c_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231); g_assert_cmphex(i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW), ==, 0x1234);
i2c_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
g_assert_cmphex(i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH), ==, 0x4231);
} }
int main(int argc, char **argv) int main(int argc, char **argv)