mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-02-21 01:07:18 +08:00
fixed the coding style in the components/drivers
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2431 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
871951930b
commit
68b7e7c481
@ -1,30 +1,29 @@
|
||||
/*
|
||||
* File : i2c-bit-ops.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, RT-Thread Development Team
|
||||
* COPYRIGHT (C) 2006 - 2012, 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
|
||||
* Date Author Notes
|
||||
* 2012-04-25 weety first version
|
||||
*/
|
||||
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef RT_I2C_BIT_DEBUG
|
||||
#define bit_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
|
||||
#define bit_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define bit_dbg(fmt, ...)
|
||||
#endif
|
||||
|
||||
|
||||
#define SET_SDA(ops, val) ops->set_sda(ops->data, val)
|
||||
#define SET_SCL(ops, val) ops->set_scl(ops->data, val)
|
||||
#define GET_SDA(ops) ops->get_sda(ops->data)
|
||||
#define GET_SCL(ops) ops->get_scl(ops->data)
|
||||
#define SET_SDA(ops, val) ops->set_sda(ops->data, val)
|
||||
#define SET_SCL(ops, val) ops->set_scl(ops->data, val)
|
||||
#define GET_SDA(ops) ops->get_sda(ops->data)
|
||||
#define GET_SCL(ops) ops->get_scl(ops->data)
|
||||
|
||||
rt_inline void i2c_delay(struct rt_i2c_bit_ops *ops)
|
||||
{
|
||||
@ -36,11 +35,11 @@ rt_inline void i2c_delay2(struct rt_i2c_bit_ops *ops)
|
||||
ops->udelay(ops->delay_us);
|
||||
}
|
||||
|
||||
#define SDA_L(ops) SET_SDA(ops, 0)
|
||||
#define SDA_H(ops) SET_SDA(ops, 1)
|
||||
#define SCL_L(ops) SET_SCL(ops, 0)
|
||||
#define SDA_L(ops) SET_SDA(ops, 0)
|
||||
#define SDA_H(ops) SET_SDA(ops, 1)
|
||||
#define SCL_L(ops) SET_SCL(ops, 0)
|
||||
|
||||
/*
|
||||
/**
|
||||
* release scl line, and wait scl line to high.
|
||||
*/
|
||||
static rt_err_t SCL_H(struct rt_i2c_bit_ops *ops)
|
||||
@ -73,7 +72,6 @@ done:
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
||||
static void i2c_start(struct rt_i2c_bit_ops *ops)
|
||||
{
|
||||
#ifdef RT_I2C_BIT_DEBUG
|
||||
@ -101,7 +99,6 @@ static void i2c_restart(struct rt_i2c_bit_ops *ops)
|
||||
SCL_L(ops);
|
||||
}
|
||||
|
||||
|
||||
static void i2c_stop(struct rt_i2c_bit_ops *ops)
|
||||
{
|
||||
SDA_L(ops);
|
||||
@ -122,6 +119,7 @@ rt_inline rt_bool_t i2c_waitack(struct rt_i2c_bit_ops *ops)
|
||||
if (SCL_H(ops) < 0)
|
||||
{
|
||||
bit_dbg("wait ack timeout\n");
|
||||
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
@ -133,7 +131,6 @@ rt_inline rt_bool_t i2c_waitack(struct rt_i2c_bit_ops *ops)
|
||||
return ack;
|
||||
}
|
||||
|
||||
|
||||
static rt_int32_t i2c_writeb(struct rt_i2c_bus_device *bus, rt_uint8_t data)
|
||||
{
|
||||
rt_int32_t i;
|
||||
@ -152,9 +149,9 @@ static rt_int32_t i2c_writeb(struct rt_i2c_bus_device *bus, rt_uint8_t data)
|
||||
bit_dbg("i2c_writeb: 0x%02x, "
|
||||
"wait scl pin high timeout at bit %d\n",
|
||||
data, i);
|
||||
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
}
|
||||
SCL_L(ops);
|
||||
i2c_delay(ops);
|
||||
@ -162,7 +159,6 @@ static rt_int32_t i2c_writeb(struct rt_i2c_bus_device *bus, rt_uint8_t data)
|
||||
return i2c_waitack(ops);
|
||||
}
|
||||
|
||||
|
||||
static rt_int32_t i2c_readb(struct rt_i2c_bus_device *bus)
|
||||
{
|
||||
rt_uint8_t i;
|
||||
@ -179,6 +175,7 @@ static rt_int32_t i2c_readb(struct rt_i2c_bus_device *bus)
|
||||
{
|
||||
bit_dbg("i2c_readb: wait scl pin high "
|
||||
"timeout at bit %d\n", 7 - i);
|
||||
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
@ -191,8 +188,8 @@ static rt_int32_t i2c_readb(struct rt_i2c_bus_device *bus)
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
static rt_size_t i2c_send_bytes(struct rt_i2c_bus_device *bus, struct rt_i2c_msg *msg)
|
||||
static rt_size_t i2c_send_bytes(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg *msg)
|
||||
{
|
||||
rt_int32_t ret;
|
||||
rt_size_t bytes = 0;
|
||||
@ -206,21 +203,24 @@ static rt_size_t i2c_send_bytes(struct rt_i2c_bus_device *bus, struct rt_i2c_msg
|
||||
|
||||
if ((ret > 0) || (ignore_nack && (ret == 0)))
|
||||
{
|
||||
count--;
|
||||
ptr++;
|
||||
bytes++;
|
||||
count --;
|
||||
ptr ++;
|
||||
bytes ++;
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
i2c_dbg("send bytes: NACK.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
i2c_dbg("send bytes: error %d\n", ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@ -234,16 +234,19 @@ static rt_err_t i2c_send_ack_or_nack(struct rt_i2c_bus_device *bus, int ack)
|
||||
if (SCL_H(ops) < 0)
|
||||
{
|
||||
bit_dbg("ACK or NACK timeout\n");
|
||||
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
SCL_L(ops);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_size_t i2c_recv_bytes(struct rt_i2c_bus_device *bus, struct rt_i2c_msg *msg)
|
||||
static rt_size_t i2c_recv_bytes(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg *msg)
|
||||
{
|
||||
rt_int32_t val;
|
||||
rt_int32_t bytes = 0; /* actual bytes */
|
||||
rt_int32_t bytes = 0; /* actual bytes */
|
||||
rt_uint8_t *ptr = msg->buf;
|
||||
rt_int32_t count = msg->len;
|
||||
const rt_uint32_t flags = msg->flags;
|
||||
@ -254,15 +257,15 @@ static rt_size_t i2c_recv_bytes(struct rt_i2c_bus_device *bus, struct rt_i2c_msg
|
||||
if (val >= 0)
|
||||
{
|
||||
*ptr = val;
|
||||
bytes++;
|
||||
bytes ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ptr++;
|
||||
count--;
|
||||
ptr ++;
|
||||
count --;
|
||||
|
||||
bit_dbg("recieve bytes: 0x%02x, %s\n",
|
||||
val, (flags & RT_I2C_NO_READ_ACK) ?
|
||||
@ -275,11 +278,13 @@ static rt_size_t i2c_recv_bytes(struct rt_i2c_bus_device *bus, struct rt_i2c_msg
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static rt_int32_t i2c_send_address(struct rt_i2c_bus_device *bus,
|
||||
rt_uint8_t addr, rt_int32_t retries)
|
||||
rt_uint8_t addr,
|
||||
rt_int32_t retries)
|
||||
{
|
||||
struct rt_i2c_bit_ops *ops = bus->priv;
|
||||
rt_int32_t i;
|
||||
@ -300,7 +305,8 @@ static rt_int32_t i2c_send_address(struct rt_i2c_bus_device *bus,
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, struct rt_i2c_msg *msg)
|
||||
static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg *msg)
|
||||
{
|
||||
rt_uint16_t flags = msg->flags;
|
||||
rt_uint16_t ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
|
||||
@ -308,7 +314,7 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, struct rt_i2
|
||||
|
||||
rt_uint8_t addr1, addr2;
|
||||
rt_int32_t retries;
|
||||
rt_err_t ret;
|
||||
rt_err_t ret;
|
||||
|
||||
retries = ignore_nack ? 0 : bus->retries;
|
||||
|
||||
@ -323,6 +329,7 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, struct rt_i2
|
||||
if ((ret != 1) && !ignore_nack)
|
||||
{
|
||||
bit_dbg("NACK: sending first addr\n");
|
||||
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
@ -330,6 +337,7 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, struct rt_i2
|
||||
if ((ret != 1) && !ignore_nack)
|
||||
{
|
||||
bit_dbg("NACK: sending second addr\n");
|
||||
|
||||
return -RT_EIO;
|
||||
}
|
||||
if (flags & RT_I2C_RD)
|
||||
@ -341,6 +349,7 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, struct rt_i2
|
||||
if ((ret != 1) && !ignore_nack)
|
||||
{
|
||||
bit_dbg("NACK: sending repeated addr\n");
|
||||
|
||||
return -RT_EIO;
|
||||
}
|
||||
}
|
||||
@ -359,9 +368,9 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, struct rt_i2
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
||||
static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg msgs[], rt_uint32_t num)
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num)
|
||||
{
|
||||
struct rt_i2c_msg *msg;
|
||||
struct rt_i2c_bit_ops *ops = bus->priv;
|
||||
@ -392,8 +401,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
|
||||
{
|
||||
ret = i2c_recv_bytes(bus, msg);
|
||||
if (ret >= 1)
|
||||
bit_dbg("read %d byte%s\n",
|
||||
ret, ret == 1 ? "" : "s");
|
||||
bit_dbg("read %d byte%s\n", ret, ret == 1 ? "" : "s");
|
||||
if (ret < msg->len)
|
||||
{
|
||||
if (ret >= 0)
|
||||
@ -405,8 +413,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus,
|
||||
{
|
||||
ret = i2c_send_bytes(bus, msg);
|
||||
if (ret >= 1)
|
||||
bit_dbg("write %d byte%s\n",
|
||||
ret, ret == 1 ? "" : "s");
|
||||
bit_dbg("write %d byte%s\n", ret, ret == 1 ? "" : "s");
|
||||
if (ret < msg->len)
|
||||
{
|
||||
if (ret >= 0)
|
||||
@ -424,7 +431,6 @@ out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static const struct rt_i2c_bus_device_ops i2c_bit_bus_ops =
|
||||
{
|
||||
i2c_bit_xfer,
|
||||
@ -432,8 +438,8 @@ static const struct rt_i2c_bus_device_ops i2c_bit_bus_ops =
|
||||
RT_NULL
|
||||
};
|
||||
|
||||
|
||||
rt_err_t rt_i2c_bit_add_bus(struct rt_i2c_bus_device *bus, const char *bus_name)
|
||||
rt_err_t rt_i2c_bit_add_bus(struct rt_i2c_bus_device *bus,
|
||||
const char *bus_name)
|
||||
{
|
||||
struct rt_i2c_bit_ops *bit_ops = bus->priv;
|
||||
RT_ASSERT(bit_ops != RT_NULL);
|
||||
|
@ -1,15 +1,15 @@
|
||||
/*
|
||||
* File : i2c_core.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, RT-Thread Development Team
|
||||
* COPYRIGHT (C) 2006 - 2012, 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
|
||||
* Date Author Notes
|
||||
* 2012-04-25 weety first version
|
||||
*/
|
||||
|
||||
#include <rtdevice.h>
|
||||
@ -17,7 +17,7 @@
|
||||
static struct rt_mutex i2c_core_lock;
|
||||
|
||||
rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
|
||||
const char *bus_name)
|
||||
const char *bus_name)
|
||||
{
|
||||
rt_err_t res = RT_EOK;
|
||||
|
||||
@ -33,17 +33,18 @@ rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
|
||||
i2c_dbg("I2C bus [%s] registered\n", bus_name);
|
||||
|
||||
rt_mutex_release(&i2c_core_lock);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
struct rt_i2c_bus_device* rt_i2c_bus_device_find(const char *bus_name)
|
||||
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)
|
||||
{
|
||||
i2c_dbg("I2C bus %s not exist\n", bus_name);
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
@ -52,10 +53,9 @@ struct rt_i2c_bus_device* rt_i2c_bus_device_find(const char *bus_name)
|
||||
return bus;
|
||||
}
|
||||
|
||||
|
||||
rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num)
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num)
|
||||
{
|
||||
rt_size_t ret;
|
||||
|
||||
@ -79,57 +79,53 @@ rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
|
||||
else
|
||||
{
|
||||
i2c_dbg("I2C bus operation not supported\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
const rt_uint8_t *buf,
|
||||
rt_uint32_t count)
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
const rt_uint8_t *buf,
|
||||
rt_uint32_t count)
|
||||
{
|
||||
rt_size_t ret;
|
||||
struct rt_i2c_msg msg;
|
||||
|
||||
msg.addr = addr;
|
||||
msg.addr = addr;
|
||||
msg.flags = flags & RT_I2C_ADDR_10BIT;
|
||||
msg.len = count;
|
||||
msg.buf = (rt_uint8_t *)buf;
|
||||
msg.len = count;
|
||||
msg.buf = (rt_uint8_t *)buf;
|
||||
|
||||
ret = rt_i2c_transfer(bus, &msg, 1);
|
||||
|
||||
return (ret > 0) ? count : ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t count)
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t count)
|
||||
{
|
||||
rt_size_t ret;
|
||||
struct rt_i2c_msg msg;
|
||||
RT_ASSERT(bus != RT_NULL);
|
||||
|
||||
msg.addr = addr;
|
||||
msg.flags = flags & RT_I2C_ADDR_10BIT;
|
||||
msg.addr = addr;
|
||||
msg.flags = flags & RT_I2C_ADDR_10BIT;
|
||||
msg.flags |= RT_I2C_RD;
|
||||
msg.len = count;
|
||||
msg.buf = buf;
|
||||
msg.len = count;
|
||||
msg.buf = buf;
|
||||
|
||||
ret = rt_i2c_transfer(bus, &msg, 1);
|
||||
|
||||
return (ret > 0) ? count : ret;
|
||||
}
|
||||
|
||||
|
||||
rt_err_t rt_i2c_core_init(void)
|
||||
{
|
||||
|
||||
return rt_mutex_init (&i2c_core_lock, "i2c_core_lock", RT_IPC_FLAG_FIFO);
|
||||
return rt_mutex_init(&i2c_core_lock, "i2c_core_lock", RT_IPC_FLAG_FIFO);
|
||||
}
|
||||
|
||||
|
@ -1,21 +1,35 @@
|
||||
/*
|
||||
* File : i2c_dev.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2012, 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
|
||||
*/
|
||||
|
||||
#include <rtdevice.h>
|
||||
|
||||
static rt_err_t i2c_bus_device_init(rt_device_t dev)
|
||||
{
|
||||
struct rt_i2c_bus_device* bus = (struct rt_i2c_bus_device *)dev->user_data;
|
||||
struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
|
||||
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)
|
||||
static rt_size_t i2c_bus_device_read(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t count)
|
||||
{
|
||||
rt_uint16_t addr;
|
||||
rt_uint16_t flags;
|
||||
struct rt_i2c_bus_device* bus = (struct rt_i2c_bus_device *)dev->user_data;
|
||||
struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
|
||||
|
||||
RT_ASSERT(bus != RT_NULL);
|
||||
RT_ASSERT(buffer != RT_NULL);
|
||||
@ -28,15 +42,14 @@ static rt_size_t i2c_bus_device_read (rt_device_t dev,
|
||||
return rt_i2c_master_recv(bus, addr, flags, buffer, count);
|
||||
}
|
||||
|
||||
|
||||
static rt_size_t i2c_bus_device_write (rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t count)
|
||||
static rt_size_t i2c_bus_device_write(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t count)
|
||||
{
|
||||
rt_uint16_t addr;
|
||||
rt_uint16_t flags;
|
||||
struct rt_i2c_bus_device* bus = (struct rt_i2c_bus_device *)dev->user_data;
|
||||
struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
|
||||
|
||||
RT_ASSERT(bus != RT_NULL);
|
||||
RT_ASSERT(buffer != RT_NULL);
|
||||
@ -50,18 +63,19 @@ static rt_size_t i2c_bus_device_write (rt_device_t dev,
|
||||
}
|
||||
|
||||
static rt_err_t i2c_bus_device_control(rt_device_t dev,
|
||||
rt_uint8_t cmd,
|
||||
void *args)
|
||||
rt_uint8_t cmd,
|
||||
void *args)
|
||||
{
|
||||
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;
|
||||
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 */
|
||||
/* set 10-bit addr mode */
|
||||
case RT_I2C_DEV_CTRL_10BIT:
|
||||
bus->flags |= RT_I2C_ADDR_10BIT;
|
||||
break;
|
||||
case RT_I2C_DEV_CTRL_ADDR:
|
||||
@ -85,9 +99,8 @@ static rt_err_t i2c_bus_device_control(rt_device_t dev,
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
||||
rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device* bus,
|
||||
const char* name)
|
||||
rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
|
||||
const char *name)
|
||||
{
|
||||
struct rt_device *device;
|
||||
RT_ASSERT(bus != RT_NULL);
|
||||
@ -99,11 +112,11 @@ rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device* bus,
|
||||
/* 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->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 */
|
||||
|
@ -1,15 +1,15 @@
|
||||
/*
|
||||
* File : i2c-bit-ops.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, RT-Thread Development Team
|
||||
* COPYRIGHT (C) 2006 - 2012, 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
|
||||
* Date Author Notes
|
||||
* 2012-04-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __I2C_BIT_OPS_H__
|
||||
@ -21,20 +21,20 @@ extern "C" {
|
||||
|
||||
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 *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);
|
||||
void (*udelay)(rt_uint32_t us);
|
||||
|
||||
rt_uint32_t delay_us; /* scl and sda line delay */
|
||||
rt_uint32_t timeout; /* in tick */
|
||||
rt_uint32_t timeout; /* in tick */
|
||||
};
|
||||
|
||||
rt_err_t rt_i2c_bit_add_bus(struct rt_i2c_bus_device *bus,
|
||||
const char *bus_name);
|
||||
const char *bus_name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
/*
|
||||
* File : i2c.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006, RT-Thread Development Team
|
||||
* COPYRIGHT (C) 2006 - 2012, 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
|
||||
* Date Author Notes
|
||||
* 2012-04-25 weety first version
|
||||
*/
|
||||
|
||||
#ifndef __I2C_H__
|
||||
@ -40,15 +40,15 @@ struct rt_i2c_bus_device;
|
||||
|
||||
struct rt_i2c_bus_device_ops
|
||||
{
|
||||
rt_size_t (*master_xfer) (struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num);
|
||||
rt_size_t (*slave_xfer) (struct rt_i2c_bus_device *bus,
|
||||
rt_size_t (*master_xfer)(struct rt_i2c_bus_device *bus,
|
||||
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);
|
||||
rt_size_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
|
||||
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);
|
||||
};
|
||||
|
||||
/*for i2c bus driver*/
|
||||
@ -65,27 +65,27 @@ struct rt_i2c_bus_device
|
||||
};
|
||||
|
||||
#ifdef RT_I2C_DEBUG
|
||||
#define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
|
||||
#define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define i2c_dbg(fmt, ...)
|
||||
#endif
|
||||
|
||||
rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
|
||||
const char *bus_name);
|
||||
struct rt_i2c_bus_device* rt_i2c_bus_device_find(const char *bus_name);
|
||||
const char *bus_name);
|
||||
struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
|
||||
rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num);
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num);
|
||||
rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
const rt_uint8_t *buf,
|
||||
rt_uint32_t count);
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
const rt_uint8_t *buf,
|
||||
rt_uint32_t count);
|
||||
rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t count);
|
||||
rt_uint16_t addr,
|
||||
rt_uint16_t flags,
|
||||
rt_uint8_t *buf,
|
||||
rt_uint32_t count);
|
||||
rt_err_t rt_i2c_core_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -1,3 +1,17 @@
|
||||
/*
|
||||
* File : i2c_dev.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2012, 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_DEV_H__
|
||||
#define __I2C_DEV_H__
|
||||
|
||||
@ -18,9 +32,8 @@ struct rt_i2c_priv_data
|
||||
rt_size_t number;
|
||||
};
|
||||
|
||||
rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device* bus,
|
||||
const char* name);
|
||||
|
||||
rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
|
||||
const char *name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -20,59 +20,68 @@
|
||||
|
||||
#ifdef RT_USING_MTD_NAND
|
||||
|
||||
/*
|
||||
/**
|
||||
* RT-Thread Generic Device Interface
|
||||
*/
|
||||
static rt_err_t _mtd_init(rt_device_t dev)
|
||||
{
|
||||
return RT_EOK;
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t _mtd_open(rt_device_t dev, rt_uint16_t oflag)
|
||||
{
|
||||
return RT_EOK;
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t _mtd_close(rt_device_t dev)
|
||||
{
|
||||
return RT_EOK;
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_size_t _mtd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
||||
static rt_size_t _mtd_read(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
return size;
|
||||
return size;
|
||||
}
|
||||
|
||||
static rt_size_t _mtd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
|
||||
static rt_size_t _mtd_write(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
return size;
|
||||
return size;
|
||||
}
|
||||
|
||||
static rt_err_t _mtd_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
||||
{
|
||||
return RT_EOK;
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_mtd_nand_register_device(const char* name, struct rt_mtd_nand_device* device)
|
||||
rt_err_t rt_mtd_nand_register_device(const char *name,
|
||||
struct rt_mtd_nand_device *device)
|
||||
{
|
||||
rt_device_t dev;
|
||||
rt_device_t dev;
|
||||
|
||||
dev = RT_DEVICE(device);
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
dev = RT_DEVICE(device);
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
/* set device class and generic device interface */
|
||||
dev->type = RT_Device_Class_MTD;
|
||||
dev->init = _mtd_init;
|
||||
dev->open = _mtd_open;
|
||||
dev->read = _mtd_read;
|
||||
dev->write = _mtd_write;
|
||||
dev->close = _mtd_close;
|
||||
dev->control = _mtd_control;
|
||||
/* set device class and generic device interface */
|
||||
dev->type = RT_Device_Class_MTD;
|
||||
dev->init = _mtd_init;
|
||||
dev->open = _mtd_open;
|
||||
dev->read = _mtd_read;
|
||||
dev->write = _mtd_write;
|
||||
dev->close = _mtd_close;
|
||||
dev->control = _mtd_control;
|
||||
|
||||
dev->rx_indicate = RT_NULL;
|
||||
dev->tx_complete = RT_NULL;
|
||||
dev->rx_indicate = RT_NULL;
|
||||
dev->tx_complete = RT_NULL;
|
||||
|
||||
/* register to RT-Thread device system */
|
||||
return rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
|
||||
/* register to RT-Thread device system */
|
||||
return rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -16,59 +16,67 @@
|
||||
|
||||
#ifdef RT_USING_MTD_NOR
|
||||
|
||||
/*
|
||||
/**
|
||||
* RT-Thread Generic Device Interface
|
||||
*/
|
||||
static rt_err_t _mtd_init(rt_device_t dev)
|
||||
{
|
||||
return RT_EOK;
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t _mtd_open(rt_device_t dev, rt_uint16_t oflag)
|
||||
{
|
||||
return RT_EOK;
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t _mtd_close(rt_device_t dev)
|
||||
{
|
||||
return RT_EOK;
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_size_t _mtd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
||||
static rt_size_t _mtd_read(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
return size;
|
||||
return size;
|
||||
}
|
||||
|
||||
static rt_size_t _mtd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
|
||||
static rt_size_t _mtd_write(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
return size;
|
||||
return size;
|
||||
}
|
||||
|
||||
static rt_err_t _mtd_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
||||
{
|
||||
return RT_EOK;
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_mtd_nor_register_device(const char* name, struct rt_mtd_nor_device* device)
|
||||
rt_err_t rt_mtd_nor_register_device(const char *name,
|
||||
struct rt_mtd_nor_device *device)
|
||||
{
|
||||
rt_device_t dev;
|
||||
rt_device_t dev;
|
||||
|
||||
dev = RT_DEVICE(device);
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
dev = RT_DEVICE(device);
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
/* set device class and generic device interface */
|
||||
dev->type = RT_Device_Class_MTD;
|
||||
dev->init = _mtd_init;
|
||||
dev->open = _mtd_open;
|
||||
dev->read = _mtd_read;
|
||||
dev->write = _mtd_write;
|
||||
dev->close = _mtd_close;
|
||||
dev->control = _mtd_control;
|
||||
/* set device class and generic device interface */
|
||||
dev->type = RT_Device_Class_MTD;
|
||||
dev->init = _mtd_init;
|
||||
dev->open = _mtd_open;
|
||||
dev->read = _mtd_read;
|
||||
dev->write = _mtd_write;
|
||||
dev->close = _mtd_close;
|
||||
dev->control = _mtd_control;
|
||||
|
||||
dev->rx_indicate = RT_NULL;
|
||||
dev->tx_complete = RT_NULL;
|
||||
dev->rx_indicate = RT_NULL;
|
||||
dev->tx_complete = RT_NULL;
|
||||
|
||||
/* register to RT-Thread device system */
|
||||
return rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
|
||||
/* register to RT-Thread device system */
|
||||
return rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -89,6 +89,7 @@ rt_err_t rt_completion_wait(struct rt_completion *completion,
|
||||
|
||||
__exit:
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -46,9 +46,11 @@ rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
|
||||
size = rb->buffer_size - (rb->write_index - rb->read_index);
|
||||
|
||||
/* no space */
|
||||
if (size == 0) return 0;
|
||||
if (size == 0)
|
||||
return 0;
|
||||
/* drop some data */
|
||||
if (size < length) length = size;
|
||||
if (size < length)
|
||||
length = size;
|
||||
|
||||
write_position = (rb->write_index & mask);
|
||||
if (rb->buffer_size - write_position> length)
|
||||
@ -72,7 +74,7 @@ RTM_EXPORT(rt_ringbuffer_put);
|
||||
/**
|
||||
* put a character into ring buffer
|
||||
*/
|
||||
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb, const rt_uint8_t ch)
|
||||
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
|
||||
{
|
||||
rt_uint16_t mask;
|
||||
|
||||
@ -81,7 +83,8 @@ rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb, const rt_uint8_t ch)
|
||||
mask = rb->buffer_size - 1;
|
||||
|
||||
/* whether has enough space */
|
||||
if (rb->write_index - rb->read_index == rb->buffer_size) return 0;
|
||||
if (rb->write_index - rb->read_index == rb->buffer_size)
|
||||
return 0;
|
||||
|
||||
/* put character */
|
||||
rb->buffer_ptr[rb->write_index & mask] = ch;
|
||||
@ -108,9 +111,11 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
|
||||
size = rb->write_index - rb->read_index;
|
||||
|
||||
/* no data */
|
||||
if (size == 0) return 0;
|
||||
if (size == 0)
|
||||
return 0;
|
||||
/* less data */
|
||||
if (size < length) length = size;
|
||||
if (size < length)
|
||||
length = size;
|
||||
|
||||
read_position = rb->read_index & mask;
|
||||
if (rb->buffer_size - read_position >= length)
|
||||
@ -142,7 +147,8 @@ rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
|
||||
/* ringbuffer is empty */
|
||||
if (rb->read_index == rb->write_index) return 0;
|
||||
if (rb->read_index == rb->write_index)
|
||||
return 0;
|
||||
|
||||
mask = rb->buffer_size - 1;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user