Skip to content

Commit 6a7b8f9

Browse files
nordic-babunordic-piks
authored andcommitted
tests: drivers: i2c: twim_instances: add scan i2c test.
Extend twim_instance tests with i2c scan of each bus. Test checks if there are no unexpected devices detected. Based on 'scan' command from i2c shell. Signed-off-by: Bartlomiej Buczek <[email protected]>
1 parent edce342 commit 6a7b8f9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/drivers/i2c/twim_instances/src/twim_instances.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ static void test_configure_instance(const struct device *dev)
7878
zassert_equal(-EIO, ret, "%s: i2c_write() failed (%d)", dev->name, ret);
7979
}
8080

81+
82+
/**
83+
* Test if any unexpected device can be detected on instance bus.
84+
* SMBus and i2c scan shell command based.
85+
*/
86+
static void test_scan_instance(const struct device *dev)
87+
{
88+
uint8_t first_addr = 0x00, last_addr = 0x7F;
89+
uint8_t dev_cnt = 0;
90+
91+
struct i2c_msg msg = {
92+
.buf = NULL,
93+
.len = 0U,
94+
.flags = I2C_MSG_WRITE | I2C_MSG_STOP
95+
};
96+
97+
for (uint8_t addr = first_addr; addr <= last_addr; addr += 1) {
98+
if (i2c_transfer(dev, &msg, 1, addr) == 0) {
99+
dev_cnt = dev_cnt + 1;
100+
}
101+
}
102+
zassert_equal(dev_cnt, 0,
103+
"i2c scan found %d unexpected devices on %s bus\n",
104+
dev_cnt, dev->name);
105+
}
106+
81107
static bool test_configure_capable(const struct device *dev)
82108
{
83109
return true;
@@ -88,6 +114,11 @@ ZTEST(twim_instances, test_configure)
88114
test_all_instances(test_configure_instance, test_configure_capable);
89115
}
90116

117+
ZTEST(twim_instances, test_scan)
118+
{
119+
test_all_instances(test_scan_instance, test_configure_capable);
120+
}
121+
91122
static void *suite_setup(void)
92123
{
93124
int i;

0 commit comments

Comments
 (0)