fixed a eclipse compiling warning.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2163 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
wuyangyong 2012-06-14 12:18:52 +00:00
parent 72bce28791
commit 9e3c72a635
7 changed files with 914 additions and 871 deletions

View File

@ -28,12 +28,12 @@
rt_inline void i2c_delay(struct rt_i2c_bit_ops *ops) rt_inline void i2c_delay(struct rt_i2c_bit_ops *ops)
{ {
ops->udelay((ops->delay_us + 1) >> 1); ops->udelay((ops->delay_us + 1) >> 1);
} }
rt_inline void i2c_delay2(struct rt_i2c_bit_ops *ops) rt_inline void i2c_delay2(struct rt_i2c_bit_ops *ops)
{ {
ops->udelay(ops->delay_us); ops->udelay(ops->delay_us);
} }
#define SDA_L(ops) SET_SDA(ops, 0) #define SDA_L(ops) SET_SDA(ops, 0)
@ -45,400 +45,400 @@ rt_inline void i2c_delay2(struct rt_i2c_bit_ops *ops)
*/ */
static rt_err_t SCL_H(struct rt_i2c_bit_ops *ops) static rt_err_t SCL_H(struct rt_i2c_bit_ops *ops)
{ {
rt_tick_t start; rt_tick_t start;
SET_SCL(ops, 1); SET_SCL(ops, 1);
if (!ops->get_scl) if (!ops->get_scl)
goto done; goto done;
start = rt_tick_get(); start = rt_tick_get();
while (!GET_SCL(ops)) while (!GET_SCL(ops))
{ {
if ((rt_tick_get() - start) > ops->timeout) if ((rt_tick_get() - start) > ops->timeout)
return -RT_ETIMEOUT; return -RT_ETIMEOUT;
rt_thread_delay((ops->timeout + 1) >> 1); rt_thread_delay((ops->timeout + 1) >> 1);
} }
#ifdef RT_I2C_BIT_DEBUG #ifdef RT_I2C_BIT_DEBUG
if (rt_tick_get() != start) if (rt_tick_get() != start)
{ {
bit_dbg("wait %ld tick for SCL line to go high\n", bit_dbg("wait %ld tick for SCL line to go high\n",
rt_tick_get() - start); rt_tick_get() - start);
} }
#endif #endif
done: done:
i2c_delay(ops); i2c_delay(ops);
return RT_EOK; return RT_EOK;
} }
static void i2c_start(struct rt_i2c_bit_ops *ops) static void i2c_start(struct rt_i2c_bit_ops *ops)
{ {
#ifdef RT_I2C_BIT_DEBUG #ifdef RT_I2C_BIT_DEBUG
if (ops->get_scl && !GET_SCL(ops)) if (ops->get_scl && !GET_SCL(ops))
{ {
bit_dbg("I2C bus error, SCL line low\n"); bit_dbg("I2C bus error, SCL line low\n");
} }
if (ops->get_sda && !GET_SDA(ops)) if (ops->get_sda && !GET_SDA(ops))
{ {
bit_dbg("I2C bus error, SDA line low\n"); bit_dbg("I2C bus error, SDA line low\n");
} }
#endif #endif
SDA_L(ops); SDA_L(ops);
i2c_delay(ops); i2c_delay(ops);
SCL_L(ops); SCL_L(ops);
} }
static void i2c_restart(struct rt_i2c_bit_ops *ops) static void i2c_restart(struct rt_i2c_bit_ops *ops)
{ {
SDA_H(ops); SDA_H(ops);
SCL_H(ops); SCL_H(ops);
i2c_delay(ops); i2c_delay(ops);
SDA_L(ops); SDA_L(ops);
i2c_delay(ops); i2c_delay(ops);
SCL_L(ops); SCL_L(ops);
} }
static void i2c_stop(struct rt_i2c_bit_ops *ops) static void i2c_stop(struct rt_i2c_bit_ops *ops)
{ {
SDA_L(ops); SDA_L(ops);
i2c_delay(ops); i2c_delay(ops);
SCL_H(ops); SCL_H(ops);
i2c_delay(ops); i2c_delay(ops);
SDA_H(ops); SDA_H(ops);
i2c_delay2(ops); i2c_delay2(ops);
} }
rt_inline rt_bool_t i2c_waitack(struct rt_i2c_bit_ops *ops) rt_inline rt_bool_t i2c_waitack(struct rt_i2c_bit_ops *ops)
{ {
rt_bool_t ack; rt_bool_t ack;
SDA_H(ops); SDA_H(ops);
i2c_delay(ops); i2c_delay(ops);
if (SCL_H(ops) < 0) if (SCL_H(ops) < 0)
{ {
bit_dbg("wait ack timeout\n"); bit_dbg("wait ack timeout\n");
return -RT_ETIMEOUT; return -RT_ETIMEOUT;
} }
ack = !GET_SDA(ops); /* ACK : SDA pin is pulled low */ ack = !GET_SDA(ops); /* ACK : SDA pin is pulled low */
bit_dbg("%s\n", ack ? "ACK" : "NACK"); bit_dbg("%s\n", ack ? "ACK" : "NACK");
SCL_L(ops); SCL_L(ops);
return ack; return ack;
} }
static rt_int32_t i2c_writeb(struct rt_i2c_bus_device *bus, rt_uint8_t data) static rt_int32_t i2c_writeb(struct rt_i2c_bus_device *bus, rt_uint8_t data)
{ {
rt_int32_t i; rt_int32_t i;
rt_uint8_t bit; rt_uint8_t bit;
struct rt_i2c_bit_ops *ops = bus->priv; struct rt_i2c_bit_ops *ops = bus->priv;
for (i = 7; i >= 0; i--) for (i = 7; i >= 0; i--)
{ {
SCL_L(ops); SCL_L(ops);
bit = (data >> i) & 1; bit = (data >> i) & 1;
SET_SDA(ops, bit); SET_SDA(ops, bit);
i2c_delay(ops); i2c_delay(ops);
if (SCL_H(ops) < 0) if (SCL_H(ops) < 0)
{ {
bit_dbg("i2c_writeb: 0x%02x, " bit_dbg("i2c_writeb: 0x%02x, "
"wait scl pin high timeout at bit %d\n", "wait scl pin high timeout at bit %d\n",
data, i); data, i);
return -RT_ETIMEOUT; return -RT_ETIMEOUT;
} }
} }
SCL_L(ops); SCL_L(ops);
i2c_delay(ops); i2c_delay(ops);
return i2c_waitack(ops); return i2c_waitack(ops);
} }
static rt_int32_t i2c_readb(struct rt_i2c_bus_device *bus) static rt_int32_t i2c_readb(struct rt_i2c_bus_device *bus)
{ {
rt_uint8_t i; rt_uint8_t i;
rt_uint8_t data = 0; rt_uint8_t data = 0;
struct rt_i2c_bit_ops *ops = bus->priv; struct rt_i2c_bit_ops *ops = bus->priv;
SDA_H(ops); SDA_H(ops);
i2c_delay(ops); i2c_delay(ops);
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
{ {
data <<= 1; data <<= 1;
if (SCL_H(ops) < 0) if (SCL_H(ops) < 0)
{ {
bit_dbg("i2c_readb: wait scl pin high " bit_dbg("i2c_readb: wait scl pin high "
"timeout at bit %d\n", 7 - i); "timeout at bit %d\n", 7 - i);
return -RT_ETIMEOUT; return -RT_ETIMEOUT;
} }
if (GET_SDA(ops)) if (GET_SDA(ops))
data |= 1; data |= 1;
SCL_L(ops); SCL_L(ops);
i2c_delay2(ops); i2c_delay2(ops);
} }
return data; 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_int32_t ret;
rt_size_t bytes = 0; rt_size_t bytes = 0;
const rt_uint8_t *ptr = msg->buf; const rt_uint8_t *ptr = msg->buf;
rt_int32_t count = msg->len; rt_int32_t count = msg->len;
rt_uint16_t ignore_nack = msg->flags & RT_I2C_IGNORE_NACK; rt_uint16_t ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
while (count > 0) while (count > 0)
{ {
ret = i2c_writeb(bus, *ptr); ret = i2c_writeb(bus, *ptr);
if ((ret > 0) || (ignore_nack && (ret == 0))) if ((ret > 0) || (ignore_nack && (ret == 0)))
{ {
count--; count--;
ptr++; ptr++;
bytes++; bytes++;
} }
else if (ret == 0) else if (ret == 0)
{ {
rt_kprintf("send bytes: NACK.\n"); i2c_dbg("send bytes: NACK.\n");
return -RT_ERROR; return 0;
} }
else else
{ {
rt_kprintf("send bytes: error %d\n", ret); i2c_dbg("send bytes: error %d\n", ret);
return ret; return ret;
} }
} }
return bytes; return bytes;
} }
static rt_err_t i2c_send_ack_or_nack(struct rt_i2c_bus_device *bus, int ack) static rt_err_t i2c_send_ack_or_nack(struct rt_i2c_bus_device *bus, int ack)
{ {
struct rt_i2c_bit_ops *ops = bus->priv; struct rt_i2c_bit_ops *ops = bus->priv;
if (ack) if (ack)
SET_SDA(ops, 0); SET_SDA(ops, 0);
i2c_delay(ops); i2c_delay(ops);
if (SCL_H(ops) < 0) if (SCL_H(ops) < 0)
{ {
rt_kprintf("ACK or NACK timeout\n"); bit_dbg("ACK or NACK timeout\n");
return -RT_ETIMEOUT; return -RT_ETIMEOUT;
} }
SCL_L(ops); SCL_L(ops);
return RT_EOK; 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 val;
rt_int32_t bytes = 0; /* actual bytes */ rt_int32_t bytes = 0; /* actual bytes */
rt_uint8_t *ptr = msg->buf; rt_uint8_t *ptr = msg->buf;
rt_int32_t count = msg->len; rt_int32_t count = msg->len;
const rt_uint32_t flags = msg->flags; const rt_uint32_t flags = msg->flags;
while (count > 0) while (count > 0)
{ {
val = i2c_readb(bus); val = i2c_readb(bus);
if (val >= 0) if (val >= 0)
{ {
*ptr = val; *ptr = val;
bytes++; bytes++;
} }
else else
{ {
break; break;
} }
ptr++; ptr++;
count--; count--;
bit_dbg("recieve bytes: 0x%02x, %s\n", bit_dbg("recieve bytes: 0x%02x, %s\n",
val, (flags & RT_I2C_NO_READ_ACK) ? val, (flags & RT_I2C_NO_READ_ACK) ?
"(No ACK/NACK)" : (count ? "ACK" : "NACK")); "(No ACK/NACK)" : (count ? "ACK" : "NACK"));
if (!(flags & RT_I2C_NO_READ_ACK)) if (!(flags & RT_I2C_NO_READ_ACK))
{ {
val = i2c_send_ack_or_nack(bus, count); val = i2c_send_ack_or_nack(bus, count);
if (val < 0) if (val < 0)
return val; return val;
} }
} }
return bytes; return bytes;
} }
static rt_int32_t i2c_send_address(struct rt_i2c_bus_device *bus, 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; struct rt_i2c_bit_ops *ops = bus->priv;
rt_int32_t i; rt_int32_t i;
rt_err_t ret = 0; rt_err_t ret = 0;
for (i = 0; i <= retries; i++) for (i = 0; i <= retries; i++)
{ {
ret = i2c_writeb(bus, addr); ret = i2c_writeb(bus, addr);
if (ret == 1 || i == retries) if (ret == 1 || i == retries)
break; break;
bit_dbg("send stop condition\n"); bit_dbg("send stop condition\n");
i2c_stop(ops); i2c_stop(ops);
i2c_delay2(ops); i2c_delay2(ops);
bit_dbg("send start condition\n"); bit_dbg("send start condition\n");
i2c_start(ops); i2c_start(ops);
} }
return ret; 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 flags = msg->flags;
rt_uint16_t ignore_nack = msg->flags & RT_I2C_IGNORE_NACK; rt_uint16_t ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
struct rt_i2c_bit_ops *ops = bus->priv; struct rt_i2c_bit_ops *ops = bus->priv;
rt_uint8_t addr1, addr2; rt_uint8_t addr1, addr2;
rt_int32_t retries; rt_int32_t retries;
rt_err_t ret; rt_err_t ret;
retries = ignore_nack ? 0 : bus->retries; retries = ignore_nack ? 0 : bus->retries;
if (flags & RT_I2C_ADDR_10BIT) if (flags & RT_I2C_ADDR_10BIT)
{ {
addr1 = 0xf0 | ((msg->addr >> 7) & 0x06); addr1 = 0xf0 | ((msg->addr >> 7) & 0x06);
addr2 = msg->addr & 0xff; addr2 = msg->addr & 0xff;
bit_dbg("addr1: %d, addr2: %d\n", addr1, addr2); bit_dbg("addr1: %d, addr2: %d\n", addr1, addr2);
ret = i2c_send_address(bus, addr1, retries); ret = i2c_send_address(bus, addr1, retries);
if ((ret != 1) && !ignore_nack) if ((ret != 1) && !ignore_nack)
{ {
rt_kprintf("NACK: sending first addr\n"); bit_dbg("NACK: sending first addr\n");
return -RT_EIO; return -RT_EIO;
} }
ret = i2c_writeb(bus, addr2); ret = i2c_writeb(bus, addr2);
if ((ret != 1) && !ignore_nack) if ((ret != 1) && !ignore_nack)
{ {
rt_kprintf("NACK: sending second addr\n"); bit_dbg("NACK: sending second addr\n");
return -RT_EIO; return -RT_EIO;
} }
if (flags & RT_I2C_RD) if (flags & RT_I2C_RD)
{ {
bit_dbg("send repeated start condition\n"); bit_dbg("send repeated start condition\n");
i2c_restart(ops); i2c_restart(ops);
addr1 |= 0x01; addr1 |= 0x01;
ret = i2c_send_address(bus, addr1, retries); ret = i2c_send_address(bus, addr1, retries);
if ((ret != 1) && !ignore_nack) if ((ret != 1) && !ignore_nack)
{ {
rt_kprintf("NACK: sending repeated addr\n"); bit_dbg("NACK: sending repeated addr\n");
return -RT_EIO; return -RT_EIO;
} }
} }
} }
else else
{ /* 7-bit addr */ {
addr1 = msg->addr << 1; /* 7-bit addr */
if (flags & RT_I2C_RD) addr1 = msg->addr << 1;
addr1 |= 1; if (flags & RT_I2C_RD)
ret = i2c_send_address(bus, addr1, retries); addr1 |= 1;
if ((ret != 1) && !ignore_nack) ret = i2c_send_address(bus, addr1, retries);
return -RT_EIO; if ((ret != 1) && !ignore_nack)
} return -RT_EIO;
}
return RT_EOK; return RT_EOK;
} }
static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, 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_msg *msg;
struct rt_i2c_bit_ops *ops = bus->priv; struct rt_i2c_bit_ops *ops = bus->priv;
rt_int32_t i, ret; rt_int32_t i, ret;
rt_uint16_t ignore_nack; rt_uint16_t ignore_nack;
bit_dbg("send start condition\n"); bit_dbg("send start condition\n");
i2c_start(ops); i2c_start(ops);
for (i = 0; i < num; i++) for (i = 0; i < num; i++)
{ {
msg = &msgs[i]; msg = &msgs[i];
ignore_nack = msg->flags & RT_I2C_IGNORE_NACK; ignore_nack = msg->flags & RT_I2C_IGNORE_NACK;
if (!(msg->flags & RT_I2C_NO_START)) if (!(msg->flags & RT_I2C_NO_START))
{ {
if (i) if (i)
{ {
i2c_restart(ops); i2c_restart(ops);
} }
ret = i2c_bit_send_address(bus, msg); ret = i2c_bit_send_address(bus, msg);
if ((ret != RT_EOK) && !ignore_nack) if ((ret != RT_EOK) && !ignore_nack)
{ {
bit_dbg("receive NACK from device addr 0x%02x msg %d\n", bit_dbg("receive NACK from device addr 0x%02x msg %d\n",
msgs[i].addr, i); msgs[i].addr, i);
goto out; goto out;
} }
} }
if (msg->flags & RT_I2C_RD) if (msg->flags & RT_I2C_RD)
{ {
ret = i2c_recv_bytes(bus, msg); ret = i2c_recv_bytes(bus, msg);
if (ret >= 1) if (ret >= 1)
bit_dbg("read %d byte%s\n", bit_dbg("read %d byte%s\n",
ret, ret == 1 ? "" : "s"); ret, ret == 1 ? "" : "s");
if (ret < msg->len) if (ret < msg->len)
{ {
if (ret >= 0) if (ret >= 0)
ret = -RT_EIO; ret = -RT_EIO;
goto out; goto out;
} }
} }
else else
{ {
ret = i2c_send_bytes(bus, msg); ret = i2c_send_bytes(bus, msg);
if (ret >= 1) if (ret >= 1)
bit_dbg("write %d byte%s\n", bit_dbg("write %d byte%s\n",
ret, ret == 1 ? "" : "s"); ret, ret == 1 ? "" : "s");
if (ret < msg->len) if (ret < msg->len)
{ {
if (ret >= 0) if (ret >= 0)
ret = -RT_ERROR; ret = -RT_ERROR;
goto out; goto out;
} }
} }
} }
ret = i; ret = i;
out: out:
bit_dbg("send stop condition\n"); bit_dbg("send stop condition\n");
i2c_stop(ops); i2c_stop(ops);
return ret; return ret;
} }
static const struct rt_i2c_bus_device_ops i2c_bit_bus_ops = { static const struct rt_i2c_bus_device_ops i2c_bit_bus_ops =
i2c_bit_xfer, {
RT_NULL, i2c_bit_xfer,
RT_NULL RT_NULL,
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)
{ {
rt_err_t err; struct rt_i2c_bit_ops *bit_ops = bus->priv;
RT_ASSERT(bit_ops != RT_NULL);
struct rt_i2c_bit_ops *bit_ops = bus->priv; bus->ops = &i2c_bit_bus_ops;
RT_ASSERT(bit_ops != RT_NULL);
bus->ops = &i2c_bit_bus_ops; return rt_i2c_bus_device_register(bus, bus_name);
return rt_i2c_bus_device_register(bus, bus_name);
} }

View File

@ -16,114 +16,120 @@
static struct rt_mutex i2c_core_lock; 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) rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
const char *bus_name)
{ {
rt_err_t res = RT_EOK; rt_err_t res = RT_EOK;
rt_mutex_init(&bus->lock, "i2c_bus_lock", RT_IPC_FLAG_FIFO); rt_mutex_init(&bus->lock, "i2c_bus_lock", RT_IPC_FLAG_FIFO);
rt_mutex_take(&i2c_core_lock, RT_WAITING_FOREVER); rt_mutex_take(&i2c_core_lock, RT_WAITING_FOREVER);
if (bus->timeout == 0) if (bus->timeout == 0)
bus->timeout = RT_TICK_PER_SECOND; bus->timeout = RT_TICK_PER_SECOND;
res = rt_i2c_bus_device_device_init(bus, bus_name); res = rt_i2c_bus_device_device_init(bus, bus_name);
i2c_dbg("I2C bus [%s] registered\n", bus_name); i2c_dbg("I2C bus [%s] registered\n", bus_name);
rt_mutex_release(&i2c_core_lock); rt_mutex_release(&i2c_core_lock);
return res; 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; struct rt_i2c_bus_device *bus;
rt_device_t dev = rt_device_find(bus_name); rt_device_t dev = rt_device_find(bus_name);
if (dev == RT_NULL || dev->type != RT_Device_Class_I2CBUS) if (dev == RT_NULL || dev->type != RT_Device_Class_I2CBUS)
{ {
i2c_dbg("I2C bus %s not exist\n", bus_name); i2c_dbg("I2C bus %s not exist\n", bus_name);
return RT_NULL; return RT_NULL;
} }
bus = (struct rt_i2c_bus_device *)dev->user_data; bus = (struct rt_i2c_bus_device *)dev->user_data;
return bus; return bus;
} }
rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num) rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num)
{ {
rt_size_t ret; rt_size_t ret;
if (bus->ops->master_xfer) if (bus->ops->master_xfer)
{ {
#ifdef RT_I2C_DEBUG #ifdef RT_I2C_DEBUG
for (ret = 0; ret < num; ret++) for (ret = 0; ret < num; ret++)
{ {
i2c_dbg("msgs[%d] %c, addr=0x%02x, len=%d%s\n", ret, i2c_dbg("msgs[%d] %c, addr=0x%02x, len=%d%s\n", ret,
(msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W', (msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W',
msgs[ret].addr, msgs[ret].len); msgs[ret].addr, msgs[ret].len);
} }
#endif #endif
rt_mutex_take(&bus->lock, RT_WAITING_FOREVER); rt_mutex_take(&bus->lock, RT_WAITING_FOREVER);
ret = bus->ops->master_xfer(bus, msgs, num); ret = bus->ops->master_xfer(bus, msgs, num);
rt_mutex_release(&bus->lock); rt_mutex_release(&bus->lock);
return ret; return ret;
} }
else else
{ {
rt_kprintf("I2C bus operation not supported\n"); i2c_dbg("I2C bus operation not supported\n");
return -RT_ERROR; return 0;
} }
} }
rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus, rt_uint16_t addr, rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
rt_uint16_t flags, const rt_uint8_t *buf, rt_uint16_t addr,
rt_uint32_t count) rt_uint16_t flags,
const rt_uint8_t *buf,
rt_uint32_t count)
{ {
rt_size_t ret; rt_size_t ret;
struct rt_i2c_msg msg; struct rt_i2c_msg msg;
msg.addr = addr; msg.addr = addr;
msg.flags = flags & RT_I2C_ADDR_10BIT; msg.flags = flags & RT_I2C_ADDR_10BIT;
msg.len = count; msg.len = count;
msg.buf = (rt_uint8_t *)buf; msg.buf = (rt_uint8_t *)buf;
ret = rt_i2c_transfer(bus, &msg, 1); ret = rt_i2c_transfer(bus, &msg, 1);
return (ret > 0) ? count : ret; return (ret > 0) ? count : ret;
} }
rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus, rt_uint16_t addr, rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
rt_uint16_t flags, rt_uint8_t *buf, rt_uint16_t addr,
rt_uint32_t count) rt_uint16_t flags,
rt_uint8_t *buf,
rt_uint32_t count)
{ {
rt_size_t ret; rt_size_t ret;
struct rt_i2c_msg msg; struct rt_i2c_msg msg;
RT_ASSERT(bus != RT_NULL); RT_ASSERT(bus != RT_NULL);
msg.addr = addr; msg.addr = addr;
msg.flags = flags & RT_I2C_ADDR_10BIT; msg.flags = flags & RT_I2C_ADDR_10BIT;
msg.flags |= RT_I2C_RD; msg.flags |= RT_I2C_RD;
msg.len = count; msg.len = count;
msg.buf = buf; msg.buf = buf;
ret = rt_i2c_transfer(bus, &msg, 1); ret = rt_i2c_transfer(bus, &msg, 1);
return (ret > 0) ? count : ret; return (ret > 0) ? count : ret;
} }
rt_err_t rt_i2c_core_init(void) rt_err_t rt_i2c_core_init(void)
{ {
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);
} }

