drv_ft5x06.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * File : drv_ft5x06.c
  3. * ft5x06 touch driver
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-08-08 Yang the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #ifdef RT_USING_FINSH
  27. #include <finsh.h>
  28. #endif
  29. #ifdef RT_USING_RTGUI
  30. #include <rtgui/event.h>
  31. #include <rtgui/rtgui_server.h>
  32. #endif
  33. #include "board.h"
  34. #define BSP_TOUCH_SAMPLE_HZ 30
  35. #define I2CBUS_NAME "i2c0"
  36. #define FT5x06_TS_ADDR 0x38
  37. #define TP_MAX_TOUCH_POINT 5
  38. #define DEBUG
  39. #ifdef DEBUG
  40. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  41. #else
  42. #define DEBUG_PRINTF(...)
  43. #endif
  44. #define CTRL_NOAUTO_MONITOR 0x00
  45. #define CTRL_AUTO_MONITOR 0x01
  46. #define PMODE_ACTIVE 0x00
  47. #define PMODE_MONITOR 0x01
  48. #define PMODE_STANDBY 0x02
  49. #define PMODE_HIBERNATE 0x03
  50. #define G_MODE_POLLING 0x00
  51. #define G_MODE_TRIGGER 0x01
  52. #define TOUCH_POINT_GET_EVENT(T) ((touch_event_t)((T).XH >> 6))
  53. #define TOUCH_POINT_GET_ID(T) ((T).YH >> 4)
  54. #define TOUCH_POINT_GET_X(T) ((((T).XH & 0x0f) << 8) | (T).XL)
  55. #define TOUCH_POINT_GET_Y(T) ((((T).YH & 0x0f) << 8) | (T).YL)
  56. typedef enum _touch_event
  57. {
  58. kTouch_Down = 0, /*!< The state changed to touched. */
  59. kTouch_Up = 1, /*!< The state changed to not touched. */
  60. kTouch_Contact = 2, /*!< There is a continuous touch being detected. */
  61. kTouch_Reserved = 3 /*!< No touch information available. */
  62. } touch_event_t;
  63. typedef struct _touch_point
  64. {
  65. touch_event_t TOUCH_EVENT; /*!< Indicates the state or event of the touch point. */
  66. uint8_t TOUCH_ID; /*!< Id of the touch point. This numeric value stays constant between down and up event. */
  67. uint16_t TOUCH_X; /*!< X coordinate of the touch point */
  68. uint16_t TOUCH_Y; /*!< Y coordinate of the touch point */
  69. } touch_point_t;
  70. typedef struct _ft5x06_touch_point
  71. {
  72. uint8_t XH;
  73. uint8_t XL;
  74. uint8_t YH;
  75. uint8_t YL;
  76. } ft5x06_touch_point_t;
  77. typedef struct _ft5x06_touch_data
  78. {
  79. uint8_t DEVIDE_MODE;
  80. uint8_t GEST_ID;
  81. uint8_t TD_STATUS;
  82. ft5x06_touch_point_t TOUCH;
  83. } ft5x06_touch_data_t;
  84. static struct rt_i2c_bus_device *_i2c_bus;
  85. static int _ft5x06_read(unsigned char cmd,
  86. void *buf,
  87. size_t len)
  88. {
  89. struct rt_i2c_msg msgs[2];
  90. msgs[0].addr = FT5x06_TS_ADDR;
  91. msgs[0].flags = RT_I2C_WR;
  92. msgs[0].buf = &cmd;
  93. msgs[0].len = sizeof(cmd);
  94. msgs[1].addr = FT5x06_TS_ADDR;
  95. msgs[1].flags = RT_I2C_RD;
  96. msgs[1].buf = buf;
  97. msgs[1].len = len;
  98. if (rt_i2c_transfer(_i2c_bus, msgs, 2) == 2)
  99. return len;
  100. else
  101. return -1;
  102. }
  103. #ifdef RT_USING_FINSH
  104. static int search_ft5x06(void)
  105. {
  106. struct rt_i2c_msg msgs[2];
  107. uint8_t cmd = 0xA3;
  108. uint8_t buf = 0;
  109. msgs[0].flags = RT_I2C_WR;
  110. msgs[0].buf = &cmd;
  111. msgs[0].len = sizeof(cmd);
  112. msgs[1].flags = RT_I2C_RD;
  113. msgs[1].buf = &buf;
  114. msgs[1].len = 1;
  115. for (int i = 0; i <= 0x7f; i++)
  116. {
  117. int len;
  118. msgs[0].addr = i;
  119. msgs[1].addr = i;
  120. len = rt_i2c_transfer(_i2c_bus, msgs, 2);
  121. if (len == 2)
  122. {
  123. rt_kprintf("add:%02X transfer success, id: %02X\n", i, buf);
  124. }
  125. }
  126. return 0;
  127. }
  128. FINSH_FUNCTION_EXPORT_ALIAS(search_ft5x06, sft, search ft5x06 chip);
  129. static int ft5x06_dump(void)
  130. {
  131. uint8_t i;
  132. uint8_t reg_value;
  133. DEBUG_PRINTF("[FTS] Touch Chip\r\n");
  134. for (i = 0; i < UINT8_MAX; i++)
  135. {
  136. _ft5x06_read(i, &reg_value, 1);
  137. if (i % 8 == 7)
  138. DEBUG_PRINTF("0x%02X = 0x%02X\r\n", i, reg_value);
  139. else
  140. DEBUG_PRINTF("0x%02X = 0x%02X ", i, reg_value);
  141. }
  142. DEBUG_PRINTF("\n");
  143. return 0;
  144. }
  145. FINSH_FUNCTION_EXPORT_ALIAS(ft5x06_dump, ftdump, ft5x06 dump registers);
  146. #endif
  147. static int ft5x06_read_touch(touch_point_t *dp)
  148. {
  149. #if 0
  150. uint8_t data[33];
  151. int i;
  152. _ft5x06_read(0, data, sizeof(data));
  153. for (i = 0; i < sizeof(data)/sizeof(data[0]); i++)
  154. {
  155. DEBUG_PRINTF("%02X ", data[i]);
  156. }
  157. DEBUG_PRINTF("\n");
  158. return -1;
  159. #else
  160. ft5x06_touch_data_t touch_data;
  161. _ft5x06_read(0, &touch_data, sizeof(ft5x06_touch_data_t));
  162. dp->TOUCH_X = TOUCH_POINT_GET_Y(touch_data.TOUCH);
  163. dp->TOUCH_Y = TOUCH_POINT_GET_X(touch_data.TOUCH);
  164. DEBUG_PRINTF(" ==> status : %d (%d, %d)\n", touch_data.TD_STATUS, dp->TOUCH_X, dp->TOUCH_Y);
  165. if (touch_data.TD_STATUS != 0)
  166. return 0;
  167. else
  168. return -1;
  169. #endif
  170. }
  171. static void _touch_session()
  172. {
  173. touch_point_t tpd;
  174. #ifdef RT_USING_RTGUI
  175. struct rtgui_event_mouse emouse;
  176. #endif
  177. ft5x06_read_touch(&tpd);
  178. #ifdef RT_USING_RTGUI
  179. emouse.parent.sender = RT_NULL;
  180. emouse.wid = RT_NULL;
  181. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  182. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  183. emouse.x = tpd.TOUCH_X;
  184. emouse.y = tpd.TOUCH_Y;
  185. emouse.ts = rt_tick_get();
  186. emouse.id = emouse.ts;
  187. if (emouse.id == 0) emouse.id = 1;
  188. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  189. #endif
  190. do
  191. {
  192. rt_thread_delay(RT_TICK_PER_SECOND / BSP_TOUCH_SAMPLE_HZ);
  193. if (ft5x06_read_touch(&tpd) != 0)
  194. break;
  195. #ifdef RT_USING_RTGUI
  196. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  197. emouse.x = tpd.TOUCH_X;
  198. emouse.y = tpd.TOUCH_Y;
  199. emouse.ts = rt_tick_get();
  200. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  201. #endif
  202. }
  203. while (1);
  204. #ifdef RT_USING_RTGUI
  205. /* Always send touch up event. */
  206. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  207. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
  208. emouse.x = tpd.TOUCH_X;
  209. emouse.y = tpd.TOUCH_Y;
  210. emouse.ts = rt_tick_get();
  211. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  212. #endif
  213. }
  214. static void touch_entry(void *p)
  215. {
  216. GPIO_InitTypeDef GPIO_InitStruct;
  217. __HAL_RCC_GPIOH_CLK_ENABLE();
  218. /*Configure GPIO pin : PH7 */
  219. GPIO_InitStruct.Pin = GPIO_PIN_7;
  220. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  221. GPIO_InitStruct.Pull = GPIO_NOPULL;
  222. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  223. HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
  224. while(1)
  225. {
  226. rt_thread_delay(RT_TICK_PER_SECOND / 60);
  227. if (HAL_GPIO_ReadPin(GPIOH, GPIO_PIN_7) == GPIO_PIN_RESET)
  228. {
  229. _touch_session();
  230. }
  231. else
  232. continue;
  233. }
  234. }
  235. int ft5x06_hw_init(void)
  236. {
  237. rt_thread_t tid;
  238. rt_device_t dev;
  239. dev = rt_device_find(I2CBUS_NAME);
  240. if (!dev)
  241. {
  242. DEBUG_PRINTF("search device failed: %s\n", I2CBUS_NAME);
  243. return -1;
  244. }
  245. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  246. {
  247. DEBUG_PRINTF("open device failed: %s\n", I2CBUS_NAME);
  248. return -1;
  249. }
  250. DEBUG_PRINTF("ft5x06 set i2c bus to %s\n", I2CBUS_NAME);
  251. _i2c_bus = (struct rt_i2c_bus_device *)dev;
  252. tid = rt_thread_create("touch", touch_entry, RT_NULL, 2048, 10, 20);
  253. if (!tid)
  254. {
  255. rt_device_close(dev);
  256. return -1;
  257. }
  258. rt_thread_startup(tid);
  259. return 0;
  260. }
  261. INIT_APP_EXPORT(ft5x06_hw_init);