drv_touch_gt9xx.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2006-2018, 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 "drv_touch.h"
  15. #include "string.h"
  16. #ifdef TINA_USING_TOUCH
  17. #define TP_INT_PIN 89
  18. #define TP_RST_PIN 87
  19. #ifndef TP_INT_PIN
  20. #error "Please config touch panel INT pin."
  21. #endif
  22. #ifndef TP_RST_PIN
  23. #error "Please config touch panel RST pin."
  24. #endif
  25. #ifndef IIC_RETRY_NUM
  26. #define IIC_RETRY_NUM 2
  27. #endif
  28. #define GT9xx_TS_ADDR (0x5D)
  29. #define gt9xx_READ_XY_REG (0x814E) /* 坐标寄存器 */
  30. #define gt9xx_CLEARBUF_REG (0x814E) /* 清除坐标寄存器 */
  31. #define gt9xx_CONFIG_REG (0x8047) /* 配置参数寄存器 */
  32. #define gt9xx_COMMAND_REG (0x8040) /* 实时命令 */
  33. #define gt9xx_PRODUCT_ID_REG (0x8140) /* 产品ID */
  34. #define gt9xx_VENDOR_ID_REG (0x814A) /* 当前模组选项信息 */
  35. #define gt9xx_CONFIG_VERSION_REG (0x8047) /* 配置文件版本号 */
  36. #define gt9xx_CONFIG_CHECKSUM_REG (0x80FF) /* 配置文件校验码 */
  37. #define gt9xx_FIRMWARE_VERSION_REG (0x8144) /* 固件版本号 */
  38. #if 0
  39. #define TPDEBUG rt_kprintf
  40. #else
  41. #define TPDEBUG(...)
  42. #endif
  43. static struct touch_driver gt9xx_driver;
  44. void gt9xx_hw_reset(rt_uint8_t address)
  45. {
  46. rt_tick_t delay = rt_tick_from_millisecond(30);
  47. rt_pin_mode(TP_RST_PIN, PIN_MODE_OUTPUT);
  48. rt_pin_mode(TP_INT_PIN, PIN_MODE_OUTPUT);
  49. if (address == 0x5D)
  50. {
  51. rt_pin_write(TP_RST_PIN, PIN_LOW);
  52. rt_pin_write(TP_INT_PIN, PIN_LOW);
  53. rt_thread_delay(delay);
  54. rt_pin_write(TP_RST_PIN, PIN_HIGH);
  55. rt_pin_write(TP_INT_PIN, PIN_LOW);
  56. rt_thread_delay(delay);
  57. rt_pin_write(TP_INT_PIN, PIN_LOW);
  58. rt_thread_delay(delay);
  59. rt_pin_write(TP_INT_PIN, PIN_HIGH);
  60. }
  61. else
  62. {
  63. rt_pin_write(TP_RST_PIN, PIN_LOW);
  64. rt_pin_write(TP_INT_PIN, PIN_HIGH);
  65. rt_thread_delay(delay);
  66. rt_pin_write(TP_RST_PIN, PIN_HIGH);
  67. rt_pin_write(TP_INT_PIN, PIN_HIGH);
  68. rt_thread_delay(delay);
  69. rt_pin_write(TP_INT_PIN, PIN_LOW);
  70. rt_thread_delay(delay);
  71. rt_pin_write(TP_INT_PIN, PIN_HIGH);
  72. }
  73. }
  74. static void gt9xx_soft_reset(struct rt_i2c_bus_device *i2c_bus)
  75. {
  76. rt_uint8_t buf[3];
  77. buf[0] = (rt_uint8_t)((gt9xx_COMMAND_REG >> 8) & 0xFF);
  78. buf[1] = (rt_uint8_t)(gt9xx_COMMAND_REG & 0xFF);
  79. buf[2] = 0x02;
  80. rt_touch_write(GT9xx_TS_ADDR, buf, 3);
  81. }
  82. static rt_bool_t gt9xx_probe(struct rt_i2c_bus_device *i2c_bus)
  83. {
  84. rt_uint8_t cmd[2];
  85. rt_uint8_t buffer[5] = {0};
  86. gt9xx_hw_reset(GT9xx_TS_ADDR);
  87. //gt9xx_soft_reset(i2c_bus);
  88. rt_thread_delay(RT_TICK_PER_SECOND / 5);
  89. cmd[0] = (rt_uint8_t)((gt9xx_PRODUCT_ID_REG >> 8) & 0xFF);
  90. cmd[1] = (rt_uint8_t)(gt9xx_PRODUCT_ID_REG & 0xFF);
  91. if (rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, buffer, 4) != 0)
  92. {
  93. TPDEBUG("[TP] %s failed!\n", __func__);
  94. return RT_FALSE;
  95. }
  96. buffer[4] = '\0';
  97. TPDEBUG("%#X %#X %#X %#X %#X\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
  98. if(!rt_strcmp((const char*)buffer, "911"))
  99. {
  100. rt_kprintf("[TP] Found chip gt911\n");
  101. return RT_TRUE;
  102. }
  103. else if(!rt_strcmp((const char*)buffer, "9147"))
  104. {
  105. rt_kprintf("[TP] Found chip gt9147\n");
  106. return RT_TRUE;
  107. }
  108. else if(!rt_strcmp((const char*)buffer, "9157"))
  109. {
  110. rt_kprintf("[TP] Found chip gt9157\n");
  111. return RT_TRUE;
  112. }
  113. else
  114. {
  115. rt_kprintf("[TP] Uknow chip gt9xx device: [%s]\n", buffer);
  116. }
  117. return RT_FALSE;
  118. }
  119. static void gt9xx_init(struct rt_i2c_bus_device *i2c_bus)
  120. {
  121. rt_uint8_t buf = 0;
  122. rt_uint8_t cmd[2];
  123. gt9xx_driver.isr_sem = rt_sem_create("gt9xx", 0, RT_IPC_FLAG_FIFO);
  124. RT_ASSERT(gt9xx_driver.isr_sem);
  125. cmd[0] = (rt_uint8_t)((gt9xx_CONFIG_VERSION_REG >> 8) & 0xFF);
  126. cmd[1] = (rt_uint8_t)(gt9xx_CONFIG_VERSION_REG & 0xFF);
  127. rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1);
  128. rt_kprintf("[TP] GT9xx Config version: 0x%02X\n", buf);
  129. cmd[0] = (rt_uint8_t)((gt9xx_VENDOR_ID_REG >> 8) & 0xFF);
  130. cmd[1] = (rt_uint8_t)(gt9xx_VENDOR_ID_REG & 0xFF);
  131. rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1);
  132. rt_kprintf("[TP] GT9xx Sensor id: 0x%02X\n", buf);
  133. rt_sem_release(gt9xx_driver.isr_sem);
  134. }
  135. static void gt9xx_deinit(void)
  136. {
  137. rt_sem_delete(gt9xx_driver.isr_sem);
  138. }
  139. static rt_err_t gt9xx_read_point(touch_message_t msg)
  140. {
  141. rt_uint8_t cmd[2];
  142. rt_uint8_t buf[8] = {0};
  143. static rt_uint8_t s_tp_down = 0;
  144. cmd[0] = (rt_uint8_t)((gt9xx_READ_XY_REG >> 8) & 0xFF);
  145. cmd[1] = (rt_uint8_t)(gt9xx_READ_XY_REG & 0xFF);
  146. rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, buf, 8);
  147. if((buf[0] & 0x01) == 0)
  148. {
  149. if(s_tp_down)
  150. {
  151. s_tp_down = 0;
  152. msg->event = TOUCH_EVENT_UP;
  153. }
  154. else
  155. {
  156. msg->event = TOUCH_EVENT_NONE;
  157. }
  158. }
  159. else
  160. {
  161. msg->x = ((rt_uint16_t)buf[3] << 8) | buf[2];
  162. msg->y = ((rt_uint16_t)buf[5] << 8) | buf[4];
  163. if(s_tp_down)
  164. {
  165. msg->event = TOUCH_EVENT_MOVE;
  166. }
  167. else
  168. {
  169. msg->event = TOUCH_EVENT_DOWN;
  170. s_tp_down = 1;
  171. }
  172. }
  173. buf[0] = ((gt9xx_CLEARBUF_REG >> 8) & 0xFF);
  174. buf[1] = (gt9xx_CLEARBUF_REG & 0xFF);
  175. buf[2] = 0x00;
  176. rt_touch_write(GT9xx_TS_ADDR, buf, 3);
  177. rt_sem_release(gt9xx_driver.isr_sem);
  178. return RT_EOK;
  179. }
  180. struct touch_ops gt9xx_ops =
  181. {
  182. .init = gt9xx_init,
  183. .deinit = gt9xx_deinit,
  184. .read_point = gt9xx_read_point,
  185. };
  186. static int gt9xx_driver_register(void)
  187. {
  188. gt9xx_driver.probe = gt9xx_probe;
  189. gt9xx_driver.ops = &gt9xx_ops;
  190. gt9xx_driver.user_data = RT_NULL;
  191. rt_touch_drivers_register(&gt9xx_driver);
  192. return RT_EOK;
  193. }
  194. INIT_ENV_EXPORT(gt9xx_driver_register);
  195. #endif