spapr_pci: Get rid of duplicate code for node name creation
[qemu/ar7.git] / tests / tmp105-test.c
blob34cae7a582487aff63ac235dbe9b654c7f2f0506
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 "qemu/osdep.h"
12 #include "libqtest.h"
13 #include "libqos/i2c.h"
14 #include "qapi/qmp/qdict.h"
15 #include "hw/misc/tmp105_regs.h"
17 #define TMP105_TEST_ID "tmp105-test"
18 #define TMP105_TEST_ADDR 0x49
20 static I2CAdapter *i2c;
22 static uint16_t tmp105_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
24 uint8_t resp[1];
25 i2c_send(i2c, addr, &reg, 1);
26 i2c_recv(i2c, addr, resp, 1);
27 return resp[0];
30 static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
32 uint8_t resp[2];
33 i2c_send(i2c, addr, &reg, 1);
34 i2c_recv(i2c, addr, resp, 2);
35 return (resp[0] << 8) | resp[1];
38 static void tmp105_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
39 uint8_t value)
41 uint8_t cmd[2];
42 uint8_t resp[1];
44 cmd[0] = reg;
45 cmd[1] = value;
46 i2c_send(i2c, addr, cmd, 2);
47 i2c_recv(i2c, addr, resp, 1);
48 g_assert_cmphex(resp[0], ==, cmd[1]);
51 static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
52 uint16_t value)
54 uint8_t cmd[3];
55 uint8_t resp[2];
57 cmd[0] = reg;
58 cmd[1] = value >> 8;
59 cmd[2] = value & 255;
60 i2c_send(i2c, addr, cmd, 3);
61 i2c_recv(i2c, addr, resp, 2);
62 g_assert_cmphex(resp[0], ==, cmd[1]);
63 g_assert_cmphex(resp[1], ==, cmd[2]);
66 static int qmp_tmp105_get_temperature(const char *id)
68 QDict *response;
69 int ret;
71 response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
72 "'property': 'temperature' } }", id);
73 g_assert(qdict_haskey(response, "return"));
74 ret = qdict_get_int(response, "return");
75 qobject_unref(response);
76 return ret;
79 static void qmp_tmp105_set_temperature(const char *id, int value)
81 QDict *response;
83 response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
84 "'property': 'temperature', 'value': %d } }", id, value);
85 g_assert(qdict_haskey(response, "return"));
86 qobject_unref(response);
89 #define TMP105_PRECISION (1000/16)
90 static void send_and_receive(void)
92 uint16_t value;
94 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
95 g_assert_cmpuint(value, ==, 0);
97 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
98 g_assert_cmphex(value, ==, 0);
100 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
101 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
102 g_assert_cmpuint(value, ==, 20000);
104 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
105 g_assert_cmphex(value, ==, 0x1400);
107 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */
108 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
109 g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
110 g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
112 /* Set config */
113 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
114 value = tmp105_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG);
115 g_assert_cmphex(value, ==, 0x60);
117 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
118 g_assert_cmphex(value, ==, 0x14f0);
120 /* Set precision to 9, 10, 11 bits. */
121 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x00);
122 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
123 g_assert_cmphex(value, ==, 0x1480);
125 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x20);
126 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
127 g_assert_cmphex(value, ==, 0x14c0);
129 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x40);
130 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
131 g_assert_cmphex(value, ==, 0x14e0);
133 /* stored precision remains the same */
134 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
135 g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
136 g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
138 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
139 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
140 g_assert_cmphex(value, ==, 0x14f0);
142 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
143 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
146 int main(int argc, char **argv)
148 QTestState *s = NULL;
149 int ret;
151 g_test_init(&argc, &argv, NULL);
153 s = qtest_start("-machine n800 "
154 "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
155 ",address=0x49");
156 i2c = omap_i2c_create(s, OMAP2_I2C_1_BASE);
158 qtest_add_func("/tmp105/tx-rx", send_and_receive);
160 ret = g_test_run();
162 qtest_quit(s);
163 g_free(i2c);
165 return ret;