返回列表 发新帖我要提问本帖赏金: 30.00元(功能说明)

[AT32F405] 【AT-START-F405测评】B1.主题帖:基于USB HID的触摸屏设计

[复制链接]
 楼主| tinnu 发表于 2024-5-15 20:32 | 显示全部楼层 |阅读模式
### 功能简介
  • 基于USB HID协议,制作触摸屏驱动板
  • 触摸芯片为FT5406,支持最多5点触摸

### 开发简介

1. 创建 workbench 工程
基于VSCODE和EIDE在linux等跨平台上开发的操作详见上一篇帖子:
【AT-START-F405测评】A1.workbench + ATIDE / 纯VSOCDE+EIDE 开发环境搭建 点灯

- 在上一篇的帖子的基础上增加 USB 驱动、ACC 驱动、IIC驱动、USART驱动
    - 其中IIC驱动需要将引脚重映射
wb配置.png
2. 按照上一篇帖子的方法,倒入工程到EIDE环境中。
    - 加入工程所需的库文件、USB驱动库、开发板驱动文件
    - 加入EIDE倒入时缺失的.s文件
EIDE导入.png

3. I2C驱动介绍
    - FT5406的IIC 7位地址为0x38
    - 由于SDK库是使用8位地址操作,并且无需考虑读写位,在底层会自动值位
    - 初始化 FT5406 :对0地址写0
  1. int32_t FT5406_Init(i2c_handle_type *hi2cx)
  2. {
  3.   uint8_t tx_buf[8] = {0x0, 0x0};
  4.   i2c_status_type i2c_status;

  5.   if ((i2c_status = i2c_master_transmit(hi2cx, (FT5406_I2C_ADDR << 1), tx_buf, 2, 0xFFFFFFF)) != I2C_OK)
  6.   {
  7.     printf("err send\n");
  8.   }

  9.   return 0;
  10. }



    - 读取触摸信息

  1. int32_t FT5406_GetSingleTouch(i2c_handle_type *hi2cx, int *touch_count, touch_point_t touch_array[FT5406_MAX_TOUCHES])
  2. {
  3.   int32_t ret = 1;
  4.   uint8_t touch_buf[FT5406_TOUCH_DATA_LEN] = {0};
  5.   uint8_t tx_buf[8] = {0x1, 0x20};
  6.   i2c_status_type i2c_status;

  7.   if ((i2c_status = i2c_master_transmit(hi2cx, (FT5406_I2C_ADDR << 1), tx_buf, 1, 0xFFFFFFF)) != I2C_OK)
  8.   {
  9.     ret = 0;
  10.     printf("err send\n");
  11.   }
  12.   if ((i2c_status = i2c_master_receive(hi2cx, FT5406_I2C_ADDR << 1, touch_buf, FT5406_TOUCH_DATA_LEN, 0xFFFFFFF)) != I2C_OK)
  13.   {
  14.     ret = 0;
  15.     printf("err read\n");
  16.   }

  17.   if (ret == 1)
  18.   {
  19.     ft5406_touch_data_t *touch_data = (ft5406_touch_data_t *)(void *)(touch_buf);
  20.     int i;

  21.     /* Decode number of touches */
  22.     if (touch_count)
  23.     {
  24.       *touch_count = touch_data->TD_STATUS < 5 ? touch_data->TD_STATUS : 0;
  25.     }

  26.     /* Decode valid touch points */
  27.     for (i = 0; i < *touch_count; i++)
  28.     {
  29.       touch_array[i].TOUCH_ID = TOUCH_POINT_GET_ID(touch_data->TOUCH[i]);
  30.       touch_array[i].TOUCH_EVENT = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[i]);
  31.       touch_array[i].TOUCH_X = TOUCH_POINT_GET_X(touch_data->TOUCH[i]);
  32.       touch_array[i].TOUCH_Y = TOUCH_POINT_GET_Y(touch_data->TOUCH[i]);
  33.     }

  34.     /* Clear vacant elements of touch_array */
  35.     for (; i < FT5406_MAX_TOUCHES; i++)
  36.     {
  37.       touch_array[i].TOUCH_ID = 0;
  38.       touch_array[i].TOUCH_EVENT = kTouch_Reserved;
  39.       touch_array[i].TOUCH_X = 0;
  40.       touch_array[i].TOUCH_Y = 0;
  41.     }
  42.   }

  43.   return 0;
  44. }




