]> git.proxmox.com Git - mirror_qemu.git/blame - tests/tmp105-test.c
vmstate: check subsection_found is enough
[mirror_qemu.git] / tests / tmp105-test.c
CommitLineData
6e998903
AF
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 */
91f32b0c 9
681c28a3 10#include "qemu/osdep.h"
91f32b0c 11
6e998903 12#include "libqtest.h"
cc9936a3 13#include "libqos/i2c.h"
452fcdbc 14#include "qapi/qmp/qdict.h"
0d09e41a 15#include "hw/misc/tmp105_regs.h"
6e998903 16
a4ec5bb7
PB
17#define TMP105_TEST_ID "tmp105-test"
18#define TMP105_TEST_ADDR 0x49
6e998903
AF
19
20static I2CAdapter *i2c;
6e998903 21
7373fc76
PB
22static 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
cebac614 30static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
6e998903 31{
6e998903 32 uint8_t resp[2];
cebac614 33 i2c_send(i2c, addr, &reg, 1);
6e998903 34 i2c_recv(i2c, addr, resp, 2);
cebac614
PB
35 return (resp[0] << 8) | resp[1];
36}
37
38static 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];
6e998903 43
cebac614
PB
44 cmd[0] = reg;
45 cmd[1] = value;
6e998903
AF
46 i2c_send(i2c, addr, cmd, 2);
47 i2c_recv(i2c, addr, resp, 1);
48 g_assert_cmphex(resp[0], ==, cmd[1]);
cebac614 49}
6e998903 50
cebac614
PB
51static 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];
6e998903 56
cebac614
PB
57 cmd[0] = reg;
58 cmd[1] = value >> 8;
59 cmd[2] = value & 255;
6e998903
AF
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
7373fc76
PB
66static int qmp_tmp105_get_temperature(const char *id)
67{
68 QDict *response;
69 int ret;
cebac614 70
563890c7 71 response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
7373fc76
PB
72 "'property': 'temperature' } }", id);
73 g_assert(qdict_haskey(response, "return"));
74 ret = qdict_get_int(response, "return");
cb3e7f08 75 qobject_unref(response);
7373fc76
PB
76 return ret;
77}
78
79static void qmp_tmp105_set_temperature(const char *id, int value)
80{
81 QDict *response;
82
563890c7 83 response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
7373fc76
PB
84 "'property': 'temperature', 'value': %d } }", id, value);
85 g_assert(qdict_haskey(response, "return"));
cb3e7f08 86 qobject_unref(response);
7373fc76
PB
87}
88
89#define TMP105_PRECISION (1000/16)
cebac614
PB
90static void send_and_receive(void)
91{
92 uint16_t value;
93
7373fc76 94 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
cebac614
PB
95 g_assert_cmpuint(value, ==, 0);
96
7373fc76
PB
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);
cebac614 141
a4ec5bb7
PB
142 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
143 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
cebac614
PB
144}
145
6e998903
AF
146int main(int argc, char **argv)
147{
148 QTestState *s = NULL;
149 int ret;
150
151 g_test_init(&argc, &argv, NULL);
152
a4ec5bb7
PB
153 s = qtest_start("-machine n800 "
154 "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
155 ",address=0x49");
f1dfd507 156 i2c = omap_i2c_create(s, OMAP2_I2C_1_BASE);
6e998903
AF
157
158 qtest_add_func("/tmp105/tx-rx", send_and_receive);
159
160 ret = g_test_run();
161
f1dfd507 162 qtest_quit(s);
6e998903
AF
163 g_free(i2c);
164
165 return ret;
166}