2020-06-05 05:08:13 +08:00
|
|
|
/*
|
|
|
|
* Logging of I2C messages
|
|
|
|
*
|
|
|
|
* Copyright 2020 Google LLC
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/i2c.h>
|
2020-06-05 05:08:13 +08:00
|
|
|
|
|
|
|
#define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2020-06-05 05:08:13 +08:00
|
|
|
LOG_MODULE_REGISTER(i2c);
|
|
|
|
|
2022-05-02 22:41:38 +08:00
|
|
|
#if defined(CONFIG_I2C_CALLBACK) && defined(CONFIG_POLL)
|
|
|
|
void z_i2c_transfer_signal_cb(const struct device *dev,
|
|
|
|
int result,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct k_poll_signal *sig = (struct k_poll_signal *)data;
|
|
|
|
|
|
|
|
k_poll_signal_raise(sig, result);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-01 00:57:46 +08:00
|
|
|
#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
|
|
|
|
|
2023-04-01 00:41:54 +08:00
|
|
|
void i2c_dump_msgs_rw(const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs,
|
|
|
|
uint16_t addr, bool dump_read)
|
2020-06-05 05:08:13 +08:00
|
|
|
{
|
2023-04-01 00:57:46 +08:00
|
|
|
#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
|
|
|
|
|
2023-04-01 00:41:54 +08:00
|
|
|
LOG_DBG("I2C msg: %s, addr=%x", dev->name, addr);
|
2020-06-05 05:08:13 +08:00
|
|
|
for (unsigned int i = 0; i < num_msgs; i++) {
|
|
|
|
const struct i2c_msg *msg = &msgs[i];
|
2022-12-21 18:52:59 +08:00
|
|
|
const bool is_read = msg->flags & I2C_MSG_READ;
|
|
|
|
const bool dump_data = dump_read || !is_read;
|
2020-06-05 05:08:13 +08:00
|
|
|
|
2022-12-21 18:52:59 +08:00
|
|
|
if (msg->len == 1 && dump_data) {
|
2023-04-01 00:44:40 +08:00
|
|
|
LOG_DBG(" %c %2s %1s len=01: %02x", is_read ? 'R' : 'W',
|
|
|
|
msg->flags & I2C_MSG_RESTART ? "Sr" : "",
|
|
|
|
msg->flags & I2C_MSG_STOP ? "P" : "", msg->buf[0]);
|
2022-12-21 18:52:59 +08:00
|
|
|
} else {
|
2023-04-01 00:44:40 +08:00
|
|
|
LOG_DBG(" %c %2s %1s len=%02x: ", is_read ? 'R' : 'W',
|
|
|
|
msg->flags & I2C_MSG_RESTART ? "Sr" : "",
|
|
|
|
msg->flags & I2C_MSG_STOP ? "P" : "", msg->len);
|
2022-12-21 18:52:59 +08:00
|
|
|
if (dump_data) {
|
|
|
|
LOG_HEXDUMP_DBG(msg->buf, msg->len, "contents:");
|
|
|
|
}
|
2020-06-05 05:08:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|