drv_touch_gt9xx.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-02-08 Zhangyihong the first version
  9. * 2018-04-03 XY gt9xx for 1024 * 600
  10. * 2018-04-14 liu2guang optimize int and rst to pin framework
  11. * 2017-08-08 XY imxrt1052
  12. * 2018-10-29 XY
  13. */
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include <touch.h>
  17. #include "drv_touch.h"
  18. #include "string.h"
  19. #include "drv_pin.h"
  20. #define DBG_TAG "TOUCH.gt9xx"
  21. #define DBG_LVL DBG_LOG
  22. #include <rtdbg.h>
  23. // extern tcon_panel_t _panel;
  24. #if 0
  25. #define TP_INT_PIN GET_PIN(GPIO_PORT_E, GPIO_PIN_10) /* GPIO_PORT_E GPIO_PIN_10 */
  26. #define TP_RST_PIN GET_PIN(GPIO_PORT_E, GPIO_PIN_11) /* GPIO_PORT_E GPIO_PIN_11 */
  27. #else
  28. #ifdef BSP_USING_MANGOPI // mango board
  29. #define TP_INT_PIN GET_PIN(GPIO_PORT_D, GPIO_PIN_22) /* GPIO_PORT_D GPIO_PIN_22 */
  30. #define TP_RST_PIN GET_PIN(GPIO_PORT_G, GPIO_PIN_11) /* GPIO_PORT_G GPIO_PIN_11 */
  31. #elif defined(BSP_USING_M7)
  32. #define TP_INT_PIN GET_PIN(GPIO_PORT_G, GPIO_PIN_14) /* GPIO_PORT_G GPIO_PIN_14 */
  33. #define TP_RST_PIN GET_PIN(GPIO_PORT_G, GPIO_PIN_12) /* GPIO_PORT_G GPIO_PIN_12 */
  34. #endif
  35. #endif
  36. #ifndef TP_INT_PIN
  37. #error "Please config touch panel INT pin."
  38. #endif
  39. #ifndef TP_RST_PIN
  40. #error "Please config touch panel RST pin."
  41. #endif
  42. #ifndef IIC_RETRY_NUM
  43. #define IIC_RETRY_NUM 2
  44. #endif
  45. #define GT9xx_Read_Interval (20) /* gt9xx 读取坐标最小间隔时间 */
  46. /* Either 0x5D or 0x14 could be used as the address */
  47. #define GT9xx_TS_ADDR1 (0x14)
  48. #define GT9xx_TS_ADDR2 (0x5D)
  49. static uint8_t GT9xx_TS_ADDR = GT9xx_TS_ADDR1;
  50. #define gt9xx_READ_XY_REG (0x814E) /* 坐标寄存器 */
  51. #define gt9xx_CLEARBUF_REG (0x814E) /* 清除坐标寄存器 */
  52. #define gt9xx_CONFIG_REG (0x8047) /* 配置参数寄存器 */
  53. #define gt9xx_COMMAND_REG (0x8040) /* 实时命令 */
  54. #define gt9xx_PRODUCT_ID_REG (0x8140) /* 产品ID */
  55. #define gt9xx_VENDOR_ID_REG (0x814A) /* 当前模组选项信息 */
  56. #define gt9xx_CONFIG_VERSION_REG (0x8047) /* 配置文件版本号 */
  57. #define gt9xx_CONFIG_CHECKSUM_REG (0x80FF) /* 配置文件校验码 */
  58. #define gt9xx_FIRMWARE_VERSION_REG (0x8144) /* 固件版本号 */
  59. static struct touch_driver gt9xx_driver;
  60. void gt9xx_hw_reset(rt_uint8_t address)
  61. {
  62. rt_tick_t delay = rt_tick_from_millisecond(30);
  63. rt_pin_mode(TP_RST_PIN, PIN_MODE_OUTPUT);
  64. rt_pin_mode(TP_INT_PIN, PIN_MODE_OUTPUT);
  65. if (address == 0x5D)
  66. {
  67. rt_pin_write(TP_RST_PIN, PIN_LOW);
  68. rt_pin_write(TP_INT_PIN, PIN_LOW);
  69. rt_thread_delay(delay);
  70. rt_pin_write(TP_RST_PIN, PIN_HIGH);
  71. rt_pin_write(TP_INT_PIN, PIN_LOW);
  72. rt_thread_delay(delay);
  73. rt_pin_write(TP_INT_PIN, PIN_LOW);
  74. rt_thread_delay(delay);
  75. rt_pin_write(TP_INT_PIN, PIN_HIGH);
  76. }
  77. else
  78. {
  79. rt_pin_write(TP_RST_PIN, PIN_LOW);
  80. rt_pin_write(TP_INT_PIN, PIN_HIGH);
  81. rt_thread_delay(delay);
  82. rt_pin_write(TP_RST_PIN, PIN_HIGH);
  83. rt_pin_write(TP_INT_PIN, PIN_HIGH);
  84. rt_thread_delay(delay);
  85. rt_pin_write(TP_INT_PIN, PIN_LOW);
  86. rt_thread_delay(delay);
  87. rt_pin_write(TP_INT_PIN, PIN_HIGH);
  88. }
  89. rt_thread_mdelay(5); /* more than 5ms */
  90. }
  91. static rt_bool_t gt9xx_probe(struct rt_i2c_bus_device *i2c_bus)
  92. {
  93. rt_uint8_t cmd[2];
  94. rt_uint8_t buffer[5] = {0};
  95. GT9xx_TS_ADDR = GT9xx_TS_ADDR1;
  96. gt9xx_hw_reset(GT9xx_TS_ADDR);
  97. rt_thread_delay(RT_TICK_PER_SECOND / 5);
  98. cmd[0] = (rt_uint8_t)((gt9xx_PRODUCT_ID_REG >> 8) & 0xFF);
  99. cmd[1] = (rt_uint8_t)(gt9xx_PRODUCT_ID_REG & 0xFF);
  100. if (rt_touch_read(GT9xx_TS_ADDR1, &cmd, 2, buffer, 4) != 0)
  101. {
  102. if (rt_touch_read(GT9xx_TS_ADDR2, &cmd, 2, buffer, 4) != 0)
  103. {
  104. LOG_E("Failed to fetch GT9XX ID at the address 0x%x/0x%x", GT9xx_TS_ADDR1, GT9xx_TS_ADDR2);
  105. return RT_FALSE;
  106. }
  107. GT9xx_TS_ADDR = GT9xx_TS_ADDR2;
  108. }
  109. buffer[4] = '\0';
  110. LOG_D("%#X %#X %#X %#X %#X", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
  111. if (!rt_strcmp((const char *)buffer, "911"))
  112. {
  113. LOG_I("Found chip gt911");
  114. return RT_TRUE;
  115. }
  116. else if (!rt_strcmp((const char *)buffer, "928"))
  117. {
  118. LOG_I("Found chip gt928");
  119. return RT_TRUE;
  120. }
  121. else if (!rt_strcmp((const char *)buffer, "9147"))
  122. {
  123. LOG_I("Found chip gt9147");
  124. return RT_TRUE;
  125. }
  126. else if (!rt_strcmp((const char *)buffer, "9157"))
  127. {
  128. LOG_I("Found chip gt9157");
  129. return RT_TRUE;
  130. }
  131. else
  132. {
  133. LOG_E("Uknow chip gt9xx device: [%s]", buffer);
  134. }
  135. return RT_FALSE;
  136. }
  137. static void gt9xx_init(struct rt_i2c_bus_device *i2c_bus)
  138. {
  139. rt_uint8_t buf = 0;
  140. rt_uint8_t cmd[2];
  141. cmd[0] = (rt_uint8_t)((gt9xx_CONFIG_VERSION_REG >> 8) & 0xFF);
  142. cmd[1] = (rt_uint8_t)(gt9xx_CONFIG_VERSION_REG & 0xFF);
  143. rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1);
  144. LOG_I("GT9xx Config version: 0x%02X", buf);
  145. cmd[0] = (rt_uint8_t)((gt9xx_VENDOR_ID_REG >> 8) & 0xFF);
  146. cmd[1] = (rt_uint8_t)(gt9xx_VENDOR_ID_REG & 0xFF);
  147. rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1);
  148. LOG_I("GT9xx Sensor id: 0x%02X", buf);
  149. }
  150. static void gt9xx_deinit(void)
  151. {
  152. }
  153. static rt_err_t gt9xx_read_point(struct rt_touch_data *touch_data, rt_size_t touch_num)
  154. {
  155. rt_uint8_t cmd[2];
  156. rt_uint8_t buf[8] = {0};
  157. static rt_uint8_t s_tp_down = 0;
  158. cmd[0] = (rt_uint8_t)((gt9xx_READ_XY_REG >> 8) & 0xFF);
  159. cmd[1] = (rt_uint8_t)(gt9xx_READ_XY_REG & 0xFF);
  160. rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, buf, 8);
  161. if ((buf[0] & 0x01) == 0)
  162. {
  163. if (s_tp_down)
  164. {
  165. s_tp_down = 0;
  166. touch_data->event = RT_TOUCH_EVENT_UP;
  167. }
  168. else
  169. {
  170. touch_data->event = RT_TOUCH_EVENT_NONE;
  171. }
  172. }
  173. else
  174. {
  175. touch_data->x_coordinate = ((rt_uint16_t)buf[3] << 8) | buf[2];
  176. touch_data->y_coordinate = ((rt_uint16_t)buf[5] << 8) | buf[4];
  177. // rt_kprintf("X:%d Y:%d\n", touch_data->x_coordinate, touch_data->y_coordinate);
  178. if (s_tp_down)
  179. {
  180. touch_data->event = RT_TOUCH_EVENT_MOVE;
  181. // rt_kprintf("s_tp_down\n");
  182. }
  183. else
  184. {
  185. touch_data->event = RT_TOUCH_EVENT_DOWN;
  186. s_tp_down = 1;
  187. }
  188. }
  189. buf[0] = ((gt9xx_CLEARBUF_REG >> 8) & 0xFF);
  190. buf[1] = (gt9xx_CLEARBUF_REG & 0xFF);
  191. buf[2] = 0x00;
  192. rt_touch_write(GT9xx_TS_ADDR, buf, 3);
  193. return RT_EOK;
  194. }
  195. struct touch_ops gt9xx_ops =
  196. {
  197. .init = gt9xx_init,
  198. .deinit = gt9xx_deinit,
  199. .read_point = gt9xx_read_point,
  200. };
  201. int gt9xx_driver_register(void)
  202. {
  203. gt9xx_driver.probe = gt9xx_probe;
  204. gt9xx_driver.ops = &gt9xx_ops;
  205. gt9xx_driver.read_interval = rt_tick_from_millisecond(GT9xx_Read_Interval);
  206. gt9xx_driver.check_mode = TOUCH_INT_MODE;
  207. gt9xx_driver.user_data = RT_NULL;
  208. rt_touch_drivers_register(&gt9xx_driver);
  209. return RT_EOK;
  210. }
  211. INIT_DEVICE_EXPORT(gt9xx_driver_register);