I2C

报一个LPC4008代码中I2C的bug

发布于 2015-07-27 10:42:00
淘宝买的LPC4008开发板,验证I2C时发现bug
只能写一个寄存器,调了一下,发现bug如下

static rt_size_t lpc_i2c_recv_bytes(LPC_I2C_TypeDef *I2Cx, struct rt_i2c_msg *msg)
{
rt_size_t bytes = 0;
rt_size_t len = msg->len;
rt_uint32_t stat = 0;
while (len--)
{
I2Cx->CONCLR = I2C_I2CONCLR_SIC;
if (len == 0)
{
I2Cx->CONCLR = I2C_I2CONCLR_AAC;
}
else
{
I2Cx->CONSET = I2C_I2CONSET_AA;
}
while (!(I2Cx->CONSET & I2C_I2CONSET_SI));

msg->buf[bytes++] = (uint8_t)(I2Cx->DAT & I2C_I2DAT_BITMASK);
stat = I2Cx->STAT & I2C_STAT_CODE_BITMASK;
if (len && (I2C_I2STAT_M_RX_DAT_ACK != stat))
{
i2c_dbg("i2c recv error on the byte of %d,send ack error!
", bytes);
return bytes;
}
[b][u] if ((len==0)&&(I2C_I2STAT_M_RX_DAT_NACK != stat))[/u][/b]
{
i2c_dbg("i2c recv error on the byte of %d,send nack error!
", bytes);
return bytes;
}
}

return bytes;
}

原来是else if(I2C_I2STAT_M_RX_DAT_NACK != stat)

请验证修改

查看更多

关注者
0
被浏览
2.2k
3 个回答
aozima
aozima 2015-07-27
拒绝白嫖,拒绝键盘侠!
我有空也试试
aozima
aozima 2015-07-27
拒绝白嫖,拒绝键盘侠!
更新后测试通过,这里确实有问题。
判断条件不充分,当读取多字节时,就提前异常返回了,并把总线给钳制起来了。
 software/rtthread_examples/drivers/drv_i2c.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/software/rtthread_examples/drivers/drv_i2c.c b/software/rtthread_examples/drivers/drv_i2c.c
index 3928600..af26f1e 100644
--- a/software/rtthread_examples/drivers/drv_i2c.c
+++ b/software/rtthread_examples/drivers/drv_i2c.c
@@ -142,7 +142,7 @@ static rt_size_t lpc_i2c_recv_bytes(LPC_I2C_TypeDef *I2Cx, struct rt_i2c_msg *ms
i2c_dbg("i2c recv error on the byte of %d,send ack error!
", bytes);
return bytes;
}
- else if (I2C_I2STAT_M_RX_DAT_NACK != stat)
+ else if ( (len == 0) && (I2C_I2STAT_M_RX_DAT_NACK != stat) )
{
i2c_dbg("i2c recv error on the byte of %d,send nack error!
", bytes);
return bytes;

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友