zephyr_tenzing2_se7_iicps_master.c

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