ADD I2C driver framework and I2C gpio driver

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2128 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
luohui2320@gmail.com
2012-05-22 17:32:32 +00:00
parent c891da75df
commit 55460652d3
7 changed files with 1159 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
Import('RTT_ROOT')
from building import *
cwd = GetCurrentDir()
src = Split("""
i2c_core.c
i2c_dev.c
""")
if GetDepend('RT_USING_I2C_BITOPS'):
src = src + ['i2c-bit-ops.c']
# The set of source files associated with this SConscript file.
path = [cwd]
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_I2C'], CPPPATH = path)
Return('group')
File diff suppressed because it is too large Load Diff
+41
View File
@@ -0,0 +1,41 @@
/*
* File : i2c-bit-ops.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2012-04-25 weety first version
*/
#ifndef __I2C_BIT_OPS_H__
#define __I2C_BIT_OPS_H__
#ifdef __cplusplus
extern "C" {
#endif
struct rt_i2c_bit_ops {
void *data; /* private data for lowlevel routines */
void (*set_sda) (void *data, rt_int32_t state);
void (*set_scl) (void *data, rt_int32_t state);
rt_int32_t (*get_sda) (void *data);
rt_int32_t (*get_scl) (void *data);
void (*udelay) (rt_uint32_t us);
rt_uint32_t delay_us; /* scl and sda line delay */
rt_uint32_t timeout; /* in tick */
};
rt_err_t rt_i2c_bit_add_bus(struct rt_i2c_bus *bus);
#ifdef __cplusplus
}
#endif
#endif
+117
View File
@@ -0,0 +1,117 @@
/*
* File : i2c.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2012-04-25 weety first version
*/
#ifndef __I2C_H__
#define __I2C_H__
#include <rtthread.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef RT_I2C_NAME_SIZE
#define RT_I2C_NAME_SIZE 32
#endif
#define RT_I2C_WR 0x0000
#define RT_I2C_RD (1u << 0)
#define RT_I2C_ADDR_10BIT (1u << 2) /* this is a ten bit chip address */
#define RT_I2C_NO_START (1u << 4)
#define RT_I2C_IGNORE_NACK (1u << 5)
#define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
struct rt_i2c_msg {
rt_uint16_t addr;
rt_uint16_t flags;
rt_uint16_t len;
rt_uint8_t *buf;
};
struct rt_i2c_hardware_info {
char name[RT_I2C_NAME_SIZE];
rt_uint16_t flags;
rt_uint16_t addr;
rt_uint32_t bus_id;
rt_list_t list;
};
#define RT_I2C_HARDWARE_INFO(name, flags, addr, bus_id) \
name,flags,addr,bus_id,{RT_NULL,RT_NULL}
struct rt_i2c_bus;
struct rt_i2c_bus_ops {
rt_size_t (*master_xfer) (struct rt_i2c_bus *bus, struct rt_i2c_msg *msgs, rt_uint32_t num);
rt_size_t (*slave_xfer) (struct rt_i2c_bus *bus, struct rt_i2c_msg *msgs, rt_uint32_t num);
rt_err_t (*i2c_bus_control) (struct rt_i2c_bus *bus, rt_uint32_t, rt_uint32_t);
};
/*for i2c bus driver*/
struct rt_i2c_bus {
struct rt_device *parent;
char name[RT_I2C_NAME_SIZE];
rt_uint32_t id;
const struct rt_i2c_bus_ops *ops;
struct rt_mutex lock;
rt_list_t devices;
rt_list_t list;
rt_uint32_t timeout;
rt_uint32_t retries;
void *priv;
};
struct rt_i2c_device;
struct rt_i2c_driver {
char name[RT_I2C_NAME_SIZE];
rt_err_t (*probe)(struct rt_i2c_device *device);
rt_err_t (*remove)(struct rt_i2c_device *device);
rt_list_t devices;
};
/*for i2c device driver*/
struct rt_i2c_device {
rt_uint32_t flags;
rt_uint16_t addr;
struct rt_i2c_bus *bus;
struct rt_i2c_driver *driver;
struct rt_device dev;
rt_list_t drv_list;
rt_list_t bus_list;
};
#ifdef RT_I2C_DEBUG
#define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
#else
#define i2c_dbg(fmt, ...)
#endif
rt_err_t rt_i2c_bus_register(struct rt_i2c_bus *bus);
rt_err_t rt_i2c_bus_unregister(struct rt_i2c_bus *bus);
void rt_i2c_hw_info_register(struct rt_i2c_hardware_info *info, rt_uint32_t size);
rt_err_t rt_i2c_bus_attach_driver(struct rt_i2c_driver *driver);
rt_err_t rt_i2c_bus_detach_driver(struct rt_i2c_driver *driver);
rt_size_t rt_i2c_transfer(struct rt_i2c_bus *bus,
struct rt_i2c_msg *msgs, rt_uint32_t size);
rt_size_t rt_i2c_master_send(struct rt_i2c_device *device,
const rt_uint8_t *buf, rt_uint32_t size);
rt_size_t rt_i2c_master_recv(struct rt_i2c_device *device,
rt_uint8_t *buf ,rt_uint32_t size);
rt_err_t rt_i2c_core_init();
#ifdef __cplusplus
}
#endif
#endif
File diff suppressed because it is too large Load Diff
+137
View File
@@ -0,0 +1,137 @@
#include <i2c.h>
#include <i2c_dev.h>
static rt_err_t i2c_bus_device_init(rt_device_t dev)
{
struct rt_i2c_bus* bus;
struct rt_i2c_device *i2c_device = dev->user_data;
bus = i2c_device->bus;
RT_ASSERT(bus != RT_NULL);
return RT_EOK;
}
static rt_size_t i2c_bus_device_read (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t count)
{
struct rt_i2c_bus* bus;
struct rt_i2c_device *i2c_device = dev->user_data;
bus = i2c_device->bus;
RT_ASSERT(bus != RT_NULL);
RT_ASSERT(i2c_device != RT_NULL);
RT_ASSERT(buffer != RT_NULL);
i2c_dbg("I2C bus dev [%s] reading %u bytes.\n", bus->name, count);
return rt_i2c_master_recv(i2c_device, buffer, count);
}
static rt_size_t i2c_bus_device_write (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
struct rt_i2c_bus* bus;
struct rt_i2c_device *i2c_device = dev->user_data;
bus = i2c_device->bus;
RT_ASSERT(bus != RT_NULL);
RT_ASSERT(i2c_device != RT_NULL);
RT_ASSERT(buffer != RT_NULL);
i2c_dbg("I2C bus dev writing %u bytes.\n", bus->name, size);
return rt_i2c_master_send(i2c_device, buffer, size);
}
static rt_err_t i2c_bus_device_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{
rt_err_t ret;
struct rt_i2c_bus* bus;
struct rt_i2c_priv_data *priv_data;
struct rt_i2c_device *i2c_device = dev->user_data;
bus = i2c_device->bus;
RT_ASSERT(bus != RT_NULL);
RT_ASSERT(i2c_device != RT_NULL);
switch (cmd)
{
case RT_I2C_DEV_CTRL_10BIT: /* set 10-bit addr mode */
i2c_device->flags |= RT_I2C_ADDR_10BIT;
break;
case RT_I2C_DEV_CTRL_ADDR:
i2c_device->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;
}
rt_err_t rt_i2c_bus_device_init(struct rt_i2c_bus* bus, const char* name)
{
struct rt_device *device;
struct rt_i2c_device *i2c_device;
RT_ASSERT(bus != RT_NULL);
//device = &bus->parent;
i2c_device = rt_malloc(sizeof(struct rt_i2c_device));
if (i2c_device == RT_NULL)
{
return -RT_ENOMEM;
}
rt_memset(i2c_device, 0, sizeof(struct rt_i2c_device));
device = &i2c_device->dev;
i2c_device->bus = bus;
bus->parent = device;
device->user_data = i2c_device;
/* 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;
}
rt_err_t rt_i2c_bus_device_exit(struct rt_i2c_bus* bus)
{
struct rt_device *device;
struct rt_i2c_device *i2c_device;
RT_ASSERT(bus != RT_NULL);
device = bus->parent;
i2c_device = device->user_data;;
/* register to device manager */
rt_device_unregister(device);
rt_free(i2c_device);
return RT_EOK;
}
+28
View File
@@ -0,0 +1,28 @@
#ifndef __I2C_DEV_H__
#define __I2C_DEV_H__
#include <rtthread.h>
#ifdef __cplusplus
extern "C" {
#endif
#define RT_I2C_DEV_CTRL_10BIT 0x20
#define RT_I2C_DEV_CTRL_ADDR 0x21
#define RT_I2C_DEV_CTRL_TIMEOUT 0x22
#define RT_I2C_DEV_CTRL_RW 0x23
struct rt_i2c_priv_data {
struct rt_i2c_msg *msgs;
rt_size_t number;
};
rt_err_t rt_i2c_bus_device_init(struct rt_i2c_bus* bus, const char* name);
rt_err_t rt_i2c_bus_device_exit(struct rt_i2c_bus* bus);
#ifdef __cplusplus
}
#endif
#endif