touch.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. * 2017-12-30 Sundm75 first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <stdbool.h>
  13. #include <drivers/spi.h>
  14. #include "ls1c.h"
  15. #include "ls1c_gpio.h"
  16. #include "ls1c_spi.h"
  17. #include "drv_spi.h"
  18. #include "touch.h"
  19. #ifdef XPT2046_USING_TOUCH
  20. #include <rtgui/calibration.h>
  21. #include <rtgui/event.h>
  22. #include <rtgui/kbddef.h>
  23. #include <rtgui/rtgui_server.h>
  24. #include <rtgui/rtgui_system.h>
  25. //竖屏幕 不需要 _ILI_HORIZONTAL_DIRECTION_
  26. //横屏幕 需要 _ILI_HORIZONTAL_DIRECTION_
  27. //#define _ILI_HORIZONTAL_DIRECTION_
  28. #if defined(_ILI_HORIZONTAL_DIRECTION_)
  29. #define X_WIDTH 272
  30. #define Y_WIDTH 480
  31. #else
  32. #define X_WIDTH 480
  33. #define Y_WIDTH 272
  34. #endif
  35. /*
  36. TOUCH INT: 84
  37. */
  38. #define IS_TOUCH_UP() gpio_get(TOUCH_INT_PIN)
  39. #define led_gpio 52 // led1指示
  40. #define DUMMY 0x00
  41. /*
  42. 7 6 - 4 3 2 1-0
  43. s A2-A0 MODE SER/DFR PD1-PD0
  44. */
  45. /* bit[1:0] power-down */
  46. #define POWER_MODE0 (0) /* Power-Down Between Conversions. When */
  47. /* each conversion is finished, the converter */
  48. /* enters a low-power mode. At the start of the */
  49. /* next conversion, the device instantly powers up */
  50. /* to full power. There is no need for additional */
  51. /* delays to ensure full operation, and the very first */
  52. /* conversion is valid. The Y? switch is on when in */
  53. /* power-down.*/
  54. #define POWER_MODE1 (1) /* Reference is off and ADC is on. */
  55. #define POWER_MODE2 (2) /* Reference is on and ADC is off. */
  56. #define POWER_MODE3 (3) /* Device is always powered. Reference is on and */
  57. /* ADC is on. */
  58. /* bit[2] SER/DFR */
  59. #define DIFFERENTIAL (0<<2)
  60. #define SINGLE_ENDED (1<<2)
  61. /* bit[3] mode */
  62. #define MODE_12BIT (0<<3)
  63. #define MODE_8BIT (1<<3)
  64. /* bit[6:4] differential mode */
  65. #define MEASURE_X (((1<<2) | (0<<1) | (1<<0))<<4)
  66. #define MEASURE_Y (((0<<2) | (0<<1) | (1<<0))<<4)
  67. #define MEASURE_Z1 (((0<<2) | (1<<1) | (1<<0))<<4)
  68. #define MEASURE_Z2 (((1<<2) | (0<<1) | (0<<0))<<4)
  69. /* bit[7] start */
  70. #define START (1<<7)
  71. /* X Y change. */
  72. #define TOUCH_MSR_X (START | MEASURE_X | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)
  73. #define TOUCH_MSR_Y (START | MEASURE_Y | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)
  74. /* 以下定义XPT2046 的触摸屏位置*/
  75. #if defined(_ILI_HORIZONTAL_DIRECTION_)
  76. #define MIN_X_DEFAULT 2047
  77. #define MAX_X_DEFAULT 47
  78. #define MIN_Y_DEFAULT 102
  79. #define MAX_Y_DEFAULT 1939
  80. #else
  81. #define MIN_X_DEFAULT 47
  82. #define MAX_X_DEFAULT 2047
  83. #define MIN_Y_DEFAULT 1939
  84. #define MAX_Y_DEFAULT 102
  85. #endif
  86. #define SAMP_CNT 8 //the adc array size
  87. #define SAMP_CNT_DIV2 4 //the middle of the adc array
  88. #define SH 10 // Valve value
  89. /*宏定义 */
  90. #define TOUCH_SPI_X SPI1
  91. #define TOUCH_INT_PIN 84
  92. #define TOUCH_CS_PIN 49
  93. #define TOUCH_SCK_PIN 46
  94. #define TOUCH_MISO_PIN 47
  95. #define TOUCH_MOSI_PIN 48
  96. /*创建结构体将需要用到的东西进行打包*/
  97. struct rtgui_touch_device
  98. {
  99. struct rt_device parent; /* 用于注册设备*/
  100. rt_uint16_t x, y; /* 记录读取到的位置值 */
  101. rt_bool_t calibrating; /* 触摸校准标志 */
  102. rt_touch_calibration_func_t calibration_func;/* 触摸函数 函数指针 */
  103. rt_uint16_t min_x, max_x; /* 校准后 X 方向最小 最大值 */
  104. rt_uint16_t min_y, max_y; /* 校准后 Y 方向最小 最大值 */
  105. struct rt_spi_device * spi_device; /* SPI 设备 用于通信 */
  106. struct rt_event event; /* 事件同步,用于“笔中断” */
  107. };
  108. static struct rtgui_touch_device *touch = RT_NULL;
  109. static rt_err_t touch_send_then_recv(struct rt_spi_device *device,
  110. const void *send_buf,
  111. rt_size_t send_length,
  112. void *recv_buf,
  113. rt_size_t recv_length)
  114. {
  115. rt_err_t result;
  116. struct rt_spi_message message;
  117. rt_uint8_t dummy[128] ;
  118. rt_memset(dummy, DUMMY, sizeof(dummy));
  119. RT_ASSERT(device != RT_NULL);
  120. RT_ASSERT(device->bus != RT_NULL);
  121. result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  122. if (result == RT_EOK)
  123. {
  124. if (device->bus->owner != device)
  125. {
  126. /* not the same owner as current, re-configure SPI bus */
  127. result = device->bus->ops->configure(device, &device->config);
  128. if (result == RT_EOK)
  129. {
  130. /* set SPI bus owner */
  131. device->bus->owner = device;
  132. }
  133. else
  134. {
  135. /* configure SPI bus failed */
  136. result = -RT_EIO;
  137. goto __exit;
  138. }
  139. }
  140. /* send data */
  141. message.send_buf = send_buf;
  142. message.recv_buf = RT_NULL;
  143. message.length = send_length;
  144. message.cs_take = 1;
  145. message.cs_release = 0;
  146. message.next = RT_NULL;
  147. result = device->bus->ops->xfer(device, &message);
  148. if (result == 0)
  149. {
  150. result = -RT_EIO;
  151. goto __exit;
  152. }
  153. /* recv data */
  154. message.send_buf = dummy;
  155. message.recv_buf = recv_buf;
  156. message.length = recv_length;
  157. message.cs_take = 0;
  158. message.cs_release = 1;
  159. message.next = RT_NULL;
  160. result = device->bus->ops->xfer(device, &message);
  161. if (result == 0)
  162. {
  163. result = -RT_EIO;
  164. goto __exit;
  165. }
  166. result = RT_EOK;
  167. }
  168. else
  169. {
  170. return -RT_EIO;
  171. }
  172. __exit:
  173. rt_mutex_release(&(device->bus->lock));
  174. return result;
  175. }
  176. static void rtgui_touch_calculate(void)
  177. {
  178. if (touch != RT_NULL)
  179. {
  180. /* read touch */
  181. {
  182. rt_uint8_t i, j, k, min;
  183. rt_uint16_t temp;
  184. rt_uint16_t tmpxy[2][SAMP_CNT];
  185. rt_uint8_t send_buffer[1];
  186. rt_uint8_t recv_buffer[2];
  187. for(i=0; i<SAMP_CNT; i++)
  188. {
  189. send_buffer[0] = TOUCH_MSR_X;
  190. touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
  191. rt_kprintf("touch x: %d ",(recv_buffer[0]*256|recv_buffer[1])>>4);
  192. #if defined(_ILI_HORIZONTAL_DIRECTION_)
  193. tmpxy[1][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
  194. tmpxy[1][i] >>= 4;
  195. #else
  196. tmpxy[0][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
  197. tmpxy[0][i] >>=4;
  198. #endif
  199. send_buffer[0] = TOUCH_MSR_Y;
  200. touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
  201. rt_kprintf("touch y: %d \n",(recv_buffer[0]*256|recv_buffer[1])>>4);
  202. #if defined(_ILI_HORIZONTAL_DIRECTION_)
  203. tmpxy[0][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
  204. tmpxy[0][i] >>= 4;
  205. #else
  206. tmpxy[1][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
  207. tmpxy[1][i] >>= 4;
  208. #endif
  209. }
  210. /*再次打开触摸中断*/
  211. send_buffer[0] = 1 << 7;
  212. touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
  213. touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
  214. /* calculate average */
  215. {
  216. rt_uint32_t total_x = 0;
  217. rt_uint32_t total_y = 0;
  218. for(k=0; k<2; k++)
  219. {
  220. // sorting the ADC value
  221. for(i=0; i<SAMP_CNT-1; i++)
  222. {
  223. min=i;
  224. for (j=i+1; j<SAMP_CNT; j++)
  225. {
  226. if (tmpxy[k][min] > tmpxy[k][j])
  227. min=j;
  228. }
  229. temp = tmpxy[k][i];
  230. tmpxy[k][i] = tmpxy[k][min];
  231. tmpxy[k][min] = temp;
  232. }
  233. //check value for Valve value
  234. if((tmpxy[k][SAMP_CNT_DIV2+1]-tmpxy[k][SAMP_CNT_DIV2-2]) > SH)
  235. {
  236. return;
  237. }
  238. }
  239. total_x=tmpxy[0][SAMP_CNT_DIV2-2]+tmpxy[0][SAMP_CNT_DIV2-1]+tmpxy[0][SAMP_CNT_DIV2]+tmpxy[0][SAMP_CNT_DIV2+1];
  240. total_y=tmpxy[1][SAMP_CNT_DIV2-2]+tmpxy[1][SAMP_CNT_DIV2-1]+tmpxy[1][SAMP_CNT_DIV2]+tmpxy[1][SAMP_CNT_DIV2+1];
  241. //calculate average value
  242. touch->x=total_x>>2;
  243. touch->y=total_y>>2;
  244. rt_kprintf("touch->x:%d touch->y:%d\r\n", touch->x, touch->y);
  245. } /* calculate average */
  246. } /* read touch */
  247. /* if it's not in calibration status */
  248. /*触摸值缩放*/
  249. if (touch->calibrating != RT_TRUE)
  250. {
  251. if (touch->max_x > touch->min_x)
  252. {
  253. touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x);
  254. }
  255. else
  256. {
  257. touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x);
  258. }
  259. if (touch->max_y > touch->min_y)
  260. {
  261. touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y);
  262. }
  263. else
  264. {
  265. touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y);
  266. }
  267. }
  268. }
  269. }
  270. #include "ls1c_regs.h"
  271. #define TOUCH_INT_EN __REG32(LS1C_INT4_EN)
  272. rt_inline void touch_int_cmd(rt_bool_t NewState)
  273. {
  274. if(NewState == RT_TRUE)
  275. {
  276. //TOUCH_INT_EN |= (1<<(TOUCH_INT_PIN-64));
  277. reg_set_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
  278. }
  279. else
  280. {
  281. //TOUCH_INT_EN &=(~ (1<<(TOUCH_INT_PIN-64)));
  282. reg_clr_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
  283. }
  284. }
  285. void ls1c_touch_irqhandler(void) /* TouchScreen */
  286. {
  287. if(gpio_get(TOUCH_INT_PIN)==0)
  288. {
  289. /* 触摸屏按下后操作 */
  290. if (gpio_level_low == gpio_get(led_gpio))
  291. gpio_set(led_gpio, gpio_level_high);
  292. else
  293. gpio_set(led_gpio, gpio_level_low);
  294. touch_int_cmd(RT_FALSE);
  295. rt_event_send(&touch->event, 1);
  296. }
  297. }
  298. /*管脚初始化,配置中断打开SPI1 CS0 设备*/
  299. rt_inline void touch_init(void)
  300. {
  301. unsigned int touch_int_gpio = TOUCH_INT_PIN; // 触摸屏中断
  302. int touch_irq = LS1C_GPIO_TO_IRQ(touch_int_gpio);
  303. // 初始化按键中断
  304. gpio_set_irq_type(touch_int_gpio, IRQ_TYPE_EDGE_FALLING);
  305. rt_hw_interrupt_install(touch_irq, ls1c_touch_irqhandler, RT_NULL, "touch");
  306. rt_hw_interrupt_umask(touch_irq);
  307. gpio_init(touch_int_gpio, gpio_mode_input);
  308. // 初始化led
  309. gpio_init(led_gpio, gpio_mode_output);
  310. gpio_set(led_gpio, gpio_level_high);
  311. }
  312. /* RT-Thread Device Interface */
  313. static rt_err_t rtgui_touch_init (rt_device_t dev)
  314. {
  315. rt_uint8_t send;
  316. rt_uint8_t recv_buffer[2];
  317. struct rtgui_touch_device * touch_device = (struct rtgui_touch_device *)dev;
  318. touch_init();
  319. rt_kprintf("touch_init ...\n");
  320. send = START | DIFFERENTIAL | POWER_MODE0;
  321. touch_send_then_recv(touch->spi_device, &send, 1, recv_buffer, 2);
  322. return RT_EOK;
  323. }
  324. static rt_err_t rtgui_touch_control (rt_device_t dev, int cmd, void *args)
  325. {
  326. switch (cmd)
  327. {
  328. case RT_TOUCH_CALIBRATION:
  329. touch->calibrating = RT_TRUE;
  330. touch->calibration_func = (rt_touch_calibration_func_t)args;
  331. break;
  332. case RT_TOUCH_NORMAL:
  333. touch->calibrating = RT_FALSE;
  334. break;
  335. case RT_TOUCH_CALIBRATION_DATA:
  336. {
  337. struct calibration_data* data;
  338. data = (struct calibration_data*) args;
  339. //update
  340. touch->min_x = data->min_x;
  341. touch->max_x = data->max_x;
  342. touch->min_y = data->min_y;
  343. touch->max_y = data->max_y;
  344. }
  345. break;
  346. }
  347. return RT_EOK;
  348. }
  349. void _set_mouse_position(rt_uint32_t X, rt_uint32_t Y)
  350. {}
  351. static void touch_thread_entry(void *parameter)
  352. {
  353. rt_bool_t touch_down = RT_FALSE;
  354. rt_uint32_t event_value;
  355. struct rtgui_event_mouse emouse;
  356. static struct _touch_previous
  357. {
  358. rt_uint32_t x;
  359. rt_uint32_t y;
  360. } touch_previous;
  361. RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
  362. emouse.wid = RT_NULL;
  363. while(1)
  364. {
  365. /* 接收到触摸中断事件 */
  366. if(rt_event_recv(&touch->event,
  367. 1,
  368. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  369. 100,
  370. &event_value)
  371. == RT_EOK)
  372. {
  373. while(1)
  374. {
  375. if (IS_TOUCH_UP())
  376. {
  377. /* 触摸笔抬起 */
  378. /* touch up */
  379. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP);
  380. /* use old value */
  381. emouse.x = touch->x;
  382. emouse.y = touch->y;
  383. if(touch_down != RT_TRUE)
  384. {
  385. touch_int_cmd(RT_TRUE);
  386. break;
  387. }
  388. if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
  389. {
  390. /* 触摸校准处理 */
  391. /* callback function */
  392. touch->calibration_func(emouse.x, emouse.y);
  393. }
  394. else
  395. {
  396. /* 向ui发送触摸坐标 */
  397. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  398. }
  399. rt_kprintf("touch up: (%d, %d)\n", emouse.x, emouse.y);
  400. /* clean */
  401. touch_previous.x = touch_previous.y = 0;
  402. touch_down = RT_FALSE;
  403. touch_int_cmd(RT_TRUE);
  404. break;
  405. } /* touch up */
  406. else /* touch down or move */
  407. {
  408. if(touch_down == RT_FALSE)
  409. {
  410. rt_thread_delay(RT_TICK_PER_SECOND / 10);
  411. }
  412. else
  413. {
  414. rt_thread_delay(5);
  415. }
  416. if(IS_TOUCH_UP()) continue;
  417. /* calculation */
  418. rtgui_touch_calculate();
  419. /* send mouse event */
  420. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  421. emouse.parent.sender = RT_NULL;
  422. emouse.x = touch->x;
  423. emouse.y = touch->y;
  424. _set_mouse_position(emouse.x, emouse.y);
  425. /* 光标跟随 */
  426. /* init mouse button */
  427. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN);
  428. /* send event to server */
  429. if (touch->calibrating != RT_TRUE)
  430. {
  431. #define previous_keep 8
  432. /* filter. */
  433. if((touch_previous.x > touch->x + previous_keep)
  434. || (touch_previous.x < touch->x - previous_keep)
  435. || (touch_previous.y > touch->y + previous_keep)
  436. || (touch_previous.y < touch->y - previous_keep))
  437. {
  438. touch_previous.x = touch->x;
  439. touch_previous.y = touch->y;
  440. /* 向ui发送触摸坐标 */
  441. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  442. if(touch_down == RT_FALSE)
  443. {
  444. touch_down = RT_TRUE;
  445. rt_kprintf("touch down: (%d, %d)\n", emouse.x, emouse.y);
  446. }
  447. else
  448. {
  449. rt_kprintf("touch motion: (%d, %d)\n", emouse.x, emouse.y);
  450. }
  451. }
  452. }
  453. else
  454. {
  455. touch_down = RT_TRUE;
  456. }
  457. } /* touch down or move */
  458. } /* read touch */
  459. } /* event recv */
  460. } /* thread while(1) */
  461. }
  462. rt_err_t rtgui_touch_hw_init(const char * spi_device_name)
  463. {
  464. rt_uint32_t arg[2];
  465. struct rt_device * spi_device;
  466. struct rt_thread * touch_thread;
  467. rt_err_t err;
  468. rt_kprintf("spi1 cs0 start...\n");
  469. spi_device = rt_device_find("spi10");
  470. if(spi_device == RT_NULL)
  471. {
  472. rt_kprintf("Did not find spi1, exit thread....\n");
  473. return;
  474. }
  475. err = rt_device_open(spi_device, RT_DEVICE_OFLAG_RDWR);
  476. if(err != RT_EOK)
  477. {
  478. rt_kprintf("Open spi1 failed %08X, exit thread....\n", err);
  479. return;
  480. }
  481. /* config spi */
  482. {
  483. struct rt_spi_configuration cfg;
  484. cfg.data_width = 8;
  485. cfg.mode = RT_SPI_MODE_0;
  486. cfg.max_hz = 200 * 1000; /* 200K */
  487. rt_spi_configure((struct rt_spi_device *)spi_device, &cfg);
  488. }
  489. touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device));
  490. if (touch == RT_NULL) return RT_ENOMEM; /* no memory yet */
  491. /* clear device structure */
  492. rt_memset(&(touch->parent), 0, sizeof(struct rt_device));
  493. rt_event_init(&touch->event, "touch", RT_IPC_FLAG_FIFO);
  494. touch->spi_device = (struct rt_spi_device *)spi_device;
  495. touch->calibrating = false;
  496. touch->min_x = MIN_X_DEFAULT;
  497. touch->max_x = MAX_X_DEFAULT;
  498. touch->min_y = MIN_Y_DEFAULT;
  499. touch->max_y = MAX_Y_DEFAULT;
  500. /* init device structure */
  501. touch->parent.type = RT_Device_Class_Miscellaneous;
  502. touch->parent.init = rtgui_touch_init;
  503. touch->parent.control = rtgui_touch_control;
  504. touch->parent.user_data = RT_NULL;
  505. /* register touch device to RT-Thread */
  506. rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR);
  507. touch_thread = rt_thread_create("touch_thread",
  508. touch_thread_entry, RT_NULL,
  509. 4096, RTGUI_SVR_THREAD_PRIORITY-1, 1);
  510. if (touch_thread != RT_NULL) rt_thread_startup(touch_thread);
  511. rt_device_init((rt_device_t)touch);
  512. return RT_EOK;
  513. }
  514. #endif