Files
mr-library/device/i2c.c

426 lines
13 KiB
C
Raw Normal View History

2023-11-11 02:07:22 +08:00
/*
2024-01-02 00:02:48 +08:00
* @copyright (c) 2023-2024, MR Development Team
2023-11-11 02:07:22 +08:00
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-11-09 MacRsh First version
*/
2024-01-02 00:02:48 +08:00
#include "include/device/mr_i2c.h"
2023-11-11 02:07:22 +08:00
#ifdef MR_USING_I2C
static int mr_i2c_bus_open(struct mr_dev *dev)
{
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)dev;
struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)dev->drv->ops;
2023-11-25 23:37:42 +08:00
/* Reset the hold */
i2c_bus->hold = MR_FALSE;
2023-12-27 23:47:57 +08:00
/* Default address is 0x00 and 7-bit */
2023-11-15 12:35:21 +08:00
return ops->configure(i2c_bus, &i2c_bus->config, 0x00, MR_I2C_ADDR_BITS_7);
2023-11-11 02:07:22 +08:00
}
static int mr_i2c_bus_close(struct mr_dev *dev)
{
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)dev;
struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)dev->drv->ops;
struct mr_i2c_config close_config = {0};
return ops->configure(i2c_bus, &close_config, 0x00, MR_I2C_ADDR_BITS_7);
2023-11-11 02:07:22 +08:00
}
static ssize_t mr_i2c_bus_read(struct mr_dev *dev, void *buf, size_t count)
2023-11-11 02:07:22 +08:00
{
2023-12-27 23:47:57 +08:00
return MR_ENOTSUP;
2023-11-11 02:07:22 +08:00
}
static ssize_t mr_i2c_bus_write(struct mr_dev *dev, const void *buf, size_t count)
2023-11-11 02:07:22 +08:00
{
2023-12-27 23:47:57 +08:00
return MR_ENOTSUP;
2023-11-11 02:07:22 +08:00
}
static ssize_t mr_i2c_bus_isr(struct mr_dev *dev, int event, void *args)
{
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)dev;
struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)dev->drv->ops;
2023-11-11 02:07:22 +08:00
switch (event) {
case MR_ISR_I2C_RD_INT: {
2023-11-11 02:07:22 +08:00
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)i2c_bus->owner;
uint8_t data;
2023-11-11 02:07:22 +08:00
/* Read data to FIFO */
int ret = ops->read(i2c_bus, &data, MR_ENABLE);
if (ret < 0) {
return ret;
}
2023-11-11 02:07:22 +08:00
mr_ringbuf_push_force(&i2c_dev->rd_fifo, data);
/* Call the i2c-dev ISR */
return mr_dev_isr(&i2c_dev->dev, event, MR_NULL);
2023-11-11 02:07:22 +08:00
}
default: {
2023-11-11 02:07:22 +08:00
return MR_ENOTSUP;
}
}
}
/**
* @brief This function registers a i2c-bus.
*
* @param i2c_bus The i2c-bus.
* @param path The path of the i2c-bus.
2023-11-11 02:07:22 +08:00
* @param drv The driver of the i2c-bus.
*
* @return 0 on success, otherwise an error code.
2023-11-11 02:07:22 +08:00
*/
int mr_i2c_bus_register(struct mr_i2c_bus *i2c_bus, const char *path, struct mr_drv *drv)
2023-11-11 02:07:22 +08:00
{
static struct mr_dev_ops ops = {mr_i2c_bus_open,
mr_i2c_bus_close,
mr_i2c_bus_read,
mr_i2c_bus_write,
MR_NULL,
mr_i2c_bus_isr};
2023-11-11 02:07:22 +08:00
struct mr_i2c_config default_config = MR_I2C_CONFIG_DEFAULT;
MR_ASSERT(i2c_bus != MR_NULL);
MR_ASSERT(path != MR_NULL);
MR_ASSERT(drv != MR_NULL);
MR_ASSERT(drv->ops != MR_NULL);
2023-11-11 02:07:22 +08:00
/* Initialize the fields */
i2c_bus->config = default_config;
i2c_bus->owner = MR_NULL;
2023-11-25 23:37:42 +08:00
i2c_bus->hold = MR_FALSE;
2023-11-11 02:07:22 +08:00
/* Register the i2c-bus */
return mr_dev_register(&i2c_bus->dev, path, MR_DEV_TYPE_I2C, MR_O_RDWR, &ops, drv);
2023-11-11 02:07:22 +08:00
}
MR_INLINE int i2c_dev_take_bus(struct mr_i2c_dev *i2c_dev)
{
2023-12-19 03:54:43 +08:00
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)i2c_dev->dev.parent;
2023-11-11 02:07:22 +08:00
struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)i2c_bus->dev.drv->ops;
2023-11-25 23:37:42 +08:00
/* Check if the bus is busy */
if ((i2c_bus->hold == MR_TRUE) && (i2c_dev != i2c_bus->owner)) {
2023-11-11 02:07:22 +08:00
return MR_EBUSY;
}
2023-12-27 23:47:57 +08:00
/* If the owner changes, recheck the configuration */
if (i2c_dev != i2c_bus->owner) {
if (i2c_dev->config.baud_rate != i2c_bus->config.baud_rate ||
i2c_dev->config.host_slave != i2c_bus->config.host_slave) {
int addr = (i2c_dev->config.host_slave == MR_I2C_HOST) ? 0x00 : i2c_dev->addr;
2023-11-14 21:01:04 +08:00
int ret = ops->configure(i2c_bus, &i2c_dev->config, addr, i2c_dev->addr_bits);
if (ret < 0) {
2023-11-11 02:07:22 +08:00
return ret;
}
}
i2c_bus->config = i2c_dev->config;
i2c_bus->owner = i2c_dev;
}
2023-11-25 23:37:42 +08:00
i2c_bus->hold = MR_TRUE;
2023-11-11 02:07:22 +08:00
return MR_EOK;
}
MR_INLINE int i2c_dev_release_bus(struct mr_i2c_dev *i2c_dev)
{
2023-12-19 03:54:43 +08:00
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)i2c_dev->dev.parent;
2023-11-11 02:07:22 +08:00
if (i2c_dev != i2c_bus->owner) {
2023-11-11 02:07:22 +08:00
return MR_EINVAL;
}
/* If it is a host, release the bus. The slave needs to hold the bus at all times */
if (i2c_dev->config.host_slave == MR_I2C_HOST) {
2023-11-25 23:37:42 +08:00
i2c_bus->hold = MR_FALSE;
2023-11-11 02:07:22 +08:00
}
return MR_EOK;
}
2023-11-25 23:37:42 +08:00
#define MR_I2C_RD (0)
#define MR_I2C_WR (1)
MR_INLINE int i2c_dev_send_addr(struct mr_i2c_dev *i2c_dev, int rdwr)
2023-11-25 23:37:42 +08:00
{
2023-12-19 03:54:43 +08:00
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)i2c_dev->dev.parent;
2023-11-25 23:37:42 +08:00
struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)i2c_bus->dev.drv->ops;
int addr, addr_bits;
2023-11-25 23:37:42 +08:00
/* Get the address, otherwise use the 0x00 */
2024-01-16 04:03:40 +08:00
addr = i2c_dev->addr;
addr_bits = i2c_dev->addr_bits;
2023-11-25 23:37:42 +08:00
/* Set the read command */
addr = (0xf000 | ((addr >> 8) & 0x03) << 9) | (addr & 0xff);
if (rdwr == MR_I2C_RD) {
addr |= (addr_bits == MR_I2C_ADDR_BITS_7) ? 0x01 : 0x10;
2023-11-25 23:37:42 +08:00
}
ops->start(i2c_bus);
int ret = ops->send_addr(i2c_bus, addr, addr_bits);
if (ret < 0) {
ops->stop(i2c_bus);
}
return ret;
2023-11-25 23:37:42 +08:00
}
MR_INLINE void i2c_dev_send_stop(struct mr_i2c_dev *i2c_dev)
{
2023-12-19 03:54:43 +08:00
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)i2c_dev->dev.parent;
2023-11-25 23:37:42 +08:00
struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)i2c_bus->dev.drv->ops;
ops->stop(i2c_bus);
}
MR_INLINE ssize_t i2c_dev_read(struct mr_i2c_dev *i2c_dev, uint8_t *buf, size_t count)
2023-11-25 23:37:42 +08:00
{
2023-12-19 03:54:43 +08:00
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)i2c_dev->dev.parent;
2023-11-25 23:37:42 +08:00
struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)i2c_bus->dev.drv->ops;
2023-12-31 16:32:01 +08:00
ssize_t rd_size;
2023-11-25 23:37:42 +08:00
for (rd_size = 0; rd_size < count; rd_size += sizeof(*buf)) {
int ack = ((count - rd_size) != sizeof(*buf));
int ret = ops->read(i2c_bus, buf, ack);
if (ret < 0) {
return (rd_size == 0) ? ret : rd_size;
}
buf++;
2023-12-03 02:17:04 +08:00
}
return rd_size;
2023-11-25 23:37:42 +08:00
}
MR_INLINE ssize_t i2c_dev_write(struct mr_i2c_dev *i2c_dev, const uint8_t *buf, size_t count)
2023-11-25 23:37:42 +08:00
{
2023-12-19 03:54:43 +08:00
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)i2c_dev->dev.parent;
2023-11-25 23:37:42 +08:00
struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)i2c_bus->dev.drv->ops;
2023-12-31 16:32:01 +08:00
ssize_t wr_size;
2023-11-25 23:37:42 +08:00
for (wr_size = 0; wr_size < count; wr_size += sizeof(*buf)) {
int ret = ops->write(i2c_bus, *buf);
if (ret < 0) {
return (wr_size == 0) ? ret : wr_size;
}
buf++;
2023-12-03 02:17:04 +08:00
}
return wr_size;
2023-11-25 23:37:42 +08:00
}
2023-11-11 02:07:22 +08:00
static int mr_i2c_dev_open(struct mr_dev *dev)
{
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
return mr_ringbuf_allocate(&i2c_dev->rd_fifo, i2c_dev->rd_bufsz);
}
static int mr_i2c_dev_close(struct mr_dev *dev)
{
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
mr_ringbuf_free(&i2c_dev->rd_fifo);
return MR_EOK;
}
static ssize_t mr_i2c_dev_read(struct mr_dev *dev, void *buf, size_t count)
2023-11-11 02:07:22 +08:00
{
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
ssize_t ret = i2c_dev_take_bus(i2c_dev);
if (ret < 0) {
2023-11-11 02:07:22 +08:00
return ret;
}
if (i2c_dev->config.host_slave == MR_I2C_HOST) {
2023-12-27 23:47:57 +08:00
/* Send the address of the register that needs to be read */
if (dev->position >= 0) {
ret = i2c_dev_send_addr(i2c_dev, MR_I2C_WR);
if (ret < 0) {
goto release_bus;
}
ret = i2c_dev_write(i2c_dev,
(uint8_t *)&dev->position,
(i2c_dev->config.reg_bits >> 3));
if (ret < 0) {
goto release_bus;
}
2023-11-25 23:37:42 +08:00
}
ret = i2c_dev_send_addr(i2c_dev, MR_I2C_RD);
if (ret < 0) {
goto release_bus;
}
ret = i2c_dev_read(i2c_dev, (uint8_t *)buf, count);
2023-11-25 23:37:42 +08:00
i2c_dev_send_stop(i2c_dev);
} else {
ret = (ssize_t)mr_ringbuf_read(&i2c_dev->rd_fifo, buf, count);
2023-11-11 02:07:22 +08:00
}
release_bus:
2023-11-11 02:07:22 +08:00
i2c_dev_release_bus(i2c_dev);
return ret;
}
static ssize_t mr_i2c_dev_write(struct mr_dev *dev, const void *buf, size_t count)
2023-11-11 02:07:22 +08:00
{
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
ssize_t ret = i2c_dev_take_bus(i2c_dev);
if (ret < 0) {
2023-11-11 02:07:22 +08:00
return ret;
}
if (i2c_dev->config.host_slave == MR_I2C_HOST) {
ret = i2c_dev_send_addr(i2c_dev, MR_I2C_WR);
if (ret < 0) {
goto release_bus;
}
2023-12-27 23:47:57 +08:00
/* Send the address of the register that needs to be written */
if (dev->position >= 0) {
ret = i2c_dev_write(i2c_dev,
(uint8_t *)&dev->position,
(i2c_dev->config.reg_bits >> 3));
if (ret < 0) {
goto release_bus;
}
2023-11-25 23:37:42 +08:00
}
ret = i2c_dev_write(i2c_dev, (uint8_t *)buf, count);
2023-11-25 23:37:42 +08:00
i2c_dev_send_stop(i2c_dev);
} else {
ret = i2c_dev_write(i2c_dev, (uint8_t *)buf, count);
2023-11-11 02:07:22 +08:00
}
release_bus:
2023-11-11 02:07:22 +08:00
i2c_dev_release_bus(i2c_dev);
return ret;
}
static int mr_i2c_dev_ioctl(struct mr_dev *dev, int cmd, void *args)
2023-11-11 02:07:22 +08:00
{
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
switch (cmd) {
case MR_IOC_I2C_SET_CONFIG: {
if (args != MR_NULL) {
2023-12-19 03:54:43 +08:00
struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)dev->parent;
struct mr_i2c_config config = *(struct mr_i2c_config *)args;
2023-11-11 02:07:22 +08:00
2023-11-25 23:37:42 +08:00
/* If holding the bus, release it */
if (i2c_dev == i2c_bus->owner) {
2023-11-25 23:37:42 +08:00
i2c_bus->hold = MR_FALSE;
2023-11-11 02:07:22 +08:00
i2c_bus->owner = MR_NULL;
}
2023-11-25 23:37:42 +08:00
/* Update the configuration and try again to get the bus */
i2c_dev->config = config;
if (config.host_slave == MR_I2C_SLAVE) {
2023-11-11 02:07:22 +08:00
int ret = i2c_dev_take_bus(i2c_dev);
if (ret < 0) {
2023-11-11 02:07:22 +08:00
return ret;
}
}
2024-01-16 04:03:40 +08:00
return sizeof(config);
2023-11-11 02:07:22 +08:00
}
return MR_EINVAL;
}
case MR_IOC_I2C_SET_RD_BUFSZ: {
if (args != MR_NULL) {
2023-11-11 02:07:22 +08:00
size_t bufsz = *(size_t *)args;
int ret = mr_ringbuf_allocate(&i2c_dev->rd_fifo, bufsz);
i2c_dev->rd_bufsz = 0;
if (ret < 0) {
2024-01-16 04:03:40 +08:00
return ret;
2023-11-11 02:07:22 +08:00
}
2024-01-16 04:03:40 +08:00
i2c_dev->rd_bufsz = bufsz;
return sizeof(bufsz);
2023-11-11 02:07:22 +08:00
}
return MR_EINVAL;
}
case MR_IOC_I2C_CLR_RD_BUF: {
2023-12-06 17:19:27 +08:00
mr_ringbuf_reset(&i2c_dev->rd_fifo);
return MR_EOK;
}
case MR_IOC_I2C_GET_CONFIG: {
if (args != MR_NULL) {
2023-11-11 02:07:22 +08:00
struct mr_i2c_config *config = (struct mr_i2c_config *)args;
*config = i2c_dev->config;
2024-01-16 04:03:40 +08:00
return sizeof(*config);
2023-11-11 02:07:22 +08:00
}
return MR_EINVAL;
}
case MR_IOC_I2C_GET_RD_BUFSZ: {
if (args != MR_NULL) {
2024-01-16 04:03:40 +08:00
size_t *bufsz = (size_t *)args;
*bufsz = i2c_dev->rd_bufsz;
return sizeof(*bufsz);
2023-11-11 02:07:22 +08:00
}
return MR_EINVAL;
}
case MR_IOC_I2C_GET_RD_DATASZ: {
if (args != MR_NULL) {
2024-01-16 04:03:40 +08:00
size_t *datasz = (size_t *)args;
2023-12-06 17:19:27 +08:00
2024-01-16 04:03:40 +08:00
*datasz = mr_ringbuf_get_bufsz(&i2c_dev->rd_fifo);
return sizeof(*datasz);
2023-12-06 17:19:27 +08:00
}
return MR_EINVAL;
2023-12-06 17:19:27 +08:00
}
default: {
2023-11-11 02:07:22 +08:00
return MR_ENOTSUP;
}
}
}
/**
* @brief This function registers a i2c-device.
*
* @param i2c_dev The i2c-device.
* @param path The path of the i2c-device.
2023-11-11 02:07:22 +08:00
* @param addr The address of the i2c-device.
* @param addr_bits The number of address bits of the i2c-device.
*
* @return 0 on success, otherwise an error code.
2023-11-11 02:07:22 +08:00
*/
int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *path, int addr, int addr_bits)
2023-11-11 02:07:22 +08:00
{
static struct mr_dev_ops ops = {mr_i2c_dev_open,
mr_i2c_dev_close,
mr_i2c_dev_read,
mr_i2c_dev_write,
mr_i2c_dev_ioctl,
MR_NULL};
2023-11-11 02:07:22 +08:00
struct mr_i2c_config default_config = MR_I2C_CONFIG_DEFAULT;
MR_ASSERT(i2c_dev != MR_NULL);
MR_ASSERT(path != MR_NULL);
MR_ASSERT((addr_bits == MR_I2C_ADDR_BITS_7) || (addr_bits == MR_I2C_ADDR_BITS_10));
MR_ASSERT((addr_bits != MR_I2C_ADDR_BITS_7) || (addr >= 0 && addr <= 0x7f));
MR_ASSERT((addr_bits != MR_I2C_ADDR_BITS_10) || (addr >= 0 && addr <= 0x3ff));
2023-11-11 02:07:22 +08:00
/* Initialize the fields */
i2c_dev->config = default_config;
mr_ringbuf_init(&i2c_dev->rd_fifo, MR_NULL, 0);
#ifndef MR_CFG_I2C_RD_BUFSZ
#define MR_CFG_I2C_RD_BUFSZ (0)
#endif /* MR_CFG_I2C_RD_BUFSZ */
i2c_dev->rd_bufsz = MR_CFG_I2C_RD_BUFSZ;
i2c_dev->addr = addr;
2023-11-11 02:07:22 +08:00
i2c_dev->addr_bits = addr_bits;
/* Register the i2c-device */
return mr_dev_register(&i2c_dev->dev, path, MR_DEV_TYPE_I2C, MR_O_RDWR, &ops, MR_NULL);
2023-11-11 02:07:22 +08:00
}
#endif /* MR_USING_I2C */