tmp105-test: Wrap simple building blocks for testing
[qemu/ar7.git] / tests / tmp105-test.c
blob20a1894ea57b03232b065644c423eba2ca218061
1 /*
2 * QTest testcase for the TMP105 temperature sensor
4 * Copyright (c) 2012 Andreas Färber
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
10 #include <glib.h>
12 #include "libqtest.h"
13 #include "libqos/i2c.h"
14 #include "hw/misc/tmp105_regs.h"
16 #define OMAP2_I2C_1_BASE 0x48070000
18 #define N8X0_ADDR 0x48
20 static I2CAdapter *i2c;
21 static uint8_t addr;
23 static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
25 uint8_t resp[2];
26 i2c_send(i2c, addr, &reg, 1);
27 i2c_recv(i2c, addr, resp, 2);
28 return (resp[0] << 8) | resp[1];
31 static void tmp105_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
32 uint8_t value)
34 uint8_t cmd[2];
35 uint8_t resp[1];
37 cmd[0] = reg;
38 cmd[1] = value;
39 i2c_send(i2c, addr, cmd, 2);
40 i2c_recv(i2c, addr, resp, 1);
41 g_assert_cmphex(resp[0], ==, cmd[1]);
44 static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
45 uint16_t value)
47 uint8_t cmd[3];
48 uint8_t resp[2];
50 cmd[0] = reg;
51 cmd[1] = value >> 8;
52 cmd[2] = value & 255;
53 i2c_send(i2c, addr, cmd, 3);
54 i2c_recv(i2c, addr, resp, 2);
55 g_assert_cmphex(resp[0], ==, cmd[1]);
56 g_assert_cmphex(resp[1], ==, cmd[2]);
60 static void send_and_receive(void)
62 uint16_t value;
64 value = tmp105_get16(i2c, addr, TMP105_REG_TEMPERATURE);
65 g_assert_cmpuint(value, ==, 0);
67 /* reset */
68 tmp105_set8(i2c, addr, TMP105_REG_CONFIG, 0);
70 tmp105_set16(i2c, addr, TMP105_REG_T_LOW, 0x1234);
71 tmp105_set16(i2c, addr, TMP105_REG_T_HIGH, 0x4231);
74 int main(int argc, char **argv)
76 QTestState *s = NULL;
77 int ret;
79 g_test_init(&argc, &argv, NULL);
81 s = qtest_start("-machine n800");
82 i2c = omap_i2c_create(OMAP2_I2C_1_BASE);
83 addr = N8X0_ADDR;
85 qtest_add_func("/tmp105/tx-rx", send_and_receive);
87 ret = g_test_run();
89 if (s) {
90 qtest_quit(s);
92 g_free(i2c);
94 return ret;