修改读写逻辑,解决i2c驱动无法读取的问题

This commit is contained in:
liweihao 2019-11-11 22:46:28 +08:00
parent 3138f2d8d7
commit a24b896443
1 changed files with 23 additions and 20 deletions

View File

@ -40,7 +40,8 @@ static rt_size_t master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ms
rt_uint32_t index = 0; rt_uint32_t index = 0;
struct lpc_i2c *lpc_i2c = RT_NULL; struct lpc_i2c *lpc_i2c = RT_NULL;
struct rt_i2c_msg *msg = RT_NULL; struct rt_i2c_msg *msg = RT_NULL;
i2c_master_transfer_t xfer = {0}; i2c_direction_t direction;
status_t result = kStatus_Success;
RT_ASSERT(bus != RT_NULL); RT_ASSERT(bus != RT_NULL);
@ -49,33 +50,35 @@ static rt_size_t master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ms
for(index = 0; index < num; index++) for(index = 0; index < num; index++)
{ {
msg = &msgs[index]; msg = &msgs[index];
direction = ((msg->flags & RT_I2C_RD) ? kI2C_Read : kI2C_Write);
xfer.slaveAddress = msg->addr;
xfer.flags = kI2C_TransferDefaultFlag;
xfer.subaddress = 0;
xfer.subaddressSize = 0;
xfer.data = msg->buf;
xfer.dataSize = msg->len;
if (msg->flags & RT_I2C_RD) if (!(msg->flags & RT_I2C_NO_START))
{ {
xfer.direction = kI2C_Read; /* Start condition and slave address. */
result = I2C_MasterStart(lpc_i2c->base, msg->addr, direction);
} }
else
if (result == kStatus_Success)
{ {
xfer.direction = kI2C_Write; if (direction == kI2C_Write)
} {
/* Transmit data. */
if(I2C_MasterTransferBlocking(lpc_i2c->base, &xfer) != kStatus_Success) result = I2C_MasterWriteBlocking(lpc_i2c->base, msg->buf, msg->len, kI2C_TransferDefaultFlag);
{ }
rt_kprintf("i2c bus write failed, i2c bus stop!\n"); else
goto exit; {
/* Receive Data. */
result = I2C_MasterReadBlocking(lpc_i2c->base, msg->buf, msg->len, kI2C_TransferDefaultFlag);
}
} }
} }
ret = index; if (result == kStatus_Success)
{
ret = index;
}
exit: I2C_MasterStop(lpc_i2c->base);
return ret; return ret;
} }