Skip to content

GT911 读触点,ILI9341 只刷新标记区域 ​

按住或拖动时在 ILI9341 上显示一个白色标记,松开后擦掉。读取触摸点、坐标换算和 LCD 区域写入可沿调用顺序分别观察。

板型与接线 ​

LCD 接线沿用 FSMC 点屏示例。GT911 使用独立的 3.3 V I²C 模块,PB6/PB7 接 SCL/SDA,PC7 接触摸 RESET,PB5 接 INT。I²C 需要外部上拉。接线以本示例引脚为准,使用扩展底板时对照排针复用。

沿执行路径读代码 ​

  1. 复位时 INT 保持低,与 GT911 的 0x5D 地址相配。核对产品 ID,再读原始分辨率。
  2. 状态 ready 时读第一个触点,成功读取并清状态后才把数据交给画点。I²C 失败时保留上一帧的显示。
  3. 把原始坐标映射到 240×320,擦掉旧标记和绘制新区域。屏幕边缘裁掉越界像素。

完整源码 ​

下载示例 main.c

c
#include "../common/ili9341.h"
#include "gt911.h"

static const TouchPoint display = {LCD_WIDTH, LCD_HEIGHT};
static volatile uint32_t stage, io_errors, bad_samples, drawings;
static volatile TouchPoint last_point;
void SysTick_Handler(void) { HAL_IncTick(); }

int main(void)
{
  // 1. LCD 先清为黑色背景。触摸初始化失败时保留阶段号。
  HAL_Init();
  stage = 1U;
  if (ili9341_init() != HAL_OK) { for (;;) {} }
  ili9341_fill(0U, 0U, LCD_WIDTH - 1U, LCD_HEIGHT - 1U, 0x0000U);
  stage = 2U;
  if (!gt911_init()) { for (;;) {} }
  stage = 0U;

  bool shown = false;
  TouchRect previous = {0};
  for (;;)
  {
    // 2. 本轮只有新的有效坐标或抬起才更新显示,通信失败保留上一笔。
    TouchPoint point;
    const TouchState state = gt911_next(&point, display);
    if (state == TOUCH_IO) { ++io_errors; }
    if (state == TOUCH_BAD) { ++bad_samples; }
    if ((state == TOUCH_UP || state == TOUCH_DOWN) && shown)
    {
      ili9341_fill(previous.x0, previous.y0, previous.x1, previous.y1, 0x0000U);
      shown = false;
    }

    // 3. 按下时只刷新新标记覆盖的小矩形。框裁剪到屏幕边界。
    if (state == TOUCH_DOWN)
    {
      previous = touch_marker(point, display);
      ili9341_fill(previous.x0, previous.y0, previous.x1, previous.y1, 0xFFFFU);
      shown = true;
      last_point = point;
      ++drawings;
    }
    HAL_Delay(10U);
  }
}

下载 LCD 区域驱动 · 下载坐标和裁剪 · 下载主机检查

c
#ifndef SKY_TOUCH_COORDINATES_H
#define SKY_TOUCH_COORDINATES_H

#include <stdbool.h>
#include <stdint.h>

typedef struct { uint16_t x, y; } TouchPoint;
typedef struct { uint16_t x0, y0, x1, y1; } TouchRect;

static inline uint16_t read_le16(const uint8_t bytes[2])
{
  return (uint16_t)((unsigned)bytes[0] | ((unsigned)bytes[1] << 8));
}

// source/target 的 x、y 各自为宽和高。失败时不修改 result。
static inline bool map_touch(TouchPoint raw, TouchPoint source, TouchPoint target, TouchPoint *result)
{
  if (source.x < 2U || source.y < 2U || target.x == 0U || target.y == 0U ||
      raw.x >= source.x || raw.y >= source.y)
  {
    return false;
  }
  result->x = (uint16_t)((uint32_t)raw.x * (target.x - 1U) / (source.x - 1U));
  result->y = (uint16_t)((uint32_t)raw.y * (target.y - 1U) / (source.y - 1U));
  return true;
}

// point 已在屏幕内,取左右各 4 像素并裁剪到可用范围,端点均包含。
static inline TouchRect touch_marker(TouchPoint point, TouchPoint bounds)
{
  const uint32_t right = (uint32_t)point.x + 4U, bottom = (uint32_t)point.y + 4U;
  return (TouchRect){
    .x0 = point.x >= 4U ? point.x - 4U : 0U,
    .y0 = point.y >= 4U ? point.y - 4U : 0U,
    .x1 = right < bounds.x ? right : bounds.x - 1U,
    .y1 = bottom < bounds.y ? bottom : bounds.y - 1U,
  };
}
#endif

下载 GT911 通信驱动

c
#ifndef SKY_GT911_H
#define SKY_GT911_H

#include "stm32f4xx_hal.h"
#include "coordinates.h"

enum { GT911_ADDRESS = 0x5D << 1, GT_ID = 0x8140, GT_SIZE = 0x8146, GT_STATUS = 0x814E, GT_FIRST_X = 0x8150 };
typedef enum { TOUCH_WAIT, TOUCH_UP, TOUCH_DOWN, TOUCH_BAD, TOUCH_IO } TouchState;
static I2C_HandleTypeDef touch;
static TouchPoint sensor_size;

