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.
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
)
26 i2c_send(i2c
, addr
, ®
, 1);
27 i2c_recv(i2c
, addr
, resp
, 1);
31 static uint16_t tmp105_get16(I2CAdapter
*i2c
, uint8_t addr
, uint8_t reg
)
34 i2c_send(i2c
, addr
, ®
, 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
,
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
,
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
)
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");
80 static void qmp_tmp105_set_temperature(const char *id
, int value
)
84 response
= qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
85 "'property': 'temperature', 'value': %d } }", id
, value
);
86 g_assert(qdict_haskey(response
, "return"));
90 #define TMP105_PRECISION (1000/16)
91 static void send_and_receive(void)
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);
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
;
152 g_test_init(&argc
, &argv
, NULL
);
154 s
= qtest_start("-machine n800 "
155 "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
157 i2c
= omap_i2c_create(OMAP2_I2C_1_BASE
);
159 qtest_add_func("/tmp105/tx-rx", send_and_receive
);