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
7173static 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
423425out :
424- bit_dbg ("send stop condition\n " );
426+ LOG_D ("send stop condition" );
425427 i2c_stop (ops );
426428
427429 return ret ;
0 commit comments