st1663i.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. * 2022-04-11 Wayne First version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <string.h>
  13. #define DBG_TAG "st1663i"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #include "st1663i.h"
  17. static struct rt_i2c_client st1663i_client;
  18. static void st1663i_touch_up(void *buf, rt_int8_t id);
  19. static rt_err_t st1663i_write_reg(struct rt_i2c_client *dev, rt_uint8_t reg, rt_uint8_t value)
  20. {
  21. struct rt_i2c_msg msgs;
  22. rt_uint8_t buf[2];
  23. buf[0] = reg;
  24. buf[1] = value;
  25. msgs.addr = dev->client_addr;
  26. msgs.flags = RT_I2C_WR;
  27. msgs.buf = buf;
  28. msgs.len = sizeof(buf);
  29. if (rt_i2c_transfer(dev->bus, &msgs, 1) == 1)
  30. {
  31. return RT_EOK;
  32. }
  33. else
  34. {
  35. return -RT_ERROR;
  36. }
  37. }
  38. static rt_err_t st1663i_read_reg(struct rt_i2c_client *dev, rt_uint8_t reg, rt_uint8_t *data, rt_uint8_t len)
  39. {
  40. struct rt_i2c_msg msgs[2];
  41. msgs[0].addr = dev->client_addr;
  42. msgs[0].flags = RT_I2C_WR;
  43. msgs[0].buf = &reg;
  44. msgs[0].len = ST_REGITER_LEN;
  45. msgs[1].addr = dev->client_addr;
  46. msgs[1].flags = RT_I2C_RD;
  47. msgs[1].buf = data;
  48. msgs[1].len = len;
  49. if (rt_i2c_transfer(dev->bus, msgs, 2) == 2)
  50. {
  51. return RT_EOK;
  52. }
  53. else
  54. {
  55. return -RT_ERROR;
  56. }
  57. }
  58. static rt_int16_t pre_x[ST_MAX_TOUCH];
  59. static rt_int16_t pre_y[ST_MAX_TOUCH];
  60. static rt_int16_t pre_w[ST_MAX_TOUCH];
  61. static rt_uint8_t s_tp_dowm[ST_MAX_TOUCH];
  62. static void st1663i_touch_up(void *buf, rt_int8_t id)
  63. {
  64. struct rt_touch_data *read_data = (struct rt_touch_data *)buf;
  65. if (s_tp_dowm[id] == 1)
  66. {
  67. s_tp_dowm[id] = 0;
  68. read_data[id].event = RT_TOUCH_EVENT_UP;
  69. }
  70. else
  71. {
  72. read_data[id].event = RT_TOUCH_EVENT_NONE;
  73. }
  74. read_data[id].timestamp = rt_touch_get_ts();
  75. read_data[id].width = pre_w[id];
  76. read_data[id].x_coordinate = pre_x[id];
  77. read_data[id].y_coordinate = pre_y[id];
  78. read_data[id].track_id = id;
  79. pre_x[id] = -1; /* last point is none */
  80. pre_y[id] = -1;
  81. pre_w[id] = -1;
  82. //LOG_I("%s (%d)\n", __func__, id);
  83. }
  84. static void st1663i_touch_down(void *buf, rt_int8_t id, rt_int16_t x, rt_int16_t y, rt_int16_t w)
  85. {
  86. struct rt_touch_data *read_data = (struct rt_touch_data *)buf;
  87. if (s_tp_dowm[id] == 1)
  88. {
  89. read_data[id].event = RT_TOUCH_EVENT_MOVE;
  90. }
  91. else
  92. {
  93. read_data[id].event = RT_TOUCH_EVENT_DOWN;
  94. s_tp_dowm[id] = 1;
  95. }
  96. read_data[id].timestamp = rt_touch_get_ts();
  97. read_data[id].width = w;
  98. read_data[id].x_coordinate = x;
  99. read_data[id].y_coordinate = y;
  100. read_data[id].track_id = id;
  101. pre_x[id] = x; /* save last point */
  102. pre_y[id] = y;
  103. pre_w[id] = w;
  104. //LOG_I("%s (%d %d %d %d)\n", __func__, id, x, y, w );
  105. }
  106. static int8_t pre_id[ST_MAX_TOUCH];
  107. static S_ST_REGMAP sStRegMap;
  108. static rt_uint8_t pre_touch = 0;
  109. static rt_size_t st1663i_read_point(struct rt_touch_device *touch, void *buf, rt_size_t read_num)
  110. {
  111. int i;
  112. rt_err_t error = 0;
  113. rt_int32_t touch_event, touchid;
  114. RT_ASSERT(touch);
  115. RT_ASSERT(buf);
  116. RT_ASSERT(read_num != 0);
  117. RT_ASSERT(read_num <= ST_MAX_TOUCH);
  118. error = st1663i_read_reg(&st1663i_client, 0x10, (rt_uint8_t *)&sStRegMap, sizeof(sStRegMap));
  119. if (error)
  120. {
  121. LOG_E("get touch data failed, err:%d\n", error);
  122. goto exit_read_point;
  123. }
  124. if (sStRegMap.u8Fingers > ST_MAX_TOUCH)
  125. {
  126. LOG_E("FW report max point:%d > panel info. max:%d\n", sStRegMap.u8Fingers, ST_MAX_TOUCH);
  127. goto exit_read_point;
  128. }
  129. if (pre_touch > sStRegMap.u8Fingers) /* point up */
  130. {
  131. for (i = 0; i < ST_MAX_TOUCH; i++)
  132. {
  133. rt_uint8_t j;
  134. for (j = 0; j < sStRegMap.u8Fingers; j++) /* this time touch num */
  135. {
  136. touchid = i;
  137. if (pre_id[i] == touchid) /* this id is not free */
  138. break;
  139. }
  140. if ((j == sStRegMap.u8Fingers) && (pre_id[i] != -1)) /* free this node */
  141. {
  142. // LOG_I("free %d tid=%d\n", i, pre_id[i]);
  143. st1663i_touch_up(buf, pre_id[i]);
  144. pre_id[i] = -1;
  145. }
  146. }
  147. }
  148. for (i = 0; i < sStRegMap.u8Fingers; i++)
  149. {
  150. touch_event = sStRegMap.m_sTP[i].u8Valid;
  151. touchid = i;
  152. //LOG_I("(%d/%d) %d %d\n", i, sStRegMap.u8Fingers, touchid, touch_event);
  153. pre_id[i] = touchid;
  154. if (touch_event)
  155. {
  156. rt_uint16_t x, y, w;
  157. x = ((uint16_t)sStRegMap.m_sTP[i].u8X0_H << 8) | sStRegMap.m_sTP[i].m_u8X0_L;
  158. y = ((uint16_t)sStRegMap.m_sTP[i].u8Y0_H << 8) | sStRegMap.m_sTP[i].m_u8Y0_L;
  159. w = sStRegMap.m_sTP[i].m_u8Z;
  160. //LOG_I("[%d] (%d %d %d %d)\n", touch_event, touchid, x, y, w);
  161. if (x >= touch->info.range_x || y >= touch->info.range_y)
  162. {
  163. LOG_E("invalid position, X[%d,%u,%d], Y[%d,%u,%d]\n",
  164. 0, x, touch->info.range_x,
  165. 0, y, touch->info.range_y);
  166. continue;
  167. }
  168. st1663i_touch_down(buf, touchid, x, y, w);
  169. }
  170. else
  171. {
  172. // Up
  173. st1663i_touch_up(buf, touchid);
  174. }
  175. } // for (i = 0; i < sStRegMap.u8TDStatus; i++)
  176. pre_touch = sStRegMap.u8Fingers;
  177. return read_num;
  178. exit_read_point:
  179. pre_touch = 0;
  180. return 0;
  181. }
  182. static rt_err_t st1663i_control(struct rt_touch_device *touch, int cmd, void *arg)
  183. {
  184. switch (cmd)
  185. {
  186. case RT_TOUCH_CTRL_GET_INFO:
  187. {
  188. struct rt_touch_info *info = (struct rt_touch_info *)arg;
  189. RT_ASSERT(arg);
  190. rt_memcpy(info, &touch->info, sizeof(struct rt_touch_info));
  191. break;
  192. }
  193. case RT_TOUCH_CTRL_GET_ID:
  194. break;
  195. case RT_TOUCH_CTRL_SET_X_RANGE:
  196. break;
  197. case RT_TOUCH_CTRL_SET_Y_RANGE:
  198. break;
  199. case RT_TOUCH_CTRL_SET_X_TO_Y:
  200. break;
  201. case RT_TOUCH_CTRL_SET_MODE:
  202. break;
  203. default:
  204. break;
  205. }
  206. return RT_EOK;
  207. }
  208. static struct rt_touch_ops st1663i_touch_ops =
  209. {
  210. .touch_readpoint = st1663i_read_point,
  211. .touch_control = st1663i_control,
  212. };
  213. static void st1663i_init(struct rt_i2c_client *dev)
  214. {
  215. st1663i_write_reg(dev, 0x0, 0);
  216. }
  217. int rt_hw_st1663i_init(const char *name, struct rt_touch_config *cfg)
  218. {
  219. struct rt_touch_device *touch_device = RT_NULL;
  220. rt_uint32_t bus_speed = 400000;
  221. touch_device = (struct rt_touch_device *)rt_malloc(sizeof(struct rt_touch_device));
  222. if (touch_device == RT_NULL)
  223. {
  224. LOG_E("touch device malloc fail");
  225. return -RT_ERROR;
  226. }
  227. rt_memset((void *)touch_device, 0, sizeof(struct rt_touch_device));
  228. /* hw init*/
  229. rt_pin_mode(*(rt_uint8_t *)cfg->user_data, PIN_MODE_OUTPUT);
  230. rt_pin_write(*(rt_uint8_t *)cfg->user_data, PIN_LOW);
  231. rt_thread_delay(5);
  232. rt_pin_write(*(rt_uint8_t *)cfg->user_data, PIN_HIGH);
  233. rt_thread_delay(200);
  234. rt_pin_mode(cfg->irq_pin.pin, cfg->irq_pin.mode);
  235. st1663i_client.bus = (struct rt_i2c_bus_device *)rt_device_find(cfg->dev_name);
  236. if (st1663i_client.bus == RT_NULL)
  237. {
  238. LOG_E("Can't find %s device", cfg->dev_name);
  239. return -RT_ERROR;
  240. }
  241. if (rt_device_open((rt_device_t)st1663i_client.bus, RT_DEVICE_FLAG_RDWR) != RT_EOK)
  242. {
  243. LOG_E("open %s device failed", cfg->dev_name);
  244. return -RT_ERROR;
  245. }
  246. if (rt_device_control((rt_device_t)st1663i_client.bus, RT_I2C_DEV_CTRL_CLK, &bus_speed) != RT_EOK)
  247. {
  248. LOG_E("control %s device failed", cfg->dev_name);
  249. return -RT_ERROR;
  250. }
  251. st1663i_client.client_addr = ST1663I_ADDRESS;
  252. st1663i_init(&st1663i_client);
  253. rt_memset(&pre_x[0], 0xff, ST_MAX_TOUCH * sizeof(int16_t));
  254. rt_memset(&pre_y[0], 0xff, ST_MAX_TOUCH * sizeof(int16_t));
  255. rt_memset(&pre_w[0], 0xff, ST_MAX_TOUCH * sizeof(int16_t));
  256. rt_memset(&s_tp_dowm[0], 0, ST_MAX_TOUCH * sizeof(int16_t));
  257. rt_memset(&pre_id[0], 0xff, ST_MAX_TOUCH * sizeof(int8_t));
  258. /* register touch device */
  259. touch_device->info.type = RT_TOUCH_TYPE_CAPACITANCE;
  260. touch_device->info.vendor = RT_TOUCH_VENDOR_UNKNOWN;
  261. touch_device->info.range_x = BSP_LCD_WIDTH;
  262. touch_device->info.range_y = BSP_LCD_HEIGHT;
  263. touch_device->info.point_num = ST_MAX_TOUCH;
  264. rt_memcpy(&touch_device->config, cfg, sizeof(struct rt_touch_config));
  265. touch_device->ops = &st1663i_touch_ops;
  266. rt_hw_touch_register(touch_device, name, RT_DEVICE_FLAG_INT_RX, RT_NULL);
  267. LOG_I("touch device st1663i init success");
  268. return RT_EOK;
  269. }