一、前言
在产品开发中,触摸功能是非常常见的,不同厂家的触摸调试大致相同,但又有点不太一样。这款触摸板送过来时并没有触摸功能在里面,需要在安卓板通过i2c传输数据(厂家提供)到触摸板进行升级。
二、查看引脚分布
可以看到主要有中断脚、i2c、复位脚。
三、查看规格书
可以知道这块触摸板有两个i2c地址,未有触摸功能前是0x6A,在进行了升级后i2c地址是0x15。
四、安卓端去升级触摸板
1.编写 i2c写设备代码
//argc1:i2c句柄 argc2:i2c地址(16位)argc3:写buffer argc4:写的数据长度
int write_to_i2c_device(struct i2c_client *client, u16 reg, u8 *buf, u16 len)
{
int ret;
u8 *tx_buf;
//内核中动态分配一个struct i2c_msg 结构体的内存,并将其地址存储在msgs变量中,以便后续使用。
struct i2c_msg *msgs = kzalloc(sizeof(struct i2c_msg), GFP_KERNEL);
if (!msgs)
{
return -ENOMEM;
}
//写buffer需要传输地址+数据
tx_buf = kzalloc(sizeof(reg) + len, GFP_KERNEL);
if (!tx_buf)
{
kfree(msgs);
return -ENOMEM;
}
//将16位地址分割,将前八位赋值到tx_buf[0]、后八位赋值到tx_buf[1]
tx_buf[0] = reg >> 8;
tx_buf[1] = reg & 0xff;
//因为tx_buf里面的数组是i2c地址,不能覆盖,偏移两个字节把写入的数据buf赋值到数组tx_buf中
memcpy(tx_buf + 2, buf, len);
msgs[0].addr = 0x6A;//升级前i2c地址
msgs[0].flags = 0;//写标志
msgs[0].len = len + sizeof(reg);//传输长度
msgs[0].buf = tx_buf;//传输数据
ret = i2c_transfer(client->adapter, msgs, 1);//发送数据
kfree(msgs);//释放msgs
kfree(tx_buf);//释放tx_buf数组
return ret;
}
2.编写 i2c读设备代码
读一个字节:
//argc1 : i2c句柄 argc2:设备寄存器地址 argc3:寄存器读出数据存储数组
int read_byte_from_i2c_device(struct i2c_client *client, u16 addr, u8 *data)
{
int ret;
u8 buf[2];
//写16位地址,低八位放到buf[0],高八位放到buf[1]
buf[0] = (u8)(addr >> 8);
buf[1] = (u8)(addr & 0xff);
//读设备时需要写地址,读数据,所以分两个msgs发送
struct i2c_msg msgs[] = {
{
.addr = 0x6A,//i2c地址
.flags = 0,//写标志
.len = 2,//写数据长度
.buf = buf,//写buffer
},
{
.addr = 0x6A,//i2c地址
.flags = I2C_M_RD,//读标志
.len = 1,//读数据长度
.buf = data,//读buffer存放
},
};
ret = i2c_transfer(client->adapter, msgs, 2);//发送数据
if (ret < 0)
{
pr_debug("Failed to read from I2C device: %d\n", ret);
return ret;
}
return 0;
}
读两个字节:
int read_2byte_from_i