drv_touch_gt9xx.c 7.0 KB

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