1 /*
2 * Logging of I2C messages
3 *
4 * Copyright 2020 Google LLC
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <stdio.h>
10
11 #include <zephyr/drivers/i2c.h>
12
13 #define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(i2c);
16
17 #if defined(CONFIG_I2C_CALLBACK) && defined(CONFIG_POLL)
z_i2c_transfer_signal_cb(const struct device * dev,int result,void * data)18 void z_i2c_transfer_signal_cb(const struct device *dev,
19 int result,
20 void *data)
21 {
22 struct k_poll_signal *sig = (struct k_poll_signal *)data;
23
24 k_poll_signal_raise(sig, result);
25 }
26 #endif
27
i2c_dump_msgs_rw(const char * name,const struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr,bool dump_read)28 void i2c_dump_msgs_rw(const char *name, const struct i2c_msg *msgs,
29 uint8_t num_msgs, uint16_t addr, bool dump_read)
30 {
31 LOG_DBG("I2C msg: %s, addr=%x", name, addr);
32 for (unsigned int i = 0; i < num_msgs; i++) {
33 const struct i2c_msg *msg = &msgs[i];
34 const bool is_read = msg->flags & I2C_MSG_READ;
35 const bool dump_data = dump_read || !is_read;
36
37 if (msg->len == 1 && dump_data) {
38 LOG_DBG(" %c %s%s len=01: %02x", is_read ? 'R' : 'W',
39 msg->flags & I2C_MSG_RESTART ? "Sr " : "",
40 msg->flags & I2C_MSG_STOP ? "P" : "",
41 msg->buf[0]);
42 } else {
43 LOG_DBG(" %c %s%s len=%02x: ", is_read ? 'R' : 'W',
44 msg->flags & I2C_MSG_RESTART ? "Sr " : "",
45 msg->flags & I2C_MSG_STOP ? "P" : "",
46 msg->len);
47 if (dump_data) {
48 LOG_HEXDUMP_DBG(msg->buf, msg->len, "contents:");
49 }
50 }
51 }
52 }
53