/////////////////////////////////////////////////////////////////////////////////////
i2c通用读写函数:
static int i2c_ReadData(char *rxdata, int length, u8 cmd)
{
int ret;
u8 read_cmd[1] = {cmd};
struct i2c_msg msgs[] = {
/*{
.addr = this_client->addr,
.flags = 0,
.len = 1,
.buf = read_cmd,
},*/
{
.addr = this_client->addr,
.flags = I2C_M_RD,
.len = length,
.buf = rxdata,
},
};
//printk("IIC addr = %x\n",this_client->addr);
ret = i2c_transfer(this_client->adapter, msgs, 1);
if (ret < 0)
printk("msg %s i2c read error: %d\n", __func__, ret);
return ret;
}
static int i2c_Writedata(char *txdata, int length)
{
int ret;
struct i2c_msg msg[] = {
{
.addr = this_client->addr,
.flags = 0,
.len = length,
.buf = txdata,
},
};
ret = i2c_transfer(this_client->adapter, msg, 1);
if (ret < 0)
printk("%s i2c write error: %d\n", __func__, ret);
return ret;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//Actions平台I2C读写
/**
* i2c_master_send - issue a single I2C message in master transmit mode
* @client: Handle to slave device
* @buf: Data that will be written to the slave
* @count: How many bytes to write, must be less than 64k since msg.len is u16
*
* Returns negative errno, or else the number of bytes written.
*/
int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
{
int ret;
struct i2c_adapter *adap = client->adapter;
struct i2c_msg msg;
msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
msg.len = count;
msg.buf = (char *)buf;
ret = i2c_transfer(adap, &msg, 1);
/*
* If everything went ok (i.e. 1 msg transmitted), return #bytes
* transmitted, else error code.
*/
return (ret == 1) ? count : ret;
}
EXPORT_SYMBOL(i2c_master_send);
/**
* i2c_master_recv - issue a single I2C message in master receive mode
* @client: Handle to slave device
* @buf: Where to store data read from slave
* @count: How many bytes to read, must be less than 64k since msg.len is u16
*
* Returns negative errno, or else the number of bytes read.
*/
int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
{
struct i2c_adapter *adap = client->adapter;
struct i2c_msg msg;
int ret;
msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
msg.flags |= I2C_M_RD;
msg.len = count;
msg.buf = buf;
ret = i2c_transfer(adap, &msg, 1);
/*
* If everything went ok (i.e. 1 msg received), return #bytes received,
* else error code.
*/
return (ret == 1) ? count : ret;
}
EXPORT_SYMBOL(i2c_master_recv);
/////////////////////////////////////////////////////////////////////////////////////
上一篇:关于单片机死机的一些问题
下一篇:I2C通信 读写数据过程
推荐阅读最新更新时间:2024-03-16 16:09