TP 开发指导_Rev1.0
修订记录
版本 |
日期 |
作者 |
修订内容 |
|---|---|---|---|
Rev1.0 |
26-06-02 |
ZXQ |
创建 TP 开发指导文档 |
1 简介
1.1 文档简介
本文档介绍 LTE-EC71X TP(Touch Panel,触摸屏)接口 API 情况,API 接口位于:
components/kernel/lierda_api/liot_tp/liot_tp.h 文件声明。
TP 驱动框架支持通过 I2C 或 SPI 接口连接触摸屏控制器,向应用层提供统一的初始化、触摸点读取、手势读取、中断回调、休眠唤醒、复位、固件升级和 IC 信息读取接口。
当前 SDK 示例支持 AXS5106 和 CST816D 触摸屏控制器,驱动文件位于:
components/driver/tp/axs5106/axs5106/liot_tpDev_AXS5106.c
components/driver/tp/axs5106/cst816d/liot_tpDev_CST816D.c
示例代码位于:
examples/demo/src/demo_tp.c 文件。
不同模组和硬件板卡的 TP 接口引脚、I2C 通道、I2C 地址、RST 引脚、INT 引脚需以实际硬件原理图和对应《引脚复用表》为准。
1.2 TP 原理简介
TP 通常由触摸屏控制 IC、触摸传感器、通信总线、复位引脚和中断引脚组成。触摸屏控制 IC 负责采集触摸坐标、触摸事件和手势信息,主控通过 I2C 或 SPI 读取控制 IC 的寄存器数据。
典型 TP 硬件连接如下:
I2C/SPI 总线:用于读写 TP 控制 IC 寄存器。
RST:复位 TP 控制 IC,可用于初始化、唤醒或异常恢复。
INT:触摸事件中断信号,触摸发生时由 TP 控制 IC 拉动中断引脚通知主控。
电源:需保证 TP 控制 IC 上电时序满足屏厂或 IC 厂要求。
TP 框架主要功能特性:
统一设备抽象:不同 TP 芯片通过
liot_tp_sensor_t注册到统一框架。I2C/SPI 自适应寄存器访问:驱动可通过
liot_tp_reg_read()和liot_tp_reg_write()访问寄存器。触摸点读取:支持读取触摸点数量、坐标、事件、权重和时间戳。
手势读取:支持上滑、下滑、左滑、右滑、缩放、长按等手势类型。
中断回调:支持注册触摸和手势回调,启动 INT 轮询后自动读取数据。
低功耗控制:支持睡眠、唤醒和工作模式切换。
固件升级:支持通过芯片驱动执行固件升级或自动升级。
2 API 函数概览
函数 |
说明 |
|---|---|
|
初始化 TP 并返回 TP 句柄 |
|
反初始化 TP |
|
读取触摸点数据 |
|
读取手势数据 |
|
设置触摸阈值 |
|
使 TP 进入睡眠 |
|
唤醒 TP |
|
TP 软复位 |
|
通过 RST 引脚执行硬件复位 |
|
写 TP 寄存器,I2C/SPI 自适应 |
|
读 TP 寄存器,I2C/SPI 自适应 |
|
写 TP 单字节寄存器 |
|
读 TP 单字节寄存器 |
|
对 TP 寄存器执行位写操作 |
|
获取 TP 事件组句柄 |
|
注册 TP 中断触摸/手势回调 |
|
启动或停止 INT 事件轮询 |
|
执行 TP 固件升级 |
|
设置 TP 工作模式 |
|
获取 TP IC 信息 |
|
设置 CST8xxT 固件升级所需 I2C 通道和 RST 引脚 |
|
设置 CST816D 固件升级所需 I2C 通道和 RST 引脚 |
|
使用内置固件自动升级 CST816D |
|
使用内置固件自动升级 AXS5106 |
3 类型说明
3.1 liot_tp_handle_t
TP 句柄类型定义如下:
定义
typedef void *liot_tp_handle_t;
参数
liot_tp_handle_t:TP 设备句柄,由liot_tp_init()返回,其他 TP API 调用时传入。
3.2 liot_errcode_tp_e
TP API 执行结果错误码。
定义
typedef enum {
LIOT_TP_SUCCESS = LIOT_SUCCESS,
LIOT_TP_ERR_INIT = (0x10) | LIOT_COMPONENT_STATE_INFO,
LIOT_TP_ERR_BUS,
LIOT_TP_ERR_PARAM,
LIOT_TP_ERR_HANDLE,
LIOT_TP_ERR_TIMEOUT,
LIOT_TP_ERR_NOT_SUPPORTED,
} liot_errcode_tp_e;
参数
LIOT_TP_SUCCESS:函数执行成功。LIOT_TP_ERR_INIT:初始化失败。LIOT_TP_ERR_BUS:总线访问失败。LIOT_TP_ERR_PARAM:参数错误。LIOT_TP_ERR_HANDLE:句柄错误。LIOT_TP_ERR_TIMEOUT:操作超时。LIOT_TP_ERR_NOT_SUPPORTED:当前设备或驱动不支持该操作。
3.3 liot_tp_event_e
触摸事件类型枚举定义如下:
定义
typedef enum {
LIOT_TP_EVT_DOWN = 0,
LIOT_TP_EVT_UP,
LIOT_TP_EVT_MOVE,
LIOT_TP_EVT_NONE,
} liot_tp_event_e;
参数
LIOT_TP_EVT_DOWN:按下事件。LIOT_TP_EVT_UP:抬起事件。LIOT_TP_EVT_MOVE:移动事件。LIOT_TP_EVT_NONE:无触摸事件。
3.4 liot_tp_gesture_e
手势类型枚举定义如下:
定义
typedef enum {
LIOT_TP_GESTURE_NONE = 0,
LIOT_TP_GESTURE_UP,
LIOT_TP_GESTURE_DOWN,
LIOT_TP_GESTURE_LEFT,
LIOT_TP_GESTURE_RIGHT,
LIOT_TP_GESTURE_ZOOM_IN,
LIOT_TP_GESTURE_ZOOM_OUT,
LIOT_TP_GESTURE_LONG_PRESS,
} liot_tp_gesture_e;
参数
LIOT_TP_GESTURE_NONE:无手势。LIOT_TP_GESTURE_UP:上滑。LIOT_TP_GESTURE_DOWN:下滑。LIOT_TP_GESTURE_LEFT:左滑。LIOT_TP_GESTURE_RIGHT:右滑。LIOT_TP_GESTURE_ZOOM_IN:放大。LIOT_TP_GESTURE_ZOOM_OUT:缩小。LIOT_TP_GESTURE_LONG_PRESS:长按。
3.5 liot_tp_interface_e
TP 通信接口类型枚举定义如下:
定义
typedef enum {
LIOT_TP_IF_I2C = 0,
LIOT_TP_IF_SPI,
} liot_tp_interface_e;
参数
LIOT_TP_IF_I2C:TP 使用 I2C 接口通信。LIOT_TP_IF_SPI:TP 使用 SPI 接口通信。
3.6 liot_tp_point_t
触摸点数据结构体定义如下:
定义
typedef struct {
uint8_t id;
liot_tp_event_e event;
uint16_t x;
uint16_t y;
uint8_t weight;
uint32_t timestamp_ms;
} liot_tp_point_t;
参数
类型 |
参数 |
描述 |
|---|---|---|
uint8_t |
id |
触摸点 ID |
liot_tp_event_e |
event |
触摸事件类型 |
uint16_t |
x |
X 坐标 |
uint16_t |
y |
Y 坐标 |
uint8_t |
weight |
触摸权重或压力值 |
uint32_t |
timestamp_ms |
时间戳,单位:ms |
3.7 liot_tp_touch_data_t
触摸数据结构体定义如下:
定义
typedef struct {
uint8_t touch_cnt;
liot_tp_point_t point[5];
} liot_tp_touch_data_t;
参数
类型 |
参数 |
描述 |
|---|---|---|
uint8_t |
touch_cnt |
当前有效触摸点数量 |
liot_tp_point_t[5] |
point |
触摸点数组,最多 5 点 |
3.8 liot_tp_gesture_data_t
手势数据结构体定义如下:
定义
typedef struct {
liot_tp_gesture_e gesture;
uint32_t timestamp_ms;
} liot_tp_gesture_data_t;
参数
类型 |
参数 |
描述 |
|---|---|---|
liot_tp_gesture_e |
gesture |
手势类型 |
uint32_t |
timestamp_ms |
时间戳,单位:ms |
3.9 liot_tp_work_mode_e
TP 工作模式枚举定义如下:
定义
typedef enum {
LIOT_TP_MODE_NORMAL = 0,
LIOT_TP_MODE_GESTURE,
LIOT_TP_MODE_LOW_POWER,
LIOT_TP_MODE_DEEP_SLEEP,
LIOT_TP_MODE_FACTORY_TEST,
} liot_tp_work_mode_e;
参数
LIOT_TP_MODE_NORMAL:正常工作模式。LIOT_TP_MODE_GESTURE:手势模式。LIOT_TP_MODE_LOW_POWER:低功耗模式。LIOT_TP_MODE_DEEP_SLEEP:深睡眠模式。LIOT_TP_MODE_FACTORY_TEST:工厂测试模式。
3.10 liot_tp_sensor_func_t
TP 传感器驱动函数表定义如下:
定义
typedef struct {
int (*init)(liot_tp_handle_t handle);
int (*deinit)(liot_tp_handle_t handle);
int (*read_touch)(liot_tp_handle_t handle, liot_tp_touch_data_t *data);
int (*read_gesture)(liot_tp_handle_t handle, liot_tp_gesture_data_t *data);
int (*set_threshold)(liot_tp_handle_t handle, uint8_t threshold);
int (*enter_sleep)(liot_tp_handle_t handle);
int (*wakeup)(liot_tp_handle_t handle);
int (*reset)(liot_tp_handle_t handle);
int (*exit_sleep)(liot_tp_handle_t handle);
int (*update_firmware)(liot_tp_handle_t handle, const uint8_t *fw_data, uint32_t fw_len);
int (*update_firmware_auto)(liot_tp_handle_t handle);
int (*set_work_mode)(liot_tp_handle_t handle, liot_tp_work_mode_e mode);
int (*get_ic_info)(liot_tp_handle_t handle, uint8_t *buf, uint16_t buf_len);
} liot_tp_sensor_func_t;
参数
参数 |
描述 |
|---|---|
init |
芯片驱动初始化 |
deinit |
芯片驱动反初始化 |
read_touch |
读取触摸点数据 |
read_gesture |
读取手势数据 |
set_threshold |
设置触摸阈值 |
enter_sleep |
进入睡眠 |
wakeup |
唤醒 |
reset |
软复位 |
exit_sleep |
退出睡眠 |
update_firmware |
使用指定固件数据升级 |
update_firmware_auto |
使用内置固件自动升级 |
set_work_mode |
设置工作模式 |
get_ic_info |
获取 IC 信息 |
3.11 liot_tp_sensor_t
TP 传感器设备定义如下:
定义
typedef struct {
uint8_t chip_id;
uint8_t max_points;
uint16_t width;
uint16_t height;
bool gesture_support;
liot_tp_sensor_func_t func;
} liot_tp_sensor_t;
参数
类型 |
参数 |
描述 |
|---|---|---|
uint8_t |
chip_id |
触摸 IC 芯片 ID |
uint8_t |
max_points |
最大触摸点数 |
uint16_t |
width |
触摸屏宽度 |
uint16_t |
height |
触摸屏高度 |
bool |
gesture_support |
是否支持手势 |
liot_tp_sensor_func_t |
func |
芯片驱动函数表 |
3.12 liot_tp_i2c_config_t
TP I2C 接口配置结构体定义如下:
定义
typedef struct {
liot_i2c_channel_e num;
int8_t sda;
int8_t scl;
uint8_t addr;
uint8_t scl_func;
uint8_t sda_func;
} liot_tp_i2c_config_t;
参数
类型 |
参数 |
描述 |
|---|---|---|
liot_i2c_channel_e |
num |
I2C 通道号 |
int8_t |
sda |
SDA 引脚号 |
int8_t |
scl |
SCL 引脚号 |
uint8_t |
addr |
TP I2C 7bit 从机地址 |
uint8_t |
scl_func |
SCL 引脚复用功能,0 表示使用默认 |
uint8_t |
sda_func |
SDA 引脚复用功能,0 表示使用默认 |
3.13 liot_tp_spi_config_t
TP SPI 接口配置结构体定义如下:
定义
typedef struct {
liot_spi_port_e num;
liot_spi_cpol_pol_e cpol;
liot_spi_cpha_pol_e cpha;
liot_spi_clk_e speed;
} liot_tp_spi_config_t;
参数
类型 |
参数 |
描述 |
|---|---|---|
liot_spi_port_e |
num |
SPI 总线编号 |
liot_spi_cpol_pol_e |
cpol |
SPI 时钟极性 |
liot_spi_cpha_pol_e |
cpha |
SPI 时钟相位 |
liot_spi_clk_e |
speed |
SPI 时钟频率 |
3.14 liot_tp_rst_config_t
TP 复位引脚配置结构体定义如下:
定义
typedef struct {
int8_t pin;
uint16_t delay_ms;
bool active_low;
} liot_tp_rst_config_t;
参数
类型 |
参数 |
描述 |
|---|---|---|
int8_t |
pin |
RST 引脚号,小于 0 表示不配置 |
uint16_t |
delay_ms |
复位后延时,单位:ms |
bool |
active_low |
是否低电平复位 |
3.15 liot_tp_int_config_t
TP 中断引脚配置结构体定义如下:
定义
typedef struct {
int8_t pin;
liot_intsig_e signal;
liot_gpio_pull_mode_e pull;
} liot_tp_int_config_t;
参数
类型 |
参数 |
描述 |
|---|---|---|
int8_t |
pin |
INT 引脚号,小于 0 表示不配置 |
liot_intsig_e |
signal |
中断触发方式,如 |
liot_gpio_pull_mode_e |
pull |
GPIO 上下拉配置,如 |
3.16 liot_tp_config_t
TP 完整配置结构体定义如下:
定义
typedef struct {
liot_tp_interface_e interface_type;
union {
liot_tp_i2c_config_t i2c;
liot_tp_spi_config_t spi;
};
liot_tp_rst_config_t rst;
liot_tp_int_config_t int_pin;
liot_tp_sensor_t *sensor;
bool fw_auto_update;
} liot_tp_config_t;
参数
类型 |
参数 |
描述 |
|---|---|---|
liot_tp_interface_e |
interface_type |
TP 通信接口类型 |
liot_tp_i2c_config_t |
i2c |
I2C 接口配置, |
liot_tp_spi_config_t |
spi |
SPI 接口配置, |
liot_tp_rst_config_t |
rst |
RST 复位引脚配置 |
liot_tp_int_config_t |
int_pin |
INT 中断引脚配置 |
liot_tp_sensor_t * |
sensor |
TP 芯片驱动对象 |
bool |
fw_auto_update |
是否在初始化时自动升级固件,true 表示自动升级,false 表示跳过 |
4 API 函数详解
4.1 liot_tp_init
该函数用于初始化 TP,应在使用 TP 其他 API 前调用。调用前需根据实际硬件配置 I2C/SPI、RST、INT 引脚和 TP 芯片驱动对象。
声明
liot_tp_handle_t liot_tp_init(liot_tp_config_t *config);
参数
config:[In] TP 配置参数。
返回值
liot_tp_handle_t:TP 句柄。NULL:初始化失败。
4.2 liot_tp_deinit
该函数用于反初始化 TP。
声明
liot_errcode_tp_e liot_tp_deinit(liot_tp_handle_t handle);
参数
handle:[In] TP 句柄。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.3 liot_tp_read_touch
该函数用于读取 TP 触摸点数据。
声明
liot_errcode_tp_e liot_tp_read_touch(liot_tp_handle_t handle,
liot_tp_touch_data_t *data);
参数
handle:[In] TP 句柄。
data:[Out] 触摸数据输出缓冲区。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.4 liot_tp_read_gesture
该函数用于读取 TP 手势数据。
声明
liot_errcode_tp_e liot_tp_read_gesture(liot_tp_handle_t handle,
liot_tp_gesture_data_t *data);
参数
handle:[In] TP 句柄。
data:[Out] 手势数据输出缓冲区。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.5 liot_tp_set_threshold
该函数用于设置 TP 触摸阈值,阈值含义由具体 TP 芯片驱动决定。部分 TP 驱动可能不支持直接设置阈值。
声明
liot_errcode_tp_e liot_tp_set_threshold(liot_tp_handle_t handle,
uint8_t threshold);
参数
handle:[In] TP 句柄。
threshold:[In] 触摸阈值,通常值越小越灵敏。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.6 liot_tp_sleep
该函数用于使 TP 进入睡眠。
声明
liot_errcode_tp_e liot_tp_sleep(liot_tp_handle_t handle);
参数
handle:[In] TP 句柄。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.7 liot_tp_wakeup
该函数用于唤醒 TP。
声明
liot_errcode_tp_e liot_tp_wakeup(liot_tp_handle_t handle);
参数
handle:[In] TP 句柄。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.8 liot_tp_reset
该函数用于执行 TP 软复位。
声明
liot_errcode_tp_e liot_tp_reset(liot_tp_handle_t handle);
参数
handle:[In] TP 句柄。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.9 liot_tp_hard_reset
该函数用于通过 RST 引脚执行 TP 硬件复位。
声明
liot_errcode_tp_e liot_tp_hard_reset(liot_tp_handle_t handle);
参数
handle:[In] TP 句柄。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
注意:使用该函数前,需在 liot_tp_config_t 中正确配置 rst.pin、rst.delay_ms 和 rst.active_low。
4.10 liot_tp_reg_write
该函数用于写 TP 寄存器,接口类型根据初始化配置自动选择 I2C 或 SPI。
声明
int liot_tp_reg_write(liot_tp_handle_t handle, uint8_t reg,
const uint8_t *data, uint16_t len);
参数
handle:[In] TP 句柄。
reg:[In] 寄存器地址。
data:[In] 待写入数据缓冲区。
len:[In] 待写入数据长度。单位:字节。
返回值
0:写入成功。-1:写入失败。
4.11 liot_tp_reg_read
该函数用于读 TP 寄存器,接口类型根据初始化配置自动选择 I2C 或 SPI。
声明
int liot_tp_reg_read(liot_tp_handle_t handle, uint8_t reg,
uint8_t *data, uint16_t len);
参数
handle:[In] TP 句柄。
reg:[In] 寄存器地址。
data:[Out] 读取数据输出缓冲区。
len:[In] 读取数据长度。单位:字节。
返回值
0:读取成功。-1:读取失败。
4.12 liot_tp_reg_write_byte
该函数用于写 TP 单字节寄存器。
声明
liot_errcode_tp_e liot_tp_reg_write_byte(liot_tp_handle_t handle,
uint8_t reg,
uint8_t value);
参数
handle:[In] TP 句柄。
reg:[In] 寄存器地址。
value:[In] 写入值。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.13 liot_tp_reg_read_byte
该函数用于读 TP 单字节寄存器。
声明
liot_errcode_tp_e liot_tp_reg_read_byte(liot_tp_handle_t handle,
uint8_t reg,
uint8_t *value);
参数
handle:[In] TP 句柄。
reg:[In] 寄存器地址。
value:[Out] 读取值输出指针。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.14 liot_tp_reg_write_bit
该函数用于对 TP 寄存器执行读改写位操作。
声明
liot_errcode_tp_e liot_tp_reg_write_bit(liot_tp_handle_t handle,
uint8_t reg,
uint8_t mask,
uint8_t value);
参数
handle:[In] TP 句柄。
reg:[In] 寄存器地址。
mask:[In] 位掩码。
value:[In] 写入值。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.15 liot_tp_get_event_flags
该函数用于获取 TP 事件组句柄,INT 中断触发时会置位对应事件位。
声明
osEventFlagsId_t liot_tp_get_event_flags(liot_tp_handle_t handle);
参数
handle:[In] TP 句柄。
返回值
osEventFlagsId_t:TP 事件组句柄。
4.16 liot_tp_register_int_callback
该函数用于注册 TP 中断回调函数。注册后,启动 INT 轮询时可在触摸或手势事件发生后自动回调。
声明
liot_errcode_tp_e liot_tp_register_int_callback(liot_tp_handle_t handle,
void (*touch_cb)(liot_tp_touch_data_t *data, void *ctx),
void (*gesture_cb)(liot_tp_gesture_data_t *data, void *ctx),
void *touch_ctx,
void *gesture_ctx);
参数
handle:[In] TP 句柄。
touch_cb:[In] 触摸事件回调函数,可为 NULL。
gesture_cb:[In] 手势事件回调函数,可为 NULL。
touch_ctx:[In] 触摸事件回调上下文。
gesture_ctx:[In] 手势事件回调上下文。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.17 liot_tp_enable_int
该函数用于启动或停止 INT 事件轮询。启动后,INT 中断会自动读取 TP 数据并通过回调或事件组通知应用层。
声明
liot_errcode_tp_e liot_tp_enable_int(liot_tp_handle_t handle, int enable);
参数
handle:[In] TP 句柄。
enable:[In] 使能标志,true 表示启动,false 表示停止。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.18 liot_tp_update_firmware
该函数用于执行 TP 固件升级。
声明
liot_errcode_tp_e liot_tp_update_firmware(liot_tp_handle_t handle,
const uint8_t *fw_data, uint32_t fw_len);
参数
handle:[In] TP 句柄。
fw_data:[In] 固件数据指针。
fw_len:[In] 固件数据长度。单位:字节。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.19 liot_tp_set_work_mode
该函数用于设置 TP 工作模式。
声明
liot_errcode_tp_e liot_tp_set_work_mode(liot_tp_handle_t handle,
liot_tp_work_mode_e mode);
参数
handle:[In] TP 句柄。
mode:[In] 工作模式,请参考 3.9。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.20 liot_tp_get_ic_info
该函数用于获取 TP IC 信息。输出数据格式由具体 TP 芯片驱动决定。
声明
liot_errcode_tp_e liot_tp_get_ic_info(liot_tp_handle_t handle,
uint8_t *buf, uint16_t buf_len);
参数
handle:[In] TP 句柄。
buf:[Out] IC 信息输出缓冲区。
buf_len:[In] 输出缓冲区长度。单位:字节。
返回值
liot_errcode_tp_e:执行结果码,请参考 3.2。
4.21 cst816d_set_i2c_config
该函数用于设置 CST816D 固件升级所需的 I2C 通道和 RST 引脚。CST816D 固件升级需要切换到 BOOT 地址 0x6A。
声明
void cst816d_set_i2c_config(liot_i2c_channel_e i2c_ch, int8_t rst_pin);
参数
i2c_ch:[In] I2C 通道号。
rst_pin:[In] RST 引脚号。
返回值
无。
4.22 cst816d_auto_update_firmware
该函数用于使用内置固件自动升级 CST816D。内置固件通过 liot_tp_cst816d_fw.h 选择。
声明
int cst816d_auto_update_firmware(liot_tp_handle_t handle);
参数
handle:[In] TP 句柄。
返回值
0:升级成功。-1:升级失败。
4.23 axs5106_auto_update_firmware
该函数用于使用内置固件自动升级 AXS5106。升级流程包含版本比对、CRC 校验、擦除 Flash、写入固件和 CRC 验证。
声明
int axs5106_auto_update_firmware(liot_tp_handle_t handle);
参数
handle:[In] TP 句柄。
返回值
0:升级成功。-1:升级失败。
5 代码示例
5.1 示例代码参考
examples/demo/src/demo_tp.c 文件。
以下示例展示 I2C 方式初始化 AXS5106 或 CST816D,并通过 INT 回调获取触摸和手势数据。
#include <stdio.h>
#include <string.h>
#include "cmsis_os2.h"
#include "liot_os.h"
#include "liot_tp.h"
#include "lierda_app_main.h"
#include "liot_sleep.h"
/* 传感器选择: 将要使用的设为1, 其余设为0 */
#define LIOT_TP_DEMO_TEST_AXS5106 1
#define LIOT_TP_DEMO_TEST_CST816D 0
#if LIOT_TP_DEMO_TEST_CST816D == 1
#define TP_I2C_NUM 1
#define TP_I2C_ADDR 0x15
#define TP_SDA_PIN 66
#define TP_SCL_PIN 67
#define TP_SCL_FUNC L_PIN_FUNC_2
#define TP_SDA_FUNC L_PIN_FUNC_2
#define TP_RST_PIN 20
#define TP_RST_DELAY_MS 100
#define TP_RST_ACTIVE_LOW 1
#define TP_INT_PIN 19
#define TP_FW_AUTO_UPDATE 1
#elif LIOT_TP_DEMO_TEST_AXS5106 == 1
#define TP_I2C_NUM 0
#define TP_I2C_ADDR 0x63
#define TP_SDA_PIN 62
#define TP_SCL_PIN 49
#define TP_SCL_FUNC L_PIN_FUNC_2
#define TP_SDA_FUNC L_PIN_FUNC_2
#define TP_RST_PIN 56
#define TP_RST_DELAY_MS 100
#define TP_RST_ACTIVE_LOW 1
#define TP_INT_PIN 55
#define TP_FW_AUTO_UPDATE 1
#endif
LIOT_ADD_TP_DEV(g_liot_tp_cst816d);
LIOT_ADD_TP_DEV(g_liot_tp_axs5106);
#if LIOT_TP_DEMO_TEST_CST816D == 1
#define TP_SENSOR g_liot_tp_cst816d
#elif LIOT_TP_DEMO_TEST_AXS5106 == 1
#define TP_SENSOR g_liot_tp_axs5106
#endif
static void tp_touch_callback(liot_tp_touch_data_t *data, void *ctx)
{
(void)ctx;
for (uint8_t i = 0; i < data->touch_cnt; i++) {
liot_trace("[TP] touch%d: evt=%d x=%d y=%d",
data->point[i].id,
data->point[i].event,
data->point[i].x,
data->point[i].y);
}
}
static void tp_gesture_callback(liot_tp_gesture_data_t *data, void *ctx)
{
(void)ctx;
liot_trace("[TP] gesture: %d @%lu",
data->gesture, (unsigned long)data->timestamp_ms);
}
void liot_tp_demo_thread(void *argv)
{
(void)argv;
osDelay(500);
Liot_AonPowerCtl(true);
Liot_SetVoltage(L_DOMAIN_ALL, L_VOLT_3_30V);
LiotSleepModeCfg_t mode_cfg = {LIOT_SLEEP_MODE_NORMAL};
Liot_SleepSetMode(&mode_cfg);
liot_tp_config_t tp_cfg = {
.interface_type = LIOT_TP_IF_I2C,
.i2c = {
.num = TP_I2C_NUM,
.sda = TP_SDA_PIN,
.scl = TP_SCL_PIN,
.addr = TP_I2C_ADDR,
.scl_func = TP_SCL_FUNC,
.sda_func = TP_SDA_FUNC,
},
.rst = {
.pin = TP_RST_PIN,
.delay_ms = TP_RST_DELAY_MS,
.active_low = TP_RST_ACTIVE_LOW,
},
.int_pin = {
.pin = TP_INT_PIN,
.signal = L_INT_EDGE_FALL,
.pull = LIOT_FORCE_PULL_UP,
},
.sensor = &TP_SENSOR,
.fw_auto_update = TP_FW_AUTO_UPDATE,
};
liot_tp_handle_t tp = liot_tp_init(&tp_cfg);
if (!tp) {
liot_trace("[TP] init failed");
return;
}
uint8_t ic_info[4] = {0};
if (liot_tp_get_ic_info(tp, ic_info, sizeof(ic_info)) == LIOT_TP_SUCCESS) {
liot_trace("[TP] IC info: chip_id=0x%x fw_ver=0x%x proj_id=0x%x lpm=0x%x",
ic_info[0], ic_info[1], ic_info[2], ic_info[3]);
}
liot_tp_register_int_callback(tp,
tp_touch_callback,
tp_gesture_callback,
NULL, NULL);
liot_tp_enable_int(tp, true);
while (1) {
osDelay(1000);
}
}
5.2 使用流程
根据实际硬件选择 TP 芯片驱动,如
g_liot_tp_axs5106或g_liot_tp_cst816d。配置 TP 通信接口。I2C 方式需配置通道号、SDA、SCL、I2C 地址和引脚复用功能。
配置 RST 和 INT 引脚。
调用
liot_tp_init()初始化 TP。可调用
liot_tp_get_ic_info()确认 IC 通信和固件版本。注册触摸和手势回调。
调用
liot_tp_enable_int(tp, true)启动 INT 事件轮询。在回调函数中处理触摸坐标和手势事件。
6 常见问题
6.1 当前 SDK 支持哪些 TP 芯片?
当前示例驱动支持 AXS5106 和 CST816D,具体以 LSDK/components/driver/tp 目录中的驱动文件为准。
6.2 TP 使用 I2C 还是 SPI?
TP 框架支持 I2C 和 SPI 两种接口类型,通过 liot_tp_config_t.interface_type 选择。当前 demo 使用 I2C。
6.3 AXS5106 和 CST816D 的 I2C 地址是多少?
AXS5106 示例地址为 0x63,CST816D 正常模式地址为 0x15。CST816D 固件升级 BOOT 地址为 0x6A。
6.4 初始化失败如何排查?
优先检查 TP 电源、RST 时序、I2C 通道、SDA/SCL 引脚、引脚复用、I2C 地址、INT/RST 引脚号和所选 sensor 是否与实际芯片一致。
6.5 为什么没有触摸回调?
需要确认 INT 引脚连接正确、触发方式正确、已调用 liot_tp_register_int_callback() 注册回调,并已调用 liot_tp_enable_int(tp, true) 启动 INT 事件轮询。
6.6 是否所有 TP 都支持设置触摸阈值?
不是。liot_tp_set_threshold() 是否生效取决于具体 TP 芯片驱动。AXS5106 和 CST816D 当前驱动中不支持直接设置触摸阈值。
6.7 是否所有 TP 都支持手势?
不是。是否支持手势由具体 TP 芯片和驱动决定,可参考 liot_tp_sensor_t.gesture_support 和对应驱动实现。
6.8 固件自动升级如何控制?
通过 liot_tp_config_t.fw_auto_update 控制。设置为 true 时初始化流程可执行自动升级,设置为 false 时跳过自动升级。内置固件选择需参考对应固件头文件。