gt911.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. * 2021-01-13 RiceChen the first version
  9. * 2022-02-25 Wayne optimization
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #define DBG_TAG "gt911"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #include "touch.h"
  17. #include "gt911.h"
  18. static struct rt_i2c_client gt911_client;
  19. /* hardware section */
  20. static rt_uint8_t GT911_CFG_TBL[] =
  21. {
  22. 0x6b, 0x00, 0x04, 0x58, 0x02, 0x05, 0x0d, 0x00, 0x01, 0x0f,
  23. 0x28, 0x0f, 0x50, 0x32, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00,
  24. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2a, 0x0c,
  25. 0x45, 0x47, 0x0c, 0x08, 0x00, 0x00, 0x00, 0x40, 0x03, 0x2c,
  26. 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x64, 0x32, 0x00, 0x00,
  27. 0x00, 0x28, 0x64, 0x94, 0xd5, 0x02, 0x07, 0x00, 0x00, 0x04,
  28. 0x95, 0x2c, 0x00, 0x8b, 0x34, 0x00, 0x82, 0x3f, 0x00, 0x7d,
  29. 0x4c, 0x00, 0x7a, 0x5b, 0x00, 0x7a, 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a,
  34. 0x08, 0x06, 0x04, 0x02, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  35. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36. 0x00, 0x00, 0x16, 0x18, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
  37. 0x22, 0x24, 0x13, 0x12, 0x10, 0x0f, 0x0a, 0x08, 0x06, 0x04,
  38. 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0x79, 0x01,
  41. };
  42. static void gt911_touch_up(void *buf, rt_int8_t id);
  43. static rt_err_t gt911_write_reg(struct rt_i2c_client *dev, rt_uint8_t *data, rt_uint8_t len)
  44. {
  45. struct rt_i2c_msg msgs;
  46. msgs.addr = dev->client_addr;
  47. msgs.flags = RT_I2C_WR;
  48. msgs.buf = data;
  49. msgs.len = len;
  50. if (rt_i2c_transfer(dev->bus, &msgs, 1) == 1)
  51. {
  52. return RT_EOK;
  53. }
  54. else
  55. {
  56. return -RT_ERROR;
  57. }
  58. }
  59. static rt_err_t gt911_read_regs(struct rt_i2c_client *dev, rt_uint8_t *reg, rt_uint8_t *data, rt_uint8_t len)
  60. {
  61. struct rt_i2c_msg msgs[2];
  62. msgs[0].addr = dev->client_addr;
  63. msgs[0].flags = RT_I2C_WR;
  64. msgs[0].buf = reg;
  65. msgs[0].len = GT911_REGITER_LEN;
  66. msgs[1].addr = dev->client_addr;
  67. msgs[1].flags = RT_I2C_RD;
  68. msgs[1].buf = data;
  69. msgs[1].len = len;
  70. if (rt_i2c_transfer(dev->bus, msgs, 2) == 2)
  71. {
  72. return RT_EOK;
  73. }
  74. else
  75. {
  76. return -RT_ERROR;
  77. }
  78. }
  79. static rt_err_t gt911_get_product_id(struct rt_i2c_client *dev, rt_uint8_t *data, rt_uint8_t len)
  80. {
  81. rt_uint8_t reg[2];
  82. reg[0] = (rt_uint8_t)(GT911_PRODUCT_ID >> 8);
  83. reg[1] = (rt_uint8_t)(GT911_PRODUCT_ID & 0xff);
  84. if (gt911_read_regs(dev, reg, data, len) != RT_EOK)
  85. {
  86. LOG_E("read id failed");
  87. return -RT_ERROR;
  88. }
  89. return RT_EOK;
  90. }
  91. static rt_err_t gt911_get_info(struct rt_i2c_client *dev, struct rt_touch_info *info)
  92. {
  93. rt_uint8_t reg[2];
  94. rt_uint8_t out_info[7];
  95. rt_uint8_t out_len = 7;
  96. reg[0] = (rt_uint8_t)(GT911_CONFIG_REG >> 8);
  97. reg[1] = (rt_uint8_t)(GT911_CONFIG_REG & 0xFF);
  98. if (gt911_read_regs(dev, reg, out_info, out_len) != RT_EOK)
  99. {
  100. LOG_E("read info failed");
  101. return -RT_ERROR;
  102. }
  103. info->range_x = (out_info[2] << 8) | out_info[1];
  104. info->range_y = (out_info[4] << 8) | out_info[3];
  105. info->point_num = out_info[5] & 0x0f;
  106. return RT_EOK;
  107. }
  108. static rt_err_t gt911_soft_reset(struct rt_i2c_client *dev)
  109. {
  110. rt_uint8_t buf[3];
  111. buf[0] = (rt_uint8_t)(GT911_COMMAND_REG >> 8);
  112. buf[1] = (rt_uint8_t)(GT911_COMMAND_REG & 0xFF);
  113. buf[2] = 0x02;
  114. if (gt911_write_reg(dev, buf, 3) != RT_EOK)
  115. {
  116. LOG_E("soft reset failed");
  117. return -RT_ERROR;
  118. }
  119. return RT_EOK;
  120. }
  121. static rt_int16_t pre_x[GT911_MAX_TOUCH];
  122. static rt_int16_t pre_y[GT911_MAX_TOUCH];
  123. static rt_int16_t pre_w[GT911_MAX_TOUCH];
  124. static rt_uint8_t s_tp_dowm[GT911_MAX_TOUCH];
  125. static void gt911_touch_up(void *buf, rt_int8_t id)
  126. {
  127. struct rt_touch_data *read_data = (struct rt_touch_data *)buf;
  128. if (s_tp_dowm[id] == 1)
  129. {
  130. s_tp_dowm[id] = 0;
  131. read_data[id].event = RT_TOUCH_EVENT_UP;
  132. }
  133. else
  134. {
  135. read_data[id].event = RT_TOUCH_EVENT_NONE;
  136. }
  137. read_data[id].timestamp = rt_touch_get_ts();
  138. read_data[id].width = pre_w[id];
  139. read_data[id].x_coordinate = pre_x[id];
  140. read_data[id].y_coordinate = pre_y[id];
  141. read_data[id].track_id = id;
  142. pre_x[id] = -1; /* last point is none */
  143. pre_y[id] = -1;
  144. pre_w[id] = -1;
  145. }
  146. static void gt911_touch_down(void *buf, rt_int8_t id, rt_int16_t x, rt_int16_t y, rt_int16_t w)
  147. {
  148. struct rt_touch_data *read_data = (struct rt_touch_data *)buf;
  149. if (s_tp_dowm[id] == 1)
  150. {
  151. read_data[id].event = RT_TOUCH_EVENT_MOVE;
  152. }
  153. else
  154. {
  155. read_data[id].event = RT_TOUCH_EVENT_DOWN;
  156. s_tp_dowm[id] = 1;
  157. }
  158. read_data[id].timestamp = rt_touch_get_ts();
  159. read_data[id].width = w;
  160. read_data[id].x_coordinate = x;
  161. read_data[id].y_coordinate = y;
  162. read_data[id].track_id = id;
  163. pre_x[id] = x; /* save last point */
  164. pre_y[id] = y;
  165. pre_w[id] = w;
  166. }
  167. static rt_int8_t pre_id[GT911_MAX_TOUCH];
  168. static rt_uint8_t pre_touch = 0;
  169. static rt_size_t gt911_read_point(struct rt_touch_device *touch, void *buf, rt_size_t read_num)
  170. {
  171. rt_uint8_t point_status = 0;
  172. rt_uint8_t touch_num = 0;
  173. rt_uint8_t write_buf[3];
  174. rt_uint8_t cmd[2];
  175. rt_uint8_t read_buf[8 * GT911_MAX_TOUCH] = {0};
  176. rt_uint8_t i;
  177. rt_int8_t read_id = 0;
  178. rt_int16_t input_x = 0;
  179. rt_int16_t input_y = 0;
  180. rt_int16_t input_w = 0;
  181. /* point status register */
  182. cmd[0] = (rt_uint8_t)((GT911_READ_STATUS >> 8) & 0xFF);
  183. cmd[1] = (rt_uint8_t)(GT911_READ_STATUS & 0xFF);
  184. if (gt911_read_regs(&gt911_client, cmd, &point_status, 1) != RT_EOK)
  185. {
  186. LOG_D("read point failed\n");
  187. read_num = 0;
  188. goto exit_;
  189. }
  190. if (point_status == 0) /* no data */
  191. {
  192. read_num = 0;
  193. goto exit_;
  194. }
  195. if ((point_status & 0x80) == 0) /* data is not ready */
  196. {
  197. read_num = 0;
  198. goto exit_;
  199. }
  200. touch_num = point_status & 0x0f; /* get point num */
  201. if (touch_num > GT911_MAX_TOUCH) /* point num is not correct */
  202. {
  203. read_num = 0;
  204. goto exit_;
  205. }
  206. cmd[0] = (rt_uint8_t)((GT911_POINT1_REG >> 8) & 0xFF);
  207. cmd[1] = (rt_uint8_t)(GT911_POINT1_REG & 0xFF);
  208. /* read point num is touch_num */
  209. if (gt911_read_regs(&gt911_client, cmd, read_buf, read_num * GT911_POINT_INFO_NUM) != RT_EOK)
  210. {
  211. LOG_D("read point failed\n");
  212. read_num = 0;
  213. goto exit_;
  214. }
  215. if (pre_touch > touch_num) /* point up */
  216. {
  217. for (i = 0; i < GT911_MAX_TOUCH; i++)
  218. {
  219. rt_uint8_t j;
  220. for (j = 0; j < touch_num; j++) /* this time touch num */
  221. {
  222. read_id = read_buf[j * 8] & 0x0F;
  223. if (pre_id[i] == read_id) /* this id is not free */
  224. break;
  225. }
  226. if ((j == touch_num) && (pre_id[i] != -1)) /* free this node */
  227. {
  228. gt911_touch_up(buf, pre_id[i]);
  229. pre_id[i] = -1;
  230. }
  231. }
  232. }
  233. if (touch_num > 0) /* point down */
  234. {
  235. rt_uint8_t off_set;
  236. for (i = 0; i < touch_num; i++)
  237. {
  238. off_set = i * 8;
  239. read_id = read_buf[off_set] & 0x0f;
  240. pre_id[i] = read_id;
  241. input_x = read_buf[off_set + 1] | (read_buf[off_set + 2] << 8); /* x */
  242. input_y = read_buf[off_set + 3] | (read_buf[off_set + 4] << 8); /* y */
  243. input_w = read_buf[off_set + 5] | (read_buf[off_set + 6] << 8); /* size */
  244. gt911_touch_down(buf, read_id, input_x, input_y, input_w);
  245. }
  246. }
  247. pre_touch = touch_num;
  248. exit_:
  249. write_buf[0] = (rt_uint8_t)((GT911_READ_STATUS >> 8) & 0xFF);
  250. write_buf[1] = (rt_uint8_t)(GT911_READ_STATUS & 0xFF);
  251. write_buf[2] = 0x00;
  252. gt911_write_reg(&gt911_client, write_buf, 3);
  253. return read_num;
  254. }
  255. static rt_err_t gt911_control(struct rt_touch_device *touch, int cmd, void *arg)
  256. {
  257. if (cmd == RT_TOUCH_CTRL_GET_ID)
  258. {
  259. return gt911_get_product_id(&gt911_client, arg, 6);
  260. }
  261. if (cmd == RT_TOUCH_CTRL_GET_INFO)
  262. {
  263. return gt911_get_info(&gt911_client, arg);
  264. }
  265. rt_uint8_t buf[4];
  266. rt_uint8_t i = 0;
  267. rt_uint8_t *config;
  268. config = (rt_uint8_t *)rt_calloc(1, sizeof(GT911_CFG_TBL) + GT911_REGITER_LEN);
  269. if (config == RT_NULL)
  270. {
  271. LOG_D("malloc config memory failed\n");
  272. return -RT_ERROR;
  273. }
  274. config[0] = (rt_uint8_t)((GT911_CONFIG_REG >> 8) & 0xFF);
  275. config[1] = (rt_uint8_t)(GT911_CONFIG_REG & 0xFF);
  276. rt_memcpy(&config[2], GT911_CFG_TBL, sizeof(GT911_CFG_TBL));
  277. switch (cmd)
  278. {
  279. case RT_TOUCH_CTRL_SET_X_RANGE:
  280. {
  281. rt_uint16_t x_range;
  282. x_range = *(rt_uint16_t *)arg;
  283. config[4] = (rt_uint8_t)(x_range >> 8);
  284. config[3] = (rt_uint8_t)(x_range & 0xff);
  285. GT911_CFG_TBL[2] = config[4];
  286. GT911_CFG_TBL[1] = config[3];
  287. break;
  288. }
  289. case RT_TOUCH_CTRL_SET_Y_RANGE:
  290. {
  291. rt_uint16_t y_range;
  292. y_range = *(rt_uint16_t *)arg;
  293. config[6] = (rt_uint8_t)(y_range >> 8);
  294. config[5] = (rt_uint8_t)(y_range & 0xff);
  295. GT911_CFG_TBL[4] = config[6];
  296. GT911_CFG_TBL[3] = config[5];
  297. break;
  298. }
  299. case RT_TOUCH_CTRL_SET_X_TO_Y:
  300. {
  301. config[8] ^= (1 << 3);
  302. break;
  303. }
  304. case RT_TOUCH_CTRL_SET_MODE:
  305. {
  306. rt_uint16_t trig_type;
  307. trig_type = *(rt_uint16_t *)arg;
  308. switch (trig_type)
  309. {
  310. case RT_DEVICE_FLAG_INT_RX:
  311. config[8] &= 0xFC;
  312. break;
  313. case RT_DEVICE_FLAG_RDONLY:
  314. config[8] &= 0xFC;
  315. config[8] |= 0x02;
  316. break;
  317. default:
  318. break;
  319. }
  320. break;
  321. }
  322. default:
  323. {
  324. break;
  325. }
  326. }
  327. if (gt911_write_reg(&gt911_client, config, sizeof(GT911_CFG_TBL) + GT911_ADDR_LEN) != RT_EOK)
  328. {
  329. LOG_D("send config failed");
  330. return -1;
  331. }
  332. buf[0] = (rt_uint8_t)((GT911_CHECK_SUM >> 8) & 0xFF);
  333. buf[1] = (rt_uint8_t)(GT911_CHECK_SUM & 0xFF);
  334. buf[2] = 0;
  335. for (i = GT911_ADDR_LEN; i < sizeof(GT911_CFG_TBL) + GT911_ADDR_LEN; i++)
  336. {
  337. buf[GT911_ADDR_LEN] += config[i];
  338. }
  339. buf[2] = (~buf[2]) + 1;
  340. buf[3] = 1;
  341. gt911_write_reg(&gt911_client, buf, 4);
  342. rt_free(config);
  343. return RT_EOK;
  344. }
  345. static struct rt_touch_ops gt911_touch_ops =
  346. {
  347. .touch_readpoint = gt911_read_point,
  348. .touch_control = gt911_control,
  349. };
  350. int rt_hw_gt911_init(const char *name, struct rt_touch_config *cfg)
  351. {
  352. struct rt_touch_device *touch_device = RT_NULL;
  353. rt_uint32_t bus_speed = 400000;
  354. touch_device = (struct rt_touch_device *)rt_malloc(sizeof(struct rt_touch_device));
  355. if (touch_device == RT_NULL)
  356. {
  357. LOG_E("touch device malloc fail");
  358. return -RT_ERROR;
  359. }
  360. rt_memset((void *)touch_device, 0, sizeof(struct rt_touch_device));
  361. /* hw init*/
  362. rt_pin_mode(*(rt_uint8_t *)cfg->user_data, PIN_MODE_OUTPUT);
  363. rt_pin_mode(cfg->irq_pin.pin, PIN_MODE_OUTPUT);
  364. rt_pin_write(*(rt_uint8_t *)cfg->user_data, PIN_LOW);
  365. rt_pin_write(cfg->irq_pin.pin, PIN_LOW);
  366. rt_thread_delay(10);
  367. rt_pin_write(*(rt_uint8_t *)cfg->user_data, PIN_HIGH);
  368. rt_thread_delay(10);
  369. rt_pin_write(cfg->irq_pin.pin, PIN_MODE_INPUT);
  370. rt_thread_delay(100);
  371. gt911_client.bus = (struct rt_i2c_bus_device *)rt_device_find(cfg->dev_name);
  372. if (gt911_client.bus == RT_NULL)
  373. {
  374. LOG_E("Can't find %s device", cfg->dev_name);
  375. return -RT_ERROR;
  376. }
  377. if (rt_device_open((rt_device_t)gt911_client.bus, RT_DEVICE_FLAG_RDWR) != RT_EOK)
  378. {
  379. LOG_E("open %s device failed", cfg->dev_name);
  380. return -RT_ERROR;
  381. }
  382. if (rt_device_control((rt_device_t)gt911_client.bus, RT_I2C_DEV_CTRL_CLK, &bus_speed) != RT_EOK)
  383. {
  384. LOG_E("control %s device failed", cfg->dev_name);
  385. return -RT_ERROR;
  386. }
  387. gt911_client.client_addr = GT911_ADDRESS_HIGH;
  388. gt911_soft_reset(&gt911_client);
  389. rt_memset(&pre_x[0], 0xff, GT911_MAX_TOUCH * sizeof(rt_int16_t));
  390. rt_memset(&pre_y[0], 0xff, GT911_MAX_TOUCH * sizeof(rt_int16_t));
  391. rt_memset(&pre_w[0], 0xff, GT911_MAX_TOUCH * sizeof(rt_int16_t));
  392. rt_memset(&s_tp_dowm[0], 0, GT911_MAX_TOUCH * sizeof(rt_int8_t));
  393. rt_memset(&pre_id[0], 0xff, GT911_MAX_TOUCH * sizeof(rt_uint8_t));
  394. /* register touch device */
  395. touch_device->info.type = RT_TOUCH_TYPE_CAPACITANCE;
  396. touch_device->info.vendor = RT_TOUCH_VENDOR_GT;
  397. rt_memcpy(&touch_device->config, cfg, sizeof(struct rt_touch_config));
  398. touch_device->ops = &gt911_touch_ops;
  399. rt_hw_touch_register(touch_device, name, RT_DEVICE_FLAG_INT_RX, RT_NULL);
  400. LOG_I("touch device gt911 init success");
  401. return RT_EOK;
  402. }