chore: migrate project into clean repository

This commit is contained in:
yuuux
2026-08-13 16:50:52 +08:00
commit d1d25a09e7
27405 changed files with 9422808 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
# LISA Touch Driver
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
if(CONFIG_LISA_TOUCH_DEVICE)
listenai_library_named(lisa_touch_device)
listenai_library_sources_ifdef(CONFIG_LISA_TOUCH_ARCS_CST328 lisa_touch_cst328.c)
listenai_library_sources_ifdef(CONFIG_LISA_TOUCH_ARCS_AXS15231B lisa_touch_axs15231b.c)
listenai_library_sources_ifdef(CONFIG_LISA_TOUCH_ARCS_FT5336 lisa_touch_ft5336.c)
listenai_library_sources_ifdef(CONFIG_LISA_TOUCH_ARCS_CST816D lisa_touch_cst816d.c)
listenai_library_sources_ifdef(CONFIG_LISA_TOUCH_ARCS_ST77921 lisa_touch_st77921.c)
listenai_library_sources_ifdef(CONFIG_LISA_TOUCH_ARCS_BL6133 lisa_touch_bl6133.c)
listenai_include_directories(.)
endif() # CONFIG_LISA_TOUCH_DEVICE

View File

@@ -0,0 +1,27 @@
# LISA Touch Driver Configuration
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
menuconfig LISA_TOUCH_DEVICE
bool "Enable LISA Touch Driver"
default n
depends on LISA_DEVICE
help
Enable LISA touch device driver based on lisa_device framework.
Provides touch screen interfaces for reading touch events,
touch coordinates, and gesture recognition.
This driver wraps various touch chip drivers and provides a unified
device interface for touch operations.
if LISA_TOUCH_DEVICE
rsource "Kconfig.arcs.cst328"
rsource "Kconfig.arcs.axs15231b"
rsource "Kconfig.arcs.ft5336"
rsource "Kconfig.arcs.cst816d"
rsource "Kconfig.arcs.st77921"
rsource "Kconfig.arcs.bl6133"
endif # LISA_TOUCH_DEVICE

View File

@@ -0,0 +1,41 @@
# LISA Touch Driver Configuration - AXS15231B
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
config LISA_TOUCH_ARCS_AXS15231B
bool "Enable LISA Touch ARCS AXS15231B support"
default n
help
Enable LISA Touch ARCS AXS15231B touch chip support.
AXS15231B Features:
- I2C Address: 0x3B (7-bit)
- Register: Command-based (13-byte command)
- Resolution: 390x390
- Single touch point
- Interrupt trigger: Falling edge
- Reset sequence: High->Low->High (10ms-10ms-300ms)
if LISA_TOUCH_ARCS_AXS15231B
config LISA_TOUCH_ARCS_AXS15231B_READ_TASK_PRIORITY
int "AXS15231B interrupt mode read task priority"
default 2
range 1 10
help
Set the priority of the AXS15231B touch driver's internal read task.
This task is created when interrupt mode is enabled and is responsible
for reading I2C data from the touch chip after receiving a GPIO interrupt.
Priority range: 1-10 (higher number = higher priority)
Default: 2 (tskIDLE_PRIORITY + 2)
Note:
- Higher priority ensures faster response to touch events
- Should be higher than idle task (1) but lower than critical system tasks
- Recommended range: 2-5 for most applications
endif # LISA_TOUCH_ARCS_AXS15231B

View File

@@ -0,0 +1,40 @@
# LISA Touch Driver Configuration - BL6133
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
config LISA_TOUCH_ARCS_BL6133
bool "Enable LISA Touch ARCS BL6133 support"
default n
help
Enable LISA Touch ARCS BL6133 touch chip support.
BL6133 Features:
- I2C Address: 0x2C (7-bit)
- Register: 0x01 (8-bit)
- Resolution: 320x386
- Single touch point
- Interrupt trigger: Falling edge
- Reset sequence: High->Low->High (10ms-100ms-300ms)
if LISA_TOUCH_ARCS_BL6133
config LISA_TOUCH_ARCS_BL6133_READ_TASK_PRIORITY
int "BL6133 interrupt mode read task priority"
default 2
range 1 10
help
Set the priority of the BL6133 touch driver's internal read task.
This task is created when interrupt mode is enabled and is responsible
for reading I2C data from the touch chip after receiving a GPIO interrupt.
Priority range: 1-10 (higher number = higher priority)
Default: 2 (tskIDLE_PRIORITY + 2)
Note:
- Higher priority ensures faster response to touch events
- Should be higher than idle task (1) but lower than critical system tasks
- Recommended range: 2-5 for most applications
endif # LISA_TOUCH_ARCS_BL6133

View File

@@ -0,0 +1,43 @@
# LISA Touch Driver Configuration - CST328
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
config LISA_TOUCH_ARCS_CST328
bool "Enable LISA Touch ARCS CST328 support"
default n
help
Enable LISA Touch ARCS CST328 touch chip support.
CST328 Features:
- I2C Address: 0x1D (7-bit)
- Register: 0xD000 (16-bit, little-endian)
- Resolution: 240x320
- Single touch point
- Interrupt trigger: Rising edge
- Reset sequence: High->Low->High (10ms-10ms-200ms)
if LISA_TOUCH_ARCS_CST328
config LISA_TOUCH_ARCS_CST328_READ_TASK_PRIORITY
int "CST328 interrupt mode read task priority"
default 2
range 1 10
help
Set the priority of the CST328 touch driver's internal read task.
This task is created when interrupt mode is enabled and is responsible
for reading I2C data from the touch chip after receiving a GPIO interrupt.
Priority range: 1-10 (higher number = higher priority)
Default: 2 (tskIDLE_PRIORITY + 2)
Note:
- Higher priority ensures faster response to touch events
- Should be higher than idle task (1) but lower than critical system tasks
- Recommended range: 2-5 for most applications
endif # LISA_TOUCH_ARCS_CST328

View File

@@ -0,0 +1,40 @@
# LISA Touch Driver Configuration - CST816D
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
config LISA_TOUCH_ARCS_CST816D
bool "Enable LISA Touch ARCS CST816D support"
default n
help
Enable LISA Touch ARCS CST816D touch chip support.
CST816D Features:
- I2C Address: 0x15 (7-bit)
- Register: 0xD000 (16-bit, little-endian)
- Resolution: 240x240
- Single touch point
- Interrupt trigger: Falling edge
- Reset sequence: High->Low->High (20ms-20ms-400ms)
if LISA_TOUCH_ARCS_CST816D
config LISA_TOUCH_ARCS_CST816D_READ_TASK_PRIORITY
int "CST816D interrupt mode read task priority"
default 2
range 1 10
help
Set the priority of the CST816D touch driver's internal read task.
This task is created when interrupt mode is enabled and is responsible
for reading I2C data from the touch chip after receiving a GPIO interrupt.
Priority range: 1-10 (higher number = higher priority)
Default: 2 (tskIDLE_PRIORITY + 2)
Note:
- Higher priority ensures faster response to touch events
- Should be higher than idle task (1) but lower than critical system tasks
- Recommended range: 2-5 for most applications
endif # LISA_TOUCH_ARCS_CST816D

View File

@@ -0,0 +1,40 @@
# LISA Touch Driver Configuration - FT5336
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
config LISA_TOUCH_ARCS_FT5336
bool "Enable LISA Touch ARCS FT5336 support"
default n
help
Enable LISA Touch ARCS FT5336 touch chip support.
FT5336 Features:
- I2C Address: 0x38 (7-bit)
- Register: 0x02 (8-bit)
- Resolution: 480x272
- Single touch point
- Interrupt trigger: Falling edge
- Reset sequence: High->Low->High (10ms-10ms-300ms)
if LISA_TOUCH_ARCS_FT5336
config LISA_TOUCH_ARCS_FT5336_READ_TASK_PRIORITY
int "FT5336 interrupt mode read task priority"
default 2
range 1 10
help
Set the priority of the FT5336 touch driver's internal read task.
This task is created when interrupt mode is enabled and is responsible
for reading I2C data from the touch chip after receiving a GPIO interrupt.
Priority range: 1-10 (higher number = higher priority)
Default: 2 (tskIDLE_PRIORITY + 2)
Note:
- Higher priority ensures faster response to touch events
- Should be higher than idle task (1) but lower than critical system tasks
- Recommended range: 2-5 for most applications
endif # LISA_TOUCH_ARCS_FT5336

View File

@@ -0,0 +1,40 @@
# LISA Touch Driver Configuration - ST77921
#
# Copyright (c) 2025, LISTENAI
# SPDX-License-Identifier: Apache-2.0
config LISA_TOUCH_ARCS_ST77921
bool "Enable LISA Touch ARCS ST77921 support"
default n
help
Enable LISA Touch ARCS ST77921 touch chip support.
ST77921 Features:
- I2C Address: 0x55 (7-bit)
- Register: 0x0010/0x0014 (16-bit, big-endian)
- Resolution: 480x480
- Single touch point
- Interrupt trigger: Falling edge
- Reset sequence: High->Low->High (10ms-10ms-300ms)
if LISA_TOUCH_ARCS_ST77921
config LISA_TOUCH_ARCS_ST77921_READ_TASK_PRIORITY
int "ST77921 interrupt mode read task priority"
default 2
range 1 10
help
Set the priority of the ST77921 touch driver's internal read task.
This task is created when interrupt mode is enabled and is responsible
for reading I2C data from the touch chip after receiving a GPIO interrupt.
Priority range: 1-10 (higher number = higher priority)
Default: 2 (tskIDLE_PRIORITY + 2)
Note:
- Higher priority ensures faster response to touch events
- Should be higher than idle task (1) but lower than critical system tasks
- Recommended range: 2-5 for most applications
endif # LISA_TOUCH_ARCS_ST77921

View File

