samples: i2c_scanner: support output like i2c_detect

Supports output in the same format as the i2c_detect command.

Signed-off-by: Kwon Tae-young <tykwon@m2i.co.kr>
This commit is contained in:
Kwon Tae-young 2019-12-13 10:16:35 +09:00 committed by Carles Cufí
commit 94e419c624

View file

@ -23,9 +23,9 @@
void main(void) void main(void)
{ {
struct device *i2c_dev; struct device *i2c_dev;
u8_t cnt = 0; u8_t cnt = 0, first = 0x04, last = 0x77;
printk("Starting i2c scanner...\n"); printk("Starting i2c scanner...\n\n");
i2c_dev = device_get_binding(I2C_DEV); i2c_dev = device_get_binding(I2C_DEV);
if (!i2c_dev) { if (!i2c_dev) {
@ -33,20 +33,32 @@ void main(void)
return; return;
} }
for (u8_t i = 4; i <= 0x77; i++) { printk(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
struct i2c_msg msgs[1]; for (u8_t i = 0; i <= last; i += 16) {
u8_t dst; printk("%02x: ", i);
for (u8_t j = 0; j < 16; j++) {
if (i + j < first || i + j > last) {
printk(" ");
continue;
}
/* Send the address to read from */ struct i2c_msg msgs[1];
msgs[0].buf = &dst; u8_t dst;
msgs[0].len = 0U;
msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
if (i2c_transfer(i2c_dev, &msgs[0], 1, i) == 0) { /* Send the address to read from */
printk("0x%2x FOUND\n", i); msgs[0].buf = &dst;
++cnt; msgs[0].len = 0U;
msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
if (i2c_transfer(i2c_dev, &msgs[0], 1, i + j) == 0) {
printk("%02x ", i + j);
++cnt;
} else {
printk("-- ");
}
} }
printk("\n");
} }
printk("%u devices found on %s\n", cnt, I2C_DEV);
printk("\n%u devices found on %s\n", cnt, I2C_DEV);
} }