新手上路
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2020-6-1
- 在线时间
- 5 小时
|
楼主 |
发表于 2021-7-19 15:11:54
|
显示全部楼层
platform driver:
#define DEV_NAME "i2c_3216"
struct my_i2c_3216{
struct timer_list timer;
struct i2c_client *my_i2c_client;
};
struct my_i2c_3216 i2c_3216_device;
void i2c_3216_read_reg(int reg, uint8_t *buf)
{
struct i2c_msg msg[2] = {0};
msg[0].addr = i2c_3216_device.my_i2c_client->addr;
msg[0].len = 1;
msg[0].buf = ®
msg[0].flags = 0;
msg[1].addr = i2c_3216_device.my_i2c_client->addr;
msg[1].len = 1;
msg[1].buf = buf;
msg[1].flags = I2C_M_RD;
i2c_transfer(i2c_3216_device.my_i2c_client->adapter, msg, 2);
}
void i2c_3216_write_reg(int reg, uint8_t *buf)
{
struct i2c_msg msg[2] = {0};
msg[0].addr = i2c_3216_device.my_i2c_client->addr;
msg[0].len = 1;
msg[0].buf = ®
msg[0].flags = 0;
msg[1].addr = i2c_3216_device.my_i2c_client->addr;
msg[1].len = 1;
msg[1].buf = buf;
msg[1].flags = 0;
i2c_transfer(i2c_3216_device.my_i2c_client->adapter, msg, 2);
}
//定时器中断函数,用于按键消除抖动
void timer_handler(unsigned long param)
{
uint8_t data[2] = {0};
uint16_t ps = 0;
struct my_i2c_3216 *dev = (struct my_i2c_3216 *)param;
mod_timer(&dev->timer, jiffies + msecs_to_jiffies(300));
i2c_3216_read_reg(0x0e, &data[0]);
i2c_3216_read_reg(0x0e, &data[1]);
ps = data[0] | (data[1] << 8);
printk("3216 ps data : %d \r\n", ps);
}
int i2c_3216_probe(struct i2c_client *client, const struct i2c_device_id *id )
{
uint8_t data = 0;
//保存设备信息,用于获取设备地址
i2c_3216_device.my_i2c_client = client;
if( client )
{
printk("client addr : %02x \r\n", client->addr);
}
//初始化定时器, 定时器中断中读取ps数据并输出
init_timer(&i2c_3216_device.timer);
i2c_3216_device.timer.function = timer_handler;
i2c_3216_device.timer.data = (unsigned long)&i2c_3216_device;
mod_timer(&i2c_3216_device.timer, jiffies + msecs_to_jiffies(300));
//初始化3216
data = 0x04;
i2c_3216_write_reg(0x00, &data);
mdelay(50);
data = 0x03;
i2c_3216_write_reg(0x00, &data);
}
int i2c_3216_remove(struct i2c_client *client)
{
del_timer(&i2c_3216_device.timer);
}
struct i2c_device_id _3216_id_table[] = {
{.name = "i2c_3216_dev"},
};
struct i2c_driver i2c_3216_driver = {
.probe = i2c_3216_probe,
.remove = i2c_3216_remove,
.id_table = _3216_id_table,
.driver = {
.owner = THIS_MODULE,
.name = "i2c_3216_dev",
},
};
int my_platform_probe(struct platform_device *dev)
{
//注册3216驱动
i2c_register_driver(THIS_MODULE, &i2c_3216_driver);
return 0;
}
int my_platform_remove(struct platform_device *dev)
{
i2c_del_driver(&i2c_3216_driver);
printk("my_platform_remove\r\n");
}
struct platform_driver my_platform_driver = {
.driver = {
.name = "i2c_3216",
.owner = THIS_MODULE,
},
.probe = my_platform_probe,
.remove = my_platform_remove,
};
module_platform_driver(my_platform_driver);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("i2c 3216 driver");
|
|