2012-06-14 20:18:52 +08:00
|
|
|
#include <rtdevice.h>
|
2012-05-23 01:32:32 +08:00
|
|
|
|
|
|
|
static rt_err_t i2c_bus_device_init(rt_device_t dev)
|
|
|
|
{
|
2012-06-14 20:18:52 +08:00
|
|
|
struct rt_i2c_bus_device* bus = (struct rt_i2c_bus_device *)dev->user_data;
|
|
|
|
RT_ASSERT(bus != RT_NULL);
|
2012-05-23 01:32:32 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
return RT_EOK;
|
2012-05-23 01:32:32 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
static rt_size_t i2c_bus_device_read (rt_device_t dev,
|
|
|
|
rt_off_t pos,
|
|
|
|
void *buffer,
|
|
|
|
rt_size_t count)
|
2012-05-23 01:32:32 +08:00
|
|
|
{
|
2012-06-14 20:18:52 +08:00
|
|
|
rt_uint16_t addr;
|
|
|
|
rt_uint16_t flags;
|
|
|
|
struct rt_i2c_bus_device* bus = (struct rt_i2c_bus_device *)dev->user_data;
|
2012-05-23 01:32:32 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
RT_ASSERT(bus != RT_NULL);
|
|
|
|
RT_ASSERT(buffer != RT_NULL);
|
2012-06-14 00:55:46 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
i2c_dbg("I2C bus dev [%s] reading %u bytes.\n", dev->parent.name, count);
|
2012-05-23 01:32:32 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
addr = pos & 0xffff;
|
|
|
|
flags = (pos >> 16) & 0xffff;
|
|
|
|
|
|
|
|
return rt_i2c_master_recv(bus, addr, flags, buffer, count);
|
2012-05-23 01:32:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
static rt_size_t i2c_bus_device_write (rt_device_t dev,
|
|
|
|
rt_off_t pos,
|
|
|
|
const void *buffer,
|
|
|
|
rt_size_t count)
|
2012-05-23 01:32:32 +08:00
|
|
|
{
|
2012-06-14 20:18:52 +08:00
|
|
|
rt_uint16_t addr;
|
|
|
|
rt_uint16_t flags;
|
|
|
|
struct rt_i2c_bus_device* bus = (struct rt_i2c_bus_device *)dev->user_data;
|
2012-06-14 00:55:46 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
RT_ASSERT(bus != RT_NULL);
|
|
|
|
RT_ASSERT(buffer != RT_NULL);
|
2012-05-23 01:32:32 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
i2c_dbg("I2C bus dev writing %u bytes.\n", dev->parent.name, count);
|
2012-06-14 00:55:46 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
addr = pos & 0xffff;
|
|
|
|
flags = (pos >> 16) & 0xffff;
|
2012-05-23 01:32:32 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
return rt_i2c_master_send(bus, addr, flags, buffer, count);
|
2012-05-23 01:32:32 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
static rt_err_t i2c_bus_device_control(rt_device_t dev,
|
|
|
|
rt_uint8_t cmd,
|
|
|
|
void *args)
|
2012-05-23 01:32:32 +08:00
|
|
|
{
|
2012-06-14 20:18:52 +08:00
|
|
|
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_ASSERT(bus != RT_NULL);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_I2C_DEV_CTRL_10BIT: /* set 10-bit addr mode */
|
|
|
|
bus->flags |= RT_I2C_ADDR_10BIT;
|
|
|
|
break;
|
|
|
|
case RT_I2C_DEV_CTRL_ADDR:
|
|
|
|
bus->addr = *(rt_uint16_t *)args;
|
|
|
|
break;
|
|
|
|
case RT_I2C_DEV_CTRL_TIMEOUT:
|
|
|
|
bus->timeout = *(rt_uint32_t *)args;
|
|
|
|
break;
|
|
|
|
case RT_I2C_DEV_CTRL_RW:
|
|
|
|
priv_data = (struct rt_i2c_priv_data *)args;
|
|
|
|
ret = rt_i2c_transfer(bus, priv_data->msgs, priv_data->number);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return -RT_EIO;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
2012-05-23 01:32:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device* bus,
|
|
|
|
const char* name)
|
2012-05-23 01:32:32 +08:00
|
|
|
{
|
2012-06-14 20:18:52 +08:00
|
|
|
struct rt_device *device;
|
|
|
|
RT_ASSERT(bus != RT_NULL);
|
|
|
|
|
|
|
|
device = &bus->parent;
|
2012-05-23 01:32:32 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
device->user_data = bus;
|
2012-05-23 01:32:32 +08:00
|
|
|
|
2012-06-14 20:18:52 +08:00
|
|
|
/* set device type */
|
|
|
|
device->type = RT_Device_Class_I2CBUS;
|
|
|
|
/* initialize device interface */
|
|
|
|
device->init = i2c_bus_device_init;
|
|
|
|
device->open = RT_NULL;
|
|
|
|
device->close = RT_NULL;
|
|
|
|
device->read = i2c_bus_device_read;
|
|
|
|
device->write = i2c_bus_device_write;
|
|
|
|
device->control = i2c_bus_device_control;
|
|
|
|
|
|
|
|
/* register to device manager */
|
|
|
|
rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|