@@ -0,0 +1,352 @@
# Touch 驱动
基于 lisa_device 框架的 Touch 设备驱动,为 ARCS 平台提供统一的触摸屏接口。
## 功能特性
- **多芯片支持**: 支持 CST328、AXS15231B、FT5336、CST816D、ST77921、BL6133 等触摸芯片
- **总线接口**: I2C 接口通信,自动配置 I2C 总线参数
- **工作模式**: 支持轮询模式和中断模式
- **中断优化**: 中断模式下使用驱动内部任务处理 I2C 读取,回调在任务上下文中安全执行
- **线程安全**: 内部使用互斥锁保护并发访问,自动判断线程和中断上下文
- **灵活配置**: 通过 Kconfig 选择启用的芯片,节省资源
## 配置选项
在 `prj.conf` 中启用驱动并选择芯片:
```kconfig
CONFIG_LISA_TOUCH_DEVICE=y
CONFIG_LISA_TOUCH_ARCS_CST328=y # 启用 CST328 芯片
# CONFIG_LISA_TOUCH_ARCS_AXS15231B=y # 启用 AXS15231B 芯片
# CONFIG_LISA_TOUCH_ARCS_FT5336=y # 启用 FT5336 芯片
# CONFIG_LISA_TOUCH_ARCS_CST816D=y # 启用 CST816D 芯片
# CONFIG_LISA_TOUCH_ARCS_ST77921=y # 启用 ST77921 芯片
# CONFIG_LISA_TOUCH_ARCS_BL6133=y # 启用 BL6133 芯片
CONFIG_LISA_I2C=y # 依赖 I2C 驱动
CONFIG_LISA_GPIO_DEVICE=y # 依赖 GPIO 驱动
```
根据需要选择启用一个或多个触摸芯片。每个芯片注册为独立设备,可同时使用。
## 支持的芯片
| 芯片型号 | 设备名称 | I2C地址 | 分辨率 | 状态 |
|---------|---------|---------|--------|------|
| CST328 | `touch_cst328` | 0x1D | 240x320 | ✅ 已验证 |
| AXS15231B | `touch_axs15231b` | 0x3B | 390x390 | ⚠️ 待验证 |
| FT5336 | `touch_ft5336` | 0x38 | 480x272 | ⚠️ 待验证 |
| CST816D | `touch_cst816d` | 0x15 | 240x240 | ⚠️ 待验证 |
| ST77921 | `touch_st77921` | 0x55 | 480x480 | ⚠️ 待验证 |
| BL6133 | `touch_bl6133` | 0x2C | 320x386 | ⚠️ 待验证 |
## API 接口
### 总线配置
```c
int lisa_touch_attach_bus(lisa_device_t *dev, const lisa_touch_bus_config_t *bus_config);
```
配置触摸设备的总线接口(I2C)。I2C 总线将自动配置为标准速度(100kHz)。
### 触摸控制
```c
int lisa_touch_enable(lisa_device_t *dev);
int lisa_touch_disable(lisa_device_t *dev);
int lisa_touch_get_capabilities(lisa_device_t *dev, lisa_touch_capabilities_t *caps);
```
启用/禁用触摸设备,获取设备能力信息(分辨率、最大触摸点数等)。
### 事件读取
```c
int lisa_touch_read_event(lisa_device_t *dev, lisa_touch_event_t *event);
```
读取触摸事件(按下、释放、坐标等)。
**事件类型说明**:
- `LISA_TOUCH_EVENT_NONE`: 无事件
- `LISA_TOUCH_EVENT_PRESS`: 按下事件(检测到触摸时返回,包含坐标信息)
- `LISA_TOUCH_EVENT_RELEASE`: 释放事件(检测到释放时返回)
**行为说明**:
- 检测到触摸时返回 `PRESS` 事件,包含当前坐标
- 检测到释放时返回 `RELEASE` 事件
- 轮询模式下,每次读取时如果触摸状态为按下,都会返回 `PRESS` 事件(坐标可能更新)
**返回值**:
- `LISA_DEVICE_OK (0)`: 成功读取事件
- `LISA_DEVICE_ERR_INVALID`: 参数无效
- `LISA_DEVICE_ERR_NOT_READY`: 设备未启用或 I2C 未就绪
- `LISA_DEVICE_ERR_NOT_SUPPORT`: 当前处于中断模式,不支持轮询读取
- `LISA_DEVICE_ERR_IO`: 读取失败(无有效触摸数据)
**重要提示**:
- ⚠️ **中断模式下禁止调用此函数**:如果已通过 `lisa_touch_set_int_mode(INTERRUPT)` 设置为中断模式,再调用 `lisa_touch_read_event()` 将返回 `LISA_DEVICE_ERR_NOT_SUPPORT`
- 这是为了避免轮询读取与中断任务产生资源竞争,导致状态混乱
- 中断模式下请使用回调函数接收触摸事件
### 中断模式
```c
int lisa_touch_set_callback(lisa_device_t *dev, lisa_touch_callback_t callback, void *user_data);
int lisa_touch_set_int_mode(lisa_device_t *dev, lisa_touch_int_mode_t mode);
```
设置触摸中断回调函数和中断模式(中断模式/轮询模式)。
## 使用方法
### 基本步骤
1. **配置引脚**: 将对应的 GPIO 引脚配置为 I2C 和 GPIO 功能
2. **获取设备**: 通过 `lisa_device_get()` 获取触摸设备、I2C 设备和 GPIO 设备
3. **配置总线**: 调用 `lisa_touch_attach_bus()` 配置 I2C 总线
4. **设置模式**: 调用 `lisa_touch_set_int_mode()` 设置工作模式(轮询/中断)
5. **启用设备**: 调用 `lisa_touch_enable()` 启用设备
6. **读取事件**: 调用 `lisa_touch_read_event()` 读取触摸事件(轮询模式)或设置回调(中断模式)
### 轮询模式
```c
#include "lisa_touch.h"
#include "lisa_gpio.h"
#include "IOMuxManager.h"
#include "board.h"
#include "pinmux.h"
// 1. 配置 I2C 和 GPIO 引脚(通过 pinmux 重定向或直接配置)
#ifdef CONFIG_BOARD_ARCS_EVB
void lisa_i2c0_pinmux()
{
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 22, 8); // SDA
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 23, 8); // SCL
}
void lisa_gpioa_pinmux()
{
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 25, 0); // RST
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 24, 0); // INT
}
#endif
// 2. 获取设备
lisa_device_t *touch = lisa_device_get("touch_cst328");
lisa_device_t *i2c_dev = lisa_device_get("i2c0");
lisa_device_t *gpio_dev = lisa_device_get("gpioa");
if (!touch || !i2c_dev || !gpio_dev) {
return -1;
}
// 3. 配置总线
lisa_touch_bus_config_t bus_config = {
.bus_type = LISA_TOUCH_BUS_I2C,
.config = {
.i2c = {
.i2c_dev = i2c_dev,
.int_gpio = gpio_dev,
.int_pin = 24,
.rst_gpio = gpio_dev,
.rst_pin = 25,
}
}
};
lisa_touch_attach_bus(touch, &bus_config);
// 4. 设置轮询模式(可选,默认就是轮询模式)
lisa_touch_set_int_mode(touch, LISA_TOUCH_INT_MODE_POLLING);
// 5. 启用设备
lisa_touch_enable(touch);
// 6. 获取设备能力
lisa_touch_capabilities_t caps;
lisa_touch_get_capabilities(touch, &caps);
printf("Touch: max_x=%d, max_y=%d, max_points=%d\n",
caps.max_x, caps.max_y, caps.max_points);
// 7. 循环读取触摸事件
while (1) {
lisa_touch_event_t event;
int ret = lisa_touch_read_event(touch, &event);
if (ret == LISA_DEVICE_OK) {
switch (event.type) {
case LISA_TOUCH_EVENT_PRESS:
// 按下事件
if (event.point_count > 0) {
printf("Touch: x=%4d, y=%4d, type=PRESS\n",
event.points[0].x, event.points[0].y);
}
break;
case LISA_TOUCH_EVENT_RELEASE:
printf("Touch: RELEASE\n");
break;
}
}
vTaskDelay(pdMS_TO_TICKS(50)); // 50ms 采样间隔
}
```
### 中断模式
```c
// 触摸事件回调函数(在任务上下文中调用,由驱动内部任务触发)
void touch_event_callback(const lisa_touch_event_t *event, void *user_data)
{
(void)user_data;
// 此回调在驱动内部的任务上下文中调用,可以安全使用 printf
switch (event->type) {
case LISA_TOUCH_EVENT_PRESS:
// 按下事件
if (event->point_count > 0) {
printf("Touch: x=%4d, y=%4d, type=PRESS\n",
event->points[0].x, event->points[0].y);
}
break;
case LISA_TOUCH_EVENT_RELEASE:
// 释放事件
printf("Touch: type=RELEASE\n");
break;
}
}
int main(void)
{
// ... 配置引脚和总线(同上)...
// 设置触摸事件回调函数
lisa_touch_set_callback(touch, touch_event_callback, NULL);
// 设置中断模式
lisa_touch_set_int_mode(touch, LISA_TOUCH_INT_MODE_INTERRUPT);
// 启用设备
lisa_touch_enable(touch);
// 主循环:触摸事件通过回调自动处理
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000));
printf("System running, waiting for touch events...\n");
}
}
```
**中断模式说明**:
- 驱动内部创建专用任务处理 I2C 读取
- GPIO 中断回调只发送任务通知,不执行耗时操作
- 用户回调在任务上下文中调用,可以安全使用 `printf`、`malloc` 等操作
- 回调直接收到完整的触摸数据(包括坐标),无需再次读取
- ⚠️ **中断模式下不能调用 `lisa_touch_read_event()`**,会返回 `LISA_DEVICE_ERR_NOT_SUPPORT` 错误
### 模式切换
轮询模式和中断模式可以动态切换,但同一时间只能使用一种模式:
```c
// 从轮询模式切换到中断模式
lisa_touch_set_callback(touch, touch_event_callback, NULL); // 先设置回调
lisa_touch_set_int_mode(touch, LISA_TOUCH_INT_MODE_INTERRUPT); // 切换到中断模式
// 此后不能再调用 lisa_touch_read_event()
// 从中断模式切换回轮询模式
lisa_touch_set_int_mode(touch, LISA_TOUCH_INT_MODE_POLLING); // 切换到轮询模式
// 此后可以调用 lisa_touch_read_event(),回调不再触发
```
**重要提示**:
- 中断模式下禁止调用 `lisa_touch_read_event()`,避免资源竞争
- 轮询模式下回调函数不会被调用(即使已设置)
- 切换模式前无需禁用设备,可以动态切换
## 硬件配置
### 引脚复用配置
Touch 驱动依赖 I2C 和 GPIO 驱动,引脚复用配置由 I2C 和 GPIO 驱动在初始化时自动完成。
**配置位置**:
- **I2C 引脚**: `boards/<板型名>/pinmux.c` 中的 `lisa_i2c0_pinmux()` 或 `lisa_i2c1_pinmux()` 函数
- **GPIO 引脚**: `boards/<板型名>/pinmux.c` 中的 `lisa_gpioa_pinmux()` 或 `lisa_gpiob_pinmux()` 函数
- **调用时机**: I2C 和 GPIO 设备初始化时自动调用
**示例** (参考 `boards/arcs_evb/pinmux.c`):
```c
void lisa_i2c0_pinmux()
{
/* I2C0: PA22=SDA, PA23=SCL, 功能码 8 */
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 22, 8);
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 23, 8);
}
void lisa_gpioa_pinmux()
{
/* GPIO: PA25=RST, PA24=INT, 功能码 0 */
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 25, 0);
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 24, 0);
}
```
**注意**:
- 该函数由板型相关代码实现,不同板型的引脚配置可能不同
- 如需自定义引脚配置,可在 sample 中重写 pinmux 函数(参考 `samples/drivers/devices/lisa_touch/interrupt_mode/src/main.c`)
- 触摸驱动需要以下引脚:
- I2C SDA/SCL:用于 I2C 通信
- INT:触摸中断引脚(中断模式需要)
- RST:芯片复位引脚
### 芯片特性
不同芯片的 I2C 地址、分辨率、触发方式可能不同,请参考具体芯片手册和"支持的芯片"表格。
## 工作模式对比
| 特性 | 轮询模式 | 中断模式 |
|------|---------|---------|
| **响应速度** | 取决于轮询间隔(通常 50ms) | 立即响应(< 1ms) |
| **CPU 占用** | 高(持续轮询) | 低(事件驱动) |
| **功耗** | 高 | 低 |
| **实现复杂度** | 简单 | 中等 |
| **适用场景** | 简单应用、对响应时间不敏感 | 需要快速响应、低功耗应用 |
| **回调上下文** | 任务上下文(安全) | 任务上下文(安全) |
## 注意事项
1. **I2C 自动配置**: `lisa_touch_attach_bus()` 会自动配置 I2C 总线为标准速度(100kHz),无需手动调用 `lisa_i2c_configure()`
2. **引脚配置**: 使用前需配置对应的 GPIO 引脚,驱动初始化时会自动调用 pinmux 函数,或通过 pinmux 重定向自定义引脚
3. **设备依赖**: 需要确保 I2C 和 GPIO 设备已就绪(通过 `lisa_device_ready()` 检查)
4. **中断回调**: 中断模式下,用户回调在驱动内部任务上下文中调用,可以安全使用 `printf`、`malloc` 等操作
5. **线程安全**: 驱动内部已实现线程保护,可在多线程环境中使用
6. **芯片特性**: 不同芯片的坐标系、分辨率、触发方式可能不同,请参考具体芯片手册
7. **设备选择**: 使用前需在 `prj.conf` 中启用对应的触摸芯片和依赖驱动(I2C、GPIO)
8. **多芯片使用**: 每个芯片注册为独立设备,可同时使用多个触摸芯片
## 文件说明
- `lisa_touch.h` - 驱动头文件,包含所有 API 和类型定义
- `lisa_touch_cst328.c` - CST328 芯片驱动实现
- `lisa_touch_axs15231b.c` - AXS15231B 芯片驱动实现
- `lisa_touch_ft5336.c` - FT5336 芯片驱动实现
- `lisa_touch_cst816d.c` - CST816D 芯片驱动实现
- `lisa_touch_st77921.c` - ST77921 芯片驱动实现
- `lisa_touch_bl6133.c` - BL6133 芯片驱动实现
- `Kconfig` - 主配置选项
- `Kconfig.arcs.*` - 各芯片配置选项
- `CMakeLists.txt` - 构建配置
- `README.md` - 驱动使用说明

View File

