From 2bc7dcdc2e2aef411dacef40e558ccaa32546fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Barna=C5=9B?= Date: Fri, 31 Mar 2023 18:57:46 +0200 Subject: [PATCH] i2c: add filtering of i2c dumped messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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ś --- drivers/i2c/Kconfig | 16 +++++++++- drivers/i2c/i2c_common.c | 30 +++++++++++++++++++ .../i2c/zephyr,i2c-dump-allowlist.yaml | 16 ++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 dts/bindings/i2c/zephyr,i2c-dump-allowlist.yaml diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig index 8ae217f82bf..5f62fbb4eb1 100644 --- a/drivers/i2c/Kconfig +++ b/drivers/i2c/Kconfig @@ -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 diff --git a/drivers/i2c/i2c_common.c b/drivers/i2c/i2c_common.c index 616e15a2d03..93986d6f58d 100644 --- a/drivers/i2c/i2c_common.c +++ b/drivers/i2c/i2c_common.c @@ -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]; diff --git a/dts/bindings/i2c/zephyr,i2c-dump-allowlist.yaml b/dts/bindings/i2c/zephyr,i2c-dump-allowlist.yaml new file mode 100644 index 00000000000..b1657aa7459 --- /dev/null +++ b/dts/bindings/i2c/zephyr,i2c-dump-allowlist.yaml @@ -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