touch.c 19 KB

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