static HAL_StatusTypeDef touch_read(uint16_t address, uint8_t *bytes, uint16_t length)
{
  return HAL_I2C_Mem_Read(&touch, GT911_ADDRESS, address, I2C_MEMADD_SIZE_16BIT, bytes, length, 30U);
}

static inline bool gt911_init(void)
{
  // 1. 复位期间将 INT 保持低电平选地址 0x5D,芯片启动后把 INT 改回输入。
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_I2C1_CLK_ENABLE();
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET);
  GPIO_InitTypeDef pin = {.Pin = GPIO_PIN_5, .Mode = GPIO_MODE_OUTPUT_PP};
  HAL_GPIO_Init(GPIOB, &pin);
  pin.Pin = GPIO_PIN_7;
  HAL_GPIO_Init(GPIOC, &pin);
  HAL_Delay(20U);
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET);
  HAL_Delay(60U);
  pin.Mode = GPIO_MODE_INPUT;
  pin.Pin = GPIO_PIN_5;
  HAL_GPIO_Init(GPIOB, &pin);
  HAL_Delay(100U);

  // 2. I²C1 使用外部上拉。核对产品 ID,再读取控制器报告的坐标尺寸。
  pin = (GPIO_InitTypeDef){
    .Pin = GPIO_PIN_6 | GPIO_PIN_7, .Mode = GPIO_MODE_AF_OD,
    .Speed = GPIO_SPEED_FREQ_HIGH, .Alternate = GPIO_AF4_I2C1,
  };
  HAL_GPIO_Init(GPIOB, &pin);
  touch.Instance = I2C1;
  touch.Init = (I2C_InitTypeDef){.ClockSpeed = 100000U, .DutyCycle = I2C_DUTYCYCLE_2,
                                 .AddressingMode = I2C_ADDRESSINGMODE_7BIT};
  uint8_t id[4], size[4];
  if (HAL_I2C_Init(&touch) != HAL_OK || touch_read(GT_ID, id, sizeof(id)) != HAL_OK ||
      id[0] != '9' || id[1] != '1' || id[2] != '1' ||
      touch_read(GT_SIZE, size, sizeof(size)) != HAL_OK)
  {
    return false;
  }
  sensor_size = (TouchPoint){read_le16(size), read_le16(size + 2U)};
  return sensor_size.x >= 2U && sensor_size.y >= 2U;
}

static inline TouchState gt911_next(TouchPoint *point, TouchPoint target)
{
  // 1. 没有新数据就保留触点。新数据才确定是抬起、按下还是坏包。
  uint8_t status;
  if (touch_read(GT_STATUS, &status, 1U) != HAL_OK) { return TOUCH_IO; }
  if (!(status & 0x80U)) { return TOUCH_WAIT; }
  const unsigned count = status & 0x0FU;
  uint8_t xy[4];
  if (count != 0U && count <= 5U && touch_read(GT_FIRST_X, xy, sizeof(xy)) != HAL_OK)
  {
    return TOUCH_IO;
  }

  // 2. 坐标读取成功后清状态。只有芯片接收了清零,才交付这帧给渲染。
  uint8_t zero = 0U;
  if (HAL_I2C_Mem_Write(&touch, GT911_ADDRESS, GT_STATUS, I2C_MEMADD_SIZE_16BIT, &zero, 1U, 30U) != HAL_OK)
  {
    return TOUCH_IO;
  }
  if (count == 0U) { return TOUCH_UP; }
  if (count > 5U) { return TOUCH_BAD; }
  const TouchPoint raw = {read_le16(xy), read_le16(xy + 2U)};
  return map_touch(raw, sensor_size, target, point) ? TOUCH_DOWN : TOUCH_BAD;
}

#endif

编译和观察 ​

准备 GNU Arm、CMake、Ninja 和 STM32CubeF4 V1.28.3。将固件包目录换成本机路径后,在仓库根目录运行以下命令。

sh
cmake -S examples/stm32/fsmc/ili9341-touch -B .work/sky-ili9341-touch -G Ninja -DCUBE_F4_ROOT="D:/SDK/STM32Cube_FW_F4_V1.28.3"
cmake --build .work/sky-ili9341-touch

按住和松开时观察白色标记出现与消失。若拖动方向反了,结合 last_point 确认轴是否需要交换或镜像。屏上一直无标记时先核对 stage,再看 io_errors。

练习 ​

预测题 未置 ready 的一次轮询会让白点消失吗?

核对预测

未发布新状态时保留原显示,新帧报告零触点才擦除。

改动题 把标记左右各 4 像素改为 6。

核对改法

中间区域变成 13×13,边缘仍裁剪。更新主机检查中的边界预期。

验证记录 ​

窗口区域的新旧示例均已交叉链接。主机验证了四角映射、超界拒绝和矩形裁剪,屏幕和触摸实测待补。 Goodix GT911 编程指南

运行独立主机检查。

powershell
New-Item -ItemType Directory -Force .work | Out-Null
gcc -std=c11 -Wall -Wextra -Werror -pedantic examples/stm32/fsmc/ili9341-touch/check.c -o .work/ili9341-touch-check.exe
& ./.work/ili9341-touch-check.exe