[components][i2c] 优化 controt 接口格式,增加可用命令 (#7806)

原来 rt_i2c_control 只能执行 RT_I2C_DEV_CTRL_CLK 单一命令,通过此修改可以支持更多可用命令
This commit is contained in:
sp-cai 2023-07-14 09:57:40 +08:00 committed by GitHub
parent 350626841a
commit 0ebec472e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 85 additions and 90 deletions

View File

@ -96,7 +96,7 @@ static rt_size_t _slave_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ms
return -RT_ENOSYS;
}
static rt_err_t _i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t cmd, rt_uint32_t arg)
static rt_err_t _i2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
{
return -RT_EINVAL;
}
@ -130,4 +130,4 @@ int rt_hw_i2c_init(void)
return RT_EOK;
}
INIT_BOARD_EXPORT(rt_hw_i2c_init);
#endif /* RT_USING_I2C */
#endif /* RT_USING_I2C */

View File

@ -21,8 +21,8 @@ static rt_ssize_t air105_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t air105_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t,
rt_uint32_t);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops air105_i2c_ops =
{
@ -88,8 +88,8 @@ static rt_ssize_t air105_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
return -RT_ENOSYS;
}
static rt_err_t air105_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
RT_ASSERT(bus != RT_NULL);
@ -97,7 +97,7 @@ static rt_err_t air105_i2c_bus_control(struct rt_i2c_bus_device *bus,
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
I2C_MasterSetup(i2c_id, arg);
I2C_MasterSetup(i2c_id, *(rt_uint32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -90,11 +90,11 @@ rt_size_t rt_i2c_master_xfer(struct rt_i2c_bus_device *bus,
}
rt_err_t rt_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
struct am_i2c_bus * am_i2c_bus = (struct am_i2c_bus *)bus;
//rt_uint32_t ctrl_arg = (rt_uint32_t)(arg);
//rt_uint32_t ctrl_arg = *(rt_uint32_t *)args;
RT_ASSERT(bus != RT_NULL);
am_i2c_bus = (struct am_i2c_bus *)bus->parent.user_data;

View File

@ -141,8 +141,8 @@ static rt_ssize_t imxrt_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t imxrt_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t,
rt_uint32_t);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops imxrt_i2c_ops =
{
@ -372,8 +372,8 @@ static rt_ssize_t imxrt_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
return 0;
}
static rt_err_t imxrt_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
return -RT_ERROR;
}

View File

@ -57,8 +57,8 @@ rt_size_t rt_i2c_master_xfer(struct rt_i2c_bus_device *bus,
}
rt_err_t rt_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
struct ls1c_i2c_bus * i2c_bus = (struct ls1c_i2c_bus *)bus;

View File

@ -87,7 +87,7 @@ static rt_ssize_t slave_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ms
return -RT_ENOSYS;
}
static rt_err_t i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t cmd, rt_uint32_t arg)
static rt_err_t i2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
{
return -RT_EINVAL;
}

View File

