MTk i2c读写例子



FIFO MODE (<= 8 byte):


static int i2c_write(struct i2c_client *client, uint8_t regaddr, uint8_t txbyte, uint8_t *data)
{
int ret = 0;
unsigned char buffer[8];
struct i2c_msg msg;


memset(&msg, 0, sizeof(struct i2c_msg));


buffer[0] = regaddr;
memcpy((buffer + 1),data,txbyte);

msg.addr = client->addr;
msg.flags = 0;
msg.len = txbyte+1;
msg.buf = buffer;
// msg.timing = 100;  //i2c speed KHz, if not set timing, i2c driver will use default speed 100K


ret = i2c_transfer(client->adapter, &msg, 1);


if (ret != 1)
{
//error
}


return ret;
}


static int i2c_read(struct i2c_client *client, uint8_t regaddr, uint8_t rxbyte, uint8_t *data)
{
int ret = 0;
unsigned char buffer[8];
struct i2c_msg msg;


memset(&msg, 0, sizeof(struct i2c_msg));


buffer[0] = regaddr;
msg.addr = client->addr;
msg.flags = 0;
msg.len = ((rxbyte & 0xff)<<8) | 1;
msg.buf = buffer;
msg.ext_flag = I2C_WR_FLAG | I2C_RS_FLAG;
// msg.timing = 100;


ret = i2c_transfer(client->adapter, &msg, 1);


if (ret != 1)
{
//error
}


memcpy(data, buffer, rxbyte);


return ret;
}




DMA MODE (> 8 byte):


static int i2c_write_dma(struct i2c_client *client, uint8_t regaddr, uint8_t txbyte, uint8_t *data)
{
uint8_t *v_buf = NULL;
dma_addr_t p_buf;
int ret = 0;
struct i2c_msg msg;


memset(&msg, 0, sizeof(struct i2c_msg));


v_buf = dma_alloc_coherent(&(client->adapter->dev), txbyte, &p_buf, GFP_KERNEL|GFP_DMA32);


*v_buf = regaddr;
memcpy((v_buf + 1), data, txbyte);


msg.addr = client->addr;
msg.flags = 0;
msg.len = txbyte + 1;
msg.buf = (uint8_t *)p_buf;
msg.ext_flag |= I2C_DMA_FLAG;
// msg.timing = 100;


ret = i2c_transfer(client->adapter, &msg, 1);


if (ret != 1)
{
//error
}


dma_free_coherent(&(client->adapter->dev), txbyte, v_buf, p_buf);
return ret;
}


static int i2c_read_dma(struct i2c_client *client, uint8_t regaddr, uint8_t rxbyte, uint8_t *data) 
{
uint8_t *v_buf = NULL;
dma_addr_t p_buf;
int ret = 0;
struct i2c_msg msg;


memset(&msg, 0, sizeof(struct i2c_msg));


// client->adapter->dev.coherent_dma_mask = DMA_BIT_MASK(32);
// client->adapter->dev.dma_mask = &client->adapter->dev.coherent_dma_mask;
v_buf = dma_alloc_coherent(&(client->adapter->dev), rxbyte, &p_buf, GFP_KERNEL|GFP_DMA32);


*v_buf = regaddr;
msg.addr = client->addr;
msg.flags = 0;
msg.len = ((rxbyte & 0xff)<<8) | 1;
msg.buf = (uint8_t *)p_buf;
msg.ext_flag = I2C_WR_FLAG | I2C_RS_FLAG | I2C_DMA_FLAG;
// msg.timing = 100;


ret = i2c_transfer(client->adapter, &msg, 1);


if(ret != 1)
{
//error
}


memcpy(data, v_buf, rxbyte);


dma_free_coherent(&(client->adapter->dev), rxbyte, v_buf, p_buf);
return ret;
}



©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值