target-i386: Enable "check" mode by default
[qemu/cris-port.git] / tests / tmp105-test.c
blob99db5381917993e0512364c4866272cde464b8f0
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 <glib.h>
12 #include "libqtest.h"
13 #include "libqos/i2c.h"
14 #include "hw/misc/tmp105_regs.h"
16 #define OMAP2_I2C_1_BASE 0x48070000
18 #define TMP105_TEST_ID "tmp105-test"
19 #define TMP105_TEST_ADDR 0x49
21 static I2CAdapter *i2c;
23 static uint16_t tmp105_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
25 uint8_t resp[1];
26 i2c_send(i2c, addr, &reg, 1);
27 i2c_recv(i2c, addr, resp, 1);
28 return resp[0];
31 static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
33 uint8_t resp[2];
34 i2c_send(i2c, addr, &reg, 1);
35 i2c_recv(i2c, addr, resp, 2);
36 return (resp[0] << 8) | resp[1];
39 static void tmp105_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
40 uint8_t value)
42 uint8_t cmd[2];
43 uint8_t resp[1];
45 cmd[0] = reg;
46 cmd[1] = value;
47 i2c_send(i2c, addr, cmd, 2);
48 i2c_recv(i2c, addr, resp, 1);
49 g_assert_cmphex(resp[0], ==, cmd[1]);
52 static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
53 uint16_t value)
55 uint8_t cmd[3];
56 uint8_t resp[2];
58 cmd[0] = reg;
59 cmd[1] = value >> 8;
60 cmd[2] = value & 255;
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]);
67 static int qmp_tmp105_get_temperature(const char *id)
69 QDict *response;
70 int ret;
72 response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
73 "'property': 'temperature' } }", id);
74 g_assert(qdict_haskey(response, "return"));
75 ret = qdict_get_int(response, "return");
76 QDECREF(response);
77 return ret;
80 static void qmp_tmp105_set_temperature(const char *id, int value)
82 QDict *response;
84 response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
85 "'property': 'temperature', 'value': %d } }", id, value);
86 g_assert(qdict_haskey(response, "return"));
87 QDECREF(response);
90 #define TMP105_PRECISION (1000/16)
91 static void send_and_receive(void)
93 uint16_t value;
95 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
96 g_assert_cmpuint(value, ==, 0);
98 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
99 g_assert_cmphex(value, ==, 0);
101 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
102 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
103 g_assert_cmpuint(value, ==, 20000);
105 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
106 g_assert_cmphex(value, ==, 0x1400);
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);
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);
118 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
119 g_assert_cmphex(value, ==, 0x14f0);
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);
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);
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);
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);
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);
143 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
144 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
147 int main(int argc, char **argv)
149 QTestState *s = NULL;
150 int ret;
152 g_test_init(&argc, &argv, NULL);
154 s = qtest_start("-machine n800 "
155 "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
156 ",address=0x49");
157 i2c = omap_i2c_create(OMAP2_I2C_1_BASE);
159 qtest_add_func("/tmp105/tx-rx", send_and_receive);
161 ret = g_test_run();
163 if (s) {
164 qtest_quit(s);
166 g_free(i2c);
168 return ret;