1
2
3
4
5
6
7
8
9
10
11
12#include "qemu/osdep.h"
13#include "libqtest-single.h"
14#include "libqos/qgraph.h"
15#include "libqos/i2c.h"
16#include "qapi/qmp/qdict.h"
17
18#define LSM303DLHC_MAG_TEST_ID "lsm303dlhc_mag-test"
19#define LSM303DLHC_MAG_REG_CRA 0x00
20#define LSM303DLHC_MAG_REG_CRB 0x01
21#define LSM303DLHC_MAG_REG_OUT_X_H 0x03
22#define LSM303DLHC_MAG_REG_OUT_Z_H 0x05
23#define LSM303DLHC_MAG_REG_OUT_Y_H 0x07
24#define LSM303DLHC_MAG_REG_IRC 0x0C
25#define LSM303DLHC_MAG_REG_TEMP_OUT_H 0x31
26
27static int qmp_lsm303dlhc_mag_get_property(const char *id, const char *prop)
28{
29 QDict *response;
30 int ret;
31
32 response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
33 "'property': %s } }", id, prop);
34 g_assert(qdict_haskey(response, "return"));
35 ret = qdict_get_int(response, "return");
36 qobject_unref(response);
37 return ret;
38}
39
40static void qmp_lsm303dlhc_mag_set_property(const char *id, const char *prop,
41 int value)
42{
43 QDict *response;
44
45 response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
46 "'property': %s, 'value': %d } }", id, prop, value);
47 g_assert(qdict_haskey(response, "return"));
48 qobject_unref(response);
49}
50
51static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
52{
53 int64_t value;
54 QI2CDevice *i2cdev = (QI2CDevice *)obj;
55
56
57 g_assert_cmphex(i2c_get8(i2cdev, LSM303DLHC_MAG_REG_CRB), ==, 0x20);
58
59
60 qmp_lsm303dlhc_mag_set_property(LSM303DLHC_MAG_TEST_ID, "mag-x", 100000);
61 value = qmp_lsm303dlhc_mag_get_property(
62 LSM303DLHC_MAG_TEST_ID, "mag-x");
63 g_assert_cmpint(value, ==, 100000);
64
65
66 qmp_lsm303dlhc_mag_set_property(LSM303DLHC_MAG_TEST_ID, "mag-y", 150000);
67 value = qmp_lsm303dlhc_mag_get_property(
68 LSM303DLHC_MAG_TEST_ID, "mag-y");
69 g_assert_cmpint(value, ==, 150000);
70
71
72 qmp_lsm303dlhc_mag_set_property(LSM303DLHC_MAG_TEST_ID, "mag-z", 50000);
73 value = qmp_lsm303dlhc_mag_get_property(
74 LSM303DLHC_MAG_TEST_ID, "mag-z");
75 g_assert_cmpint(value, ==, 50000);
76
77
78 qmp_lsm303dlhc_mag_set_property(LSM303DLHC_MAG_TEST_ID,
79 "temperature", 23600);
80 value = qmp_lsm303dlhc_mag_get_property(
81 LSM303DLHC_MAG_TEST_ID, "temperature");
82
83 g_assert_cmpint(value, ==, 23500);
84
85
86 value = i2c_get16(i2cdev, LSM303DLHC_MAG_REG_OUT_X_H);
87 g_assert_cmphex(value, ==, 1100);
88
89
90 value = i2c_get16(i2cdev, LSM303DLHC_MAG_REG_OUT_Y_H);
91 g_assert_cmphex(value, ==, 1650);
92
93
94 value = i2c_get16(i2cdev, LSM303DLHC_MAG_REG_OUT_Z_H);
95 g_assert_cmphex(value, ==, 490);
96
97
98 value = i2c_get16(i2cdev, LSM303DLHC_MAG_REG_TEMP_OUT_H);
99 g_assert_cmphex(value, ==, 0);
100
101
102 i2c_set8(i2cdev, LSM303DLHC_MAG_REG_CRA, 0x90);
103
104
105 value = i2c_get16(i2cdev, LSM303DLHC_MAG_REG_TEMP_OUT_H);
106 g_assert_cmphex(value, ==, 188);
107}
108
109static void reg_wraparound(void *obj, void *data, QGuestAllocator *alloc)
110{
111 uint8_t value[4];
112 QI2CDevice *i2cdev = (QI2CDevice *)obj;
113
114
115 qmp_lsm303dlhc_mag_set_property(LSM303DLHC_MAG_TEST_ID, "mag-x", 100000);
116 qmp_lsm303dlhc_mag_set_property(LSM303DLHC_MAG_TEST_ID, "mag-y", 150000);
117
118
119 i2c_read_block(i2cdev, LSM303DLHC_MAG_REG_OUT_Y_H, value, 4);
120
121 g_assert_cmphex(value[0], ==, 0x06);
122 g_assert_cmphex(value[1], ==, 0x72);
123
124 g_assert_cmphex(value[2], ==, 0x04);
125 g_assert_cmphex(value[3], ==, 0x4C);
126
127
128 i2c_read_block(i2cdev, LSM303DLHC_MAG_REG_IRC, value, 2);
129
130 g_assert_cmphex(value[0], ==, 0x33);
131
132 g_assert_cmphex(value[1], ==, 0x10);
133}
134
135static void lsm303dlhc_mag_register_nodes(void)
136{
137 QOSGraphEdgeOptions opts = {
138 .extra_device_opts = "id=" LSM303DLHC_MAG_TEST_ID ",address=0x1e"
139 };
140 add_qi2c_address(&opts, &(QI2CAddress) { 0x1E });
141
142 qos_node_create_driver("lsm303dlhc_mag", i2c_device_create);
143 qos_node_consumes("lsm303dlhc_mag", "i2c-bus", &opts);
144
145 qos_add_test("tx-rx", "lsm303dlhc_mag", send_and_receive, NULL);
146 qos_add_test("regwrap", "lsm303dlhc_mag", reg_wraparound, NULL);
147}
148libqos_init(lsm303dlhc_mag_register_nodes);
149