drv_touch_ft6206.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * 2017-08-08 Yang the first version
  9. * 2019-04-23 WillianChan porting to ft6206
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <rtdevice.h>
  14. #include "drv_touch.h"
  15. #include <stdint.h>
  16. #include <string.h>
  17. #ifdef BSP_USING_TOUCH_FT6206
  18. #define DBG_ENABLE
  19. #define DBG_SECTION_NAME "TOUCH.ft"
  20. #define DBG_LEVEL TOUCH_DBG_LEVEL
  21. #define DBG_COLOR
  22. #include <rtdbg.h>
  23. static struct rt_i2c_bus_device *ft_i2c_bus;
  24. static struct touch_drivers ft_driver;
  25. static int ft_read(struct rt_i2c_bus_device *i2c_bus, rt_uint8_t addr, rt_uint8_t *buffer, rt_size_t length)
  26. {
  27. int ret = -1;
  28. int retries = 0;
  29. struct rt_i2c_msg msgs[] =
  30. {
  31. {
  32. .addr = ft_driver.address,
  33. .flags = RT_I2C_WR,
  34. .len = 1,
  35. .buf = &addr,
  36. },
  37. {
  38. .addr = ft_driver.address,
  39. .flags = RT_I2C_RD,
  40. .len = length,
  41. .buf = buffer,
  42. },
  43. };
  44. while (retries < IIC_RETRY_NUM)
  45. {
  46. ret = rt_i2c_transfer(i2c_bus, msgs, 2);
  47. if (ret == 2)break;
  48. retries++;
  49. }
  50. if (retries >= IIC_RETRY_NUM)
  51. {
  52. LOG_E("%s i2c read error: %d", __func__, ret);
  53. return -1;
  54. }
  55. return ret;
  56. }
  57. static void ft_write(touch_drv_t driver, struct rt_i2c_bus_device *i2c_bus, rt_uint8_t addr, rt_uint8_t *buffer, rt_size_t length)
  58. {
  59. rt_uint8_t *send_buffer = rt_malloc(length + 1);
  60. RT_ASSERT(send_buffer);
  61. send_buffer[0] = addr;
  62. memcpy(send_buffer + 1, buffer, length);
  63. struct rt_i2c_msg msgs[] =
  64. {
  65. {
  66. .addr = ft_driver.address,
  67. .flags = RT_I2C_WR,
  68. .len = length + 1,
  69. .buf = send_buffer,
  70. }
  71. };
  72. length = rt_i2c_transfer(i2c_bus, msgs, 1);
  73. rt_free(send_buffer);
  74. send_buffer = RT_NULL;
  75. }
  76. static void ft_isr_enable(rt_bool_t enable)
  77. {
  78. rt_pin_irq_enable(BSP_TOUCH_INT_PIN, enable);
  79. }
  80. static void ft_touch_isr(void *parameter)
  81. {
  82. ft_isr_enable(RT_FALSE);
  83. rt_sem_release(ft_driver.isr_sem);
  84. }
  85. static rt_err_t ft_read_point(touch_msg_t msg)
  86. {
  87. int ret = -1;
  88. uint8_t point_num = 0;
  89. static uint8_t s_tp_down = 0;
  90. uint8_t point[6];
  91. ret = ft_read(ft_i2c_bus, 0x02, &point_num, 1);
  92. if (ret < 0)
  93. {
  94. return RT_ERROR;
  95. }
  96. if (point_num == 0)
  97. {
  98. if (s_tp_down)
  99. {
  100. s_tp_down = 0;
  101. msg->event = TOUCH_EVENT_UP;
  102. return RT_EOK;
  103. }
  104. msg->event = TOUCH_EVENT_NONE;
  105. return RT_ERROR;
  106. }
  107. ret = ft_read(ft_i2c_bus, 0x03, point, 6);
  108. if (ret < 0)
  109. {
  110. return RT_ERROR;
  111. }
  112. msg->y = (point[0]&0x0F) << 8 | point[1];
  113. msg->x = (point[2]&0x0F) << 8 | point[3];
  114. if (s_tp_down)
  115. {
  116. msg->event = TOUCH_EVENT_MOVE;
  117. return RT_EOK;
  118. }
  119. msg->event = TOUCH_EVENT_DOWN;
  120. s_tp_down = 1;
  121. return RT_EOK;
  122. }
  123. static void ft_init(struct rt_i2c_bus_device *i2c_bus)
  124. {
  125. if (ft_i2c_bus == RT_NULL)
  126. {
  127. ft_i2c_bus = i2c_bus;
  128. }
  129. ft_driver.isr_sem = rt_sem_create("ft", 0, RT_IPC_FLAG_FIFO);
  130. RT_ASSERT(ft_driver.isr_sem);
  131. rt_pin_mode(BSP_TOUCH_INT_PIN, PIN_MODE_INPUT_PULLUP);
  132. rt_pin_attach_irq(BSP_TOUCH_INT_PIN, PIN_IRQ_MODE_FALLING, ft_touch_isr, RT_NULL);
  133. rt_thread_mdelay(200);
  134. }
  135. static void ft_deinit(void)
  136. {
  137. if (ft_driver.isr_sem)
  138. {
  139. rt_sem_delete(ft_driver.isr_sem);
  140. ft_driver.isr_sem = RT_NULL;
  141. }
  142. }
  143. struct touch_ops ft_ops =
  144. {
  145. ft_isr_enable,
  146. ft_read_point,
  147. ft_init,
  148. ft_deinit,
  149. };
  150. static rt_bool_t ft_probe(struct rt_i2c_bus_device *i2c_bus)
  151. {
  152. int err = 0;
  153. uint8_t cid = 0xFF;
  154. ft_i2c_bus = i2c_bus;
  155. /* FT6206 Chip identification register address is 0xA8 */
  156. err = ft_read(ft_i2c_bus, 0xA8, (uint8_t *)&cid, 1);
  157. if (err < 0)
  158. {
  159. LOG_E("%s failed: %d", __func__, err);
  160. return RT_FALSE;
  161. }
  162. LOG_I("touch CID:0x%02X", cid);
  163. /* FT6206 ID Value is 0x11 */
  164. if(cid == 0x11)
  165. {
  166. return RT_TRUE;
  167. }
  168. return RT_FALSE;
  169. }
  170. int ft_driver_register(void)
  171. {
  172. /* TouchScreen FT6206 Slave I2C address is 0x54
  173. * 0x54 << 1 = 0x2A
  174. */
  175. ft_driver.address = 0x2A;
  176. ft_driver.probe = ft_probe;
  177. ft_driver.ops = &ft_ops;
  178. ft_driver.user_data = RT_NULL;
  179. rt_touch_drivers_register(&ft_driver);
  180. return 0;
  181. }
  182. INIT_DEVICE_EXPORT(ft_driver_register);
  183. #endif /* BSP_USING_TOUCH_FT6206 */