View File

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

View File

@ -19,20 +19,22 @@
extern "C" { extern "C" {
#endif #endif
struct rt_i2c_bit_ops { struct rt_i2c_bit_ops
void *data; /* private data for lowlevel routines */ {
void (*set_sda) (void *data, rt_int32_t state); void *data; /* private data for lowlevel routines */
void (*set_scl) (void *data, rt_int32_t state); void (*set_sda) (void *data, rt_int32_t state);
rt_int32_t (*get_sda) (void *data); void (*set_scl) (void *data, rt_int32_t state);
rt_int32_t (*get_scl) (void *data); 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 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); rt_err_t rt_i2c_bit_add_bus(struct rt_i2c_bus_device *bus,
const char *bus_name);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -28,31 +28,40 @@ extern "C" {
#define RT_I2C_IGNORE_NACK (1u << 5) #define RT_I2C_IGNORE_NACK (1u << 5)
#define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */ #define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
struct rt_i2c_msg { struct rt_i2c_msg
rt_uint16_t addr; {
rt_uint16_t flags; rt_uint16_t addr;
rt_uint16_t len; rt_uint16_t flags;
rt_uint8_t *buf; rt_uint16_t len;
rt_uint8_t *buf;
}; };
struct rt_i2c_bus_device; struct rt_i2c_bus_device;
struct rt_i2c_bus_device_ops { 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, struct rt_i2c_msg msgs[], rt_uint32_t num); rt_size_t (*master_xfer) (struct rt_i2c_bus_device *bus,
rt_err_t (*i2c_bus_control) (struct rt_i2c_bus_device *bus, rt_uint32_t, rt_uint32_t); struct rt_i2c_msg msgs[],
rt_uint32_t num);
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*/ /*for i2c bus driver*/
struct rt_i2c_bus_device { struct rt_i2c_bus_device
struct rt_device parent; {
const struct rt_i2c_bus_device_ops *ops; struct rt_device parent;
rt_uint16_t flags; const struct rt_i2c_bus_device_ops *ops;
rt_uint16_t addr; rt_uint16_t flags;
struct rt_mutex lock; rt_uint16_t addr;
rt_uint32_t timeout; struct rt_mutex lock;
rt_uint32_t retries; rt_uint32_t timeout;
void *priv; rt_uint32_t retries;
void *priv;
}; };
#ifdef RT_I2C_DEBUG #ifdef RT_I2C_DEBUG
@ -61,15 +70,22 @@ struct rt_i2c_bus_device {
#define i2c_dbg(fmt, ...) #define i2c_dbg(fmt, ...)
#endif #endif
rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus, const char *bus_name); 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); 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); rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus, rt_uint16_t addr, struct rt_i2c_msg msgs[],
rt_uint16_t flags, const rt_uint8_t *buf, rt_uint32_t num);
rt_uint32_t count); rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus, rt_uint16_t addr, rt_uint16_t addr,
rt_uint16_t flags, rt_uint8_t *buf, rt_uint16_t flags,
rt_uint32_t count); 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_err_t rt_i2c_core_init(void); rt_err_t rt_i2c_core_init(void);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -12,12 +12,14 @@ extern "C" {
#define RT_I2C_DEV_CTRL_TIMEOUT 0x22 #define RT_I2C_DEV_CTRL_TIMEOUT 0x22
#define RT_I2C_DEV_CTRL_RW 0x23 #define RT_I2C_DEV_CTRL_RW 0x23
struct rt_i2c_priv_data { struct rt_i2c_priv_data
struct rt_i2c_msg *msgs; {
rt_size_t number; struct rt_i2c_msg *msgs;
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 #ifdef __cplusplus

View File

@ -8,25 +8,26 @@
/* completion flag */ /* completion flag */
struct rt_completion struct rt_completion
{ {
rt_uint32_t flag; rt_uint32_t flag;
/* suspended list */ /* suspended list */
rt_list_t suspended_list; rt_list_t suspended_list;
}; };
/* ring buffer */ /* ring buffer */
struct rt_ringbuffer struct rt_ringbuffer
{ {
rt_uint16_t read_index, write_index; rt_uint16_t read_index, write_index;
rt_uint8_t *buffer_ptr; rt_uint8_t *buffer_ptr;
rt_uint16_t buffer_size; rt_uint16_t buffer_size;
}; };
/** /**
* Completion * Completion
*/ */
void rt_completion_init(struct rt_completion* completion); void rt_completion_init(struct rt_completion* completion);
rt_err_t rt_completion_wait(struct rt_completion* completion, rt_int32_t timeout); rt_err_t rt_completion_wait(struct rt_completion* completion,
rt_int32_t timeout);
void rt_completion_done(struct rt_completion* completion); void rt_completion_done(struct rt_completion* completion);
/** /**
@ -36,44 +37,52 @@ void rt_completion_done(struct rt_completion* completion);
/** /**
* RingBuffer for DeviceDriver * RingBuffer for DeviceDriver
*/ */
void rt_ringbuffer_init(struct rt_ringbuffer* rb, rt_uint8_t *pool, rt_uint16_t size); void rt_ringbuffer_init(struct rt_ringbuffer* rb,
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer* rb, const rt_uint8_t *ptr, rt_uint16_t length); rt_uint8_t *pool,
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb, const rt_uint8_t ch); rt_uint16_t size);
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer* rb, rt_uint8_t *ptr, rt_uint16_t length); rt_size_t rt_ringbuffer_put(struct rt_ringbuffer* rb,
const rt_uint8_t *ptr,
rt_uint16_t length);
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb,
const rt_uint8_t ch);
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer* rb,
rt_uint8_t *ptr,
rt_uint16_t length);
rt_size_t rt_ringbuffer_available_size(struct rt_ringbuffer* rb); rt_size_t rt_ringbuffer_available_size(struct rt_ringbuffer* rb);
rt_size_t rt_ringbuffer_emptry_size(struct rt_ringbuffer* rb); rt_size_t rt_ringbuffer_emptry_size(struct rt_ringbuffer* rb);
#ifdef RT_USING_SPI #ifdef RT_USING_SPI
#include "drivers/spi.h" #include "drivers/spi.h"
#endif #endif /* RT_USING_SPI */
#ifdef RT_USING_MTD_NOR #ifdef RT_USING_MTD_NOR
#include "drivers/mtd_nor.h" #include "drivers/mtd_nor.h"
#endif #endif /* RT_USING_MTD_NOR */
#ifdef RT_USING_MTD_NAND #ifdef RT_USING_MTD_NAND
#include "drivers/mtd_nand.h" #include "drivers/mtd_nand.h"
#endif #endif /* RT_USING_MTD_NAND */
#ifdef RT_USING_USB_DEVICE #ifdef RT_USING_USB_DEVICE
#include "drivers/usb_device.h" #include "drivers/usb_device.h"
#endif #endif /* RT_USING_USB_DEVICE */
#ifdef RT_USING_USB_HOST #ifdef RT_USING_USB_HOST
#include "drivers/usb_host.h" #include "drivers/usb_host.h"
#endif #endif /* RT_USING_USB_HOST */
#ifdef RT_USING_SERIAL #ifdef RT_USING_SERIAL
#include "drivers/serial.h" #include "drivers/serial.h"
#endif #endif /* RT_USING_SERIAL */
#ifdef RT_USING_I2C #ifdef RT_USING_I2C
#include "drivers/i2c.h" #include "drivers/i2c.h"
#include "drivers/i2c_dev.h" #include "drivers/i2c_dev.h"
#endif
#ifdef RT_USING_I2C_BITOPS #ifdef RT_USING_I2C_BITOPS
#include "drivers/i2c-bit-ops.h" #include "drivers/i2c-bit-ops.h"
#endif #endif /* RT_USING_I2C_BITOPS */
#endif #endif /* RT_USING_I2C */
#endif /* __RT_DEVICE_H__ */