@@ -0,0 +1,323 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file lisa_touch.h
* @brief LISA Touch 触摸设备驱动接口
*/
#pragma once
#include "lisa_device.h"
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ========================================================================
* Touch 类型定义
* ======================================================================== */
/**
* @brief Touch 总线接口类型
*/
typedef enum {
LISA_TOUCH_BUS_I2C = 0, /* I2C 接口 */
} lisa_touch_bus_type_t;
/**
* @brief Touch 总线配置(I2C)
*/
typedef struct {
lisa_device_t *i2c_dev; /* I2C设备指针 */
lisa_device_t *int_gpio; /* 中断信号的GPIO设备(可选)*/
uint32_t int_pin; /* 中断引脚号 */
lisa_device_t *rst_gpio; /* 复位信号的GPIO设备(可选)*/
uint32_t rst_pin; /* 复位引脚号 */
} lisa_touch_bus_i2c_config_t;
/**
* @brief Touch 总线配置联合体
*/
typedef union {
lisa_touch_bus_i2c_config_t i2c;
} lisa_touch_bus_config_u;
/**
* @brief Touch 总线配置结构体
*/
typedef struct {
lisa_touch_bus_type_t bus_type; /* 总线类型 */
lisa_touch_bus_config_u config; /* 总线配置 */
} lisa_touch_bus_config_t;
/**
* @brief Touch 点状态
*/
typedef enum {
LISA_TOUCH_POINT_RELEASED = 0, /* 释放 */
LISA_TOUCH_POINT_PRESSED = 1, /* 按下 */
LISA_TOUCH_POINT_CONTACT = 2, /* 持续接触 */
} lisa_touch_point_state_t;
/**
* @brief Touch 触摸点数据
*/
typedef struct {
uint16_t x; /* X 坐标 */
uint16_t y; /* Y 坐标 */
uint8_t id; /* 触摸点ID(用于多点触摸) */
lisa_touch_point_state_t state; /* 触摸点状态 */
uint16_t pressure; /* 压力值(可选,如果硬件支持)*/
uint16_t area; /* 接触面积(可选,如果硬件支持)*/
} lisa_touch_point_t;
/**
* @brief Touch 事件类型
*/
typedef enum {
LISA_TOUCH_EVENT_NONE = 0, /* 无事件 */
LISA_TOUCH_EVENT_PRESS = 1, /* 按下事件 */
LISA_TOUCH_EVENT_RELEASE = 2, /* 释放事件 */
LISA_TOUCH_EVENT_CONTACT = 3, /* 持续接触事件 */
LISA_TOUCH_EVENT_GESTURE = 4, /* 手势事件 */
} lisa_touch_event_type_t;
/**
* @brief Touch 手势类型
*/
typedef enum {
LISA_TOUCH_GESTURE_NONE = 0, /* 无手势 */
LISA_TOUCH_GESTURE_SWIPE_UP = 1, /* 向上滑动 */
LISA_TOUCH_GESTURE_SWIPE_DOWN = 2, /* 向下滑动 */
LISA_TOUCH_GESTURE_SWIPE_LEFT = 3, /* 向左滑动 */
LISA_TOUCH_GESTURE_SWIPE_RIGHT = 4, /* 向右滑动 */
LISA_TOUCH_GESTURE_PINCH_IN = 5, /* 缩小 */
LISA_TOUCH_GESTURE_PINCH_OUT = 6, /* 放大 */
LISA_TOUCH_GESTURE_ROTATE_CW = 7, /* 顺时针旋转 */
LISA_TOUCH_GESTURE_ROTATE_CCW = 8, /* 逆时针旋转 */
} lisa_touch_gesture_t;
/**
* @brief Touch 事件数据
*/
typedef struct {
lisa_touch_event_type_t type; /* 事件类型 */
uint8_t point_count; /* 触摸点数量 */
lisa_touch_point_t points[10]; /* 触摸点数据(支持最多10点触摸)*/
lisa_touch_gesture_t gesture; /* 手势类型(仅当事件类型为GESTURE时有效)*/
} lisa_touch_event_t;
/**
* @brief Touch 能力结构体
*/
typedef struct {
uint16_t max_x; /* 最大 X 坐标 */
uint16_t max_y; /* 最大 Y 坐标 */
uint8_t max_points; /* 最大触摸点数 */
bool has_pressure; /* 是否支持压力检测 */
bool has_gesture; /* 是否支持手势识别 */
uint32_t supported_gestures; /* 支持的手势位掩码 */
} lisa_touch_capabilities_t;
/**
* @brief Touch 事件回调函数类型
*
* @param event 触摸事件数据
* @param user_data 用户数据指针
*
* @note 如果需要访问设备,可以通过 user_data 传入设备指针
*/
typedef void (*lisa_touch_callback_t)(const lisa_touch_event_t *event, void *user_data);
/**
* @brief Touch 中断模式
*/
typedef enum {
LISA_TOUCH_INT_MODE_POLLING = 0, /* 轮询模式 */
LISA_TOUCH_INT_MODE_INTERRUPT = 1, /* 中断模式 */
} lisa_touch_int_mode_t;
/* ========================================================================
* Touch 设备 API 结构体
* ======================================================================== */
typedef struct {
int (*get_capabilities)(lisa_device_t *dev, lisa_touch_capabilities_t *caps);
int (*read_event)(lisa_device_t *dev, lisa_touch_event_t *event);
int (*enable)(lisa_device_t *dev);
int (*disable)(lisa_device_t *dev);
int (*attach_bus)(lisa_device_t *dev, const lisa_touch_bus_config_t *bus_config);
int (*set_callback)(lisa_device_t *dev, lisa_touch_callback_t callback, void *user_data);
int (*set_int_mode)(lisa_device_t *dev, lisa_touch_int_mode_t mode);
} lisa_touch_api_t;
/* ========================================================================
* Touch 对外接口函数
* ======================================================================== */
/**
* @brief 获取触摸设备能力
*
* @param dev Touch设备指针
* @param caps 输出参数,用于接收设备能力信息
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_touch_get_capabilities(lisa_device_t *dev, lisa_touch_capabilities_t *caps)
{
if (!dev || !dev->api || !caps) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_api_t *api = (lisa_touch_api_t *)dev->api;
return api->get_capabilities ? api->get_capabilities(dev, caps) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 读取触摸事件
*
* @param dev Touch设备指针
* @param event 输出参数,用于接收触摸事件数据
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_touch_read_event(lisa_device_t *dev, lisa_touch_event_t *event)
{
if (!dev || !dev->api || !event) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_api_t *api = (lisa_touch_api_t *)dev->api;
return api->read_event ? api->read_event(dev, event) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 启用触摸设备
*
* @param dev Touch设备指针
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_touch_enable(lisa_device_t *dev)
{
if (!dev || !dev->api) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_api_t *api = (lisa_touch_api_t *)dev->api;
return api->enable ? api->enable(dev) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 禁用触摸设备
*
* @param dev Touch设备指针
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_touch_disable(lisa_device_t *dev)
{
if (!dev || !dev->api) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_api_t *api = (lisa_touch_api_t *)dev->api;
return api->disable ? api->disable(dev) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 附加触摸总线接口
*
* 用于配置触摸总线接口(I2C/SPI等)及相关引脚。
*
* @param dev Touch设备指针
* @param bus_config 总线配置结构体
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_touch_attach_bus(lisa_device_t *dev, const lisa_touch_bus_config_t *bus_config)
{
if (!dev || !dev->api || !bus_config) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_api_t *api = (lisa_touch_api_t *)dev->api;
return api->attach_bus ? api->attach_bus(dev, bus_config) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 设置触摸事件回调函数
*
* @param dev Touch设备指针
* @param callback 回调函数指针
* @param user_data 用户数据指针
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_touch_set_callback(lisa_device_t *dev, lisa_touch_callback_t callback, void *user_data)
{
if (!dev || !dev->api) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_api_t *api = (lisa_touch_api_t *)dev->api;
return api->set_callback ? api->set_callback(dev, callback, user_data) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/**
* @brief 设置中断模式
*
* @param dev Touch设备指针
* @param mode 中断模式
*
* @return 0 成功
* @return LISA_DEVICE_ERR_INVALID 参数无效
* @return LISA_DEVICE_ERR_NOT_SUPPORT 不支持该操作
* @return <0 其他错误
*/
static inline int lisa_touch_set_int_mode(lisa_device_t *dev, lisa_touch_int_mode_t mode)
{
if (!dev || !dev->api) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_api_t *api = (lisa_touch_api_t *)dev->api;
return api->set_int_mode ? api->set_int_mode(dev, mode) : LISA_DEVICE_ERR_NOT_SUPPORT;
}
/* ========================================================================
* 便捷宏定义
* ======================================================================== */
/**
* @brief 手势支持位掩码
*/
#define LISA_TOUCH_GESTURE_MASK_SWIPE_UP (1 << LISA_TOUCH_GESTURE_SWIPE_UP)
#define LISA_TOUCH_GESTURE_MASK_SWIPE_DOWN (1 << LISA_TOUCH_GESTURE_SWIPE_DOWN)
#define LISA_TOUCH_GESTURE_MASK_SWIPE_LEFT (1 << LISA_TOUCH_GESTURE_SWIPE_LEFT)
#define LISA_TOUCH_GESTURE_MASK_SWIPE_RIGHT (1 << LISA_TOUCH_GESTURE_SWIPE_RIGHT)
#define LISA_TOUCH_GESTURE_MASK_PINCH_IN (1 << LISA_TOUCH_GESTURE_PINCH_IN)
#define LISA_TOUCH_GESTURE_MASK_PINCH_OUT (1 << LISA_TOUCH_GESTURE_PINCH_OUT)
#define LISA_TOUCH_GESTURE_MASK_ROTATE_CW (1 << LISA_TOUCH_GESTURE_ROTATE_CW)
#define LISA_TOUCH_GESTURE_MASK_ROTATE_CCW (1 << LISA_TOUCH_GESTURE_ROTATE_CCW)
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,539 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file lisa_touch_axs15231b.c
* @brief AXS15231B 触摸芯片驱动 - LISA Touch 设备实现
*/
#include "lisa_touch.h"
#include "lisa_i2c.h"
#include "lisa_gpio.h"
#include "lisa_thread.h"
#include <stddef.h>
#include <string.h>
#include <lisa_mutex.h>
#include "FreeRTOS.h"
#include "task.h"
#include "portmacro.h"
#define LOG_TAG "lisa_touch_axs15231b"
#include <lisa_log.h>
/* AXS15231B I2C 地址 */
#define AXS15231B_I2C_SLAVE_ADDRESS 0x3B
/* AXS15231B 寄存器定义 */
#define AXS_TOUCH_GESTURE_POS 0
#define AXS_TOUCH_POINT_NUM_POS 1
#define AXS_TOUCH_EVENT_POS 2
#define AXS_TOUCH_X_H_POS 2
#define AXS_TOUCH_X_L_POS 3
#define AXS_TOUCH_ID_POS 4
#define AXS_TOUCH_Y_H_POS 4
#define AXS_TOUCH_Y_L_POS 5
#define AXS_TOUCH_WEIGHT_POS 6
#define AXS_TOUCH_AREA_POS 7
#define AXS_TOUCH_DOWN 0
#define AXS_TOUCH_UP 1
#define AXS_TOUCH_CONTACT 2
/* AXS15231B 触摸能力 */
#define AXS15231B_MAX_X 390
#define AXS15231B_MAX_Y 390
#define AXS15231B_MAX_POINTS 1
/* 自动判断上下文的互斥锁宏(lisa_mutex_lock/unlock 已自动判断上下文) */
#define DEVICE_LOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_lock(priv->mutex, LISA_OS_WAIT_FOREVER); \
} \
} while (0)
#define DEVICE_UNLOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_unlock(priv->mutex); \
} \
} while (0)
/* ===== AXS15231B 设备私有数据 ===== */
typedef struct {
lisa_device_t *i2c_dev; /* I2C 设备指针 */
lisa_device_t *int_gpio; /* 中断 GPIO 设备指针 */
lisa_device_t *rst_gpio; /* 复位 GPIO 设备指针 */
uint32_t int_pin; /* 中断引脚号 */
uint32_t rst_pin; /* 复位引脚号 */
lisa_touch_callback_t callback; /* 触摸事件回调函数 */
void *callback_user_data; /* 回调用户数据 */
lisa_mutex_t *mutex; /* 互斥锁 */
bool initialized; /* 初始化标志 */
bool enabled; /* 使能标志 */
bool last_pressed; /* 上次按下状态 */
TaskHandle_t read_task_handle; /* I2C 读取任务句柄(用于中断模式) */
bool interrupt_mode_active; /* 中断模式是否激活 */
} lisa_touch_axs15231b_priv_t;
/* ===== AXS15231B 设备静态实例 ===== */
static lisa_touch_axs15231b_priv_t touch_axs15231b_priv;
/* ===== 内部辅助函数 ===== */
/* 前向声明 */
static int axs15231b_touch_read_event_internal(lisa_touch_axs15231b_priv_t *priv, lisa_touch_event_t *event);
/**
* @brief 读取 AXS15231B 坐标
*/
static int axs15231b_read_coordinates(lisa_device_t *i2c_dev, uint16_t *x, uint16_t *y, bool *pressed)
{
if (!i2c_dev) {
return LISA_DEVICE_ERR_INVALID;
}
uint8_t read_cmd[13] = {0xb5, 0xab, 0xa5, 0x5a, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t read_buf[14] = {0};
/* 发送读取命令 */
int ret = lisa_i2c_write(i2c_dev, AXS15231B_I2C_SLAVE_ADDRESS, read_cmd, sizeof(read_cmd));
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "I2C write failed: %d", ret);
return ret;
}
/* 读取数据 */
ret = lisa_i2c_read(i2c_dev, AXS15231B_I2C_SLAVE_ADDRESS, read_buf, sizeof(read_buf));
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "I2C read failed: %d", ret);
return ret;
}
uint8_t point_num = read_buf[AXS_TOUCH_POINT_NUM_POS] & 0x0F;
/* 无效数据 */
if (point_num != 1) {
return LISA_DEVICE_ERR_IO;
}
*x = ((read_buf[AXS_TOUCH_X_H_POS] & 0x0F) << 8) + read_buf[AXS_TOUCH_X_L_POS];
*y = ((read_buf[AXS_TOUCH_Y_H_POS] & 0x0F) << 8) + read_buf[AXS_TOUCH_Y_L_POS];
*pressed = ((read_buf[AXS_TOUCH_EVENT_POS] >> 6) == AXS_TOUCH_UP) ? false : true;
return LISA_DEVICE_OK;
}
/**
* @brief I2C 读取任务(用于中断模式)
* @note 此任务在中断模式下运行,等待任务通知后读取 I2C 数据
*/
static void axs15231b_read_task(void *pvParameters)
{
lisa_touch_axs15231b_priv_t *priv = (lisa_touch_axs15231b_priv_t *)pvParameters;
while (1) {
/* 等待任务通知(中断会发送通知)
* 使用 pdFALSE 清除所有累积的通知,避免重复处理
*/
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
if (!priv->enabled || !priv->interrupt_mode_active) {
continue;
}
/* 在任务上下文中读取完整的触摸数据 */
lisa_touch_event_t event = {0};
int ret = axs15231b_touch_read_event_internal(priv, &event);
if (ret == LISA_DEVICE_OK && priv->callback) {
/* 通过回调函数返回完整数据 */
priv->callback(&event, priv->callback_user_data);
}
}
}
/**
* @brief GPIO 中断回调
*/
static void axs15231b_gpio_irq_callback(uint32_t pin, void *user_data)
{
(void)pin;
lisa_touch_axs15231b_priv_t *priv = (lisa_touch_axs15231b_priv_t *)user_data;
if (!priv || !priv->interrupt_mode_active) {
return;
}
/* 在中断上下文中发送任务通知,唤醒读取任务 */
if (priv->read_task_handle != NULL) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(priv->read_task_handle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
/**
* @brief 硬件复位芯片
*/
static int axs15231b_hardware_reset(lisa_touch_axs15231b_priv_t *priv)
{
if (!priv->rst_gpio || !lisa_device_ready(priv->rst_gpio)) {
LISA_LOGW(LOG_TAG, "Reset GPIO not available, skip reset");
return LISA_DEVICE_OK;
}
/* 配置复位引脚为输出 */
int ret = lisa_gpio_configure(priv->rst_gpio, priv->rst_pin, LISA_GPIO_OUTPUT);
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "Failed to configure reset pin: %d", ret);
return ret;
}
/* 复位序列:高->低->高 */
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(10);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 0);
lisa_thread_mdelay(10);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(300);
LISA_LOGI(LOG_TAG, "AXS15231B hardware reset completed");
return LISA_DEVICE_OK;
}
/* ===== LISA Touch API 实现 ===== */
/**
* @brief 获取触摸设备能力
*/
static int axs15231b_touch_get_capabilities(lisa_device_t *dev, lisa_touch_capabilities_t *caps)
{
if (!lisa_device_is_initialized(dev) || !caps) {
return LISA_DEVICE_ERR_INVALID;
}
memset(caps, 0, sizeof(lisa_touch_capabilities_t));
caps->max_x = AXS15231B_MAX_X;
caps->max_y = AXS15231B_MAX_Y;
caps->max_points = AXS15231B_MAX_POINTS;
caps->has_pressure = false;
caps->has_gesture = false;
caps->supported_gestures = 0;
LISA_LOGI(LOG_TAG, "Get capabilities: max_x=%d, max_y=%d, max_points=%d",
caps->max_x, caps->max_y, caps->max_points);
return LISA_DEVICE_OK;
}
/**
* @brief 内部读取触摸事件函数(供任务和 API 调用)
*/
static int axs15231b_touch_read_event_internal(lisa_touch_axs15231b_priv_t *priv, lisa_touch_event_t *event)
{
if (!priv || !event || !priv->enabled) {
return LISA_DEVICE_ERR_INVALID;
}
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
/* 读取触摸坐标 */
uint16_t x, y;
bool pressed;
int ret = axs15231b_read_coordinates(priv->i2c_dev, &x, &y, &pressed);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
/* 过滤异常报点:滑动过程中偶现异常报点 */
if (priv->last_pressed && pressed) {
if (x <= 1 || y <= 1) {
DEVICE_UNLOCK(priv);
return LISA_DEVICE_ERR_IO;
}
}
/* 填充事件结构体 */
memset(event, 0, sizeof(lisa_touch_event_t));
if (pressed) {
event->type = LISA_TOUCH_EVENT_PRESS;
event->point_count = 1;
event->points[0].id = 0;
event->points[0].x = x;
event->points[0].y = y;
event->points[0].state = LISA_TOUCH_POINT_PRESSED;
event->points[0].pressure = 0;
event->points[0].area = 0;
LISA_LOGD(LOG_TAG, "Touch event: x=%d, y=%d", x, y);
} else {
event->type = LISA_TOUCH_EVENT_RELEASE;
event->point_count = 0;
}
priv->last_pressed = pressed;
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 读取触摸事件(API 接口)
*/
static int axs15231b_touch_read_event(lisa_device_t *dev, lisa_touch_event_t *event)
{
if (!lisa_device_is_initialized(dev) || !event) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_axs15231b_priv_t *priv = (lisa_touch_axs15231b_priv_t *)dev->priv_data;
/* 中断模式下禁止轮询读取,避免与中断任务产生资源竞争 */
if (priv->interrupt_mode_active) {
LISA_LOGW(LOG_TAG, "Cannot read_event in interrupt mode, use callback instead");
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
return axs15231b_touch_read_event_internal(priv, event);
}
/**
* @brief 启用触摸设备
*/
static int axs15231b_touch_enable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_axs15231b_priv_t *priv = (lisa_touch_axs15231b_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = true;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "AXS15231B touch device enabled");
return LISA_DEVICE_OK;
}
/**
* @brief 禁用触摸设备
*/
static int axs15231b_touch_disable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_axs15231b_priv_t *priv = (lisa_touch_axs15231b_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = false;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "AXS15231B touch device disabled");
return LISA_DEVICE_OK;
}
/**
* @brief 附加触摸总线接口
*/
static int axs15231b_touch_attach_bus(lisa_device_t *dev, const lisa_touch_bus_config_t *bus_config)
{
if (!lisa_device_is_initialized(dev) || !bus_config) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_axs15231b_priv_t *priv = (lisa_touch_axs15231b_priv_t *)dev->priv_data;
if (bus_config->bus_type != LISA_TOUCH_BUS_I2C) {
LISA_LOGE(LOG_TAG, "AXS15231B only supports I2C bus");
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
DEVICE_LOCK(priv);
/* 保存总线配置 */
priv->i2c_dev = bus_config->config.i2c.i2c_dev;
priv->int_gpio = bus_config->config.i2c.int_gpio;
priv->rst_gpio = bus_config->config.i2c.rst_gpio;
priv->int_pin = bus_config->config.i2c.int_pin;
priv->rst_pin = bus_config->config.i2c.rst_pin;
/* 检查必需的设备 */
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "I2C device not ready");
return LISA_DEVICE_ERR_NOT_READY;
}
/* 自动配置 I2C 总线(标准速度,主机模式) */
lisa_i2c_config_t i2c_config = {
.speed = LISA_I2C_SPEED_STANDARD,
.master_mode = true,
.slave_addr = 0,
};
int ret = lisa_i2c_configure(priv->i2c_dev, &i2c_config);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "I2C configuration failed: %d", ret);
return ret;
}
LISA_LOGI(LOG_TAG, "I2C auto-configured (speed: %d Hz)", i2c_config.speed);
/* 硬件复位芯片 */
ret = axs15231b_hardware_reset(priv);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
/* 配置中断引脚(如果提供) */
if (priv->int_gpio && lisa_device_ready(priv->int_gpio)) {
ret = lisa_gpio_configure(priv->int_gpio, priv->int_pin,
LISA_GPIO_INPUT | LISA_GPIO_PULL_UP);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to configure interrupt pin: %d", ret);
return ret;
}
LISA_LOGI(LOG_TAG, "AXS15231B interrupt pin configured");
}
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "AXS15231B touch bus attached successfully");
return LISA_DEVICE_OK;
}
/**
* @brief 设置触摸事件回调函数
*/
static int axs15231b_touch_set_callback(lisa_device_t *dev, lisa_touch_callback_t callback, void *user_data)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_axs15231b_priv_t *priv = (lisa_touch_axs15231b_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->callback = callback;
priv->callback_user_data = user_data;
DEVICE_UNLOCK(priv);
LISA_LOGD(LOG_TAG, "AXS15231B touch callback %s", callback ? "set" : "cleared");
return LISA_DEVICE_OK;
}
/**
* @brief 设置中断模式
*/
static int axs15231b_touch_set_int_mode(lisa_device_t *dev, lisa_touch_int_mode_t mode)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_axs15231b_priv_t *priv = (lisa_touch_axs15231b_priv_t *)dev->priv_data;
if (!priv->int_gpio || !lisa_device_ready(priv->int_gpio)) {
LISA_LOGW(LOG_TAG, "Interrupt GPIO not available");
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
int ret = LISA_DEVICE_OK;
if (mode == LISA_TOUCH_INT_MODE_INTERRUPT) {
/* 如果任务不存在,创建读取任务 */
if (priv->read_task_handle == NULL) {
/* 使用 Kconfig 配置的任务优先级 */
UBaseType_t task_priority = tskIDLE_PRIORITY + CONFIG_LISA_TOUCH_ARCS_AXS15231B_READ_TASK_PRIORITY;
xTaskCreate(axs15231b_read_task, "axs15231b_read", 1024, priv,
task_priority, &priv->read_task_handle);
if (priv->read_task_handle == NULL) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to create read task");
return LISA_DEVICE_ERR_INIT_FAIL;
}
LISA_LOGI(LOG_TAG, "AXS15231B read task created with priority %d", task_priority);
}
/* 配置并启用中断 (下降沿触发) */
ret = lisa_gpio_configure_irq(priv->int_gpio, priv->int_pin,
LISA_GPIO_IRQ_EDGE_FALLING,
axs15231b_gpio_irq_callback, priv);
if (ret == LISA_DEVICE_OK) {
ret = lisa_gpio_enable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
priv->interrupt_mode_active = true;
LISA_LOGI(LOG_TAG, "AXS15231B interrupt mode enabled");
}
}
} else {
/* 禁用中断 */
priv->interrupt_mode_active = false;
ret = lisa_gpio_disable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
LISA_LOGI(LOG_TAG, "AXS15231B interrupt mode disabled (polling mode)");
}
/* 注意:不删除任务,以便下次切换回中断模式时重用 */
}
DEVICE_UNLOCK(priv);
return ret;
}
/* ===== AXS15231B Touch API 实例 ===== */
static const lisa_touch_api_t axs15231b_touch_api = {
.get_capabilities = axs15231b_touch_get_capabilities,
.read_event = axs15231b_touch_read_event,
.enable = axs15231b_touch_enable,
.disable = axs15231b_touch_disable,
.attach_bus = axs15231b_touch_attach_bus,
.set_callback = axs15231b_touch_set_callback,
.set_int_mode = axs15231b_touch_set_int_mode,
};
/* ===== 设备初始化函数 ===== */
static int lisa_touch_axs15231b_init(void)
{
memset(&touch_axs15231b_priv, 0, sizeof(lisa_touch_axs15231b_priv_t));
touch_axs15231b_priv.mutex = lisa_mutex_create();
if (!touch_axs15231b_priv.mutex) {
LISA_LOGE(LOG_TAG, "Failed to create mutex");
return LISA_DEVICE_ERR_INIT_FAIL;
}
touch_axs15231b_priv.enabled = false;
touch_axs15231b_priv.initialized = true;
touch_axs15231b_priv.last_pressed = false;
LISA_LOGI(LOG_TAG, "AXS15231B touch device initialized successfully");
return LISA_DEVICE_OK;
}
/* ===== 设备注册 ===== */
LISA_DEVICE_REGISTER(touch_axs15231b, /* 设备名称 */
&axs15231b_touch_api, /* API指针 */
&touch_axs15231b_priv, /* 私有数据指针 */
NULL, /* 用户数据 */
lisa_touch_axs15231b_init, /* 初始化函数 */
LISA_DEVICE_PRIORITY_NORMAL); /* 优先级 */

