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