Skip to content

Commit e459084

Browse files
authored
Merge pull request #3220 from a1012112796/writing/zzc/i2c_debug_msg
[Device/I2C] Chang debug message print way to ulog
2 parents 86e61dd + ab981a3 commit e459084

File tree

7 files changed

+89
-51
lines changed

7 files changed

+89
-51
lines changed

bsp/gd32303e-eval/drivers/drv_i2c.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131

3232
#include <rtdevice.h>
3333

34+
#define DBG_TAG "drv.I2C"
35+
#ifdef RT_I2C_DEBUG
36+
#define DBG_LVL DBG_LOG
37+
#else
38+
#define DBG_LVL DBG_INFO
39+
#endif
40+
#include <rtdbg.h>
41+
3442
#ifdef RT_USING_I2C_BITOPS
3543

3644
/*user can change this*/
@@ -256,15 +264,15 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg
256264
{
257265
if (gd32_i2c_read(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
258266
{
259-
i2c_dbg("i2c bus write failed,i2c bus stop!\n");
267+
LOG_E("i2c bus write failed,i2c bus stop!");
260268
goto out;
261269
}
262270
}
263271
else
264272
{
265273
if (gd32_i2c_write(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
266274
{
267-
i2c_dbg("i2c bus write failed,i2c bus stop!\n");
275+
LOG_E("i2c bus write failed,i2c bus stop!");
268276
goto out;
269277
}
270278
}
@@ -273,7 +281,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg
273281
ret = i;
274282

275283
out:
276-
i2c_dbg("send stop condition\n");
284+
LOG_E("send stop condition\n");
277285

278286
return ret;
279287
}

bsp/gd32e230k-start/drivers/drv_i2c.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818

1919
#include <rtdevice.h>
2020

21+
#define DBG_TAG "drv.I2C"
22+
#ifdef RT_I2C_DEBUG
23+
#define DBG_LVL DBG_LOG
24+
#else
25+
#define DBG_LVL DBG_INFO
26+
#endif
27+
#include <rtdbg.h>
28+
2129
#ifdef RT_USING_I2C_BITOPS
2230

2331
/*user can change this*/
@@ -243,15 +251,15 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg
243251
{
244252
if (gd32_i2c_read(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
245253
{
246-
i2c_dbg("i2c bus write failed,i2c bus stop!\n");
254+
LOG_E("i2c bus write failed,i2c bus stop!");
247255
goto out;
248256
}
249257
}
250258
else
251259
{
252260
if (gd32_i2c_write(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0)
253261
{
254-
i2c_dbg("i2c bus write failed,i2c bus stop!\n");
262+
LOG_E("i2c bus write failed,i2c bus stop!");
255263
goto out;
256264
}
257265
}
@@ -260,7 +268,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg
260268
ret = i;
261269

262270
out:
263-
i2c_dbg("send stop condition\n");
271+
LOG_D("send stop condition");
264272

265273
return ret;
266274
}

components/drivers/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,21 @@ config RT_USING_I2C
8888
default n
8989

9090
if RT_USING_I2C
91+
config RT_I2C_DEBUG
92+
bool "Use I2C debug mesage"
93+
default n
94+
9195
config RT_USING_I2C_BITOPS
9296
bool "Use GPIO to simulate I2C"
9397
default y
9498
endif
9599

100+
if RT_USING_I2C_BITOPS
101+
config RT_I2C_BITOPS_DEBUG
102+
bool "Use simulate I2C debug mesage"
103+
default n
104+
endif
105+
96106
config RT_USING_PIN
97107
bool "Using generic GPIO device drivers"
98108
default y

components/drivers/i2c/i2c-bit-ops.c

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
#include <rtdevice.h>
1212

13-
#ifdef RT_I2C_BIT_DEBUG
14-
#define bit_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
13+
#define DBG_TAG "I2C"
14+
#ifdef RT_I2C_BITOPS_DEBUG
15+
#define DBG_LVL DBG_LOG
1516
#else
16-
#define bit_dbg(fmt, ...)
17+
#define DBG_LVL DBG_INFO
1718
#endif
19+
#include <rtdbg.h>
1820

1921
#define SET_SDA(ops, val) ops->set_sda(ops->data, val)
2022
#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)
5456
return -RT_ETIMEOUT;
5557
rt_thread_delay((ops->timeout + 1) >> 1);
5658
}
57-
#ifdef RT_I2C_BIT_DEBUG
59+
#ifdef RT_I2C_BITOPS_DEBUG
5860
if (rt_tick_get() != start)
5961
{
60-
bit_dbg("wait %ld tick for SCL line to go high\n",
61-
rt_tick_get() - start);
62+
LOG_D("wait %ld tick for SCL line to go high",
63+
rt_tick_get() - start);
6264
}
6365
#endif
6466

@@ -70,14 +72,14 @@ static rt_err_t SCL_H(struct rt_i2c_bit_ops *ops)
7072

7173
static void i2c_start(struct rt_i2c_bit_ops *ops)
7274
{
73-
#ifdef RT_I2C_BIT_DEBUG
75+
#ifdef RT_I2C_BITOPS_DEBUG
7476
if (ops->get_scl && !GET_SCL(ops))
7577
{
76-
bit_dbg("I2C bus error, SCL line low\n");
78+
LOG_E("I2C bus error, SCL line low");
7779
}
7880
if (ops->get_sda && !GET_SDA(ops))
7981
{
80-
bit_dbg("I2C bus error, SDA line low\n");
82+
LOG_E("I2C bus error, SDA line low");
8183
}
8284
#endif
8385
SDA_L(ops);
@@ -114,13 +116,13 @@ rt_inline rt_bool_t i2c_waitack(struct rt_i2c_bit_ops *ops)
114116

115117
if (SCL_H(ops) < 0)
116118
{
117-
bit_dbg("wait ack timeout\n");
119+
LOG_W("wait ack timeout");
118120

119121
return -RT_ETIMEOUT;
120122
}
121123

122124
ack = !GET_SDA(ops); /* ACK : SDA pin is pulled low */
123-
bit_dbg("%s\n", ack ? "ACK" : "NACK");
125+
LOG_D("%s", ack ? "ACK" : "NACK");
124126

125127
SCL_L(ops);
126128

@@ -142,8 +144,8 @@ static rt_int32_t i2c_writeb(struct rt_i2c_bus_device *bus, rt_uint8_t data)
142144
i2c_delay(ops);
143145
if (SCL_H(ops) < 0)
144146
{
145-
bit_dbg("i2c_writeb: 0x%02x, "
146-
"wait scl pin high timeout at bit %d\n",
147+
LOG_D("i2c_writeb: 0x%02x, "
148+
"wait scl pin high timeout at bit %d",
147149
data, i);
148150

149151
return -RT_ETIMEOUT;
@@ -169,8 +171,8 @@ static rt_int32_t i2c_readb(struct rt_i2c_bus_device *bus)
169171

170172
if (SCL_H(ops) < 0)
171173
{
172-
bit_dbg("i2c_readb: wait scl pin high "
173-
"timeout at bit %d\n", 7 - i);
174+
LOG_D("i2c_readb: wait scl pin high "
175+
"timeout at bit %d", 7 - i);
174176

175177
return -RT_ETIMEOUT;
176178
}
@@ -205,13 +207,13 @@ static rt_size_t i2c_send_bytes(struct rt_i2c_bus_device *bus,
205207
}
206208
else if (ret == 0)
207209
{
208-
i2c_dbg("send bytes: NACK.\n");
210+
LOG_D("send bytes: NACK.");
209211

210212
return 0;
211213
}
212214
else
213215
{
214-
i2c_dbg("send bytes: error %d\n", ret);
216+
LOG_E("send bytes: error %d", ret);
215217

216218
return ret;
217219
}
@@ -229,7 +231,7 @@ static rt_err_t i2c_send_ack_or_nack(struct rt_i2c_bus_device *bus, int ack)
229231
i2c_delay(ops);
230232
if (SCL_H(ops) < 0)
231233
{
232-
bit_dbg("ACK or NACK timeout\n");
234+
LOG_E("ACK or NACK timeout.");
233235

234236
return -RT_ETIMEOUT;
235237
}
@@ -263,7 +265,7 @@ static rt_size_t i2c_recv_bytes(struct rt_i2c_bus_device *bus,
263265
ptr ++;
264266
count --;
265267

266-
bit_dbg("recieve bytes: 0x%02x, %s\n",
268+
LOG_D("recieve bytes: 0x%02x, %s",
267269
val, (flags & RT_I2C_NO_READ_ACK) ?
268270
"(No ACK/NACK)" : (count ? "ACK" : "NACK"));
269271

@@ -291,10 +293,10 @@ static rt_int32_t i2c_send_address(struct rt_i2c_bus_device *bus,
291293
ret = i2c_writeb(bus, addr);
292294
if (ret == 1 || i == retries)
293295
break;
294-
bit_dbg("send stop condition\n");
296+
LOG_D("send stop condition");
295297
i2c_stop(ops);
296298
i2c_delay2(ops);
297-
bit_dbg("send start condition\n");
299+
LOG_D("send start condition");
298300
i2c_start(ops);
299301
}
300302

@@ -319,32 +321,32 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus,
319321
addr1 = 0xf0 | ((msg->addr >> 7) & 0x06);
320322
addr2 = msg->addr & 0xff;
321323

322-
bit_dbg("addr1: %d, addr2: %d\n", addr1, addr2);
324+
LOG_D("addr1: %d, addr2: %d", addr1, addr2);
323325

324326
ret = i2c_send_address(bus, addr1, retries);
325327
if ((ret != 1) && !ignore_nack)
326328
{
327-
bit_dbg("NACK: sending first addr\n");
329+
LOG_W("NACK: sending first addr");
328330

329331
return -RT_EIO;
330332
}
331333

332334
ret = i2c_writeb(bus, addr2);
333335
if ((ret != 1) && !ignore_nack)
334336
{
335-
bit_dbg("NACK: sending second addr\n");
337+
LOG_W("NACK: sending second addr");
336338

337339
return -RT_EIO;
338340
}
339341
if (flags & RT_I2C_RD)
340342
{
341-
bit_dbg("send repeated start condition\n");
343+
LOG_D("send repeated start condition");
342344
i2c_restart(ops);
343345
addr1 |= 0x01;
344346
ret = i2c_send_address(bus, addr1, retries);
345347
if ((ret != 1) && !ignore_nack)
346348
{
347-
bit_dbg("NACK: sending repeated addr\n");
349+
LOG_E("NACK: sending repeated addr");
348350

349351
return -RT_EIO;
350352
}
@@ -373,7 +375,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
373375
rt_int32_t i, ret;
374376
rt_uint16_t ignore_nack;
375377

376-
bit_dbg("send start condition\n");
378+
LOG_D("send start condition");
377379
i2c_start(ops);
378380
for (i = 0; i < num; i++)
379381
{
@@ -388,7 +390,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
388390
ret = i2c_bit_send_address(bus, msg);
389391
if ((ret != RT_EOK) && !ignore_nack)
390392
{
391-
bit_dbg("receive NACK from device addr 0x%02x msg %d\n",
393+
LOG_D("receive NACK from device addr 0x%02x msg %d",
392394
msgs[i].addr, i);
393395
goto out;
394396
}
@@ -397,7 +399,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
397399
{
398400
ret = i2c_recv_bytes(bus, msg);
399401
if (ret >= 1)
400-
bit_dbg("read %d byte%s\n", ret, ret == 1 ? "" : "s");
402+
LOG_D("read %d byte%s", ret, ret == 1 ? "" : "s");
401403
if (ret < msg->len)
402404
{
403405
if (ret >= 0)
@@ -409,7 +411,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
409411
{
410412
ret = i2c_send_bytes(bus, msg);
411413
if (ret >= 1)
412-
bit_dbg("write %d byte%s\n", ret, ret == 1 ? "" : "s");
414+
LOG_D("write %d byte%s", ret, ret == 1 ? "" : "s");
413415
if (ret < msg->len)
414416
{
415417
if (ret >= 0)
@@ -421,7 +423,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
421423
ret = i;
422424

423425
out:
424-
bit_dbg("send stop condition\n");
426+
LOG_D("send stop condition");
425427
i2c_stop(ops);
426428

427429
return ret;

components/drivers/i2c/i2c_core.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
#include <rtdevice.h>
1212

13+
#define DBG_TAG "I2C"
14+
#ifdef RT_I2C_DEBUG
15+
#define DBG_LVL DBG_LOG
16+
#else
17+
#define DBG_LVL DBG_INFO
18+
#endif
19+
#include <rtdbg.h>
20+
1321
rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
1422
const char *bus_name)
1523
{
@@ -21,7 +29,7 @@ rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
2129

2230
res = rt_i2c_bus_device_device_init(bus, bus_name);
2331

24-
i2c_dbg("I2C bus [%s] registered\n", bus_name);
32+
LOG_I("I2C bus [%s] registered", bus_name);
2533

2634
return res;
2735
}
@@ -32,7 +40,7 @@ struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name)
3240
rt_device_t dev = rt_device_find(bus_name);
3341
if (dev == RT_NULL || dev->type != RT_Device_Class_I2CBUS)
3442
{
35-
i2c_dbg("I2C bus %s not exist\n", bus_name);
43+
LOG_E("I2C bus %s not exist", bus_name);
3644

3745
return RT_NULL;
3846
}
@@ -53,9 +61,9 @@ rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
5361
#ifdef RT_I2C_DEBUG
5462
for (ret = 0; ret < num; ret++)
5563
{
56-
i2c_dbg("msgs[%d] %c, addr=0x%02x, len=%d\n", ret,
57-
(msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W',
58-
msgs[ret].addr, msgs[ret].len);
64+
LOG_D("msgs[%d] %c, addr=0x%02x, len=%d", ret,
65+
(msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W',
66+
msgs[ret].addr, msgs[ret].len);
5967
}
6068
#endif
6169

@@ -67,7 +75,7 @@ rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
6775
}
6876
else
6977
{
70-
i2c_dbg("I2C bus operation not supported\n");
78+
LOG_E("I2C bus operation not supported");
7179

7280
return 0;
7381
}

0 commit comments

Comments
 (0)