drv_xpt2046.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-11-08 bigmagic first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "drv_xpt2046.h"
  13. //http://www.lcdwiki.com/MHS-3.5inch_RPi_Display
  14. #define DBG_TAG "xpt2046"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. //XPT2046
  18. #define READ_X (0xD0)
  19. #define READ_Y (0x90)
  20. #define TFT_WIDTH (320)
  21. #define TFT_HEIGHT (480)
  22. //freq
  23. #define TOUCH_SPI_MAX_FREQ (10*1000)
  24. #define TP_IRQ_PIN (17)
  25. #define TOUCH_DEVICE_NAME ("spi0.1")
  26. static struct rt_semaphore touch_ack;
  27. static rt_touch_t touch_device = RT_NULL;
  28. static rt_thread_t touch_tid = RT_NULL;
  29. #define TOUCH_THREAD_STACK_SIZE (1024)
  30. #define TOUCH_THREAD_PRIORITY (30)
  31. #define TOUCH_THREAD_TIMESLICE (10)
  32. rt_uint8_t touch_flag = 0;
  33. rt_uint16_t touch_x_val = 0;
  34. rt_uint16_t touch_y_val = 0;
  35. extern struct rt_semaphore lcd_spi_lock;
  36. static void touch_read_x_y(void *dev, rt_uint16_t *x, rt_uint16_t *y)
  37. {
  38. struct rt_spi_device *touch_dev = (struct rt_spi_device *)dev;
  39. struct rt_spi_message msg1,msg2,msg3,msg4;
  40. rt_uint16_t readx_val = 0,ready_val = 0;
  41. rt_uint8_t readx[2];
  42. rt_uint8_t ready[2];
  43. rt_sem_take(&lcd_spi_lock, RT_WAITING_FOREVER);
  44. int read_x_id = READ_X;
  45. int read_y_id = READ_Y;
  46. msg1.send_buf = &read_x_id;
  47. msg1.recv_buf = RT_NULL;
  48. msg1.length = 1;
  49. msg1.cs_take = 1;
  50. msg1.cs_release = 0;
  51. msg1.next = &msg2;
  52. msg2.send_buf = RT_NULL;
  53. msg2.recv_buf = &readx[0];
  54. msg2.length = 2;
  55. msg2.cs_take = 0;
  56. msg2.cs_release = 0;
  57. msg2.next = &msg3;
  58. msg3.send_buf = &read_y_id;
  59. msg3.recv_buf = RT_NULL;
  60. msg3.length = 1;
  61. msg3.cs_take = 0;
  62. msg3.cs_release = 0;
  63. msg3.next = &msg4;
  64. msg4.send_buf = RT_NULL;
  65. msg4.recv_buf = &ready[0];
  66. msg4.length = 2;
  67. msg4.cs_take = 0;
  68. msg4.cs_release = 1;
  69. msg4.next = RT_NULL;
  70. rt_spi_transfer_message(touch_dev, &msg1);
  71. readx_val = ((readx[0] << 8) | readx[1]) >> 4;
  72. ready_val = ((ready[0] << 8) | ready[1]) >> 4;
  73. rt_sem_release(&lcd_spi_lock);
  74. *x = readx_val;
  75. *y = ready_val;
  76. }
  77. /*
  78. XPT2046:Width:320 High:480
  79. no pressed:(0x800,0xfff)
  80. ---ETH----USB-----------------------
  81. | (0x800,0x800) (0xfff,0x800) |
  82. | |
  83. | (0x800,0xFFF) (0xfff,0xfff) |
  84. ------------------------------------
  85. */
  86. #define XMIN 0x800
  87. #define YMAX 0xfff
  88. void read_tp(void *dev, rt_uint16_t *x, rt_uint16_t *y)
  89. {
  90. struct rt_spi_device *touch_dev = (struct rt_spi_device *)dev;
  91. rt_uint8_t try = 0;
  92. uint16_t _y[5] = {0,0,0,0,0};
  93. uint16_t _x[5] = {0,0,0,0,0};
  94. uint16_t x_val = 0;
  95. uint16_t y_val = 0;
  96. uint16_t cur_x = 0;
  97. uint16_t cur_y = 0;
  98. int index = 0;
  99. while(1)
  100. {
  101. try = try + 1;
  102. touch_read_x_y(touch_dev, x, y);
  103. if((*x > XMIN) && (*y < YMAX))
  104. {
  105. _x[index] = *x;
  106. _y[index] = *y;
  107. index = index + 1;
  108. }
  109. if(index == 5)
  110. {
  111. break;
  112. }
  113. if(try > 10)
  114. {
  115. break;
  116. }
  117. }
  118. x_val = (_x[0] + _x[1] + _x[2] + _x[3]+ _x[4]) / index;
  119. y_val = (_y[0] + _y[1] + _y[2] + _y[3]+ _y[4]) / index;
  120. cur_x = (x_val - 0x800) * TFT_WIDTH / 0x800;
  121. cur_y = (y_val - 0x800) * TFT_HEIGHT / 0x800;
  122. if((cur_x < TFT_WIDTH) && (cur_y < TFT_HEIGHT))
  123. {
  124. *x = TFT_WIDTH - cur_x;
  125. *y = TFT_HEIGHT - cur_y;
  126. }
  127. else
  128. {
  129. *x = 0;
  130. *y = 0;
  131. }
  132. }
  133. static void touch_thread_entry(void *param)
  134. {
  135. rt_uint16_t x,y;
  136. struct rt_spi_device *touch_dev;
  137. touch_dev = (struct rt_spi_device *)rt_device_find(TOUCH_DEVICE_NAME);
  138. touch_dev->config.max_hz = TOUCH_SPI_MAX_FREQ;
  139. if (!touch_dev)
  140. {
  141. rt_kprintf("no %s!\n", TOUCH_DEVICE_NAME);
  142. }
  143. while (1)
  144. {
  145. rt_sem_take(&touch_ack, RT_WAITING_FOREVER);
  146. read_tp(touch_dev, &x, &y);
  147. if((x!= 0) && (y !=0))
  148. {
  149. touch_x_val = x;
  150. touch_y_val = y;
  151. touch_flag = 1;
  152. }
  153. rt_pin_mode(TP_IRQ_PIN, PIN_MODE_INPUT_PULLUP);
  154. }
  155. }
  156. static void touch_readly(void *args)
  157. {
  158. if(rt_pin_read(TP_IRQ_PIN) == PIN_LOW)
  159. {
  160. rt_pin_mode(TP_IRQ_PIN, PIN_MODE_OUTPUT);
  161. rt_pin_write(TP_IRQ_PIN,PIN_HIGH);
  162. rt_sem_release(&touch_ack);
  163. }
  164. }
  165. static rt_ssize_t xpt2046_read_point(struct rt_touch_device *touch, void *buf, rt_size_t read_num)
  166. {
  167. rt_uint16_t* touchxy = (rt_uint16_t *)buf;
  168. if((read_num != 0) && (touch_flag == 1))
  169. {
  170. touchxy[0] = touch_x_val;
  171. touchxy[1] = touch_y_val;
  172. touch_flag = 0;
  173. return read_num;
  174. }
  175. else
  176. {
  177. return 0;
  178. }
  179. }
  180. static rt_err_t xpt2046_control(struct rt_touch_device *device, int cmd, void *data)
  181. {
  182. return RT_EOK;
  183. }
  184. static struct rt_touch_ops touch_ops =
  185. {
  186. .touch_readpoint = xpt2046_read_point,
  187. .touch_control = xpt2046_control,
  188. };
  189. static int hw_xpt2046_touch_init(void)
  190. {
  191. //touch sem
  192. rt_sem_init(&touch_ack, "touch_ack", 0, RT_IPC_FLAG_FIFO);
  193. touch_tid = rt_thread_create("touch",
  194. touch_thread_entry, RT_NULL,
  195. TOUCH_THREAD_STACK_SIZE,
  196. TOUCH_THREAD_PRIORITY, TOUCH_THREAD_TIMESLICE);
  197. if (touch_tid != RT_NULL)
  198. rt_thread_startup(touch_tid);
  199. rt_pin_mode(TP_IRQ_PIN, PIN_MODE_INPUT_PULLUP);
  200. rt_pin_attach_irq(TP_IRQ_PIN, PIN_IRQ_MODE_LOW_LEVEL, touch_readly, RT_NULL);
  201. rt_pin_irq_enable(TP_IRQ_PIN, PIN_IRQ_ENABLE);
  202. touch_device = (rt_touch_t)rt_calloc(1, sizeof(struct rt_touch_device));
  203. if (touch_device == RT_NULL)
  204. return -RT_ERROR;
  205. /* register touch device */
  206. touch_device->info.type = RT_TOUCH_TYPE_RESISTANCE;
  207. touch_device->info.vendor = RT_TOUCH_VENDOR_UNKNOWN;
  208. //rt_memcpy(&touch_device->config, cfg, sizeof(struct rt_touch_config));
  209. touch_device->ops = &touch_ops;
  210. rt_hw_touch_register(touch_device, "xpt2046", RT_DEVICE_FLAG_INT_RX, RT_NULL);
  211. return 0;
  212. }
  213. INIT_DEVICE_EXPORT(hw_xpt2046_touch_init);