优化模拟i2c时序逻辑,使模拟I2C时序更加接近设定的延迟时间 模拟I2C的IO翻转时间单位改为ns,使I2C通讯速率可调节度更准确

This commit is contained in:
2023-08-02 13:00:07 +08:00
parent ba913a7eaf
commit 4ca7e495ab
2 changed files with 12 additions and 9 deletions

View File

@ -25,12 +25,12 @@
rt_inline void i2c_delay(struct rt_i2c_bit_ops *ops) rt_inline void i2c_delay(struct rt_i2c_bit_ops *ops)
{ {
ops->udelay((ops->delay_us + 1) >> 1); ops->ndelay((ops->delay_ns + 1) >> 1);
} }
rt_inline void i2c_delay2(struct rt_i2c_bit_ops *ops) rt_inline void i2c_delay2(struct rt_i2c_bit_ops *ops)
{ {
ops->udelay(ops->delay_us); ops->ndelay(ops->delay_ns);
} }
#define SDA_L(ops) SET_SDA(ops, 0) #define SDA_L(ops) SET_SDA(ops, 0)
@ -47,7 +47,10 @@ static rt_err_t SCL_H(struct rt_i2c_bit_ops *ops)
SET_SCL(ops, 1); SET_SCL(ops, 1);
if (!ops->get_scl) if (!ops->get_scl)
goto done; {
i2c_delay(ops);
return RT_EOK;
}
start = rt_tick_get(); start = rt_tick_get();
i2c_delay(ops); i2c_delay(ops);
@ -65,9 +68,6 @@ static rt_err_t SCL_H(struct rt_i2c_bit_ops *ops)
} }
#endif #endif
done:
i2c_delay(ops);
return RT_EOK; return RT_EOK;
} }
@ -181,7 +181,7 @@ static rt_int32_t i2c_readb(struct rt_i2c_bus_device *bus)
if (GET_SDA(ops)) if (GET_SDA(ops))
data |= 1; data |= 1;
SCL_L(ops); SCL_L(ops);
i2c_delay2(ops); i2c_delay(ops);
} }
return data; return data;

View File

@ -11,6 +11,9 @@
#ifndef __I2C_BIT_OPS_H__ #ifndef __I2C_BIT_OPS_H__
#define __I2C_BIT_OPS_H__ #define __I2C_BIT_OPS_H__
#include <rtdef.h>
#include <rtdevice.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -23,9 +26,9 @@ struct rt_i2c_bit_ops
rt_int32_t (*get_sda)(void *data); rt_int32_t (*get_sda)(void *data);
rt_int32_t (*get_scl)(void *data); rt_int32_t (*get_scl)(void *data);
void (*udelay)(rt_uint32_t us); void (*ndelay)(rt_uint32_t us);
rt_uint32_t delay_us; /* scl and sda line delay */ rt_uint32_t delay_ns; /* scl and sda line delay */
rt_uint32_t timeout; /* in tick */ rt_uint32_t timeout; /* in tick */
}; };