1 /*
2  * Copyright (c) 2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/i2c.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/ztest.h>
10 
11 /* command to configure port direction of NXP PCA95xx */
12 #define REG_CONF_PORT0 0x06U
13 
14 /* test data used to write into registers */
15 uint8_t test_data[2] = {0xAA, 0xAA};
16 
17 /**
18  * @brief Test i2c api by communicating with pca95xx
19  * @details
20  * - get i2c mainline device
21  * - write data into pca95xx
22  * - read data from pca95xx
23  * - check read data whether correct
24  */
ZTEST(boards_mec172x_pca95xx,test_i2c_pca95xx)25 ZTEST(boards_mec172x_pca95xx, test_i2c_pca95xx)
26 {
27 	int32_t ret;
28 	uint8_t datas[3];
29 	uint32_t i2c_cfg = I2C_SPEED_SET(I2C_SPEED_STANDARD) | I2C_MODE_CONTROLLER;
30 
31 	/* get i2c device */
32 	const struct i2c_dt_spec i2c = I2C_DT_SPEC_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(nxp_pca95xx));
33 
34 	zassert_true(device_is_ready(i2c.bus), "I2C controller device is not ready");
35 
36 	/* configure i2c device */
37 	ret = i2c_configure(i2c.bus, i2c_cfg);
38 	zassert_true(ret == 0, "Failed to configure i2c device");
39 
40 	/* write configuration to register 6 and 7 of PCA95XX*/
41 	(void)memset(datas, 0, 3);
42 	datas[0] = REG_CONF_PORT0;
43 	datas[1] = test_data[0];
44 	datas[2] = test_data[1];
45 	ret = i2c_write_dt(&i2c, datas, 3);
46 	zassert_true(ret == 0, "Failed to write data to i2c device");
47 
48 	/* read configuration from register 6 and 7 */
49 	(void)memset(datas, 0, 3);
50 	datas[0] = REG_CONF_PORT0;
51 	ret = i2c_write_dt(&i2c, datas, 1);
52 	zassert_true(ret == 0, "Failed to write data to i2c device");
53 
54 	(void)memset(datas, 0, 3);
55 	ret = i2c_read_dt(&i2c, datas, 2);
56 	zassert_true(ret == 0, "Failed to read data from i2c device");
57 
58 	/* check read data whether correct */
59 	ret = memcmp(datas, test_data, 2);
60 	zassert_true(ret == 0, "Read data is different to write data");
61 }
62 
63 ZTEST_SUITE(boards_mec172x_pca95xx, NULL, NULL, NULL, NULL, NULL);
64