]> git.proxmox.com Git - mirror_qemu.git/blob - tests/tmp105-test.c
Merge remote-tracking branch 'remotes/vivier2/tags/trivial-branch-pull-request' into...
[mirror_qemu.git] / tests / tmp105-test.c
1 /*
2 * QTest testcase for the TMP105 temperature sensor
3 *
4 * Copyright (c) 2012 Andreas Färber
5 *
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 */
9
10 #include "qemu/osdep.h"
11
12 #include "libqtest.h"
13 #include "libqos/i2c.h"
14 #include "qapi/qmp/qdict.h"
15 #include "hw/misc/tmp105_regs.h"
16
17 #define TMP105_TEST_ID "tmp105-test"
18 #define TMP105_TEST_ADDR 0x49
19
20 static I2CAdapter *i2c;
21
22 static uint16_t tmp105_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
23 {
24 uint8_t resp[1];
25 i2c_send(i2c, addr, &reg, 1);
26 i2c_recv(i2c, addr, resp, 1);
27 return resp[0];
28 }
29
30 static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
31 {
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];
36 }
37
38 static void tmp105_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
39 uint8_t value)
40 {
41 uint8_t cmd[2];
42 uint8_t resp[1];
43
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]);
49 }
50
51 static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
52 uint16_t value)
53 {
54 uint8_t cmd[3];
55 uint8_t resp[2];
56
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]);
64 }
65
66 static int qmp_tmp105_get_temperature(const char *id)
67 {
68 QDict *response;
69 int ret;
70
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;
77 }
78
79 static void qmp_tmp105_set_temperature(const char *id, int value)
80 {
81 QDict *response;
82
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);
87 }
88
89 #define TMP105_PRECISION (1000/16)
90 static void send_and_receive(void)
91 {
92 uint16_t value;
93
94 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
95 g_assert_cmpuint(value, ==, 0);
96
97 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
98 g_assert_cmphex(value, ==, 0);
99
100 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
101 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
102 g_assert_cmpuint(value, ==, 20000);
103
104 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
105 g_assert_cmphex(value, ==, 0x1400);
106
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);
111
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);
116
117 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
118 g_assert_cmphex(value, ==, 0x14f0);
119
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);
124
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);
128
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);
132
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);
137
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);
141
142 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
143 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
144 }
145
146 int main(int argc, char **argv)
147 {
148 QTestState *s = NULL;
149 int ret;
150
151 g_test_init(&argc, &argv, NULL);
152
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);
157
158 qtest_add_func("/tmp105/tx-rx", send_and_receive);
159
160 ret = g_test_run();
161
162 qtest_quit(s);
163 g_free(i2c);
164
165 return ret;
166 }