xref: /qemu/tests/qtest/tmp105-test.c (revision b355f08a)
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  */
9 
10 #include "qemu/osdep.h"
11 
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"
17 
18 #define TMP105_TEST_ID   "tmp105-test"
19 #define TMP105_TEST_ADDR 0x49
20 
21 static int qmp_tmp105_get_temperature(const char *id)
22 {
23     QDict *response;
24     int ret;
25 
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);
31     return ret;
32 }
33 
34 static void qmp_tmp105_set_temperature(const char *id, int value)
35 {
36     QDict *response;
37 
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);
42 }
43 
44 #define TMP105_PRECISION (1000/16)
45 static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
46 {
47     uint16_t value;
48     QI2CDevice *i2cdev = (QI2CDevice *)obj;
49 
50     value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
51     g_assert_cmpuint(value, ==, 0);
52 
53     value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
54     g_assert_cmphex(value, ==, 0);
55 
56     qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
57     value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
58     g_assert_cmpuint(value, ==, 20000);
59 
60     value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
61     g_assert_cmphex(value, ==, 0x1400);
62 
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);
67 
68     /* Set config */
69     i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
70     value = i2c_get8(i2cdev, TMP105_REG_CONFIG);
71     g_assert_cmphex(value, ==, 0x60);
72 
73     value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
74     g_assert_cmphex(value, ==, 0x14f0);
75 
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);
81 
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);
86 
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);
91 
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);
96 
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);
101 
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);
106 }
107 
108 static void tmp105_register_nodes(void)
109 {
110     QOSGraphEdgeOptions opts = {
111         .extra_device_opts = "id=" TMP105_TEST_ID ",address=0x49"
112     };
113     add_qi2c_address(&opts, &(QI2CAddress) { 0x49 });
114 
115     qos_node_create_driver("tmp105", i2c_device_create);
116     qos_node_consumes("tmp105", "i2c-bus", &opts);
117 
118     qos_add_test("tx-rx", "tmp105", send_and_receive, NULL);
119 }
120 libqos_init(tmp105_register_nodes);
121