@ -37,7 +37,7 @@ static rt_ssize_t sam_i2c_slave_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t sam_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t, rt_uint32_t);
int cmd, void *args);
static const struct rt_i2c_bus_device_ops sam_i2c_ops =
{
@ -90,8 +90,8 @@ static rt_ssize_t sam_i2c_slave_xfer(struct rt_i2c_bus_device *bus,
}
static rt_err_t sam_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
return -RT_ERROR;
struct sam_i2c_bus *sam_i2c = (struct sam_i2c_bus *)bus;
@ -101,7 +101,7 @@ static rt_err_t sam_i2c_bus_control(struct rt_i2c_bus_device *bus,
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
i2c_m_sync_set_baudrate(sam_i2c->i2c_desc, 0, arg);
i2c_m_sync_set_baudrate(sam_i2c->i2c_desc, 0, *(rt_uint32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -73,8 +73,8 @@ static rt_ssize_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t u32Cmd,
rt_uint32_t u32Value);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops nu_i2c_ops =
{
@ -83,17 +83,17 @@ static const struct rt_i2c_bus_device_ops nu_i2c_ops =
.i2c_bus_control = nu_i2c_bus_control
};
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t u32Cmd, rt_uint32_t u32Value)
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
{
nu_i2c_bus_t *nu_i2c;
RT_ASSERT(bus != RT_NULL);
nu_i2c = (nu_i2c_bus_t *) bus;
switch (u32Cmd)
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
I2C_SetBusClockFreq(nu_i2c->I2C, u32Value);
I2C_SetBusClockFreq(nu_i2c->I2C, *(rt_uint32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -82,8 +82,8 @@ static rt_ssize_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t u32Cmd,
rt_uint32_t u32Value);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops nu_i2c_ops =
{
@ -92,17 +92,17 @@ static const struct rt_i2c_bus_device_ops nu_i2c_ops =
.i2c_bus_control = nu_i2c_bus_control
};
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t u32Cmd, rt_uint32_t u32Value)
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
{
nu_i2c_bus_t *nu_i2c;
RT_ASSERT(bus != RT_NULL);
nu_i2c = (nu_i2c_bus_t *) bus;
switch (u32Cmd)
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
I2C_SetBusClockFreq(nu_i2c->I2C, u32Value);
I2C_SetBusClockFreq(nu_i2c->I2C, *(rt_uint32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -94,8 +94,8 @@ static rt_ssize_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t u32Cmd,
rt_uint32_t u32Value);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops nu_i2c_ops =
{
@ -104,17 +104,17 @@ static const struct rt_i2c_bus_device_ops nu_i2c_ops =
.i2c_bus_control = nu_i2c_bus_control
};
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t u32Cmd, rt_uint32_t u32Value)
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
{
nu_i2c_bus_t nu_i2c;
RT_ASSERT(bus);
nu_i2c = (nu_i2c_bus_t) bus;
switch (u32Cmd)
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
I2C_SetBusClockFreq(nu_i2c->I2C, u32Value);
I2C_SetBusClockFreq(nu_i2c->I2C, *(rt_uint32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -61,8 +61,8 @@ static rt_ssize_t nu_ui2c_mst_xfer(struct rt_i2c_bus_device *bus,
rt_uint32_t num);
static rt_err_t nu_ui2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t u32Cmd,
rt_uint32_t u32Value);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops nu_ui2c_ops =
{
@ -71,17 +71,17 @@ static const struct rt_i2c_bus_device_ops nu_ui2c_ops =
.i2c_bus_control = nu_ui2c_bus_control,
};
static rt_err_t nu_ui2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t u32Cmd, rt_uint32_t u32Value)
static rt_err_t nu_ui2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
{
nu_ui2c_bus_t nu_ui2c;
RT_ASSERT(bus);
nu_ui2c = (nu_ui2c_bus_t) bus;
switch (u32Cmd)
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
UI2C_SetBusClockFreq(nu_ui2c->UI2C, u32Value);
UI2C_SetBusClockFreq(nu_ui2c->UI2C, *(rt_uint32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -82,8 +82,8 @@ static rt_ssize_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t u32Cmd,
rt_uint32_t u32Value);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops nu_i2c_ops =
{
@ -92,17 +92,17 @@ static const struct rt_i2c_bus_device_ops nu_i2c_ops =
.i2c_bus_control = nu_i2c_bus_control
};
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t u32Cmd, rt_uint32_t u32Value)
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
{
nu_i2c_bus_t *nu_i2c;
RT_ASSERT(bus != RT_NULL);
nu_i2c = (nu_i2c_bus_t *) bus;
switch (u32Cmd)
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
I2C_SetBusClockFreq(nu_i2c->I2C, u32Value);
I2C_SetBusClockFreq(nu_i2c->I2C, *(rt_uint32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -103,8 +103,8 @@ static rt_ssize_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t u32Cmd,
rt_uint32_t u32Value);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops nu_i2c_ops =
{
@ -113,17 +113,17 @@ static const struct rt_i2c_bus_device_ops nu_i2c_ops =
.i2c_bus_control = nu_i2c_bus_control
};
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t u32Cmd, rt_uint32_t u32Value)
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, int u32Cmd, void *args)
{
nu_i2c_bus_t *nu_i2c;
RT_ASSERT(bus != RT_NULL);
nu_i2c = (nu_i2c_bus_t *) bus;
switch (u32Cmd)
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
I2C_SetBusClockFreq(nu_i2c->I2C, u32Value);
I2C_SetBusClockFreq(nu_i2c->I2C, *(rt_uint32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -506,7 +506,7 @@ static rt_ssize_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
return i;
}
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t u32Cmd, rt_uint32_t u32Value)
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
{
nu_i2c_bus_t psNuI2cBus;
nu_i2c_dev_t psNuI2cDev;
@ -516,10 +516,10 @@ static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t u3
psNuI2cBus = (nu_i2c_bus_t) bus;
psNuI2cDev = &psNuI2cBus->dev;
switch (u32Cmd)
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
nu_i2c_set_speed(psNuI2cDev, (int32_t)u32Value);
nu_i2c_set_speed(psNuI2cDev, *(int32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -101,8 +101,8 @@ static rt_ssize_t nu_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t u32Cmd,
rt_uint32_t u32Value);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops nu_i2c_ops =
{
@ -111,17 +111,17 @@ static const struct rt_i2c_bus_device_ops nu_i2c_ops =
.i2c_bus_control = nu_i2c_bus_control
};
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t u32Cmd, rt_uint32_t u32Value)
static rt_err_t nu_i2c_bus_control(struct rt_i2c_bus_device *bus, int cmd, void *args)
{
nu_i2c_bus_t *nu_i2c;
RT_ASSERT(bus != RT_NULL);
nu_i2c = (nu_i2c_bus_t *) bus;
switch (u32Cmd)
switch (cmd)
{
case RT_I2C_DEV_CTRL_CLK:
I2C_SetBusClockFreq(nu_i2c->I2C, u32Value);
I2C_SetBusClockFreq(nu_i2c->I2C, *(rt_uint32_t *)args);
break;
default:
return -RT_EIO;

View File

@ -110,8 +110,8 @@ static rt_ssize_t raspi_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t raspi_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t,
rt_uint32_t);
int cmd,
void *args);
static rt_uint32_t i2c_byte_wait_us = 0;
static rt_ssize_t raspi_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
@ -148,8 +148,8 @@ static rt_ssize_t raspi_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
return 0;
}
static rt_err_t raspi_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
return RT_EOK;
}

View File

@ -115,8 +115,8 @@ static rt_ssize_t raspi_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t raspi_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t,
rt_uint32_t);
int cmd,
void *args);
static rt_uint32_t i2c_byte_wait_us = 0;
static rt_ssize_t raspi_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
@ -153,8 +153,8 @@ static rt_ssize_t raspi_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
return 0;
}
static rt_err_t raspi_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
return RT_EOK;
}

View File

@ -162,8 +162,8 @@ static rt_ssize_t raspi_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
}
static rt_err_t raspi_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
return RT_EOK;
}

