Skip to content

Commit 8f65ea4

Browse files
[bsp/nrf5x] fix i2c driver bug
1 parent e055a00 commit 8f65ea4

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

bsp/nrf5x/libraries/drivers/drv_i2c.c

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2020-11-15 xckhmf First Verison
9+
* 2021-11-27 chenyingchun fix _master_xfer bug
910
*
1011
*/
1112

@@ -56,36 +57,54 @@ static int twi_master_init(struct rt_i2c_bus_device *bus)
5657
nrfx_twi_twim_bus_recover(config.scl,config.sda);
5758

5859
rtn = nrfx_twim_init(p_instance,&config,NULL,NULL);
60+
if (rtn != NRFX_SUCCESS)
61+
{
62+
return rtn;
63+
}
5964
nrfx_twim_enable(p_instance);
6065
return 0;
6166
}
6267

6368
static rt_size_t _master_xfer(struct rt_i2c_bus_device *bus,
64-
struct rt_i2c_msg msgs[],
65-
rt_uint32_t num)
69+
struct rt_i2c_msg msgs[],
70+
rt_uint32_t num)
6671
{
67-
nrfx_twim_t const * p_instance = &((drv_i2c_cfg_t *)bus->priv)->twi_instance;
72+
struct rt_i2c_msg *msg;
73+
nrfx_twim_t const *p_instance = &((drv_i2c_cfg_t *)bus->priv)->twi_instance;
6874
nrfx_err_t ret = NRFX_ERROR_INTERNAL;
6975
uint32_t no_stop_flag = 0;
76+
rt_int32_t i = 0;
7077

71-
nrfx_twim_xfer_desc_t xfer = NRFX_TWIM_XFER_DESC_TX(msgs->addr,msgs->buf, msgs->len);
72-
if((msgs->flags & 0x01) == RT_I2C_WR)
78+
for (i = 0; i < num; i++)
7379
{
74-
xfer.type = NRFX_TWIM_XFER_TX;
75-
if((msgs->flags & 0x40) == RT_I2C_NO_READ_ACK)
80+
msg = &msgs[i];
81+
nrfx_twim_xfer_desc_t xfer = NRFX_TWIM_XFER_DESC_TX(msg->addr, msg->buf, msg->len);
82+
83+
if (msg->flags & RT_I2C_RD)
7684
{
77-
no_stop_flag = NRFX_TWIM_FLAG_TX_NO_STOP;
85+
xfer.type = NRFX_TWIM_XFER_RX;
86+
}
87+
else
88+
{
89+
xfer.type = NRFX_TWIM_XFER_TX;
90+
if (msg->flags & RT_I2C_NO_READ_ACK)
91+
{
92+
no_stop_flag = NRFX_TWIM_FLAG_TX_NO_STOP;
93+
}
94+
}
95+
96+
ret = nrfx_twim_xfer(p_instance, &xfer, no_stop_flag);
97+
if (ret != NRFX_SUCCESS)
98+
{
99+
goto out;
78100
}
79101
}
80-
else if((msgs->flags & 0x01) == RT_I2C_RD)
81-
{
82-
xfer.type = NRFX_TWIM_XFER_RX;
83-
}
84-
ret = nrfx_twim_xfer(p_instance,&xfer,no_stop_flag);
85-
return (ret == NRFX_SUCCESS) ? msgs->len : 0;
86102

103+
out:
104+
return i;
87105
}
88106

107+
89108
static const struct rt_i2c_bus_device_ops _i2c_ops =
90109
{
91110
_master_xfer,

0 commit comments

Comments
 (0)