View File

@@ -0,0 +1,479 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file lisa_touch_bl6133.c
* @brief BL6133 触摸芯片驱动 - LISA Touch 设备实现
*/
#include "lisa_touch.h"
#include "lisa_i2c.h"
#include "lisa_gpio.h"
#include "lisa_thread.h"
#include <stddef.h>
#include <string.h>
#include <lisa_mutex.h>
#include "FreeRTOS.h"
#include "task.h"
#include "portmacro.h"
#define LOG_TAG "lisa_touch_bl6133"
#include <lisa_log.h>
/* BL6133 I2C 地址 */
#define BL6133_I2C_SLAVE_ADDRESS 0x2C
/* BL6133 寄存器定义 */
#define BL6XXX_POINT_REG 0x01
/* BL6133 触摸事件类型 */
enum {
BL6XXX_TOUCH_EVENT_DOWN,
BL6XXX_TOUCH_EVENT_UP,
BL6XXX_TOUCH_EVENT_CONTACT_OR_MOVE,
BL6XXX_TOUCH_EVENT_NONE,
};
/* BL6133 触摸能力 */
#define BL6133_MAX_X 320
#define BL6133_MAX_Y 386
#define BL6133_MAX_POINTS 1
/* 自动判断上下文的互斥锁宏(lisa_mutex_lock/unlock 已自动判断上下文) */
#define DEVICE_LOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_lock(priv->mutex, LISA_OS_WAIT_FOREVER); \
} \
} while (0)
#define DEVICE_UNLOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_unlock(priv->mutex); \
} \
} while (0)
/* ===== BL6133 设备私有数据 ===== */
typedef struct {
lisa_device_t *i2c_dev; /* I2C 设备指针 */
lisa_device_t *int_gpio; /* 中断 GPIO 设备指针 */
lisa_device_t *rst_gpio; /* 复位 GPIO 设备指针 */
uint32_t int_pin; /* 中断引脚号 */
uint32_t rst_pin; /* 复位引脚号 */
lisa_touch_callback_t callback; /* 触摸事件回调函数 */
void *callback_user_data; /* 回调用户数据 */
lisa_mutex_t *mutex; /* 互斥锁 */
bool initialized; /* 初始化标志 */
bool enabled; /* 使能标志 */
TaskHandle_t read_task_handle; /* I2C 读取任务句柄(用于中断模式) */
bool interrupt_mode_active; /* 中断模式是否激活 */
} lisa_touch_bl6133_priv_t;
/* ===== BL6133 设备静态实例 ===== */
static lisa_touch_bl6133_priv_t touch_bl6133_priv;
/* ===== 内部辅助函数 ===== */
/* 前向声明 */
static int bl6133_touch_read_event_internal(lisa_touch_bl6133_priv_t *priv, lisa_touch_event_t *event);
/**
* @brief 读取 BL6133 寄存器
*/
static int bl6133_read_reg(lisa_device_t *i2c_dev, uint8_t reg, uint8_t *data, uint32_t len)
{
if (!i2c_dev || !data || len == 0) {
return LISA_DEVICE_ERR_INVALID;
}
/* BL6133 使用 8 位寄存器地址 */
lisa_i2c_msg_t msgs[2] = {
{.addr = BL6133_I2C_SLAVE_ADDRESS, .flags = LISA_I2C_FLAG_NONE, .len = 1, .buf = &reg},
{.addr = BL6133_I2C_SLAVE_ADDRESS, .flags = LISA_I2C_FLAG_READ, .len = len, .buf = data}
};
int ret = lisa_i2c_transfer(i2c_dev, msgs, 2);
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "I2C transfer failed: %d", ret);
return ret;
}
return LISA_DEVICE_OK;
}
/**
* @brief I2C 读取任务(用于中断模式)
* @note 此任务在中断模式下运行,等待任务通知后读取 I2C 数据
*/
static void bl6133_read_task(void *pvParameters)
{
lisa_touch_bl6133_priv_t *priv = (lisa_touch_bl6133_priv_t *)pvParameters;
while (1) {
/* 等待任务通知(中断会发送通知)
* 使用 pdFALSE 清除所有累积的通知,避免重复处理
*/
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
if (!priv->enabled || !priv->interrupt_mode_active) {
continue;
}
/* 在任务上下文中读取完整的触摸数据 */
lisa_touch_event_t event = {0};
int ret = bl6133_touch_read_event_internal(priv, &event);
if (ret == LISA_DEVICE_OK && priv->callback) {
/* 通过回调函数返回完整数据 */
priv->callback(&event, priv->callback_user_data);
}
}
}
/**
* @brief GPIO 中断回调
*/
static void bl6133_gpio_irq_callback(uint32_t pin, void *user_data)
{
(void)pin;
lisa_touch_bl6133_priv_t *priv = (lisa_touch_bl6133_priv_t *)user_data;
if (!priv || !priv->interrupt_mode_active) {
return;
}
/* 在中断上下文中发送任务通知,唤醒读取任务 */
if (priv->read_task_handle != NULL) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(priv->read_task_handle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
/**
* @brief 硬件复位芯片
*/
static int bl6133_hardware_reset(lisa_touch_bl6133_priv_t *priv)
{
if (!priv->rst_gpio || !lisa_device_ready(priv->rst_gpio)) {
LISA_LOGW(LOG_TAG, "Reset GPIO not available");
return LISA_DEVICE_OK;
}
lisa_gpio_configure(priv->rst_gpio, priv->rst_pin, LISA_GPIO_OUTPUT);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(10);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 0);
lisa_thread_mdelay(100); /* BL6133 需要较长的低电平时间 */
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(300);
LISA_LOGI(LOG_TAG, "BL6133 reset completed");
return LISA_DEVICE_OK;
}
/* ===== LISA Touch API 实现 ===== */
/**
* @brief 获取触摸设备能力
*/
static int bl6133_touch_get_capabilities(lisa_device_t *dev, lisa_touch_capabilities_t *caps)
{
if (!lisa_device_is_initialized(dev) || !caps) {
return LISA_DEVICE_ERR_INVALID;
}
memset(caps, 0, sizeof(lisa_touch_capabilities_t));
caps->max_x = BL6133_MAX_X;
caps->max_y = BL6133_MAX_Y;
caps->max_points = BL6133_MAX_POINTS;
caps->has_pressure = false;
caps->has_gesture = false;
caps->supported_gestures = 0;
return LISA_DEVICE_OK;
}
/**
* @brief 内部读取触摸事件函数(供任务和 API 调用)
*/
static int bl6133_touch_read_event_internal(lisa_touch_bl6133_priv_t *priv, lisa_touch_event_t *event)
{
if (!priv || !event || !priv->enabled) {
return LISA_DEVICE_ERR_INVALID;
}
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
/* 读取触摸点数据(6字节) */
uint8_t buf[6] = {0};
int ret = bl6133_read_reg(priv->i2c_dev, BL6XXX_POINT_REG, buf, sizeof(buf));
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
/*
* 数据格式:
* BYTE0 | gesture_code[7:0]
* BYTE1 | point_number[7:0]
* BYTE2 | event[7:6] | x_high[5:0]
* BYTE3 | x_low[7:0]
* BYTE4 | touch_id[7:4] | y_high[3:0]
* BYTE5 | y_low[7:0]
*/
uint8_t event_type = (buf[2] >> 6) & 0x03;
bool pressed = (event_type == BL6XXX_TOUCH_EVENT_CONTACT_OR_MOVE) ? true : false;
/* 注意:该芯片在触摸时总是输出 event 2 (CONTACT_OR_MOVE),而不是 event 0 (DOWN) */
uint16_t x = (((uint16_t)(buf[2] & 0x3F)) << 8) | buf[3];
uint16_t y = (((uint16_t)(buf[4] & 0x0F)) << 8) | buf[5];
uint8_t touch_id = (buf[4] >> 4) & 0x0F;
memset(event, 0, sizeof(lisa_touch_event_t));
if (pressed) {
event->type = LISA_TOUCH_EVENT_PRESS;
event->point_count = 1;
event->points[0].id = (buf[4] >> 4) & 0x0F;
event->points[0].x = x;
event->points[0].y = y;
event->points[0].state = LISA_TOUCH_POINT_PRESSED;
LISA_LOGD(LOG_TAG, "Touch: x=%d, y=%d, id=%d", x, y, event->points[0].id);
} else {
event->type = LISA_TOUCH_EVENT_RELEASE;
event->point_count = 0;
}
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 读取触摸事件(API 接口)
*/
static int bl6133_touch_read_event(lisa_device_t *dev, lisa_touch_event_t *event)
{
if (!lisa_device_is_initialized(dev) || !event) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_bl6133_priv_t *priv = (lisa_touch_bl6133_priv_t *)dev->priv_data;
/* 中断模式下禁止轮询读取,避免与中断任务产生资源竞争 */
if (priv->interrupt_mode_active) {
LISA_LOGW(LOG_TAG, "Cannot read_event in interrupt mode, use callback instead");
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
return bl6133_touch_read_event_internal(priv, event);
}
/**
* @brief 启用触摸设备
*/
static int bl6133_touch_enable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_bl6133_priv_t *priv = (lisa_touch_bl6133_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = true;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "BL6133 touch device enabled");
return LISA_DEVICE_OK;
}
/**
* @brief 禁用触摸设备
*/
static int bl6133_touch_disable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_bl6133_priv_t *priv = (lisa_touch_bl6133_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = false;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "BL6133 touch device disabled");
return LISA_DEVICE_OK;
}
/**
* @brief 附加触摸总线接口
*/
static int bl6133_touch_attach_bus(lisa_device_t *dev, const lisa_touch_bus_config_t *bus_config)
{
if (!lisa_device_is_initialized(dev) || !bus_config) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_bl6133_priv_t *priv = (lisa_touch_bl6133_priv_t *)dev->priv_data;
if (bus_config->bus_type != LISA_TOUCH_BUS_I2C) {
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
DEVICE_LOCK(priv);
priv->i2c_dev = bus_config->config.i2c.i2c_dev;
priv->int_gpio = bus_config->config.i2c.int_gpio;
priv->rst_gpio = bus_config->config.i2c.rst_gpio;
priv->int_pin = bus_config->config.i2c.int_pin;
priv->rst_pin = bus_config->config.i2c.rst_pin;
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
DEVICE_UNLOCK(priv);
return LISA_DEVICE_ERR_NOT_READY;
}
lisa_i2c_config_t i2c_config = {
.speed = LISA_I2C_SPEED_STANDARD,
.master_mode = true,
.slave_addr = 0,
};
int ret = lisa_i2c_configure(priv->i2c_dev, &i2c_config);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
ret = bl6133_hardware_reset(priv);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
if (priv->int_gpio && lisa_device_ready(priv->int_gpio)) {
lisa_gpio_configure(priv->int_gpio, priv->int_pin, LISA_GPIO_INPUT | LISA_GPIO_PULL_UP);
}
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "BL6133 touch bus attached");
return LISA_DEVICE_OK;
}
/**
* @brief 设置触摸事件回调函数
*/
static int bl6133_touch_set_callback(lisa_device_t *dev, lisa_touch_callback_t callback, void *user_data)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_bl6133_priv_t *priv = (lisa_touch_bl6133_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->callback = callback;
priv->callback_user_data = user_data;
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 设置中断模式
*/
static int bl6133_touch_set_int_mode(lisa_device_t *dev, lisa_touch_int_mode_t mode)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_bl6133_priv_t *priv = (lisa_touch_bl6133_priv_t *)dev->priv_data;
if (!priv->int_gpio || !lisa_device_ready(priv->int_gpio)) {
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
int ret = LISA_DEVICE_OK;
if (mode == LISA_TOUCH_INT_MODE_INTERRUPT) {
/* 如果任务不存在,创建读取任务 */
if (priv->read_task_handle == NULL) {
/* 使用 Kconfig 配置的任务优先级 */
UBaseType_t task_priority = tskIDLE_PRIORITY + CONFIG_LISA_TOUCH_ARCS_BL6133_READ_TASK_PRIORITY;
xTaskCreate(bl6133_read_task, "bl6133_read", 1024, priv,
task_priority, &priv->read_task_handle);
if (priv->read_task_handle == NULL) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to create read task");
return LISA_DEVICE_ERR_INIT_FAIL;
}
LISA_LOGI(LOG_TAG, "BL6133 read task created with priority %d", task_priority);
}
ret = lisa_gpio_configure_irq(priv->int_gpio, priv->int_pin,
LISA_GPIO_IRQ_EDGE_FALLING,
bl6133_gpio_irq_callback, priv);
if (ret == LISA_DEVICE_OK) {
ret = lisa_gpio_enable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
priv->interrupt_mode_active = true;
LISA_LOGI(LOG_TAG, "BL6133 interrupt mode enabled");
}
}
} else {
/* 禁用中断 */
priv->interrupt_mode_active = false;
ret = lisa_gpio_disable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
LISA_LOGI(LOG_TAG, "BL6133 interrupt mode disabled (polling mode)");
}
/* 注意:不删除任务,以便下次切换回中断模式时重用 */
}
DEVICE_UNLOCK(priv);
return ret;
}
/* ===== BL6133 Touch API 实例 ===== */
static const lisa_touch_api_t bl6133_api = {
.get_capabilities = bl6133_touch_get_capabilities,
.read_event = bl6133_touch_read_event,
.enable = bl6133_touch_enable,
.disable = bl6133_touch_disable,
.attach_bus = bl6133_touch_attach_bus,
.set_callback = bl6133_touch_set_callback,
.set_int_mode = bl6133_touch_set_int_mode,
};
/* ===== 设备初始化函数 ===== */
static int lisa_touch_bl6133_init(void)
{
memset(&touch_bl6133_priv, 0, sizeof(lisa_touch_bl6133_priv_t));
touch_bl6133_priv.mutex = lisa_mutex_create();
if (!touch_bl6133_priv.mutex) {
return LISA_DEVICE_ERR_INIT_FAIL;
}
touch_bl6133_priv.enabled = false;
touch_bl6133_priv.initialized = true;
LISA_LOGI(LOG_TAG, "BL6133 touch device initialized");
return LISA_DEVICE_OK;
}
/* ===== 设备注册 ===== */
LISA_DEVICE_REGISTER(touch_bl6133,
&bl6133_api,
&touch_bl6133_priv,
NULL,
lisa_touch_bl6133_init,
LISA_DEVICE_PRIORITY_NORMAL);

