打开内核配置项CONFIG_I2C_CHARDEV,加载i2c-dev模块。
设备节点:/dev/i2c-x。
用户空间通过设备节点访问挂载到i2c适配器上的任意i2c设备。.
i2c-dev模块的read/write函数不支持block传输,一次最多传输8字节数据。
修改read/write函数支持DMA传输,以实现block传输。DMA传输 BUFFER 大小的限制是255字节。
代码修改如下:i2c-dev.c
#include
#define MAX_BUFFER_SIZE 255
static ssize_t i2cdev_write_dma(struct file *file,const char __user *buf, size_t count, loff_t *offset)
{
int ret;
struct i2c_client *client = file->private_data;
char *pI2cDMAWriteBuf = NULL;
unsigned int PhyAddrForDMABuf = 0;
if (count > MAX_BUFFER_SIZE )
count = MAX_BUFFER_SIZE ;
pI2cDMAWriteBuf = (char *)dma_alloc_coherent(NULL, MAX_BUFFER_SIZE ,&PhyAddrForDMABuf , GFP_KERNEL);
if (pI2cDMAWriteBuf == NULL)
{
return -ENOMEM;
}
if (copyt_from_user(pI2cDMAWriteBuf , buf, count))
{
// error
dma_free_coherent(NULL, MAX_BUFFER_SIZE , pI2cDMAWriteBuf , PhyAddrForDMABuf );
pI2cDMAWriteBuf = NULL;
PhyAddrForDMABuf = 0;
return -EFAULT;
}
client->addr &= I2C_MASK_FLAG;
client->ext_flag |= I2C_DMA_FLAG;
client->ext_flag |= I2C_A_FILTER_MSG;
ret = i2c_master_sent(client, (unsigned char *)PhyAddrForDMABuf, count);
dma_free_coherent(NULL, MAX_BUFFER_SIZE , pI2cDMAWriteBuf , PhyAddrForDMABuf );
pI2cDMAWriteBuf = NULL;
PhyAddrForDMABuf = 0;
return ret;
}
static ssize_t i2cdev_write_dma(struct file *file,const char __user *buf, size_t count, loff_t *offset)
{
int ret;
struct i2c_client *client = file->private_data;
char *pI2cDMAReadBuf = NULL;
unsigned int PhyAddrForDMABuf = 0;
if (count > MAX_BUFFER_SIZE )
count = MAX_BUFFER_SIZE ;
pI2cDMAReadBuf = (char *)dma_alloc_coherent(NULL, MAX_BUFFER_SIZE , &PhyAddrForDMABuf, GFP_KERNEL);
if (pI2cDMAReadBuf == NULL)
{
return -ENOMEM;
}
client->addr &= I2C_MASK_FLAG;
client->ext_flag |= I2C_DMA_FLAG;
client->ext_flag |= I2C_A_FILTER_MSG;
ret = i2c_master_recv(client, (unsigned char *)PhyAddrForDMABuf, count);
if (ret >= 0)
{
if(copy_to_user(buf, pI2cDMAReadBuf, ret))
{
ret = -EFAULT;
}
}
dma_free_coherent(NULL, MAX_BUFFER_SIZE , pI2cDMAReadBuf, PhyAddrForDMABuf );
pI2cDMAWriteBuf = NULL;
PhyAddrForDMABuf = 0;
return ret;
}
static const struct file_operations i2cdev_fops = {
...
.read= i2cdev_read_dma,
.write = i2cdev_write_dma,
};