zephyr_zcu102_iicps_master.c

  1/*
  2 * Copyright (c) 2024 Advanced Micro Devices, Inc.
  3 *
  4 * SPDX-License-Identifier: Apache-2.0
  5 */
  6
  7
  8#include <zephyr/drivers/i2c.h>
  9#include <zephyr/logging/log.h>
 10
 11LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
 12
 13#define PSI2C_I2C_MUX_ADDR     0x74
 14#define PSI2C_I2C_MUX_CHANNEL  0x01U
 15
 16static const struct device *const ps_i2c_dev = DEVICE_DT_GET(DT_NODELABEL(ps_i2c1));
 17
 18static int32_t iicps_master_configure_test(void)
 19{
 20	int32_t ret;
 21	uint32_t i2c_cfg, i2c_rd_cfg;
 22
 23	LOG_INF("IIC-PS: master: configure-test: Start");
 24
 25	/* check if driver is properly initialized */
 26	if (device_is_ready(ps_i2c_dev) == false) {
 27		LOG_INF("PS-I2C: device is NOT_READY");
 28		ret = -ENODEV;
 29		goto out;
 30	}
 31	LOG_INF("PS-I2C: device READY");
 32
 33	/* configure iic speed */
 34	LOG_INF("PS-I2C: master: i2c_configure: Progress");
 35	i2c_cfg = I2C_SPEED_SET(I2C_SPEED_STANDARD) | I2C_MODE_CONTROLLER;
 36	ret = i2c_configure(ps_i2c_dev, i2c_cfg);
 37	if (ret != 0) {
 38		LOG_INF("PS-I2C: master: i2c_configure: failed: %d", ret);
 39		goto out;
 40	}
 41	LOG_INF("PS-I2C: master: i2c_configure: Success");
 42
 43	/* read i2c configuration */
 44	LOG_INF("PS-I2C: master: i2c_get_config: Progress");
 45	i2c_rd_cfg = 0;
 46	ret = i2c_get_config(ps_i2c_dev, &i2c_rd_cfg);
 47	if (ret != 0) {
 48		LOG_INF("PS-I2C: master: i2c_get_config: failed: %d", ret);
 49		goto out;
 50	}
 51	LOG_INF("PS-I2C: master: i2c_get_config: Success");
 52
 53	/* verify if same */
 54	if (i2c_cfg != i2c_rd_cfg) {
 55		LOG_INF("PS-I2C: master: invalid get_config, rd_cfg: %08Xh, exp_cfg: %08Xh",
 56			i2c_rd_cfg, i2c_cfg);
 57		goto out;
 58	}
 59
 60out:
 61	if (ret != 0) {
 62		LOG_INF("IIC-PS: master: configure-test: FAILED");
 63	} else {
 64		LOG_INF("IIC-PS: master: configure-test: PASSED");
 65	}
 66
 67	return ret;
 68}
 69
 70static int32_t iicps_master_transfer_test(void)
 71{
 72	int32_t ret;
 73	struct i2c_msg msg[2];
 74	uint8_t WriteBuffer = 1U;
 75	uint8_t ReadBuffer;
 76
 77	LOG_INF("IIC-PS: master: transfer-test: Start");
 78
 79	/* perform a 1 byte of i2c write and then i2c read */
 80	LOG_INF("PS-I2C: i2c_transfer: write: Progress");
 81	msg[0].buf = &WriteBuffer;
 82	msg[0].len = 1;
 83	msg[0].flags = (uint8_t)I2C_MSG_WRITE;
 84	ret = i2c_transfer(ps_i2c_dev, &msg[0], 1, PSI2C_I2C_MUX_ADDR);
 85	if (ret != 0) {
 86		LOG_INF("PS-I2C: i2c_transfer: write: failed: %d", ret);
 87		goto out;
 88	}
 89	LOG_INF("PS-I2C: i2c_transfer: write: PASSED");
 90
 91	msg[1].buf = &ReadBuffer;
 92	msg[1].len = 1;
 93	msg[1].flags = (uint8_t)(I2C_MSG_READ);
 94	ret = i2c_transfer(ps_i2c_dev, &msg[1], 1, PSI2C_I2C_MUX_ADDR);
 95	if (ret != 0) {
 96		LOG_INF("PS-I2C: i2c_transfer: read: failed: %d", ret);
 97		goto out;
 98	}
 99	LOG_INF("PS-I2C: i2c_transfer: read: PASSED");
100
101	LOG_INF("PS-I2C: i2c_transfer: multi-msg: Progress");
102	ret = i2c_transfer(ps_i2c_dev, &msg[0], 2, PSI2C_I2C_MUX_ADDR);
103	if (ret != 0) {
104		LOG_INF("PS-I2C: i2c_transfer: multi-msg: failed: %d", ret);
105		goto out;
106	}
107	LOG_INF("PS-I2C: i2c_transfer: multi-msg: PASSED");
108
109out:
110	if (ret != 0) {
111		LOG_INF("IIC-PS: master: transfer-test: FAILED");
112	} else {
113		LOG_INF("IIC-PS: master: transfer-test: PASSED");
114	}
115
116	return ret;
117}
118
119static int32_t iicps_master_repstart_test(void)
120{
121	int32_t ret;
122	uint8_t wr_datas[6];
123	uint8_t rd_datas[6];
124
125	LOG_INF("IIC-PS: master: rep-start-test: Start");
126
127	LOG_INF("PS-I2C: master: write-read-rep-start: SLAVE:IIC-MUX Progress");
128	wr_datas[0] = PSI2C_I2C_MUX_CHANNEL;
129	LOG_INF("PS-I2C: master: write-read-rep-start: write data: %02Xh ", wr_datas[0]);
130	(void)memset(rd_datas, 0, sizeof(rd_datas));
131	ret = i2c_write_read(ps_i2c_dev, PSI2C_I2C_MUX_ADDR, wr_datas, 1, rd_datas, 1);
132	if (ret != 0) {
133		LOG_INF("PS-I2C: master: write-read-rep-start: SLAVE:IIC-MUX FAILED, ret:%d", ret);
134		goto out;
135	}
136	LOG_INF("PS-I2C: master: write-read-rep-start: SLAVE:IIC-MUX success");
137	LOG_INF("PS-I2C: master: write-read-rep-start: read data: %02Xh", rd_datas[0]);
138
139out:
140	if (ret == 0) {
141		LOG_INF("IIC-PS: master: rep-start-test: PASSED");
142	} else {
143		LOG_INF("IIC-PS: master: rep-start-test: FAILED");
144	}
145
146	return ret;
147}
148
149static void iicps_master_mode_test(void)
150{
151	int32_t ret;
152
153	LOG_INF("IIC-PS: master mode test: Start");
154
155	ret = iicps_master_configure_test();
156	if (ret != 0) {
157		goto out;
158	}
159
160	ret = iicps_master_transfer_test();
161	if (ret != 0) {
162		goto out;
163	}
164
165	ret = iicps_master_repstart_test();
166	if (ret != 0) {
167		goto out;
168	}
169
170out:
171	if (ret != 0) {
172		LOG_INF("IIC-PS: master mode test: FAILED");
173	} else {
174		LOG_INF("IIC-PS: master mode test: PASSED");
175	}
176}
177
178int main(void)
179{
180	/* master usecase */
181	iicps_master_mode_test();
182
183	return 0;
184}