View File

@ -37,8 +37,8 @@ static rt_ssize_t wm_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t wm_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t,
rt_uint32_t);
int cmd,
void *args);
static const struct rt_i2c_bus_device_ops wm_i2c_ops =
{
@ -132,8 +132,8 @@ static rt_ssize_t wm_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
return 0;
}
static rt_err_t wm_i2c_bus_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
return -RT_ERROR;
}

View File

@ -89,14 +89,14 @@ rt_ssize_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
}
rt_err_t rt_i2c_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg)
int cmd,
void *args)
{
rt_err_t ret;
if(bus->ops->i2c_bus_control)
{
ret = bus->ops->i2c_bus_control(bus, cmd, arg);
ret = bus->ops->i2c_bus_control(bus, cmd, args);
return ret;
}
else

View File

@ -67,7 +67,6 @@ static rt_err_t i2c_bus_device_control(rt_device_t dev,
rt_err_t ret;
struct rt_i2c_priv_data *priv_data;
struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
rt_uint32_t bus_clock;
RT_ASSERT(bus != RT_NULL);
@ -88,16 +87,8 @@ static rt_err_t i2c_bus_device_control(rt_device_t dev,
return -RT_EIO;
}
break;
case RT_I2C_DEV_CTRL_CLK:
bus_clock = *(rt_uint32_t *)args;
ret = rt_i2c_control(bus, cmd, bus_clock);
if (ret < 0)
{
return -RT_EIO;
}
break;
default:
break;
return rt_i2c_control(bus, cmd, args);
}
return RT_EOK;

View File

@ -45,8 +45,8 @@ struct rt_i2c_bus_device_ops
struct rt_i2c_msg msgs[],
rt_uint32_t num);
rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
rt_uint32_t,
rt_uint32_t);
int cmd,
void *args);
};
/*for i2c bus driver*/
@ -74,8 +74,8 @@ rt_ssize_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
rt_err_t rt_i2c_control(struct rt_i2c_bus_device *bus,
rt_uint32_t cmd,
rt_uint32_t arg);
int cmd,
void *args);
rt_ssize_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
rt_uint16_t addr,
rt_uint16_t flags,

View File

@ -23,6 +23,10 @@ extern "C" {
#define RT_I2C_DEV_CTRL_TIMEOUT (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x03)
#define RT_I2C_DEV_CTRL_RW (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x04)
#define RT_I2C_DEV_CTRL_CLK (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x05)
#define RT_I2C_DEV_CTRL_UNLOCK (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x06)
#define RT_I2C_DEV_CTRL_GET_STATE (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x07)
#define RT_I2C_DEV_CTRL_GET_MODE (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x08)
#define RT_I2C_DEV_CTRL_GET_ERROR (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x09)
struct rt_i2c_priv_data
{