drv_touch_gt9xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * File : drv_touch_gt9xx.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 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. * 2018-02-08 Zhangyihong the first version
  23. * 2018-04-03 XY gt9xx for 1024 * 600
  24. * 2018-10-11 sundm75 ls1c for 480 * 272
  25. */
  26. #include "rtthread.h"
  27. #include "drv_touch.h"
  28. #include "string.h"
  29. #include "ls1c_gpio.h"
  30. #include "ls1c.h"
  31. #include "ls1c_pin.h"
  32. #ifdef TINA_USING_TOUCH
  33. #define TP_INT_PIN (89)
  34. #define TP_RESET_PIN (87)
  35. #define LED_PIN (52)
  36. #define gt9xx_READ_XY_REG 0x814E /* 坐标寄存器 当前检测到的触摸情况 */
  37. #define gt9xx_CLEARBUF_REG 0x814E /* 清除坐标寄存器 */
  38. #define gt9xx_CONFIG_REG 0x8047 /* 配置参数寄存器 */
  39. #define gt9xx_COMMAND_REG 0x8040 /* 实时命令 */
  40. #define gt9xx_PRODUCT_ID_REG 0x8140 /* productid */
  41. #define gt9xx_VENDOR_ID_REG 0x814A /* 当前模组选项信息 */
  42. #define gt9xx_CONFIG_VERSION_REG 0x8047 /* 配置文件版本号 */
  43. #define gt9xx_CONFIG_CHECKSUM_REG 0x80FF /* 配置文件校验码 */
  44. #define gt9xx_FIRMWARE_VERSION_REG 0x8144 /* 固件版本号 */
  45. #define IIC_RETRY_NUM 1
  46. void touch_down(void);
  47. void touch_mo(void);
  48. void touch_up(void);
  49. static struct rt_i2c_bus_device *gt9xx_i2c_bus;
  50. static void gt9xx_isr_enable(rt_bool_t enable);
  51. static rt_err_t gt9xx_read_point(touch_msg_t msg);
  52. static void gt9xx_init(struct rt_i2c_bus_device *i2c_bus);
  53. static void gt9xx_deinit(void);
  54. static int gt9xx_read_xy(void);
  55. static gpio_direction_output( int pin, int level)
  56. {
  57. gpio_init(pin, gpio_mode_output);
  58. gpio_set(pin, level);
  59. }
  60. static gpio_direction_input(int pin)
  61. {
  62. gpio_init(pin, gpio_mode_input);
  63. }
  64. static gpio_irq_enable( int pin)
  65. {
  66. int touch_irq = LS1C_GPIO_TO_IRQ(TP_INT_PIN);
  67. rt_hw_interrupt_umask(touch_irq);
  68. }
  69. static gpio_irq_disable(int pin)
  70. {
  71. int touch_irq = LS1C_GPIO_TO_IRQ(TP_INT_PIN);
  72. rt_hw_interrupt_mask(touch_irq);
  73. }
  74. static gpio_set_value(int pin, int level)
  75. {
  76. gpio_set(pin, level);
  77. }
  78. struct touch_ops gt9xx_ops =
  79. {
  80. gt9xx_isr_enable,
  81. gt9xx_read_point,
  82. gt9xx_init,
  83. gt9xx_deinit,
  84. };
  85. static struct touch_drivers gt9xx_driver;
  86. extern struct lcd_config lcd_config;
  87. static rt_uint8_t gt9xx_config[186];
  88. static int gt9xx_read(struct rt_i2c_bus_device *i2c_bus, rt_uint16_t addr, rt_uint8_t *buffer, rt_size_t length)
  89. {
  90. int ret = -1;
  91. int retries = 0;
  92. rt_uint8_t tmp_buf[2];
  93. struct rt_i2c_msg msgs[] =
  94. {
  95. {
  96. .addr = gt9xx_driver.address,
  97. .flags = RT_I2C_WR,
  98. .len = 2,
  99. .buf = tmp_buf,
  100. },
  101. {
  102. .addr = gt9xx_driver.address,
  103. .flags = RT_I2C_RD,
  104. .len = length,
  105. .buf = buffer,
  106. },
  107. };
  108. tmp_buf[0] = (rt_uint8_t)(addr >> 8);
  109. tmp_buf[1] = (rt_uint8_t)(addr);
  110. while (retries < IIC_RETRY_NUM)
  111. {
  112. ret = rt_i2c_transfer(i2c_bus, msgs, 2);
  113. ret = rt_i2c_transfer(i2c_bus, msgs, 2);
  114. ret = rt_i2c_transfer(i2c_bus, msgs, 2);
  115. ret = rt_i2c_transfer(i2c_bus, msgs, 2);
  116. if (ret == 2)break;
  117. retries++;
  118. }
  119. if (retries >= IIC_RETRY_NUM)
  120. {
  121. rt_kprintf("%s i2c read error: %d\n", __func__, ret);
  122. return -1;
  123. }
  124. return ret;
  125. }
  126. static void gt9xx_write(struct rt_i2c_bus_device *i2c_bus, rt_uint16_t addr, rt_uint8_t *buffer, rt_size_t length)
  127. {
  128. rt_uint8_t *send_buffer = rt_malloc(length + 2);
  129. RT_ASSERT(send_buffer);
  130. send_buffer[0] = (rt_uint8_t)(addr >> 8);
  131. send_buffer[1] = (rt_uint8_t)(addr);
  132. memcpy(send_buffer + 2, buffer, length);
  133. struct rt_i2c_msg msgs[] =
  134. {
  135. {
  136. .addr = gt9xx_driver.address,
  137. .flags = RT_I2C_WR,
  138. .len = length + 2,
  139. .buf = send_buffer,
  140. }
  141. };
  142. length = rt_i2c_transfer(i2c_bus, msgs, 1);
  143. rt_free(send_buffer);
  144. send_buffer = RT_NULL;
  145. }
  146. static void gt9xx_isr_enable(rt_bool_t enable)
  147. {
  148. if (enable)
  149. {
  150. gpio_irq_enable( TP_INT_PIN);
  151. }
  152. else
  153. {
  154. gpio_irq_disable( TP_INT_PIN);
  155. }
  156. }
  157. static rt_err_t gt9xx_read_point(touch_msg_t msg)
  158. {
  159. rt_uint8_t buf[8];
  160. rt_uint8_t clean = 0;
  161. static rt_uint8_t s_tp_down = 0;
  162. gt9xx_read(gt9xx_i2c_bus, gt9xx_READ_XY_REG, buf, 8);
  163. gt9xx_write(gt9xx_i2c_bus, gt9xx_CLEARBUF_REG, &clean, 1);
  164. if ((buf[0] & 0x80) == 0)
  165. {
  166. if (s_tp_down)
  167. {
  168. s_tp_down = 0;
  169. msg->event = TOUCH_EVENT_UP;
  170. return RT_EOK;
  171. }
  172. msg->event = TOUCH_EVENT_NONE;
  173. return RT_EOK;
  174. }
  175. msg->x = ((rt_uint16_t)buf[3] << 8) | buf[2];
  176. msg->y = ((rt_uint16_t)buf[5] << 8) | buf[4];
  177. if (s_tp_down)
  178. {
  179. msg->event = TOUCH_EVENT_MOVE;
  180. return RT_EOK;
  181. }
  182. msg->event = TOUCH_EVENT_DOWN;
  183. s_tp_down = 1;
  184. return RT_EOK;
  185. }
  186. void gt9xx_touch_isr(int irq, void *param)
  187. {
  188. gpio_irq_disable(TP_INT_PIN);
  189. rt_sem_release(gt9xx_driver.isr_sem);
  190. }
  191. static void gt9xx_set_address(rt_uint8_t address)
  192. {
  193. pin_set_purpose(TP_INT_PIN, PIN_PURPOSE_OTHER);
  194. pin_set_purpose(TP_RESET_PIN, PIN_PURPOSE_OTHER);
  195. gpio_direction_output( TP_INT_PIN, 0);
  196. gpio_direction_output( TP_RESET_PIN, 0);
  197. if (address == 0x5D)
  198. {
  199. rt_thread_delay(30);
  200. gpio_set_value( TP_RESET_PIN, 1);
  201. rt_thread_delay(300);
  202. pin_set_purpose(TP_INT_PIN, PIN_PURPOSE_OTHER);
  203. gpio_direction_input(TP_INT_PIN);
  204. rt_thread_delay(10);
  205. }
  206. else
  207. {
  208. gpio_set_value( TP_INT_PIN, 1);
  209. gpio_set_value( TP_RESET_PIN, 0);
  210. rt_thread_delay(30);
  211. gpio_set_value( TP_RESET_PIN, 1);
  212. gpio_set_value( TP_INT_PIN, 1);
  213. rt_thread_delay(30);
  214. gpio_set_value( TP_INT_PIN, 0);
  215. rt_thread_delay(30);
  216. gpio_set_value( TP_INT_PIN, 1);
  217. }
  218. }
  219. static void gt9xx_soft_reset(struct rt_i2c_bus_device *i2c_bus)
  220. {
  221. rt_uint8_t buf = 2;
  222. gt9xx_write(i2c_bus, gt9xx_COMMAND_REG, &buf, 1);
  223. }
  224. static void gt9xx_init(struct rt_i2c_bus_device *i2c_bus)
  225. {
  226. rt_uint8_t id = 0;
  227. int touch_irq = LS1C_GPIO_TO_IRQ(TP_INT_PIN);
  228. gt9xx_driver.isr_sem = rt_sem_create("gt9xx", 0, RT_IPC_FLAG_FIFO);
  229. RT_ASSERT(gt9xx_driver.isr_sem);
  230. gt9xx_i2c_bus = i2c_bus;
  231. gt9xx_set_address(gt9xx_driver.address);
  232. gt9xx_read(i2c_bus, gt9xx_CONFIG_VERSION_REG, &id, 1);
  233. rt_kprintf("\r\nGT9xx Config version:0x%02X\r\n", id);
  234. gt9xx_read(i2c_bus, gt9xx_VENDOR_ID_REG, &id, 1);
  235. rt_kprintf("\r\nGT9xx sensor id:0x%02X\r\n", id);
  236. gpio_set_irq_type( TP_INT_PIN, IRQ_TYPE_EDGE_RISING);
  237. gpio_irq_disable( TP_INT_PIN);
  238. rt_hw_interrupt_install(touch_irq, gt9xx_touch_isr, RT_NULL, "touch");
  239. rt_thread_delay(RT_TICK_PER_SECOND / 5);
  240. gpio_init(LED_PIN, gpio_mode_output);
  241. }
  242. static int gt9xx_write_config(void)
  243. {
  244. int i;
  245. rt_uint8_t config_checksum = 0;
  246. gt9xx_set_address(gt9xx_driver.address);
  247. //Add sth...
  248. gt9xx_config[5] = 0x05;
  249. gt9xx_config[6] = 0x0C;
  250. for (i = 0; i < sizeof(gt9xx_config) - 2; i++)
  251. {
  252. config_checksum += gt9xx_config[i];
  253. }
  254. gt9xx_config[184] = (~config_checksum) + 1;
  255. gt9xx_config[185] = 0x01;
  256. gt9xx_write(gt9xx_i2c_bus, gt9xx_CONFIG_REG, gt9xx_config, sizeof(gt9xx_config));
  257. return 0;
  258. }
  259. MSH_CMD_EXPORT(gt9xx_write_config,please read first);
  260. static int gt9xx_read_config(void)
  261. {
  262. int i;
  263. rt_uint8_t buf[8];
  264. rt_uint8_t clean = 0;
  265. gt9xx_read(gt9xx_i2c_bus, gt9xx_CONFIG_VERSION_REG, gt9xx_config, sizeof(gt9xx_config));
  266. gt9xx_config[sizeof(gt9xx_config)-1] = 1;
  267. rt_kprintf("\n");
  268. for(i = 0; i < sizeof(gt9xx_config); i++)
  269. {
  270. rt_kprintf("0x%02X,",gt9xx_config[i]);
  271. if((i+1)%8 == 0)
  272. {
  273. rt_kprintf("\n");
  274. }
  275. }
  276. rt_kprintf("\n");
  277. return 0;
  278. }
  279. MSH_CMD_EXPORT(gt9xx_read_config,read gt9xx config);
  280. static int gt9xx_read_xy(void)
  281. {
  282. int i;
  283. rt_uint8_t buf[8];
  284. rt_uint8_t clean = 0;
  285. rt_uint16_t x,y;
  286. gt9xx_read(gt9xx_i2c_bus, gt9xx_READ_XY_REG, buf, 8);
  287. gt9xx_write(gt9xx_i2c_bus, gt9xx_CLEARBUF_REG, &clean, 1);
  288. x = ((rt_uint16_t)buf[3] << 8) | buf[2];
  289. y = ((rt_uint16_t)buf[5] << 8) | buf[4];
  290. rt_kprintf("\n814e= 0x%02x; 814f= 0x%02x;\n x1 : %d, y1 : %d\n", buf[0], buf[1], x, y);
  291. rt_kprintf("\n");
  292. return 0;
  293. }
  294. MSH_CMD_EXPORT(gt9xx_read_xy,read gt9xx xy);
  295. static rt_bool_t gt9xx_probe(struct rt_i2c_bus_device *i2c_bus)
  296. {
  297. rt_uint8_t buffer[5] = { 0 };
  298. gt9xx_set_address(gt9xx_driver.address);
  299. gt9xx_soft_reset(i2c_bus);
  300. rt_thread_delay(10);
  301. gt9xx_read(i2c_bus, gt9xx_PRODUCT_ID_REG, buffer, 4);
  302. buffer[4] = '\0';
  303. if (buffer[0] == '9' && buffer[1] == '1' && buffer[2] == '1')
  304. {
  305. rt_kprintf("Found chip gt911\r\n");
  306. return RT_TRUE;
  307. }
  308. else if (buffer[0] == '9' && buffer[1] == '1' && buffer[2] == '4' && buffer[3] == '7')
  309. {
  310. rt_kprintf("Found chip gt9147\r\n");
  311. return RT_TRUE;
  312. }
  313. else if (buffer[0] == '9' && buffer[1] == '1' && buffer[2] == '5' && buffer[3] == '7')
  314. {
  315. rt_kprintf("Found chip gt9157\r\n");
  316. return RT_TRUE;
  317. }
  318. else
  319. {
  320. rt_kprintf("Uknow chip :");
  321. rt_kprintf("%d%d%d%d\r\n",buffer[0], buffer[1] , buffer[2], buffer[3]);
  322. return RT_TRUE;
  323. }
  324. return RT_FALSE;
  325. }
  326. static void gt9xx_deinit(void)
  327. {
  328. rt_sem_delete(gt9xx_driver.isr_sem);
  329. }
  330. static int gt9xx_driver_register(void)
  331. {
  332. rt_kprintf("\r\n%s \r\n", __FUNCTION__);
  333. gt9xx_driver.address = 0x5D;
  334. gt9xx_driver.probe = gt9xx_probe;
  335. gt9xx_driver.ops = &gt9xx_ops;
  336. gt9xx_driver.user_data = RT_NULL;
  337. rt_touch_drivers_register(&gt9xx_driver);
  338. return 0;
  339. }
  340. INIT_ENV_EXPORT(gt9xx_driver_register);
  341. void touch_down(void)
  342. {
  343. gpio_set(LED_PIN, gpio_level_low);
  344. }
  345. void touch_mo(void)
  346. {
  347. if (0 == (gpio_get(LED_PIN)))
  348. gpio_set(LED_PIN, gpio_level_high);
  349. else
  350. gpio_set(LED_PIN, gpio_level_low);
  351. }
  352. void touch_up(void)
  353. {
  354. gpio_set(LED_PIN, gpio_level_high);
  355. }
  356. #endif