Merge pull request #3929 from geniusgogo/i2c_update

[dirver/i2c] i2c driver support bus lock, STOP control
This commit is contained in:
Bernard Xiong 2020-12-10 22:44:04 +08:00 committed by GitHub
commit e2d951ad2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 5 deletions

View File

@ -97,7 +97,7 @@ if RT_USING_I2C
default y default y
if RT_USING_I2C_BITOPS if RT_USING_I2C_BITOPS
config RT_I2C_BITOPS_DEBUG config RT_I2C_BITOPS_DEBUG
bool "Use simulate I2C debug message" bool "Use simulate I2C debug message"
default n default n
endif endif

View File

@ -375,8 +375,6 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
rt_int32_t i, ret; rt_int32_t i, ret;
rt_uint16_t ignore_nack; rt_uint16_t ignore_nack;
LOG_D("send start condition");
i2c_start(ops);
for (i = 0; i < num; i++) for (i = 0; i < num; i++)
{ {
msg = &msgs[i]; msg = &msgs[i];
@ -387,6 +385,11 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
{ {
i2c_restart(ops); i2c_restart(ops);
} }
else
{
LOG_D("send start condition");
i2c_start(ops);
}
ret = i2c_bit_send_address(bus, msg); ret = i2c_bit_send_address(bus, msg);
if ((ret != RT_EOK) && !ignore_nack) if ((ret != RT_EOK) && !ignore_nack)
{ {
@ -423,8 +426,11 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
ret = i; ret = i;
out: out:
LOG_D("send stop condition"); if (!(msg->flags & RT_I2C_NO_STOP))
i2c_stop(ops); {
LOG_D("send stop condition");
i2c_stop(ops);
}
return ret; return ret;
} }

View File

@ -23,6 +23,7 @@ extern "C" {
#define RT_I2C_NO_START (1u << 4) #define RT_I2C_NO_START (1u << 4)
#define RT_I2C_IGNORE_NACK (1u << 5) #define RT_I2C_IGNORE_NACK (1u << 5)
#define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */ #define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
#define RT_I2C_NO_STOP (1u << 7)
struct rt_i2c_msg struct rt_i2c_msg
{ {
@ -83,6 +84,17 @@ rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
rt_uint16_t flags, rt_uint16_t flags,
rt_uint8_t *buf, rt_uint8_t *buf,
rt_uint32_t count); rt_uint32_t count);
rt_inline rt_err_t rt_i2c_bus_lock(struct rt_i2c_bus_device *bus, rt_tick_t timeout)
{
return rt_mutex_take(&bus->lock, timeout);
}
rt_inline rt_err_t rt_i2c_bus_unlock(struct rt_i2c_bus_device *bus)
{
return rt_mutex_release(&bus->lock);
}
int rt_i2c_core_init(void); int rt_i2c_core_init(void);
#ifdef __cplusplus #ifdef __cplusplus