refactor(touch): 集成硬件 I2C 驱动并添加 EXTI 中断支持

- 将 touch 底层从软件 I2C 位冲切换为硬件 I2C0 (PB6/PB7)
- 修复 i2c_driver I2C0 引脚映射 (PB8/PB9 -> PB6/PB7)
- 重写 master_write_read() 实现正确的重复起始条件
- 添加 i2c_driver.h C API 声明供 C 代码调用
- 添加 GT1151 EXTI 中断 (PD11, EXTI11 下降沿触发)
- 删除 touch.h 中废弃的软件 I2C 函数声明
- test_touch() 改为中断驱动,无触摸时 I2C 总线空闲
This commit is contained in:
2026-04-26 20:36:54 +08:00
parent 851c3db56e
commit cbad06d616
6 changed files with 560 additions and 464 deletions
+104 -1
View File
@@ -6,6 +6,7 @@
#include "sdram_manager.h"
#include "flash_manager.h"
#include "lcd.h"
#include "touch.h"
#include <cstdio>
#include <cstring>
@@ -346,7 +347,106 @@ static void concurrent_loop(void)
}
// =============================================================
// 主函数
// Touch 测试
// =============================================================
static void test_touch(void)
{
printf("\r\n===== Touch 测试 (中断驱动) =====\r\n");
uint8_t ret = GT1151_Init();
if (ret != 0)
{
printf(" Touch 初始化失败 (ret=%d)\r\n", ret);
return;
}
printf(" Touch 初始化成功,等待触摸中断...\r\n");
LCD_Clear(0x0841);
POINT_COLOR = CYAN;
LCD_ShowString(10, 10, 460, 24, 16, 0, (uint8_t *)"=== Touch Test (IRQ) ===");
POINT_COLOR = WHITE;
LCD_ShowString(10, 35, 460, 24, 16, 0, (uint8_t *)"Touch screen now");
LCD_ShowString(10, 55, 300, 24, 16, 0, (uint8_t *)"IRQ driven, watch UART");
LCD_ShowString(10, 75, 200, 24, 16, 0, (uint8_t *)"X:0 Y:0");
uint16_t last_x = 0xFFFF;
uint16_t last_y = 0xFFFF;
uint32_t idle_counter = 0;
bool was_touching = false;
uint32_t iter = 0;
g_touch_irq_flag = 0;
while (1)
{
if (g_touch_irq_flag)
{
g_touch_irq_flag = 0;
delay_1ms(5);
GT1151_Scan(0);
if (tp_dev.sta & TP_PRES_DOWN)
{
idle_counter = 0;
uint16_t x = tp_dev.x[0];
uint16_t y = tp_dev.y[0];
printf("[TOUCH] iter=%lu sta=0x%02X x=%u y=%u\r\n",
(unsigned long)iter, tp_dev.sta, x, y);
if (x < lcddev.width && y < lcddev.height)
{
POINT_COLOR = BLACK;
LCD_FillRectangle(10, 75, 200, 95);
POINT_COLOR = GREEN;
char buf[24];
sprintf(buf, "X:%-3d Y:%-3d", x, y);
LCD_ShowString(10, 75, 200, 24, 16, 0, (uint8_t *)buf);
if (was_touching && last_x != 0xFFFF)
{
POINT_COLOR = MAGENTA;
LCD_DrawLine(last_x, last_y, x, y);
}
POINT_COLOR = YELLOW;
LCD_FillCircle(x, y, 4);
last_x = x;
last_y = y;
was_touching = true;
}
}
else
{
was_touching = false;
last_x = 0xFFFF;
last_y = 0xFFFF;
}
iter++;
}
else
{
idle_counter++;
delay_1ms(10);
if (idle_counter > 500)
{
printf(" 无触摸超过 5 秒,退出 Touch 测试\r\n");
break;
}
}
}
LCD_Clear(BLUE);
printf(" Touch 测试完成\r\n");
delay_1ms(500);
}
// =============================================================
// 主函数
// =============================================================
int main(void)
{
@@ -438,6 +538,9 @@ int main(void)
concurrent_loop();
delay_1ms(500);
test_touch();
delay_1ms(500);
// ---- 主循环 ----
led_pattern(0x0F);
printf("\r\n===== 所有外设测试完成 =====\r\n");