focaltech_ts.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * File : focaltech_ts.c
  3. * This file is part of RT-Thread RTOS
  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-01-01 Urey first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include <board.h>
  27. #include <drv_gpio.h>
  28. #include <rtgui/event.h>
  29. #include <rtgui/rtgui_server.h>
  30. #include <string.h>
  31. #ifdef RT_USING_FT6x06
  32. #include "focaltech_ts.h"
  33. #ifndef BIT
  34. #define BIT(n) (0x01u << (n))
  35. #endif
  36. static int fts_init_success = 0;
  37. #define TP_DEBUG 0
  38. #if TP_DEBUG
  39. #define TP_DBG(...) rt_kprintf("[TP]"),rt_kprintf(__VA_ARGS__)
  40. #else
  41. #define TP_DBG(...)
  42. #endif
  43. /*ic update info*/
  44. static struct Upgrade_Info fts_updateinfo[] =
  45. {
  46. {0x55,"FT5x06",TPD_MAX_POINTS_5,AUTO_CLB_NEED,50, 30, 0x79, 0x03, 10, 2000},
  47. {0x08,"FT5606",TPD_MAX_POINTS_5,AUTO_CLB_NEED,50, 10, 0x79, 0x06, 100, 2000},
  48. {0x0a,"FT5x16",TPD_MAX_POINTS_5,AUTO_CLB_NEED,50, 30, 0x79, 0x07, 10, 1500},
  49. {0x06,"FT6x06",TPD_MAX_POINTS_2,AUTO_CLB_NONEED,100, 30, 0x79, 0x08, 10, 2000},
  50. {0x36,"FT6x36",TPD_MAX_POINTS_2,AUTO_CLB_NONEED,10, 10, 0x79, 0x18, 10, 2000},
  51. {0x55,"FT5x06i",TPD_MAX_POINTS_5,AUTO_CLB_NEED,50, 30, 0x79, 0x03, 10, 2000},
  52. {0x14,"FT5336",TPD_MAX_POINTS_5,AUTO_CLB_NONEED,30, 30, 0x79, 0x11, 10, 2000},
  53. {0x13,"FT3316",TPD_MAX_POINTS_5,AUTO_CLB_NONEED,30, 30, 0x79, 0x11, 10, 2000},
  54. {0x12,"FT5436i",TPD_MAX_POINTS_5,AUTO_CLB_NONEED,30, 30, 0x79, 0x11, 10, 2000},
  55. {0x11,"FT5336i",TPD_MAX_POINTS_5,AUTO_CLB_NONEED,30, 30, 0x79, 0x11, 10, 2000},
  56. {0x54,"FT5x46",TPD_MAX_POINTS_5,AUTO_CLB_NONEED,2, 2, 0x54, 0x2c, 20, 2000},
  57. {0x58,"FT5x22",TPD_MAX_POINTS_5,AUTO_CLB_NONEED,2, 2, 0x58, 0x2c, 20, 2000},
  58. {0x59,"FT5x26",TPD_MAX_POINTS_5,AUTO_CLB_NONEED,30, 50, 0x79, 0x10, 1, 2000},
  59. };
  60. static struct Upgrade_Info fts_updateinfo_curr;
  61. static int touch_down_up_status = 0;
  62. #ifndef TOUCH_MAX_X
  63. # define TOUCH_MAX_X 480
  64. #endif
  65. #ifndef TOUCH_MAX_Y
  66. # define TOUCH_MAX_Y 320
  67. #endif
  68. #define ANDROID_INPUT_PROTOCOL_B
  69. #define FTS_RESET_PIN_NAME "ft3417-rst"
  70. #define FTS_INT_PIN_NAME "ft3417-int"
  71. static uint8_t buf_addr[2] = { 0 };
  72. static uint8_t buf_value[2] = { 0 };
  73. /************************************************************************
  74. * Name: fts_i2c_Read
  75. * Brief: i2c read
  76. * Input: i2c info, write buf, write len, read buf, read len
  77. * Output: get data in the 3rd buf
  78. * Return: fail <0
  79. ***********************************************************************/
  80. static int fts_i2c_Read(struct fts_ts_data *fts_ts, char *writebuf, int writelen, char *readbuf, int readlen)
  81. {
  82. struct rt_i2c_msg msgs[2];
  83. int ret;
  84. if (writelen > 0)
  85. {
  86. msgs[0].addr = fts_ts->addr;
  87. msgs[0].flags = RT_I2C_WR;
  88. msgs[0].len = writelen;
  89. msgs[0].buf = writebuf;
  90. msgs[1].addr = fts_ts->addr;
  91. msgs[1].flags = RT_I2C_RD;
  92. msgs[1].len = readlen;
  93. msgs[1].buf = readbuf;
  94. ret = rt_i2c_transfer(fts_ts->i2c_bus, msgs, 2);
  95. if (ret < 0)
  96. {
  97. TP_DBG("f%s: i2c read error. error code = %d \n", __func__, ret);
  98. }
  99. }
  100. else
  101. {
  102. msgs[0].addr = fts_ts->addr;
  103. msgs[0].flags = RT_I2C_RD;
  104. msgs[0].len = readlen;
  105. msgs[0].buf = readbuf;
  106. ret = rt_i2c_transfer(fts_ts->i2c_bus, msgs, 1);
  107. if (ret < 0)
  108. {
  109. TP_DBG("%s:i2c read error. error code = %d \n", __func__, ret);
  110. }
  111. }
  112. return ret;
  113. }
  114. /************************************************************************
  115. * Name: fts_i2c_Write
  116. * Brief: i2c write
  117. * Input: i2c info, write buf, write len
  118. * Output: no
  119. * Return: fail <0
  120. ***********************************************************************/
  121. static int fts_i2c_Write(struct fts_ts_data *fts_ts, char *writebuf, int writelen)
  122. {
  123. struct rt_i2c_msg msgs[2];
  124. int ret;
  125. msgs[0].addr = fts_ts->addr;
  126. msgs[0].flags = RT_I2C_WR;
  127. msgs[0].len = writelen;
  128. msgs[0].buf = writebuf;
  129. ret = rt_i2c_transfer(fts_ts->i2c_bus, msgs, 1);
  130. if (ret < 0)
  131. {
  132. TP_DBG("%s i2c write error.\n", __func__);
  133. }
  134. return ret;
  135. }
  136. /************************************************************************
  137. * Name: fts_read_Touchdata
  138. * Brief: report the point information
  139. * Input: event info
  140. * Output: get touch data in pinfo
  141. * Return: success is zero
  142. ***********************************************************************/
  143. static unsigned int buf_count_add=0;
  144. static unsigned int buf_count_neg=0;
  145. //unsigned int buf_count_add1;
  146. //unsigned int buf_count_neg1;
  147. static uint8_t buf_touch_data[30 * POINT_READ_BUF] = { 0 }; //0xFF
  148. static int fts_read_Touchdata(struct fts_ts_data *fts_ts)
  149. {
  150. struct fts_event *event = &fts_ts->event;
  151. uint8_t buf[POINT_READ_BUF] = { 0 }; //0xFF
  152. int ret = -1;
  153. int i = 0;
  154. uint8_t pointid = FTS_MAX_ID;
  155. //uint8_t pt00f=0;
  156. ret = fts_i2c_Read(fts_ts, buf, 1, buf, POINT_READ_BUF);
  157. if (ret < 0)
  158. {
  159. TP_DBG("%s read touchdata failed.\n", __func__);
  160. return ret;
  161. }
  162. buf_count_add++;
  163. //buf_count_add1=buf_count_add;
  164. rt_memcpy(buf_touch_data + (((buf_count_add - 1) % 30) * POINT_READ_BUF),
  165. buf, sizeof(uint8_t) * POINT_READ_BUF);
  166. return 0;
  167. }
  168. /************************************************************************
  169. * Name: fts_report_value
  170. * Brief: report the point information
  171. * Input: event info
  172. * Output: no
  173. * Return: success is 0(RT_EOK)
  174. ***********************************************************************/
  175. static struct rtgui_event_mouse emouse = {0};
  176. static int xx = 0, yy = 0, zz = 0;
  177. static int fts_report_value(struct fts_ts_data *fts_ts)
  178. {
  179. struct fts_event *event = &fts_ts->event;
  180. int i,result;
  181. int uppoint = 0;
  182. int touchs = 0;
  183. uint8_t pointid = FTS_MAX_ID;
  184. uint8_t buf[POINT_READ_BUF] = { 0 };//0xFF
  185. //struct rtgui_event_mouse emouse;
  186. static int touch_down = 0;
  187. buf_count_neg++;
  188. //buf_count_neg1=buf_count_neg;
  189. rt_memcpy(buf,
  190. buf_touch_data + (((buf_count_neg - 1) % 30) * POINT_READ_BUF),
  191. sizeof(uint8_t) * POINT_READ_BUF);
  192. rt_memset(event, 0, sizeof(struct fts_event));
  193. event->touch_point_num = buf[FT_TOUCH_POINT_NUM] & 0x0F;
  194. event->touch_point = 0;
  195. for (i = 0; i < fts_updateinfo_curr.TPD_MAX_POINTS; i++)
  196. {
  197. pointid = (buf[FTS_TOUCH_ID_POS + FTS_TOUCH_STEP * i]) >> 4;
  198. if (pointid >= FTS_MAX_ID)
  199. break;
  200. else
  201. event->touch_point++;
  202. #if TOUCH_SWAP_XY
  203. event->au16_y[i] = (((int16_t) buf[FTS_TOUCH_X_H_POS + FTS_TOUCH_STEP * i]) & 0x0F) << 8
  204. | (((int16_t) buf[FTS_TOUCH_X_L_POS + FTS_TOUCH_STEP * i])& 0xFF);
  205. event->au16_x[i] = (((int16_t) buf[FTS_TOUCH_Y_H_POS + FTS_TOUCH_STEP * i]) & 0x0F) << 8
  206. | (((int16_t) buf[FTS_TOUCH_Y_L_POS + FTS_TOUCH_STEP * i]) & 0xFF);
  207. #else
  208. event->au16_x[i] = (((int16_t) buf[FTS_TOUCH_X_H_POS + FTS_TOUCH_STEP * i]) & 0x0F) << 8
  209. | (((int16_t) buf[FTS_TOUCH_X_L_POS + FTS_TOUCH_STEP * i])& 0xFF);
  210. event->au16_y[i] = (((int16_t) buf[FTS_TOUCH_Y_H_POS + FTS_TOUCH_STEP * i]) & 0x0F) << 8
  211. | (((int16_t) buf[FTS_TOUCH_Y_L_POS + FTS_TOUCH_STEP * i]) & 0xFF);
  212. #endif
  213. #if TOUCH_SWAP_Y
  214. event->au16_y[i] = TOUCH_MAX_Y - event->au16_y[i];
  215. #endif
  216. TP_DBG("event->au16_x[%d] = %04x\n",i,event->au16_x[i]);
  217. TP_DBG("event->au16_y[%d] = %04x\n",i,event->au16_y[i]);
  218. event->au8_touch_event[i] = buf[FTS_TOUCH_EVENT_POS + FTS_TOUCH_STEP * i] >> 6;
  219. event->au8_finger_id[i] = (buf[FTS_TOUCH_ID_POS + FTS_TOUCH_STEP * i]) >> 4;
  220. event->pressure[i] = (buf[FTS_TOUCH_XY_POS + FTS_TOUCH_STEP * i]);//cannot constant value
  221. event->area[i] = (buf[FTS_TOUCH_MISC + FTS_TOUCH_STEP * i]) >> 4;
  222. if((event->au8_touch_event[i]==0 || event->au8_touch_event[i]==2)&&((event->touch_point_num==0)||(event->pressure[i]==0 && event->area[i]==0 )))
  223. return 1;
  224. #ifdef DEBUG
  225. TP_DBG("id=%d event=%d x=%d y=%d pressure=%d area=%d\n",
  226. event->au8_finger_id[i],
  227. event->au8_touch_event[i],
  228. event->au16_x[i],
  229. event->au16_y[i],
  230. event->pressure[i],
  231. event->area[i]);
  232. #endif
  233. }
  234. /*protocol B*/
  235. for (i = 0; i < event->touch_point; i++)
  236. {
  237. if (event->au8_touch_event[i]== 0 || event->au8_touch_event[i] == 2)
  238. {
  239. // input_mt_report_slot_state(fts_ts->input_dev, MT_TOOL_FINGER, true);
  240. // input_report_abs(fts_ts->input_dev, ABS_MT_PRESSURE, event->pressure[i]);
  241. // input_report_abs(fts_ts->input_dev, ABS_MT_TOUCH_MAJOR, event->area[i]);
  242. // input_report_abs(fts_ts->input_dev, ABS_MT_POSITION_X, event->au16_x[i]);
  243. // input_report_abs(fts_ts->input_dev, ABS_MT_POSITION_Y, event->au16_y[i]);
  244. touchs |= BIT(event->au8_finger_id[i]);
  245. fts_ts->touchs |= BIT(event->au8_finger_id[i]);
  246. TP_DBG("finger true\n");
  247. TP_DBG("report_abs_X = %d, report_abs_Y = %d !\n", event->au16_x[i], event->au16_y[i]);
  248. if(touch_down_up_status == 1)
  249. {
  250. //send mouse motion event;
  251. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  252. emouse.x = event->au16_x[0] > 479 ? 479 : event->au16_x[0];
  253. emouse.y = event->au16_y[0];
  254. emouse.ts = rt_tick_get();
  255. if (xx != 0 || yy != 0 || (xx == 0 && yy == 0))
  256. {
  257. if (xx != emouse.x || emouse.y != yy)
  258. {
  259. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  260. TP_DBG("RTGUI_EVENT_MOUSE_MOTION x=%d,y=%d\n",event->au16_x[0],event->au16_y[0]);
  261. zz = 0;
  262. }
  263. else
  264. {
  265. zz ++;
  266. }
  267. }
  268. xx = emouse.x;
  269. yy = emouse.y;
  270. if (zz >= 10)
  271. {
  272. xx = 0;
  273. yy = 0;
  274. }
  275. }
  276. }
  277. else
  278. {
  279. uppoint++;
  280. // input_mt_report_slot_state(fts_ts->input_dev, MT_TOOL_FINGER, false);
  281. fts_ts->touchs &= ~BIT(event->au8_finger_id[i]);
  282. }
  283. }
  284. if (0 == (fts_ts->touchs ^ touchs))
  285. {
  286. for (i = 0; i < CFG_MAX_TOUCH_POINTS; i++)
  287. {
  288. if (BIT(i) & (fts_ts->touchs ^ touchs))
  289. {
  290. // input_mt_slot(fts_ts->input_dev, i);
  291. // input_mt_report_slot_state(fts_ts->input_dev, MT_TOOL_FINGER, false);
  292. TP_DBG("finger false\n");
  293. }
  294. }
  295. }
  296. fts_ts->touchs = touchs;
  297. if(event->touch_point == uppoint && touch_down_up_status == 1)
  298. {
  299. // input_report_key(fts_ts->input_dev, BTN_TOUCH, 0);
  300. touch_down_up_status = 0;
  301. TP_DBG("touch up !\n");
  302. /* Always send touch up event. */
  303. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  304. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
  305. emouse.x = event->au16_x[0] > 479 ? 479 : event->au16_x[0];
  306. emouse.y = event->au16_y[0];
  307. emouse.ts = rt_tick_get();
  308. do
  309. {
  310. result = rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  311. if (result != RT_EOK)
  312. {
  313. rt_thread_delay(RT_TICK_PER_SECOND / 10);
  314. }
  315. }
  316. while (result != RT_EOK);
  317. TP_DBG("RTGUI_MOUSE_BUTTON_UP x=%d,y=%d\n",event->au16_x[0],event->au16_y[0]);
  318. }
  319. else
  320. {
  321. // input_report_key(fts_ts->input_dev, BTN_TOUCH, event->touch_point > 0);
  322. if (touch_down_up_status == 0)
  323. {
  324. touch_down_up_status = 1;
  325. TP_DBG("touch down !\n");
  326. //send mouse down event
  327. emouse.parent.sender = RT_NULL;
  328. emouse.wid = RT_NULL;
  329. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  330. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  331. emouse.x = event->au16_x[0] > 479 ? 479 : event->au16_x[0];
  332. emouse.y = event->au16_y[0];
  333. emouse.ts = rt_tick_get();
  334. emouse.id = emouse.ts;
  335. do
  336. {
  337. result = rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  338. if (result != RT_EOK)
  339. {
  340. rt_thread_delay(RT_TICK_PER_SECOND / 10);
  341. }
  342. }
  343. while (result != RT_EOK);
  344. TP_DBG("RTGUI_MOUSE_BUTTON_DOWN x=%d,y=%d\n",event->au16_x[0],event->au16_y[0]);
  345. }
  346. }
  347. return 0;
  348. }
  349. /************************************************************************
  350. * Name: fts_get_upgrade_array
  351. * Brief: decide which ic
  352. * Input: no
  353. * Output: get ic info in fts_updateinfo_curr
  354. * Return: no
  355. ***********************************************************************/
  356. static void fts_get_upgrade_array(struct fts_ts_data *fts_ts)
  357. {
  358. uint8_t reg_ofs;
  359. uint8_t chip_id;
  360. uint32_t i;
  361. reg_ofs = FTS_REG_CHIP_ID;
  362. fts_i2c_Read(fts_ts,&reg_ofs,1,&chip_id,1);
  363. TP_DBG("%s chip_id = %x\n", __func__, chip_id);
  364. for (i = 0; i < sizeof(fts_updateinfo) / sizeof(struct Upgrade_Info); i++)
  365. {
  366. if (chip_id == fts_updateinfo[i].CHIP_ID)
  367. {
  368. memcpy(&fts_updateinfo_curr, &fts_updateinfo[i], sizeof(struct Upgrade_Info));
  369. break;
  370. }
  371. }
  372. if(i >= sizeof(fts_updateinfo)/sizeof(struct Upgrade_Info))
  373. {
  374. memcpy(&fts_updateinfo_curr, &fts_updateinfo[0], sizeof(struct Upgrade_Info));
  375. }
  376. }
  377. /************************************************************************
  378. * Name: fts_ts_probe
  379. * Brief: driver entrance function for initial/power on/create channel
  380. * Input: i2c info, device id
  381. * Output: no
  382. * Return: 0
  383. ***********************************************************************/
  384. int fts_ts_probe(struct fts_ts_data *fts_ts,struct rt_i2c_bus_device *i2c_bus, const uint8_t addr)
  385. {
  386. int err = 0;
  387. uint8_t uc_reg_value;
  388. uint8_t uc_reg_addr;
  389. TP_DBG("FT device prob process Start !\n");
  390. fts_ts->i2c_bus = i2c_bus;
  391. fts_ts->addr = addr;
  392. fts_ts->init_success = 0;
  393. if (fts_ts->x_max > TOUCH_MAX_X)
  394. fts_ts->x_max = TOUCH_MAX_X;
  395. if (fts_ts->y_max > TOUCH_MAX_Y)
  396. fts_ts->y_max = TOUCH_MAX_Y;
  397. fts_get_upgrade_array(fts_ts);
  398. /*get some register information */
  399. uc_reg_addr = FTS_REG_FW_VER;
  400. err = fts_i2c_Read(fts_ts, &uc_reg_addr, 1, &uc_reg_value, 1);
  401. if (err < 0)
  402. {
  403. fts_ts->init_success = 0;
  404. fts_ts->fw_ver = 0xff;
  405. }
  406. else
  407. {
  408. fts_ts->init_success = 1;
  409. TP_DBG("Firmware version = 0x%x\n", uc_reg_value);
  410. fts_ts->fw_ver = uc_reg_value;
  411. }
  412. uc_reg_addr = FTS_REG_POINT_RATE;
  413. err = fts_i2c_Read(fts_ts, &uc_reg_addr, 1, &uc_reg_value, 1);
  414. if (err < 0)
  415. fts_ts->init_success = 0;
  416. else
  417. {
  418. fts_ts->init_success = 1;
  419. TP_DBG("report rate is %dHz.\n", uc_reg_value * 10);
  420. }
  421. uc_reg_addr = FTS_REG_THGROUP;
  422. err = fts_i2c_Read(fts_ts, &uc_reg_addr, 1, &uc_reg_value, 1);
  423. if (err < 0)
  424. fts_ts->init_success = 0;
  425. else
  426. {
  427. fts_ts->init_success = 1;
  428. TP_DBG("touch threshold is %d.\n", uc_reg_value * 4);
  429. }
  430. uc_reg_addr = FTS_REG_VENDOR_ID;
  431. err = fts_i2c_Read(fts_ts, &uc_reg_addr, 1, &uc_reg_value, 1);
  432. if (err < 0)
  433. fts_ts->init_success = 0;
  434. else
  435. {
  436. fts_ts->init_success = 1;
  437. TP_DBG("VENDOR ID = 0x%x\n", uc_reg_value);
  438. }
  439. if (fts_ts->init_success == 1)
  440. fts_init_success = 1;
  441. return 0;
  442. }
  443. /************************************************************************
  444. * Name: fts_ts_interrupt
  445. * Brief: the focaltech device will signal the host about TRIGGER_FALLING, and processed when the interrupt is asserted.
  446. * Input: irq, device id
  447. * Output: no
  448. * Return: irq handle
  449. ***********************************************************************/
  450. static void fts_ts_interrupt(struct fts_ts_data *fts_ts)
  451. {
  452. rt_sem_release(&fts_ts->sem);
  453. return ;
  454. }
  455. static void thread_fts_ts_service(void *param)
  456. {
  457. struct fts_ts_data *fts_ts = (struct fts_ts_data *)param;
  458. int ret = 0;
  459. while(rt_sem_take(&fts_ts->sem,RT_WAITING_FOREVER) == RT_EOK)
  460. {
  461. #ifdef FTS_GESTRUE
  462. i2c_smbus_read_i2c_block_data(fts_ts->client, 0xd0, 1, &state);
  463. /*TP_DBG("tpd fts_read_Gestruedata state=%d\n", state);*/
  464. if (state == 1)
  465. {
  466. fts_read_Gestruedata(fts_ts);
  467. rt_hw_interrupt_umask(fts_ts->irq);
  468. /*continue;*/
  469. }
  470. else
  471. {
  472. #endif
  473. //disable_irq_nosync(fts_ts->irq);
  474. ret = fts_read_Touchdata(fts_ts);
  475. if (ret == 0)
  476. fts_report_value(fts_ts);
  477. rt_thread_delay(RT_TICK_PER_SECOND / 30);
  478. #ifdef FTS_GESTRUE
  479. }
  480. #endif
  481. }
  482. }
  483. static struct fts_ts_data g_fts_ts =
  484. {
  485. .addr = 0,
  486. .fw_ver = 0, //firmware version
  487. .x_min = 0,
  488. .x_max = 480,
  489. .y_min = 0,
  490. .y_max = 320,
  491. .touchs = 0,
  492. .init_success = 0,
  493. };
  494. int rt_hw_touch_init(void)
  495. {
  496. struct rt_i2c_bus_device *i2c_bus;
  497. #define TP_INT_PORT GPIO_PORT_C
  498. #define TP_INT_PIN GPIO_Pin_25
  499. /* init IO */
  500. gpio_direction_input(TP_INT_PORT,TP_INT_PIN);
  501. gpio_enable_pull(TP_INT_PORT,TP_INT_PIN);
  502. /* register irq */
  503. gpio_mask_irq(TP_INT_PORT,TP_INT_PIN);
  504. gpio_set_func(TP_INT_PORT,TP_INT_PIN,GPIO_INPUT_PULL | GPIO_INT_FE);
  505. gpio_set_irq_callback(TP_INT_PORT,TP_INT_PIN,fts_ts_interrupt, (void*)&g_fts_ts);
  506. /* try to probe device */
  507. i2c_bus = rt_i2c_bus_device_find("i2c0");
  508. if (i2c_bus == RT_NULL)
  509. {
  510. rt_kprintf("[TP]:can't find the i2c bus:%s\n", "i2c0");
  511. return -RT_EIO;
  512. }
  513. fts_ts_probe(&g_fts_ts,i2c_bus,FTS_SLAVE_ADDR);
  514. if(g_fts_ts.init_success == 1)
  515. {
  516. rt_thread_t tid;
  517. /* init semaphore wakeup thread... */
  518. rt_sem_init(&g_fts_ts.sem,"tp_sem",0,RT_IPC_FLAG_FIFO);
  519. /* create thread for fts device */
  520. tid = rt_thread_create("tp_srv",
  521. thread_fts_ts_service, (void *) &g_fts_ts,
  522. 2048,
  523. RT_TOUCH_THREAD_PRIORITY,
  524. 10);
  525. if(tid != RT_NULL)
  526. rt_thread_startup(tid);
  527. /* enable interrupt */
  528. gpio_unmask_irq(TP_INT_PORT,TP_INT_PIN);
  529. return RT_EOK;
  530. }
  531. return -RT_EIO;
  532. }
  533. INIT_DEVICE_EXPORT(rt_hw_touch_init);
  534. #endif /* RT_USING_FT6x06 */