View File

@@ -0,0 +1,528 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file lisa_touch_cst328.c
* @brief CST328 触摸芯片驱动 - LISA Touch 设备实现
*/
#include "lisa_touch.h"
#include "lisa_i2c.h"
#include "lisa_gpio.h"
#include "lisa_thread.h"
#include <stddef.h>
#include <string.h>
#include <lisa_mutex.h>
#include "FreeRTOS.h"
#include "task.h"
#include "portmacro.h"
#define LOG_TAG "lisa_touch_cst328"
#include <lisa_log.h>
/* CST328 I2C 地址 */
#define CST328_I2C_SLAVE_ADDRESS 0x1D
/* CST328 寄存器定义 */
#define CST328_TOUCH_INFO_REG 0xD000
#define CST328_STATUS_PRESSED 0x06
/* CST328 触摸能力 */
#define CST328_MAX_X 240
#define CST328_MAX_Y 320
#define CST328_MAX_POINTS 1
/* 自动判断上下文的互斥锁宏(lisa_mutex_lock/unlock 已自动判断上下文) */
#define DEVICE_LOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_lock(priv->mutex, LISA_OS_WAIT_FOREVER); \
} \
} while (0)
#define DEVICE_UNLOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_unlock(priv->mutex); \
} \
} while (0)
/* ===== CST328 设备私有数据 ===== */
typedef struct {
lisa_device_t *i2c_dev; /* I2C 设备指针 */
lisa_device_t *int_gpio; /* 中断 GPIO 设备指针 */
lisa_device_t *rst_gpio; /* 复位 GPIO 设备指针 */
uint32_t int_pin; /* 中断引脚号 */
uint32_t rst_pin; /* 复位引脚号 */
lisa_touch_callback_t callback; /* 触摸事件回调函数 */
void *callback_user_data; /* 回调用户数据 */
lisa_mutex_t *mutex; /* 互斥锁 */
bool initialized; /* 初始化标志 */
bool enabled; /* 使能标志 */
bool interrupt_mode_active; /* 中断模式是否激活 */
TaskHandle_t read_task_handle; /* I2C 读取任务句柄(用于中断模式) */
} lisa_touch_cst328_priv_t;
/* ===== CST328 设备静态实例 ===== */
static lisa_touch_cst328_priv_t touch_cst328_priv;
/* ===== 内部辅助函数 ===== */
/* 前向声明 */
static int cst328_touch_read_event_internal(lisa_touch_cst328_priv_t *priv, lisa_touch_event_t *event);
/**
* @brief 读取 CST328 寄存器
*/
static int cst328_read_reg(lisa_device_t *i2c_dev, uint16_t reg, uint8_t *data, uint32_t len)
{
if (!i2c_dev || !data || len == 0) {
return LISA_DEVICE_ERR_INVALID;
}
/* 准备寄存器地址(小端序) */
uint8_t reg_buf[2];
reg_buf[0] = (uint8_t)(reg & 0xFF); /* 低字节 */
reg_buf[1] = (uint8_t)((reg >> 8) & 0xFF); /* 高字节 */
/* 构造 I2C 消息:写寄存器地址,然后读数据 */
lisa_i2c_msg_t msgs[2] = {
{
.addr = CST328_I2C_SLAVE_ADDRESS,
.flags = LISA_I2C_FLAG_NONE, /* 写 */
.len = sizeof(reg_buf),
.buf = reg_buf,
},
{
.addr = CST328_I2C_SLAVE_ADDRESS,
.flags = LISA_I2C_FLAG_READ, /* 读 */
.len = len,
.buf = data,
}
};
/* 执行组合传输 */
int ret = lisa_i2c_transfer(i2c_dev, msgs, 2);
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "I2C transfer failed: %d", ret);
return ret;
}
return LISA_DEVICE_OK;
}
/**
* @brief I2C 读取任务(用于中断模式)
* @note 此任务在中断模式下运行,等待任务通知后读取 I2C 数据
* 因为 I2C 驱动使用 vTaskDelay,不能在中断上下文中调用
*/
static void cst328_read_task(void *pvParameters)
{
lisa_touch_cst328_priv_t *priv = (lisa_touch_cst328_priv_t *)pvParameters;
while (1) {
/* 等待任务通知(中断会发送通知)
* 使用 pdFALSE 清除所有累积的通知,避免重复处理
*/
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
if (!priv->enabled || !priv->interrupt_mode_active) {
continue;
}
/* 在任务上下文中读取完整的触摸数据 */
lisa_touch_event_t event = {0};
int ret = cst328_touch_read_event_internal(priv, &event);
/* 直接调用回调,去重逻辑由应用层处理 */
if (ret == LISA_DEVICE_OK && priv->callback) {
priv->callback(&event, priv->callback_user_data);
}
}
}
/**
* @brief GPIO 中断回调
* @note 在中断模式下,此函数只发送任务通知,不执行 I2C 读取
* 因为 I2C 驱动使用 vTaskDelay,不能在中断上下文中调用
*/
static void cst328_gpio_irq_callback(uint32_t pin, void *user_data)
{
(void)pin;
lisa_touch_cst328_priv_t *priv = (lisa_touch_cst328_priv_t *)user_data;
if (!priv || !priv->interrupt_mode_active) {
return;
}
/* 在中断上下文中发送任务通知,唤醒读取任务 */
if (priv->read_task_handle != NULL) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(priv->read_task_handle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
/**
* @brief 硬件复位芯片
*/
static int cst328_hardware_reset(lisa_touch_cst328_priv_t *priv)
{
if (!priv->rst_gpio || !lisa_device_ready(priv->rst_gpio)) {
LISA_LOGW(LOG_TAG, "Reset GPIO not available, skip reset");
return LISA_DEVICE_OK;
}
/* 配置复位引脚为输出 */
int ret = lisa_gpio_configure(priv->rst_gpio, priv->rst_pin, LISA_GPIO_OUTPUT);
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "Failed to configure reset pin: %d", ret);
return ret;
}
/* 复位序列:高->低->高 */
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(10);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 0);
lisa_thread_mdelay(10);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(200);
LISA_LOGI(LOG_TAG, "CST328 hardware reset completed");
return LISA_DEVICE_OK;
}
/* ===== LISA Touch API 实现 ===== */
/**
* @brief 获取触摸设备能力
*/
static int cst328_touch_get_capabilities(lisa_device_t *dev, lisa_touch_capabilities_t *caps)
{
if (!lisa_device_is_initialized(dev) || !caps) {
return LISA_DEVICE_ERR_INVALID;
}
memset(caps, 0, sizeof(lisa_touch_capabilities_t));
caps->max_x = CST328_MAX_X;
caps->max_y = CST328_MAX_Y;
caps->max_points = CST328_MAX_POINTS;
caps->has_pressure = false;
caps->has_gesture = false;
caps->supported_gestures = 0;
LISA_LOGI(LOG_TAG, "Get capabilities: max_x=%d, max_y=%d, max_points=%d",
caps->max_x, caps->max_y, caps->max_points);
return LISA_DEVICE_OK;
}
/**
* @brief 内部读取触摸事件函数(供任务和 API 调用)
*/
static int cst328_touch_read_event_internal(lisa_touch_cst328_priv_t *priv, lisa_touch_event_t *event)
{
if (!priv || !event || !priv->enabled) {
return LISA_DEVICE_ERR_INVALID;
}
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
/* 读取触摸数据寄存器 */
uint8_t read_buf[4] = {0};
int ret = cst328_read_reg(priv->i2c_dev, CST328_TOUCH_INFO_REG, read_buf, sizeof(read_buf));
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to read touch register: %d", ret);
return ret;
}
/* 解析触摸状态 */
bool pressed = ((read_buf[0] & 0x0F) == CST328_STATUS_PRESSED);
LISA_LOGD(LOG_TAG, "Raw data: [0]=0x%02X [1]=0x%02X [2]=0x%02X [3]=0x%02X, pressed=%d",
read_buf[0], read_buf[1], read_buf[2], read_buf[3], pressed);
/* 解析坐标(组合高低位) */
uint16_t x = (read_buf[1] << 4) | ((read_buf[3] >> 4) & 0x0F);
uint16_t y = (read_buf[2] << 4) | (read_buf[3] & 0x0F);
/* 填充事件结构体 */
memset(event, 0, sizeof(lisa_touch_event_t));
if (pressed) {
event->type = LISA_TOUCH_EVENT_PRESS;
event->point_count = 1;
event->points[0].id = 0;
event->points[0].x = x;
event->points[0].y = y;
event->points[0].state = LISA_TOUCH_POINT_PRESSED;
event->points[0].pressure = 0; /* CST328 不支持压力检测 */
event->points[0].area = 0; /* CST328 不支持面积检测 */
LISA_LOGD(LOG_TAG, "Touch event: x=%d, y=%d", x, y);
} else {
event->type = LISA_TOUCH_EVENT_RELEASE;
event->point_count = 0;
}
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 读取触摸事件(API 接口)
*/
static int cst328_touch_read_event(lisa_device_t *dev, lisa_touch_event_t *event)
{
if (!lisa_device_is_initialized(dev) || !event) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst328_priv_t *priv = (lisa_touch_cst328_priv_t *)dev->priv_data;
/* 中断模式下禁止轮询读取,避免与中断任务产生资源竞争 */
if (priv->interrupt_mode_active) {
LISA_LOGW(LOG_TAG, "Cannot read_event in interrupt mode, use callback instead");
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
return cst328_touch_read_event_internal(priv, event);
}
/**
* @brief 启用触摸设备
*/
static int cst328_touch_enable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst328_priv_t *priv = (lisa_touch_cst328_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = true;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "CST328 touch device enabled");
return LISA_DEVICE_OK;
}
/**
* @brief 禁用触摸设备
*/
static int cst328_touch_disable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst328_priv_t *priv = (lisa_touch_cst328_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = false;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "CST328 touch device disabled");
return LISA_DEVICE_OK;
}
/**
* @brief 附加触摸总线接口
*/
static int cst328_touch_attach_bus(lisa_device_t *dev, const lisa_touch_bus_config_t *bus_config)
{
if (!lisa_device_is_initialized(dev) || !bus_config) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst328_priv_t *priv = (lisa_touch_cst328_priv_t *)dev->priv_data;
if (bus_config->bus_type != LISA_TOUCH_BUS_I2C) {
LISA_LOGE(LOG_TAG, "CST328 only supports I2C bus");
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
DEVICE_LOCK(priv);
/* 保存总线配置 */
priv->i2c_dev = bus_config->config.i2c.i2c_dev;
priv->int_gpio = bus_config->config.i2c.int_gpio;
priv->rst_gpio = bus_config->config.i2c.rst_gpio;
priv->int_pin = bus_config->config.i2c.int_pin;
priv->rst_pin = bus_config->config.i2c.rst_pin;
/* 检查必需的设备 */
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "I2C device not ready");
return LISA_DEVICE_ERR_NOT_READY;
}
/* 自动配置 I2C 总线(标准速度,主机模式) */
lisa_i2c_config_t i2c_config = {
.speed = LISA_I2C_SPEED_STANDARD,
.master_mode = true,
.slave_addr = 0,
};
int ret = lisa_i2c_configure(priv->i2c_dev, &i2c_config);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "I2C configuration failed: %d", ret);
return ret;
}
LISA_LOGI(LOG_TAG, "I2C auto-configured (speed: %d Hz)", i2c_config.speed);
/* 硬件复位芯片 */
ret = cst328_hardware_reset(priv);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
/* 配置中断引脚(如果提供) */
if (priv->int_gpio && lisa_device_ready(priv->int_gpio)) {
ret = lisa_gpio_configure(priv->int_gpio, priv->int_pin,
LISA_GPIO_INPUT | LISA_GPIO_PULL_UP);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to configure interrupt pin: %d", ret);
return ret;
}
LISA_LOGI(LOG_TAG, "CST328 interrupt pin configured");
}
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "CST328 touch bus attached successfully");
return LISA_DEVICE_OK;
}
/**
* @brief 设置触摸事件回调函数
*/
static int cst328_touch_set_callback(lisa_device_t *dev, lisa_touch_callback_t callback, void *user_data)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst328_priv_t *priv = (lisa_touch_cst328_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->callback = callback;
priv->callback_user_data = user_data;
DEVICE_UNLOCK(priv);
LISA_LOGD(LOG_TAG, "CST328 touch callback %s", callback ? "set" : "cleared");
return LISA_DEVICE_OK;
}
/**
* @brief 设置中断模式
*/
static int cst328_touch_set_int_mode(lisa_device_t *dev, lisa_touch_int_mode_t mode)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst328_priv_t *priv = (lisa_touch_cst328_priv_t *)dev->priv_data;
if (!priv->int_gpio || !lisa_device_ready(priv->int_gpio)) {
LISA_LOGW(LOG_TAG, "Interrupt GPIO not available");
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
int ret = LISA_DEVICE_OK;
if (mode == LISA_TOUCH_INT_MODE_INTERRUPT) {
/* 如果任务不存在,创建读取任务 */
if (priv->read_task_handle == NULL) {
/* 使用 Kconfig 配置的任务优先级 */
UBaseType_t task_priority = tskIDLE_PRIORITY + CONFIG_LISA_TOUCH_ARCS_CST328_READ_TASK_PRIORITY;
xTaskCreate(cst328_read_task, "cst328_read", 1024, priv,
task_priority, &priv->read_task_handle);
if (priv->read_task_handle == NULL) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to create read task");
return LISA_DEVICE_ERR_INIT_FAIL;
}
LISA_LOGI(LOG_TAG, "CST328 read task created with priority %d", task_priority);
}
/* 配置并启用中断 */
ret = lisa_gpio_configure_irq(priv->int_gpio, priv->int_pin,
LISA_GPIO_IRQ_EDGE_RISING,
cst328_gpio_irq_callback, priv);
if (ret == LISA_DEVICE_OK) {
ret = lisa_gpio_enable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
priv->interrupt_mode_active = true;
LISA_LOGI(LOG_TAG, "CST328 interrupt mode enabled");
}
}
} else {
/* 禁用中断 */
priv->interrupt_mode_active = false;
ret = lisa_gpio_disable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
LISA_LOGI(LOG_TAG, "CST328 interrupt mode disabled (polling mode)");
}
/* 注意:不删除任务,以便下次切换回中断模式时重用 */
}
DEVICE_UNLOCK(priv);
return ret;
}
/* ===== CST328 Touch API 实例 ===== */
static const lisa_touch_api_t cst328_touch_api = {
.get_capabilities = cst328_touch_get_capabilities,
.read_event = cst328_touch_read_event,
.enable = cst328_touch_enable,
.disable = cst328_touch_disable,
.attach_bus = cst328_touch_attach_bus,
.set_callback = cst328_touch_set_callback,
.set_int_mode = cst328_touch_set_int_mode,
};
/* ===== 设备初始化函数 ===== */
static int lisa_touch_cst328_init(void)
{
memset(&touch_cst328_priv, 0, sizeof(lisa_touch_cst328_priv_t));
touch_cst328_priv.mutex = lisa_mutex_create();
if (!touch_cst328_priv.mutex) {
LISA_LOGE(LOG_TAG, "Failed to create mutex");
return LISA_DEVICE_ERR_INIT_FAIL;
}
touch_cst328_priv.enabled = false;
touch_cst328_priv.initialized = true;
LISA_LOGI(LOG_TAG, "CST328 touch device initialized successfully");
return LISA_DEVICE_OK;
}
/* ===== 设备注册 ===== */
LISA_DEVICE_REGISTER(touch_cst328, /* 设备名称 */
&cst328_touch_api, /* API指针 */
&touch_cst328_priv, /* 私有数据指针 */
NULL, /* 用户数据 */
lisa_touch_cst328_init, /* 初始化函数 */
LISA_DEVICE_PRIORITY_NORMAL); /* 优先级 */

