]> git.proxmox.com Git - mirror_qemu.git/blob - tests/tmp105-test.c
tmp105-test: Add a second sensor and test that one
[mirror_qemu.git] / tests / tmp105-test.c
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 <glib.h>
11
12 #include "libqtest.h"
13 #include "libqos/i2c.h"
14 #include "hw/misc/tmp105_regs.h"
15
16 #define OMAP2_I2C_1_BASE 0x48070000
17
18 #define TMP105_TEST_ID "tmp105-test"
19 #define TMP105_TEST_ADDR 0x49
20
21 static I2CAdapter *i2c;
22
23 static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
24 {
25 uint8_t resp[2];
26 i2c_send(i2c, addr, &reg, 1);
27 i2c_recv(i2c, addr, resp, 2);
28 return (resp[0] << 8) | resp[1];
29 }
30
31 static void tmp105_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
32 uint8_t value)
33 {
34 uint8_t cmd[2];
35 uint8_t resp[1];
36
37 cmd[0] = reg;
38 cmd[1] = value;
39 i2c_send(i2c, addr, cmd, 2);
40 i2c_recv(i2c, addr, resp, 1);
41 g_assert_cmphex(resp[0], ==, cmd[1]);
42 }
43
44 static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
45 uint16_t value)
46 {
47 uint8_t cmd[3];
48 uint8_t resp[2];
49
50 cmd[0] = reg;
51 cmd[1] = value >> 8;
52 cmd[2] = value & 255;
53 i2c_send(i2c, addr, cmd, 3);
54 i2c_recv(i2c, addr, resp, 2);
55 g_assert_cmphex(resp[0], ==, cmd[1]);
56 g_assert_cmphex(resp[1], ==, cmd[2]);
57 }
58
59
60 static void send_and_receive(void)
61 {
62 uint16_t value;
63
64 value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
65 g_assert_cmpuint(value, ==, 0);
66
67 /* reset */
68 tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0);
69
70 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
71 tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
72 }
73
74 int main(int argc, char **argv)
75 {
76 QTestState *s = NULL;
77 int ret;
78
79 g_test_init(&argc, &argv, NULL);
80
81 s = qtest_start("-machine n800 "
82 "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
83 ",address=0x49");
84 i2c = omap_i2c_create(OMAP2_I2C_1_BASE);
85
86 qtest_add_func("/tmp105/tx-rx", send_and_receive);
87
88 ret = g_test_run();
89
90 if (s) {
91 qtest_quit(s);
92 }
93 g_free(i2c);
94
95 return ret;
96 }