diff --git a/bsp/gd32303e-eval/drivers/drv_i2c.c b/bsp/gd32303e-eval/drivers/drv_i2c.c index 7c8ba277c8..059b211c01 100644 --- a/bsp/gd32303e-eval/drivers/drv_i2c.c +++ b/bsp/gd32303e-eval/drivers/drv_i2c.c @@ -31,6 +31,14 @@ #include +#define DBG_TAG "drv.I2C" +#ifdef RT_I2C_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif +#include + #ifdef RT_USING_I2C_BITOPS /*user can change this*/ @@ -256,7 +264,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg { if (gd32_i2c_read(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0) { - i2c_dbg("i2c bus write failed,i2c bus stop!\n"); + LOG_E("i2c bus write failed,i2c bus stop!"); goto out; } } @@ -264,7 +272,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg { if (gd32_i2c_write(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0) { - i2c_dbg("i2c bus write failed,i2c bus stop!\n"); + LOG_E("i2c bus write failed,i2c bus stop!"); goto out; } } @@ -273,7 +281,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ret = i; out: - i2c_dbg("send stop condition\n"); + LOG_E("send stop condition\n"); return ret; } diff --git a/bsp/gd32e230k-start/drivers/drv_i2c.c b/bsp/gd32e230k-start/drivers/drv_i2c.c index 473a2b1bda..57f49e4633 100644 --- a/bsp/gd32e230k-start/drivers/drv_i2c.c +++ b/bsp/gd32e230k-start/drivers/drv_i2c.c @@ -18,6 +18,14 @@ #include +#define DBG_TAG "drv.I2C" +#ifdef RT_I2C_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif +#include + #ifdef RT_USING_I2C_BITOPS /*user can change this*/ @@ -243,7 +251,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg { if (gd32_i2c_read(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0) { - i2c_dbg("i2c bus write failed,i2c bus stop!\n"); + LOG_E("i2c bus write failed,i2c bus stop!"); goto out; } } @@ -251,7 +259,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg { if (gd32_i2c_write(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0) { - i2c_dbg("i2c bus write failed,i2c bus stop!\n"); + LOG_E("i2c bus write failed,i2c bus stop!"); goto out; } } @@ -260,7 +268,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ret = i; out: - i2c_dbg("send stop condition\n"); + LOG_D("send stop condition"); return ret; } diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index c2e6d3a411..12b82671e3 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -88,11 +88,21 @@ config RT_USING_I2C default n if RT_USING_I2C + config RT_I2C_DEBUG + bool "Use I2C debug mesage" + default n + config RT_USING_I2C_BITOPS bool "Use GPIO to simulate I2C" default y endif +if RT_USING_I2C_BITOPS + config RT_I2C_BITOPS_DEBUG + bool "Use simulate I2C debug mesage" + default n +endif + config RT_USING_PIN bool "Using generic GPIO device drivers" default y diff --git a/components/drivers/i2c/i2c-bit-ops.c b/components/drivers/i2c/i2c-bit-ops.c index 478cf99fd6..99338eef54 100644 --- a/components/drivers/i2c/i2c-bit-ops.c +++ b/components/drivers/i2c/i2c-bit-ops.c @@ -10,11 +10,13 @@ #include -#ifdef RT_I2C_BIT_DEBUG -#define bit_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__) +#define DBG_TAG "I2C" +#ifdef RT_I2C_BITOPS_DEBUG +#define DBG_LVL DBG_LOG #else -#define bit_dbg(fmt, ...) +#define DBG_LVL DBG_INFO #endif +#include #define SET_SDA(ops, val) ops->set_sda(ops->data, val) #define SET_SCL(ops, val) ops->set_scl(ops->data, val) @@ -54,11 +56,11 @@ static rt_err_t SCL_H(struct rt_i2c_bit_ops *ops) return -RT_ETIMEOUT; rt_thread_delay((ops->timeout + 1) >> 1); } -#ifdef RT_I2C_BIT_DEBUG +#ifdef RT_I2C_BITOPS_DEBUG if (rt_tick_get() != start) { - bit_dbg("wait %ld tick for SCL line to go high\n", - rt_tick_get() - start); + LOG_D("wait %ld tick for SCL line to go high", + rt_tick_get() - start); } #endif @@ -70,14 +72,14 @@ done: static void i2c_start(struct rt_i2c_bit_ops *ops) { -#ifdef RT_I2C_BIT_DEBUG +#ifdef RT_I2C_BITOPS_DEBUG if (ops->get_scl && !GET_SCL(ops)) { - bit_dbg("I2C bus error, SCL line low\n"); + LOG_E("I2C bus error, SCL line low"); } if (ops->get_sda && !GET_SDA(ops)) { - bit_dbg("I2C bus error, SDA line low\n"); + LOG_E("I2C bus error, SDA line low"); } #endif SDA_L(ops); @@ -114,13 +116,13 @@ rt_inline rt_bool_t i2c_waitack(struct rt_i2c_bit_ops *ops) if (SCL_H(ops) < 0) { - bit_dbg("wait ack timeout\n"); + LOG_W("wait ack timeout"); return -RT_ETIMEOUT; } ack = !GET_SDA(ops); /* ACK : SDA pin is pulled low */ - bit_dbg("%s\n", ack ? "ACK" : "NACK"); + LOG_D("%s", ack ? "ACK" : "NACK"); SCL_L(ops); @@ -142,8 +144,8 @@ static rt_int32_t i2c_writeb(struct rt_i2c_bus_device *bus, rt_uint8_t data) i2c_delay(ops); if (SCL_H(ops) < 0) { - bit_dbg("i2c_writeb: 0x%02x, " - "wait scl pin high timeout at bit %d\n", + LOG_D("i2c_writeb: 0x%02x, " + "wait scl pin high timeout at bit %d", data, i); return -RT_ETIMEOUT; @@ -169,8 +171,8 @@ static rt_int32_t i2c_readb(struct rt_i2c_bus_device *bus) if (SCL_H(ops) < 0) { - bit_dbg("i2c_readb: wait scl pin high " - "timeout at bit %d\n", 7 - i); + LOG_D("i2c_readb: wait scl pin high " + "timeout at bit %d", 7 - i); return -RT_ETIMEOUT; } @@ -205,13 +207,13 @@ static rt_size_t i2c_send_bytes(struct rt_i2c_bus_device *bus, } else if (ret == 0) { - i2c_dbg("send bytes: NACK.\n"); + LOG_D("send bytes: NACK."); return 0; } else { - i2c_dbg("send bytes: error %d\n", ret); + LOG_E("send bytes: error %d", ret); return ret; } @@ -229,7 +231,7 @@ static rt_err_t i2c_send_ack_or_nack(struct rt_i2c_bus_device *bus, int ack) i2c_delay(ops); if (SCL_H(ops) < 0) { - bit_dbg("ACK or NACK timeout\n"); + LOG_E("ACK or NACK timeout."); return -RT_ETIMEOUT; } @@ -263,7 +265,7 @@ static rt_size_t i2c_recv_bytes(struct rt_i2c_bus_device *bus, ptr ++; count --; - bit_dbg("recieve bytes: 0x%02x, %s\n", + LOG_D("recieve bytes: 0x%02x, %s", val, (flags & RT_I2C_NO_READ_ACK) ? "(No ACK/NACK)" : (count ? "ACK" : "NACK")); @@ -291,10 +293,10 @@ static rt_int32_t i2c_send_address(struct rt_i2c_bus_device *bus, ret = i2c_writeb(bus, addr); if (ret == 1 || i == retries) break; - bit_dbg("send stop condition\n"); + LOG_D("send stop condition"); i2c_stop(ops); i2c_delay2(ops); - bit_dbg("send start condition\n"); + LOG_D("send start condition"); i2c_start(ops); } @@ -319,12 +321,12 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, addr1 = 0xf0 | ((msg->addr >> 7) & 0x06); addr2 = msg->addr & 0xff; - bit_dbg("addr1: %d, addr2: %d\n", addr1, addr2); + LOG_D("addr1: %d, addr2: %d", addr1, addr2); ret = i2c_send_address(bus, addr1, retries); if ((ret != 1) && !ignore_nack) { - bit_dbg("NACK: sending first addr\n"); + LOG_W("NACK: sending first addr"); return -RT_EIO; } @@ -332,19 +334,19 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, ret = i2c_writeb(bus, addr2); if ((ret != 1) && !ignore_nack) { - bit_dbg("NACK: sending second addr\n"); + LOG_W("NACK: sending second addr"); return -RT_EIO; } if (flags & RT_I2C_RD) { - bit_dbg("send repeated start condition\n"); + LOG_D("send repeated start condition"); i2c_restart(ops); addr1 |= 0x01; ret = i2c_send_address(bus, addr1, retries); if ((ret != 1) && !ignore_nack) { - bit_dbg("NACK: sending repeated addr\n"); + LOG_E("NACK: sending repeated addr"); return -RT_EIO; } @@ -373,7 +375,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, rt_int32_t i, ret; rt_uint16_t ignore_nack; - bit_dbg("send start condition\n"); + LOG_D("send start condition"); i2c_start(ops); for (i = 0; i < num; i++) { @@ -388,7 +390,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, ret = i2c_bit_send_address(bus, msg); if ((ret != RT_EOK) && !ignore_nack) { - bit_dbg("receive NACK from device addr 0x%02x msg %d\n", + LOG_D("receive NACK from device addr 0x%02x msg %d", msgs[i].addr, i); goto out; } @@ -397,7 +399,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, { ret = i2c_recv_bytes(bus, msg); if (ret >= 1) - bit_dbg("read %d byte%s\n", ret, ret == 1 ? "" : "s"); + LOG_D("read %d byte%s", ret, ret == 1 ? "" : "s"); if (ret < msg->len) { if (ret >= 0) @@ -409,7 +411,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, { ret = i2c_send_bytes(bus, msg); if (ret >= 1) - bit_dbg("write %d byte%s\n", ret, ret == 1 ? "" : "s"); + LOG_D("write %d byte%s", ret, ret == 1 ? "" : "s"); if (ret < msg->len) { if (ret >= 0) @@ -421,7 +423,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, ret = i; out: - bit_dbg("send stop condition\n"); + LOG_D("send stop condition"); i2c_stop(ops); return ret; diff --git a/components/drivers/i2c/i2c_core.c b/components/drivers/i2c/i2c_core.c index c0f0c70565..f358c2b758 100644 --- a/components/drivers/i2c/i2c_core.c +++ b/components/drivers/i2c/i2c_core.c @@ -10,6 +10,14 @@ #include +#define DBG_TAG "I2C" +#ifdef RT_I2C_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif +#include + rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus, const char *bus_name) { @@ -21,7 +29,7 @@ rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus, res = rt_i2c_bus_device_device_init(bus, bus_name); - i2c_dbg("I2C bus [%s] registered\n", bus_name); + LOG_I("I2C bus [%s] registered", bus_name); return res; } @@ -32,7 +40,7 @@ struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name) rt_device_t dev = rt_device_find(bus_name); if (dev == RT_NULL || dev->type != RT_Device_Class_I2CBUS) { - i2c_dbg("I2C bus %s not exist\n", bus_name); + LOG_E("I2C bus %s not exist", bus_name); return RT_NULL; } @@ -53,9 +61,9 @@ rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus, #ifdef RT_I2C_DEBUG for (ret = 0; ret < num; ret++) { - i2c_dbg("msgs[%d] %c, addr=0x%02x, len=%d\n", ret, - (msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W', - msgs[ret].addr, msgs[ret].len); + LOG_D("msgs[%d] %c, addr=0x%02x, len=%d", ret, + (msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W', + msgs[ret].addr, msgs[ret].len); } #endif @@ -67,7 +75,7 @@ rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus, } else { - i2c_dbg("I2C bus operation not supported\n"); + LOG_E("I2C bus operation not supported"); return 0; } diff --git a/components/drivers/i2c/i2c_dev.c b/components/drivers/i2c/i2c_dev.c index c784a4e4ee..59f728be09 100644 --- a/components/drivers/i2c/i2c_dev.c +++ b/components/drivers/i2c/i2c_dev.c @@ -11,6 +11,14 @@ #include +#define DBG_TAG "I2C" +#ifdef RT_I2C_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif +#include + static rt_size_t i2c_bus_device_read(rt_device_t dev, rt_off_t pos, void *buffer, @@ -23,7 +31,7 @@ static rt_size_t i2c_bus_device_read(rt_device_t dev, RT_ASSERT(bus != RT_NULL); RT_ASSERT(buffer != RT_NULL); - i2c_dbg("I2C bus dev [%s] reading %u bytes.\n", dev->parent.name, count); + LOG_D("I2C bus dev [%s] reading %u bytes.", dev->parent.name, count); addr = pos & 0xffff; flags = (pos >> 16) & 0xffff; @@ -43,7 +51,7 @@ static rt_size_t i2c_bus_device_write(rt_device_t dev, RT_ASSERT(bus != RT_NULL); RT_ASSERT(buffer != RT_NULL); - i2c_dbg("I2C bus dev [%s] writing %u bytes.\n", dev->parent.name, count); + LOG_D("I2C bus dev [%s] writing %u bytes.", dev->parent.name, count); addr = pos & 0xffff; flags = (pos >> 16) & 0xffff; diff --git a/components/drivers/include/drivers/i2c.h b/components/drivers/include/drivers/i2c.h index c6376efa89..17d57288cd 100644 --- a/components/drivers/include/drivers/i2c.h +++ b/components/drivers/include/drivers/i2c.h @@ -67,12 +67,6 @@ struct rt_i2c_client rt_uint16_t client_addr; }; -#ifdef RT_I2C_DEBUG -#define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__) -#else -#define i2c_dbg(fmt, ...) -#endif - rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus, const char *bus_name); struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);