View File

@@ -0,0 +1,492 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file lisa_touch_cst816d.c
* @brief CST816D 触摸芯片驱动 - LISA Touch 设备实现
*/
#include "lisa_touch.h"
#include "lisa_i2c.h"
#include "lisa_gpio.h"
#include "lisa_thread.h"
#include <stddef.h>
#include <string.h>
#include <lisa_mutex.h>
#include "FreeRTOS.h"
#include "task.h"
#include "portmacro.h"
#define LOG_TAG "lisa_touch_cst816d"
#include <lisa_log.h>
/* CST816D I2C 地址 */
#define CST816D_I2C_SLAVE_ADDRESS 0x15
/* CST816D 寄存器定义 */
#define CST816D_TOUCH_INFO_REG 0xD000
/* CST816D 触摸能力 */
#define CST816D_MAX_X 240
#define CST816D_MAX_Y 240
#define CST816D_MAX_POINTS 1
/* 自动判断上下文的互斥锁宏(支持中断和线程上下文) */
/* 自动判断上下文的互斥锁宏(lisa_mutex_lock/unlock 已自动判断上下文) */
#define DEVICE_LOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_lock(priv->mutex, LISA_OS_WAIT_FOREVER); \
} \
} while (0)
#define DEVICE_UNLOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_unlock(priv->mutex); \
} \
} while (0)
/* ===== CST816D 设备私有数据 ===== */
typedef struct {
lisa_device_t *i2c_dev; /* I2C 设备指针 */
lisa_device_t *int_gpio; /* 中断 GPIO 设备指针 */
lisa_device_t *rst_gpio; /* 复位 GPIO 设备指针 */
uint32_t int_pin; /* 中断引脚号 */
uint32_t rst_pin; /* 复位引脚号 */
lisa_touch_callback_t callback; /* 触摸事件回调函数 */
void *callback_user_data; /* 回调用户数据 */
lisa_mutex_t *mutex; /* 互斥锁 */
bool initialized; /* 初始化标志 */
bool enabled; /* 使能标志 */
TaskHandle_t read_task_handle; /* I2C 读取任务句柄(用于中断模式) */
bool interrupt_mode_active; /* 中断模式是否激活 */
} lisa_touch_cst816d_priv_t;
/* ===== CST816D 设备静态实例 ===== */
static lisa_touch_cst816d_priv_t touch_cst816d_priv;
/* ===== 内部辅助函数 ===== */
/* 前向声明 */
static int cst816d_touch_read_event_internal(lisa_touch_cst816d_priv_t *priv, lisa_touch_event_t *event);
/**
* @brief 读取 CST816D 寄存器
*/
static int cst816d_read_reg(lisa_device_t *i2c_dev, uint16_t reg, uint8_t *data, uint32_t len)
{
if (!i2c_dev || !data || len == 0) {
return LISA_DEVICE_ERR_INVALID;
}
/* 准备寄存器地址(小端序:低字节在前) */
uint8_t reg_buf[2] = {(uint8_t)(reg & 0xFF), (uint8_t)((reg >> 8) & 0xFF)};
/* 构造 I2C 消息:先写寄存器地址,再读数据 */
lisa_i2c_msg_t msgs[2] = {
{.addr = CST816D_I2C_SLAVE_ADDRESS, .flags = LISA_I2C_FLAG_NONE, .len = sizeof(reg_buf), .buf = reg_buf},
{.addr = CST816D_I2C_SLAVE_ADDRESS, .flags = LISA_I2C_FLAG_READ, .len = len, .buf = data}
};
int ret = lisa_i2c_transfer(i2c_dev, msgs, 2);
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "I2C transfer failed: %d", ret);
return ret;
}
return LISA_DEVICE_OK;
}
/**
* @brief I2C 读取任务(用于中断模式)
* @note 此任务在中断模式下运行,等待任务通知后读取 I2C 数据
*/
static void cst816d_read_task(void *pvParameters)
{
lisa_touch_cst816d_priv_t *priv = (lisa_touch_cst816d_priv_t *)pvParameters;
while (1) {
/* 等待任务通知(中断会发送通知)
* 使用 pdFALSE 清除所有累积的通知,避免重复处理
*/
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
if (!priv->enabled || !priv->interrupt_mode_active) {
continue;
}
/* 在任务上下文中读取完整的触摸数据 */
lisa_touch_event_t event = {0};
int ret = cst816d_touch_read_event_internal(priv, &event);
if (ret == LISA_DEVICE_OK && priv->callback) {
/* 通过回调函数返回完整数据 */
priv->callback(&event, priv->callback_user_data);
}
}
}
/**
* @brief GPIO 中断回调
*/
static void cst816d_gpio_irq_callback(uint32_t pin, void *user_data)
{
(void)pin;
lisa_touch_cst816d_priv_t *priv = (lisa_touch_cst816d_priv_t *)user_data;
if (!priv || !priv->interrupt_mode_active) {
return;
}
/* 在中断上下文中发送任务通知,唤醒读取任务 */
if (priv->read_task_handle != NULL) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(priv->read_task_handle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
/**
* @brief 硬件复位芯片
*/
static int cst816d_hardware_reset(lisa_touch_cst816d_priv_t *priv)
{
if (!priv->rst_gpio || !lisa_device_ready(priv->rst_gpio)) {
LISA_LOGW(LOG_TAG, "Reset GPIO not available, skip reset");
return LISA_DEVICE_OK;
}
/* 配置复位引脚为输出 */
int ret = lisa_gpio_configure(priv->rst_gpio, priv->rst_pin, LISA_GPIO_OUTPUT);
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "Failed to configure reset pin: %d", ret);
return ret;
}
/* 复位序列:高->低->高 */
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(20);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 0);
lisa_thread_mdelay(20);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(400);
LISA_LOGI(LOG_TAG, "CST816D hardware reset completed");
return LISA_DEVICE_OK;
}
/* ===== LISA Touch API 实现 ===== */
/**
* @brief 获取触摸设备能力
*/
static int cst816d_touch_get_capabilities(lisa_device_t *dev, lisa_touch_capabilities_t *caps)
{
if (!lisa_device_is_initialized(dev) || !caps) {
return LISA_DEVICE_ERR_INVALID;
}
memset(caps, 0, sizeof(lisa_touch_capabilities_t));
caps->max_x = CST816D_MAX_X;
caps->max_y = CST816D_MAX_Y;
caps->max_points = CST816D_MAX_POINTS;
caps->has_pressure = false;
caps->has_gesture = false;
caps->supported_gestures = 0;
return LISA_DEVICE_OK;
}
/**
* @brief 内部读取触摸事件函数(供任务和 API 调用)
*/
static int cst816d_touch_read_event_internal(lisa_touch_cst816d_priv_t *priv, lisa_touch_event_t *event)
{
if (!priv || !event || !priv->enabled) {
return LISA_DEVICE_ERR_INVALID;
}
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
/* 读取触摸信息寄存器(6字节) */
uint8_t read_buf[6] = {0};
int ret = cst816d_read_reg(priv->i2c_dev, CST816D_TOUCH_INFO_REG, read_buf, sizeof(read_buf));
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
/* 解析触摸状态和坐标 */
bool pressed = (read_buf[0] != 0);
uint16_t x = ((read_buf[2] & 0x0F) << 8) | read_buf[3];
uint16_t y = ((read_buf[4] & 0x0F) << 8) | read_buf[5];
/* 填充事件结构体 */
memset(event, 0, sizeof(lisa_touch_event_t));
if (pressed) {
event->type = LISA_TOUCH_EVENT_PRESS;
event->point_count = 1;
event->points[0].id = 0;
event->points[0].x = x;
event->points[0].y = y;
event->points[0].state = LISA_TOUCH_POINT_PRESSED;
LISA_LOGD(LOG_TAG, "Touch: x=%d, y=%d", x, y);
} else {
event->type = LISA_TOUCH_EVENT_RELEASE;
event->point_count = 0;
}
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 读取触摸事件(API 接口)
*/
static int cst816d_touch_read_event(lisa_device_t *dev, lisa_touch_event_t *event)
{
if (!lisa_device_is_initialized(dev) || !event) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst816d_priv_t *priv = (lisa_touch_cst816d_priv_t *)dev->priv_data;
/* 中断模式下禁止轮询读取,避免与中断任务产生资源竞争 */
if (priv->interrupt_mode_active) {
LISA_LOGW(LOG_TAG, "Cannot read_event in interrupt mode, use callback instead");
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
return cst816d_touch_read_event_internal(priv, event);
}
/**
* @brief 启用触摸设备
*/
static int cst816d_touch_enable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst816d_priv_t *priv = (lisa_touch_cst816d_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = true;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "CST816D touch device enabled");
return LISA_DEVICE_OK;
}
/**
* @brief 禁用触摸设备
*/
static int cst816d_touch_disable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst816d_priv_t *priv = (lisa_touch_cst816d_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = false;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "CST816D touch device disabled");
return LISA_DEVICE_OK;
}
/**
* @brief 附加触摸总线接口
*/
static int cst816d_touch_attach_bus(lisa_device_t *dev, const lisa_touch_bus_config_t *bus_config)
{
if (!lisa_device_is_initialized(dev) || !bus_config) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst816d_priv_t *priv = (lisa_touch_cst816d_priv_t *)dev->priv_data;
if (bus_config->bus_type != LISA_TOUCH_BUS_I2C) {
LISA_LOGE(LOG_TAG, "CST816D only supports I2C bus");
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
DEVICE_LOCK(priv);
/* 保存总线配置 */
priv->i2c_dev = bus_config->config.i2c.i2c_dev;
priv->int_gpio = bus_config->config.i2c.int_gpio;
priv->rst_gpio = bus_config->config.i2c.rst_gpio;
priv->int_pin = bus_config->config.i2c.int_pin;
priv->rst_pin = bus_config->config.i2c.rst_pin;
/* 检查必需的设备 */
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "I2C device not ready");
return LISA_DEVICE_ERR_NOT_READY;
}
/* 自动配置 I2C 总线(标准速度,主机模式) */
lisa_i2c_config_t i2c_config = {
.speed = LISA_I2C_SPEED_STANDARD,
.master_mode = true,
.slave_addr = 0,
};
int ret = lisa_i2c_configure(priv->i2c_dev, &i2c_config);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "I2C configuration failed: %d", ret);
return ret;
}
LISA_LOGI(LOG_TAG, "I2C auto-configured (speed: %d Hz)", i2c_config.speed);
/* 硬件复位芯片 */
ret = cst816d_hardware_reset(priv);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
/* 配置中断引脚(如果提供) */
if (priv->int_gpio && lisa_device_ready(priv->int_gpio)) {
ret = lisa_gpio_configure(priv->int_gpio, priv->int_pin,
LISA_GPIO_INPUT | LISA_GPIO_PULL_UP);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to configure interrupt pin: %d", ret);
return ret;
}
LISA_LOGI(LOG_TAG, "CST816D interrupt pin configured");
}
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "CST816D touch bus attached successfully");
return LISA_DEVICE_OK;
}
/**
* @brief 设置触摸事件回调函数
*/
static int cst816d_touch_set_callback(lisa_device_t *dev, lisa_touch_callback_t callback, void *user_data)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst816d_priv_t *priv = (lisa_touch_cst816d_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->callback = callback;
priv->callback_user_data = user_data;
DEVICE_UNLOCK(priv);
LISA_LOGD(LOG_TAG, "CST816D touch callback %s", callback ? "set" : "cleared");
return LISA_DEVICE_OK;
}
/**
* @brief 设置中断模式
*/
static int cst816d_touch_set_int_mode(lisa_device_t *dev, lisa_touch_int_mode_t mode)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_cst816d_priv_t *priv = (lisa_touch_cst816d_priv_t *)dev->priv_data;
if (!priv->int_gpio || !lisa_device_ready(priv->int_gpio)) {
LISA_LOGW(LOG_TAG, "Interrupt GPIO not available");
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
int ret = LISA_DEVICE_OK;
if (mode == LISA_TOUCH_INT_MODE_INTERRUPT) {
/* 如果任务不存在,创建读取任务 */
if (priv->read_task_handle == NULL) {
/* 使用 Kconfig 配置的任务优先级 */
UBaseType_t task_priority = tskIDLE_PRIORITY + CONFIG_LISA_TOUCH_ARCS_CST816D_READ_TASK_PRIORITY;
xTaskCreate(cst816d_read_task, "cst816d_read", 1024, priv,
task_priority, &priv->read_task_handle);
if (priv->read_task_handle == NULL) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to create read task");
return LISA_DEVICE_ERR_INIT_FAIL;
}
LISA_LOGI(LOG_TAG, "CST816D read task created with priority %d", task_priority);
}
/* 配置并启用中断(下降沿触发) */
ret = lisa_gpio_configure_irq(priv->int_gpio, priv->int_pin,
LISA_GPIO_IRQ_EDGE_FALLING,
cst816d_gpio_irq_callback, priv);
if (ret == LISA_DEVICE_OK) {
ret = lisa_gpio_enable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
priv->interrupt_mode_active = true;
LISA_LOGI(LOG_TAG, "CST816D interrupt mode enabled");
}
}
} else {
/* 禁用中断 */
priv->interrupt_mode_active = false;
ret = lisa_gpio_disable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
LISA_LOGI(LOG_TAG, "CST816D interrupt mode disabled (polling mode)");
}
/* 注意:不删除任务,以便下次切换回中断模式时重用 */
}
DEVICE_UNLOCK(priv);
return ret;
}
/* ===== CST816D Touch API 实例 ===== */
static const lisa_touch_api_t cst816d_touch_api = {
.get_capabilities = cst816d_touch_get_capabilities,
.read_event = cst816d_touch_read_event,
.enable = cst816d_touch_enable,
.disable = cst816d_touch_disable,
.attach_bus = cst816d_touch_attach_bus,
.set_callback = cst816d_touch_set_callback,
.set_int_mode = cst816d_touch_set_int_mode,
};
/* ===== 设备初始化函数 ===== */
static int lisa_touch_cst816d_init(void)
{
memset(&touch_cst816d_priv, 0, sizeof(lisa_touch_cst816d_priv_t));
touch_cst816d_priv.mutex = lisa_mutex_create();
if (!touch_cst816d_priv.mutex) {
LISA_LOGE(LOG_TAG, "Failed to create mutex");
return LISA_DEVICE_ERR_INIT_FAIL;
}
touch_cst816d_priv.enabled = false;
touch_cst816d_priv.initialized = true;
LISA_LOGI(LOG_TAG, "CST816D touch device initialized successfully");
return LISA_DEVICE_OK;
}
/* ===== 设备注册 ===== */
LISA_DEVICE_REGISTER(touch_cst816d, /* 设备名称 */
&cst816d_touch_api, /* API指针 */
&touch_cst816d_priv, /* 私有数据指针 */
NULL, /* 用户数据 */
lisa_touch_cst816d_init, /* 初始化函数 */
LISA_DEVICE_PRIORITY_NORMAL); /* 优先级 */

