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.
10 #include "qemu/osdep.h"
12 #include "libqtest-single.h"
13 #include "libqos/qgraph.h"
14 #include "libqos/i2c.h"
15 #include "qapi/qmp/qdict.h"
16 #include "hw/sensor/tmp105_regs.h"
18 #define TMP105_TEST_ID "tmp105-test"
19 #define TMP105_TEST_ADDR 0x49
21 static int qmp_tmp105_get_temperature(const char *id)
26 response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
27 "'property': 'temperature' } }", id);
28 g_assert(qdict_haskey(response, "return"));
29 ret = qdict_get_int(response, "return");
30 qobject_unref(response);
34 static void qmp_tmp105_set_temperature(const char *id, int value)
38 response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
39 "'property': 'temperature', 'value': %d } }", id, value);
40 g_assert(qdict_haskey(response, "return"));
41 qobject_unref(response);
44 #define TMP105_PRECISION (1000/16)
45 static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
48 QI2CDevice *i2cdev = (QI2CDevice *)obj;
50 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
51 g_assert_cmpuint(value, ==, 0);
53 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
54 g_assert_cmphex(value, ==, 0);
56 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
57 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
58 g_assert_cmpuint(value, ==, 20000);
60 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
61 g_assert_cmphex(value, ==, 0x1400);
63 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */
64 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
65 g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
66 g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
69 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
70 value = i2c_get8(i2cdev, TMP105_REG_CONFIG);
71 g_assert_cmphex(value, ==, 0x60);
73 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
74 g_assert_cmphex(value, ==, 0x14f0);
76 /* Set precision to 9, 10, 11 bits. */
77 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x00);
78 g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x00);
79 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
80 g_assert_cmphex(value, ==, 0x1480);
82 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x20);
83 g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x20);
84 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
85 g_assert_cmphex(value, ==, 0x14c0);
87 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x40);
88 g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x40);
89 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
90 g_assert_cmphex(value, ==, 0x14e0);
92 /* stored precision remains the same */
93 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
94 g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
95 g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
97 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
98 g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x60);
99 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
100 g_assert_cmphex(value, ==, 0x14f0);
102 i2c_set16(i2cdev, TMP105_REG_T_LOW, 0x1234);
103 g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_LOW), ==, 0x1234);
104 i2c_set16(i2cdev, TMP105_REG_T_HIGH, 0x4231);
105 g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_HIGH), ==, 0x4231);
108 static void tmp105_register_nodes(void)
110 QOSGraphEdgeOptions opts = {
111 .extra_device_opts = "id=" TMP105_TEST_ID ",address=0x49"
113 add_qi2c_address(&opts, &(QI2CAddress) { 0x49 });
115 qos_node_create_driver("tmp105", i2c_device_create);
116 qos_node_consumes("tmp105", "i2c-bus", &opts);
118 qos_add_test("tx-rx", "tmp105", send_and_receive, NULL);
120 libqos_init(tmp105_register_nodes);