Skip to content

Commit a24b896

Browse files
committed
修改读写逻辑,解决i2c驱动无法读取的问题
1 parent 3138f2d commit a24b896

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

bsp/lpc54114-lite/drivers/drv_i2c.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ static rt_size_t master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ms
4040
rt_uint32_t index = 0;
4141
struct lpc_i2c *lpc_i2c = RT_NULL;
4242
struct rt_i2c_msg *msg = RT_NULL;
43-
i2c_master_transfer_t xfer = {0};
43+
i2c_direction_t direction;
44+
status_t result = kStatus_Success;
4445

4546
RT_ASSERT(bus != RT_NULL);
4647

@@ -49,33 +50,35 @@ static rt_size_t master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ms
4950
for(index = 0; index < num; index++)
5051
{
5152
msg = &msgs[index];
52-
53-
xfer.slaveAddress = msg->addr;
54-
xfer.flags = kI2C_TransferDefaultFlag;
55-
xfer.subaddress = 0;
56-
xfer.subaddressSize = 0;
57-
xfer.data = msg->buf;
58-
xfer.dataSize = msg->len;
59-
60-
if (msg->flags & RT_I2C_RD)
61-
{
62-
xfer.direction = kI2C_Read;
63-
}
64-
else
53+
direction = ((msg->flags & RT_I2C_RD) ? kI2C_Read : kI2C_Write);
54+
55+
if (!(msg->flags & RT_I2C_NO_START))
6556
{
66-
xfer.direction = kI2C_Write;
57+
/* Start condition and slave address. */
58+
result = I2C_MasterStart(lpc_i2c->base, msg->addr, direction);
6759
}
68-
69-
if(I2C_MasterTransferBlocking(lpc_i2c->base, &xfer) != kStatus_Success)
60+
61+
if (result == kStatus_Success)
7062
{
71-
rt_kprintf("i2c bus write failed, i2c bus stop!\n");
72-
goto exit;
63+
if (direction == kI2C_Write)
64+
{
65+
/* Transmit data. */
66+
result = I2C_MasterWriteBlocking(lpc_i2c->base, msg->buf, msg->len, kI2C_TransferDefaultFlag);
67+
}
68+
else
69+
{
70+
/* Receive Data. */
71+
result = I2C_MasterReadBlocking(lpc_i2c->base, msg->buf, msg->len, kI2C_TransferDefaultFlag);
72+
}
7373
}
7474
}
7575

76-
ret = index;
76+
if (result == kStatus_Success)
77+
{
78+
ret = index;
79+
}
7780

78-
exit:
81+
I2C_MasterStop(lpc_i2c->base);
7982
return ret;
8083
}
8184

0 commit comments

Comments
 (0)