View File

@@ -0,0 +1,463 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file lisa_touch_ft5336.c
* @brief FT5336 触摸芯片驱动 - LISA Touch 设备实现
*/
#include "lisa_touch.h"
#include "lisa_i2c.h"
#include "lisa_gpio.h"
#include "lisa_thread.h"
#include <stddef.h>
#include <string.h>
#include <lisa_mutex.h>
#include "FreeRTOS.h"
#include "task.h"
#include "portmacro.h"
#define LOG_TAG "lisa_touch_ft5336"
#include <lisa_log.h>
/* FT5336 I2C 地址 */
#define FT5336_I2C_SLAVE_ADDRESS 0x38
/* FT5336 寄存器定义 */
#define REG_TD_STATUS 0x02U
#define FT5336_TOUCH_UP 1
/* FT5336 触摸能力 */
#define FT5336_MAX_X 480
#define FT5336_MAX_Y 272
#define FT5336_MAX_POINTS 1
/* 自动判断上下文的互斥锁宏(lisa_mutex_lock/unlock 已自动判断上下文) */
#define DEVICE_LOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_lock(priv->mutex, LISA_OS_WAIT_FOREVER); \
} \
} while (0)
#define DEVICE_UNLOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_unlock(priv->mutex); \
} \
} while (0)
/* ===== FT5336 设备私有数据 ===== */
typedef struct {
lisa_device_t *i2c_dev; /* I2C 设备指针 */
lisa_device_t *int_gpio; /* 中断 GPIO 设备指针 */
lisa_device_t *rst_gpio; /* 复位 GPIO 设备指针 */
uint32_t int_pin; /* 中断引脚号 */
uint32_t rst_pin; /* 复位引脚号 */
lisa_touch_callback_t callback; /* 触摸事件回调函数 */
void *callback_user_data; /* 回调用户数据 */
lisa_mutex_t *mutex; /* 互斥锁 */
bool initialized; /* 初始化标志 */
bool enabled; /* 使能标志 */
TaskHandle_t read_task_handle; /* I2C 读取任务句柄(用于中断模式) */
bool interrupt_mode_active; /* 中断模式是否激活 */
} lisa_touch_ft5336_priv_t;
/* ===== FT5336 设备静态实例 ===== */
static lisa_touch_ft5336_priv_t touch_ft5336_priv;
/* ===== 内部辅助函数 ===== */
/* 前向声明 */
static int ft5336_touch_read_event_internal(lisa_touch_ft5336_priv_t *priv, lisa_touch_event_t *event);
/**
* @brief 读取 FT5336 寄存器
*/
static int ft5336_read_reg(lisa_device_t *i2c_dev, uint8_t reg, uint8_t *data, uint32_t len)
{
if (!i2c_dev || !data || len == 0) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_i2c_msg_t msgs[2] = {
{ .addr = FT5336_I2C_SLAVE_ADDRESS, .flags = LISA_I2C_FLAG_NONE, .len = 1, .buf = &reg },
{ .addr = FT5336_I2C_SLAVE_ADDRESS, .flags = LISA_I2C_FLAG_READ, .len = len, .buf = data }
};
int ret = lisa_i2c_transfer(i2c_dev, msgs, 2);
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "I2C transfer failed: %d", ret);
return ret;
}
return LISA_DEVICE_OK;
}
/**
* @brief I2C 读取任务(用于中断模式)
* @note 此任务在中断模式下运行,等待任务通知后读取 I2C 数据
*/
static void ft5336_read_task(void *pvParameters)
{
lisa_touch_ft5336_priv_t *priv = (lisa_touch_ft5336_priv_t *)pvParameters;
while (1) {
/* 等待任务通知(中断会发送通知)
* 使用 pdFALSE 清除所有累积的通知,避免重复处理
*/
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
if (!priv->enabled || !priv->interrupt_mode_active) {
continue;
}
/* 在任务上下文中读取完整的触摸数据 */
lisa_touch_event_t event = {0};
int ret = ft5336_touch_read_event_internal(priv, &event);
if (ret == LISA_DEVICE_OK && priv->callback) {
/* 通过回调函数返回完整数据 */
priv->callback(&event, priv->callback_user_data);
}
}
}
/**
* @brief GPIO 中断回调
*/
static void ft5336_gpio_irq_callback(uint32_t pin, void *user_data)
{
(void)pin;
lisa_touch_ft5336_priv_t *priv = (lisa_touch_ft5336_priv_t *)user_data;
if (!priv || !priv->interrupt_mode_active) {
return;
}
/* 在中断上下文中发送任务通知,唤醒读取任务 */
if (priv->read_task_handle != NULL) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(priv->read_task_handle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
/**
* @brief 硬件复位芯片
*/
static int ft5336_hardware_reset(lisa_touch_ft5336_priv_t *priv)
{
if (!priv->rst_gpio || !lisa_device_ready(priv->rst_gpio)) {
LISA_LOGW(LOG_TAG, "Reset GPIO not available");
return LISA_DEVICE_OK;
}
lisa_gpio_configure(priv->rst_gpio, priv->rst_pin, LISA_GPIO_OUTPUT);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(10);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 0);
lisa_thread_mdelay(10);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(300);
LISA_LOGI(LOG_TAG, "FT5336 hardware reset completed");
return LISA_DEVICE_OK;
}
/* ===== LISA Touch API 实现 ===== */
/**
* @brief 获取触摸设备能力
*/
static int ft5336_touch_get_capabilities(lisa_device_t *dev, lisa_touch_capabilities_t *caps)
{
if (!lisa_device_is_initialized(dev) || !caps) {
return LISA_DEVICE_ERR_INVALID;
}
memset(caps, 0, sizeof(lisa_touch_capabilities_t));
caps->max_x = FT5336_MAX_X;
caps->max_y = FT5336_MAX_Y;
caps->max_points = FT5336_MAX_POINTS;
caps->has_pressure = false;
caps->has_gesture = false;
caps->supported_gestures = 0;
return LISA_DEVICE_OK;
}
/**
* @brief 内部读取触摸事件函数(供任务和 API 调用)
*/
static int ft5336_touch_read_event_internal(lisa_touch_ft5336_priv_t *priv, lisa_touch_event_t *event)
{
if (!priv || !event || !priv->enabled) {
return LISA_DEVICE_ERR_INVALID;
}
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
uint8_t read_buf[5] = {0};
int ret = ft5336_read_reg(priv->i2c_dev, REG_TD_STATUS, read_buf, sizeof(read_buf));
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
uint8_t point_num = read_buf[0] & 0x0F;
if (point_num != 1) {
DEVICE_UNLOCK(priv);
return LISA_DEVICE_ERR_IO;
}
uint16_t x = ((read_buf[1] & 0x0F) << 8) + read_buf[2];
uint16_t y = ((read_buf[3] & 0x0F) << 8) + read_buf[4];
bool pressed = ((read_buf[1] >> 6) == FT5336_TOUCH_UP) ? false : true;
memset(event, 0, sizeof(lisa_touch_event_t));
if (pressed) {
event->type = LISA_TOUCH_EVENT_PRESS;
event->point_count = 1;
event->points[0].id = 0;
event->points[0].x = x;
event->points[0].y = y;
event->points[0].state = LISA_TOUCH_POINT_PRESSED;
LISA_LOGD(LOG_TAG, "Touch: x=%d, y=%d", x, y);
} else {
event->type = LISA_TOUCH_EVENT_RELEASE;
event->point_count = 0;
}
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 读取触摸事件(API 接口)
*/
static int ft5336_touch_read_event(lisa_device_t *dev, lisa_touch_event_t *event)
{
if (!lisa_device_is_initialized(dev) || !event) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_ft5336_priv_t *priv = (lisa_touch_ft5336_priv_t *)dev->priv_data;
/* 中断模式下禁止轮询读取,避免与中断任务产生资源竞争 */
if (priv->interrupt_mode_active) {
LISA_LOGW(LOG_TAG, "Cannot read_event in interrupt mode, use callback instead");
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
return ft5336_touch_read_event_internal(priv, event);
}
/**
* @brief 启用触摸设备
*/
static int ft5336_touch_enable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_ft5336_priv_t *priv = (lisa_touch_ft5336_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = true;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "FT5336 touch device enabled");
return LISA_DEVICE_OK;
}
/**
* @brief 禁用触摸设备
*/
static int ft5336_touch_disable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_ft5336_priv_t *priv = (lisa_touch_ft5336_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = false;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "FT5336 touch device disabled");
return LISA_DEVICE_OK;
}
/**
* @brief 附加触摸总线接口
*/
static int ft5336_touch_attach_bus(lisa_device_t *dev, const lisa_touch_bus_config_t *bus_config)
{
if (!lisa_device_is_initialized(dev) || !bus_config) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_ft5336_priv_t *priv = (lisa_touch_ft5336_priv_t *)dev->priv_data;
if (bus_config->bus_type != LISA_TOUCH_BUS_I2C) {
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
DEVICE_LOCK(priv);
priv->i2c_dev = bus_config->config.i2c.i2c_dev;
priv->int_gpio = bus_config->config.i2c.int_gpio;
priv->rst_gpio = bus_config->config.i2c.rst_gpio;
priv->int_pin = bus_config->config.i2c.int_pin;
priv->rst_pin = bus_config->config.i2c.rst_pin;
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
DEVICE_UNLOCK(priv);
return LISA_DEVICE_ERR_NOT_READY;
}
lisa_i2c_config_t i2c_config = {
.speed = LISA_I2C_SPEED_STANDARD,
.master_mode = true,
.slave_addr = 0,
};
int ret = lisa_i2c_configure(priv->i2c_dev, &i2c_config);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
ret = ft5336_hardware_reset(priv);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
if (priv->int_gpio && lisa_device_ready(priv->int_gpio)) {
lisa_gpio_configure(priv->int_gpio, priv->int_pin, LISA_GPIO_INPUT | LISA_GPIO_PULL_UP);
}
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "FT5336 touch bus attached");
return LISA_DEVICE_OK;
}
/**
* @brief 设置触摸事件回调函数
*/
static int ft5336_touch_set_callback(lisa_device_t *dev, lisa_touch_callback_t callback, void *user_data)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_ft5336_priv_t *priv = (lisa_touch_ft5336_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->callback = callback;
priv->callback_user_data = user_data;
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 设置中断模式
*/
static int ft5336_touch_set_int_mode(lisa_device_t *dev, lisa_touch_int_mode_t mode)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_ft5336_priv_t *priv = (lisa_touch_ft5336_priv_t *)dev->priv_data;
if (!priv->int_gpio || !lisa_device_ready(priv->int_gpio)) {
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
int ret = LISA_DEVICE_OK;
if (mode == LISA_TOUCH_INT_MODE_INTERRUPT) {
/* 如果任务不存在,创建读取任务 */
if (priv->read_task_handle == NULL) {
/* 使用 Kconfig 配置的任务优先级 */
UBaseType_t task_priority = tskIDLE_PRIORITY + CONFIG_LISA_TOUCH_ARCS_FT5336_READ_TASK_PRIORITY;
xTaskCreate(ft5336_read_task, "ft5336_read", 1024, priv,
task_priority, &priv->read_task_handle);
if (priv->read_task_handle == NULL) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to create read task");
return LISA_DEVICE_ERR_INIT_FAIL;
}
LISA_LOGI(LOG_TAG, "FT5336 read task created with priority %d", task_priority);
}
ret = lisa_gpio_configure_irq(priv->int_gpio, priv->int_pin,
LISA_GPIO_IRQ_EDGE_FALLING,
ft5336_gpio_irq_callback, priv);
if (ret == LISA_DEVICE_OK) {
ret = lisa_gpio_enable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
priv->interrupt_mode_active = true;
LISA_LOGI(LOG_TAG, "FT5336 interrupt mode enabled");
}
}
} else {
/* 禁用中断 */
priv->interrupt_mode_active = false;
ret = lisa_gpio_disable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
LISA_LOGI(LOG_TAG, "FT5336 interrupt mode disabled (polling mode)");
}
/* 注意:不删除任务,以便下次切换回中断模式时重用 */
}
DEVICE_UNLOCK(priv);
return ret;
}
/* ===== FT5336 Touch API 实例 ===== */
static const lisa_touch_api_t ft5336_touch_api = {
.get_capabilities = ft5336_touch_get_capabilities,
.read_event = ft5336_touch_read_event,
.enable = ft5336_touch_enable,
.disable = ft5336_touch_disable,
.attach_bus = ft5336_touch_attach_bus,
.set_callback = ft5336_touch_set_callback,
.set_int_mode = ft5336_touch_set_int_mode,
};
/* ===== 设备初始化函数 ===== */
static int lisa_touch_ft5336_init(void)
{
memset(&touch_ft5336_priv, 0, sizeof(lisa_touch_ft5336_priv_t));
touch_ft5336_priv.mutex = lisa_mutex_create();
if (!touch_ft5336_priv.mutex) {
return LISA_DEVICE_ERR_INIT_FAIL;
}
touch_ft5336_priv.enabled = false;
touch_ft5336_priv.initialized = true;
LISA_LOGI(LOG_TAG, "FT5336 touch device initialized");
return LISA_DEVICE_OK;
}
/* ===== 设备注册 ===== */
LISA_DEVICE_REGISTER(touch_ft5336,
&ft5336_touch_api,
&touch_ft5336_priv,
NULL,
lisa_touch_ft5336_init,
LISA_DEVICE_PRIORITY_NORMAL);

View File

