i2c: add filtering of i2c dumped messages

This commit adds option to dump i2c messages of only specified
devices. It makes it easier to debug communication of specific
i2c device instead of logging all i2c communication.
The filter of devices is specifiec in device-tree using the
node with "zephyr,i2c-dump-filter" compatible string.

Example of device-tree node:
i2c-dump-filter {
	compatible = "zephyr,i2c-dump-filter";
	devices = < &display0 >, < &sensor3 >;
};

Signed-off-by: Michał Barnaś <mb@semihalf.com>
This commit is contained in:
Michał Barnaś 2023-03-31 18:57:46 +02:00 committed by Carles Cufí
commit 2bc7dcdc2e
3 changed files with 61 additions and 1 deletions

View file

@ -30,12 +30,26 @@ config I2C_STATS
Enable I2C Stats.
config I2C_DUMP_MESSAGES
bool "Log all I2C transactions"
bool "Log I2C transactions"
depends on LOG
depends on I2C_LOG_LEVEL_DBG
help
Dump every I2C transaction to the system log as debug level log messages.
config I2C_DUMP_MESSAGES_ALLOWLIST
bool "Use allowlist for logging of I2C transactions"
depends on I2C_DUMP_MESSAGES
depends on DT_HAS_ZEPHYR_I2C_DUMP_ALLOWLIST_ENABLED
help
Use allowlist to specify which devices transactions should be logged.
The allowlist is defined in the devicetree using the compatible string of
"zephyr,i2c-dump-allowlist" and phandles to the devices that need to be traced.
Example of devicetree node:
i2c-dump-allowlist {
compatible = "zephyr,i2c-dump-allowlist";
devices = < &display0 >, < &sensor3 >;
};
config I2C_CALLBACK
bool "I2C asynchronous callback API"
help

View file

@ -25,9 +25,39 @@ void z_i2c_transfer_signal_cb(const struct device *dev,
}
#endif
#ifdef CONFIG_I2C_DUMP_MESSAGES_ALLOWLIST
#define DEF_BUS_WITH_ADDR(node, prop, idx) I2C_DT_SPEC_GET(DT_PHANDLE_BY_IDX(node, prop, idx)),
#define DEF_ALLOWLIST_DEV(node) DT_FOREACH_PROP_ELEM(node, devices, DEF_BUS_WITH_ADDR)
struct i2c_dt_spec messages_allowlist[] = {
DT_FOREACH_STATUS_OKAY(zephyr_i2c_dump_allowlist, DEF_ALLOWLIST_DEV)};
#undef DEF_ALLOWLIST_DEV
#undef DEF_BUS_WITH_ADDR
#endif
void i2c_dump_msgs_rw(const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs,
uint16_t addr, bool dump_read)
{
#ifdef CONFIG_I2C_DUMP_MESSAGES_ALLOWLIST
bool found_dev = 0;
for (int a = 0; a < ARRAY_SIZE(messages_allowlist); a++) {
struct i2c_dt_spec *allowed = &messages_allowlist[a];
if (dev != allowed->bus || addr != allowed->addr) {
continue;
} else {
found_dev = 1;
break;
}
}
if (!found_dev) {
return;
}
#endif
LOG_DBG("I2C msg: %s, addr=%x", dev->name, addr);
for (unsigned int i = 0; i < num_msgs; i++) {
const struct i2c_msg *msg = &msgs[i];

View file

@ -0,0 +1,16 @@
# Copyright 2023 Google LLC
# SPDX-License-Identifier: Apache-2.0
description: Devices allowlist for i2c messages dump
compatible: "zephyr,i2c-dump-allowlist"
include: base.yaml
properties:
status:
const: "okay"
devices:
required: true
type: phandles