1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /*
8 * @addtogroup t_i2c_basic
9 * @{
10 * @defgroup t_i2c_read_write test_i2c_read_write
11 * @brief TestPurpose: verify I2C master can read and write
12 * @}
13 */
14
15 #include <zephyr/drivers/i2c.h>
16 #include <zephyr/kernel.h>
17 #include <zephyr/ztest.h>
18
19 #if DT_NODE_HAS_STATUS_OKAY(DT_ALIAS(i2c_0))
20 #define I2C_DEV_NODE DT_ALIAS(i2c_0)
21 #elif DT_NODE_HAS_STATUS_OKAY(DT_ALIAS(i2c_1))
22 #define I2C_DEV_NODE DT_ALIAS(i2c_1)
23 #elif DT_NODE_HAS_STATUS_OKAY(DT_ALIAS(i2c_2))
24 #define I2C_DEV_NODE DT_ALIAS(i2c_2)
25 #else
26 #error "Please set the correct I2C device"
27 #endif
28
29 uint32_t i2c_cfg = I2C_SPEED_SET(I2C_SPEED_STANDARD) | I2C_MODE_CONTROLLER;
30
31 #define GY271_HMC_ADDR (0x1E)
32 #define GY271_QMC_ADDR (0x0D)
33
34 #if defined(CONFIG_SENSOR_GY271_QMC)
35 #define GY271_ADDR GY271_QMC_ADDR
36 #elif defined(CONFIG_SENSOR_GY271_HMC)
37 #define GY271_ADDR GY271_HMC_ADDR
38 #else
39 #error "No sensor type defined"
40 #endif
41
test_gy271(void)42 static int test_gy271(void)
43 {
44 unsigned char datas[6];
45 const struct device *const i2c_dev = DEVICE_DT_GET(I2C_DEV_NODE);
46 uint32_t i2c_cfg_tmp;
47
48 if (!device_is_ready(i2c_dev)) {
49 TC_PRINT("I2C device is not ready\n");
50 return TC_FAIL;
51 }
52
53 /* 1. Verify i2c_configure() */
54 if (i2c_configure(i2c_dev, i2c_cfg)) {
55 TC_PRINT("I2C config failed\n");
56 return TC_FAIL;
57 }
58
59 /* 2. Verify i2c_get_config() */
60 if (i2c_get_config(i2c_dev, &i2c_cfg_tmp)) {
61 TC_PRINT("I2C get_config failed\n");
62 return TC_FAIL;
63 }
64 if (i2c_cfg != i2c_cfg_tmp) {
65 TC_PRINT("I2C get_config returned invalid config\n");
66 return TC_FAIL;
67 }
68
69 #ifdef CONFIG_SENSOR_GY271_QMC
70 datas[0] = 0x09;
71 datas[1] = 0x01;
72
73 if (i2c_write(i2c_dev, datas, 2, GY271_ADDR)) {
74 TC_PRINT("Fail to configure sensor GY271\n");
75 return TC_FAIL;
76 }
77 #else /* GY271 HMC */
78 datas[0] = 0x01;
79 datas[1] = 0x20;
80
81 /* 3. verify i2c_write() */
82 if (i2c_write(i2c_dev, datas, 2, GY271_ADDR)) {
83 TC_PRINT("Fail to configure sensor GY271\n");
84 return TC_FAIL;
85 }
86
87 datas[0] = 0x02;
88 datas[1] = 0x00;
89 if (i2c_write(i2c_dev, datas, 2, GY271_ADDR)) {
90 TC_PRINT("Fail to configure sensor GY271\n");
91 return TC_FAIL;
92 }
93 #endif
94
95 k_sleep(K_MSEC(1));
96
97 #ifdef CONFIG_SENSOR_GY271_QMC
98 /* Sensor data bits start from 0x00 to 0x05 */
99 datas[0] = 0x00;
100 #else /* GY271 HMC */
101 datas[0] = 0x03;
102 #endif
103
104 if (i2c_write(i2c_dev, datas, 1, GY271_ADDR)) {
105 TC_PRINT("Fail to write to sensor GY271\n");
106 return TC_FAIL;
107 }
108
109 (void)memset(datas, 0, sizeof(datas));
110
111 /* 4. verify i2c_read() */
112 if (i2c_read(i2c_dev, datas, 6, GY271_ADDR)) {
113 TC_PRINT("Fail to fetch sample from sensor GY271\n");
114 return TC_FAIL;
115 }
116
117 TC_PRINT("axis raw data: %d %d %d %d %d %d\n", datas[0], datas[1], datas[2], datas[3],
118 datas[4], datas[5]);
119
120 return TC_PASS;
121 }
122
test_burst_gy271(void)123 static int test_burst_gy271(void)
124 {
125 unsigned char datas[6];
126 const struct device *const i2c_dev = DEVICE_DT_GET(I2C_DEV_NODE);
127 uint32_t i2c_cfg_tmp;
128
129 if (!device_is_ready(i2c_dev)) {
130 TC_PRINT("I2C device is not ready\n");
131 return TC_FAIL;
132 }
133
134 /* 1. verify i2c_configure() */
135 if (i2c_configure(i2c_dev, i2c_cfg)) {
136 TC_PRINT("I2C config failed\n");
137 return TC_FAIL;
138 }
139
140 /* 2. Verify i2c_get_config() */
141 if (i2c_get_config(i2c_dev, &i2c_cfg_tmp)) {
142 TC_PRINT("I2C get_config failed\n");
143 return TC_FAIL;
144 }
145 if (i2c_cfg != i2c_cfg_tmp) {
146 TC_PRINT("I2C get_config returned invalid config\n");
147 return TC_FAIL;
148 }
149
150 #ifdef CONFIG_SENSOR_GY271_QMC
151 datas[0] = 0x09;
152 datas[1] = 0x01;
153
154 if (i2c_burst_write(i2c_dev, GY271_ADDR, 0x00, datas, 2)) {
155 TC_PRINT("Fail to configure sensor GY271 QMC\n");
156 }
157 #else
158 datas[0] = 0x01;
159 datas[1] = 0x20;
160 datas[2] = 0x02;
161 datas[3] = 0x00;
162
163 /* 3. verify i2c_burst_write() */
164 if (i2c_burst_write(i2c_dev, GY271_ADDR, 0x00, datas, 4)) {
165 TC_PRINT("Fail to write to sensor GY271\n");
166 return TC_FAIL;
167 }
168
169 k_sleep(K_MSEC(1));
170
171 (void)memset(datas, 0, sizeof(datas));
172 #endif
173
174 #ifdef CONFIG_SENSOR_GY271_QMC
175 /* Sensor data bits start from 0x00 to 0x05 */
176 int start_bit = 0x00;
177 #else /* GY271 HMC */
178 int start_bit = 0x03;
179 #endif
180 /* 4. verify i2c_burst_read() */
181 if (i2c_burst_read(i2c_dev, GY271_ADDR, start_bit, datas, 6)) {
182 TC_PRINT("Fail to fetch sample from sensor GY271\n");
183 return TC_FAIL;
184 }
185
186 TC_PRINT("axis raw data: %d %d %d %d %d %d\n", datas[0], datas[1], datas[2], datas[3],
187 datas[4], datas[5]);
188
189 return TC_PASS;
190 }
191
ZTEST(i2c_gy271,test_i2c_gy271)192 ZTEST(i2c_gy271, test_i2c_gy271)
193 {
194 zassert_true(test_gy271() == TC_PASS);
195 }
196
ZTEST(i2c_gy271,test_i2c_burst_gy271)197 ZTEST(i2c_gy271, test_i2c_burst_gy271)
198 {
199 zassert_true(test_burst_gy271() == TC_PASS);
200 }
201
202 ZTEST_SUITE(i2c_gy271, NULL, NULL, NULL, NULL, NULL);
203