@@ -0,0 +1,522 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file lisa_touch_st77921.c
* @brief ST77921 触摸芯片驱动 - LISA Touch 设备实现
*/
#include "lisa_touch.h"
#include "lisa_i2c.h"
#include "lisa_gpio.h"
#include "lisa_thread.h"
#include <stddef.h>
#include <string.h>
#include <lisa_mutex.h>
#include "FreeRTOS.h"
#include "task.h"
#include "portmacro.h"
#define LOG_TAG "lisa_touch_st77921"
#include <lisa_log.h>
/* ST77921 I2C 地址 */
#define ST77921_I2C_SLAVE_ADDRESS 0x55
/* ST77921 寄存器定义 */
#define ST77921_TOUCH_REG_X_RESOLUTION_HIGH 0x0005U
#define ST77921_TOUCH_REG_X_RESOLUTION_LOW 0x0006U
#define ST77921_TOUCH_REG_Y_RESOLUTION_HIGH 0x0007U
#define ST77921_TOUCH_REG_Y_RESOLUTION_LOW 0x0008U
#define ST77921_TOUCH_REG_MAX_TOUCHES 0x0009U
#define ST77921_TOUCH_REG_TOUCH_INFO 0x0010U
#define ST77921_TOUCH_REG_TOUCH_DATA 0x0014U
/* ST77921 触摸能力 */
#define ST77921_MAX_X 480
#define ST77921_MAX_Y 480
#define ST77921_MAX_POINTS 1
/* 自动判断上下文的互斥锁宏(lisa_mutex_lock/unlock 已自动判断上下文) */
#define DEVICE_LOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_lock(priv->mutex, LISA_OS_WAIT_FOREVER); \
} \
} while (0)
#define DEVICE_UNLOCK(priv) \
do { \
if (priv->mutex) { \
lisa_mutex_unlock(priv->mutex); \
} \
} while (0)
/* ===== ST77921 设备私有数据 ===== */
typedef struct {
lisa_device_t *i2c_dev; /* I2C 设备指针 */
lisa_device_t *int_gpio; /* 中断 GPIO 设备指针 */
lisa_device_t *rst_gpio; /* 复位 GPIO 设备指针 */
uint32_t int_pin; /* 中断引脚号 */
uint32_t rst_pin; /* 复位引脚号 */
lisa_touch_callback_t callback; /* 触摸事件回调函数 */
void *callback_user_data; /* 回调用户数据 */
lisa_mutex_t *mutex; /* 互斥锁 */
bool initialized; /* 初始化标志 */
bool enabled; /* 使能标志 */
uint16_t x_res; /* X 分辨率 */
uint16_t y_res; /* Y 分辨率 */
uint8_t max_touches; /* 最大触摸点数 */
TaskHandle_t read_task_handle; /* I2C 读取任务句柄(用于中断模式) */
bool interrupt_mode_active; /* 中断模式是否激活 */
} lisa_touch_st77921_priv_t;
/* ===== ST77921 设备静态实例 ===== */
static lisa_touch_st77921_priv_t touch_st77921_priv;
/* ===== 内部辅助函数 ===== */
/* 前向声明 */
static int st77921_touch_read_event_internal(lisa_touch_st77921_priv_t *priv, lisa_touch_event_t *event);
/**
* @brief 读取 ST77921 寄存器
*/
static int st77921_read_reg(lisa_device_t *i2c_dev, uint16_t reg, uint8_t *data, uint32_t len)
{
if (!i2c_dev || !data || len == 0) {
return LISA_DEVICE_ERR_INVALID;
}
uint8_t reg_buf[2];
reg_buf[0] = (reg >> 8) & 0xFF; /* 高字节在前 */
reg_buf[1] = reg & 0xFF; /* 低字节 */
lisa_i2c_msg_t msgs[2] = {
{.addr = ST77921_I2C_SLAVE_ADDRESS, .flags = LISA_I2C_FLAG_NONE, .len = sizeof(reg_buf), .buf = reg_buf},
{.addr = ST77921_I2C_SLAVE_ADDRESS, .flags = LISA_I2C_FLAG_READ, .len = len, .buf = data}
};
int ret = lisa_i2c_transfer(i2c_dev, msgs, 2);
if (ret != LISA_DEVICE_OK) {
LISA_LOGE(LOG_TAG, "I2C transfer failed: %d", ret);
return ret;
}
return LISA_DEVICE_OK;
}
/**
* @brief 读取芯片信息(分辨率、最大触摸点数)
*/
static int st77921_read_info(lisa_touch_st77921_priv_t *priv)
{
uint8_t rx_buffer[4] = {0};
int ret = st77921_read_reg(priv->i2c_dev, ST77921_TOUCH_REG_X_RESOLUTION_HIGH, rx_buffer, sizeof(rx_buffer));
if (ret != LISA_DEVICE_OK) {
return ret;
}
priv->x_res = ((rx_buffer[0] & 0x3F) << 8) + rx_buffer[1];
priv->y_res = ((rx_buffer[2] & 0x3F) << 8) + rx_buffer[3];
uint8_t max_touches_buf[1] = {0};
ret = st77921_read_reg(priv->i2c_dev, ST77921_TOUCH_REG_MAX_TOUCHES, max_touches_buf, 1);
if (ret != LISA_DEVICE_OK) {
return ret;
}
priv->max_touches = max_touches_buf[0];
if (priv->max_touches > ST77921_MAX_POINTS) {
LISA_LOGW(LOG_TAG, "Unexpected max_touches: %d", priv->max_touches);
priv->max_touches = ST77921_MAX_POINTS;
}
LISA_LOGI(LOG_TAG, "ST77921 info: x_res=%d, y_res=%d, max_touches=%d",
priv->x_res, priv->y_res, priv->max_touches);
return LISA_DEVICE_OK;
}
/**
* @brief I2C 读取任务(用于中断模式)
* @note 此任务在中断模式下运行,等待任务通知后读取 I2C 数据
*/
static void st77921_read_task(void *pvParameters)
{
lisa_touch_st77921_priv_t *priv = (lisa_touch_st77921_priv_t *)pvParameters;
while (1) {
/* 等待任务通知(中断会发送通知)
* 使用 pdFALSE 清除所有累积的通知,避免重复处理
*/
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
if (!priv->enabled || !priv->interrupt_mode_active) {
continue;
}
/* 在任务上下文中读取完整的触摸数据 */
lisa_touch_event_t event = {0};
int ret = st77921_touch_read_event_internal(priv, &event);
if (ret == LISA_DEVICE_OK && priv->callback) {
/* 通过回调函数返回完整数据 */
priv->callback(&event, priv->callback_user_data);
}
}
}
/**
* @brief GPIO 中断回调
*/
static void st77921_gpio_irq_callback(uint32_t pin, void *user_data)
{
(void)pin;
lisa_touch_st77921_priv_t *priv = (lisa_touch_st77921_priv_t *)user_data;
if (!priv || !priv->interrupt_mode_active) {
return;
}
/* 在中断上下文中发送任务通知,唤醒读取任务 */
if (priv->read_task_handle != NULL) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(priv->read_task_handle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
/**
* @brief 硬件复位芯片
*/
static int st77921_hardware_reset(lisa_touch_st77921_priv_t *priv)
{
if (!priv->rst_gpio || !lisa_device_ready(priv->rst_gpio)) {
LISA_LOGW(LOG_TAG, "Reset GPIO not available");
return LISA_DEVICE_OK;
}
lisa_gpio_configure(priv->rst_gpio, priv->rst_pin, LISA_GPIO_OUTPUT);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(10);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 0);
lisa_thread_mdelay(10);
lisa_gpio_write_pin(priv->rst_gpio, priv->rst_pin, 1);
lisa_thread_mdelay(300);
LISA_LOGI(LOG_TAG, "ST77921 reset completed");
return LISA_DEVICE_OK;
}
static int st77921_touch_get_capabilities(lisa_device_t *dev, lisa_touch_capabilities_t *caps)
{
if (!lisa_device_is_initialized(dev) || !caps) {
return LISA_DEVICE_ERR_INVALID;
}
memset(caps, 0, sizeof(lisa_touch_capabilities_t));
caps->max_x = ST77921_MAX_X;
caps->max_y = ST77921_MAX_Y;
caps->max_points = ST77921_MAX_POINTS;
caps->has_pressure = false;
caps->has_gesture = false;
caps->supported_gestures = 0;
return LISA_DEVICE_OK;
}
/**
* @brief 内部读取触摸事件函数(供任务和 API 调用)
*/
static int st77921_touch_read_event_internal(lisa_touch_st77921_priv_t *priv, lisa_touch_event_t *event)
{
if (!priv || !event || !priv->enabled) {
return LISA_DEVICE_ERR_INVALID;
}
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
/* 读取触摸信息寄存器(4字节) */
uint8_t touch_info[4] = {0};
int ret = st77921_read_reg(priv->i2c_dev, ST77921_TOUCH_REG_TOUCH_INFO, touch_info, sizeof(touch_info));
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
/* 检查是否有触摸事件(bit 3) */
if (touch_info[0] & (1 << 3)) {
/* 读取触摸数据(7字节 * max_touches) */
uint8_t touch_data[7] = {0};
ret = st77921_read_reg(priv->i2c_dev, ST77921_TOUCH_REG_TOUCH_DATA, touch_data, sizeof(touch_data));
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
uint16_t x = ((touch_data[0] & 0x0F) << 8) + touch_data[1];
uint16_t y = ((touch_data[2] & 0x0F) << 8) + touch_data[3];
bool pressed = (touch_data[0] & 0x80) ? true : false;
memset(event, 0, sizeof(lisa_touch_event_t));
if (pressed) {
event->type = LISA_TOUCH_EVENT_PRESS;
event->point_count = 1;
event->points[0].id = 0;
event->points[0].x = x;
event->points[0].y = y;
event->points[0].state = LISA_TOUCH_POINT_PRESSED;
LISA_LOGD(LOG_TAG, "Touch: x=%d, y=%d", x, y);
} else {
event->type = LISA_TOUCH_EVENT_RELEASE;
event->point_count = 0;
}
} else {
memset(event, 0, sizeof(lisa_touch_event_t));
event->type = LISA_TOUCH_EVENT_RELEASE;
event->point_count = 0;
}
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 读取触摸事件(API 接口)
*/
static int st77921_touch_read_event(lisa_device_t *dev, lisa_touch_event_t *event)
{
if (!lisa_device_is_initialized(dev) || !event) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_st77921_priv_t *priv = (lisa_touch_st77921_priv_t *)dev->priv_data;
/* 中断模式下禁止轮询读取,避免与中断任务产生资源竞争 */
if (priv->interrupt_mode_active) {
LISA_LOGW(LOG_TAG, "Cannot read_event in interrupt mode, use callback instead");
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
return st77921_touch_read_event_internal(priv, event);
}
/**
* @brief 启用触摸设备
*/
static int st77921_touch_enable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_st77921_priv_t *priv = (lisa_touch_st77921_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = true;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "ST77921 touch device enabled");
return LISA_DEVICE_OK;
}
/**
* @brief 禁用触摸设备
*/
static int st77921_touch_disable(lisa_device_t *dev)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_st77921_priv_t *priv = (lisa_touch_st77921_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->enabled = false;
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "ST77921 touch device disabled");
return LISA_DEVICE_OK;
}
/**
* @brief 附加触摸总线接口
*/
static int st77921_touch_attach_bus(lisa_device_t *dev, const lisa_touch_bus_config_t *bus_config)
{
if (!lisa_device_is_initialized(dev) || !bus_config) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_st77921_priv_t *priv = (lisa_touch_st77921_priv_t *)dev->priv_data;
if (bus_config->bus_type != LISA_TOUCH_BUS_I2C) {
return LISA_DEVICE_ERR_NOT_SUPPORT;
}
DEVICE_LOCK(priv);
priv->i2c_dev = bus_config->config.i2c.i2c_dev;
priv->int_gpio = bus_config->config.i2c.int_gpio;
priv->rst_gpio = bus_config->config.i2c.rst_gpio;
priv->int_pin = bus_config->config.i2c.int_pin;
priv->rst_pin = bus_config->config.i2c.rst_pin;
if (!priv->i2c_dev || !lisa_device_ready(priv->i2c_dev)) {
DEVICE_UNLOCK(priv);
return LISA_DEVICE_ERR_NOT_READY;
}
lisa_i2c_config_t i2c_config = {
.speed = LISA_I2C_SPEED_STANDARD,
.master_mode = true,
.slave_addr = 0,
};
int ret = lisa_i2c_configure(priv->i2c_dev, &i2c_config);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
ret = st77921_hardware_reset(priv);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
return ret;
}
/* 读取芯片信息 */
ret = st77921_read_info(priv);
if (ret != LISA_DEVICE_OK) {
DEVICE_UNLOCK(priv);
LISA_LOGW(LOG_TAG, "Failed to read chip info, using defaults");
}
if (priv->int_gpio && lisa_device_ready(priv->int_gpio)) {
lisa_gpio_configure(priv->int_gpio, priv->int_pin, LISA_GPIO_INPUT | LISA_GPIO_PULL_UP);
}
DEVICE_UNLOCK(priv);
LISA_LOGI(LOG_TAG, "ST77921 touch bus attached");
return LISA_DEVICE_OK;
}
/**
* @brief 设置触摸事件回调函数
*/
static int st77921_touch_set_callback(lisa_device_t *dev, lisa_touch_callback_t callback, void *user_data)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_st77921_priv_t *priv = (lisa_touch_st77921_priv_t *)dev->priv_data;
DEVICE_LOCK(priv);
priv->callback = callback;
priv->callback_user_data = user_data;
DEVICE_UNLOCK(priv);
return LISA_DEVICE_OK;
}
/**
* @brief 设置中断模式
*/
static int st77921_touch_set_int_mode(lisa_device_t *dev, lisa_touch_int_mode_t mode)
{
if (!lisa_device_is_initialized(dev)) {
return LISA_DEVICE_ERR_INVALID;
}
lisa_touch_st77921_priv_t *priv = (lisa_touch_st77921_priv_t *)dev->priv_data;
if (!priv->int_gpio || !lisa_device_ready(priv->int_gpio)) {
return LISA_DEVICE_ERR_NOT_READY;
}
DEVICE_LOCK(priv);
int ret = LISA_DEVICE_OK;
if (mode == LISA_TOUCH_INT_MODE_INTERRUPT) {
/* 如果任务不存在,创建读取任务 */
if (priv->read_task_handle == NULL) {
/* 使用 Kconfig 配置的任务优先级 */
UBaseType_t task_priority = tskIDLE_PRIORITY + CONFIG_LISA_TOUCH_ARCS_ST77921_READ_TASK_PRIORITY;
xTaskCreate(st77921_read_task, "st77921_read", 1024, priv,
task_priority, &priv->read_task_handle);
if (priv->read_task_handle == NULL) {
DEVICE_UNLOCK(priv);
LISA_LOGE(LOG_TAG, "Failed to create read task");
return LISA_DEVICE_ERR_INIT_FAIL;
}
LISA_LOGI(LOG_TAG, "ST77921 read task created with priority %d", task_priority);
}
ret = lisa_gpio_configure_irq(priv->int_gpio, priv->int_pin,
LISA_GPIO_IRQ_EDGE_FALLING,
st77921_gpio_irq_callback, priv);
if (ret == LISA_DEVICE_OK) {
ret = lisa_gpio_enable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
priv->interrupt_mode_active = true;
LISA_LOGI(LOG_TAG, "ST77921 interrupt mode enabled");
}
}
} else {
/* 禁用中断 */
priv->interrupt_mode_active = false;
ret = lisa_gpio_disable_irq(priv->int_gpio, priv->int_pin);
if (ret == LISA_DEVICE_OK) {
LISA_LOGI(LOG_TAG, "ST77921 interrupt mode disabled (polling mode)");
}
/* 注意:不删除任务,以便下次切换回中断模式时重用 */
}
DEVICE_UNLOCK(priv);
return ret;
}
/* ===== ST77921 Touch API 实例 ===== */
static const lisa_touch_api_t st77921_api = {
.get_capabilities = st77921_touch_get_capabilities,
.read_event = st77921_touch_read_event,
.enable = st77921_touch_enable,
.disable = st77921_touch_disable,
.attach_bus = st77921_touch_attach_bus,
.set_callback = st77921_touch_set_callback,
.set_int_mode = st77921_touch_set_int_mode,
};
/* ===== 设备初始化函数 ===== */
static int lisa_touch_st77921_init(void)
{
memset(&touch_st77921_priv, 0, sizeof(lisa_touch_st77921_priv_t));
touch_st77921_priv.mutex = lisa_mutex_create();
if (!touch_st77921_priv.mutex) {
return LISA_DEVICE_ERR_INIT_FAIL;
}
touch_st77921_priv.enabled = false;
touch_st77921_priv.initialized = true;
touch_st77921_priv.x_res = ST77921_MAX_X;
touch_st77921_priv.y_res = ST77921_MAX_Y;
touch_st77921_priv.max_touches = ST77921_MAX_POINTS;
LISA_LOGI(LOG_TAG, "ST77921 touch device initialized");
return LISA_DEVICE_OK;
}
/* ===== 设备注册 ===== */
LISA_DEVICE_REGISTER(touch_st77921,
&st77921_api,
&touch_st77921_priv,
NULL,
lisa_touch_st77921_init,
LISA_DEVICE_PRIORITY_NORMAL);