drv_touch_ft.c 4.5 KB

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