]> git.proxmox.com Git - mirror_qemu.git/blame - tests/tmp105-test.c
libqos: Use explicit QTestState for rtas operations
[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
6e998903
AF
17#define OMAP2_I2C_1_BASE 0x48070000
18
a4ec5bb7
PB
19#define TMP105_TEST_ID "tmp105-test"
20#define TMP105_TEST_ADDR 0x49
6e998903
AF
21
22static I2CAdapter *i2c;
6e998903 23
7373fc76
PB
24static uint16_t tmp105_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
25{
26 uint8_t resp[1];
27 i2c_send(i2c, addr, &reg, 1);
28 i2c_recv(i2c, addr, resp, 1);
29 return resp[0];
30}
31
cebac614 32static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
6e998903 33{
6e998903 34 uint8_t resp[2];
cebac614 35 i2c_send(i2c, addr, &reg, 1);
6e998903 36 i2c_recv(i2c, addr, resp, 2);
cebac614
PB
37 return (resp[0] << 8) | resp[1];
38}
39
40static void tmp105_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
41 uint8_t value)
42{
43 uint8_t cmd[2];
44 uint8_t resp[1];
6e998903 45
cebac614
PB
46 cmd[0] = reg;
47 cmd[1] = value;
6e998903
AF
48 i2c_send(i2c, addr, cmd, 2);
49 i2c_recv(i2c, addr, resp, 1);
50 g_assert_cmphex(resp[0], ==, cmd[1]);
cebac614 51}
6e998903 52
cebac614
PB
53static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
54 uint16_t value)
55{
56 uint8_t cmd[3];
57 uint8_t resp[2];
6e998903 58
cebac614
PB
59 cmd[0] = reg;
60 cmd[1] = value >> 8;
61 cmd[2] = value & 255;
6e998903
AF
62 i2c_send(i2c, addr, cmd, 3);
63 i2c_recv(i2c, addr, resp, 2);
64 g_assert_cmphex(resp[0], ==, cmd[1]);
65 g_assert_cmphex(resp[1], ==, cmd[2]);
66}
67
7373fc76
PB
68static int qmp_tmp105_get_temperature(const char *id)
69{
70 QDict *response;
71 int ret;
cebac614 72
563890c7 73 response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
7373fc76
PB
74 "'property': 'temperature' } }", id);
75 g_assert(qdict_haskey(response, "return"));
76 ret = qdict_get_int(response, "return");
77 QDECREF(response);
78 return ret;
79}
80
81static void qmp_tmp105_set_temperature(const char *id, int value)
82{
83 QDict *response;
84
563890c7 85 response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
7373fc76
PB
86 "'property': 'temperature', 'value': %d } }", id, value);
87 g_assert(qdict_haskey(response, "return"));
88 QDECREF(response);
89}
90
91#define TMP105_PRECISION (1000/16)
cebac614
PB
92static void send_and_receive(void)
93{
94 uint16_t value;
95
7373fc76 96 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
cebac614
PB
97 g_assert_cmpuint(value, ==, 0);
98
7373fc76
PB
99 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
100 g_assert_cmphex(value, ==, 0);
101
102 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
103 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
104 g_assert_cmpuint(value, ==, 20000);
105
106 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
107 g_assert_cmphex(value, ==, 0x1400);
108
109 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */
110 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
111 g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
112 g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
113
114 /* Set config */
115 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
116 value = tmp105_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG);
117 g_assert_cmphex(value, ==, 0x60);
118
119 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
120 g_assert_cmphex(value, ==, 0x14f0);
121
122 /* Set precision to 9, 10, 11 bits. */
123 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x00);
124 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
125 g_assert_cmphex(value, ==, 0x1480);
126
127 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x20);
128 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
129 g_assert_cmphex(value, ==, 0x14c0);
130
131 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x40);
132 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
133 g_assert_cmphex(value, ==, 0x14e0);
134
135 /* stored precision remains the same */
136 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
137 g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
138 g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
139
140 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
141 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
142 g_assert_cmphex(value, ==, 0x14f0);
cebac614 143
a4ec5bb7
PB
144 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
145 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
cebac614
PB
146}
147
6e998903
AF
148int main(int argc, char **argv)
149{
150 QTestState *s = NULL;
151 int ret;
152
153 g_test_init(&argc, &argv, NULL);
154
a4ec5bb7
PB
155 s = qtest_start("-machine n800 "
156 "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
157 ",address=0x49");
6e998903 158 i2c = omap_i2c_create(OMAP2_I2C_1_BASE);
6e998903
AF
159
160 qtest_add_func("/tmp105/tx-rx", send_and_receive);
161
162 ret = g_test_run();
163
164 if (s) {
165 qtest_quit(s);
166 }
167 g_free(i2c);
168
169 return ret;
170}