From a24b8964431d53904a4ca1c36cac1b73eb2169e9 Mon Sep 17 00:00:00 2001 From: liweihao Date: Mon, 11 Nov 2019 22:46:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=BB=E5=86=99=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E8=A7=A3=E5=86=B3i2c=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=AF=BB=E5=8F=96=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/lpc54114-lite/drivers/drv_i2c.c | 43 +++++++++++++++-------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/bsp/lpc54114-lite/drivers/drv_i2c.c b/bsp/lpc54114-lite/drivers/drv_i2c.c index 0caee3453c..81be945067 100644 --- a/bsp/lpc54114-lite/drivers/drv_i2c.c +++ b/bsp/lpc54114-lite/drivers/drv_i2c.c @@ -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; struct lpc_i2c *lpc_i2c = 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); @@ -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++) { msg = &msgs[index]; - - xfer.slaveAddress = msg->addr; - xfer.flags = kI2C_TransferDefaultFlag; - xfer.subaddress = 0; - xfer.subaddressSize = 0; - xfer.data = msg->buf; - xfer.dataSize = msg->len; + direction = ((msg->flags & RT_I2C_RD) ? kI2C_Read : kI2C_Write); - 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(I2C_MasterTransferBlocking(lpc_i2c->base, &xfer) != kStatus_Success) - { - rt_kprintf("i2c bus write failed, i2c bus stop!\n"); - goto exit; + if (direction == kI2C_Write) + { + /* Transmit data. */ + result = I2C_MasterWriteBlocking(lpc_i2c->base, msg->buf, msg->len, kI2C_TransferDefaultFlag); + } + else + { + /* 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; }