TP 触摸屏开发指南
本文档介绍本芯片的 TP(Touch Panel,触摸屏)驱动开发方法,涵盖电容式触摸屏(CTP)和电阻式触摸屏(RTP)。
前置知识
阅读本文档前,建议先了解以下基础知识:
- C 语言编程:驱动开发和 API 调用均使用 C 语言
- TWI 总线:CTP 电容触摸屏通过 TWI 总线与主控通信
- GPIO(通用输入输出引脚):触摸屏中断和复位信号通过 GPIO 引脚连接
- RTOS 基础:任务、信号量、中断处理等概念
- LVGL(Light and Versatile Graphics Library,轻量级通用图形库):应用层 GUI 框架,通过 Input 子系统获取触摸事件
功能介绍
层次结构
本平台通过 Input 输入子系统 统一管理触摸屏设备。Input 子系统提供标准的事件上报接口,支持电容式触摸屏(CTP)和电阻式触摸屏(RTP)两种类型。
Input 子系统特性:
- 统一的事件模型:支持 EV_KEY(按键)、EV_ABS(绝对坐标)、EV_SYN(同步)等标准事件类型
- 环形缓冲区:使用环形缓冲区存储输入事件,支持多任务读取
- 信号量同步:通过信号量实现事件到达通知,支持阻塞和非阻塞读取
- 多设备管理:通过链表管理多个输入设备,按名称匹配 evdev(事件设备)
- 中断驱动:触摸事件通过 GPIO(通用输入输出引脚)中断触发,在独立线程中处理数据读取和上报
TP 驱动支持两种触摸屏类型:
RTP 电阻式触摸屏
| 驱动 | 通信接口 | 配置宏 | 说明 |
|---|---|---|---|
| TPADC RTP | TPADC(触摸面板模数转换器) | CONFIG_DRIVERS_TOUCHSCREEN_RTP=y | 基于片内 TPADC 模块,无需外部 IC,需校准 |
CTP 电容式触摸屏
| 驱动 | 通信接口 | 配置宏 | 说明 |
|---|---|---|---|
| CTP 统一框架 | TWI | CONFIG_DRIVERS_TOUCHSCREEN_CTP=y | 支持 FT5446、GT928 等芯片,统一驱动框架 |
已废弃的 CTP 独立驱动:GT911 / TLSC6X / CST226SE / ILI2117 为早期独立驱动,目前已不再维护,请使用 CTP 统一框架。
注:melis 系统中对应配置宏为
CONFIG_RTP_SECLET/CONFIG_CTP_SECLET(legacy 类型选择),freertos 系统为CONFIG_DRIVERS_TOUCHSCREEN_*,具体见对应系统 menuconfig。
TP 模块在系统中的层次结构如下:
应用层(App / LVGL / GUI)
|
Input 子系统(sunxi-input)
|-- sunxi_input_open() # 打开设备获取文件描述符
|-- sunxi_input_read() # 非阻塞读取事件
|-- sunxi_input_readb() # 阻塞读取事件
|-- sunxi_input_get_sync() # 获取可读事件数量
|
TP 驱动层(drivers/drv/input/tp/)
|-- ctp/sunxi_ctp.c # CTP 统一框架
|-- rtp/sunxi_tpadc_rtp.c # RTP 驱动
|
硬件层(TWI / TPADC / GPIO 中断)
源码结构
sunxi-input.cInput 子系统核心代码input-event-codes.h事件类型定义KconfigTP 驱动配置选项Makefile编译规则sunxi_ctp.cCTP 框架核心代码sunxi_ctp_dev.c设备管理(注册/查找驱动)sunxi_ctp_dev.h驱动接口和数据结构定义KconfigMakefileft5446.cFocalTech FT5446 驱动ft5446.hgt928.cGoodix GT928 驱动gt928.h
sunxi_tpadc_rtp.cRTP 驱动核心代码sunxi_tpadc_rtp.hRTP 数据结构定义drv_rtp.cRTP 校准和数据处理sunxi_drv_rtp.hRTP 驱动接口
Input 子系统工作机制
Input 子系统的核心数据结构:
- sunxi_input_dev:输入设备,描述一个输入设备的能力(支持的事件类型和编码)
- sunxi_evdev:事件设备,为每个打开的设备创建,维护独立的事件缓冲区和信号量
- sunxi_input_event:输入事件,包含 type(事件类型)、code(事件编码)、value(事件值)
事件上报流程:
- TP 驱动硬件中断触发 → ISR(中断服务例程)中发送信号量唤醒事件处理线程
- 事件处理线程通过 TWI 读取触摸坐标数据
- 调用
sunxi_input_event()上报 EV_ABS(坐标)和 EV_KEY(触摸状态)事件 - 通过 EV_SYN/SYN_REPORT 同步事件表示一帧数据结束
- 应用层通过
sunxi_input_read()读取事件
配置使用
TP 驱动主开关
在 menuconfig 中使能 TP 驱动:
Drivers Options → other drivers → input driver →
[*] touchscreen driver
RTP 电阻式触摸屏 menuconfig 路径:
Drivers Options → other drivers → input driver → touchscreen driver →
[*] enable touchscreen(tpadc) Driver → CONFIG_DRIVERS_TOUCHSCREEN_RTP
[*] enable touchscreen drivers APIs test command → CONFIG_DRIVERS_TEST_TOUCHSCREEN
CTP 电容式触摸屏 menuconfig 路径:
Drivers Options → other drivers → input driver → touchscreen driver →
[*] enable CTP touchscreen driver → CONFIG_DRIVERS_TOUCHSCREEN_CTP
CTP Devices →
[*] support FT5446 CTP device → CONFIG_CTP_DEVICES_FT5446
[ ] support GT928 CTP device → CONFIG_CTP_DEVICES_GT928
[*] enable touchscreen drivers APIs test command → CONFIG_DRIVERS_TEST_TOUCHSCREEN
RTP 电阻式触摸屏
当使用电阻式触摸屏时,需要使能 RTP 驱动并配置 [rtp_para] 段。
defconfig 配置
CONFIG_DRIVERS_TOUCHSCREEN=y
CONFIG_DRIVERS_TOUCHSCREEN_RTP=y
CONFIG_DRIVERS_TEST_TOUCHSCREEN=y
CONFIG_DRIVERS_TPADC 由 CONFIG_DRIVERS_TOUCHSCREEN_RTP 通过 select DRIVERS_TPADC 自动选中,用户无需(也不应手动关闭)单独配置。使能 CONFIG_DRIVERS_TEST_TOUCHSCREEN=y 后,在 shell 输入 rtp_test(RTP)或 tp_test(CTP)即可启动触摸事件打印测试。
sys_config 配置(board/<chip>/<board>/configs/sys_config.fex)
[rtp_para]
rtp_used = 1
rtp_adjust_cfg_path = "/data/tp.cfg"
rtp_screen_max_x = 1024
rtp_screen_max_y = 600
| 配置项 | 说明 |
|---|---|
rtp_used | 是否使能电阻式触摸屏,0=禁用,1=使能 |
rtp_adjust_cfg_path | 触摸校准配置文件路径,用于存储触摸屏校准数据 |
rtp_screen_max_x | 触摸屏 X 轴最大坐标值,应与 LCD 屏幕宽度一致 |
rtp_screen_max_y | 触摸屏 Y 轴最大坐标值,应与 LCD 屏幕高度一致 |
RTP 首次上电若 /data/tp.cfg(rtp_adjust_cfg_path)不存在会自动进入校准流程,按屏幕左上→右上→左下→右下→中心顺序点按 5 个点;校准完成后数据写入配置文件。也可在 shell 执行 rtp_test 命令(需使能 CONFIG_DRIVERS_TEST_TOUCHSCREEN)手动触发。
CTP 电容式触摸屏
当使用电容式触摸屏时,请使用 CTP 统一框架,不要使用已废弃的独立驱动。
defconfig 配置
CONFIG_DRIVERS_TOUCHSCREEN=y
CONFIG_DRIVERS_TOUCHSCREEN_CTP=y
CONFIG_CTP_DEVICES_FT5446=y
CONFIG_DRIVERS_TEST_TOUCHSCREEN=y
sys_config 配置(board/<chip>/<board>/configs/sys_config.fex)
[ctp_para]
ctp_used = 1
ctp_twi_id = 1
ctp_driver = "sunxi_ft5446"
ctp_screen_max_x = 1024
ctp_screen_max_y = 600
ctp_reverse_x = 0
ctp_reverse_y = 0
ctp_modify_reg = 1
ctp_int_port = port:PD07<1><1><3><1>
ctp_wakeup = port:PD06<1><1><3><1>
| 字段 | 值 | 说明 |
|---|---|---|
ctp_used | 1 | 是否使能 CTP 电容触摸屏,0=禁用,1=使能 |
ctp_twi_id | 1 | TWI 总线编号,如 0=TWI0,1=TWI1 |
ctp_driver | "sunxi_ft5446" | 驱动名称,必须与 defconfig 中选中的芯片驱动匹配("sunxi_ft5446" / "sunxi_gt928") |
ctp_screen_max_x | 1024 | 触摸屏 X 轴最大坐标值,应与 LCD 屏幕宽度一致 |
ctp_screen_max_y | 600 | 触摸屏 Y 轴最大坐标值,应与 LCD 屏幕高度一致 |
ctp_reverse_x | 0 | X 轴坐标反转,0=正常,1=沿 X 轴翻转(坐标 = max_x − 原始坐标) |
ctp_reverse_y | 0 | Y 轴坐标反转,0=正常,1=沿 Y 轴翻转(坐标 = max_y − 原始坐标) |
ctp_modify_reg | 1 | 屏幕方向切换(交换 X/Y 轴),0=不交换,1=交换 X 和 Y 坐标 |
ctp_int_port | port:PD07<1><1><3><1> | 触摸中断引脚,格式见下方 GPIO 格式说明 |
ctp_wakeup | port:PD06<1><1><3><1> | 触摸复位/唤醒引脚,格式同上 |
GPIO 配置格式(以 port:PD07<1><1><3><1> 为例):
| 部分 | 含义 | 示例值 | 说明 |
|---|---|---|---|
PD07 | GPIO 引脚 | PD07 | P=GPIO 前缀,D = 第 4 组(A=1, B=2, C=3, D=4…),07 = 组内第 7 号脚。GPIO 编号 = (组-1) × 32 + 序号 |
<1> | 功能选择 (mul_sel) | 1 | GPIO 复用功能编号 |
<1> | 上下拉 (pull) | 1 | 内部上下拉,1=上拉,0=禁用 |
<3> | 驱动能力 (drv_level) | 3 | GPIO 驱动强度等级 |
<1> | 默认电平 (data) | 1 | GPIO 输出时的默认数据电平 |
GT928 配置示例
[ctp_para]
ctp_used = 1
ctp_twi_id = 1
ctp_driver = "sunxi_gt928"
ctp_screen_max_x = 1024
ctp_screen_max_y = 600
ctp_int_port = port:PD07<1><1><3><1>
ctp_wakeup = port:PD06<1><1><3><1>
CONFIG_DRIVERS_TOUCHSCREEN=y
CONFIG_DRIVERS_TOUCHSCREEN_CTP=y
CONFIG_CTP_DEVICES_GT928=y
说明:ctp_driver 未配置时默认 "sunxi_gt928"(7 位地址 0x5D)。本平台 evb1 默认未开启 CONFIG_DRIVERS_TOUCHSCREEN_CTP,需在 menuconfig 中打开。
CTP 坐标处理流程(在 ctp_task 线程中执行,参照 sunxi_ctp.c):
- 从触摸 IC 通过 TWI 读取原始坐标 (x, y)
- 如果
ctp_modify_reg=1,交换 X 和 Y:temp=x, x=y, y=temp - 钳制上界:
x >= max_x → x = max_x - 1,y >= max_y → y = max_y - 1(仅钳上界,下界由x > 0 && y > 0判定为触摸释放) - 如果
ctp_reverse_x=1,翻转 X:x = max_x − x - 如果
ctp_reverse_y=1,翻转 Y:y = max_y − y - 通过
sunxi_input_event()上报到 Input 子系统
应用层通过调用 sunxi_input_init() 来初始化 TP 驱动(非系统自动调用),该函数未在头文件中导出,需要使用 extern 声明。典型初始化位置是 LVGL 的输入设备初始化函数 touchpad_init() 中(参考 lichee/rtos/components/common/thirdparty/littlevgl-9/platform/indev/sunxi_indev_freertos.c),流程为:
extern int sunxi_input_init(void)声明- 调用
sunxi_input_init()初始化 TP 驱动 - 调用
sunxi_input_open("touchscreen")获取文件描述符 - 在 LVGL 的
touchpad_is_pressed()中通过sunxi_input_get_sync()+sunxi_input_read()读取触摸事件
编译验证
编译完成后,TP 相关驱动产物位于:
- RTP 驱动:
lichee/rtos/build/<board>/drivers/drv/input/tp/rtp/ - CTP 驱动:
lichee/rtos/build/<board>/drivers/drv/input/tp/ctp/
可通过以下命令验证 TP 驱动是否已编译进固件:
# 检查 .config 中 TP 相关配置
grep DRIVERS_TOUCHSCREEN lichee/rtos/build/<board>/.config
# 检查符号表中是否有 TP 初始化函数
donm | grep -E "tpadc_rtp_init|sunxi_ctp_init"
接口介绍
Input 子系统接口分为三类:事件编码(所有代码示例中的宏定义)、应用层接口(LVGL / 用户代码使用)和驱动内部接口(TP 驱动开发使用)。
事件编码定义
以下为触摸屏常用的输入事件编码,定义在 lichee/rtos/drivers/drv/input/input-event-codes.h 中,后续所有代码示例中的宏均来源于此:
// 事件类型
EV_SYN // 同步事件
EV_KEY // 按键事件
EV_ABS // 绝对坐标事件
// 绝对坐标编码
ABS_MT_POSITION_X // 多点触摸 X 坐标
ABS_MT_POSITION_Y // 多点触摸 Y 坐标
ABS_MT_TOUCH_MAJOR // 触摸面积(RTP 上报)
ABS_MT_WIDTH_MAJOR // 触摸宽度(RTP 上报)
ABS_MT_TRACKING_ID // 触摸点追踪 ID
ABS_MT_PRESSURE // 触摸压力(仅 CTP 上报,RTP 不上报)
// 按键编码
BTN_TOUCH // 触摸按键
KEY_MENU // 菜单键
KEY_BACK // 返回键
KEY_HOMEPAGE // 主页键
// 同步编码
SYN_REPORT // 报告同步(表示一帧数据结束)
SYN_MT_REPORT // 多点触摸报告同步
多点触摸(SYN_MT_REPORT / input_mt_sync_t())仅在已废弃独立驱动(gt911、tlsc6x、cst226se、ili2117)中实现;CTP 统一框架与 RTP 当前仅支持单点上报。
两类驱动实际可被应用层观测到的事件编码不同(均以 SYN_REPORT 结尾):
- CTP:
ABS_MT_TRACKING_ID、ABS_MT_POSITION_X/Y、ABS_MT_PRESSURE(按下 100 / 抬起 0) - RTP:驱动源码中上报
ABS_MT_*事件,但其能力注册仅含ABS_X/ABS_Y/BTN_TOUCH(drv_rtp.c的sunxi_rtp_register),ABS_MT_*事件会被 Input 子系统按能力位过滤丢弃。实际可被应用层观测的只有BTN_TOUCH(按下 1 / 抬起 0);坐标为 SDK 侧能力注册与上报不一致,需 SDK 补齐ABS_MT_*能力注册后方可上报。
应用层接口
应用层使用流程:init → open → read/get_sync。
sunxi_input_init
int sunxi_input_init(void);
功能:初始化 Input 子系统及触摸屏驱动。根据 defconfig 配置调用对应驱动的初始化函数。
返回值:0 成功,-1 失败
使用方式:该函数未在头文件中导出,使用时需 extern 声明。实际使用见 lichee/rtos/components/common/thirdparty/littlevgl-9/platform/indev/sunxi_indev_freertos.c 的 touchpad_init():
#include <sunxi-input.h>
static int32_t touch_fd = -1;
void touchpad_init(void)
{
extern int sunxi_input_init(void);
int32_t ret = sunxi_input_init();
if (ret < 0) {
LV_LOG_WARN("sunxi_input_init fail");
return;
}
touch_fd = sunxi_input_open("touchscreen");
if (touch_fd < 0) {
LV_LOG_WARN("touchscreen open err");
return;
}
}
touchpad_is_pressed(读取侧完整实现,来源同上):
bool touchpad_is_pressed(void)
{
bool touch_down = false;
int32_t count = 0, pressure = 0;
if (touch_fd < 0) return false;
count = sunxi_input_get_sync(touch_fd);
if (count > 0) {
for (int32_t i = 0; i < count; i++) {
sunxi_input_read(touch_fd, &event, sizeof(struct sunxi_input_event));
if (event.type == EV_ABS) {
switch (event.code) {
case ABS_MT_POSITION_X: touch_x = event.value; break;
case ABS_MT_POSITION_Y: touch_y = event.value; break;
case ABS_MT_TRACKING_ID: touch_id = event.value; break;
case ABS_MT_PRESSURE: pressure = event.value; break;
}
} else if (event.type == EV_SYN && event.code == SYN_REPORT) {
if (pressure > 0) touch_down = true;
}
}
}
return touch_down;
}
上述 touchpad_is_pressed() 用 ABS_MT_PRESSURE(pressure > 0)判断按下,该逻辑仅适用于 CTP。
RTP 驱动不上报 ABS_MT_PRESSURE(且 ABS_MT_* 坐标事件也因能力注册缺失被过滤,见上文)。RTP 实际可被应用层观测的只有 BTN_TOUCH(按下 1 / 抬起 0)。若使用 RTP,touchpad_is_pressed() 应改为基于 BTN_TOUCH 判断,否则 pressure 恒为 0、touch_down 恒为 false,触摸事件将无法被识别。
sunxi_input_open
int sunxi_input_open(const char *name);
功能:打开指定名称的输入设备,创建 evdev 结构体(含环形缓冲区、信号量),返回文件描述符。
参数:name — 设备名称,TP 驱动统一为 "touchscreen"
返回值:>=0 文件描述符,-1 设备不存在
实际使用(来源同上):
touch_fd = sunxi_input_open("touchscreen");
if (touch_fd < 0) {
LV_LOG_WARN("touchscreen open err");
return;
}
sunxi_input_read
int sunxi_input_read(int fd, void *buffer, unsigned int size);
功能:非阻塞读取输入事件。从环形缓冲区读取最多 size 字节,每个事件为 struct sunxi_input_event(12 字节)。无数据时立即返回 0。
参数:
fd—sunxi_input_open()返回的文件描述符buffer— 事件数据缓冲区size— 缓冲区大小(字节)
返回值:>0 实际读取字节数,0 无可用事件,-1 参数错误
sunxi_input_get_sync
int sunxi_input_get_sync(int fd);
功能:查询环形缓冲区中一帧完整事件(以 SYN_REPORT 为分隔)的事件数量。
参数:fd — 文件描述符
返回值:>0 可读事件数,0 无数据,-1 参数错误
sunxi_input_readb()阻塞直到收到一帧SYN_REPORT才返回;返回-1为 fd 错误或信号量等待失败,>0为读取字节数。接口真实可用,但因阻塞语义与事件驱动模型相悖,通常不推荐使用。
使用示例
以下示例均来自实际源码中的测试代码。
非阻塞读取(GT911/TLSC6X 测试代码)
参考自 lichee/rtos/drivers/drv/input/tp/gt911/gt911.c 和 tlsc6x/tlsc6x.c 中的 tp_test_task():
#include <stdio.h>
#include <string.h>
#include <FreeRTOS.h>
#include "sunxi-input.h"
#include "input-event-codes.h"
void tp_test_task(void *parm)
{
int fd;
int x = -1, y = -1;
struct sunxi_input_event event;
memset(&event, 0, sizeof(struct sunxi_input_event));
/* 1. 打开触摸屏设备 */
fd = sunxi_input_open("touchscreen");
if (fd < 0) {
printf("touchscreen open err\n");
vTaskDelete(NULL);
}
while (1) {
/* 2. 非阻塞读取事件 */
sunxi_input_read(fd, &event, sizeof(struct sunxi_input_event));
if (event.type == EV_ABS) {
switch (event.code) {
case ABS_MT_POSITION_X:
x = event.value;
break;
case ABS_MT_POSITION_Y:
y = event.value;
break;
case ABS_MT_TRACKING_ID:
break;
}
} else if (event.type == EV_KEY) {
if (event.code == BTN_TOUCH) {
if (event.value == 0)
printf("event.code == BTN_TOUCH, value = %d\n", event.value);
}
}
if (x >= 0 && y >= 0) {
printf("====press (%d, %d)====\n", x, y);
x = -1;
y = -1;
}
}
vTaskDelete(NULL);
}
批量读取 + 同步事件(CTP 测试代码)
参考自 lichee/rtos/drivers/drv/input/tp/ctp/sunxi_ctp.c 中的 ctp_test_task()。该模式先通过 sunxi_input_get_sync() 查询可读事件数量,再循环读取,最后通过 EV_SYN/SYN_REPORT 判断一帧数据是否完整:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <FreeRTOS.h>
#include <hal_osal.h>
#include "sunxi-input.h"
#include "input-event-codes.h"
void ctp_test_task(void *parm)
{
int touch_fd;
uint16_t touch_x, touch_y, touch_id;
struct sunxi_input_event event;
bool touch_down = false;
int count = 0, pressure = 0;
memset(&event, 0, sizeof(struct sunxi_input_event));
/* 1. 打开触摸屏设备 */
touch_fd = sunxi_input_open("touchscreen");
if (touch_fd < 0) {
printf("touchscreen open err\n");
hal_thread_stop(NULL);
}
printf("CTP test started, waiting for touch events...\n");
while (1) {
vTaskDelay(10);
touch_down = false;
/* 2. 查询可读事件数量 */
count = sunxi_input_get_sync(touch_fd);
if (count > 0) {
/* 3. 逐个读取事件 */
for (int i = 0; i < count; i++) {
sunxi_input_read(touch_fd, &event, sizeof(struct sunxi_input_event));
if (event.type == EV_ABS) {
switch (event.code) {
case ABS_MT_POSITION_X:
touch_x = event.value;
break;
case ABS_MT_POSITION_Y:
touch_y = event.value;
break;
case ABS_MT_TRACKING_ID:
touch_id = event.value;
break;
case ABS_MT_PRESSURE:
pressure = event.value;
break;
}
} else if (event.type == EV_SYN && event.code == SYN_REPORT) {
/* 4. SYN_REPORT 标志一帧触摸数据的结束 */
if (pressure > 0) {
printf("lv_indev: x:%d, y:%d\n", touch_x, touch_y);
touch_down = true;
}
}
}
}
if (touch_down) {
printf("tp_test: x:%d, y:%d, id:%d\n", touch_x, touch_y, touch_id);
}
}
hal_thread_stop(NULL);
}
以上示例直接取自源码中的测试函数。sunxi_input_read 为非阻塞读取,sunxi_input_get_sync 用于批量获取可读事件数。sunxi_input_readb 虽然提供了阻塞读取接口,但实际没有使用场景,不推荐使用。
驱动内部接口
以下接口在编写 TP 驱动时使用,用于创建和注册输入设备。普通应用开发者无需关注此章节。
sunxi_input_allocate_device
struct sunxi_input_dev *sunxi_input_allocate_device(void);
功能:从堆中分配 sunxi_input_dev 结构体并清零。
返回值:非空指针成功,NULL 内存不足
sunxi_input_register_device
int sunxi_input_register_device(struct sunxi_input_dev *dev);
功能:将设备加入全局设备链表。内部自动设置 EV_SYN 能力。
参数:dev — 已设置 name 和能力的输入设备
返回值:0 成功,-1 设备名称为空
input_set_capability
void input_set_capability(struct sunxi_input_dev *dev, unsigned int type, unsigned int code);
功能:设置输入设备支持的事件类型和编码。内部使用位图记录,用于后续 sunxi_input_event() 上报时的合法性检查。
参数:
dev— 输入设备指针type— 事件类型,支持EV_KEY、EV_REL、EV_ABS、EV_MSCcode— 事件编码,如ABS_MT_POSITION_X(坐标)、BTN_TOUCH(触摸按键)
sunxi_input_event
void sunxi_input_event(struct sunxi_input_dev *dev,
unsigned int type, unsigned int code, unsigned int value);
功能:驱动层上报输入事件。内部先检查该事件类型是否已通过 input_set_capability() 注册,验证通过后写入所有匹配的 evdev 环形缓冲区,并在 SYN_REPORT 时发送信号量唤醒读端。
参数:
dev— 输入设备指针type— 事件类型code— 事件编码value— 事件值
驱动初始化完整示例
以 lichee/rtos/drivers/drv/input/tp/tlsc6x/tlsc6x.c 的 tlsc6x_init() 为例,展示驱动内部接口的实际用法:
struct sunxi_input_dev *input_dev;
/* 1. 分配输入设备 */
input_dev = sunxi_input_allocate_device();
if (NULL == input_dev) {
tlsc_err("input dev alloc err\n");
goto err;
}
/* 2. 设置设备名称 */
input_dev->name = "touchscreen";
/* 3. 设置设备能力(支持的事件类型和编码) */
input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X); // 支持绝对坐标 X
input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y); // 支持绝对坐标 Y
input_set_capability(input_dev, EV_ABS, ABS_MT_TOUCH_MAJOR); // 支持触摸面积
input_set_capability(input_dev, EV_ABS, ABS_MT_TRACKING_ID); // 支持追踪 ID
input_set_capability(input_dev, EV_KEY, BTN_TOUCH); // 支持触摸按键
input_set_capability(input_dev, EV_KEY, KEY_MENU); // 支持菜单键
input_set_capability(input_dev, EV_KEY, KEY_HOMEPAGE); // 支持主页键
input_set_capability(input_dev, EV_KEY, KEY_BACK); // 支持返回键
/* 4. 注册到 Input 子系统 */
sunxi_input_register_device(input_dev);
驱动内部通常封装以下 static inline 辅助函数简化事件上报调用(参考 gt911.c、tlsc6x.c、sunxi_ctp.c、gpadc-key.c):
static inline void input_report_key_t(struct sunxi_input_dev *dev, unsigned int code, int value)
{
sunxi_input_event(dev, EV_KEY, code, !!value);
}
static inline void input_report_abs_t(struct sunxi_input_dev *dev, unsigned int code, int value)
{
sunxi_input_event(dev, EV_ABS, code, value);
}
static inline void input_sync_t(struct sunxi_input_dev *dev)
{
sunxi_input_event(dev, EV_SYN, SYN_REPORT, 0);
}
static inline void input_mt_sync_t(struct sunxi_input_dev *dev)
{
sunxi_input_event(dev, EV_SYN, SYN_MT_REPORT, 0);
}
驱动开发示例
以 ctp/tpdev/ft5446.c 为例,展示编写一个 CTP 驱动需要实现的接口。
第一步:定义驱动结构体
static sunxi_ctp_hw_t ft5446_dev; /* 设备硬件信息(I2C地址、TWI端口、GPIO等) */
第二步:实现 bind 函数
bind 函数保存从 sysconfig 传入的硬件参数:
static void ft5446_bind(sunxi_ctp_hw_t *dev)
{
ft5446_dev.i2c_addr = FT5446_I2C_ADDR;
ft5446_dev.twi_port = dev->twi_port;
ft5446_dev.reset_gpio = dev->reset_gpio;
ft5446_dev.interrupt_gpio = dev->interrupt_gpio;
}
第三步:实现 reset 函数
不同触摸 IC 的复位时序和要求差异较大,必须参考对应芯片手册中的 Power-On Sequence / Reset Timing 章节。以下为 FT5446 的复位流程,仅作参考。
复位顺序:RST 拉低 → INT 拉低 → 延时 10 ms → RST 拉高 → 延时 40 ms → INT 设为输入 → 读芯片 ID 验证 → 配置工作模式/触摸阈值:
static int ft5446_reset(void)
{
/* RST 和 INT 均拉低 */
hal_gpio_set_direction(ft5446_dev.reset_gpio, GPIO_DIRECTION_OUTPUT);
hal_gpio_set_data(ft5446_dev.reset_gpio, 0);
hal_gpio_set_direction(ft5446_dev.interrupt_gpio, GPIO_DIRECTION_OUTPUT);
hal_gpio_set_data(ft5446_dev.interrupt_gpio, 0);
vTaskDelay(pdMS_TO_TICKS(10));
/* RST 拉高,等待芯片初始化 */
hal_gpio_set_data(ft5446_dev.reset_gpio, 1);
vTaskDelay(pdMS_TO_TICKS(40));
/* INT 设为输入,接收触摸中断 */
hal_gpio_set_direction(ft5446_dev.interrupt_gpio, GPIO_DIRECTION_INPUT);
/* 读取芯片 ID 验证设备(FT5446 兼容 0x54 / 0x58) */
uint8_t chip_id[1] = {0};
sunxi_ctp_regmap_read(&ft5446_dev, CTP_REGLEN_1, FT5446_CHIP_ID_REG, chip_id);
if (chip_id[0] != 0x54 && chip_id[0] != 0x58)
return EPDK_FAIL;
/* 设置工作模式和触摸阈值(完整实现还会写入 FW 版本、ID_G 模式、周期等寄存器,此处从简) */
sunxi_ctp_regmap_write(&ft5446_dev, CTP_REGLEN_1, FT5446_DEVICE_MODE_REG, 0);
sunxi_ctp_regmap_write(&ft5446_dev, CTP_REGLEN_1, FT5446_TOUCH_THRESHOLD_REG, 0x16);
return EPDK_OK;
}
第四步:实现数据检查和读取
/* 检查是否有触摸数据就绪 */
static int ft5446_check_data_ready(void)
{
uint8_t status;
sunxi_ctp_regmap_read(&ft5446_dev, CTP_REGLEN_1,
FT5446_TD_STATUS_REG, &status);
return (status & 0x0F) ? 1 : 0;
}
/* 读取触摸数据 */
static void ft5446_read_touch_data(struct sunxi_ctp_touch_data_t *touch_data_t)
{
__u8 touch_data[8] = {0};
touch_data_t->status = CTP_EVENT_NONE;
touch_data_t->x = 0;
touch_data_t->y = 0;
touch_data_t->pressure = 0;
touch_data_t->id = 0;
iic_read_bytes_end_cmd(ft5446_dev.twi_port, ft5446_dev.i2c_addr,
FT_TP1_REG, CTP_REGLEN_1, touch_data, 4);
touch_data_t->x = ((touch_data[0] & 0x0F) << 8) | touch_data[1];
touch_data_t->y = ((touch_data[2] & 0x0F) << 8) | touch_data[3];
touch_data_t->pressure = 0xFF;
touch_data_t->status = CTP_EVENT_PRESS_DOWN;
}
ft5446_clear_interrupt() 为空实现(直接返回成功),其中断清除依赖 check_data_ready() 读取 TD 状态寄存器完成;各芯片实现可不同(如 GT928 需显式写数据状态寄存器清除)。
第五步:注册驱动
static struct sunxi_ctp_driver ft5446_driver = {
.name = "sunxi_ft5446", /* 须与 sysconfig 中 ctp_driver 一致 */
.reset = ft5446_reset,
.bind = ft5446_bind,
.read_touch_data = ft5446_read_touch_data,
.clear_interrupt = ft5446_clear_interrupt,
.check_data_ready = ft5446_check_data_ready,
};
void sunxi_ctp_driver_ft5446_register(void)
{
sunxi_ctp_driver_register(&ft5446_driver);
}
第六步:中断注册与事件上报线程
/* CTP 中断注册(参照 sunxi_ctp.c 的 ctp_io_cfg) */
hal_gpio_set_pull(interrupt_gpio, GPIO_PULL_UP);
hal_gpio_to_irq(interrupt_gpio, &irq_num);
hal_gpio_irq_request(irq_num, ctp_interrupt_callback, IRQ_TYPE_EDGE_BOTH, ctp);
hal_gpio_irq_enable(irq_num);
/* 事件上报线程(参照 ctp_task):阻塞等中断信号量 → 读数据 → 上报 → 清中断 */
说明:中断回调只 post 信号量;事件上报在独立线程中完成(阻塞等待 → 读触摸数据 → 坐标变换 → input_report_abs() 上报 → clear_interrupt())。RTP 不走 GPIO 中断,走 TPADC 中断回调。
完整源码见
lichee/rtos/drivers/drv/input/tp/ctp/tpdev/ft5446.c。
常见问题
RTP 电阻式触摸屏
Q:RTP 触摸屏无反应,触摸后没有事件输出?
- 现象:触摸触摸屏时,应用程序收不到触摸事件数据。
- 原因:RTP 驱动未正确初始化,或 sysconfig 中
rtp_used未设置为 1。 - 解决办法:检查 defconfig 中
CONFIG_DRIVERS_TOUCHSCREEN_RTP=y是否使能;检查 sysconfig 中[rtp_para]段rtp_used = 1是否配置。
Q:RTP 触摸坐标与实际位置偏差较大?
- 现象:触摸位置与屏幕显示位置明显偏离。
- 原因:电阻式触摸屏需要校准,校准数据未生成或丢失。
- 解决办法:
- 检查 sysconfig 中
rtp_adjust_cfg_path指定的校准文件路径(如"/data/tp.cfg")是否存在 - 重新执行触摸屏校准流程生成校准数据
- 确保
rtp_screen_max_x/rtp_screen_max_y与 LCD 分辨率一致
- 检查 sysconfig 中
CTP 电容式触摸屏
Q:CTP 触摸屏 TWI 通信失败?
- 现象:CTP 驱动初始化时 TWI 读写报错,无法检测触摸控制器。
- 原因:TWI 总线未初始化、复位时序不正确。
- 解决办法:
- 检查 sysconfig 中
ctp_twi_id指定的 TWI 总线编号与实际硬件连接一致 - 确认 CTP 芯片的 TWI 地址与驱动中定义一致
- 检查 sysconfig 中
Q:CTP 触摸屏中断未触发?
- 现象:CTP 驱动初始化完成但无法响应触摸操作,中断函数未被调用。
- 原因:中断 GPIO 引脚配置错误,或中断触发方式不匹配。
- 解决办法:检查 sysconfig 中
ctp_int_port的引脚配置是否正确;确认 GPIO 复用功能是否设置正确(<1>表示 GPIO 功能)。
Q:CTP 触摸坐标偏移或方向不对?
- 现象:触摸位置与屏幕显示位置不一致,X/Y 轴反向或交换。
- 原因:坐标映射配置不正确。
- 解决办法:
- X/Y 轴反向:调整
ctp_reverse_x/ctp_reverse_y - X/Y 轴交换:调整
ctp_modify_reg - 坐标范围:确保
ctp_screen_max_x/ctp_screen_max_y与 LCD 分辨率匹配
- X/Y 轴反向:调整
通用问题
Q:触摸屏多点触摸时坐标跳变或丢失?
- 现象:多点触摸时部分触摸点坐标异常或某些触点未被检测到。
- 原因:触摸屏报点率不足或驱动中最大触摸点数限制。
- 解决办法:
- 检查驱动中
MAX_FINGERS等宏定义的最大触摸点数 - 确认 TWI 通信速率是否足够
- 检查硬件电源和干扰情况
- 检查驱动中
附录
输入事件结构体
struct sunxi_input_event {
unsigned int type; // 事件类型
unsigned int code; // 事件编码
unsigned int value; // 事件值
};
触摸屏设备名称
所有 TP 驱动统一使用设备名称 "touchscreen",通过 sunxi_input_open("touchscreen") 打开设备。
相关文件
| 文件 | 路径 |
|---|---|
| Input 子系统头文件 | lichee/rtos/drivers/drv/input/sunxi-input.h |
| Input 子系统实现 | lichee/rtos/drivers/drv/input/sunxi-input.c |
| 事件编码定义 | lichee/rtos/drivers/drv/input/input-event-codes.h |
| CTP 框架实现 | lichee/rtos/drivers/drv/input/tp/ctp/sunxi_ctp.c |
| CTP 设备管理 | lichee/rtos/drivers/drv/input/tp/ctp/sunxi_ctp_dev.c |
| FT5446 驱动 | lichee/rtos/drivers/drv/input/tp/ctp/tpdev/ft5446.c |
| GT928 驱动 | lichee/rtos/drivers/drv/input/tp/ctp/tpdev/gt928.c |
| RTP 驱动 | lichee/rtos/drivers/drv/input/tp/rtp/drv_rtp.c |
| TPADC RTP 驱动 | lichee/rtos/drivers/drv/input/tp/rtp/sunxi_tpadc_rtp.c |
RTP 电阻式触摸屏
- 通信接口:TPADC(片内 ADC 模块,无需外部通信总线)
- 驱动路径:
lichee/rtos/drivers/drv/input/tp/rtp/ - 初始化函数:
sunxi_tpadc_rtp_init() - 依赖 HAL:
CONFIG_DRIVERS_TPADC=y(依赖 TPADC 模块) - sysconfig:
[rtp_para]段 - 特点:无需外部触摸 IC,通过 TPADC 采集触摸电压,需校准
CTP 电容式触摸屏
- 通信接口:TWI
- 框架路径:
lichee/rtos/drivers/drv/input/tp/ctp/ - 初始化函数:
sunxi_ctp_init() - 支持芯片:FT5446(
sunxi_ft5446)、GT928(sunxi_gt928) - sysconfig:
[ctp_para]段 - 特点:统一框架管理,支持驱动动态注册,配置灵活
TP 驱动初始化调用链
sunxi_input_init()
├── sunxi_tpadc_rtp_init() // CONFIG_DRIVERS_TOUCHSCREEN_RTP=y (RTP)
└── sunxi_ctp_init() // CONFIG_DRIVERS_TOUCHSCREEN_CTP=y (CTP)