2012-11-22 16:43:40 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2012-11-22 16:43:40 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-11-22 16:43:40 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-04-25 weety first version
|
2021-04-20 00:26:28 +08:00
|
|
|
* 2021-04-20 RiceChen added support for bus control api
|
2012-11-22 16:43:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
2019-11-26 07:41:43 +08:00
|
|
|
#define DBG_TAG "I2C"
|
|
|
|
#ifdef RT_I2C_DEBUG
|
|
|
|
#define DBG_LVL DBG_LOG
|
|
|
|
#else
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#endif
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2012-11-22 16:43:40 +08:00
|
|
|
rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
|
|
|
|
const char *bus_name)
|
|
|
|
{
|
|
|
|
rt_err_t res = RT_EOK;
|
|
|
|
|
2021-11-18 04:57:15 +08:00
|
|
|
rt_mutex_init(&bus->lock, "i2c_bus_lock", RT_IPC_FLAG_PRIO);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2014-11-01 16:38:53 +08:00
|
|
|
if (bus->timeout == 0) bus->timeout = RT_TICK_PER_SECOND;
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
res = rt_i2c_bus_device_device_init(bus, bus_name);
|
|
|
|
|
2019-11-26 07:41:43 +08:00
|
|
|
LOG_I("I2C bus [%s] registered", bus_name);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name)
|
|
|
|
{
|
|
|
|
struct rt_i2c_bus_device *bus;
|
|
|
|
rt_device_t dev = rt_device_find(bus_name);
|
|
|
|
if (dev == RT_NULL || dev->type != RT_Device_Class_I2CBUS)
|
|
|
|
{
|
2019-11-26 07:41:43 +08:00
|
|
|
LOG_E("I2C bus %s not exist", bus_name);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bus = (struct rt_i2c_bus_device *)dev->user_data;
|
|
|
|
|
|
|
|
return bus;
|
|
|
|
}
|
|
|
|
|
2023-03-10 12:42:58 +08:00
|
|
|
rt_ssize_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
|
2012-11-22 16:43:40 +08:00
|
|
|
struct rt_i2c_msg msgs[],
|
|
|
|
rt_uint32_t num)
|
|
|
|
{
|
2023-03-10 12:42:58 +08:00
|
|
|
rt_ssize_t ret;
|
2023-03-11 12:44:03 +08:00
|
|
|
rt_err_t err;
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
if (bus->ops->master_xfer)
|
|
|
|
{
|
|
|
|
#ifdef RT_I2C_DEBUG
|
|
|
|
for (ret = 0; ret < num; ret++)
|
|
|
|
{
|
2019-11-26 07:41:43 +08:00
|
|
|
LOG_D("msgs[%d] %c, addr=0x%02x, len=%d", ret,
|
|
|
|
(msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W',
|
|
|
|
msgs[ret].addr, msgs[ret].len);
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
|
|
|
#endif
|
2023-03-11 12:44:03 +08:00
|
|
|
err = rt_mutex_take(&bus->lock, RT_WAITING_FOREVER);
|
|
|
|
if (err != RT_EOK)
|
|
|
|
{
|
|
|
|
return (rt_ssize_t)err;
|
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
ret = bus->ops->master_xfer(bus, msgs, num);
|
2023-03-11 12:44:03 +08:00
|
|
|
err = rt_mutex_release(&bus->lock);
|
|
|
|
if (err != RT_EOK)
|
|
|
|
{
|
|
|
|
return (rt_ssize_t)err;
|
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-26 07:41:43 +08:00
|
|
|
LOG_E("I2C bus operation not supported");
|
2023-03-11 12:44:03 +08:00
|
|
|
return -RT_EINVAL;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 00:26:28 +08:00
|
|
|
rt_err_t rt_i2c_control(struct rt_i2c_bus_device *bus,
|
2023-07-14 09:57:40 +08:00
|
|
|
int cmd,
|
|
|
|
void *args)
|
2021-04-20 00:26:28 +08:00
|
|
|
{
|
|
|
|
rt_err_t ret;
|
|
|
|
|
|
|
|
if(bus->ops->i2c_bus_control)
|
|
|
|
{
|
2023-07-14 09:57:40 +08:00
|
|
|
ret = bus->ops->i2c_bus_control(bus, cmd, args);
|
2021-04-20 00:26:28 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_E("I2C bus operation not supported");
|
2023-03-11 12:44:03 +08:00
|
|
|
return -RT_EINVAL;
|
2021-04-20 00:26:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 12:42:58 +08:00
|
|
|
rt_ssize_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
|
2012-11-22 16:43:40 +08:00
|
|
|
rt_uint16_t addr,
|
|
|
|
rt_uint16_t flags,
|
|
|
|
const rt_uint8_t *buf,
|
|
|
|
rt_uint32_t count)
|
|
|
|
{
|
2023-03-10 12:42:58 +08:00
|
|
|
rt_ssize_t ret;
|
2012-11-22 16:43:40 +08:00
|
|
|
struct rt_i2c_msg msg;
|
|
|
|
|
|
|
|
msg.addr = addr;
|
2019-09-06 14:44:50 +08:00
|
|
|
msg.flags = flags;
|
2012-11-22 16:43:40 +08:00
|
|
|
msg.len = count;
|
|
|
|
msg.buf = (rt_uint8_t *)buf;
|
|
|
|
|
|
|
|
ret = rt_i2c_transfer(bus, &msg, 1);
|
|
|
|
|
2023-05-09 13:13:02 +08:00
|
|
|
return (ret == 1) ? count : ret;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
|
|
|
|
2023-03-10 12:42:58 +08:00
|
|
|
rt_ssize_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
|
2012-11-22 16:43:40 +08:00
|
|
|
rt_uint16_t addr,
|
|
|
|
rt_uint16_t flags,
|
|
|
|
rt_uint8_t *buf,
|
|
|
|
rt_uint32_t count)
|
|
|
|
{
|
2023-03-10 12:42:58 +08:00
|
|
|
rt_ssize_t ret;
|
2012-11-22 16:43:40 +08:00
|
|
|
struct rt_i2c_msg msg;
|
|
|
|
RT_ASSERT(bus != RT_NULL);
|
|
|
|
|
|
|
|
msg.addr = addr;
|
2019-09-06 14:44:50 +08:00
|
|
|
msg.flags = flags | RT_I2C_RD;
|
2012-11-22 16:43:40 +08:00
|
|
|
msg.len = count;
|
|
|
|
msg.buf = buf;
|
|
|
|
|
|
|
|
ret = rt_i2c_transfer(bus, &msg, 1);
|
|
|
|
|
2023-05-09 13:13:02 +08:00
|
|
|
return (ret == 1) ? count : ret;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|