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(DT_ALIAS(i2c_0), okay)
20 #define I2C_DEV_NODE	DT_ALIAS(i2c_0)
21 #elif DT_NODE_HAS_STATUS(DT_ALIAS(i2c_1), okay)
22 #define I2C_DEV_NODE	DT_ALIAS(i2c_1)
23 #elif DT_NODE_HAS_STATUS(DT_ALIAS(i2c_2), okay)
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 
test_gy271(void)31 static int test_gy271(void)
32 {
33 	unsigned char datas[6];
34 	const struct device *const i2c_dev = DEVICE_DT_GET(I2C_DEV_NODE);
35 	uint32_t i2c_cfg_tmp;
36 
37 	if (!device_is_ready(i2c_dev)) {
38 		TC_PRINT("I2C device is not ready\n");
39 		return TC_FAIL;
40 	}
41 
42 	/* 1. Verify i2c_configure() */
43 	if (i2c_configure(i2c_dev, i2c_cfg)) {
44 		TC_PRINT("I2C config failed\n");
45 		return TC_FAIL;
46 	}
47 
48 	/* 2. Verify i2c_get_config() */
49 	if (i2c_get_config(i2c_dev, &i2c_cfg_tmp)) {
50 		TC_PRINT("I2C get_config failed\n");
51 		return TC_FAIL;
52 	}
53 	if (i2c_cfg != i2c_cfg_tmp) {
54 		TC_PRINT("I2C get_config returned invalid config\n");
55 		return TC_FAIL;
56 	}
57 
58 	datas[0] = 0x01;
59 	datas[1] = 0x20;
60 
61 	/* 3. verify i2c_write() */
62 	if (i2c_write(i2c_dev, datas, 2, 0x1E)) {
63 		TC_PRINT("Fail to configure sensor GY271\n");
64 		return TC_FAIL;
65 	}
66 
67 	datas[0] = 0x02;
68 	datas[1] = 0x00;
69 	if (i2c_write(i2c_dev, datas, 2, 0x1E)) {
70 		TC_PRINT("Fail to configure sensor GY271\n");
71 		return TC_FAIL;
72 	}
73 
74 	k_sleep(K_MSEC(1));
75 
76 	datas[0] = 0x03;
77 	if (i2c_write(i2c_dev, datas, 1, 0x1E)) {
78 		TC_PRINT("Fail to write to sensor GY271\n");
79 		return TC_FAIL;
80 	}
81 
82 	(void)memset(datas, 0, sizeof(datas));
83 
84 	/* 4. verify i2c_read() */
85 	if (i2c_read(i2c_dev, datas, 6, 0x1E)) {
86 		TC_PRINT("Fail to fetch sample from sensor GY271\n");
87 		return TC_FAIL;
88 	}
89 
90 	TC_PRINT("axis raw data: %d %d %d %d %d %d\n",
91 				datas[0], datas[1], datas[2],
92 				datas[3], datas[4], datas[5]);
93 
94 	return TC_PASS;
95 }
96 
test_burst_gy271(void)97 static int test_burst_gy271(void)
98 {
99 	unsigned char datas[6];
100 	const struct device *const i2c_dev = DEVICE_DT_GET(I2C_DEV_NODE);
101 	uint32_t i2c_cfg_tmp;
102 
103 	if (!device_is_ready(i2c_dev)) {
104 		TC_PRINT("I2C device is not ready\n");
105 		return TC_FAIL;
106 	}
107 
108 	/* 1. verify i2c_configure() */
109 	if (i2c_configure(i2c_dev, i2c_cfg)) {
110 		TC_PRINT("I2C config failed\n");
111 		return TC_FAIL;
112 	}
113 
114 	/* 2. Verify i2c_get_config() */
115 	if (i2c_get_config(i2c_dev, &i2c_cfg_tmp)) {
116 		TC_PRINT("I2C get_config failed\n");
117 		return TC_FAIL;
118 	}
119 	if (i2c_cfg != i2c_cfg_tmp) {
120 		TC_PRINT("I2C get_config returned invalid config\n");
121 		return TC_FAIL;
122 	}
123 
124 	datas[0] = 0x01;
125 	datas[1] = 0x20;
126 	datas[2] = 0x02;
127 	datas[3] = 0x00;
128 
129 	/* 3. verify i2c_burst_write() */
130 	if (i2c_burst_write(i2c_dev, 0x1E, 0x00, datas, 4)) {
131 		TC_PRINT("Fail to write to sensor GY271\n");
132 		return TC_FAIL;
133 	}
134 
135 	k_sleep(K_MSEC(1));
136 
137 	(void)memset(datas, 0, sizeof(datas));
138 
139 	/* 4. verify i2c_burst_read() */
140 	if (i2c_burst_read(i2c_dev, 0x1E, 0x03, datas, 6)) {
141 		TC_PRINT("Fail to fetch sample from sensor GY271\n");
142 		return TC_FAIL;
143 	}
144 
145 	TC_PRINT("axis raw data: %d %d %d %d %d %d\n",
146 				datas[0], datas[1], datas[2],
147 				datas[3], datas[4], datas[5]);
148 
149 	return TC_PASS;
150 }
151 
ZTEST(i2c_gy271,test_i2c_gy271)152 ZTEST(i2c_gy271, test_i2c_gy271)
153 {
154 	zassert_true(test_gy271() == TC_PASS);
155 }
156 
ZTEST(i2c_gy271,test_i2c_burst_gy271)157 ZTEST(i2c_gy271, test_i2c_burst_gy271)
158 {
159 	zassert_true(test_burst_gy271() == TC_PASS);
160 }
161 
162 ZTEST_SUITE(i2c_gy271, NULL, NULL, NULL, NULL, NULL);
163