完成cst816t的触摸坐标上报,lvgl中能够正常读取

This commit is contained in:
Yu Xifeng 2024-11-28 07:35:59 +00:00
parent 74b90cef1a
commit 611d7bc87b
2 changed files with 72 additions and 23 deletions

View File

@ -19,6 +19,7 @@
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/platform_device.h> /*平台设备*/
#include <linux/workqueue.h>
#include "cst816_update.h"
@ -42,11 +43,13 @@ struct class *class_mpu6050; //保存创建的类
struct device *device_mpu6050; // 保存创建的设备
struct device_node *mpu6050_device_node; //rgb_led的设备树节点结构体
static struct workqueue_struct *button_wq;
static struct work_struct button_work;
/*------------------IIC设备内容----------------------*/
struct i2c_client *mpu6050_client = NULL; //保存mpu6050设备对应的i2c_client结构体,匹配成功后由.prob函数带回。
static struct input_dev *cst816x_i_dev;
static int i2c_write_16bit_address(struct i2c_client *client, u16 address, u8 data)
{
@ -88,7 +91,7 @@ static int i2c_read_cst816x(struct i2c_client *mpu6050_client, u8 address, void
int error = 0;
u8 address_data = address;
struct i2c_msg mpu6050_msg[2];
printk(KERN_EMERG "\n i2c_read addr:%X \n",mpu6050_client->addr);
/*设置读取位置msg*/
mpu6050_msg[0].addr = mpu6050_client->addr; //mpu6050在 iic 总线上的地址
mpu6050_msg[0].flags = 0; //标记为发送数据
@ -279,25 +282,44 @@ static struct file_operations mpu6050_chr_dev_fops =
/*按键中断处理函数*/
static irqreturn_t button_input_irq_hander(int irq, void *dev_id)
{
// struct button_data *priv = dev_id;
// int button_satus;
/* 将工作项添加到工作队列中 */
queue_work(button_wq, &button_work);
// /*读取按键引脚的电平,根据读取得到的结果输入按键状态*/
// button_satus = (gpiod_get_value(priv->button_input_gpiod) & 1);
// if(button_satus)
// {
// input_report_key(priv->button_input_dev, BTN_0, 1);
// input_sync(priv->button_input_dev);
// }
// else
// {
// input_report_key(priv->button_input_dev, BTN_0, 0);
// input_sync(priv->button_input_dev);
// }
printk(KERN_EMERG "\t button_input_irq_hander \n");
return IRQ_HANDLED;
return IRQ_HANDLED;
}
static void button_work_fn(struct work_struct *work)
{
uint8_t touch_tmp[12];
uint16_t touch_coordinate_x = 0;
uint16_t touch_coordinate_y = 0;
i2c_read_cst816x(mpu6050_client, 0x00, &touch_tmp[0], 7);
// 解析坐标值
touch_coordinate_x = ((uint16_t)(touch_tmp[3] & 0x0f) << 8) + (uint16_t)touch_tmp[4];
touch_coordinate_y = ((uint16_t)(touch_tmp[5] & 0x0f) << 8) + (uint16_t)touch_tmp[6];
// printk(KERN_EMERG "\t touch_coordinate X:%d,Y:%d (%d)\n", touch_coordinate_x, touch_coordinate_y,touch_tmp[3] & 0xf0);
/* 上报触摸坐标 */
input_report_abs(cst816x_i_dev, ABS_MT_SLOT, 0);
input_report_abs(cst816x_i_dev, ABS_X, touch_coordinate_x);
input_report_abs(cst816x_i_dev, ABS_Y, touch_coordinate_y);
if((touch_tmp[3] & 0xf0) == 0x80)
{
input_report_abs(cst816x_i_dev, ABS_MT_TRACKING_ID, 0);
input_report_key(cst816x_i_dev, BTN_TOUCH, 1);
}else{
input_report_abs(cst816x_i_dev, ABS_MT_TRACKING_ID, -1);
input_report_key(cst816x_i_dev, BTN_TOUCH, 0);
}
/* 触摸事件结束,调用 input_sync() 同步 */
input_sync(cst816x_i_dev);
}
static int btn_open(struct input_dev *i_dev)
{
pr_info("input device opened()\n");
@ -335,7 +357,7 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
pr_err("CST816D: Failed to allocate input device\n");
return -ENOMEM;
}
cst816x_i_dev = i_dev;
/* 分配私有数据 */
priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
if (!priv) {
@ -371,8 +393,25 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
priv->irq = gpiod_to_irq(irq_gpio);
priv->Interrupt_gpiod = irq_gpio;
set_bit(EV_KEY, i_dev->evbit); /*设置要使用的输入事件类型*/
set_bit(BTN_0, i_dev->keybit); /* 按钮 0*/
/* 设置设备事件类型 */
set_bit(EV_ABS, i_dev->evbit); /* 使用绝对坐标 */
set_bit(ABS_X, i_dev->absbit); /* X轴 */
set_bit(ABS_Y, i_dev->absbit); /* Y轴 */
set_bit(ABS_PRESSURE, i_dev->absbit); /* 压力 */
set_bit(ABS_MT_SLOT, i_dev->absbit); /* 设置多点触控槽 */
set_bit(ABS_MT_POSITION_X, i_dev->absbit); /* 设置 X 坐标 */
set_bit(ABS_MT_POSITION_Y, i_dev->absbit); /* 设置 Y 坐标 */
set_bit(ABS_MT_PRESSURE, i_dev->absbit); /* 设置触摸点压力 */
set_bit(ABS_MT_TRACKING_ID, i_dev->absbit); /* 触摸点的跟踪 ID */
/* 设置触摸屏设备的事件 */
/* 设置绝对坐标 */
input_set_abs_params(i_dev, ABS_X, 0, 240, 0, 0); /* 设置 X 坐标的范围 */
input_set_abs_params(i_dev, ABS_Y, 0, 300, 0, 0); /* 设置 Y 坐标的范围 */
input_set_abs_params(i_dev, ABS_PRESSURE, 0, 255, 0, 0); /* 设置压力范围 */
ret = input_register_device(priv->button_input_dev);
if (ret) {
@ -381,7 +420,7 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
}
ret = request_any_context_irq(priv->irq, button_input_irq_hander,IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "input-button", priv);
ret = request_any_context_irq(priv->irq, button_input_irq_hander,IRQF_TRIGGER_FALLING, "input-button", priv);
if (ret < 0) {
dev_err(&client->dev,"请求gpio中断失败\n");
goto err_btn;
@ -409,7 +448,8 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
mpu6050_client = client;
gpio_direction_output(io_cst816d_reset, 0);
usleep_range(10000, 12000); // 精确延时 50~55ms
// usleep_range(10000, 12000); // 精确延时 50~55ms
usleep_range(500000, 550000); // 精确延时 50~55ms
gpio_direction_output(io_cst816d_reset, 1);
usleep_range(500000, 550000); // 精确延时 50~55ms
@ -465,7 +505,13 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
printk(KERN_EMERG "\n chip_id:%X \n",chip_id);
}
/* 创建工作队列 */
button_wq = create_workqueue("button_wq");
if (!button_wq)
return -ENOMEM;
/* 初始化工作结构 */
INIT_WORK(&button_work, button_work_fn);
printk(KERN_EMERG "\n cst816d ok! \n");
return 0;
@ -495,6 +541,9 @@ static int cst816d_remove(struct i2c_client *client)
if (!priv)
return -EINVAL;
/* 清理工作队列 */
flush_workqueue(button_wq);
destroy_workqueue(button_wq);
/* 1. 注销输入子系统 */
input_unregister_device(priv->button_input_dev);

Binary file not shown.