Skip to content

Commit db1f14e

Browse files
bjarki-andreasendanieldegrasse
authored andcommitted
tests: drivers: i2c: target_api: add device_deinit test case
Add device_deinit test case which, in case there are two devices using gpio loopbacks, deinits both controller and target devices, then uses gpio to configure and toggle the pins of the bus, then reinitializes both devices. Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
1 parent f22ffee commit db1f14e

File tree

1 file changed

+57
-0
lines changed
  • tests/drivers/i2c/i2c_target_api/src

1 file changed

+57
-0
lines changed

tests/drivers/i2c/i2c_target_api/src/main.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <zephyr/drivers/i2c.h>
1818
#include <zephyr/drivers/i2c/target/eeprom.h>
19+
#include <zephyr/drivers/gpio.h>
1920

2021
#include <zephyr/ztest.h>
2122

@@ -166,6 +167,62 @@ static int run_program_read(const struct device *i2c, uint8_t addr,
166167
return 0;
167168
}
168169

170+
ZTEST(i2c_eeprom_target, test_deinit)
171+
{
172+
const struct device *const i2c_0 = DEVICE_DT_GET(DT_BUS(NODE_EP0));
173+
const struct device *const i2c_1 = DEVICE_DT_GET(DT_BUS(NODE_EP1));
174+
const struct gpio_dt_spec sda_pin_0 =
175+
GPIO_DT_SPEC_GET_OR(DT_PATH(zephyr_user), sda0_gpios, {});
176+
const struct gpio_dt_spec scl_pin_0 =
177+
GPIO_DT_SPEC_GET_OR(DT_PATH(zephyr_user), scl0_gpios, {});
178+
const struct gpio_dt_spec sda_pin_1 =
179+
GPIO_DT_SPEC_GET_OR(DT_PATH(zephyr_user), sda1_gpios, {});
180+
const struct gpio_dt_spec scl_pin_1 =
181+
GPIO_DT_SPEC_GET_OR(DT_PATH(zephyr_user), scl1_gpios, {});
182+
int ret;
183+
184+
if (i2c_0 == i2c_1) {
185+
TC_PRINT(" gpio loopback required for test\n");
186+
ztest_test_skip();
187+
}
188+
189+
if (scl_pin_0.port == NULL || sda_pin_0.port == NULL ||
190+
scl_pin_1.port == NULL || sda_pin_1.port == NULL) {
191+
TC_PRINT(" bus gpios not specified in zephyr,path\n");
192+
ztest_test_skip();
193+
}
194+
195+
ret = device_deinit(i2c_0);
196+
if (ret == -ENOTSUP) {
197+
TC_PRINT(" device deinit not supported\n");
198+
ztest_test_skip();
199+
}
200+
201+
zassert_ok(ret);
202+
203+
ret = device_deinit(i2c_1);
204+
if (ret == -ENOTSUP) {
205+
TC_PRINT(" device deinit not supported\n");
206+
zassert_ok(device_init(i2c_0));
207+
ztest_test_skip();
208+
}
209+
210+
zassert_ok(gpio_pin_configure_dt(&sda_pin_0, GPIO_INPUT));
211+
zassert_ok(gpio_pin_configure_dt(&sda_pin_1, GPIO_OUTPUT_INACTIVE));
212+
zassert_ok(gpio_pin_configure_dt(&scl_pin_0, GPIO_INPUT));
213+
zassert_ok(gpio_pin_configure_dt(&scl_pin_1, GPIO_OUTPUT_INACTIVE));
214+
zassert_equal(gpio_pin_get_dt(&sda_pin_0), 0);
215+
zassert_equal(gpio_pin_get_dt(&scl_pin_0), 0);
216+
zassert_ok(gpio_pin_set_dt(&sda_pin_1, 1));
217+
zassert_ok(gpio_pin_set_dt(&scl_pin_1, 1));
218+
zassert_equal(gpio_pin_get_dt(&sda_pin_0), 1);
219+
zassert_equal(gpio_pin_get_dt(&scl_pin_0), 1);
220+
zassert_ok(gpio_pin_configure_dt(&sda_pin_1, GPIO_INPUT));
221+
zassert_ok(gpio_pin_configure_dt(&scl_pin_1, GPIO_INPUT));
222+
zassert_ok(device_init(i2c_0));
223+
zassert_ok(device_init(i2c_1));
224+
}
225+
169226
ZTEST(i2c_eeprom_target, test_eeprom_target)
170227
{
171228
const struct device *const eeprom_0 = DEVICE_DT_GET(NODE_EP0);

0 commit comments

Comments
 (0)