4. USB HID驱动介绍
  •     由于FT5406支持最多5点触摸,因此HID协议的报告描述符也设置为5点。
  •     基于例程库里面的 CUSTOM HID 例程,创建一个 hid_print 驱动文件
  •     加入如下的 设备描述符、设备配置符、报告描述符


  •         设备描述符

        
  1. ALIGNED_HEAD static uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] ALIGNED_TAIL =
  2. {
  3.   USB_DEVICE_DESC_LEN,                   /* bLength */
  4.   USB_DESCIPTOR_TYPE_DEVICE,             /* bDescriptorType */
  5.   0x10,                                  /* bcdUSB */
  6.   0x01,
  7.   0x00,                                  /* bDeviceClass */
  8.   0x00,                                  /* bDeviceSubClass */
  9.   0x00,                                  /* bDeviceProtocol */
  10.   USB_MAX_EP0_SIZE,                      /* bMaxPacketSize */
  11.   LBYTE(USBD_CUSHID_VENDOR_ID),                 /* idVendor */
  12.   HBYTE(USBD_CUSHID_VENDOR_ID),                 /* idVendor */
  13.   LBYTE(USBD_CUSHID_PRODUCT_ID),                /* idProduct */
  14.   HBYTE(USBD_CUSHID_PRODUCT_ID),                /* idProduct */
  15.   0x00,                                  /* bcdDevice rel. 2.00 */
  16.   0x00,
  17.   USB_MFC_STRING,                        /* Index of manufacturer string */
  18.   USB_PRODUCT_STRING,                    /* Index of product string */
  19.   USB_LANGID_STRING,                     /* Index of serial number string */
  20.   1                                      /* bNumConfigurations */
  21. };



  • 设备配置符

  1. ALIGNED_HEAD static uint8_t g_usbd_configuration[USBD_CUSHID_CONFIG_DESC_SIZE] ALIGNED_TAIL =
  2. {
  3.         // 配置描述符
  4.   USB_DEVICE_CFG_DESC_LEN,               /* bLength: configuration descriptor size */
  5.   USB_DESCIPTOR_TYPE_CONFIGURATION,      /* bDescriptorType: configuration */
  6.   LBYTE(USBD_CUSHID_CONFIG_DESC_SIZE),          /* wTotalLength: bytes returned */
  7.   HBYTE(USBD_CUSHID_CONFIG_DESC_SIZE),          /* wTotalLength: bytes returned */
  8.   0x01,                                  /* bNumInterfaces: 1 interface */
  9.   0x01,                                  /* bConfigurationValue: configuration value */
  10.   0x00,                                  /* iConfiguration: index of string descriptor describing
  11.                                             the configuration */
  12.   0xA0,                                  /* bmAttributes: self powered and support remote wakeup */
  13.   0x32,                                  /* MaxPower 100 mA: this current is used for detecting vbus */

  14.         // 接口描述符
  15.   USB_DEVICE_IF_DESC_LEN,                /* bLength: interface descriptor size */
  16.   USB_DESCIPTOR_TYPE_INTERFACE,          /* bDescriptorType: interface descriptor type */
  17.   0x00,                                  /* bInterfaceNumber: number of interface */
  18.   0x00,                                  /* bAlternateSetting: alternate set */
  19.   0x01,                                  /* bNumEndpoints: number of endpoints */
  20.   USB_CLASS_CODE_HID,                    /* bInterfaceClass: class code hid */
  21.   0x00,                                  /* bInterfaceSubClass: subclass code */
  22.   0x00,                                  /* bInterfaceProtocol: protocol code */
  23.   0x00,                                  /* iInterface: index of string descriptor */

  24.         // HID类描述符
  25.   0x09,                                  /* bLength: size of HID descriptor in bytes */
  26.   HID_CLASS_DESC_HID,                    /* bDescriptorType: HID descriptor type */
  27.   LBYTE(CUSHID_BCD_NUM),
  28.   HBYTE(CUSHID_BCD_NUM),                 /* bcdHID: HID class specification release number */
  29.   0x00,                                  /* bCountryCode: hardware target conutry */
  30.   0x01,                                  /* bNumDescriptors: number of HID class descriptor to follow */
  31.   HID_CLASS_DESC_REPORT,                 /* bDescriptorType: report descriptor type */
  32.         0x86, 0x01,                                                                                                                /* wDescriptorLength: total length of reprot descriptor */

  33.         // 端点描述符
  34.         0x07, 0x05, 0x81, 0x03, THIS_ENDP0_SIZE, 0x00, 0x0A,  //端点描述符
  35. };



  • 报告描述符(过长,这里省略,详见代码)


  1. ALIGNED_HEAD uint8_t g_usbd_custom_hid_report[USBD_CUSHID_SIZ_REPORT_DESC] ALIGNED_TAIL =...



  • 将读取到的FT5406数据转化为 HID 协议


  1.     hidFlagLast = hidFlagNow;
  2.     hidFlagNow = 0;
  3.     for (size_t i = 0; i < 5; i++)
  4.     {
  5.       hidOneReport = &hidReport.w[i];
  6.       hidOneReport->id = i;
  7.       hidOneReport->isPressed = (touch_array[i].TOUCH_EVENT == kTouch_Down) || (touch_array[i].TOUCH_EVENT == kTouch_Contact);
  8.       if (hidOneReport->isPressed)
  9.       {
  10.         hidOneReport->x = touch_array[i].TOUCH_X;
  11.         hidOneReport->y = touch_array[i].TOUCH_Y;
  12.         hidOneReport->w = 30;
  13.         hidFlagNow++;
  14.       }
  15.       printf("%x,%x,%x,%x,%x\t", i, touch_array[i].TOUCH_EVENT,
  16.              touch_array[i].TOUCH_ID, touch_array[i].TOUCH_X, touch_array[i].TOUCH_Y);
  17.     }
  18.     printf("\n");
  19.     hidReport.num = hidFlagNow;



5. 驱动演示
    - 目前仅能在linux系统下驱动成功,可能是windows下需要打驱动和配置特殊的 product id 到设备描述符。
    - 演示: 21ic-at32f405评测6.gif
    - 代码:mcu_at32f405_hid_touch_screen



打赏榜单

ArteryMCU 打赏了 30.00 元 2024-06-07
理由:[F405开发板评测活动]内容优质

您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

72

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

15

主题

72

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部